<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
	by it308.com
*/
class Emailsystem extends MY_Controller {
	protected $data = array();
	public function __construct(){
		parent::__construct();
		$this->pageCount = 8;
		$this->load->library('mysession');
		$this->getwebconfig();
		//读取网站配置
	}
	public function index(){
		$this->check_login();
		$this->data['webname']=$this->webconfig['webname']."-网站首页";
		$this->load->view('header',$this->data);
	}
	public function settingGen(){
		$this->check_login();
		$this->data['css'] = 'setting';
		$this->data['webname']=$this->webconfig['webname']."-常规设置";
		$this->load->model('usetting_m');
		$res = $this->usetting_m->get(array('uid'=>$this->user['id']));
		$this->data['usetting'] = $res;
		$this->load->view('header',$this->data);
		$this->load->view('settingGen');
	}
	//常规设置
	public function settingGen_pro(){
		$this->check_login();
		$email = $this->input->post('email');
		$pass = $this->input->post('pass');
		$smtp =  $this->input->post('smtp');
		$smtp_port =  $this->input->post('smtp_port');
		$sendname = $this->input->post('sendname');
		$this->load->model('usetting_m');
		$res = $this->usetting_m->update(array('uid'=>$this->user['id']),array('email'=>$email,"pass"=>$pass, 'smtp'=>$smtp, 'smtp_port'=>$smtp_port, 'sendname'=>$sendname));
		redirectmy('/emailsystem/settingGen');
	}
	//模板设置
	public function settingTemp($currentPage=1){
		$this->check_login();
		$this->data['webname']=$this->webconfig['webname']."-模板设置";
		$this->load->model('temp_m');
		
		$count = count($this->temp_m->get(array('uid'=>$this->user['id'])));				//总页
		if($count>=1){
			if ($currentPage<1) {
				$currentPage = 1;
			}
			if ($currentPage>$count) {
				$currentPage = $count;
			}
			$page = ceil($count/$this->pageCount)-1;
			$data['page'] = $currentPage;
			$data['page_size'] = $this->pageCount;
			$data['total'] = $count;
			$this->load->library('pagination',$data);
			$res = $this->temp_m->getLimit(array('uid'=>$this->user['id']),($currentPage-1)*$this->pageCount,$this->pageCount);
			$this->data['temp'] = $res;
		}else{
			$this->data['temp'] = array();
		}
		
		$this->load->view('header',$this->data);
		$this->load->view('settingTemp');
	}
	//创建模版处理
	public function settingTemp_pro(){
		$this->check_login();
		$name = $this->input->post('name');
		$workPath = getcwd().'/public/kindue/plugins/template/upLoadTemp/'.md5($this->user['id'])."/";
		if ( !is_dir($workPath)){
			mkdir($workPath);
		}
		//$config['upload_path'] = './public/kindue/plugins/template/upLoadTemp/'.md5($this->user['id'])."/";
		$size = 1232896;
		$file = $_FILES['temp'];
		if($file['size'] > $size){
			$this->data['webname']=$this->webconfig['webname']."-上传错误";
			$this->data['title']="上传错误!";
			$this->data['content']='文件大小超出限制，请重新上传！';
			$this->load->view('error',$this->data);
			return false;
		}
		$this->load->helper('checkformat');
		if($file['tmp_name']==""){
			$this->data['webname']=$this->webconfig['webname']."-上传错误";
			$this->data['title']="上传错误!";
			$this->data['content']='上传文件不能为空！';
			$this->load->view('error',$this->data);
			return false;
		}
		$file_type = file_type($file['tmp_name']);
		//echo $file_type;
		if( $file_type != "" ){
			$this->data['webname']=$this->webconfig['webname']."-上传错误";
			$this->data['title']="上传错误!";
			$this->data['content']='文件类型不正确！';
			$this->load->view('error',$this->data);
			return false;
		}
		$urlname = time().rand(100,999).".html";
		$filename = $workPath.$urlname;
		//移动文件到当前用户目录
		move_uploaded_file($file['tmp_name'],$filename);
		//写库
		$data['time'] = time();
		$data['name'] = $urlname;
		$data['tempname'] = $name;
		$data['uid'] = $this->user['id'];
		$data['uname'] = $this->user['user'];
		$this->load->model('temp_m');
		$res = $this->temp_m->insert($data);
		if($res){
			redirectmy('emailsystem/settingTemp');
		}
	}
	public function settingTemp_Edit($tid = 0){
		$this->check_login();
		$this->load->model('temp_m');
		$email = $this->input->post('email');
		$id = $this->input->post('tempid');
		$name = $this->input->post('name');
		if($email && $id && $name){
			$data['tempname'] = $name;
			$res = $this->temp_m->update(array('uid'=>$this->user['id'],'id'=>$id), $data);
			if(count($res)<=0){
				$this->data['webname']=$this->webconfig['webname']."-模版错误";
				$this->data['title']="模版编辑失败!";
				$this->data['content']='请填写完整的信息！';
				$this->load->view('error',$this->data);
				return false;
			}else{
				//编辑成功，写模版数据到文件
				$workPath = getcwd().'/public/kindue/plugins/template/upLoadTemp/'.md5($this->user['id'])."/";
				$res = $this->temp_m->get(array('uid'=>$this->user['id'],'id'=>$id));
				$filename = $res[0]->name;
				file_put_contents($workPath.$filename, $email);
				redirectmy('emailsystem/settingTemp');
			}
		}
		
		if(!$tid){
			$this->data['webname']=$this->webconfig['webname']."-模版错误";
			$this->data['title']="模版无法编辑!";
			$this->data['content']='请提供正常的模版！';
			$this->load->view('error',$this->data);
			return false;
		}
		$res = $this->temp_m->get(array('uid'=>$this->user['id'],'id'=>$tid));
		if(count($res)<=0){
			$this->data['webname']=$this->webconfig['webname']."-模版错误";
			$this->data['title']="模版无法编辑!";
			$this->data['content']='请检查模版是否存在！';
			$this->load->view('error',$this->data);
			return false;
		}
		$workPath = getcwd().'/public/kindue/plugins/template/upLoadTemp/'.md5($this->user['id'])."/";
		$filename = $res[0]->name;
		$this->data['tempid'] = $res[0]->id;
		$this->data['tempname'] = $res[0]->tempname;
		$this->data['html'] = file_get_contents($workPath.$filename);
		$this->load->view('header', $this->data);
		$this->load->view('settingTempEdit');
		
	}
	public function settingTemp_del($tid = 0){
		$this->check_login();
		if(!$tid){
			$this->data['webname']=$this->webconfig['webname']."-模版错误";
			$this->data['title']="无法删除模版!";
			$this->data['content']='请提供正常的模版！';
			$this->load->view('error',$this->data);
			return false;
		}
		$this->load->model('temp_m');
		$res = $this->temp_m->get(array('uid'=>$this->user['id'],'id'=>$tid));
		$filename = $res[0]->name;
		if(count($res)<=0){
			$this->data['webname']=$this->webconfig['webname']."-模版错误";
			$this->data['title']="模版无法删除!";
			$this->data['content']='请检查模版是否存在！';
			$this->load->view('error',$this->data);
			return false;
		}
		$res = $this->temp_m->delete(array('uid'=>$this->user['id'], 'id'=>$tid));
		if(count($res)<=0){
			$this->data['webname']=$this->webconfig['webname']."-模版错误";
			$this->data['title']="模版删除失败!";
			$this->data['content']='数据库错误！';
			$this->load->view('error',$this->data);
			return false;
		}
		//删除模版数据文件
		$workPath = getcwd().'/public/kindue/plugins/template/upLoadTemp/'.md5($this->user['id'])."/";
		unlink($workPath . $filename);
		redirectmy('emailsystem/settingTemp');
	}
	public function settingTemp_view($tid = 0){
		$this->check_login();
		if(!$tid){
			$this->data['webname']=$this->webconfig['webname']."-模版错误";
			$this->data['title']="模版无法显示!";
			$this->data['content']='请提供正常的模版！';
			$this->load->view('error',$this->data);
			return false;
		}
		$this->load->model('temp_m');
		$res = $this->temp_m->get(array('uid'=>$this->user['id'],'id'=>$tid));
		if(count($res)<=0){
			$this->data['webname']=$this->webconfig['webname']."-模版错误";
			$this->data['title']="模版无法显示!";
			$this->data['content']='请检查模版是否存在！';
			$this->load->view('error',$this->data);
			return false;
		}
		//print_r($res);
		//打开模版文件
		$workPath = getcwd().'/public/kindue/plugins/template/upLoadTemp/'.md5($this->user['id'])."/";
		$filename = $res[0]->name;
		$this->data['html'] = file_get_contents($workPath.$filename);
		$this->load->view('header', $this->data);
		$this->load->view('settingTempView');
	}
	public function ue(){
		$this->load->view('ue');
	}
	//首页
	public function email($currentPage = 0){
		$this->check_login();
		$this->data['css'] = 'email';
		$this->data['webname']=$this->webconfig['webname']."-新建邮件";
		//获取所有分组
		$this->load->model('groups_m');
		$res = $this->groups_m->getNew(array('uid'=>$this->user['id']));
		$this->data['group'] = $res;
		//获取所有模版
		$this->load->model('temp_m');
		$res = $this->temp_m->get(array('uid'=>$this->user['id']));
		$this->data['temp'] = $res;
		
		//读取当前未发送邮件
		$this->load->model('email_m');
		$count = count($this->email_m->get(array('uid'=>$this->user['id'])));				//总页
		if($count>=1){
			if ($currentPage<1) {
				$currentPage = 1;
			}
			if ($currentPage>$count) {
				$currentPage = $count;
			}
			$page = ceil($count/$this->pageCount)-1;
			$data['page'] = $currentPage;
			$data['page_size'] = $this->pageCount;
			$data['total'] = $count;
			$this->load->library('pagination',$data);
			$res = $this->email_m->getLimit(array('uid'=>$this->user['id']) ,($currentPage-1)*$this->pageCount,$this->pageCount);
			$this->data['email'] = $res;
		}else{
			$this->data['email'] = array();
		}
		//分隔附件 和 他的分组
		$this->load->model('emailGroup_m');
		foreach($this->data['email'] as $key=>$val){
			$this->data['email'][$key]->file_array = explode(';', $val->file);
			$this->data['email'][$key]->file_array = clear_Array($this->data['email'][$key]->file_array);
			$this->data['email'][$key]->group = $this->emailGroup_m->getInner(array('eid'=>$val->id));
		}
		//print_r($this->data);
		$this->load->view('header',$this->data);
		$this->load->view('email');
	}
	public function emailView($eid = 0){
		$this->check_login();
		//$workPath = getcwd().'/upload/'.md5($this->user['id'])."/";
		$this->load->model('email_m');
		$this->load->model('emailGroup_m');
		$this->load->model('emailsend_m');
		
		$this->data['webname']=$this->webconfig['webname']."-邮件浏览";
		if( !$eid ){
			$this->data['webname']=$this->webconfig['webname']."-浏览邮件错误";
			$this->data['title']="浏览错误!";
			$this->data['content']='请选择一个邮件！';
			$this->load->view('error',$this->data);
			return false;
		}
		//获取这个邮件的信息
		$this->data['email'] = $this->email_m->get(array('uid'=>$this->user['id'], 'id'=>$eid));
		if( count($this->data['email'] ) <=0 ){
			$this->data['webname']=$this->webconfig['webname']."-浏览邮件错误";
			$this->data['title']="浏览错误!";
			$this->data['content']='这个邮件不存在！';
			$this->load->view('error',$this->data);
			return false;
		}
		$path = explode(';', $this->data['email'][0]->file);
		$path = clear_Array($path);
		$filePath = array();
		foreach ($path as $key => $value) {
			$filePath[$key]['path'] = base_url() . '/upload/'.md5($this->user['id'])."/" . $value;
			$filePath[$key]['name'] = $value;
		}
		$this->data['email'][0]->filePath = $filePath;
		//获取分组 
		$this->data['group'] = $this->emailGroup_m->getInner(array('eid'=>$eid));
		//获取发送成功的邮件
		$this->data['accEmail'] = count($this->emailsend_m->get(array('eid'=>$eid, 'status'=>1)));
		//获取 败的邮件
		$this->data['errEmail'] = count($this->emailsend_m->get(array('eid'=>$eid, 'status'=>-1)));
		$this->data['eid'] = $eid;
		$this->load->view('header',$this->data);
		$this->load->view('email_view');
	}
	public function view_data($eid){
		$this->check_login();
		$this->load->model('email_m');
		if( !$eid ){
			$this->data['webname']=$this->webconfig['webname']."-浏览邮件错误";
			$this->data['title']="浏览错误!";
			$this->data['content']='请选择一个邮件！';
			$this->load->view('error',$this->data);
			return false;
		}
		//获取这个邮件的信息
		$this->data['email'] = $this->email_m->get(array('uid'=>$this->user['id'], 'id'=>$eid));
		if( count($this->data['email'])<=0 ){
			$this->data['webname']=$this->webconfig['webname']."-浏览邮件错误";
			$this->data['title']="浏览错误!";
			$this->data['content']='请选择一个邮件！';
			$this->load->view('error',$this->data);
			return false;
		}
		echo $this->data['email'][0]->email;
	}
	public function emailAdd(){
		$this->check_login();
		$size = 10485760;			//10m
		$this->load->model('email_m');
		$data['title'] = $this->input->post('title');
		//$data['title'] = $this->input->post('group');
		$data['tempid'] = $this->input->post('template');			//id
		$data['date'] = $this->input->post('ddate');
		$data['email'] = $this->input->post('email');
		$data['time'] = time();
		if( !$data['title'] ){
			$this->data['webname']=$this->webconfig['webname']."-添加邮件错误";
			$this->data['title']="添加邮件错误!";
			$this->data['content']='你得给一个标题，亲！';
			$this->load->view('error',$this->data);
			return false;
		}
		if( !$data['email'] ){
			$this->data['webname']=$this->webconfig['webname']."-添加邮件错误";
			$this->data['title']="添加邮件错误!";
			$this->data['content']='没有内容发了也白发是吧！请给内容，亲！';
			$this->load->view('error',$this->data);
			return false;
		}
		$group = $this->input->post('group');
		if(count($group)<=0 or !is_array($group)){
			$this->data['webname']=$this->webconfig['webname']."-添加邮件错误";
			$this->data['title']="没有分组!";
			$this->data['content']='你至少得选择一个分组！';
			$this->load->view('error',$this->data);
			return false;
		}
		//有附件移动附件
		if(count($_FILES['file']['name'])>=1){
			foreach($_FILES['file']['size'] as $key=>$val){
				if($val>$size){
					$this->data['webname']=$this->webconfig['webname']."-添加邮件错误";
					$this->data['title']="附件错误!";
					$this->data['content']='你的附件大小已经超出10M了！';
					$this->load->view('error',$this->data);
					return false;
					break;
				}
			}
		}
		//检查目录是否存在
		$workPath = getcwd().'/upload/'.md5($this->user['id'])."/";
		if ( !is_dir($workPath)){
			mkdir($workPath);
		}
		//挪动附件
		$fileName = "";
		$filesuffix = "";
		if(count($_FILES['file']['name'])>=1){
			foreach($_FILES['file']['tmp_name'] as $key=>$val){
				//$file = $workPath . $_FILES['file']['name'][$key];		//随机名
				//$file  = time().rand(100,999) . "." .getFileSuffix($_FILES['file']['name'][$key]);
				$trueFile = $_FILES['file']['name'][$key];			//php的move无法移动
				//检测是否重名
				while( is_file(utogbk($workPath . $trueFile)) ){
					$trueFile = getFileSuffix($trueFile, 1) . "(1)." . getFileSuffix($trueFile);
					if( !is_file(utogbk($workPath . $trueFile)) ){
						break;
					}
				}
				
				$fileName .= $trueFile . ";";
				//$filesuffix .= $file . ';';
				move_uploaded_file($_FILES['file']['tmp_name'][$key], utogbk($workPath . $trueFile));
			}
		}
		//$data['innerFile'] = $filesuffix;
		$data['file'] = $fileName;
		$data['uid'] = $this->user['id'];
		$email = $this->email_m->insert($data);
		if( !$email ){
			$this->data['webname']=$this->webconfig['webname']."-添加邮件错误";
			$this->data['title']="添加邮件错误!";
			$this->data['content']='数据库错误请刷新重试！';
			$this->load->view('error',$this->data);
			return false;
		}
		
		
		$this->load->model('emailGroup_m');
		
		foreach($group as $key=>$val){
			//插入所添加的分组
			$this->emailGroup_m->insert(array('eid'=>$email, 'gid'=>$val, 'time'=>time()));
		}
		redirectmy('emailsystem/email');
	}
	public function emailDel($eid){
		$this->check_login();
		$this->load->model('email_m');
		if( !$eid ){
			$this->data['webname']=$this->webconfig['webname']."-删除邮箱错误";
			$this->data['title']="删除邮件错误!";
			$this->data['content']='未找到该邮件，请返回重试！';
			$this->load->view('error',$this->data);
			return false;
		}
		$res = $this->email_m->get(array('id'=>$eid,'uid'=>$this->user['id']));
		if(count($res)<=0){
			$this->data['webname']=$this->webconfig['webname']."-删除邮箱错误";
			$this->data['title']="删除邮件错误!";
			$this->data['content']='未找到该邮件，请返回重试！';
			$this->load->view('error',$this->data);
			return false;
		}
		//删除附件
		$res = $this->email_m->get(array('id'=>$eid, 'uid'=>$this->user['id']));
		$file =  $res[0]->file;
		$file = explode(';', $file);
		$file = clear_Array($file);
		
		$workPath = getcwd().'/upload/'.md5($this->user['id'])."/";
		foreach ($file as $key => $value) {
			if( is_file(utogbk($workPath . $value)) ){
				unlink(utogbk($workPath . $value));
			}
		}
		//删除邮件
		$this->email_m->delete(array('id'=>$eid, 'uid'=>$this->user['id']));
		//删除分组
		$this->load->model('emailGroup_m');
		$this->emailGroup_m->delete(array('eid'=>$eid));
		redirectmy('emailsystem/email');
		
	}
	
	public function users(){
		$this->check_login();
		$this->data['css'] = 'users';
		$this->data['webname']=$this->webconfig['webname']."-用户管理";
		$this->load->model('users_m');
		$users = $this->input->get('users');
		$group = $this->input->get('group');
		$currentPage = $this->input->get('page') ? $this->input->get('page') : 1;
		
		/*
		parse_str($this->input->server('QUERY_STRING'), $getParams);
		//$users = $this->input->get('users');
		$users="";yjuy
		$group="";
		if(isset($getParams['all'])){
			$users = $getParams['all'];
		}
		if(isset($getParams['group'])){
			$group = $getParams['group'];
		}*/
		//$group = $this->input->get('group');
		
		$where['uid'] = $this->user['id'];
		$whereTmp['users.uid'] = $where['uid'];
		$wherelinkUsers = "";
		$wherelinkGroup = "";
		$wherelike = "";
		if($users!=""){
			if($users!="all"){		//不是查询所有
				//$where['userword'] = $users;
				$wherelike = $users;			//['users.userword'] 
				///$whereTmp['users.userword like "%'] = $users.'%"';
				$wherelinkUsers .= "&users={$users}";			//字母导航link
			}
		}
		if($group){
			$where['group'] = $group;					
			$whereTmp['users.group'] = $group;
			$wherelinkGroup .= "&group={$group}";			//字母导航link
		}
		$this->data['where'] = $where;
		$count = count($this->users_m->get($where,$wherelike));			//14
		//$spdata = $this->users_m->get($where,$wherelike);					//首拼数据要用这里的 ，不要用查询分页出来的数据
		$spdata = $this->users_m->get(array("uid"=>$this->user['id']));					//首拼数据要用这里的 ，不要用查询分页出来的数据
		
		$this->data['count'] = $count;
		if($count>=1){
			if ($currentPage<1) {
				$currentPage = 1;
			}
			if ($currentPage>$count) {
				$currentPage = $count;
			}
			$page = ceil($count/$this->pageCount)-1;
			$data['page'] = $currentPage;
			$data['page_size'] = $this->pageCount;
			$data['total'] = $count;
			//$data['extlink'] = $extlink;
			$this->load->library('paginationres',$data);
			$res = $this->users_m->getLimit($whereTmp,$wherelike,($currentPage-1)*$this->pageCount,$this->pageCount);
			$this->data['users'] = $res;
		}else{
			$this->data['users'] = array();
		}
		//这里处理首拼
		$sp = $spdata;
		$spstr = "";
		for ($i=48; $i < 57; $i++) {
			$char = chr($i);
			$istrue = false;
			foreach ($sp as $key => $value) {
				if (substr($value->userword, 0,1)==$char){
					$istrue = true;
				}
			}
			if(!$istrue){			//users=f
				$spstr .= "<a href='javascript:void(0);' class='btn btn-small btn-inverse disabled'>{$char}</a>";
			}else{
				$spstr .= "<a href='?c=emailsystem&m=users{$wherelinkGroup}&users={$char}' class='btn btn-small btn-inverse'>{$char}</a>";
			}
		}
		for ($i=97; $i < 123; $i++) {
			$char = chr($i);
			$istrue = false;
			foreach ($sp as $key => $value) {
				if (substr($value->userword, 0,1)==$char){
					$istrue = true;
				}
			}
			if(!$istrue){			//users=f
				$spstr .= "<a href='javascript:void(0);' class='btn btn-small btn-inverse disabled'>{$char}</a>";
			}else{
				$spstr .= "<a href='?c=emailsystem&m=users{$wherelinkGroup}&users={$char}' class='btn btn-small btn-inverse'>{$char}</a>";
			}
		}
		
		$this->data['spstr'] = $spstr;
		///print_r($this->data['users']);
		//获取用户的所有分组
		$data=array();
		$this->load->model('groups_m');
		$data['uid'] = $this->user['id'];
		$group =  $this->groups_m->getWhere($data);
		$this->data['group'] = $group;
		
		$this->load->view('header',$this->data);
		$this->load->view('users');
	}
	public function edit_users($uid=0){
		$this->check_login();
		$this->load->model('users_m');
		$groupID = $this->input->post('group');
		$usersName = $this->input->post('name');
		$usersEmail = $this->input->post('email');
		if( $groupID!="" and $usersName!="" and $usersEmail!="" ){
			//更新用户
			$udata['group'] = $groupID;
			$udata['user'] = $usersName;
			$udata['email'] =  $usersEmail;
			$this->load->library('cctoen');
			$uwhere['id'] = $uid;
			$uwhere['uid'] = $this->user['id'];
			$udata['userword'] = $this->cctoen->Pinyin($usersName,1);		//汉字转拼音
			$res = $this->users_m->update($uwhere,$udata);
			if($res){
				redirectmy('?c=emailsystem&m=users');
			}else{
				$this->data['webname']=$this->webconfig['webname']."-编辑用户";
				$this->data['title']="编辑用户失败!";
				$this->data['content']='原因未知，请联系管理员？';
				$this->load->view('error',$this->data);
				return false;
			}
		}
		
		if (!$uid) {
			$this->data['webname']=$this->webconfig['webname']."-编辑用户";
			$this->data['title']="编辑用户失败!";
			$this->data['content']='你确定你有该用户？';
			$this->load->view('error',$this->data);
			return false;
		}
		
		$where['users.id']=$uid;
		$where['users.uid']=$this->user['id'];
		$res =  $this->users_m->getID($where);
		if (count($res)<=0) {
			$this->data['webname']=$this->webconfig['webname']."-编辑用户";
			$this->data['title']="编辑用户失败!";
			$this->data['content']='你确定你有该用户？';
			$this->load->view('error',$this->data);
			return false;
		}
		$this->data['users'] = $res[0];
		//获取所有分组
		//获取用户的所有分组
		$data=array();
		$this->load->model('groups_m');
		$data['uid'] = $this->user['id'];
		$group =  $this->groups_m->getWhere($data);
		$this->data['group'] = $group;
		
		$this->load->view('header',$this->data);
		$this->load->view('edit_users');
	}
	public  function del_users($uid=0){
		$this->check_login();
		$this->load->model('users_m');
		if(!$uid){
			$this->data['webname']=$this->webconfig['webname']."-编辑用户";
			$this->data['title']="删除用户失败!";
			$this->data['content']='你没有选择要删除的用户？';
			$this->load->view('error',$this->data);
			return false;
		}
		//检查用户是否属于当然登录的用户
		$res = $this->users_m->get(array("uid"=>$this->user['id'],"id"=>$uid));
		if(count($res)<=0){
			$this->data['webname']=$this->webconfig['webname']."-编辑用户";
			$this->data['title']="删除用户失败!";
			$this->data['content']='你没有删除该用户的权限？';
			$this->load->view('error',$this->data);
			return false;
		}
		$this->users_m->delete(array("id"=>$uid));
		redirectmy('?c=emailsystem&m=users');
	}
	public function users_add(){
		$this->check_login();
		$this->load->model('users_m');
		//是否是添加用户
		$userName = $this->input->post('name');
		$groupName = $this->input->post('group');
		$email =  $this->input->post('email');
		if($userName != ""){
			$this->load->library('cctoen');
			$data=array();
			$data['uid'] = $this->user['id'];
			$data['email'] = $email;
			$data['user'] = $userName;
			$data['group'] = $groupName;
			$data['userword'] = $this->cctoen->Pinyin($userName,1);		//汉字转拼音
			$data['time']  = time();
			$res = $this->users_m->insert($data);
			if(!$res){
				$this->data['webname']=$this->webconfig['webname']."-添加用户失败";
				$this->data['title']='添加用户失败!';
				$this->data['content']='未知错误，请联系管理员!';
				$this->load->view('error',$this->data);
				return false;
			}
		}
		redirectmy('?c=emailsystem&m=users');
	}
	/**
	 * 返回分组分页数据
	 */
	public function ajax_group($type,$currentPage){
		
	}
	public function group($currentPage=1){
		$this->check_login();
		$this->data['css'] = 'group';
		$this->load->model('groups_m');
		$count = count($this->groups_m->get('uid',$this->user['id'],$this->user['id']));				//总页
		if($count>=1){
			if ($currentPage<1) {
				$currentPage = 1;
			}
			if ($currentPage>$count) {
				$currentPage = $count;
			}
			$page = ceil($count/$this->pageCount)-1;
			$data['page'] = $currentPage;
			$data['page_size'] = $this->pageCount;
			$data['total'] = $count;
			$this->load->library('pagination',$data);
			$res = $this->data['groups'] = $this->groups_m->getLimit('uid',$this->user['id'],($currentPage-1)*$this->pageCount,$this->pageCount,$this->user['id']);
			$this->data['groups'] = $res;
		}else{
			$this->data['groups'] = array();
		}
		
		$this->data['webname']=$this->webconfig['webname']."-分组管理";
		$this->load->view('header',$this->data);
		$this->load->view('group');
	}
	public function group_add(){
		$this->check_login();
		$this->load->model('groups_m');
		$groupName = $this->input->post('name');
		if($groupName!=""){
			$data['name']=$groupName;
			$data['uid']=$this->user['id'];
			$data['time']=time();
			$res = $this->groups_m->insert($data);
			redirectmy('/emailsystem/group');
		}else{
			$this->data['webname']=$this->webconfig['webname']."-添加分组";
			$this->data['title']='添加分组失败!';
			$this->data['content']='分组添加失败，请重新添加!';
			$this->load->view('error',$this->data);
			return false;
		}
		
	}
	public function edit_group($gid){
		$this->check_login();
		$this->load->model('groups_m');
		$groupName = $this->input->post('name');
		$groupID = $this->input->post('id');
		if($groupID!="" and $groupName!=""){
			$where['uid'] = $this->user['id'];
			$where['id'] = $groupID;
			$data['name'] = $groupName;
			$res = $this->groups_m->update($where,$data);
			if($res>=1){
				redirectmy('/emailsystem/group');
				return true;
			}else{
				$this->data['webname']=$this->webconfig['webname']."-更新分组";
				$this->data['title']='更新分组失败!';
				$this->data['content']='原因未知，请重试或联系管理员!';
				$this->load->view('error',$this->data);
				return false;
			}
		}
		$this->data['webname']=$this->webconfig['webname']."-编辑分组";
		if($gid==""){
			$this->data['webname']=$this->webconfig['webname']."-编辑分组";
			$this->data['title']='编辑分组失败!';
			$this->data['content']='未找到分组？';
			$this->load->view('error',$this->data);
		}
		$where['id']=$gid;
		$where['uid']=$this->user['id'];
		$res =  $this->groups_m->getID($where);
		if (count($res)<=0) {
			$this->data['webname']=$this->webconfig['webname']."-编辑分组";
			$this->data['title']="编辑分组失败!";
			$this->data['content']='你确定你有该编组？';
			$this->load->view('error',$this->data);
		}
		$this->data['group'] = $res;
		$this->load->view('header',$this->data);
		$this->load->view('edit_group');
	}
	public function del_group($gid=0){
		$this->check_login();
		$this->load->model('groups_m');
		if(!$gid){
			$this->data['webname']=$this->webconfig['webname']."-删除分组";
			$this->data['title']='删除失败!';
			$this->data['content']='你没有提供分组名!';
			$this->load->view('error',$this->data);
			return false;
		}
		$where['uid'] = $this->user['id'];
		$where['id'] = $gid;
		$res = $this->groups_m->delete($where);
		if(!$res){
			$this->data['webname']=$this->webconfig['webname']."-删除分组";
			$this->data['title']='删除失败!';
			$this->data['content']='请刷新分组再试!';
			$this->load->view('error',$this->data);
			return false;
		}else{
			redirectmy('/emailsystem/group');
		}
	}
	public function login(){
		$this->data['webname']=$this->webconfig['webname']."-用户登陆";
		$this->load->view('login',$this->data);
	}
	public function login_pro(){
		$this->data['webname']=$this->webconfig['webname']."-用户登陆";
		$user = $this->input->post('user');
		$pass = md5($this->input->post('pass'));
		$code = $this->input->post('code');
		$scode = $this->mysession->userdata('emailsystemcode');
		if($code!=$scode){
			$this->data['status'] = -1;
			$this->data['msg'] = '验证码错误!';
			$this->load->view('login',$this->data);
			return false;
		}
		$this->load->model('user_m');
		$res = $this->user_m->getwhere('user',$user);
		if(count($res)<=0){
			$this->data['status'] = -1;
			$this->data['msg'] = '密码错误!';
			$this->load->view('login',$this->data);
			return false;
		}
		if($res[0]->pass!=$pass){
			$this->data['status'] = -1;
			$this->data['msg'] = '密码错误!';
			$this->load->view('login',$this->data);
			return false;
		}
		//验证成功写用户信息
		$data['user'] = $res[0]->user;
		$data['email'] = $res[0]->email;
		$data['id'] = $res[0]->id;
		$this->mysession->set_userdata('emailuser',$data);
		redirectmy('emailsystem/email');
	}
	public function register(){
		if($this->webconfig['reg']==0){
			//不能注册
			$this->data['webname']=$this->webconfig['webname']."-注册错误";
			$this->data['title']='注册失败';
			$this->data['content']='注册功能已经关闭，请联系管理员!';
			$this->load->view('error',$this->data);
			return false;
		}
		$this->load->view('register',$this->data);
	}
	
	public function register_pro(){
		$this->load->model('user_m');
		$user = $this->input->post('user');
		$pass = md5($this->input->post('pass'));
		$conpass = md5($this->input->post('conpass'));
		$email = $this->input->post('email');
		$code = $this->input->post('code');
		$time = time();
		
		
		$res = $this->user_m->getwhere('user',$user);
		if(count($res)>=1){
			$this->data['status'] = -1;
			$this->data['msg'] = '用户名已存在!';
			$this->load->view('register',$this->data);
			return false;
		}
		$res = $this->user_m->getwhere('email',$email);
		if(count($res)>=1){
			$this->data['status'] = -1;
			$this->data['msg'] = '邮箱已存在!';
			$this->load->view('register',$this->data);
			return false;
		}
		if($pass!=$conpass){
			$this->data['status'] = -1;
			$this->data['msg'] = '两次输入密码不一致!';
			$this->load->view('register',$this->data);
			return false;
		}
		$scode = $this->mysession->userdata('emailsystemcode');
		if($code!=$scode){
			$this->data['status'] = -1;
			$this->data['msg'] = '验证码错误!';
			$this->load->view('register',$this->data);
			return false;
		}
		//写库
		$data['user'] = $user;
		$data['pass'] = $pass;
		$data['email'] = $email;
		$data['time'] = $time;
		$res = $this->user_m->insert($data);
		if($res){
			//写用户注册的邮箱到setting
			$this->load->model('usetting_m');
			$res = $this->user_m->get(array('email'=>$email));
			$data = array();
			$data['email'] = $email;
			$data['uid'] = $res[0]->id;
			$this->usetting_m->insert($data);
			redirectmy('emailsystem/login');
		}else{
			$this->data['status'] = -1;
			$this->data['msg'] = '注册失败，请重新再试一下!';
			$this->load->view('register',$this->data);
			return false;
		}
	}
	
	
	public function code(){
		$this->load->library('code');
		$this->code->session_var = 'emailsystemcode';
		$this->code->scale = 1;
		$this->code->CreateImage();
	}
	public function check_login(){
		$this->user = $this->mysession->userdata('emailuser');
		if( !($this->user) ){
			//未登录
			redirectmy('emailsystem/login');
		}
	}
	public function getwebconfig(){
		/**
		 * 		[webname] => 邮件管理系统
		    	[description] => 一个邮件在线管理系统
		    	[keywords] => 邮件,在线邮件,在线邮件发送系统
		    	[reg] => 0
		    	[close] => 1
		    	[closeq] => 网站临时维护，现正关闭
		 */
		$this->load->model('setting_m');
		$this->webconfig = $this->setting_m->get();
		
		$this->data['description'] = $this->webconfig['description'];
		$this->data['keywords'] = $this->webconfig['keywords'];
		$this->data['webname'] = $this->webconfig['webname']."-网站首页";
	}
	public function  loginout(){
		$this->mysession->set_userdata('emailuser',null);
		redirectmy('emailsystem/login');
	}
}