auth.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. class Auth
  10. {
  11. private $users, $usession, $user, $uinfo, $ausers, $profile;
  12. public function __construct()
  13. {
  14. $this->users = new Users();
  15. $this->usession = new Usession();
  16. $this->user = new User();
  17. $this->ausers = new AUsers();
  18. $this->profile = new Uprofile();
  19. }
  20. private function __setcookie($key, $value)
  21. {
  22. setcookie($this->cookiePre . $key, $value, 0, '/', $this->siteUrl, 0);
  23. }
  24. public function login($uname, $upasswd)
  25. {
  26. $uinfo = $this->checkLogin($uname, $upasswd);
  27. if (isset($uinfo['uid'])) {
  28. $this->uinfo = $uinfo;
  29. return TRUE;
  30. } else {
  31. return FALSE;
  32. }
  33. }
  34. public function loginWithUserName($uname)
  35. {
  36. $uinfo = $this->checkLoginWithUserName($uname);
  37. if (isset($uinfo['uid'])) {
  38. $this->uinfo = $uinfo;
  39. return TRUE;
  40. } else {
  41. return FALSE;
  42. }
  43. }
  44. public function checkLoginWithUserName($uemail)
  45. {
  46. return $this->user->loginWithUserName($uemail);
  47. }
  48. public function getUinfo()
  49. {
  50. return $this->uinfo;
  51. }
  52. public function getUid()
  53. {
  54. if (isset($_SESSION['uid']) && $_SESSION['uid']) {
  55. return $_SESSION['uid'];
  56. } else {
  57. return FALSE;
  58. }
  59. }
  60. public function getUemail()
  61. {
  62. if (isset($_SESSION['uemail']) && $_SESSION['uemail']) {
  63. return $_SESSION['uemail'];
  64. } else {
  65. return 0;
  66. }
  67. }
  68. public function setUid($uid)
  69. {
  70. return $_SESSION['uid'] = $uid;
  71. }
  72. public function setVerifyMobile($array = array('mobile' => 0, 'code' => 0))
  73. {
  74. $_SESSION['verifymobile'] = $array;
  75. }
  76. public function getVerifyMobile()
  77. {
  78. if (isset($_SESSION['verifymobile']) && $_SESSION['verifymobile']) {
  79. return $_SESSION['verifymobile'];
  80. } else {
  81. return FALSE;
  82. }
  83. }
  84. public function setUemail($uemail)
  85. {
  86. return $_SESSION['uemail'] = $uemail;
  87. }
  88. public function getAvatar($uid)
  89. {
  90. // $dir1 = ceil($uid / 10000);
  91. // $dir2 = ceil($uid % 10000 / 1000);
  92. // $url = 'http://sso.smartcost.com.cn/' . 'data/avatar/' . $dir1 . '/' . $dir2 . '/' . $uid . '/';
  93. // $avatar = array('180' => $url . '180x180.jpg', '90' => $url . '90x90.jpg', '45' => $url . '45x45.jpg', '30' => $url . '30x30.jpg');
  94. // return $avatar;
  95. $valArray = $this->profile->getOne(array('where' => 'userid=?', 'param' => array($uid), 'asArray' => TRUE));
  96. return Doo::conf()->APP_URL . $valArray['avatar'];
  97. }
  98. public function getSignpath($uid)
  99. {
  100. $valArray = $this->profile->getOne(array('where' => 'userid=?', 'param' => array($uid), 'asArray' => TRUE));
  101. return Doo::conf()->APP_URL . $valArray['signpath'];
  102. }
  103. public function getName($uid)
  104. {
  105. $name = $this->profile->getOne(array('select' => 'name', 'where' => 'userid=?', 'param' => array($uid), 'asArray' => TRUE));
  106. return $name['name'];
  107. }
  108. public function getRowByUid($uid)
  109. {
  110. return $this->profile->getOne(array('where' => 'userid=?', 'param' => array($uid), 'asArray' => TRUE));
  111. }
  112. public function checkUserEmail($email)
  113. {
  114. $result = $this->users->getOne(array('select' => 'uid', 'where' => 'uemail=?', 'param' => array($email), 'asArray' => TRUE));
  115. if(!empty($result)){
  116. return $this->profile->getOne(array('where' => 'userid=?', 'param' => array($result['uid']), 'asArray' => TRUE));
  117. }else{
  118. return '';
  119. }
  120. }
  121. public function checkLogin($uemail, $upasswd)
  122. {
  123. return $this->user->login($uemail, $upasswd);
  124. }
  125. public function logout()
  126. {
  127. session_destroy();
  128. setcookie('token', '-1', 0, '/', 'jl.local', FALSE, TRUE);
  129. }
  130. public function checkauth()
  131. {
  132. //TODO 启用SESSION变量避免重复查询数据库
  133. if (isset($_COOKIE['M0s5Yi_yn_k']) && isset($_COOKIE['M0s5Yi_yn_v'])) {
  134. $uname = $this->decryptCookie($_COOKIE['M0s5Yi_yn_k']);
  135. $passwd = $this->decryptCookie($_COOKIE['M0s5Yi_yn_v']);
  136. if ($uname && $passwd) {
  137. return TRUE;
  138. } else {
  139. return FALSE;
  140. }
  141. } else {
  142. return FALSE;
  143. }
  144. }
  145. function isLoggedIn()
  146. {
  147. if (isset($_SESSION['token']) && isset($_COOKIE['token'])) {
  148. if ($_SESSION['token'] != $_COOKIE['token']) {
  149. return TRUE;
  150. }
  151. }
  152. return FALSE;
  153. }
  154. public function getUname()
  155. {
  156. //TODO 启用SESSION变量避免重复查询数据库
  157. if (isset($_COOKIE['M0s5Yi_yn_k']) && isset($_COOKIE['M0s5Yi_yn_v'])) {
  158. $uname = $this->decryptCookie($_COOKIE['M0s5Yi_yn_k']);
  159. $passwd = $this->decryptCookie($_COOKIE['M0s5Yi_yn_v']);
  160. if ($uname && $passwd) {
  161. return $uname;
  162. } else {
  163. return FALSE;
  164. }
  165. } else {
  166. return FALSE;
  167. }
  168. }
  169. private function encryptCookie($value)
  170. {
  171. if (!$value) {
  172. return false;
  173. }
  174. $key = '290234lk23jk23djLHSWCs92s';
  175. $text = $value;
  176. $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
  177. $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  178. $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
  179. return trim(base64_encode($crypttext)); //encode for cookie
  180. }
  181. private function decryptCookie($value)
  182. {
  183. if (!$value) {
  184. return false;
  185. }
  186. $key = '290234lk23jk23djLHSWCs92s';
  187. $crypttext = base64_decode($value); //decode cookie
  188. $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
  189. $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  190. $decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
  191. return trim($decrypttext);
  192. }
  193. }
  194. ?>