LoginController.php 4.3 KB

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