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

class Ulog 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('CsdjEmail');
                $this->load->model('CsdjUser');
	}

	public function index()
	{
            $ac = $this->uri->segment(4);
		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='';
                }
				if($ac=='user'){
                      $this->load->get_user_templates();
				}else{
                      $this->load->get_templates();
	            }

		    if(!$this->CsdjUser->User_Login(1)){
		            $template=$this->load->view('cs-ulogin.html','',true);
		            $Mark_Text=str_replace("{cscms:login}","no",$template);
		            $Mark_Text=str_replace('{cscms:reglink}',site_url('user/reg/'),$Mark_Text);
		            $Mark_Text=str_replace('{cscms:passlink}',site_url('user/pass/'),$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);
		            $Mark_Text=str_replace('{cscms:logadd}',"CSCMS_Logadd();",$Mark_Text);
                    }else{
                            $sqlu="SELECT * FROM ".CS_SqlPrefix."user where cs_id='".$uid."'";
		            $row=$this->CsdjDB->get_all($sqlu);
                            if(empty($row[0]->CS_Nichen)) $row[0]->CS_Nichen=$row[0]->CS_Name;
		            $template=$this->load->view('cs-uinfo.html','',true);
		            $Mark_Text=str_replace("[user:login]","yes",$template);
		            $Mark_Text=str_replace('{cscms:uname}',$user,$Mark_Text);
		            $Mark_Text=str_replace('{cscms:uid}',$uid,$Mark_Text);
		            $Mark_Text=str_replace('{cscms:unichen}',$row[0]->CS_Nichen,$Mark_Text);
		            $Mark_Text=str_replace('{cscms:ucion}',$row[0]->CS_Cion,$Mark_Text);
		            $Mark_Text=str_replace('{cscms:uvip}',$this->CsdjSkins->userlevel($row[0]->CS_Vip),$Mark_Text);
		            $Mark_Text=str_replace('{cscms:upic}',$this->CsdjSkins->piclink('logo',$row[0]->CS_Logo),$Mark_Text);
		            $Mark_Text=str_replace('{cscms:ulink}',$this->CsdjSkins->userlink('index',$uid,$user),$Mark_Text);
		            $Mark_Text=str_replace('{cscms:uindex}',site_url('user/space/'),$Mark_Text);
		            $Mark_Text=str_replace('{cscms:upaylink}',site_url('user/pay/'),$Mark_Text);
		            $Mark_Text=str_replace('{cscms:uviplink}',site_url('user/pay/vip'),$Mark_Text);
		            $Mark_Text=str_replace('{cscms:path}',Web_Path,$Mark_Text);
		            $Mark_Text=str_replace('{cscms:umusiccount}',Web_Path,$Mark_Text);
		            $Mark_Text=str_replace('{cscms:logout}',"CSCMS_Logout();",$Mark_Text);
                            $Mark_Text=$this->CsdjUser->getuser($Mark_Text,$row);
                    }
                    if($ac=='user'){
                         $TempImg = Web_Path.'skins/'.Skins_user_dir.'/';
                    }else{
                         $TempImg = Web_Path.'skins/'.Skins_dir.'/';
                    }
                    $TempImg = str_replace('//','/',$TempImg);
                    $TempImg = substr(substr($TempImg,0,strlen($TempImg)-1),0,strrpos(substr($TempImg,0,strlen($TempImg)-1),'/')+1);
	                $Mark_Text  = str_replace("{cscms:tempurl}",$TempImg,$Mark_Text);
                    $this->CsdjSkins->skins($Mark_Text);
        }

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

            if(empty($username)) exit('10001');  //用户名为空
            if(empty($userpass)) exit('10002');  //密码为空

            //可以用ID、会员名、邮箱来进行登入
            $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){
                    exit('10003');  //帐号不存在
            }else{
                           if($row[0]->CS_Pass!=md5($userpass)){
                                  exit('10004'); //密码错误
                           }elseif($row[0]->CS_SID==1){
                                  exit('10005'); //帐号被锁定
                           }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);
                                   }
                                   exit('10007'); //邮件未激活
                           }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_LogIP'] = $this->CsdjSkins->GetIP();
	                       $updata['CS_Logms'] = time();
                               $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);
                                  exit('10006'); //登入成功
                           }
                    }
        }

	public function logout()
	{

                //删除在线状态
                $updata['CS_Zx']=0;
                $this->CsdjDB->get_update ('user',$this->session->userdata('cs_id'),$updata);

                $ulogin['cs_id']="";
                $ulogin['cs_name']="";
                $ulogin['cs_pass']="";
                $this->session->set_userdata($ulogin);
                exit('10001'); //退出成功
        }
}

