<?php
/*
* ===========================================
* 项目: CTB文本论坛
* 版本: 1.0
* 团队: 实易数码
* 小组: CTB文本论坛小组
* 作者: windnets，16hot，险情
* 功能: 发表/回复帖子模块
* ===========================================
* Copyright (c) 2002
* 团队主页: http://www.11cn.org
* 团队信箱: ------------------
* 创建日期: 2002.6
* 修改日期: 2003-4-10
* 修改说明: 代码整理/优化
* 版权声明: 这个程序是免费软件.
* ===========================================
*/


class Module extends CommonClass
{

    /**
     *构造函数
     *
     */
    function execute()
    {

        $this->checkPost();

        switch ($this->input['action']) {
            case 'posted':
                $this->writePost();
                break;
            case 'showReply':
                $this->showReply();
                break;
            case 'reply':
                $this->writeReply();
                break;
            case 'showPoll':
                $this->showPost();
                break;
            case 'submitPoll':
                $this->submitPoll();
                break;
            default: 
                $this->showPost();
                break;
        }


    } // end func

    /**
     *功能: 检查用户
     *参数: 无
     *返回: 无
     *说明: 
     *创建: 2002-12-1
     *修改: 
     **/
    function checkPost() {
        if ($this->sess['sess_limit_user'] == "0"){
        	$this->showMsg("您的名字已经被管理员锁定，请联系管理员","./index.php");
        }

        $this->file = $this->set[dataPath]."/forums.php";
        $this->forum = $this->select_line("2",$this->input['forumid']);
        $check = $this->checkForum($this->input['forumid']);
        if ($check[0] == "0") {
            $this->showMsg("您无权查看这个版块,若想查看，请联系管理员");
        }
        if ($check[2] == "0") {
            $this->showMsg("此为只读版块，禁止发表文章","./index.php?mods=forumdisplay&forumid=$forumid");
        }

        if (!isset($this->sess['sess_uid']) || $this->sess['sess_uid'] == "") {
            if ($this->input['postid'] == "") {
                $sess_url = $_SERVER['PHP_SELF']."?mods=post&forumid=".$this->input['forumid'];
            } else {
                $_SESSION['sess_url'] = $_SERVER['PHP_SELF']."?mods=post&action=showreply&forumid={$this->input['forumid']}&postid={$this->input['postid']}";
            }
			$jump_back_url = str_replace('&', '~~~~', str_replace('=', '^^^^', $this->input['ctb_location']));
            $this->showMsg("注册会员才能发表,请先登陆/注册","./index.php?mods=member&action=login&jump_back=".$jump_back_url);
        }
    }


    /**
     *显示发表主题页面
     *
     */
    function showPost()
    {

        if("showpoll" == $this->input['action']) {
            $title = "发表投票";
            eval("\$showPoll = \"".$this->getTemplate("showPoll")."\";");
        }else {
            $title = "发表主题";
        }
        $this->BAN = " &gt; <a href=index.php?mods=forumdisplay&forumid={$this->input['forumid']}>".$this->forum[3]."</a> &gt; ".$title;

        $header = $this->header($title);
        $actionModlue = "post";
        $actionType = "posted";
        eval("\$this->outPut(\"".$this->getTemplate("post")."\");");

    } // end func


    /**
     *写入主题信息和内容
     *
     */
    function writePost()
    {
        if (!trim($this->input['topicTitle'])) {
            $this->showMsg("请填写主题");
        } elseif (!trim($this->input['topicBody'])) {
            $this->showMsg("请填写信息内容");
        } elseif (strlen($this->input['replyTitle']) > $this->set[postTopicWidth]) {
            $this->showMsg("您填写的标题太长了");
        } elseif (strlen($this->input['replyBody']) > $this->set[postCommonWidth]) {
            $this->showMsg("您填写的帖子内容太长了");
        }

        if (file_exists($this->set[dataPath]."/".$this->input['forumid']."/list.php")) {
            $this->file = $this->set[dataPath]."/".$this->input['forumid']."/list.php";
            $maxId = $this->get_max_value("1");
            $this->postId = $maxId+1;
        } else {
            $this->postId = 1;
        }

        $this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid'].".php";
        $line = $this->get();
        if (time() - $line[22] < $this->set[limitPostTime] and $this->sess['sess_ugrade'] !=1) {
            $this->showMsg("抱歉，我们已经打开了防止灌水机制。限定为：<font color=red> {$this->set[limitPostTime]} </font> 秒");
        } else {
            $line[22] = time();
        }
        $line[13]++;

        if ($line[4] == "2") {
            $this->file = $this->set[dataPath]."/membertitle.php";
            $userCode = $this->openfile();
            for ($i=0; $i<=count($userCode)-1; $i++) {
                if($line[13] >= $userCode[$i][1]) {
                    $line[16] = $userCode[$i][2];
                    $line[17] = $userCode[$i][3];
                    break;
                }
            }
        } elseif ($line[4] == "3") {
            $line[16] = "斑竹";
            $line[17] = "pips0.gif";
        } elseif( $line[4] == "1") {
            $line[16] = "管理员";
            $line[17] = "pips00.gif";
        }
        $line[19] = $this->input['forumid'];
        $line[20] = $this->postId;
        $line[21] = $this->input['topicTitle'];
        $line[22] = time();
        $line[23] = "";

        $this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid'].".php";
        $this->overwrite($line);

        $topicTitle = $this->safeChars($this->input['topicTitle']);
        $topicBody = $this->safeChars($this->input['topicBody']);
		if ($this->set[replaceWhenPost] == 'Y') {
			$topicTitle = $this->replaceBadWord($topicTitle);
			$topicBody = $this->replaceBadWord($topicBody);
		}
        $this->postTime = time();

        $postList = array(stripslashes($this->code),$this->postId,$this->sess['sess_uname'],$this->postTime,"0","0","0","0","post","0","0","\n");
        $topinNum = array(stripslashes($this->code),$this->postId,$topicTitle,"1","0",$this->sess['sess_uname'],$this->postTime,$this->input['pic'],"","1","1","","","post","0","0","0","","","");

        if ("poll" == $this->input['postPoll']){
            $pollArray = explode("\n",$this->input['pollItem']);
            $getPollCount = count($pollArray);
            if ($getPollCount > 8){
                $this->showMsg("抱歉，管理员限定投票选项不能超过 8 条。");
            }
            for ($i=0;$i<=$getPollCount;$i++){
                $pollArray[$i] = $this->safeChars($pollArray[$i]);
				$pollArray[$i] = $this->set[replaceWhenPost] == 'Y' ? $this->replaceBadWord($pollArray[$i]) : $pollArray[$i];
            }
            $pollList = array(stripslashes($this->code),$this->postId,"0",$pollArray[0],"0",$pollArray[1],"0",$pollArray[2],"0",$pollArray[3],"0",$pollArray[4],"0",$pollArray[5],"0",$pollArray[6],"0",$pollArray[7],"");
            $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->postId.".php4";
            $this->add_line($pollList);

            $pollUser = array(stripslashes($this->code),"user","");
            $this->add_line($pollUser);
            if ($this->input['multiChoice'] == "1"){  // changed by cx
                $pollMode = "1";
            }else {
                $pollMode = "0";
            }
            $pollEndTime = mktime(0,0,0,$this->input['timeOutMonth'],$this->input['timeOutDay'],$this->input['timeOutYear']);
            $postList = array(stripslashes($this->code),$this->postId,$this->sess['sess_uname'],$this->postTime,"0","0","0","0","vote","0","0","\n");
            $topinNum = array(stripslashes($this->code),$this->postId,$topicTitle,"1","0",$this->sess['sess_uname'],$this->postTime,$this->input['pic'],"","","","","","vote",$pollMode,$pollEndTime,"0","0","","");

            @chmod($this->file,0777);
        }

        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/list.php";
        $this->insertTop($this->file,$postList);
        @chmod($this->file,0777);
        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->postId."_.php";
        $this->add_line($topinNum);
        @chmod($this->file,0777);

        if (!$this->input['disableCtbCode']) {
            $disableCtbCode = "0";
        }else {
            $disableCtbCode = "1";
        }
        if (!$this->input['disableUrl']) {
            $disableUrl = "0";
        }else {
            $disableUrl = "1";
        }
        if (!$this->input['disableSymbol']) {
            $disableSymbol = "0";
        }else {
            $disableSymbol = "1";
        }
        if (!$this->input['disableSig']) {
            $disableSig = "0";
        }else {
            $disableSig = "1";
        }
        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->postId.".php";
        $post = array(stripslashes($this->code),"1",$topicTitle,addslashes($topicBody),$this->postTime,$this->sess['sess_uid'],"","","1",$disableCtbCode,$disableUrl,$disableSymbol,$disableSig,"0","","");
        $this->add_line($post);
        @chmod($this->file,0777);

        $pageNumber = $this->upForumPost(1);

        $postShowMsg = "您已经成功发表了该主题<br><br>
            <li><a href=\"./index.php?mods=topicdisplay&forumid={$this->input['forumid']}&postid={$this->postId}\">返回查看主题</a><br>
            </li>
            <li><a href=\"./index.php?mods=forumdisplay&forumid={$this->input['forumid']}\">返回查看版块</a><br>
            </li>
            <li><a href=\"./index.php\">返回论坛首页</a></li></ul>";
        $this->showMsg($postShowMsg,"./index.php?mods=topicdisplay&forumid={$this->input['forumid']}&postid={$this->postId}&p=$pageNumber");

    } // end func


    /**
     *显示回复页面
     *
     */
    function showReply()
    {

        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid']."_.php";
        $postTitles = $this->get();
        $this->BAN = " &gt; <a href=\"index.php?mods=forumdisplay&forumid={$this->input['forumid']}\">".$this->forum[3]."</a> &gt; <a href=\"index.php?mods=topicdisplay&forumid={$this->input['forumid']}&postid={$this->input['postid']}\">".$postTitles[2]."</a> &gt; 回复主题";

        $postTitleFirst = $postTitles[2];
		$postTitleFirst = $this->set[replaceWhenPost] == 'Y' ? $postTitleFirst : $this->replaceBadWord($postTitleFirst);

        $header = $this->header("回复主题");

        eval("\$this->outPut(\"".$this->getTemplate("reply")."\");");

    } // end func

    /**
     *写入回复
     *2002-5-14修改
     */
    function writeReply()
    {

        if (!isset($this->sess['sess_uid']) || $this->sess['sess_uid'] == "") {
			$jump_back_url = str_replace('&', '~~~~', str_replace('=', '^^^^', $this->input['ctb_location']));
            $this->showMsg("注册会员才能发表,请先登陆/注册","./index.php?mods=member&action=login&jump_back=".$jump_back_url);
        } elseif (!trim($this->input['replyTitle']) || !trim($this->input['replyBody'])) {
            $this->showMsg("请填写回复标题和信息");
        } elseif (strlen($this->input['replyTitle']) > $this->set[postTopicWidth]) {
            $this->showMsg("您填写的标题太长了");
        } elseif (strlen($this->input['replyBody']) > $this->set[postCommonWidth]) {
            $this->showMsg("您填写的帖子容易太长了");
        }

        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/list.php";
        $line = $this->select_line("1",$this->input['postid']);
        if ($line[4] == "1") {
            $this->showMsg("此帖子已被管理员锁定");
        }

        $this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid'].".php";
        $line = $this->get();
        if (time()-$line[22] < $this->set[limitPostTime] and $this->sess['sess_ugrade'] !=1) {
            $this->showMsg("抱歉，我们已经打开了防止灌水机制。限定为：<font color=red>{$this->set[limitPostTime]}</font> 秒");
        } else {
            $line[22] = time();
        }
        $line[13]++;

        if ($line[4] == "2") {
            $this->file = $this->set[dataPath]."/membertitle.php";
            $userCode = $this->openfile();
            for ($i=0; $i<=count($userCode)-1; $i++) {
                if($line[13] >= $userCode[$i][1]) {
                    $line[16] = $userCode[$i][2];
                    $line[17] = $userCode[$i][3];
                    break;
                }
            }
        } elseif ($line[4] == "3") {
            $line[16] = "斑竹";
            $line[17] = "pips0.gif";
        } elseif ($line[4] == "1") {
            $line[16] = "管理员";
            $line[17] = "pips00.gif";
        }
        $line[19] = $this->input['forumid'];
        $line[20] = $this->input['postid'];
        $line[21] = $this->input['replyTitle'];
        $line[22] = time();
        $line[23] = "";

        $this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid'].".php";
        $this->overwrite($line);
        @chmod($this->file,0777);

        $replyTitle = $this->safeChars($this->input['replyTitle']);
        $replyBody = $this->safeChars($this->input['replyBody']);
		if ($this->set[replaceWhenPost] == 'Y') {
			$replyTitle = $this->replaceBadWord($replyTitle);
			$replyBody = $this->replaceBadWord($replyBody);
		}
        $replyTime = time();

        if ($this->input['topicid'] != "") {
            $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid'].".php";
            $quoteLine = $this->select_line("1", $this->input['topicid']);
            $quoteMessage = $quoteLine[3];
			$quoteMessage = $this->set[replaceWhenPost] == 'Y' ? $this->replaceBadWord($quoteMessage) : $quoteMessage;
            if (file_exists($this->set[dataPath]."/users/".$quoteLine[5].".php")) {
                $this->file = $this->set[dataPath]."/users/".$quoteLine[5].".php";
                $quoteLine2 = $this->get();
                $quoteName = $quoteLine2[3];
            }
        }
        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid'].".php";
        if (!file_exists($this->file)) {
            $this->showMsg("无效主题");
        }
        $replyId = $this->getlines() + 1;

        if (!$this->input['disableCtbCode']) {
            $disableCtbCode = "0";
        }else {
            $disableCtbCode = "1";
        }
        if (!$this->input['disableUrl']) {
            $disableUrl = "0";
        }else {
            $disableUrl = "1";
        }
        if (!$this->input['disableSymbol']) {
            $disableSymbol = "0";
        }else {
            $disableSymbol = "1";
        }
        if (!$this->input['disableSig']) {
            $disableSig = "0";
        }else {
            $disableSig = "1";
        }
        $reply = array(stripslashes($this->code),$replyId,$replyTitle,addslashes($replyBody),$replyTime,$this->sess['sess_uid'],$quoteName,$quoteMessage,"1",$disableCtbCode,$disableUrl,$disableSymbol,$disableSig,"0","","");
        $this->add_line($reply);
        @chmod($this->file,0777);

        $countFile = $this->upForumPost(0);

        $this->upTopicPost();

        $listnum = $this->set[postList];
        $pageNumber = ($countFile % $listnum) == "0" ? intval(($countFile)/$listnum) : intval(($countFile)/$listnum)+1;
        $postShowMsg = "您已经成功回复了该主题<br><br>
            <li><a href=\"./index.php?mods=topicdisplay&forumid={$this->input['forumid']}&postid={$this->input['postid']}&p=$pageNumber\">返回查看主题</a><br>
            </li>
            <li><a href=\"./index.php?mods=forumdisplay&forumid={$this->input['forumid']}\">返回查看版块</a><br>
            </li>
            <li><a href='index.php'>返回论坛首页</a></ul>";
        $this->showMsg($postShowMsg,"./index.php?mods=topicdisplay&forumid={$this->input['forumid']}&postid={$this->input['postid']}&p=$pageNumber");

    } // end func


    /**
     *更新论坛的主题和帖子统计数
     *2002-4-29
     */
    function upForumPost($topic)
    {

        if(!$this->postId) {
            $this->postId = $this->input['postid'];
        }
        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->postId.".php";
        $pageNumber = $this->getlines();
        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/status.php";
        @$line = $this->get();

        if ($topic == "0") {
            $line[2]++;
            $line[3] = $this->postId;
            $line[4] = $this->input['replyTitle'];
            $line[5] = time();
            $line[6] = $this->sess['sess_uid'];
            $line[7] = $this->sess['sess_uname'];
            $line[8] = $pageNumber;
        } else {
            $line[1]++;
            $line[2]++;
            $line[3] = $this->postId;
            $line[4] = $this->input['topicTitle'];
            $line[5] = time();//$this->postTime;
            $line[6] = $this->sess['sess_uid'];
            $line[7] = $this->sess['sess_uname'];
            $line[8] = $pageNumber;
        }
		$line[4] = $this->set[replaceWhenPost] == 'Y' ? $this->replaceBadWord($line[4]) : $line[4];

        $this->overwrite($line);
        @chmod($this->file,0777);
        return $pageNumber; #

    } // end func

    /**
    *更新主题帖子数和主题列表次序
    *2002-4-29
    */
    function upTopicPost()
    {

        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid']."_.php";
        $line = $this->get();
        $line[3]++;
        $line[6] = time();
        $line[5] = $this->sess['sess_uname'];
        $this->overwrite($line);
        @chmod($this->file,0777);

        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/list.php";
        $line = $this->select_line("1",$this->input['postid']);
        $this->delete2("1",$this->input['postid']);
        $this->insertTop($this->file,$line);
        @chmod($this->file,0777);

    } // end func


    /**
     *以下为投票处理部分
     *
     */
    function submitPoll()
    {
        if (!$this->input['forumid'] || !$this->input['postid'] || !$this->input['voteId']) {
            $this->showMsg("请将数据填写完整");
        }
        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid'].".php4";
        $pollItems = $this->openfile();
        for ($i=2;$i<count($pollItems[1]);$i++) { 
            if($this->sess['sess_uid'] == $pollItems[1][$i]) {
                $this->showMsg("您已经投过票了，请不要重复参与。");
                //exit();
            }
        }
        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid']."_.php";
        $pollduoxuan = $this->get();
        if ($pollduoxuan[17] == 1) {
            $this->showMsg("此投票已经被管理员锁定，若有问题请联系管理员。");
		}
            $pollduoxuan[16]++;
            $this->update2("1",$this->input['postid'],$pollduoxuan);
        if ($pollduoxuan[14] == "0") {
            $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid'].".php4";
            $line = $this->get();
            $voteAddId = $this->input['voteId'];
            $line[$this->input['voteId']*2]++;
            $this->update2("1",$this->input['postid'],$line);
        }
        if ($pollduoxuan[14]=="1") {
            $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid'].".php4";
            $line = $this->get();
            $voteIdCount = count($this->input['voteId']);
            for ($i=0;$i<$voteIdCount;$i++){
                list($voteAddId,$this->input['voteId'][$i])=each($this->input['voteId']);
                $line[$voteAddId*2]++;
                $this->update2("1",$this->input['postid'],$line);
            }
        }

        $postShowMsg = "您已经成功参与了本次投票<br><br>
            <li><a href=\"./index.php?mods=topicdisplay&forumid={$this->input[forumid]}&postid={$this->input[postid]}&p=$page_num\">返回查看投票</a><br>
            </li>
            <li><a href=\"./index.php?mods=forumdisplay&forumid={$this->input[forumid]}\">返回查看版块</a><br>
            </li>
            <li><a href=\"./index.php\">返回论坛首页</a></ul>";
        //
        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid'].".php4";
        $userLimit = $this->select_line("1","user");
        $userAddUser = count($userLimit)-1;
        $userLimit[$userAddUser] = $this->sess['sess_uid'];
        $userLimit[$userAddUser+1] = "";
        $this->update2("1","user",$userLimit);
        @chmod($this->file,0777);


        $this->showMsg($postShowMsg,"./index.php?mods=topicdisplay&forumid={$this->input['forumid']}&postid={$this->input['postid']}&p=$page_num");


    } //end func

} // end class


?>