LoginController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 (isset($_GET['username'])) {
  38. $retval = $this->auth->checkLoginWithUserName($_GET['username']);
  39. if (isset($retval['uid'])) {
  40. $this->auth->setUid($retval['uid']);
  41. $this->auth->setUemail($retval['uemail']);
  42. $_SESSION['token'] = sha1($this->create_randomstr() . $_SESSION['uid']);
  43. setcookie('token', $_SESSION['token'], 0, '/', Doo::conf()->APP_URL, FALSE, TRUE);
  44. return Doo::conf()->APP_URL . 'project/index';
  45. } else {
  46. $this->data['tips'] = '<div class="alert alert-error"><span data-icon="t" aria-hidden="true"></span> 帐号不存在或者密码错误</div>';
  47. }
  48. } else {
  49. if (isset($_POST['uemail']) && isset($_POST['upasswd'])) {
  50. if ($this->isValidFormHash($_POST['tokenform'])) {
  51. $retval = $this->auth->checkLogin($_POST['uemail'], $_POST['upasswd']);
  52. if (isset($retval['uid'])) {
  53. $this->auth->setUid($retval['uid']);
  54. $this->auth->setUemail($retval['uemail']);
  55. $_SESSION['token'] = sha1($this->create_randomstr() . $_SESSION['uid']);
  56. setcookie('token', $_SESSION['token'], 0, '/', Doo::conf()->APP_URL, FALSE, TRUE);
  57. return Doo::conf()->APP_URL . 'project/index';
  58. } else {
  59. $this->data['tips'] = '<div class="alert alert-error"><span data-icon="t" aria-hidden="true"></span> 帐号不存在或者密码错误</div>';
  60. }
  61. } else {
  62. return Doo::conf()->APP_URL;
  63. }
  64. }
  65. }
  66. $this->data['_token_'] = $this->generateFormHash($this->create_randomstr());
  67. $this->data['proName'] = $this->aconfig->getOne(array('select' => 'proName', 'asArray' => TRUE))['proName'];
  68. $this->data['ver'] = DOO::conf()->ver;
  69. $this->render('login', $this->data);
  70. }
  71. function Signout()
  72. {
  73. $this->auth->logout();
  74. return Doo::conf()->APP_URL;
  75. }
  76. function IsSessionHijacking()
  77. {
  78. $string = $_SERVER['HTTP_USER_AGENT'];
  79. if (!isset($_SESSION['randstr']))
  80. $_SESSION['randstr'] = $this->create_randomstr();
  81. $string .= $_SESSION['randstr'];
  82. $token = md5($string);
  83. $_SESSION['token'] = $token;
  84. if ($_SESSION['token'] == md5($_SERVER['HTTP_USER_AGENT'] . $_SESSION['randstr'])) {
  85. return FALSE;
  86. } else {
  87. return TRUE;
  88. }
  89. }
  90. function isLoggedIn()
  91. {
  92. if (isset($_SESSION['token']) && isset($_COOKIE['token'])) {
  93. if ($_SESSION['token'] != $_COOKIE['token']) {
  94. $this->Signout();
  95. setcookie('token', '-1', 0, '/', Doo::conf()->APP_URL, FALSE, TRUE);
  96. }
  97. return isset($_SESSION['uid']);
  98. }
  99. }
  100. function generateFormHash($salt)
  101. {
  102. $hash = sha1(mt_rand(1, 1000000) . $salt);
  103. $_SESSION['csrf_hash'] = $hash;
  104. return $hash;
  105. }
  106. function isValidFormHash($hash)
  107. {
  108. return $_SESSION['csrf_hash'] === $hash;
  109. }
  110. /**
  111. * 随机字符串函数
  112. * @param $password 密码
  113. * @param $random 随机数
  114. */
  115. function random($length, $chars = '0123456789')
  116. {
  117. $hash = '';
  118. $max = strlen($chars) - 1;
  119. for ($i = 0; $i < $length; $i++) {
  120. $hash .= $chars[mt_rand(0, $max)];
  121. }
  122. return $hash;
  123. }
  124. /**
  125. * 生成随机字符串
  126. * @param string $lenth 长度
  127. * @return string 字符串
  128. */
  129. function create_randomstr($lenth = 6)
  130. {
  131. return $this->random($lenth, '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ');
  132. }
  133. }
  134. ?>