<?
include_once("./_common.php");

// 4.11
@include_once("$board_skin_path/delete_all.head.skin.php");

$count_write = 0;
$count_comment = 0;

$tmp_array = array();
if ($wr_id) // 选择删除
    $tmp_array[0] = $wr_id;
else // 批量删除
    $tmp_array = $_POST[chk_wr_id];


// 执行
@include_once("$board_skin_path/delete_all.skin.php");


// 为了删除回复，所以倒序读取
for ($i=count($tmp_array)-1; $i>=0; $i--) 
{
    $write = sql_fetch(" select * from $write_table where wr_id = '{$tmp_array[$i]}' ");

    if ($is_admin == "super") // 管理员通过
        ;
    else if ($is_admin == "group") // 版块管理员通过
    {
        $mb = get_member($write[mb_id]);
        if ($member[mb_id] == $group[gr_admin]) // 判断是否越权
        {
            if ($member[mb_level] >= $mb[mb_level]) // 等级检查
                ;
            else
                continue;
        } 
        else
            continue;
    } 
    else if ($is_admin == "board") // 版主
    {
        $mb = get_member($write[mb_id]);
        if ($member[mb_id] == $board[bo_admin]) // 是否属于自己管理
            if ($member[mb_level] >= $mb[mb_level]) // 等级检查
                ;
            else
                continue;
        else
            continue;
    } 
    else if ($member[mb_id] && $member[mb_id] == $write[mb_id]) // 自己是作者
    {
        ;
    } 
    else if ($wr_password && !$write[mb_id] && sql_password($wr_password) == $write[wr_password]) // 密码确认
    {
        ;
    } 
    else
        continue;   // 剩余禁止删除

    $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])
            continue;

    //修正 原文与回复编号不一致的bug
    //$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 ";
    $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] 主题被删");

            // 附件
            $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' ");
    $board[bo_notice] = $bo_notice;
}

// 减少字节
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' ");

// 4.11
@include_once("$board_skin_path/delete_all.tail.skin.php");

goto_url("/bbs/board.php?bo_table=$bo_table&page=$page" . $qstr);
?>
