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

if ($is_admin)
{
    if (!($token && get_session("ss_delete_token") == $token)) 
        alert("数据验证错误，删除失败");
}

//$wr = sql_fetch(" select * from $write_table where wr_id = '$wr_id' ");

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

if ($is_admin == "super") // 网站管理员验证通过
    ;
else if ($is_admin == "group") { // 版块管理员验证通过
    $mb = get_member($write[mb_id]);
    if ($member[mb_id] != $group[gr_admin]) // 检查是否属于自己管理版块
        alert("管理权限错误，不能进行管理操作 Code:1000");
    else if ($member[mb_level] < $mb[mb_level]) // 检查是否越权
        alert("管理权限错误，不能进行管理操作 Code:1001"); 
} else if ($is_admin == "board") { // 检查是否是论坛管理员
    $mb = get_member($write[mb_id]);
    if ($member[mb_id] != $board[bo_admin]) // 检查是否属于自己的论坛
        alert("管理权限错误，不能进行管理操作 Code:1000");
    else if ($member[mb_level] < $mb[mb_level]) // 检查是否越权
        alert("管理权限错误，不能进行管理操作 Code:1001");
} 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 ";
$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' ");

// 减少计数
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' ");

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

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