LoginController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. Doo::loadClass('auth');
  3. Doo::loadClass('profile');
  4. Doo::loadModelAt('aconfig', 'admin');
  5. /**
  6. * MainController
  7. * Feel free to delete the methods and replace them with your own code.
  8. *
  9. * @author NoNZero
  10. */
  11. class LoginController extends DooController
  12. {
  13. private $data, $auth, $profile, $aconfig;
  14. public function beforeRun($resource, $action)
  15. {
  16. $uGroups = $this->profile->getUidByname($this->auth->getUid());
  17. if (!isset($uGroups['groups']))
  18. $uGroups['groups'] = 'anonymous';
  19. $falg = Doo::acl()->isAllowed($uGroups['groups'], $resource, $action);
  20. if (!$falg)
  21. return Doo::acl()->defaultFailedRoute;
  22. }
  23. public function __construct()
  24. {
  25. $this->auth = new Auth();
  26. $this->profile = new Uprofile();
  27. $this->aconfig = new AConfig();
  28. $this->data['rootUrl'] = Doo::conf()->APP_URL;
  29. }
  30. public function welcome()
  31. {
  32. $this->render('welcome', $this->data);
  33. }
  34. public function Signin()
  35. {
  36. $this->data['tips'] = '';
  37. if ($_GET['username']) {
  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. setcookie('token', $_SESSION['token'], 0, '/', Doo::conf()->APP_URL, FALSE, TRUE);
  45. return Doo::conf()->APP_URL . 'project/index';
  46. } else {
  47. $this->data['tips'] = '<div class="alert alert-error"><span data-icon="t" aria-hidden="true"></span> 帐号不存在或者密码错误</div>';
  48. }
  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. setcookie('token', $_SESSION['token'], 0, '/', Doo::conf()->APP_URL, FALSE, TRUE);
  59. return Doo::conf()->APP_URL . 'project/index';
  60. } else {
  61. $this->data['tips'] = '<div class="alert alert-error"><span data-icon="t" aria-hidden="true"></span> 帐号不存在或者密码错误</div>';
  62. }
  63. } else {
  64. return Doo::conf()->APP_URL;
  65. }
  66. }
  67. }
  68. $this->data['_token_'] = $this->generateFormHash($this->create_randomstr());
  69. $this->data['proName'] = $this->aconfig->getOne(array('select' => 'proName', 'asArray' => TRUE))['proName'];
  70. $this->data['ver'] = DOO::conf()->ver;
  71. $this->render('login', $this->data);
  72. }
  73. function Signout()
  74. {
  75. $this->auth->logout();
  76. return Doo::conf()->APP_URL;
  77. }
  78. function IsSessionHijacking()
  79. {
  80. $string = $_SERVER['HTTP_USER_AGENT'];
  81. if (!isset($_SESSION['randstr']))
  82. $_SESSION['randstr'] = $this->create_randomstr();
  83. $string .= $_SESSION['randstr'];
  84. $token = md5($string);
  85. $_SESSION['token'] = $token;
  86. if ($_SESSION['token'] == md5($_SERVER['HTTP_USER_AGENT'] . $_SESSION['randstr'])) {
  87. return FALSE;
  88. } else {
  89. return TRUE;
  90. }
  91. }
  92. function isLoggedIn()
  93. {
  94. if (isset($_SESSION['token']) && isset($_COOKIE['token'])) {
  95. if ($_SESSION['token'] != $_COOKIE['token']) {
  96. $this->Signout();
  97. setcookie('token', '-1', 0, '/', Doo::conf()->APP_URL, FALSE, TRUE);
  98. }
  99. return isset($_SESSION['uid']);
  100. }
  101. }
  102. function generateFormHash($salt)
  103. {
  104. $hash = sha1(mt_rand(1, 1000000) . $salt);
  105. $_SESSION['csrf_hash'] = $hash;
  106. return $hash;
  107. }
  108. function isValidFormHash($hash)
  109. {
  110. return $_SESSION['csrf_hash'] === $hash;
  111. }
  112. /**
  113. * 随机字符串函数
  114. * @param $password 密码
  115. * @param $random 随机数
  116. */
  117. function random($length, $chars = '0123456789')
  118. {
  119. $hash = '';
  120. $max = strlen($chars) - 1;
  121. for ($i = 0; $i < $length; $i++) {
  122. $hash .= $chars[mt_rand(0, $max)];
  123. }
  124. return $hash;
  125. }
  126. /**
  127. * 生成随机字符串
  128. * @param string $lenth 长度
  129. * @return string 字符串
  130. */
  131. function create_randomstr($lenth = 6)
  132. {
  133. return $this->random($lenth, '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ');
  134. }
  135. }
  136. ?>