<?php
         /**
         * 检查是否登录，如果cookie存在，session不存在就对session进行赋值
         * @return string
         */
        function checklogin()
        {
            $ci = & get_instance();
            $ci->load->model('user_model');


            getLoginInfoBySSOUcenter();

            if(!empty($_COOKIE['esfuserid']) && !empty($_COOKIE['esfpassword']))
            {
                $esfuserid = intval($_COOKIE['esfuserid']);

                if( !isset($_SESSION['esfuserid'] ))
                {
                   $_SESSION['esfuserid'] = '';
                }

                $esfpassword = trim($_COOKIE['esfpassword']);

                $check_login = $_SESSION['esfuserid'] == $esfuserid ? $_SESSION['esfuserid'] : ($esfuserid > 0 && $esfpassword != '' ? $ci->user_model->check_user($esfuserid, $esfpassword) : 0);

                if($check_login>0 && !$_SESSION['esfshowname']){
                    $ci = & get_instance();
                    $ci->load->library("user_class");
                    $ci->user_class->user_login($esfuserid,$_COOKIE['esfpassword'],$_SESSION['esfusertelno'],$_SESSION['esfusertelno']);
                }
            }
            else
            {
                $check_login = 0;
            }

            return $check_login;
        }


		function getLoginInfoBySSO(){
			if(empty($_COOKIE['sso_token'])){
				return false;
			}
			$ci = & get_instance();
			if(empty($_COOKIE['esfuserid']) || empty($_COOKIE['esfpassword'])){
				$url = 'http://api.house365.com/passport/check_login_bytoken.php?sso_token='.$_COOKIE['sso_token'];
				$info = json_decode(curl_get_contents($url),1);
				if(is_array($info)){
					if (isset($info['data']['passport_uid']) && $info['data']['passport_uid']) {
						$passport_uid = $info['data']['passport_uid'];
						$ci->load->model('user_model');
						$user = $ci->user_model->get_data(array('form_name' => 'personal_user','where'=>array('passport_uid'=>$passport_uid),'select'=>array('id','telno','password')),'esfbak');
						if($user){
							$esfuserid = $user[0]['id'];
							$esfpassword = $user[0]['password'];
							setcookie('esfuserid',$esfuserid,time() + 3600 * 24 * 7,'/','.house365.com');
							setcookie('esfpassword',$esfpassword,time() + 3600 * 24 * 7,'/','.house365.com');
						}
					}
				}else{
					return false;
				}
			}

			return $info;
		}

        function getLoginInfoBySSOUcenter(){
            if(empty($_COOKIE['sso_token'])){
                return false;
            }
            $ci = & get_instance();
            if(empty($_COOKIE['esfuserid']) || empty($_COOKIE['esfpassword'])){
                $url = GB_GET_USER_INFO_BY_SSO_URL;
                $cookieStr = "sso_token=" . $_COOKIE['sso_token'];
                $info = curl_get_contents_new($url, '', $cookieStr);
                if(is_array($info) && !empty($info)){
                    if (isset($info['data']['id']) && $info['data']['id']) {
                        $passport_uid = $info['data']['id'];
                        $ci->load->model('user_model');
                        $user = $ci->user_model->get_data(array('form_name' => 'personal_user','where'=>array('passport_uid'=>$passport_uid),'select'=>array('id','telno','password')),'dbuser');
                        if($user){
                            $esfuserid = $user[0]['id'];
                            $esfpassword = $user[0]['password'];
                            setcookie('esfuserid',$esfuserid,time() + 3600 * 24 * 7,'/','.house365.com');
                            setcookie('esfpassword',$esfpassword,time() + 3600 * 24 * 7,'/','.house365.com');
                        }
                    }
                }else{
                    return false;
                }
            }

            return $info;
        }


        function getmesnumbyuidwd()
        {
         $ci = & get_instance();
         $ci->load->model('user_model');
         $num = $ci->user_model->getmessagenumbyuidwd($_COOKIE['esfuserid']);
         setcoandsess("esf".GB_CITY."mesnum",$num);
         return $num;
        }
		
		function getmesnumbyuid()
        {
         $ci = & get_instance();
         $ci->load->model('user_model');
         $num = $ci->user_model->getmessagenumbyuid($_COOKIE['esfuserid']);
         return $num;
        }
		
		function get_match_num_byuidwd()
        {
         $ci = & get_instance();
         $ci->load->model('user_model');
         $num = $ci->user_model->get_match_num_byuid($_COOKIE['esfuserid']);
         setcoandsess("esfmatchnum",$num);
         return $num;
        }
		
	    function getnewanswerbyuidwd()
        {
         $ci = & get_instance();
         $ci->load->model('ask_model');
         $num = $ci->ask_model->getnewanswerbyuid($_COOKIE['esfuserid']);
         setcoandsess("esfasknum",$num);
         return $num;
        }

         /**
         * 获取用户头像
         * @param type $uid
         * @return string
         */
        function getpicheadbyuid()
        {
         $ci = & get_instance();
         $ci->load->model('user_model');
         $pic = $ci->user_model->get_data(array('form_name' => 'personal_user','where'=>array('id'=>$_COOKIE['esfuserid']),'select'=>array('head_pic')),'esfbak');
         if(empty($pic[0]['head_pic'])){ $pic[0]['head_pic'] = USER_CENTER.'images/pep.jpg';}
         return $pic[0]['head_pic'];
        }






        //图片上传
        function common_file_upload($fileName, $watermark = false, $resize = false, $width = 640, $height = 480, $swidth = 120, $sheight = 90, $maxsize = "5120")
        {

            $ci = & get_instance();
            $ci->load->library('uploadfile_class',array('filename'=>$fileName));
            $ci->uploadfile_class->setFileType("jpg|png");
            $ci->uploadfile_class->setMaxSize($maxsize);
            $ci->uploadfile_class->setUploadType("ftp");
            $ci->uploadfile_class->setSaveDir("/".GB_CITY."house/");

            $ci->uploadfile_class->setWatermark($watermark);
            $ci->uploadfile_class->setWatermarkType(1);
            $ci->uploadfile_class->setWatermarkPosition("rb");
            $ci->uploadfile_class->setWatermarkString("HOUSE365.COM");

            $ci->uploadfile_class->setImageCut(true);
            $ci->uploadfile_class->setImageWidth($width);
            $ci->uploadfile_class->setImageHeight($height);

            if($resize)
            {
                $ci->uploadfile_class->setResizeImage(true);
                $ci->uploadfile_class->setResizeCut(false);
                $ci->uploadfile_class->setResizeNeedWater(false);
                $ci->uploadfile_class->setResizeReality();
                $ci->uploadfile_class->setResizeWidth($swidth);
                $ci->uploadfile_class->setResizeHeight($sheight);
            }

            if ($ci->uploadfile_class->upload() == "success") {
                return $resize ? $ci->uploadfile_class->getResizeImageURL() : $ci->uploadfile_class->getSaveFileURL(); //图片地址
            } else {
                return false;
            }
        }
?>