<?php
/*
\* ===========================================
\* 项目: CTB文本论坛
\* 版本:1.4
\* 团队: 实易数码
\* 小组: CTB文本论坛小组
\* 作者: 险情
\* 功能: 后台版块处理程式
\* ===========================================
\* Copyright (c) 2002
\* 团队主页: http://www.11cn.org
\* 团队信箱: ------
\* 创建日期: 2002/7/19
\* 修改日期: 2002-12-27
\* 修改说明: ------
\* 版权声明: 这个程序是免费软件.
\* ===========================================
*/

//=============================================================================

class Module extends CommonClass
{
    function execute()
    {
        switch($this->input['action']) {
			//
            case 'sentMsg':
                $this->sentMsg();
                break;
            case 'doSentMsg':
                $this->doSentMsg();
                break;
            case 'delSentMsg':
                $this->delSentMsg();
                break;
            default :
                $this->showAll();
			//
        }
    } // end func

//-----------------------------------------------------------------------------
//---1、显示所有论坛用户------作者：险情
//---2、
//---3、
//-----------------------------------------------------------------------------

//=============================================================================
//短信息群发
    function sentMsg(){
        //获取历史发送记录
        $this->file = "../".$this->set[dataPath]."/adminoldmsg.php";
        $systemuser = $this->openfile();
        $num = 0;
    	for($i=0;$i<count($systemuser);$i++){
            $num++;
            $sent_time = date("Y-m-d H:i:s",$systemuser[$i][5]);
            $oldmsg_list .= " <tr bgcolor='#f5f5f5'> 
            <td  align=center>$num</td>
            <td  align=center>".$systemuser[$i][1]."</td>
            <td>".$systemuser[$i][3]."</td>
            <td  align=center>".$systemuser[$i][2]."</td>
            <td  align=center>$sent_time</td>
            <td  align=center><a href=\"./main.php?mod=usermsg&action=delSentMsg&id=".$systemuser[$i][5]."\">删除</a></td>
         </tr>";
        }

        eval("\$this->outPut(\"".$this->getTemplate("msgSent")."\");");
    } // end func


//=============================================================================
//短信息群发操作
    function doSentMsg(){

		//简单格式化数据
		//exit("ddd");
		//exit($_SESSION['sess_msgto']);
		if($this->input and !$_SESSION['sess_msgtitle']){
            if ($this->input[sentMode] == "user" and !$this->input[msgTo])
                $this->adminShowWrong("main.php?mod=usermsg&action=sentMsg","您没有添加接收用户，请不要留空。");
            //$this->input = $this->inputCheck("main.php?mod=usermsg&action=sentMsg");
			if (!$this->input['msgTitle'] or !$this->input['msgContent']){
                $this->adminShowWrong("main.php?mod=usermsg&action=sentMsg","您没有添加必须数据，请不要留空。");

			}
		}
        $msgTime = time();
		if (!$this->input[sentSuccessNum] or $this->input[sentSuccessNum]<0){
            $sentSuccessNum = 0;
		}
		else{
            $sentSuccessNum = $this->input[sentSuccessNum];
		}
		if (!$this->input[sentLeave]){
            $this->input[sentLeave] = 0;
		}
             //exit("测试数据：".$sentSuccessNum);

		//将发送消息写入session
        if (!$_SESSION['sess_msgto']) $_SESSION['sess_msgto']= $this->input['msgTo'];
        if (!$_SESSION['sess_msgtitle']) $_SESSION['sess_msgtitle'] = $this->input['msgTitle'];
		if (!$_SESSION['sess_msgcontent']) $_SESSION['sess_msgcontent'] = $this->input['msgContent'];
        if (!$_SESSION['sess_msgnum']) $_SESSION['sess_msgnum'] =  $this->input['msgNum'];
		if (!$_SESSION['sess_sentmode']) $_SESSION['sess_sentmode'] =  $this->input['sentMode'];
        //exit($_SESSION['sess_sentmode']);
        //发送指定情况
        if ($_SESSION['sess_sentmode'] == "user"){
            $msgToOne = explode("|",$_SESSION['sess_msgto']);
			$getAllNum = count($msgToOne);
			if($getAllNum>$_SESSION['sess_msgnum'] and ($sentSuccessNum+$_SESSION['sess_msgnum'])<$getAllNum){
                $getI = $sentSuccessNum+$_SESSION['sess_msgnum'];
			}
			else{
                $getI = $getAllNum;
			}
            for ($i=$sentSuccessNum;$i<$getI;$i++) {
            //通过用户名字，取得其存储文件
                $sentToId = $this->getIdFromName($msgToOne[$i]);
				$sentSuccessNum++;
                if ($sentToId){   
                    //向存储文件中写入信息
                    $this->file =  "../".$this->set[dataPath]."/users/$sentToId"."new.php";
                    $userlist = array($this->code,$msgToOne[$i],$this->sess['sess_adminname'],$_SESSION['sess_msgtitle'],$_SESSION['sess_msgcontent'],$msgTime,"2",$sess_adminid,"","\n");
                    @$this->insert_line($userlist);
                    //写入备份
					$this->file = "../".$this->set[dataPath]."/adminoldmsg".".php";
                    $userlist = array($this->code,$msgToOne[$i],$this->sess['sess_adminname'],$_SESSION['sess_msgtitle'],$_SESSION['sess_msgcontent'],$msgTime,"0",$sess_adminid,"","\n");
                    @$this->insert_line($userlist);
                }
            }//end for 

         //发送全体情况
         }elseif ($_SESSION['sess_sentmode'] == "all"){
            $this->file = "../".$this->set[dataPath]."/users/list.php";
            $msgToOne = $this->openfile();
			$getAllNum = count($msgToOne);
			if($getAllNum>$_SESSION['sess_msgnum'] and (sentSuccessNum+$_SESSION['sess_msgnum'])<$getAllNum){
                $getI = $sentSuccessNum+$_SESSION['sess_msgnum'];
			}
			else{
                $getI = $getAllNum;
			}
            for ($i=$sentSuccessNum;$i<$getI;$i++) {
            //通过用户名字，取得其存储文件
                $sentToId = $msgToOne[$i][1];
                $msgTo = $msgToOne[$i][4];
				$sentSuccessNum++;
                if ($sentToId){
                    //向存储文件中写入信息
                    $this->file =  "../".$this->set[dataPath]."/users/$sentToId"."new.php";
                    $userlist = array($this->code,$msgTo,$this->sess['sess_adminname'],$_SESSION['sess_msgtitle'],$_SESSION['sess_msgcontent'],$msgTime,"2",$sess_adminid,"","\n");
                    @$this->insert_line($userlist);
                }
            }//end for 
			//写入备份
			if($this->input['sentMode']=="all"){
            $this->file =  "../".$this->set[dataPath]."/adminoldmsg".".php";
            $userlist = array($this->code,"全体注册用户",$this->sess['sess_adminname'],$_SESSION['sess_msgtitle'],$_SESSION['sess_msgcontent'],$msgTime,"0",$sess_adminid,"","\n");
            @$this->insert_line($userlist);
			}

        }else{
            $this->adminShowWrong("main.php?mod=usermsg&action=sentMsg","发送错误：请选择发送模式");
        }

		$sentLeave = $getAllNum-$sentSuccessNum;
		if($sentLeave<0){
            $sentLeave =0;
		}
		if ($sentLeave==0){
            $_SESSION['sess_msgto']="";
			$_SESSION['sess_msgtitle']="";
			$_SESSION['sess_msgcontent']="";
			$_SESSION['sess_msgnum']="";
			$_SESSION['sess_sentmode']="";
            $this->adminShowMsg("main.php?mod=usermsg&action=sentMsg","您的短信息已经成功发送，共发送成功 <b>$getAllNum</b> 个。");

		}
        $this->adminShowWrong("main.php?mod=usermsg&action=sentMsg","您的短信息已经成功发送，您共需要发送 <b>$getAllNum</b> 个<br>共发送成功 <b>$sentSuccessNum</b> 个，还有 <b>$sentLeave</b> 个没有发送。<br><a href=\"main.php?mod=usermsg&action=doSentMsg&sentLeave=$sentLeave&sentSuccessNum=$sentSuccessNum\"><b>按此处执行下一次发送</b></a>");
    } // end func

//=============================================================================
//删除一个一个历史发送消息
    function delSentMsg()
    {   
        $id = $this->input['id'];
        $this->file =  "../".$this->set[dataPath]."/adminoldmsg".".php";
        $bluser = $this->delete(5,$id);

        $this->adminShowMsg("main.php?mod=usermsg&action=sentMsg","您已经成功删除了一条历史消息");
    } // end func


//<<
//=============================================================================
} //end class
