auth.php 9.3 KB

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