auth.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 getUinfo()
  35. {
  36. return $this->uinfo;
  37. }
  38. public function getUid()
  39. {
  40. if (isset($_SESSION['uid']) && $_SESSION['uid']) {
  41. return $_SESSION['uid'];
  42. } else {
  43. return FALSE;
  44. }
  45. }
  46. public function getUemail()
  47. {
  48. if (isset($_SESSION['uemail']) && $_SESSION['uemail']) {
  49. return $_SESSION['uemail'];
  50. } else {
  51. return 0;
  52. }
  53. }
  54. public function setUid($uid)
  55. {
  56. return $_SESSION['uid'] = $uid;
  57. }
  58. public function setUemail($uemail)
  59. {
  60. return $_SESSION['uemail'] = $uemail;
  61. }
  62. public function getAvatar($uid)
  63. {
  64. // $dir1 = ceil($uid / 10000);
  65. // $dir2 = ceil($uid % 10000 / 1000);
  66. // $url = 'http://sso.smartcost.com.cn/' . 'data/avatar/' . $dir1 . '/' . $dir2 . '/' . $uid . '/';
  67. // $avatar = array('180' => $url . '180x180.jpg', '90' => $url . '90x90.jpg', '45' => $url . '45x45.jpg', '30' => $url . '30x30.jpg');
  68. // return $avatar;
  69. $valArray = $this->profile->getOne(array('where' => 'userid=?', 'param' => array($uid), 'asArray' => TRUE));
  70. return Doo::conf()->APP_URL . $valArray['avatar'];
  71. }
  72. public function checkLogin($uemail, $upasswd)
  73. {
  74. return $this->user->login($uemail, $upasswd);
  75. }
  76. public function logout()
  77. {
  78. session_destroy();
  79. setcookie('token', '-1', 0, '/', 'jl.local', FALSE, TRUE);
  80. }
  81. public function checkauth()
  82. {
  83. //TODO 启用SESSION变量避免重复查询数据库
  84. if (isset($_COOKIE['M0s5Yi_yn_k']) && isset($_COOKIE['M0s5Yi_yn_v'])) {
  85. $uname = $this->decryptCookie($_COOKIE['M0s5Yi_yn_k']);
  86. $passwd = $this->decryptCookie($_COOKIE['M0s5Yi_yn_v']);
  87. if ($uname && $passwd) {
  88. return TRUE;
  89. } else {
  90. return FALSE;
  91. }
  92. } else {
  93. return FALSE;
  94. }
  95. }
  96. function isLoggedIn()
  97. {
  98. if (isset($_SESSION['token']) && isset($_COOKIE['token'])) {
  99. if ($_SESSION['token'] != $_COOKIE['token']) {
  100. return TRUE;
  101. }
  102. }
  103. return FALSE;
  104. }
  105. public function getUname()
  106. {
  107. //TODO 启用SESSION变量避免重复查询数据库
  108. if (isset($_COOKIE['M0s5Yi_yn_k']) && isset($_COOKIE['M0s5Yi_yn_v'])) {
  109. $uname = $this->decryptCookie($_COOKIE['M0s5Yi_yn_k']);
  110. $passwd = $this->decryptCookie($_COOKIE['M0s5Yi_yn_v']);
  111. if ($uname && $passwd) {
  112. return $uname;
  113. } else {
  114. return FALSE;
  115. }
  116. } else {
  117. return FALSE;
  118. }
  119. }
  120. private function encryptCookie($value)
  121. {
  122. if (!$value) {
  123. return false;
  124. }
  125. $key = '290234lk23jk23djLHSWCs92s';
  126. $text = $value;
  127. $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
  128. $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  129. $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv);
  130. return trim(base64_encode($crypttext)); //encode for cookie
  131. }
  132. private function decryptCookie($value)
  133. {
  134. if (!$value) {
  135. return false;
  136. }
  137. $key = '290234lk23jk23djLHSWCs92s';
  138. $crypttext = base64_decode($value); //decode cookie
  139. $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
  140. $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  141. $decrypttext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $crypttext, MCRYPT_MODE_ECB, $iv);
  142. return trim($decrypttext);
  143. }
  144. }
  145. ?>