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

class Module extends CommonClass
{
    
    /**
     *构造函数
     *
     */
    function execute()
    {
        if (!$this->sess['sess_uid'] || !$this->sess['sess_uname'] || !$this->sess['sess_upass']) {
            $this->showMsg("注册用户才可以使用短信息功能，请先登陆/注册!","index.php?mods=member&action=login");
        }
        $this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid']."old.php";
        if (file_exists($this->file)) {
            $oldCount = $this->getlines();
        } else {
            $oldCount = "0";
        }
        $this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid']."new.php";
        if (file_exists($this->file)) {
            $newCount = $this->getlines();
        } else {
            $newCount = "0";
        }
        $this->oldAndNew = $newCount + $oldCount;
        $this->limit = $this->set['msgMax'] - $this->oldAndNew;
        $this->newCount = $newCount;
        switch($this->input['action']) {
            case 'show_mgs':
                $this->showMgs();
                break;
            case 'sentmgs_act':
                $this->sentMgsAct();
                break;
            case 'sentmgs_htm':
                $this->sentMgsHtm();
                break;
            case 'show_old_onemgs':
                if("1" == $this->input['mod']) {
                    $this->showOldOnemgs();
                }
                if("2" == $this->input['mod']) {
                    $this->showNewOnemgs();
                }
                if("0" == $this->input['mod']) {
                    $this->showOldOnemgs();
                }
                break;
            case 'del_mgs':
                $this->showMsg("你确定要删除所有的短信息吗？","ask");
                break;
            case 'del_mgs2':
                $this->delMgs();
                break;
            case 'del_sent_mgs':
                $this->showMsg("你确定要删除所有发送过的短信息吗？","ask");
                break;
            case 'del_sent_mgs2':
                $this->delSentMgs();
                break;
            case 'del_old':
                $this->delOld();
                break;
            case 'old_mgs':
                $this->showSentMgs();
                break;
            default:
                $this->showMgs();
                break;
        }
    } //end func

    /**
    *显示所有消息函数
    *
    */
    function showMgs()
    {
        $this->BAN = " &gt; 您的短信息";
        $header = $this->header("您的短信息");
        $recnum = $this->set['msgList'];
        $listnum = $this->set['msgPage'];
        
        $this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid']."old.php";
        $oldMgsArray = $this->select2(6,"1");
        
        $this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid']."new.php";
        $newMgsArray = $this->read_file();
        $totleMgsCode = array_merge_recursive ($newMgsArray,$oldMgsArray);
        
        $totalFile = count($totleMgsCode);
        $totalNum = intval(($totalFile)/$recnum)+1;;
        $p = $this->input['p'];
        if ( !$p ) { $p = 1; } 
        for($i = ($p-1)*$recnum; $i < $p*$recnum; $i++) { 
            $num = $i+1;
            $mgsCode = $this->make_array ($totleMgsCode[$i]);
            if(trim($mgsCode[1])) {
                $mgsTo = $mgsCode[1];
                $mgsFrom = "发送人：".$mgsCode[2];
                $mgsTitle = $mgsCode[3];
                $mgsContent = $mgsCode[4];
				if ($this->set[replaceWhenPost] != 'Y') {
					$mgsTitle = $this->replaceBadWord($mgsTitle);
					$mgsContent = $this->replaceBadWord($mgsContent);
				}
                $mgsTime = date($this->set[dateformat],$mgsCode[5]);
                if ($mgsCode[6] == "1") {
                    $mgsMod = "看过";
                }
                if ($mgsCode[6] == "2") {
                    $mgsMod = "<font color=blue>未看</font>";
                }
        
                eval ("\$mgsBody .= \"".$this->getTemplate("usermgs_1")."\";");
            }
        }
        
        $whatSee = $this->sess['sess_uname']."：您正在查看所有的短信息";
        $page = $this->getPage("$PHP_SELF?mods=usermgs",$totalNum);
        eval("\$this->outPut(\"".$this->getTemplate("usermgs1")."\");");

    }// end func

    /**
    *显示发出去的消息函数
    *
    */
    function showSentMgs()
    {
        $this->BAN = " &gt; 您的短信息";
        $header = $this->header("您的短信息");
        $recnum = $this->set['msgList'];
        $listnum = $this->set['msgPage'];
        
        $this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid']."old.php";
        $oldMgsArray = $this->select2(6,"0");
        
        $totalFile = count($oldMgsArray);
        $totalNum = intval(($totalFile)/$recnum)+1;;
        $p = $this->input['p'];
        if ( !$p ) { $p = 1; } 
        for($i = ($p-1)*$recnum; $i < $p*$recnum; $i++) {
            $num = $i+1;
            $mgsCode = $this->make_array ($oldMgsArray[$i]);
            if(trim($mgsCode[1])) {
                $mgsTo = $mgsCode[2];
                $mgsFrom = "接受人：".$mgsCode[1];
                $mgsTitle = $mgsCode[3];
                $mgsContent = $mgsCode[4];
				// added by cx for bad words replacement
				if ($this->set[replaceWhenPost] != 'Y') {
					$mgsTitle = $this->replaceBadWord($mgsTitle);
					$mgsContent = $this->replaceBadWord($mgsContent);
				}
                $mgsTime = date($this->set[dateformat],$mgsCode[5]);
                $mgsMod = "发送";
        
                eval ("\$mgsBody .= \"".$this->getTemplate("usermgs_1")."\";");
            }
        }
         
        $whatSee = $this->sess['sess_uname']."：您正在查看所有发出去的短信息";
        $page = $this->getPage("$PHP_SELF?mods=usermgs&action=old_mgs",$totalNum);
        eval("\$this->outPut(\"".$this->getTemplate("usermgs1")."\");");

    }// end func
    
    /**
    *显示单个旧的消息函数
    *
    */
    function showOldOnemgs()
    {
        $this->BAN = " &gt; <a href=\"./index.php?mods=usermgs\">您的短信息</a> &gt; 阅读短信";
        $header = $this->header("您的短信息");

        $this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid']."old.php";
        $oldMgsCode = $this->openfile();
        $totalFile = $this->getlines();
        for ($i=0; $i<=$totalFile - 1; $i++) {
            if ($oldMgsCode[$i][5] == $this->input['id']) {
                $mgsTo = $oldMgsCode[$i][1];
                $mgsFrom = $oldMgsCode[$i][2];
                $mgsTitle = $oldMgsCode[$i][3];
                $mgsContent = $oldMgsCode[$i][4];
				if ($this->set[replaceWhenPost] != 'Y') {
					$mgsTitle = $this->replaceBadWord($mgsTitle);
					$mgsContent = $this->replaceBadWord($mgsContent);
				}
                $mgsTime = $oldMgsCode[$i][5];
                $mgsMod = $oldMgsCode[$i][6];
            }
        }
         
        eval ("\$mgsBody .= \"".$this->getTemplate("usermgs_2")."\";");
        $whatSee = $this->sess['sess_uname']."：您正在查看".$mgsFrom."给您的短信息";
        $page = "<a href='javascript:window.history.go(-1);'>返回</a>";
        eval("\$this->outPut(\"".$this->getTemplate("usermgs1")."\");");
    } // end func

    /**
    *显示单个新的消息函数
    *
    */
    function showNewOnemgs()
    {
        $this->BAN = " &gt; <a href=\"./index.php?mods=usermgs\">您的短信息</a> &gt; 阅读短信";
        $header = $this->header("您的短信息");

        $this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid']."new.php";
        $oldMgsCode = $this->openfile();
        $totalFile = $this->getlines();
        for ($i=0;$i<=$totalFile-1;$i++) {
            if ($oldMgsCode[$i][5] == $this->input['id']) {
                $mgsTo = $oldMgsCode[$i][1];
                $mgsFrom = $oldMgsCode[$i][2];
                $mgsTitle = $oldMgsCode[$i][3];
                $mgsContent = $oldMgsCode[$i][4];
				if ($this->set[replaceWhenPost] != 'Y') {
					$mgsTitle = $this->replaceBadWord($mgsTitle);
					$mgsContent = $this->replaceBadWord($mgsContent);
				}
                $mgsTime = $oldMgsCode[$i][5];
                $mgsMod = "1";
                $mgsFromId = $oldMgsCode[$i][7];
            }
        }
        $this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid']."old.php";
        $userList = array(stripslashes($this->code),$mgsTo,$mgsFrom,$mgsTitle,$mgsContent,$mgsTime,"1",$mgsFromId,"","\n");
        @$this->insert_line($userList);
        $this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid']."new.php";
        $this->delete("5",$mgsTime);
        if ($totalFile == 1) {
            unlink ($this->set[dataPath]."/users/".$this->sess['sess_uid']."new.php");
        }
        
        eval ("\$mgsBody .= \"".$this->getTemplate("usermgs_2")."\";");
        $whatSee = $this->sess['sess_uname']."：您正在查看".$mgsFrom."给您的短信息";
        $page = "<a href='./index.php?mods=usermgs'>返回</a>";
        eval("\$this->outPut(\"".$this->getTemplate("usermgs1")."\");");
    }// end func

    /**
    *发送消息显示函数
    *
    */
    function sentMgsHtm()
    {
        $this->BAN = " &gt; <a href=\"./index.php?mods=usermgs\">您的短信息</a> &gt; 发送短信";
        $header = $this->header("您的短信息");
        eval ("\$mgsBody .= \"".$this->getTemplate("usermgs_3")."\";");
        $whatSee = $this->sess['sess_uname']."：您正在准备发送短信息";
        $page = "<a href='./index.php?mods=usermgs'>返回</a>";
        eval("\$this->outPut(\"".$this->getTemplate("usermgs1")."\");");
    }// end func

    /**
    *发送消息处理显示函数
    *
    */
    function sentMgsAct()
    {
        if ($this->input['mgsTo'] == $this->sess['sess_uname']) {
            $this->showMsg("您不能给自己发送信息");
        }

        $mgsFrom = $this->sess['sess_uname'];
        $mgsTime = time();
        $mgsMod = "2";    
        $mgsFromId = $this->sess['sess_uid'];
        $mgsTo = $this->safeChars($this->input['mgsTo']);
        $mgsContent = $this->safeChars($this->input['mgsContent']);
        $mgsTitle = $this->safeChars($this->input['mgsTitle']);
		if ($this->set[replaceWhenPost] == 'Y') {
			$mgsTitle = $this->replaceBadWord($mgsTitle);
			$mgsContent = $this->replaceBadWord($mgsContent);
		}

        if (!trim($mgsTo) || !trim($mgsTitle) || !trim($mgsContent) || !trim($mgsFrom))    {
            $this->showMsg("请您把资料填写完整");
        }
        if (strlen($mgsTo) > 56 || strlen($mgsContent) > 2000 || strlen($mgsTitle) > 56) {
            $this->showMsg("您发送的信息过长");
        }

        $this->file = $this->set[dataPath]."/users/list.php";
        @$userCode = $this->openfile();
        @$totalFile = $this->getlines();
        for ($i=0;$i<=$totalFile-1;$i++) {
            $this->file = $this->set[dataPath]."/users/".$userCode[$i][1].".php";
           @$fileHotInfo = $this->get();
            if($fileHotInfo[3] == $mgsTo) {
                $mgsToId = $userCode[$i][1];
                break;    
            }
        }
        
        if (!isset($mgsToId))    {
            $this->showMsg("这个用户不存在","./index.php?mods=usermgs");
        }
        $this->file = $this->set[dataPath]."/users/".$mgsToId."new.php";
        $userList = array(stripslashes($this->code),$mgsTo,$mgsFrom,$mgsTitle,$mgsContent,$mgsTime,$mgsMod,$mgsFromId,"","\n");
        @$this->insert_line($userList);
        
        $this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid']."old.php";
        $userList = array(stripslashes($this->code),$mgsTo,$mgsFrom,$mgsTitle,$mgsContent,$mgsTime,"0",$mgsFromId,"","\n");
        @$this->insert_line($userList);
        $this->showMsg("您已经成功发送短信息","./index.php?mods=usermgs");
    }// end func

    /**
    *删除旧的消息
    *
    */
    function delOld() 
    {
        $this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid']."old.php";
        $this->delete("5",$this->input['delOldId']);

        $this->showMsg("您已经成功清除信息","./index.php?mods=usermgs");
    }// end func

    /**
    *删除所有旧的消息
    *
    */
    function delMgs() 
    {
        if (file_exists($this->set[dataPath]."/users/".$this->sess['sess_uid']."old.php")) {
            unlink($this->set[dataPath]."/users/".$this->sess['sess_uid']."old.php");
        }
        if (file_exists($this->set[dataPath]."/users/".$this->sess['sess_uid']."new.php")) {
            unlink($this->set[dataPath]."/users/".$this->sess['sess_uid']."new.php");
        }

        $this->showMsg("您已经成功清除信息","./index.php?mods=usermgs");
    }// end func

    /**
    *删除所有旧的消息
    *
    */
    function delSentMgs() 
    {
        $this->file = $this->set[dataPath]."/users/".$this->sess['sess_uid']."old.php";
        @$this->delete("6","0");

        $this->showMsg("您已经成功清除信息","./index.php?mods=usermgs");
    }// end func

}// end class
?>