<?php
/*
\* ===========================================
\* 项目: CTB文本论坛
\* 版本: 1.0
\* 团队: 实易数码
\* 小组: 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 'userEmail':
                $this->userEmail();
                break;
            case 'doSentEmail':
                $this->doSentEmail();
                break;
			//
        }
    } // end func

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

//=============================================================================
//发送邮件页面
    function userEmail(){
        //获取历史发送记录
        $this->file = "../".$this->set[dataPath]."/adminoldemail.php";
        $systemuser = $this->openfile();
        $num = 0;
    	for($i=0;$i<count($systemuser);$i++){
            $num++;
            $sentTime = date("Y-m-d H:i:s",$systemuser[$i][5]);
            $oldEmailList .= " <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>$sentTime</td>
            <td  align=center><a href=\"./main.php?mod=usermsg&action=delSentMsg&id=".$systemuser[$i][5]."\">删除</a></td>
         </tr>";
        }
    	eval("\$this->outPut(\"".$this->getTemplate("userEmail")."\");");
    } // end func

//=============================================================================
//email群发操作
    function doSentEmail(){

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

			}
		}
        $emailTime = time();
		
		//获取发送个数和for开始单位
		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_emailto']) $_SESSION['sess_emailto']= $this->input['emailTo'];
        if (!$_SESSION['sess_emailtitle']) $_SESSION['sess_emailtitle'] = $this->input['emailTitle'];
		if (!$_SESSION['sess_emailcontent']) $_SESSION['sess_emailcontent'] = $this->input['emailContent'];
        if (!$_SESSION['sess_emailnum']) $_SESSION['sess_emailnum'] =  $this->input['emailNum'];
		if (!$_SESSION['sess_sentmode']) $_SESSION['sess_sentmode'] =  $this->input['sentMode'];
        if (!$_SESSION['sess_emailmode']) $_SESSION['sess_emailmode'] =  $this->input['emailMode'];

		/*
		echo "获取用户资料：<br>";
		echo $_SESSION['sess_emailto']."<br>";
        echo $_SESSION['sess_emailtitle']."<br>";
		echo $_SESSION['sess_emailcontent']."<br>";
		echo $_SESSION['sess_emailnum']."<br>";
		echo $_SESSION['sess_sentmode']."<br>";
		echo $_SESSION['sess_emailmode'];
            $_SESSION['sess_emailto']="";
			$_SESSION['sess_emailtitle']="";
			$_SESSION['sess_emailcontent']="";
			$_SESSION['sess_emailnum']="";
			$_SESSION['sess_sentmode']="";
			$_SESSION['sess_emailmode']="";
		exit();
		*/
        //发送指定情况
        if ($_SESSION['sess_sentmode'] == "user"){
            $emailToOne = explode("|",$_SESSION['sess_emailto']);
			$getAllNum = count($emailToOne);
			if($getAllNum>$_SESSION['sess_emailnum'] and ($sentSuccessNum+$_SESSION['sess_emailnum'])<$getAllNum){
                $getI = $sentSuccessNum+$_SESSION['sess_emailnum'];
			}
			else{
                $getI = $getAllNum;
			}
            $sentToEmailArray = array();
            for ($i=$sentSuccessNum;$i<$getI;$i++) {
            //通过用户名字，取得其存储文件
                $sentToEmail = $this->getEmailFromName($emailToOne[$i]);
				$sentSuccessNum++;
                if ($sentToEmail){  
                    //执行发送邮件
					$sentToEmailArray[] = $sentToEmail;
                    //$this->sendSmallEmail($sentToEmail, $_SESSION['sess_emailtitle'], $_SESSION['sess_emailcontent'], $_SESSION['sess_emailmode'],$this->set[emailAdmin]);
                    //写入备份
					$this->file = "../".$this->set[dataPath]."/adminoldemail".".php";
                    $userlist = array(stripslashes($this->code),$emailToOne[$i],$this->sess['sess_adminname'],$_SESSION['sess_emailtitle'],$_SESSION['sess_emailcontent'],$emailTime,"0","","","\n");
                    @$this->insert_line($userlist);
                }
            }//end for 
            $sentToEmail = join(",",$sentToEmailArray);
			$this->sendSmallEmail($sentToEmail, $_SESSION['sess_emailtitle'], $_SESSION['sess_emailcontent'], $_SESSION['sess_emailmode'],$this->set[emailAdmin]);


         //发送全体情况
         }elseif ($_SESSION['sess_sentmode'] == "all"){
            $this->file = "../".$this->set[dataPath]."/users/list.php";
            $emailToOne = $this->openfile();
			$getAllNum = count($emailToOne);
			if($getAllNum>$_SESSION['sess_emailnum'] and (sentSuccessNum+$_SESSION['sess_emailnum'])<$getAllNum){
                $getI = $sentSuccessNum+$_SESSION['sess_emailnum'];
			}
			else{
                $getI = $getAllNum;
			}

            $sentToEmailArray = array();
            for ($i=$sentSuccessNum;$i<$getI;$i++) {
            //通过用户名字，取得其存储文件
                $sentToEmail = $this->getEmailFromId($emailToOne[$i][1]);
				$sentSuccessNum++;
                if ($sentToEmail){

                    $sentToEmailArray[] = $sentToEmail;
					//执行发送邮件操作
                    //$this->sendSmallEmail($sentToEmail, $_SESSION['sess_emailtitle'], $_SESSION['sess_emailcontent'], $_SESSION['sess_emailmode'],$this->set[emailAdmin]);

                }
            }//end for 
            //执行发送
            $sentToEmail = join(",",$sentToEmailArray);
            $this->sendSmallEmail($sentToEmail, $_SESSION['sess_emailtitle'], $_SESSION['sess_emailcontent'], $_SESSION['sess_emailmode'],$this->set[emailAdmin]);

			//写入备份
			if($this->input['sentMode']=="all"){
				$this->file =  "../".$this->set[dataPath]."/adminoldemail".".php";
				$userlist = array(stripslashes($this->code),"全体注册用户",$this->sess['sess_adminname'],$_SESSION['sess_emailtitle'],$_SESSION['sess_emailcontent'],$emailTime,"0","","","\n");
				@$this->insert_line($userlist);
			}

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

		$sentLeave = $getAllNum-$sentSuccessNum;
		if($sentLeave<0){
            $sentLeave =0;
		}
		if ($sentLeave==0){
            $_SESSION['sess_emailto']="";
			$_SESSION['sess_emailtitle']="";
			$_SESSION['sess_emailcontent']="";
			$_SESSION['sess_emailnum']="";
			$_SESSION['sess_sentmode']="";
			$_SESSION['sess_emailmode']="";

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




//=============================================================================
//通过用户名字取得email
    function getEmailFromName($userName){
        if($userId = $this->getIdFromName($userName)){
			$this->file = "../".$this->set[dataPath]."/users/".$userId.".php";
			$sendInfo = $this->get();
			$userEmail = $sendInfo[5];
			if($userEmail){
		        return $userEmail;
			}else{
                return false;
			}
		}
		else{
            return false;
		}
	} 
    function getEmailFromId($userId){
			$this->file = "../".$this->set[dataPath]."/users/".$userId.".php";
			$sendInfo = $this->get();
			$userEmail = $sendInfo[5];
			if($userEmail){
		        return $userEmail;
			}else{
                return false;
			}
	} 
//<<
//=============================================================================
} //end class
