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

set_session('ss_bo_table', $_REQUEST['bo_table']);
set_session('ss_wr_id', $_REQUEST['wr_id']);

// 090713
if (!$board[bo_table])
{
    if ($cwin) // 查看回复
       alert_close("您访问的论坛不存在或已被关闭", $g4[path]);
    else
       alert("您访问的论坛不存在或已被关闭", $g4[path]);
}

if (!$bo_table)
    alert("bo_table 数值传递失败！\\n\\nwrite.php?bo_table=code 方式传递", $g4[path]);

@include_once ("$g4[path]/skin/board/write.head.skin.php");
@include_once ("$board_skin_path/write.head.skin.php");

$notice_array = explode("\n", trim($board[bo_notice]));

if ($w == "")
{
    if (isset($wr_id))
        alert("主题尚未设定 \$wr_id值", "$g4[bbs_path]/board.php?bo_table=$bo_table");

    if ($member[mb_level] < $board[bo_write_level]) {
        if ($member[mb_id])
            alert("您没有发表主题权限！");
        else
            alert("您没有发表主题权限！\\n\\n如果是会员请您登录后使用！", "/bbs/login.php?$qstr&url=".urlencode("$_SERVER[PHP_SELF]?bo_table=$bo_table"));
    }

    /*
    if ($member[mb_point] + $board[bo_write_point] < 0 && !$is_admin)
        alert("您当前拥有的积分(".number_format($member[mb_point]).")不足支付发表主题所需(".number_format($board[bo_write_point]).")积分\\n\\n请充值或赚取足够积分后重试");
    */

    // 去除负数的 true 
    //$tmp_point = $member[mb_point] ? $member[mb_point] : 0;
    $tmp_point = ($member[mb_point] > 0) ? $member[mb_point] : 0;
    if ($tmp_point + $board[bo_write_point] < 0 && !$is_admin)
        alert("您当前拥有的积分(".number_format($member[mb_point]).")不足支付发表主题需要的(".number_format($board[bo_write_point]).")积分\\n\\n请您充值或赚取足够积分后重试！");

    $title_msg = "发表主题";
}
else if ($w == "u")
{
    // JXL 1.00 : 
    //if ($member[mb_level] < $board[bo_write_level]) {
    if($member['mb_id'] && $write['mb_id'] == $member['mb_id'])
        ;
    else if ($member[mb_level] < $board[bo_write_level]) {
        if ($member[mb_id])
            alert("您没有编辑权限！");
        else
            alert("您没有编辑权限！\\n\\n如果是会员请您登录后使用！", "/bbs/login.php?$qstr&url=".urlencode("$_SERVER[PHP_SELF]?bo_table=$bo_table"));
    }

    $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("此文已被引用！无法进行编辑或删除！");

    // 回复权限
    $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_modify] && !$is_admin)
        alert("此文已有关联回复，无法进行编辑操作\\n\\n本版块设定超过{$board[bo_count_modify]}条以上回复的主题禁止修改");

    $title_msg = "编辑";
}
else if ($w == "r")
{
    if ($member[mb_level] < $board[bo_reply_level]) {
        if ($member[mb_id])
            alert("您没有回复权限！");
        else
            alert("您没有回复权限！\\n\\n如果是会员请您登录后使用！", "/bbs/login.php?$qstr&url=".urlencode("$_SERVER[PHP_SELF]?bo_table=$bo_table"));
    }

    /*
    if ($member[mb_point] + $board[bo_comment_point] < 0)
        alert("您当前拥有的积分(".number_format($member[mb_point]).")不足以支付回复所需的(".number_format($board[bo_comment_point]).")积分\\n\\n请充值积分或赚取足够的积分后重试！");
    */

    $tmp_point = $member[mb_point] ? $member[mb_point] : 0;
    if ($tmp_point + $board[bo_write_point] < 0 && !$is_admin)
        alert("您当前拥有的积分(".number_format($member[mb_point]).")不足以支付回复所需的(".number_format($board[bo_comment_point]).")积分\\n\\n请充值积分或赚取足够的积分后重试！");

    //if (preg_match("/[^0-9]{0,1}{$wr_id}[\r]{0,1}/",$board[bo_notice]))
    if (in_array((int)$wr_id, $notice_array))
        alert("公告禁止发表回复内容！");

    //----------
    // 4.06.13 : 修正他人可以访问加密能容的漏洞
    // 回复
    if ($write[wr_is_comment])
        alert("访问出错！请刷新或稍后再试！");

    // 检查是否加密
    if (strstr($write[wr_option], "secret")) {
        if ($write[mb_id]) {
            // 只有加密主题作者及管理员可以查看
            if (!($write[mb_id] == $member[mb_id] || $is_admin))
                alert("加密主题只有作者本人或管理员才能发表回复！");
        } else {
            // 游客不能对加密内容进行回复
            if (!$is_admin)
                alert("游客不能对加密主题进行回复");
        }
    }
    //----------

    // 排列
    $reply_array = &$write;

    // 最多回复
    if (strlen($reply_array[wr_reply]) == 10)
        alert("主题已被限制回复！\\n\\n已超过最大回复等级10");

    $reply_len = strlen($reply_array[wr_reply]) + 1;
    if ($board[bo_reply_order]) {
        $begin_reply_char = 'A';
        $end_reply_char = 'Z';
        $reply_number = +1;
        $sql = " select MAX(SUBSTRING(wr_reply, $reply_len, 1)) as reply from $write_table where wr_num = '$reply_array[wr_num]' and SUBSTRING(wr_reply, $reply_len, 1) <> '' ";
    } else {
        $begin_reply_char = 'Z';
        $end_reply_char = 'A';
        $reply_number = -1;
        $sql = " select MIN(SUBSTRING(wr_reply, $reply_len, 1)) as reply from $write_table where wr_num = '$reply_array[wr_num]' and SUBSTRING(wr_reply, $reply_len, 1) <> '' ";
    }
    if ($reply_array[wr_reply]) $sql .= " and wr_reply like '$reply_array[wr_reply]%' ";
    $row = sql_fetch($sql);

    if (!$row[reply])
        $reply_char = $begin_reply_char;
    else if ($row[reply] == $end_reply_char) // A~Z共26
        alert("此主题已被限制回复！\\n\\n此主题回复已达上限26层");
    else
        $reply_char = chr(ord($row[reply]) + $reply_number);

    $reply = $reply_array[wr_reply] . $reply_char;

    $title_msg = "回复";
} else
    alert("w 数据传输出错！");


// 版块权限
if ($group[gr_use_access])
{
    if (!$member[mb_id])
        alert("您没有访问权限或等级不足\\n\\n如果是会员请您登录后使用！", "login.php?$qstr&url=".urlencode("$_SERVER[PHP_SELF]?bo_table=$bo_table"));

    if ($is_admin == 'super' || $group[gr_admin] == $member[mb_id] || $board[bo_admin] == $member[mb_id])
        ; // 通过
    else {
        // 版块
        $sql = " select gr_id from $g4[group_member_table] where gr_id = '$board[gr_id]' and mb_id = '$member[mb_id]' ";
        $row = sql_fetch($sql);
        if (!$row[gr_id])
            alert("您没访问权限或等级不足！\\n\\n如果您有疑问请联系论坛版主或管理员！");
    }
}

$g4[title] = "$group[gr_subject] > $board[bo_subject] > " . $title_msg;

if (($w == "u" || $w == "r") && !$write[wr_id])
    alert("您访问的主题不存在！\\n\\n文章可能已被删除或移动", $g4[path]);

$is_notice = false;
if ($is_admin && $w != "r")
{
    $is_notice = true;

    if ($w == "u")
    {
        // 修改时不显示公告选项
        if ($write[wr_reply])
            $is_notice = false;
        else
        {
            $notice_checked = "";
            //if (preg_match("/^".$wr_id."/m", trim($board[bo_notice])))
            //if (preg_match("/[^0-9]{0,1}{$wr_id}[\r]{0,1}/",$board[bo_notice]))
            if (in_array((int)$wr_id, $notice_array))
                $notice_checked = "checked";
        }
    }
}

$is_html = false;
if ($member[mb_level] >= $board[bo_html_level])
    $is_html = true;

/*
// 强制加密 : 061021
$is_secret = false;
if ($board[bo_use_secret])
    $is_secret = true;
*/
$is_secret = $board[bo_use_secret];
// DHTML  : 061021
//$is_dhtml_editor = $board[bo_use_dhtml_editor];
// 090713
if ($board[bo_use_dhtml_editor] && $member[mb_level] >= $board[bo_html_level])
    $is_dhtml_editor = true;
else
    $is_dhtml_editor = false;

$is_mail = false;
if ($config[cf_email_use] && $board[bo_use_email])
    $is_mail = true;

$recv_email_checked = "";
if ($w == "" || strstr($write[wr_option], "mail"))
    $recv_email_checked = "checked";

$is_name = false;
$is_password = false;
$is_email = false;
if (!$member[mb_id] || ($is_admin && $w == 'u' && $member[mb_id] != $write[mb_id])) {
    $is_name = true;
    $is_password = true;
    $is_email = true;
    $is_homepage = true;
}

$is_category = false;
if ($board[bo_use_category]) {
    $ca_name = $write[ca_name];
    $category_option = get_category_option($bo_table);
    $is_category = true;
}

$is_link = false;
if ($member[mb_level] >= $board[bo_link_level])
    $is_link = true;

$is_file = false;
if ($member[mb_level] >= $board[bo_upload_level])
    $is_file = true;

$is_file_content = false;
if ($board[bo_use_file_content])
    $is_file_content = true;

// 引用通告
$is_trackback = false;
if ($board[bo_use_trackback] && $member[mb_level] >= $board[bo_trackback_level])
    $is_trackback = true;

if ($w == "" || $w == "r") {
    if ($member[mb_id]) {
        $name = get_text(cut_str($write[wr_name],20));
        $email = $member[mb_email];
        $homepage = get_text($member[mb_homepage]);
    }
}

if ($w == "")
    $password_required = "required";
else if ($w == "u") {
    $password_required = "";

    if (!$is_admin) {
        if (!($member[mb_id] && $member[mb_id] == $write[mb_id]))
            if (sql_password($wr_password) != $write[wr_password])
                alert("您输入的密码错误！");
    }

    $name = get_text(cut_str($write[wr_name],20));
    $email = $write[wr_email];
    $homepage = get_text($write[wr_homepage]);

    for ($i=1; $i<=$g4[link_count]; $i++) {
        $write["wr_link".$i] = get_text($write["wr_link".$i]);
        $link[$i] = $write["wr_link".$i];
    }

    $trackback = $write[wr_trackback];

    if (strstr($write[wr_option], "html1")) {
        $html_checked = "checked";
        $html_value = "html1";
    } else if (strstr($write[wr_option], "html2")) {
        $html_checked = "checked";
        $html_value = "html2";
    } else
        $html_value = "";

    if (strstr($write[wr_option], "secret"))
        $secret_checked = "checked";

    $file = get_file($bo_table, $wr_id);
} else if ($w == "r") {
    if (strstr($write[wr_option], "secret")) {
        $is_secret = true;
        $secret_checked = "checked";
    }

    $password_required = "required";

    for ($i=1; $i<=$g4[link_count]; $i++) {
        $write["wr_link".$i] = get_text($write["wr_link".$i]);
    }
}

$subject = preg_replace("/\"/", "&#034;", get_text(cut_str($write[wr_subject], 255), 0));
if ($w == "")
    $content = $board[bo_insert_content];
else if ($w == "r") {
    //if (!$write[wr_html]) {
    if (!strstr($write[wr_option], "html")) {
        $content = "\n\n\n>"
                 //. "\n> $write[wr_datetime], \"$write[wr_name]\"发表的主题. ↓"
                 . "\n>"
                 . "\n> " . preg_replace("/\n/", "\n> ", get_text($write[wr_content], 0))
                 . "\n>"
                 . "\n";

    }
} else
    $content = get_text($write[wr_content], 0);

$upload_max_filesize = number_format($board[bo_upload_size]) . " 字节";

$width = $board[bo_table_width];
if ($width <= 100)
    $width .= '%';

// 字节限定
if ($is_admin)
{
    $write_min = $write_max = 0;
}
else
{
    $write_min = (int)$board[bo_write_min];
    $write_max = (int)$board[bo_write_max];
}

include_once("$g4[path]/head.sub.php");
include_once("./board_head.php");

// 防止垃圾信息
//include_once ("./norobot.inc.php");

//--------------------------------------------------------------------------
// 变量
$file_script = "";
$file_length = -1;
// 编辑时显示删除
if ($w == "u")
{
    for ($i=0; $i<$file[count]; $i++)
    {
        $row = sql_fetch(" select bf_file, bf_content from $g4[board_file_table] where bo_table = '$bo_table' and wr_id = '$wr_id' and bf_no = '$i' ");
        if ($row[bf_file])
        {
            $file_script .= "add_file(\"<input type='checkbox' name='bf_file_del[$i]' value='1'><a href='{$file[$i][href]}'>{$file[$i][source]}({$file[$i][size]})</a> 删除";
            if ($is_file_content)
                //$file_script .= "<br><input type='text' class=ed size=50 name='bf_content[$i]' value='{$row[bf_content]}' title='请输入附件介绍内容'>";
                // 修正附件输入 ' 或 " 显示错误的bug
                $file_script .= "<br><input type='text' class=ed size=50 name='bf_content[$i]' value='".addslashes(get_text($row[bf_content]))."' title='请输入附件说明'>";
            $file_script .= "\");\n";
        }
        else
            $file_script .= "add_file('');\n";
    }
    $file_length = $file[count] - 1;
}

if ($file_length < 0)
{
    $file_script .= "add_file('');\n";
    $file_length = 0;
}
//--------------------------------------------------------------------------

include_once ("$board_skin_path/write.skin.php");

if (!$member[mb_id])
    echo "<script language='javascript' src='$g4[path]/js/md5.js'></script>\n";

// 数据扩展
//echo "<script type='text/javascript'> var g4_cf_filter = '$config[cf_filter]'; </script>\n";
//echo "<script type='text/javascript' src='$g4[path]/js/filter.js'></script>\n";

include_once("./board_tail.php");
include_once("$g4[path]/tail.sub.php");

@include_once ("$board_skin_path/write.tail.skin.php");
?>
