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

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

if ($write[wr_is_comment])
{
    /*
    if ($cwin) // 回复
        alert_close("回复没有详细");
    else
        alert("回复没有详细");
    */
    goto_url("/bbs/board.php?bo_table=$bo_table&wr_id=$write[wr_parent]#c_{$wr_id}");
}

if (!$bo_table)
{
    $msg = "bo_table 设定错误！\\n\\nboard.php?bo_table=code 方式传递";
    if ($cwin) // 回复
        alert_close($msg);
    else
        alert($msg);
}

// wr_id 浏览
if ($wr_id)
{
    // 没有指定文章就跳到论坛目录
    if (!$write[wr_id])
    {
        $msg = "您访问的主题不存在！\\n\\n文章可能已被删除或移动";
        if ($cwin)
            alert_close($msg);
        else
            alert($msg, "/bbs/board.php?bo_table=$bo_table");
    }

    // 版块全线
    if ($group[gr_use_access])
    {
        if (!$member[mb_id]) {
            $msg = "此版块禁止游客浏览！\\n\\n如果是会员请您登录后使用！";
            if ($cwin)
                alert_close($msg);
            else
                alert($msg, "/bbs/login.php?wr_id=$wr_id{$qstr}&url=".urlencode("/bbs/board.php?bo_table=$bo_table&wr_id=$wr_id"));
        }

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

    // 会员等级
    if ($member[mb_level] < $board[bo_read_level])
    {
        if ($member[mb_id])
            //alert("您没有阅读权限！");
            alert("您没有阅读权限！", $g4[path]);
        else
            alert("您没有阅读权限！\\n\\n如果是会员请您登录后使用！", "/bbs/login.php?wr_id=$wr_id{$qstr}&url=".urlencode("/bbs/board.php?bo_table=$bo_table&wr_id=$wr_id"));
    }

    // 自己的文章或者是管理员就通过
    if (($write[mb_id] && $write[mb_id] == $member[mb_id]) || $is_admin)
        ;
    else
    {
        // 加密内容
        if (strstr($write[wr_option], "secret"))
        {
            // 会员发表加密内容 管理员回复后
            // 修改会员不能查看管理员回复内容的bug
            $is_owner = false;
            if ($write[wr_reply] && $member[mb_id])
            {
                $sql = " select mb_id from $write_table
                          where wr_num = '$write[wr_num]'
                            and wr_reply = ''
                            and wr_is_comment = '0' ";
                $row = sql_fetch($sql);
                if ($row[mb_id] == $member[mb_id])
                    $is_owner = true;
            }

            $ss_name = "ss_secret_{$bo_table}_$write[wr_num]";

            if (!$is_owner)
            {
                //$ss_name = "ss_secret_{$bo_table}_{$wr_id}";
                // 输入一次的密码以后访问不需要再次输入密码
                // 如果不存在
                //if ("$bo_table|$write[wr_num]" != get_session("ss_secret"))
                if (!get_session($ss_name))
                    goto_url("/bbs/password.php?w=s&bo_table=$bo_table&wr_id=$wr_id{$qstr}");
            }

            set_session($ss_name, TRUE);
        }
    }

    // 阅读过的主题关闭浏览器前多次点击计算一次
    $ss_name = "ss_view_{$bo_table}_{$wr_id}";
  if (!get_session($ss_name)) 
            { 
                sql_query(" update $write_table set wr_hit = wr_hit + 1 where wr_id = '$wr_id' "); 

                // 如果是作者 
                if ($write[mb_id] && $write[mb_id] == $member[mb_id]) { 
                    ; 
                } else if ($is_guest && $board[bo_read_level] == 1 && $write[wr_ip] == $_SERVER['REMOTE_ADDR']) { 
                    // 如果非会员，且阅读等级为1，IP地址相同时认定为自己发表的文章
                    ; 
                } else { 
                    /* 
                    // 注册会员才能阅读
                    if ($board[bo_read_level] > 1) { 
                        if ($member[mb_point] + $board[bo_read_point] < 0) 
                            alert("当前积分(".number_format($member[mb_point]).")不够支付阅读文章所需积分(".number_format($board[bo_read_point]).")\\n\\n请您赚取积分后重试"); 

                        insert_point($member[mb_id], $board[bo_read_point], "$board[bo_subject] $wr_id 浏览主题", $bo_table, $wr_id, '阅读'); 
                    } 
                    */ 
                    // 开启阅读文章消耗积分功能
                    if ($board[bo_read_point] && $member[mb_point] + $board[bo_read_point] < 0) 
                        alert("当前积分(".number_format($member[mb_point]).")不够支付阅读文章所需积分(".number_format($board[bo_read_point]).")\\n\\n请您赚取积分后重试"); 

                    insert_point($member[mb_id], $board[bo_read_point], "$board[bo_subject] $wr_id 浏览主题", $bo_table, $wr_id, '阅读'); 
                } 

                set_session($ss_name, TRUE); 
            } 
			
    $g4[title] = "$group[gr_subject] > $board[bo_subject] > " . strip_tags(conv_subject($write[wr_subject], 255));
}
else
{
    if ($member[mb_level] < $board[bo_list_level])
    {
        if ($member[mb_id])
            alert("您没有访问目录权限或等级不足！", $g4[path]);
        else
            alert("您没有访问目录权限或等级不足！\\n\\n如果是会员请您登录后使用！", "/bbs/login.php?wr_id=$wr_id{$qstr}&url=".urlencode("board.php?bo_table=$bo_table&wr_id=$wr_id"));
    }

    if (!$page) $page = 1;

    $g4[title] = "$group[gr_subject] > $board[bo_subject] $page 页";
}

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

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

// IP显示与否
$ip = "";
$is_ip_view = $board[bo_use_ip_view];
if ($is_admin) {
    $is_ip_view = true;
    $ip = $write[wr_ip];
} else // 如果不是管理员则隐藏部分ip
    $ip = preg_replace("/([0-9]+).([0-9]+).([0-9]+).([0-9]+)/", "\\1.**.\\3.\\4", $write[wr_ip]);

// 分类功能
$is_category = false;
$category_name = "";
if ($board[bo_use_category]) {
    $is_category = true;
    $category_name = $write[ca_name]; // 分类名
}

// 推荐功能
$is_good = false;
if ($board[bo_use_good])
    $is_good = true;

// 反对功能
$is_nogood = false;
if ($board[bo_use_nogood])
    $is_nogood = true;

$admin_href = "";
// 网站管理员或版块管理员直接通过
if ($member[mb_id] && ($is_admin == 'super' || $group[gr_admin] == $member[mb_id]))
    $admin_href = "$g4[admin_path]/board_form.php?w=u&bo_table=$bo_table";

if (!($board[bo_use_comment] && $cwin))
    include_once("./board_head.php");

echo "<script language=\"javascript\" src=\"$g4[path]/js/sideview.js\"></script>\n";

if (!($board[bo_use_comment] && $cwin)) {
    //  INCLUDE
    if ($wr_id)
        include_once("./view.php");

    // 全部目录
    //if ($board[bo_use_list_view] || empty($wr_id))
    if ($member[mb_level] >= $board[bo_list_level] && $board[bo_use_list_view] || empty($wr_id))
        include_once ("./list.php");

    include_once("./board_tail.php");
}
else
    include_once("./view_comment.php");

echo "\n<!-- 应用模板 : $board[bo_skin] -->\n";

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