<?
include_once("./_common.php");

//$wr = sql_fetch(" select * from $write_table where wr_id = '$wr_id' ");

if ($is_admin == "super") // ÃÖ°í°ü¸®ÀÚ Åë°ú
    ;
else if ($is_admin == "group") { // ±×·ì°ü¸®ÀÚ
    $mb = get_member($write[mb_id]);
    if ($member[mb_id] != $group[gr_admin]) // ÀÚ½ÅÀÌ °ü¸®ÇÏ´Â ±×·ìÀÎ°¡?
        alert("ÀÚ½ÅÀÌ °ü¸®ÇÏ´Â ±×·ìÀÇ °Ô½ÃÆÇÀÌ ¾Æ´Ï¹Ç·Î »èÁ¦ÇÒ ¼ö ¾ø½À´Ï´Ù.");
    else if ($member[mb_level] < $mb[mb_level]) // ÀÚ½ÅÀÇ ·¹º§ÀÌ Å©°Å³ª °°´Ù¸é Åë°ú
        alert("ÀÚ½ÅÀÇ ±ÇÇÑº¸´Ù ³ôÀº ±ÇÇÑÀÇ È¸¿øÀÌ ÀÛ¼ºÇÑ ±ÛÀº »èÁ¦ÇÒ ¼ö ¾ø½À´Ï´Ù.");
} else if ($is_admin == "board") { // °Ô½ÃÆÇ°ü¸®ÀÚÀÌ¸é
    $mb = get_member($write[mb_id]);
    if ($member[mb_id] != $board[bo_admin]) // ÀÚ½ÅÀÌ °ü¸®ÇÏ´Â °Ô½ÃÆÇÀÎ°¡?
        alert("ÀÚ½ÅÀÌ °ü¸®ÇÏ´Â °Ô½ÃÆÇÀÌ ¾Æ´Ï¹Ç·Î »èÁ¦ÇÒ ¼ö ¾ø½À´Ï´Ù.");
    else if ($member[mb_level] < $mb[mb_level]) // ÀÚ½ÅÀÇ ·¹º§ÀÌ Å©°Å³ª °°´Ù¸é Åë°ú
        alert("ÀÚ½ÅÀÇ ±ÇÇÑº¸´Ù ³ôÀº ±ÇÇÑÀÇ È¸¿øÀÌ ÀÛ¼ºÇÑ ±ÛÀº »èÁ¦ÇÒ ¼ö ¾ø½À´Ï´Ù.");
} else if ($member[mb_id]) {
    if ($member[mb_id] != $write[mb_id])
        alert("ÀÚ½ÅÀÇ ±ÛÀÌ ¾Æ´Ï¹Ç·Î »èÁ¦ÇÒ ¼ö ¾ø½À´Ï´Ù.");
} else {
    if ($write[mb_id])
        alert("·Î±×ÀÎ ÈÄ »èÁ¦ÇÏ¼¼¿ä.", "./login.php?url=".urlencode("./board.php?bo_table=$bo_table&wr_id=$wr_id"));
    else if (sql_password($wr_password) != $write[wr_password])
        alert("ÆÐ½º¿öµå°¡ Æ²¸®¹Ç·Î »èÁ¦ÇÒ ¼ö ¾ø½À´Ï´Ù."); 
}

$len = strlen($write[wr_reply]);
if ($len < 0) $len = 0; 
$reply = substr($write[wr_reply], 0, $len);

// ¿ø±Û¸¸ ±¸ÇÑ´Ù.
$sql = " select count(*) as cnt from $write_table
          where wr_reply like '$reply%'
            and wr_id <> '$write[wr_id]'
            and wr_num = '$write[wr_num]'
            and wr_is_comment = 0 ";
$row = sql_fetch($sql);
if ($row[cnt] && !$is_admin)
    alert("ÀÌ ±Û°ú °ü·ÃµÈ ´äº¯±ÛÀÌ Á¸ÀçÇÏ¹Ç·Î »èÁ¦ ÇÒ ¼ö ¾ø½À´Ï´Ù.\\n\\n¿ì¼± ´äº¯±ÛºÎÅÍ »èÁ¦ÇÏ¿© ÁÖ½Ê½Ã¿À.");

// ÄÚ¸àÆ® ´Þ¸° ¿ø±ÛÀÇ »èÁ¦ ¿©ºÎ
$sql = " select count(*) as cnt from $write_table
          where wr_parent = '$wr_id'
            and mb_id <> '$member[mb_id]'
            and wr_is_comment = 1 ";
$row = sql_fetch($sql);
if ($row[cnt] >= $board[bo_count_delete] && !$is_admin)
    alert("ÀÌ ±Û°ú °ü·ÃµÈ ÄÚ¸àÆ®°¡ Á¸ÀçÇÏ¹Ç·Î »èÁ¦ ÇÒ ¼ö ¾ø½À´Ï´Ù.\\n\\nÄÚ¸àÆ®°¡ {$board[bo_count_delete]}°Ç ÀÌ»ó ´Þ¸° ¿ø±ÛÀº »èÁ¦ÇÒ ¼ö ¾ø½À´Ï´Ù.");


// »ç¿ëÀÚ ÄÚµå ½ÇÇà
@include_once("$board_skin_path/delete.skin.php");

// ³ª¶ó¿À¸§´Ô ¼öÁ¤ : ¿ø±Û°ú ÄÚ¸àÆ®¼ö°¡ Á¤»óÀûÀ¸·Î ¾÷µ¥ÀÌÆ® µÇÁö ¾Ê´Â ¿À·ù¸¦ Àâ¾Æ ÁÖ¼Ì½À´Ï´Ù.
//$sql = " select wr_id, mb_id, wr_comment from $write_table where wr_parent = '$write[wr_id]' order by wr_id ";

//³»¿ëº¸±â¿¡¼­ ±Û»èÁ¦½Ã ÈÞÁöÅëÀ¸·Î º¸³»¾î ÀÓ½ÃÀúÀåÇÏ±â À§ÇØ¼­ ¾Æ·¡ÀÇ ¿øº»¶óÀÎÀ» ¼öÁ¤ÇÔ
//¾Æ·¡ÀÇ ¶óÀÎÀÌ ¿øº»ÀÌ¸ç ÈÞÁöÅë ¶§¹®¿¡ ÁÖ¼®Ã³¸®ÇÔ  -Çæ·©ÀÌ-
//$sql = " select wr_id, mb_id, wr_is_comment from $write_table where wr_parent = '$write[wr_id]' order by wr_id ";
//ÈÞÁöÅë ¶§¹®¿¡ »èÁ¦ÇÏ°íÀÚ ÇÏ´Â ÇØ´ç ±ÛÀÇ  ÀüÃ¼ µ¥ÀÌÅ¸¸¦ ºÒ·¯µéÀÓ	  -Çæ·©ÀÌ-
$sql = " select * from $write_table where wr_parent = '$write[wr_id]' order by wr_id "; 

$result = sql_query($sql);
while ($row = sql_fetch_array($result)) 
{
    // ¿ø±ÛÀÌ¶ó¸é
    if (!$row[wr_is_comment]) 
    {
        // ¿ø±Û Æ÷ÀÎÆ® »èÁ¦
        if (!delete_point($row[mb_id], $bo_table, $row[wr_id], '¾²±â'))
            insert_point($row[mb_id], $board[bo_write_point] * (-1), "$board[bo_subject] $row[wr_id] ±Û»èÁ¦");


// ÇöÀç ³»¿ëº¸±â »óÅÂ¿¡¼­ ±Û»èÁ¦½Ã ÁöÁ¤ÇÑ ÈÞÁöÅë °Ô½ÃÆÇÀ¸·Î º¹»çÇÔ -Çæ·©ÀÌ-
// Ã·ºÎÆÄÀÏÀ» º¹»çÇÏ±â À§ÇØ¼­ À§Ä¡»ó ¿©±â·ÎÇÔ

   $dic_ids ="sub_and";  // ÈÞÁöÅëÀ¸·Î »ç¿ëÇÒ °Ô½ÃÆÇ¸íÀ» Àû¾îÁÖ¼¼¿ä. bo_table ¸í
   $src_dir = "$g4[path]/data/file/$bo_table"; // ¿øº» Ã·ºÎÆÄÀÏ µð·ºÅä¸®
   $dst_dir = "$g4[path]/data/file/$dic_ids"; // º¹»çº»(ÈÞÁöÅë) Ã·ºÎÆÄÀÏ µð·ºÅä¸®
 // ±Û»èÁ¦½Ã »èÁ¦ÇÏ´Â È¸¿øÀÇ Á¤º¸¸¦ ¸ðµÎ ±â·ÏÇÔ	 -¾ðÁ¦-¾îµð¼­-´©°¡ (°Ô½ÃÆÇ¸í,¾ÆÀÌµð,ÀÌ¸§,´Ð³Û,¾ÆÀÌÇÇ µî)
   $row[wr_content]=addslashes($row[wr_content]);
   $row[wr_content] .= "\n* ".$bo_table." (".$board[bo_subject].")°Ô½ÃÆÇÀ¸·Î ºÎÅÍ (".$g4[time_ymdhis].") ÀÏ¿¡ »èÁ¦µÊ";
   $row[wr_content] .= "\n* »èÁ¦ÀÚ: (ÀÌ¸§: ".$member[mb_id]." ¤Ó ´Ð³×ÀÓ: ".$member[mb_nick]." ¤Ó ¾ÆÀÌµð: ".$member[mb_id]." ¤Ó ¾ÆÀÌÇÇ: ".$_SERVER[REMOTE_ADDR].") ¿¡ ÀÇÇØ¼­ »èÁ¦µÊ";
  // ÈÞÁöÅëÀÇ °Ô½ÃÆÇ¸íÀ» ¿Ï¼ºÇÏ°í
   $bo_table_name ="".$g4[write_prefix]."".$dic_ids.""; 
  // ¿ä°Ô ¾øÀ¸¸é °Ô½ÃÆÇ ¸®½ºÆ®°¡ µÚÁ×¹ÚÁ× ¤»¤» ±ÍÃ®°Ô½Ã¸®  
   $wr_num = get_next_num($bo_table_name);
  // ÈÞÁöÅëÀ¸·Î ¿ä¶¥
   $sql = " insert into g4_write_$dic_ids
                set wr_num = '$wr_num',
                    wr_reply = '$row[wr_reply]',
                    wr_comment = 0,
                    ca_name = '$row[ca_name]',
                    wr_option = '',
                    wr_subject = '$row[wr_subject]',
                    wr_content = '$row[wr_content]',
                    wr_link1 = '$row[wr_link1]',
                    wr_link2 = '$row[wr_link2]',
                    wr_link1_hit = 0,
                    wr_link2_hit = 0,
                    wr_trackback = '$row[wr_trackback]',
                    wr_hit = 0,
                    wr_good = 0,
                    wr_nogood = 0,
                    mb_id = '$row[mb_id]',
                    wr_password = '$row[wr_password]',
                    wr_name = '$row[wr_name]',
                    wr_email = '$row[wr_email]',
                    wr_homepage = '$row[wr_homepage]',
                    wr_datetime = '$row[wr_datetime]',
                    wr_ip = '$row[wr_ip]',
                    wr_1 = '$row[wr_1]',
                    wr_2 = '$row[wr_2]',
                    wr_3 = '$row[wr_3]',
                    wr_4 = '$row[wr_4]',
                    wr_5 = '$row[wr_5]',
                    wr_6 = '$row[wr_6]',
                    wr_7 = '$row[wr_7]',
                    wr_8 = '$row[wr_8]',
                    wr_9 = '$row[wr_9]',
                    wr_10 = '$row[wr_10]' ";
    sql_query($sql);

    $wr_id = mysql_insert_id();

    // ºÎ¸ð ¾ÆÀÌµð¿¡ UPDATE
    sql_query(" update g4_write_$dic_ids set wr_parent = '$wr_id' where wr_id = '$wr_id' ");
	// °Ô½Ã±Û 1 Áõ°¡
    sql_query("update $g4[board_table] set bo_count_write = bo_count_write + 1 where bo_table = '$dic_ids'");
	// ÇöÀç »èÁ¦ ´çÇÏ´Â ±ÛÀÇ Ã·ºÎÆÄÀÏÀÌ ¸î°³ÀÖ³ª?
	  $save_parent = $wr_id;
      $sql3 = " select * from $g4[board_file_table] where bo_table = '$bo_table' and wr_id = '$row[wr_id]' order by bf_no ";
      $result3 = sql_query($sql3);
      for ($k=0; $row3 = sql_fetch_array($result3); $k++) 
          {
        if ($row3[bf_file]) 
          {
   // »èÁ¦ÇÏ´Â ±Û¿¡¼­ Ã·ºÎ ÆÄÀÏÀÌ ÀÖ´Ù¸é  Ã·ºÎÆÄÀÏÀ» ÈÞÁöÅëÀ¸·Î º¹»çÇÏ°í ÆÛ¹Ì¼ÇÀ» º¯°æÇÔ
         @copy("$src_dir/$row3[bf_file]", "$dst_dir/$row3[bf_file]");
         @chmod("$dst_dir/$row3[bf_file]", 0606);
           }

           $sql = " insert into $g4[board_file_table] 
               set bo_table = '$dic_ids', 
                   wr_id = '$save_parent', 
                   bf_no = '$row3[bf_no]', 
                   bf_source = '$row3[bf_source]', 
                   bf_file = '$row3[bf_file]', 
                   bf_download = '$row3[bf_download]', 
                   bf_content = '$row3[bf_content]',
                   bf_filesize = '$row3[bf_filesize]',
                   bf_width = '$row3[bf_width]',
                   bf_height = '$row3[bf_height]',
                   bf_type = '$row3[bf_type]',
                   bf_datetime = '$row3[bf_datetime]' ";
                   sql_query($sql);

}
//ÈÞÁöÅë¿¡´Â ÄÚ¸àÆ®¿Í ÃÖ±Ù°Ô½Ã¹°Àº ±â·Ï¾ÈÇÔ
//¿©±â±îÁö ±Û»èÁ¦½Ã ÇöÀç »èÁ¦ ´çÇÏ´Â ±Û°ú Ã·ºÎ ÆÄÀÏÀ» ÈÞÁöÅëÀ¸·Î º¹»çÇÔ -Çæ·©ÀÌ-



        // ¾÷·ÎµåµÈ ÆÄÀÏÀÌ ÀÖ´Ù¸é ÆÄÀÏ»èÁ¦
        $sql2 = " select * from $g4[board_file_table] where bo_table = '$bo_table' and wr_id = '$row[wr_id]' ";
        $result2 = sql_query($sql2);
        while ($row2 = sql_fetch_array($result2))
            @unlink("$g4[path]/data/file/$bo_table/$row2[bf_file]");
            
        // ÆÄÀÏÅ×ÀÌºí Çà »èÁ¦
        sql_query(" delete from $g4[board_file_table] where bo_table = '$bo_table' and wr_id = '$row[wr_id]' ");

        $count_write++;
    } 
    else 
    {
        // ÄÚ¸àÆ® Æ÷ÀÎÆ® »èÁ¦
        if (!delete_point($row[mb_id], $bo_table, $row[wr_id], 'ÄÚ¸àÆ®'))
            insert_point($row[mb_id], $board[bo_comment_point] * (-1), "$board[bo_subject] {$write[wr_id]}-{$row[wr_id]} ÄÚ¸àÆ®»èÁ¦");

        $count_comment++;
    }

}

// °Ô½Ã±Û »èÁ¦
sql_query(" delete from $write_table where wr_parent = '$write[wr_id]' ");

// ÃÖ±Ù°Ô½Ã¹° »èÁ¦
sql_query(" delete from $g4[board_new_table] where bo_table = '$bo_table' and wr_parent = '$write[wr_id]' ");

// ½ºÅ©·¦ »èÁ¦
sql_query(" delete from $g4[scrap_table] where bo_table = '$bo_table' and wr_id = '$write[wr_id]' ");

// °øÁö»çÇ× »èÁ¦
$notice_array = explode("\n", trim($board[bo_notice]));
$bo_notice = "";
for ($k=0; $k<count($notice_array); $k++)
    if ((int)$write[wr_id] != (int)$notice_array[$k])
        $bo_notice .= $notice_array[$k] . "\n";
$bo_notice = trim($bo_notice);
sql_query(" update $g4[board_table] set bo_notice = '$bo_notice' where bo_table = '$bo_table' ");

// ±Û¼ýÀÚ °¨¼Ò
if ($count_write > 0 || $count_comment > 0)
    sql_query(" update $g4[board_table] set bo_count_write = bo_count_write - '$count_write', bo_count_comment = bo_count_comment - '$count_comment' where bo_table = '$bo_table' ");

goto_url("./board.php?bo_table=$bo_table&page=$page" . $qstr);
?>
