LoginController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. Doo::loadClass('auth');
  3. Doo::loadClass('profile');
  4. Doo::loadClass('utoken');
  5. Doo::loadModelAt('aconfig', 'admin');
  6. /**
  7. * MainController
  8. * Feel free to delete the methods and replace them with your own code.
  9. *
  10. * @author NoNZero
  11. */
  12. class LoginController extends DooController
  13. {
  14. private $data, $auth, $profile, $aconfig;
  15. public function beforeRun($resource, $action)
  16. {
  17. $uGroups = $this->profile->getUidByname($this->auth->getUid());
  18. if (!isset($uGroups['groups']))
  19. $uGroups['groups'] = 'anonymous';
  20. $falg = Doo::acl()->isAllowed($uGroups['groups'], $resource, $action);
  21. if (!$falg)
  22. return Doo::acl()->defaultFailedRoute;
  23. }
  24. public function __construct()
  25. {
  26. $this->auth = new Auth();
  27. $this->profile = new Uprofile();
  28. $this->aconfig = new AConfig();
  29. $this->data['rootUrl'] = Doo::conf()->APP_URL;
  30. }
  31. public function welcome()
  32. {
  33. $this->render('welcome', $this->data);
  34. }
  35. public function Signin()
  36. {
  37. $this->data['tips'] = '';
  38. if (isset($_GET['username'])) {
  39. $retval = $this->auth->checkLoginWithUserName($_GET['username']);
  40. if (isset($retval['uid'])) {
  41. $this->auth->setUid($retval['uid']);
  42. $this->auth->setUemail($retval['uemail']);
  43. $_SESSION['token'] = sha1($this->create_randomstr() . $_SESSION['uid']);
  44. $this->auth->updateToken($retval['uid'],$_SESSION['token'],1);
  45. setcookie('token', $_SESSION['token'], 0, '/', Doo::conf()->APP_URL, FALSE, TRUE);
  46. return Doo::conf()->APP_URL . 'project/index';
  47. } else {
  48. $this->data['tips'] = '<div class="alert alert-danger"><span data-icon="t" aria-hidden="true"></span> 帐号或密码错误,请检查输入是否有误。</div>';
  49. }
  50. } else {
  51. if (isset($_POST['uemail']) && isset($_POST['upasswd'])) {
  52. if ($this->isValidFormHash($_POST['tokenform'])) {
  53. $retval = $this->auth->checkLogin($_POST['uemail'], $_POST['upasswd']);
  54. if (isset($retval['uid'])) {
  55. $this->auth->setUid($retval['uid']);
  56. $this->auth->setUemail($retval['uemail']);
  57. $_SESSION['token'] = sha1($this->create_randomstr() . $_SESSION['uid']);
  58. $this->auth->updateToken($retval['uid'],$_SESSION['token'],1);
  59. setcookie('token', $_SESSION['token'], 0, '/', Doo::conf()->APP_URL, FALSE, TRUE);
  60. return Doo::conf()->APP_URL . 'project/index';
  61. } else {
  62. $this->data['tips'] = '<div class="alert alert-danger"><span data-icon="t" aria-hidden="true"></span> 帐号或密码错误,请检查输入是否有误。</div>';
  63. }
  64. } else {
  65. return Doo::conf()->APP_URL;
  66. }
  67. }
  68. }
  69. $this->getsoftware();
  70. $this->data['_token_'] = $this->generateFormHash($this->create_randomstr());
  71. $this->data['proName'] = $this->aconfig->getOne(array('select' => 'proName', 'asArray' => TRUE))['proName'];
  72. $this->data['ver'] = DOO::conf()->ver;
  73. $this->render('login', $this->data);
  74. }
  75. public function Signin2()
  76. {
  77. $this->data['tips'] = '';
  78. if (isset($_POST['uemail']) && isset($_POST['upasswd'])) {
  79. if ($this->isValidFormHash($_POST['tokenform'])) {
  80. $retval = $this->auth->checkLogin2($_POST['uemail'], $_POST['upasswd']);
  81. if (isset($retval['uid'])) {
  82. $this->auth->setUid($retval['uid']);
  83. $this->auth->setUemail($retval['uemail']);
  84. $_SESSION['token'] = sha1($this->create_randomstr() . $_SESSION['uid']);
  85. $this->auth->updateToken($retval['uid'],$_SESSION['token'],1);
  86. setcookie('token', $_SESSION['token'], 0, '/', Doo::conf()->APP_URL, FALSE, TRUE);
  87. return Doo::conf()->APP_URL . 'project/index';
  88. } else {
  89. $this->data['tips'] = '<div class="alert alert-danger"><span data-icon="t" aria-hidden="true"></span> 帐号或密码错误,请检查输入是否有误。</div>';
  90. }
  91. } else {
  92. return Doo::conf()->APP_URL . 'stealth';
  93. }
  94. }
  95. $this->getsoftware();
  96. $this->data['_token_'] = $this->generateFormHash($this->create_randomstr());
  97. $this->data['proName'] = $this->aconfig->getOne(array('select' => 'proName', 'asArray' => TRUE))['proName'];
  98. $this->data['ver'] = DOO::conf()->ver;
  99. $this->render('login-stealth', $this->data);
  100. }
  101. function getsoftware(){
  102. $StrJson = ($this->aconfig->getOne(array('select' => 'upgradeinfo', 'asArray' => TRUE))['upgradeinfo']);
  103. $upgradeinfo = json_decode($StrJson, true);
  104. if ($upgradeinfo) {
  105. $this->data['version'] = $upgradeinfo['version'];
  106. $this->data['download'] = $upgradeinfo['download'];
  107. }
  108. }
  109. function Signout()
  110. {
  111. $this->auth->logout();
  112. return Doo::conf()->APP_URL;
  113. }
  114. function IsSessionHijacking()
  115. {
  116. $string = $_SERVER['HTTP_USER_AGENT'];
  117. if (!isset($_SESSION['randstr']))
  118. $_SESSION['randstr'] = $this->create_randomstr();
  119. $string .= $_SESSION['randstr'];
  120. $token = md5($string);
  121. $_SESSION['token'] = $token;
  122. if ($_SESSION['token'] == md5($_SERVER['HTTP_USER_AGENT'] . $_SESSION['randstr'])) {
  123. return FALSE;
  124. } else {
  125. return TRUE;
  126. }
  127. }
  128. function isLoggedIn()
  129. {
  130. if (isset($_SESSION['token']) && isset($_COOKIE['token'])) {
  131. if ($_SESSION['token'] != $_COOKIE['token']) {
  132. $this->Signout();
  133. setcookie('token', '-1', 0, '/', Doo::conf()->APP_URL, FALSE, TRUE);
  134. }
  135. return isset($_SESSION['uid']);
  136. }
  137. }
  138. function generateFormHash($salt)
  139. {
  140. $hash = sha1(mt_rand(1, 1000000) . $salt);
  141. $_SESSION['csrf_hash'] = $hash;
  142. return $hash;
  143. }
  144. function isValidFormHash($hash)
  145. {
  146. return $_SESSION['csrf_hash'] === $hash;
  147. }
  148. /**
  149. * 随机字符串函数
  150. * @param $password 密码
  151. * @param $random 随机数
  152. */
  153. function random($length, $chars = '0123456789')
  154. {
  155. $hash = '';
  156. $max = strlen($chars) - 1;
  157. for ($i = 0; $i < $length; $i++) {
  158. $hash .= $chars[mt_rand(0, $max)];
  159. }
  160. return $hash;
  161. }
  162. /**
  163. * 生成随机字符串
  164. * @param string $lenth 长度
  165. * @return string 字符串
  166. */
  167. function create_randomstr($lenth = 6)
  168. {
  169. return $this->random($lenth, '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ');
  170. }
  171. }
  172. ?>