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


class Module extends CommonClass
{

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

        $check = $this->checkForum($this->input['forumid']);
    	if ($check[0] == "0") {
            $this->showMsg("您无权查看这个版块,若想查看，请联系管理员","./index.php");
        }
    	if ($check[1] == "0") {
            $this->showMsg("只有登陆用户才能查看此版块，请先登陆/注册!","index.php?mods=member&action=login");
        }
		
		$this->file = $this->set[dataPath]."/forums.php";             
		$forums = $this->read_file();
		$optionLine="<option value=\"no\">跳转论坛至...</option>";
		foreach ($forums as $line) {
			$optionLineCode = $this->make_array($line);
			if ($optionLineCode[1] == "forum") {
				$optionLine .= $optionLineCode[2] != $this->input['forumid'] ? "<option value=".$optionLineCode[2].">　|-".$optionLineCode[3]."</option>" : "<option value=".$optionLineCode[2]." selected=\"selected\">　|-".$optionLineCode[3]."</option>";  // by cx
			}
			if ($optionLineCode[1] == "cat") {
				$optionLine.="<option value=\"no\">╋".$optionLineCode[3]."</option>";
			}
			if($optionLineCode[2]==$this->input['forumid']){
			  $this->forum = $optionLineCode;
			}
		}
		$this->forumList="<select name='forumid' class='input5' onchange=\"window.location=('./index.php?mods=forumdisplay&forumid='+this.options[this.selectedIndex].value+'');\">".$optionLine."</select>";  // by cx forumjump
                
        switch ($this->input['action']) {
            case 'next':
                $this->NextPrev(1);
                break;
            case 'prev':
                $this->NextPrev(2);
                break;
            case "say":
                $this->sayRead();
                break;
            case "print":
                $this->printMode();
            	break;
            case "sentFriend":
                $this->sentFriend();
            	break;
            case "doSent":
                $this->doSent();
            	break;
            default:
                $this->postRead();
                break;
        }
            
        $this->addOnline();
    } // end func

    /**
     *跳转到上一/下一主题
     *
     */
	function NextPrev($next_prev)
	{
        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/list.php";
    	if (!file_exists($this->file)) {
            $this->showMsg("无效的主题");
        }

		$next_prev_line = $this->select_next_prev_line(1, $this->input['postid'], $next_prev);
		if ($next_prev_line == 0 or $next_prev_line[0] == '') {
			if ($next_prev == 1) {
				$this->showMsg("抱歉，本讨论区没有找到更新的主题");
			} else {
				$this->showMsg("抱歉，本讨论区没有找到更早的主题");
			}
		} else {
			header("Location: ./index.php?mods=topicdisplay&forumid={$this->input['forumid']}&postid=$next_prev_line[1]");
			exit;
		}
	}

	/**
     *显示公告内容
     *
     */
	function sayRead()
    {

    	if ($this->input['forumid'] == "all")  {
            $this->BAN = " &gt; 本站公告";

            $header = $this->header("本站公告");
            $this->file = $this->set[dataPath]."/announce.php";
        }
    	else {
            $this->file = $this->set[dataPath]."/forums.php";
            $forum = $this->select_line("2",$this->input['forumid']);
            $this->BAN = " &gt; <a href=index.php?mods=forumdisplay&forumid={$this->input['forumid']}>".$forum[3]."</a> &gt; 本版公告";
            $header = $this->header("本版公告");
            $this->file = $this->set[dataPath]."/".$this->input['forumid']."/announce.php";
        }
    	if (file_exists($this->file)) {
            $sayInfo = $this->read_file();
            $i=1;
        	foreach ($sayInfo as $sayLine) {
                $num = $i++;
                $sayLineCode = $this->make_array($sayLine);
                $sayTime = date($this->set['dateformat'],$sayLineCode[1]);
                $sayUserid = $sayLineCode[4];
                $sayUser = $sayLineCode[5];
                $sayTopic = $sayLineCode[2];
                $sayWord = $this->ctbCode($sayLineCode[3]);
				$sayPic = $sayLineCode[6];
            	eval ("\$sayBody .= \"".$this->getTemplate("forumSayRead")."\";");
                $sayLineCode[7]++;
                $this->update("1",$sayLineCode[1],$sayLineCode);
            }
        	eval("\$this->outPut(\"".$this->getTemplate("forumSayRead2")."\");");
        }
    }

    /**
     *显示帖子内容
     *
     */
	function postRead()
    {

        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid']."_.php";
    	if (!file_exists($this->file)) {
            $this->showMsg("无效的主题");
        }
        $line = $this->get();
        $this->BAN = " &gt; <a href=\"index.php?mods=forumdisplay&forumid={$this->input['forumid']}\">".$this->forum[3]."</a> &gt; ".$line[2];
        $header = $this->header($line[2]);
        $postTitleFirst = $line[2];
        if ($line[13] == "vote"){
            $pollEndTime = date($this->set[dateformat],$line[15]);
            $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid'].".php4";
            $pollItems = $this->openfile();
            if ($line[16] == "0") {
                $pollTotalNumber = "1";
            }else{
                $pollTotalNumber = $line[16];
            }
             
            for ($i=1;$i<=(count($pollItems[0])-3)/2;$i++) { 
                if ($pollItems[0][$i*2+1] && $pollItems[0][$i*2]>=0) {
                    $pollWidth = ($pollItems[0][$i*2]/$pollTotalNumber)*400;
					$pollItems[0][$i*2+1] = $this->set[replaceWhenPost] == 'Y' ? $pollItems[0][$i*2+1] : $this->replaceBadWord($pollItems[0][$i*2+1]);
                if ($line[14] == "0"){
                    $pollShow .= "
                    <tr>
                    <td>&nbsp;
                      <input type=\"radio\" name=\"voteId\" value=\"$i\">{$pollItems[0][$i*2+1]}:
                    </td>
                    <td valign=middle><img src=\"{$this->tplPath}/images/poll/$i.gif\" width=$pollWidth height=10 align=absMiddle>&nbsp;{$pollItems[0][$i*2]} 票.
                    </td>
                  </tr>";
                }
                if ($line[14] == "1"){
                    $pollShow .= "
                    <tr>
                    <td>&nbsp;
                      <input type=\"checkbox\" name=\"voteId[$i]\" value=\"1\">{$pollItems[0][$i*2+1]}:
                    </td>
                    <td valign=middle><img src=\"{$this->tplPath}/images/poll/$i.gif\" width=$pollWidth height=10 align=absMiddle>&nbsp;{$pollItems[0][$i*2]} 票.
                    </td>
                    </tr>";
                }
                //end
            }
            }
            $pollCheck = "您还没有参与本次投票。<input type=submit value=Vote class=\"input1\">";
            for ($i=2;$i<count($pollItems[1]);$i++) { 
                if($this->sess['sess_uid'] == $pollItems[1][$i]) {
                  $pollCheck = "您已经参与了本次投票，谢谢您的参与。";
                  break;
                }
            }
            if ($line[15] < time()) {
                $pollCheck = "本投票已经超过了投票期，被系统自动锁定了。";
            }
            if (!$this->sess['sess_uid']) {
                $pollCheck = "若您想参与投票，请先登陆，谢谢。";
            }
            if ($line[17] == 1) {
                $pollCheck = "<font color=red>此投票已经被管理员锁定，若有问题请联系管理员。</font>";
            }

            eval ("\$voteBody .= \"".$this->getTemplate("postReadVote")."\";");
        }
        
        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid'].".php";
    	if (!file_exists($this->file)) {
            $this->showMsg("无效的主题");
        }
        $postLine = $this->openfile();
        $totalFile = count($postLine); //总的帖子数量
        $recnum = $this->set[postList];
        $totalNumber = ($totalFile % $recnum) == "0" ? intval(($totalFile)/$recnum) : intval(($totalFile)/$recnum)+1;
        $p = $this->input['p'];
    	if ( !$p ) { $p = 1; }
    	for( $i = ($p-1) * $recnum; $i < $recnum * $p; $i++) { 
        	if ($postLine[$i][2] != "") {
                unset($postTitle);
                unset($postTime);
                unset($postAuthor);
                unset($signature);
                $postTitle = $postLine[$i][2];
                $postTime = date($this->set[dateformat],$postLine[$i][4]);
                $this->file = $this->set[dataPath]."/users/".$postLine[$i][5].".php";
            	if (file_exists($this->file)) {
					if(!$sig[$postLine[$i][5]]){
                        $sig[$postLine[$i][5]] = $this->get();
					}
                	if ($postLine[$i][9] == "0" && $postLine[$i][10] == "0") {
                    	if ($postLine[$i][11] == "0"){
                            $postBody = $this->ctbCode($postLine[$i][3],1,1);
                        }
                    	if ($postLine[$i][11] == "1"){
                            $postBody = $this->ctbCode($postLine[$i][3],1);
                        }
                    }elseif ($postLine[$i][9] == "0" && $postLine[$i][10]=="1") {
                    	if ($postLine[$i][11] == "0"){
                            $postBody = $this->ctbCode($postLine[$i][3],0,1);
                        }
                    	if ($postLine[$i][11] == "1"){
                            $postBody = $this->ctbCode($postLine[$i][3],0);
                        }
                    }elseif ($postLine[$i][9]=="1"){
                        $postBody = $postLine[$i][3];
                    }else{
                        $postBody = $this->ctbCode($postLine[$i][3],1,1);
                    }
                    
                    $signature = $this->ctbCode($sig[$postLine[$i][5]][15]);
                	if (!empty($signature) && $postLine[$i][12]==0) {
                        $signature = "<br><img src='{$this->tplPath}/images/system/signature.gif' height='16'  width='363'><br>".nl2br($signature);
                    }else{
                        $signature = "";
                    }

					if ($this->set[replaceWhenPost] != 'Y') {
						$postTitle = $this->replaceBadWord($postTitle);
						$postBody = $this->replaceBadWord($postBody);
						$signature = $this->replaceBadWord($signature);
					}

                    $postAuthor = $sig[$postLine[$i][5]][3];
                    $postAuthorId = $sig[$postLine[$i][5]][1];
                    $postTotal = $sig[$postLine[$i][5]][13];
                	if ($sig[$postLine[$i][5]][8] != "") {
                        $showQQ = "<a href=\"http://search.tencent.com/cgi-bin/friend/user_show_info?ln={$sig[$postLine[$i][5]][8]}\" target=\"blank\"><img src=\"{$this->tplPath}/images/system/person_qq.gif\" width=16 height=16 alt=\"$postAuthor 的QQ是{$sig[$postLine[$i][5]][8]}\" border=0></a>";
                    }else{
                        $showQQ = "";
                    }
                    
                    if ($sig[$postLine[$i][5]][6]!=""){
                        $showHomePage = "<a href=\"{$sig[$postLine[$i][5]][6]}\" target=\"blank\"><img src=\"{$this->tplPath}/images/system/home.gif\" width=16 height=16 alt=\"查看 $postAuthor 的主页\" border=0></a>";
                    }else {
                        $showHomePage = '';
                    }
                    
                	if (empty($sig[$postLine[$i][5]][14])) {
                        $postFace = "{$this->tplPath}/images/face/no_portait.gif";     
                    } else {
						$postFaceInfo = explode("|",$sig[$postLine[$i][5]][14]);
						if ($postFaceInfo[2] == "1"){
                            $postFace = $this->set[dataPath]."/upface/".$postFaceInfo[1];
						}
						else 
                            $postFace = "{$this->tplPath}/images/face/".$postFaceInfo[0];
                    }
                	if (empty($sig[$postLine[$i][5]][7])) {
                        $postFrom = "未知";     
                    } else {
                        $postFrom = $sig[$postLine[$i][5]][7];
                    }
                    
                    $postAuthorTitle = $sig[$postLine[$i][5]][16];
                    $postAuthorStar = $sig[$postLine[$i][5]][17];
                    $postAuthorTime = date("Y-m-d",$sig[$postLine[$i][5]][11]);
                    $topicPic = $line[7];

					$sig[$postLine[$i][5]][23]=trim($sig[$postLine[$i][5]][23]);
					if (!empty($sig[$postLine[$i][5]][23]) and $sig[$postLine[$i][5]][23]) {
					   $postAuthorHonor ="<IMG border=0 src=\"{$this->tplPath}/images/system/teamad.gif\" width=16><b>".$sig[$postLine[$i][5]][23]."</b><br>";
					}
					else $postAuthorHonor ="";
                              
                    unset($postQuote);
                    if (!empty($postLine[$i][6]) && !empty($postLine[$i][7])) {
                        $postQuoteInfo = $this->ctbCode($postLine[$i][7]);
						$postQuoteInfo = $this->set[replaceWhenPost] == 'Y' ? $postQuoteInfo : $this->replaceBadWord($postQuoteInfo);

                        eval ("\$postQuote = \"".$this->getTemplate("postQuote")."\";");
                    }
                    
                    $topicid = $postLine[$i][1];
                    $postGrade = $sig[$postLine[$i][5]][18];
                	if($postLine[$i][8] == "0"  || empty($postLine[$i][8])) {
                        $is_no = "disabled";
                    }
                    if($postLine[$i][8] == "1") {
                        $is_no = "";
                    }
					if ($postLine[$i][1] == "1"){
					$getUpload = trim($line[11]);
                    if (!empty($getUpload)) {
                       $uploadFiles = explode("|",$getUpload);
                       $downLoadNum = explode("|",$line[12]);
					   if ($line[9] == "0") {
					       $uploadMessage = "<font color = red>上传附件禁止</font>";
					   }
					   else {
                          $uploadMessage = "<font color = #005177>上传附件允许</font>(<i>最小发贴:<font color=red>{$this->set[uploadFileLimit]}</font></i>)";
					   }
					   if ($line[10] == "0") {
					   $downloadMessage = ">><font color = red>下载附件禁止</font>";
					   }
					   else{
                       $downloadMessage = ">><font color = #005177>下载附件允许</font>(<i>最小发贴:<font color=red>{$this->set[downloadFileLimit]}</font></i>)";
					   }
					   $uploadFileSize = $this->set[uploadFileSize]/1000;
					   $uploadFileShow = "<img src=\"{$this->tplPath}/images/system/fujian.gif\"  >&nbsp;以下是本帖子所带附件(<i>要求:<<font color = red>$uploadFileSize</font>k</i>)>>$uploadMessage$downloadMessage<br><table align=center border=0 cellPadding=1 cellSpacing=1 width=100%><tr height=1><td colspan=7><hr color=#f5f5f5></td></tr>";
					   for ($j=0; $j<count($uploadFiles)-1;$j++){
                           $uploadFileInfo = explode("%",$uploadFiles[$j]);
                           $uploadFileTime = date("Y-m-d H:i",$uploadFileInfo[2]);

						    $fileInfo = split("\.", $uploadFileInfo[0]);
                            $fileInfoNum=count($fileInfo);
                            $fileType = $this->getFileType($fileInfo[$fileInfoNum-1]);
						   //

                           $uploadFileShow .="<tr bgcolor=\"white\"><td width=25%>".$fileType."&nbsp;".$uploadFileInfo[0]."</td><td width=10%>". ($uploadFileInfo[4]/1000)."k</td><td width=15%>". $uploadFileInfo[1]."</td><td width=22%>".$uploadFileTime."</td><td width=10%>".$downLoadNum[$j]."</td><td width=10%><a href=\"./index.php?mods=upload&action=downloadfile&forumid={$this->input[forumid]}&postid={$this->input[postid]}&topicid={$topicid}&downloadid=$j\"><img src=\"{$this->tplPath}/images/system/delete2.gif\" border=0 alt=\"下载：$uploadFileInfo[0]\" target=blank></a></td><td width=8%><a href=\"./index.php?mods=upload&action=deletefile&forumid={$this->input[forumid]}&postid={$this->input[postid]}&topicid={$topicid}&downloadid=$j\"><img src=\"{$this->tplPath}/images/system/delete1.gif\"  border=0 alt=\"删除：$uploadFileInfo[0]\"></a></td></tr>";
					   }
                       $uploadFileShow .= "</table>";

					}
					}
					else $uploadFileShow = "";
                    $postLine[$i][14] = trim($postLine[$i][14]);
                    if ($postLine[$i][14])  $editInfo = $postLine[$i][14];
					else $editInfo = "";
                }

            	eval ("\$showPostBody .= \"".$this->getTemplate("postReadBody")."\";");
            }
        } 
        $page = $this->getPage("$PHP_SELF?mods={$this->input['mods']}&forumid={$this->input['forumid']}&postid={$this->input['postid']}",$totalNumber);

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

        $this->topicClick();
    
    } // end func


    
    /**
     *统计人气
     *2002-4-29
     */
	function topicClick()
    {
        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid']."_.php";
        $line = $this->get();
        $line[4]++;
        $this->overwrite($line);
    } // end func


	function sayClick()
    {
    	if ($this->input['forumid'] == "all") {
            $this->file = $this->set[dataPath]."/announce.php";
        }else {
            $this->file = $this->set[dataPath]."/".$this->input['forumid']."/announce.php";
        }
        $sayLineCode[7]++;
        $this->update2("1",$sayLineCode[1],$sayLineCode);
    } // end func


    /**
     *以打印模式显示文章内容
     *
     */
	function printMode()
    {

        $this->file = $this->set[dataPath]."/forums.php";
        $forum = $this->select_line("2",$this->input['forumid']);
        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid']."_.php";
        $line = $this->get();
		$line[2] = $this->set[replaceWhenPost] == 'Y' ? $line[2] : $this->replaceBadWord($line[2]);
        $BAN = "<a href=./index.php><font color=black><b>".$this->set[ctbTitle]."</b></font></a> &gt; <a href=index.php?mods=forumdisplay&forumid={$this->input['forumid']}><font color=black><b>".$forum[3]."</b></font></a> &gt; <b>".$line[2]."</b>";
        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid'].".php";
    	if (!file_exists($this->file)) {
            $this->showMsg("无效的主题");
        }
        $postLine = $this->openfile();
    	for( $i = 0; $i < count($postLine); $i++) { 
        	if ($postLine[$i][2] != "") {
                $postBody = $this->ctbCode($postLine[$i][3]);
				$postBody = $this->set[replaceWhenPost] == 'Y' ? $postBody : $this->replaceBadWord($postBody);
                $postTime = date($this->set[dateformat],$postLine[$i][4]);
                $this->file = $this->set[dataPath]."/users/".$postLine[$i][5].".php";
            	if (file_exists($this->file)) {
                    $sig[$postLine[$i][5]] = $this->get();
                    $postAuthorId = $sig[$postLine[$i][5]][1];
                    $postAuthor = "<a href=\"index.php?mods=member&action=info&id=$postAuthorId\"><font color=black><b>".$sig[$postLine[$i][5]][3]."</b></font></a>";
                	eval ("\$printBit .= \"".$this->getTemplate("printBit")."\";");
                }
            } 
        }
    	eval("\$this->outPut(\"".$this->getTemplate("print")."\",\"0\");");
    }

    /**
     *将文章发送到指定信箱
     *
     */
	function sentFriend()
    {

    	if (!$this->sess['sess_uid'] || !$this->sess['sess_uname'] || !$this->sess['sess_upass']) {
            $this->showMsg("注册用户才可以使用发送邮件功能，请先登陆/注册!");
        }

        $this->file = $this->set[dataPath]."/forums.php";
        $forum = $this->select_line("2",$this->input['forumid']);
        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid']."_.php";
        $postTitles = $this->get();
        $this->file = $this->set[dataPath]."/".$this->input['forumid']."/".$this->input['postid'].".php";
        $postContentInfo = $this->get();
		if ($this->set[replaceWhenPost] != 'Y') {
			$postTitles[2] = $this->replaceBadWord($postTitles[2]);
			$postContentInfo[3] = $this->replaceBadWord($postContentInfo[3]);
		}
		$postContent = str_replace("<br>","\n", "\n".$postTitles[2]."\n______________________________________________\n\n".$postContentInfo[3]);
        //
        $this->BAN = " &gt; <a href=index.php?mods=forumdisplay&forumid={$this->input['forumid']}>".$forum[3]."</a> &gt; <a href=index.php?mods=topicdisplay&forumid={$this->input['forumid']}&postid={$this->input['postid']}>".$postTitles[2]."</a> &gt; 发送文章";

        $header = $this->header("发送文章");
        eval("\$this->outPut(\"".$this->getTemplate("sentFriend")."\");");
    }
    /**
     *将文章发送到指定信箱
     *
     */
	function doSent()
    {

    	if (!$this->input['emailMode'] || !$this->input['toName'] || !$this->input['toEmail'] || !$this->input['subject'] || !$this->input['message']){
            $this->showMsg("请您将资料填写完整");
        }
    	if (!$this->checkemail($this->input['toEmail'])){
            $this->showMsg("<font color=red>您的信箱地址不合法!</font>");
        }
        $subject =$this->input['subject'];
        $message = $this->input['message'];
		$this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid'].".php";
		$sendInfo = $this->get();
		$emailFrom = $this->set[ctbTitle]."的邮件<".$this->set[emailAdmin].">";
        $this->sendSmallEmail($this->input['toEmail'], $subject, $message); //$sendInfo[5]
        $postShowMsg = "您已经成功将文章发送到指定信箱<br><br>
            <li><a href=\"./index.php?mods=topicdisplay&forumid={$this->input['forumid']}&postid={$this->input['postid']}\">返回查看主题</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=$page_num");
                    
}



} // end class

?>