<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 5                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.php.net/license/3_0.txt.                                  |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Original Author <author@example.com>                        |
// |          Your Name <you@example.com>                                 |
// +----------------------------------------------------------------------+
//
// $Id:$

require "../Run.php";
require root_path . '/include/page.inc.php';
require root_path . '/include/class/huifu.class.php';
require root_path . '/include/postHf.fun.php';
require root_path . "/include/getsize.fun.php";
require root_path . "/include/fun/bbs.fun.php";
/*初始化回复数据*/
if (!is_file("../lib/" . WAPID . "/bbscomm_" . $_GET['cid'] . ".txt")) {
    file_put_contents("../lib/" . WAPID . "/bbscomm_" . $_GET['cid'] . ".txt", NULL);
}
$bbscomm_str = explode("|", file_get_contents("../lib/" . WAPID . "/bbscomm_" . $_GET['cid'] . ".txt"));
$randcomm = $bbscomm_str[array_rand($bbscomm_str) ];
/*列表数目*/
$filename = root_path . '/lib/bbs/' . WAPID . '_' . $_GET['cid'] . '.txt';
if (!is_dir(root_path . '/lib/bbs')) {
    mkdir(root_path . '/lib/bbs');
}
if (!is_file($filename))
{
    file_put_contents($filename, '12');
}
$this_list = file_get_contents($filename);
define("LIMIT_HUIFU", $this_list);
$filename = root_path . '/lib/bbs/' . WAPID . '_css.txt';
if (!is_file($filename)) {
    file_put_contents($filename, htmlspecialchars('<link rel="stylesheet" type="text/css" href="/style/css/bbs_view.css">'));
}
$this_css = htmlzm(file_get_contents($filename),'decode');
define("BBSCSS", $this_css);

$view = mysql_query("SELECT bbsid,userid,title,content,pic,name,v,time,gro,zip,zan,xi,nu,zipstr,viewType,viewValue FROM bbs WHERE bbsid='{$_GET['id']}'");
if ($view && mysql_num_rows($view) > 0) {
    list($bbsid, $userid, $title, $content, $pic, $name, $v, $time, $hgro, $zip, $zan, $xi, $nu, $zipstr, $viewType, $viewValue) = mysql_fetch_row($view);
    define("USERID", $userid);
} else {
    ehtml("资源已被移除或者地址错误!");
    exit;
}
/*栏目预判*/
if ($_GET['cid'] <= 0 || $_GET['cid'] == '' || $_GET['cid'] == NULL)
{
    tourl("location: http://" . $_SERVER["HTTP_HOST"]."/bbs/content.php?cid=".$hgro."&id=".$bbsid);
	exit();
}
/*
+------------------------------------------
* 函数用于识别是否已经赞过
+------------------------------------------
*/

if ($_GET['action'] == 'zan'||$_GET['action'] == 'xi'||$_GET['action'] == 'nu') {
	if(LOGIN == 1){
	if(setBbsZan('get') == FALSE)
	{
		if($_GET['action'] == 'zan'){
		$umod = $zan + 1;
		$result = mysql_query("UPDATE bbs SET zan='{$umod}' WHERE bbsid='{$_GET["id"]}'");
		mysql_query("UPDATE bbs SET view_time='{$v_t}' WHERE bbsid='{$_GET['id']}'");
		}elseif ($_GET['action'] == 'xi') {
		$umod = $xi + 1;
		$result = mysql_query("UPDATE bbs SET xi='{$umod}' WHERE bbsid='{$_GET["id"]}'");
		mysql_query("UPDATE bbs SET view_time='{$v_t}' WHERE bbsid='{$_GET['id']}'");
		} elseif ($_GET['action'] == 'nu') {
		$umod = $nu + 1;
		$result = mysql_query("UPDATE bbs SET nu='{$umod}' WHERE bbsid='{$_GET["id"]}'");
		mysql_query("UPDATE bbs SET view_time='{$v_t}' WHERE bbsid='{$_GET['id']}'");
		}
		setBbsZan('set');
		tourl("content.php?cid=" . $_GET['cid'] . "&id=" . $_GET['id'] . "&page=" . $_GET['page']);
	}else{
		ehtml('您已经进行过赞等操作，无需再次进行哦!');
		exit();
	}
	}else{
	ehtml('请登录后再进行评论操作!');
	exit();
	}
} 
/*访问量统计*/
if ($_POST['action'] == NULL || $_POST['acion'] == '') {
    $vs = $v + 1;
    $sql = "UPDATE bbs SET v='$vs' WHERE bbsid='{$_GET["id"]}'";
    $result = mysql_query($sql);
}
/*图片处理*/
if ($zip == NULL) {
    $load = NULL;
} else {
    $d = explode('|', $zip);
    $file_arr = explode('|', $zipstr);
    $i = 1;
    foreach ($d as $value) {
        $filetype = substr(strrchr($value, ".") , 1);
        $allowtype = array(
            "jpeg",
            "png",
            "jpg",
            "gif",
            "JPEG",
            "PNG",
            "JPG",
            "GIF"
        );
        $img = '';
        if (in_array($filetype, $allowtype)) {
            $img = '<div class="bbsimg"><img src="' . $value . '" alt="' . $value . '"></div>';
        }
        $download.= $img . '<a href="' . $value . '">附件' . $i . '下载(' . getFileSize($value) . ')<img src="../style/rar.gif"/></a><br><text>' . $file_arr[$i - 1] . '</text><br>';
        $i++;
    }
    $load = '<div class="download">' . $download . '</div>';
}

if ($_GET['px'] == '1') {
    $paixu_text = '升序回复';
    $paixu = 0;
    define('PX', 1);
    cookie(MD5($_SERVER['HTTP_HOST'] . '_PX') , 1);
} elseif ($_GET['px'] == '0') {
    $paixu_text = '降序回复';
    $paixu = 1;
    define('PX', 0);
    cookie(MD5($_SERVER['HTTP_HOST'] . '_PX') , 0);
} elseif ($_COOKIE[MD5($_SERVER['HTTP_HOST'] . '_PX') ] == 1) {
    $paixu_text = '升序回复';
    $paixu = 0;
    define('PX', 1);
    cookie(MD5($_SERVER['HTTP_HOST'] . '_PX') , 1);
} else {
    $paixu_text = '降序回复';
    $paixu = 1;
    define('PX', 0);
    cookie(MD5($_SERVER['HTTP_HOST'] . '_PX') , 0);
}
if ($_POST['action'] == 'post') {
    echo head("回复提交", $css);
    if (LOGIN == 1 && !is_bbshei(MYID)) {
        //加载回复提交函数
        $hgro = $_POST['id'];
        /*为回复添加表情*/
        $contenth = $_POST['feel'] == '0' ? $_POST['htext'] : '/'.$_POST['feel'] .';'. $_POST['htext'];
        $result = mysql_query("SELECT hid,userid,htext,touserid,siteid,gro,time FROM bbscomm WHERE siteid='{$wapid}' AND userid='" . MYID . "' ORDER BY hid DESC LIMIT 1");
        $maxnum = mysql_num_rows($result);
        if ($result && $maxnum > 0) {
            while (list($yz_hid, $yz_userid, $yz_htext, $yz_touserid, $yz_siteid, $yz_gro, $yz_time) = mysql_fetch_row($result)) {
                if (MD5($yz_htext) == MD5($contenth) || time() - $yz_time < 10) {
                    ehtml("禁止评论相同内容，或者您的评论速度过快！");
					exit();
                }
            }
        }
        $hFinfo = postHf($contenth, $id, $wapid, $hgro, 'NULL', 1000);
        if ($hFinfo && mysql_affected_rows() > 0) {
            $sql = "UPDATE bbs SET view_time='{$v_t}' WHERE bbsid='{$_GET['id']}'";
            mysql_query($sql);
            $value = cre('+', 20, $id);
            /*发送短信通知楼主*/
            $title = $nc . '回复了您的帖子《 ' . $title . ' 》';
            $content = '您好，' . $nc . ' 回复了您的帖子《[url=http://' . HTTP_HOST . '/bbs/content.php?cid=' . $_GET['cid'] . '&id=' . $_GET['id'] . ']' . $title . '[/url]》[br]回复内容[br]：' . $_POST['htext'] . '[br]快去看看吧。[url=http://' . HTTP_HOST . '/bbs/content.php?cid=' . $_GET['cid'] . '&id=' . $_GET['id'] . ']飞机直达[/url]！';
            $user_id = $userid;
            if ($_POST['mess'] == '1') {
                if (SendMessage($title, $content, $user_id, NULL) == 1) {
                    $mtip = '已经短信通知楼主了！';
                } else {
                    $mtip = '哎呀，楼主没有收到你的短息哦！';
                }
            } else {
                $mtip = '默默地留言了';
            }
            echo '<div class="tip">评论成功,您已经收到系统奖励的20积分了！您的当前积分余额为' . $value . '<br>' . $mtip . '</div>';
        } else {
            echo '<div class="tip">评论失败</div>';
        }
    }
    echo '<div class="mylink">';
    echo '<a href="content.php?cid=' . $_GET['cid'] . '&id=' . $_GET['id'] . '&page=' . $_GET['page'] . '&px=' . PX . '&p=' . $_GET['p'] . '">返回主题</a></div>';
    echo footer(1);
} else {
    $v_t = time();
    $limit = LIMIT_HUIFU;
    $nums = huifunums($_GET['id']);
    $listnum = pagen($nums, $limit);
    $kaishi = slist($_GET['p'], $limit);
    $huifu1 = new hf($wapid, $_GET['id'], $kaishi, $limit, $login);
    $huifu_list = $huifu1->huifulist();
    $url = 'content.php?cid=' . $_GET['cid'] . '&id=' . $_GET['id'] . '&page=' . $_GET['page'] . '&px=' . PX . '&';
    $page = page($nums, $limit, $url, 'p', 'view');
    if (PX == 1) {
        $pagen = 1;
    } else {
        $pagen = pagen($nums + 1, $limit);
    }
    $thispage = $_GET['p'];
    $thispage = $thispage < 1 ? 1 : $thispage;
    $tpl = new dd_Tpl;
    $tpl->tpl_path(root_path . '/DdTpl/comdir');
    echo  head($title,BBSCSS);
    $assign = array(
		'headimg'      =>  user('head', 'id', $userid),
		"nav"          =>  topNav('帖子阅读','<a href="list.php?cid='.$_GET['cid'].'">列表</a>','<a href="add.php?cid='.$_GET['cid'].'">发帖</a>'),
		"page"         =>  $page,
		"bbs_id"       =>  $_GET['id'],
		"bbs_classid"  =>  $_GET['cid'],
		"userxid"      =>  user('xid', 'id', $userid),
		"info"         =>  user( 'nc', 'id', $userid) . '(' . user('xid', 'id', $userid) . ')',
		"title"        =>  $title,
		"login"        =>  LOGIN ,
		"zan"          =>  $zan,
		"nu"           =>  $nu,
		"px_url"       =>  $paixu,
		"thispx"       =>  PX,
		"host"         =>  HTTP_HOST,
		"px_text"      =>  $paixu_text,
		"content"      =>  viewType($viewType, $viewValue, viewStr($content, 9000, $_GET['id'])) . $load,
		"randcomm"     =>  $randcomm,
		"thispage"     =>  $thispage,
		"gopage"       =>  $pagen,
		"time"         =>  date("Y/m/d H:i:s", $time),
		"qianm"        =>  user("qianm", 'id', $userid),
		"huifu"        =>  $huifu_list
		);
	$tpl->assign($assign);
    $tpl->display("bbs");
}
echo footer(1);