<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
	by it308.com
*/
class EmailAjax extends MY_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->library('mysession');
		header('Content-type:text/json');
	}
	//获取用户的模版
	public function getTemp(){
		$this->ajax_login();
		$tid = $this->input->post('tid');
		if( !$tid ){
			$res = array(
				'status' => -1,
				'msg' => '请提供一个正确的模版！',
			);
			exit (json_encode((object)$res));
		}
		//找到模版并返回
		$this->load->model('temp_m');
		$tmp = $this->temp_m->get(array('id'=>$tid, 'uid'=>$this->user['id']));
		if(count($tmp)<=0){
			$res = array(
				'status' => -1,
				'msg' => '未找到模版，请刷新重试！',
			);
			exit (json_encode((object)$res));
		}
		$workPath = getcwd().'/public/kindue/plugins/template/upLoadTemp/'.md5($this->user['id'])."/";
		$html = file_get_contents($workPath . $tmp[0]->name);
		$res = array(
			'status' => 1,
			'msg' => '提示信息！',
			'html' => $html
		);
		echo json_encode((object)$res);
	}
	public function emailsend(){
		$this->ajax_login();
		error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
		$this->load->model('emailGroup_m');
		$this->load->model('users_m');
		$this->load->model('emailsend_m');
		$this->load->model('email_m');
		//获取发送者信息
		$eid = $this->input->post('eid');
		$type = $this->input->post('type');
		$res = $this->email_m->get(array('id'=>$eid, 'uid'=>$this->user['id']));
		$content = $res[0]->email;				//发送内容
		$title = $res[0]->title;						//标题
		$file = explode(';', $res[0]->file);								//http://url.cn/0yZrmU  http://url.cn/0boOWn  http://url.cn/3RKCR7 fk
		$file = clear_Array($file);
		
		//获取发送者信息
		$this->load->model('usetting_m');
		$setting = $this->usetting_m->get(array('uid'=>$this->user['id']));
		if(!$setting[0]->email){
			$res = array(
				'status' => -1,
				'msg' => '你的邮箱地址没有设置！',
			);
			exit (json_encode((object)$res));
		}
		if(!$setting[0]->pass){
			$res = array(
				'status' => -1,
				'msg' => '你的邮箱密码没有设置！',
			);
			exit (json_encode((object)$res));
		}
		if(!$setting[0]->smtp){
			$res = array(
				'status' => -1,
				'msg' => 'MTP服务器没有设置！',
			);
			exit (json_encode((object)$res));
		}
		//发件
		$this->load->library('email');
			//配置发送者参数
		$config['protocol'] = 'smtp';
		$config['smtp_host'] = $setting[0]->smtp;
		$config['smtp_user'] = $setting[0]->email;
		$config['smtp_pass'] = $setting[0]->pass;
		$config['mailtype'] = 'html';
		$config['validate'] = true;
		$config['priority'] = 1;
		$config['crlf']  = "\\r\\n";
		$config['smtp_port'] = 25;
		$config['charset'] = 'utf-8';
		$config['wordwrap'] = TRUE;
		$workPath = getcwd().'/upload/'.md5($this->user['id'])."/";
		$this->email->initialize($config);
		foreach ($file as $key => $value) {
			$this->email->attach(utogbk($workPath . $value));
		}
		$this->email->subject($title);				//发送标题
		$this->email->message($content);		//主内容
		
		
		//标志  用来标记主发件  是否完成
		$emailFlag = true;
		if($type==0){
			$sendemail = $this->emailsend_m->get(array('eid'=>$eid));
		}elseif($type==-1){
			$sendemail = $this->emailsend_m->get(array('eid'=>$eid,  'status'=>-1));
		}
		
		foreach ($sendemail as $key => $value) {
			$send['user'] = $value->uname;			//发送人名字
			$tmp = $this->users_m->get(array('uid'=>$this->user['id'], 'id'=>$value->uid));
			$send['email'] = $tmp[0]->email;		//发送人邮箱
			
			$this->email->from($setting[0]->email, $setting[0]->sendname);//发件人
			$this->email->to($send['email']);		//接受者邮件
			$this->email->reply_to($setting[0]->email, $setting[0]->sendname);		//快速回复给发送者信息
			$status = $this->email->send();
			if($status){
				//更新发件列表状态
				$this->emailsend_m->update(array('id'=>$value->id),array('status'=>1));
			}else{
				$this->emailsend_m->update(array('id'=>$value->id),array('status'=>-1));
				if($emailFlag){
					$emailFlag = false;
				}
			}
		}
		//更新主邮件列表
		if(!$emailFlag){
			$this->email_m->update(array('id'=>$eid, 'uid'=>$this->user['id']), array('status'=>-1, 'sendtime'=>time()));
		}else{
			$this->email_m->update(array('id'=>$eid, 'uid'=>$this->user['id']), array('status'=>1, 'sendtime'=>time()));
		}
		
		if($emailFlag){
			$res = array(
				'status' => 1,
				'msg' => '所有邮件发送完毕！',
			);
			echo (json_encode((object)$res));
		}else{
			$res = array(
				'status' => -1,
				'msg' => '有邮件发送失败的，请点击"状态查看并重新发送"！',
			);
			echo (json_encode((object)$res));
		}
		
	}
	
	
	/**
	 * 预处理邮件    读取所有当前在列表里面的邮件  一一保存到  代发件数据库
	 */
	public function emailpro(){
		$this->ajax_login();
		$eid = $this->input->post('eid');
		$type = $this->input->post('type');
		if( !$eid ){
			$res = array(
				'status' => -1,
				'msg' => '未找到邮件！',
			);
			exit (json_encode((object)$res));
		}
		//检查权限
		$this->load->model('email_m');
		$res = $this->email_m->get(array('id'=>$eid, 'uid'=>$this->user['id']));
		if(count($res)<=0){
			$res = array(
				'status' => -1,
				'msg' => '未找到邮件！',
			);
			exit (json_encode((object)$res));
		}
		
		
		//开始预处理预发送的邮件
			//找到这封邮件要发送的分组
		$this->load->model('emailGroup_m');
		$this->load->model('users_m');
		$this->load->model('emailsend_m');
		
		//预处理前，删除所有属于当前邮件以前发送的邮件列表
		if($type==-1){
			$res = $this->emailsend_m->get(array('eid'=>$eid));
			
			if(count($res)>=1){
				$res = array(
					'status' => 1,
					'msg' => '正确处理！',
				);
				exit (json_encode((object)$res));
			}
		}
		
		$this->emailsend_m->delete(array('eid'=>$eid));
		$res = $this->emailGroup_m->get(array('eid'=>$res[0]->id));			//获取归属于当前邮件的所有分组
		//找到所有用户
		$userList = array();
		foreach ($res as $key => $value) {
			$uesr = $this->users_m->get(array('group'=>$value->gid, 'uid'=>$this->user['id']));
			$userList[] = $uesr;
		}
		$userList = clear_Array($userList);
		$userSend = array();
		
		foreach ($userList as $key => $value) {
			foreach ($value as $k => $v) {
				$userSend[] = $v;
				
			}
		}
		//保存到数据库
		foreach ($userSend as $key => $value) {
			$this->emailsend_m->insert(array('eid'=>$eid, 'uid'=>$value->id, 'uname'=>$value->user, 'num'=>$key));
		}
		
		$res = array(
			'status' => 1,
			'msg' => '正确处理！',
		);
		echo (json_encode((object)$res));
		
	}
	
	
	
	//ajax检查前台登录
	public function ajax_login(){
		$suser = $this->mysession->userdata('emailuser');
		$this->user = $suser;
		if($suser==""){
			$res = array(
				'status' => -1,
				'msg' => '请登陆后在操作！',
			);
			exit(json_encode((object)$res));
			return false;
		}else{
			return true;
		}
	}
}