auth.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. session_start(); // starts new or resumes existing session
  3. session_regenerate_id(true); // regenerates SESSIONID to prevent hijacking
  4. Doo::loadModel('users');
  5. Doo::loadModel('usession');
  6. Doo::loadModelAt('ausers', 'admin');
  7. Doo::loadClass('user');
  8. Doo::loadModel('uprofile');
  9. Doo::loadModel('utoken');
  10. class Auth
  11. {
  12. private $users, $usession, $user, $uinfo, $ausers, $profile,$__token;
  13. public function __construct()
  14. {
  15. $this->users = new Users();
  16. $this->usession = new Usession();
  17. $this->user = new User();
  18. $this->ausers = new AUsers();
  19. $this->profile = new Uprofile();
  20. $this->__token = new Utoken();
  21. }
  22. private function __setcookie($key, $value)
  23. {
  24. setcookie($this->cookiePre . $key, $value, 0, '/', $this->siteUrl, 0);
  25. }
  26. public function login($uname, $upasswd)
  27. {
  28. $uinfo = $this->checkLogin($uname, $upasswd);
  29. if (isset($uinfo['uid'])) {
  30. $this->uinfo = $uinfo;
  31. return TRUE;
  32. } else {
  33. return FALSE;
  34. }
  35. }
  36. public function loginWithUserName($uname)
  37. {
  38. $uinfo = $this->checkLoginWithUserName($uname);
  39. if (isset($uinfo['uid'])) {
  40. $this->uinfo = $uinfo;
  41. return TRUE;
  42. } else {
  43. return FALSE;
  44. }
  45. }
  46. public function checkLoginWithUserName($uemail)
  47. {
  48. return $this->user->loginWithUserName($uemail);
  49. }
  50. public function getUinfo()
  51. {
  52. return $this->uinfo;
  53. }
  54. public function getUid()
  55. {
  56. if (isset($_SESSION['uid']) && $_SESSION['uid']) {
  57. return $_SESSION['uid'];
  58. } else {
  59. return FALSE;
  60. }
  61. }
  62. public function getUemail()
  63. {
  64. if (isset($_SESSION['uemail']) && $_SESSION['uemail']) {
  65. return $_SESSION['uemail'];
  66. } else {
  67. return 0;
  68. }
  69. }
  70. public function setUid($uid)
  71. {
  72. return $_SESSION['uid'] = $uid;
  73. }
  74. public function setVerifyMobile($array = array('mobile' => 0, 'code' => 0))
  75. {
  76. $_SESSION['verifymobile'] = $array;
  77. }
  78. public function getVerifyMobile()
  79. {
  80. if (isset($_SESSION['verifymobile']) && $_SESSION['verifymobile']) {
  81. return $_SESSION['verifymobile'];
  82. } else {
  83. return FALSE;
  84. }
  85. }
  86. public function setUemail($uemail)
  87. {
  88. return $_SESSION['uemail'] = $uemail;
  89. }
  90. public function getAvatar($uid)
  91. {
  92. // $dir1 = ceil($uid / 10000);
  93. // $dir2 = ceil($uid % 10000 / 1000);
  94. // $url = 'http://sso.smartcost.com.cn/' . 'data/avatar/' . $dir1 . '/' . $dir2 . '/' . $uid . '/';
  95. // $avatar = array('180' => $url . '180x180.jpg', '90' => $url . '90x90.jpg', '45' => $url . '45x45.jpg', '30' => $url . '30x30.jpg');
  96. // return $avatar;
  97. $valArray = $this->profile->getOne(array('where' => 'userid=?', 'param' => array($uid), 'asArray' => TRUE));
  98. return Doo::conf()->APP_URL . $valArray['avatar'];
  99. }
  100. public function getSignpath($uid)
  101. {
  102. $valArray = $this->profile->getOne(array('where' => 'userid=?', 'param' => array($uid), 'asArray' => TRUE));
  103. return Doo::conf()->APP_URL . $valArray['signpath'];
  104. }
  105. public function getName($uid)
  106. {
  107. $name = $this->profile->getOne(array('select' => 'name', 'where' => 'userid=?', 'param' => array($uid), 'asArray' => TRUE));
  108. return $name['name'];
  109. }
  110. public function getEmail($uid)
  111. {
  112. $name = $this->users->getOne(array('select' => 'uemail', 'where' => 'uid=?', 'param' => array($uid), 'asArray' => TRUE));
  113. return $name['uemail'];
  114. }
  115. public function getRowByUid($uid)
  116. {
  117. return $this->profile->getOne(array('where' => 'userid=?', 'param' => array($uid), 'asArray' => TRUE));
  118. }
  119. public function checkUserEmail($email)
  120. {
  121. $result = $this->users->getOne(array('select' => 'uid', 'where' => 'uemail=?', 'param' => array($email), 'asArray' => TRUE));
  122. if(!empty($result)){
  123. return $this->profile->getOne(array('where' => 'userid=?', 'param' => array($result['uid']), 'asArray' => TRUE));
  124. }else{
  125. return '';
  126. }
  127. }
  128. public function checkLogin($uemail, $upasswd)
  129. {
  130. return $this->user->login($uemail, $upasswd);
  131. }
  132. public function logout()
  133. {
  134. session_destroy();
  135. setcookie('token', '-1', 0, '/', 'jl.local', FALSE, TRUE);
  136. }
  137. public function checkauth()
  138. {
  139. //TODO 启用SESSION变量避免重复查询数据库
  140. if (isset($_COOKIE['M0s5Yi_yn_k']) && isset($_COOKIE['M0s5Yi_yn_v'])) {
  141. $uname = $this->decryptCookie($_COOKIE['M0s5Yi_yn_k']);
  142. $passwd = $this->decryptCookie($_COOKIE['M0s5Yi_yn_v']);
  143. if ($uname && $passwd) {
  144. return TRUE;
  145. } else {
  146. return FALSE;
  147. }
  148. } else {
  149. return FALSE;
  150. }
  151. }
  152. function isLoggedIn()
  153. {
  154. if (isset($_SESSION['token']) && isset($_COOKIE['token'])) {
  155. if ($_SESSION['token'] != $_COOKIE['token']) {
  156. return TRUE;
  157. }
  158. }
  159. return FALSE;
  160. }
  161. public function getUname()
  162. {
  163. //TODO 启用SESSION变量避免重复查询数据库
  164. if (isset($_COOKIE['M0s5Yi_yn_k']) && isset($_COOKIE['M0s5Yi_yn_v'])) {
  165. $uname = $this->decryptCookie($_COOKIE['M0s5Yi_yn_k']);
  166. $passwd = $this->decryptCookie($_COOKIE['M0s5Yi_yn_v']);
  167. if ($uname && $passwd) {
  168. return $uname;
  169. } else {
  170. return FALSE;
  171. }
  172. } else {
  173. return FALSE;
  174. }
  175. }
  176. public function updateToken($uid,$token,$comefrom){
  177. $utokenmsg = $this->__token->getOne(array('where' => 'uid='.$uid.' and comefrom='.$comefrom, 'asArray' => TRUE));
  178. if(empty($utokenmsg)){
  179. $this->__token->uid = $uid;
  180. $this->__token->token = $token;
  181. $this->__token->addtime = time();
  182. $this->__token->endtime = time()+86400; // 一天有效期
  183. $this->__token->comefrom = $comefrom;
  184. $this->__token->insert();
  185. }else{
  186. $this->__token->id = $utokenmsg['id'];
  187. $this->__token->token = $token;
  188. $this->__token->addtime = time();
  189. $this->__token->endtime = time()+86400; // 一天有效期
  190. $this->__token->comefrom = $comefrom;
  191. $this->__token->update();
  192. }
  193. }
  194. public function getWebToken($uid){
  195. $utokenmsg = $this->__token->getOne(array('where' => 'comefrom=1 and uid='.$uid, 'asArray' => TRUE));
  196. if(!empty($utokenmsg) && $utokenmsg['endtime'] > time()){
  197. return $utokenmsg['token'];
  198. }else{
  199. return '';
  200. }
  201. }
  202. public function getAppToken($uid){
  203. $utokenmsg = $this->__token->getOne(array('where' => 'comefrom=3 and uid='.$uid, 'asArray' => TRUE));
  204. if(!empty($utokenmsg)){
  205. return $utokenmsg['token'];
  206. }else{
  207. return '';
  208. }
  209. }
  210. public function checkLoginByScan($uid,$token){
  211. $uinfo = $this->getWebToken($uid);
  212. if($uinfo && $uinfo == $token) {
  213. $this->uinfo = $this->users->getOne(array('where' => 'uid='.$uid, 'asArray' => TRUE));
  214. return TRUE;
  215. }
  216. return FALSE;
  217. }
  218. public function AppLoginToken($uid,$token){
  219. $utokenmsg = $this->__token->getOne(array('where' => 'comefrom=3 and uid='.$uid, 'asArray' => TRUE));
  220. if(!empty($utokenmsg)){
  221. $this->__token->id = $utokenmsg['id'];
  222. $this->__token->token = $token;
  223. $this->__token->update();
  224. }else{
  225. $this->__token->uid = $uid;
  226. $this->__token->token = $token;
  227. $this->__token->comefrom = 3;
  228. $this->__token->insert();
  229. }
  230. }
  231. public function CheckAppLoginToken($uid,$token){
  232. $uinfo = $this->getAppToken($uid);
  233. if($uinfo && $uinfo == $token) {
  234. $this->uinfo = $this->users->getOne(array('where' => 'uid='.$uid, 'asArray' => TRUE));
  235. return TRUE;
  236. }
  237. return FALSE;
  238. }
  239. private function encryptCookie($value)
  240. {
  241. if (!$value) {
  242. return false;
  243. }
  244. $key = '290234lk23jk23djLHSWCs92s';
  245. $text = $value;
  246. $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
  247. $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  248. $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
  249. return trim(base64_encode($crypttext)); //encode for cookie
  250. }
  251. private function decryptCookie($value)
  252. {
  253. if (!$value) {
  254. return false;
  255. }
  256. $key = '290234lk23jk23djLHSWCs92s';
  257. $crypttext = base64_decode($value); //decode cookie
  258. $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
  259. $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  260. $decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
  261. return trim($decrypttext);
  262. }
  263. }
  264. ?>