123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <?php
- session_start(); // starts new or resumes existing session
- session_regenerate_id(true); // regenerates SESSIONID to prevent hijacking
- Doo::loadModel('users');
- Doo::loadModel('usession');
- Doo::loadModelAt('ausers', 'admin');
- Doo::loadClass('user');
- Doo::loadModel('uprofile');
- Doo::loadModel('utoken');
- class Auth
- {
- private $users, $usession, $user, $uinfo, $ausers, $profile,$__token;
- public function __construct()
- {
- $this->users = new Users();
- $this->usession = new Usession();
- $this->user = new User();
- $this->ausers = new AUsers();
- $this->profile = new Uprofile();
- $this->__token = new Utoken();
- }
- private function __setcookie($key, $value)
- {
- setcookie($this->cookiePre . $key, $value, 0, '/', $this->siteUrl, 0);
- }
- public function login($uname, $upasswd)
- {
- $uinfo = $this->checkLogin($uname, $upasswd);
- if (isset($uinfo['uid'])) {
- $this->uinfo = $uinfo;
- return TRUE;
- } else {
- return FALSE;
- }
- }
- public function loginWithUserName($uname)
- {
- $uinfo = $this->checkLoginWithUserName($uname);
- if (isset($uinfo['uid'])) {
- $this->uinfo = $uinfo;
- return TRUE;
- } else {
- return FALSE;
- }
- }
- public function checkLoginWithUserName($uemail)
- {
- return $this->user->loginWithUserName($uemail);
- }
- public function getUinfo()
- {
- return $this->uinfo;
- }
- public function getUid()
- {
- if (isset($_SESSION['uid']) && $_SESSION['uid']) {
- return $_SESSION['uid'];
- } else {
- return FALSE;
- }
- }
- public function getUemail()
- {
- if (isset($_SESSION['uemail']) && $_SESSION['uemail']) {
- return $_SESSION['uemail'];
- } else {
- return 0;
- }
- }
- public function setUid($uid)
- {
- return $_SESSION['uid'] = $uid;
- }
- public function setVerifyMobile($array = array('mobile' => 0, 'code' => 0))
- {
- $_SESSION['verifymobile'] = $array;
- }
- public function getVerifyMobile()
- {
- if (isset($_SESSION['verifymobile']) && $_SESSION['verifymobile']) {
- return $_SESSION['verifymobile'];
- } else {
- return FALSE;
- }
- }
- public function setUemail($uemail)
- {
- return $_SESSION['uemail'] = $uemail;
- }
- public function getAvatar($uid)
- {
- // $dir1 = ceil($uid / 10000);
- // $dir2 = ceil($uid % 10000 / 1000);
- // $url = 'http://sso.smartcost.com.cn/' . 'data/avatar/' . $dir1 . '/' . $dir2 . '/' . $uid . '/';
- // $avatar = array('180' => $url . '180x180.jpg', '90' => $url . '90x90.jpg', '45' => $url . '45x45.jpg', '30' => $url . '30x30.jpg');
- // return $avatar;
- $valArray = $this->profile->getOne(array('where' => 'userid=?', 'param' => array($uid), 'asArray' => TRUE));
- return Doo::conf()->APP_URL . $valArray['avatar'];
- }
- public function getSignpath($uid)
- {
- $valArray = $this->profile->getOne(array('where' => 'userid=?', 'param' => array($uid), 'asArray' => TRUE));
- return Doo::conf()->APP_URL . $valArray['signpath'];
- }
- public function getName($uid)
- {
- $name = $this->profile->getOne(array('select' => 'name', 'where' => 'userid=?', 'param' => array($uid), 'asArray' => TRUE));
- return $name['name'];
- }
- public function getEmail($uid)
- {
- $name = $this->users->getOne(array('select' => 'uemail', 'where' => 'uid=?', 'param' => array($uid), 'asArray' => TRUE));
- return $name['uemail'];
- }
- public function getRowByUid($uid)
- {
- return $this->profile->getOne(array('where' => 'userid=?', 'param' => array($uid), 'asArray' => TRUE));
- }
- public function checkUserEmail($email)
- {
- $result = $this->users->getOne(array('select' => 'uid', 'where' => 'uemail=?', 'param' => array($email), 'asArray' => TRUE));
- if(!empty($result)){
- return $this->profile->getOne(array('where' => 'userid=?', 'param' => array($result['uid']), 'asArray' => TRUE));
- }else{
- return '';
- }
- }
- public function checkLogin($uemail, $upasswd)
- {
- return $this->user->login($uemail, $upasswd);
- }
- public function logout()
- {
- session_destroy();
- setcookie('token', '-1', 0, '/', 'jl.local', FALSE, TRUE);
- }
- public function checkauth()
- {
- //TODO 启用SESSION变量避免重复查询数据库
- if (isset($_COOKIE['M0s5Yi_yn_k']) && isset($_COOKIE['M0s5Yi_yn_v'])) {
- $uname = $this->decryptCookie($_COOKIE['M0s5Yi_yn_k']);
- $passwd = $this->decryptCookie($_COOKIE['M0s5Yi_yn_v']);
- if ($uname && $passwd) {
- return TRUE;
- } else {
- return FALSE;
- }
- } else {
- return FALSE;
- }
- }
- function isLoggedIn()
- {
- if (isset($_SESSION['token']) && isset($_COOKIE['token'])) {
- if ($_SESSION['token'] != $_COOKIE['token']) {
- return TRUE;
- }
- }
- return FALSE;
- }
- public function getUname()
- {
- //TODO 启用SESSION变量避免重复查询数据库
- if (isset($_COOKIE['M0s5Yi_yn_k']) && isset($_COOKIE['M0s5Yi_yn_v'])) {
- $uname = $this->decryptCookie($_COOKIE['M0s5Yi_yn_k']);
- $passwd = $this->decryptCookie($_COOKIE['M0s5Yi_yn_v']);
- if ($uname && $passwd) {
- return $uname;
- } else {
- return FALSE;
- }
- } else {
- return FALSE;
- }
- }
- public function updateToken($uid,$token,$comefrom){
- $utokenmsg = $this->__token->getOne(array('where' => 'uid='.$uid.' and comefrom='.$comefrom, 'asArray' => TRUE));
- if(empty($utokenmsg)){
- $this->__token->uid = $uid;
- $this->__token->token = $token;
- $this->__token->addtime = time();
- $this->__token->endtime = time()+86400; // 一天有效期
- $this->__token->comefrom = $comefrom;
- $this->__token->insert();
- }else{
- $this->__token->id = $utokenmsg['id'];
- $this->__token->token = $token;
- $this->__token->addtime = time();
- $this->__token->endtime = time()+86400; // 一天有效期
- $this->__token->comefrom = $comefrom;
- $this->__token->update();
- }
- }
- public function getWebToken($uid){
- $utokenmsg = $this->__token->getOne(array('where' => 'comefrom=1 and uid='.$uid, 'asArray' => TRUE));
- if(!empty($utokenmsg) && $utokenmsg['endtime'] > time()){
- return $utokenmsg['token'];
- }else{
- return '';
- }
- }
- public function getAppToken($uid){
- $utokenmsg = $this->__token->getOne(array('where' => 'comefrom=3 and uid='.$uid, 'asArray' => TRUE));
- if(!empty($utokenmsg)){
- return $utokenmsg['token'];
- }else{
- return '';
- }
- }
- public function checkLoginByScan($uid,$token){
- $uinfo = $this->getWebToken($uid);
- if($uinfo && $uinfo == $token) {
- $this->uinfo = $this->users->getOne(array('where' => 'uid='.$uid, 'asArray' => TRUE));
- return TRUE;
- }
- return FALSE;
- }
- public function AppLoginToken($uid,$token){
- $utokenmsg = $this->__token->getOne(array('where' => 'comefrom=3 and uid='.$uid, 'asArray' => TRUE));
- if(!empty($utokenmsg)){
- $this->__token->id = $utokenmsg['id'];
- $this->__token->token = $token;
- $this->__token->update();
- }else{
- $this->__token->uid = $uid;
- $this->__token->token = $token;
- $this->__token->comefrom = 3;
- $this->__token->insert();
- }
- }
- public function CheckAppLoginToken($uid,$token){
- $uinfo = $this->getAppToken($uid);
- if($uinfo && $uinfo == $token) {
- $this->uinfo = $this->users->getOne(array('where' => 'uid='.$uid, 'asArray' => TRUE));
- return TRUE;
- }
- return FALSE;
- }
- private function encryptCookie($value)
- {
- if (!$value) {
- return false;
- }
- $key = '290234lk23jk23djLHSWCs92s';
- $text = $value;
- $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
- $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
- $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
- return trim(base64_encode($crypttext)); //encode for cookie
- }
- private function decryptCookie($value)
- {
- if (!$value) {
- return false;
- }
- $key = '290234lk23jk23djLHSWCs92s';
- $crypttext = base64_decode($value); //decode cookie
- $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
- $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
- $decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
- return trim($decrypttext);
- }
- }
- ?>
|