<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pass extends CI_Controller {

	function __construct(){
		parent::__construct();
        	$this->load->helper('url');
        	$this->load->helper('form');
                $this->load->library('session'); //加载session
		$this->load->model('CsdjDB');
		$this->db->cache_off();
		$this->load->model('CsdjSkins');
                $this->load->model('CsdjEmail');
	}

	public function index()
	{
                $this->load->get_user_templates();
		$template=$this->load->view('user-pass.html','',true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');
		$Mark_Text=str_replace("{cscms:title}","会员密码找回_".Web_Name,$template);
		$Mark_Text=str_replace('{cscms:regadd}',site_url('user/reg/add/'),$Mark_Text);
		$Mark_Text=str_replace('{cscms:passlink}',site_url('user/pass/'),$Mark_Text);
		$Mark_Text=str_replace('{cscms:loginlink}',site_url('user/login/'),$Mark_Text);
		$Mark_Text=str_replace('{cscms:qqlink}',site_url('user/login/qq/'),$Mark_Text);
		$Mark_Text=str_replace('{cscms:passadd}',site_url('user/pass/add/'),$Mark_Text);
		$Mark_Text=str_replace('{cscms:codes}',site_url('user/codes/'),$Mark_Text);
                $this->CsdjSkins->skins($Mark_Text);
        }

	public function add()
	{
                $username = $this->input->get_post('username', TRUE);   //username
                $useremail = $this->input->get_post('useremail', TRUE);   //useremail
                $codes = $this->input->get_post('codes', TRUE);   //code

                if(empty($username)) $this->CsdjSkins->Msg_url('用户名不能为空!','javascript:history.back();');   
                if(empty($useremail)) $this->CsdjSkins->Msg_url('邮箱不能为空!','javascript:history.back();');  
                if(empty($codes)) $this->CsdjSkins->Msg_url('验证码不能为空!','javascript:history.back();'); 
                if(strtolower($codes)!=strtolower($this->session->userdata('cs_codes'))) $this->CsdjSkins->Msg_url('验证码不正确!','javascript:history.back();'); 

                //可以用ID、会员名、邮箱来进行登入
                $sqlu="SELECT CS_ID,CS_Email,CS_Pass FROM ".CS_SqlPrefix."user where cs_name='".$username."' or cs_email='".$username."'";
		        $row=$this->CsdjDB->get_all($sqlu);
		        if(!$row){
                      $this->CsdjSkins->Msg_url('抱歉，该帐号不存在!','javascript:history.back();');  
                }else{
                      if($row[0]->CS_Email!=$useremail){
                                   $this->CsdjSkins->Msg_url('抱歉，邮箱错误!','javascript:history.back();');  
                      }

                      $key=md5($row[0]->CS_ID.$row[0]->CS_Email.$row[0]->CS_Pass);

                      $Msg['username'] = $username;
                      $Msg['url']      = site_url('user/pass/passto/')."?key=".$key."&username=".$username;
                      $title   =$this->CsdjSkins->getmsgto(stripslashes(html_entity_decode(User_PassTitle)),$Msg);
                      $content =$this->CsdjSkins->getmsgto(stripslashes(html_entity_decode(User_PassContent)),$Msg);


                      $this->CsdjEmail->Email_Add($useremail,$title,$content);
                      $this->CsdjSkins->Msg_url('密码邮件已经发送到您的邮箱，请去邮箱查看！!',site_url('user/login/'),'ok'); 
                }
	}

	public function passto()
	{
                $key = $this->input->get_post('key', TRUE);   //key
                $username = $this->input->get_post('username', TRUE);   //username

                $sqlu="SELECT CS_ID,CS_Email,CS_Pass FROM ".CS_SqlPrefix."user where cs_name='".$username."' or cs_email='".$username."'";
		        $row=$this->CsdjDB->get_all($sqlu);
				if(!$row || md5($row[0]->CS_ID.$row[0]->CS_Email.$row[0]->CS_Pass)!=$key){
                      $this->CsdjSkins->Msg_url('密码找回KEY验证失败，请重新找回!',site_url('user/login/'));
                }else{

                      $this->load->get_user_templates();
		              $template=$this->load->view('user-passto.html','',true);
		              $template=$this->CsdjSkins->TopandBottom($template,'user');
		              $Mark_Text=str_replace("{cscms:title}","会员密码找回_".Web_Name,$template);
		              $Mark_Text=str_replace('{cscms:passto}',site_url('user/pass/passtoadd').'?username='.$username.'&key='.$key,$Mark_Text);
                      $this->CsdjSkins->skins($Mark_Text);
                }
	}

	public function passtoadd()
	{
                $key = $this->input->get_post('key', TRUE);
                $username = $this->input->get_post('username', TRUE);
                $CS_Pass = $this->input->get_post('userpass', TRUE);   //userpass  
                $CS_Pass2 =$this->input->get_post('userpass2', TRUE);   //userpass2  

                if(empty($CS_Pass)) $this->CsdjSkins->Msg_url('密码不能为空!','javascript:history.back();');  
                if($CS_Pass!=$CS_Pass2) $this->CsdjSkins->Msg_url('两次密码不一直!','javascript:history.back();');  

                $sqlu="SELECT CS_ID,CS_Email,CS_Pass FROM ".CS_SqlPrefix."user where cs_name='".$username."' or cs_email='".$username."'";
		        $row=$this->CsdjDB->get_all($sqlu);
				if(!$row || md5($row[0]->CS_ID.$row[0]->CS_Email.$row[0]->CS_Pass)!=$key){
                      $this->CsdjSkins->Msg_url('密码找回KEY验证失败，请重新找回!',site_url('user/login/'));
                }else{
                     $updata['CS_Pass']=md5($CS_Pass);
                     $this->CsdjDB->get_update ('user',$row[0]->CS_ID,$updata);
				}
                $this->CsdjSkins->Msg_url('恭喜您，密码修改成功!',site_url('user/login/'),'ok'); 
        }
}

