<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login 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->load->model('CsdjSkins');
		$this->load->model('CsdjUser');
	}

	public function index()
	{
		if($this->session->userdata('cs_id')){
                     $uid=$this->session->userdata('cs_id');
                }else{
                     $uid=0;
                }
		if($this->session->userdata('cs_name')){
                     $user=$this->session->userdata('cs_name');
                }else{
                     $user='';
                }
                $Mark_Text='';
                $this->load->get_user_templates();
		$template=$this->load->view('user-login.html','',true);
		$template=$this->CsdjSkins->TopandBottom($template,'user');

                if($uid==0 || $user==''){
		            $Mark_Text=str_replace("{cscms:title}","会员登入_".Web_Name,$template);
		            $Mark_Text=str_replace('{cscms:loginadd}',site_url('user/login/add/'),$Mark_Text);
		            $Mark_Text=str_replace('{cscms:passlink}',site_url('user/pass/'),$Mark_Text);
		            $Mark_Text=str_replace('{cscms:reglink}',site_url('user/reg/'),$Mark_Text);
		            $Mark_Text=str_replace('{cscms:qqlink}',site_url('open/login/qq/'),$Mark_Text);
	                    $Mark_Text=str_replace('{cscms:weibolink}',site_url("open/login/weibo"),$Mark_Text);
	                    $Mark_Text=str_replace('{cscms:kaixinlink}',site_url("open/login/kaixin"),$Mark_Text);
	                    $Mark_Text=str_replace('{cscms:baidulink}',site_url("open/login/baidu"),$Mark_Text);
	                    $Mark_Text=str_replace('{cscms:doubanlink}',site_url("open/login/douban"),$Mark_Text);
	                    $Mark_Text=str_replace('{cscms:sohulink}',site_url("open/login/sohu"),$Mark_Text);
	                    $Mark_Text=str_replace('{cscms:wangyilink}',site_url("open/login/netease"),$Mark_Text);
	                    $Mark_Text=str_replace('{cscms:renrenlink}',site_url("open/login/renren"),$Mark_Text);
                }else{
		    if($this->CsdjUser->User_Login(1)){
                            header("Location: ".site_url('user/space/').""); 
                    }else{
                            $ulogin['cs_id']="";
                            $ulogin['cs_name']="";
                            $ulogin['cs_pass']="";
                            $this->session->set_userdata($ulogin);
                            header("Location: ".site_url('user/login').""); 
                    }
                }
                $this->CsdjSkins->skins($Mark_Text);
        }

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

            if(empty($username)) $this->CsdjSkins->Msg_url('用户名不能为空!','javascript:history.back();');  
            if(empty($userpass)) $this->CsdjSkins->Msg_url('密码不能为空!','javascript:history.back();');  

            //可以用会员名、邮箱来进行登入
            $sqlu="SELECT CS_Pass,CS_SID,CS_YID,CS_ID,CS_Name,CS_LogNum,CS_Cion,CS_Vip,CS_LogTime,CS_EndTime 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_Pass!=md5($userpass)){
                                   $this->CsdjSkins->Msg_url('抱歉，密码错误!','javascript:history.back();');  
                           }elseif($row[0]->CS_SID==1){
                                   $this->CsdjSkins->Msg_url('抱歉，您的帐号已经被锁定或者没有验证审核，请联系站长!','javascript:history.back();');  
                           }elseif($row[0]->CS_YID==1){
                                   if(!$this->session->userdata('username')){
                                          $this->load->helper('string');
                                          $keyname=$username."_passkey";
                                          $key=random_string('alnum', 16);
                                          $Pass[$keyname]=$key;
                                          $Pass['username']=$username;
                                          $this->session->set_userdata($Pass);

                                          $Msg['username'] = $username;
                                          $Msg['url']      = site_url('user/reg/addto/')."?key=".$key."&username=".$username;
                                          $title   =$this->CsdjSkins->getmsgto(stripslashes(html_entity_decode(User_RegEmailTitle)),$Msg);
                                          $content =$this->CsdjSkins->getmsgto(stripslashes(html_entity_decode(User_RegEmailContent)),$Msg);
                                          $this->CsdjEmail->Email_Add($useremail,$title,$content);
                                   }
                                   $this->CsdjSkins->Msg_url('抱歉，您的帐号没有激活，请去邮箱激活!','javascript:history.back();');  
                           }else{
		               if(Cion_Log>0 && date("Y-m-d",strtotime($row[0]->CS_LogTime))!=date('Y-m-d')){
                                   $updata['CS_Cion']  = $row[0]->CS_Cion+Cion_Log;
                               }

                               //判断VIP到期
                               $viptime=$this->CsdjSkins->DateBj('n',$row[0]->CS_EndTime,date('Y-m-d H:i:s'));
                               IF($row[0]->CS_Vip>0 && $viptime>10){
	                                $updata['CS_Vip']  = 0;
	                                $updata['CS_VipTime']  = '00-00-00 00:00:00';
	                                $updata['CS_EndTime']  = '00-00-00 00:00:00';
                               }
	                       $updata['CS_Zx']  = 1;
	                       $updata['CS_LogNum']   = $row[0]->CS_LogNum+1;
	                       $updata['CS_LogTime'] = date('Y-m-d H:i:s');
	                       $updata['CS_Logms'] = time();
	                       $updata['CS_LogIP'] = $this->CsdjSkins->GetIP();
                               $this->CsdjDB->get_update ('user',$row[0]->CS_ID,$updata);

                                  $ulogin['cs_id']=$row[0]->CS_ID;
                                  $ulogin['cs_name']=$row[0]->CS_Name;
                                  $ulogin['cs_pass']=md5($row[0]->CS_Pass);
                                  $this->session->set_userdata($ulogin);
                                  $this->CsdjSkins->Msg_url('恭喜，登入成功!',site_url('user/space/'),'ok'); 
                           }
                    }
        }
}

