LoginController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. private $data, $auth, $profile,$aconfig;
  13. public function beforeRun($resource, $action) {
  14. $uGroups = $this->profile->getUidByname($this->auth->getUid());
  15. if (!isset($uGroups['groups']))
  16. $uGroups['groups'] = 'anonymous';
  17. $falg = Doo::acl()->isAllowed($uGroups['groups'], $resource, $action);
  18. if (!$falg)
  19. return Doo::acl()->defaultFailedRoute;
  20. }
  21. public function __construct() {
  22. $this->auth = new Auth();
  23. $this->profile = new Uprofile();
  24. $this->aconfig = new AConfig();
  25. $this->data['rootUrl'] = Doo::conf()->APP_URL;
  26. }
  27. // public function Signup() {
  28. // $this->render('login', $this->data);
  29. // }
  30. // public function beforeRun($resource, $action) {
  31. // $uname = $this->auth->getUname();
  32. // if ($uname) {
  33. // $uGroups = $this->users->getUidByname($this->auth->getUname());
  34. // } else {
  35. // $uGroups['groups'] = 'anonymous';
  36. // }
  37. // $falg = Doo::acl()->isAllowed($uGroups['groups'], $resource, $action);
  38. // if (!$falg)
  39. // return Doo::conf()->APP_URL;
  40. // }
  41. public function welcome() {
  42. $this->render('welcome', $this->data);
  43. }
  44. public function Signin() {
  45. $this->data['tips'] = '';
  46. // 密码采用PHPASS
  47. // 防止跨站采用user_agent随机串
  48. // 重复提交CRSF_FORM
  49. // 自动登录 可采用登录后生成一个可验证字符串,要求输入密码可通过网上登录查看(从客户端点击)加验证码
  50. // if ($this->auth->isLoggedIn())
  51. // return Doo::conf()->APP_URL . 'project/welcome';
  52. if (isset($_POST['uemail']) && isset($_POST['upasswd'])) {
  53. if ($this->isValidFormHash($_POST['tokenform'])) {
  54. $retval = $this->auth->checkLogin($_POST['uemail'], $_POST['upasswd']);
  55. if (isset($retval['uid'])) {
  56. $this->auth->setUid($retval['uid']);
  57. $this->auth->setUemail($retval['uemail']);
  58. $_SESSION['token'] = sha1($this->create_randomstr() . $_SESSION['uid']);
  59. setcookie('token', $_SESSION['token'], 0, '/', Doo::conf()->APP_URL, FALSE, TRUE);
  60. // $profileArray = $this->profile->getProWithUid($this->auth->getUid());
  61. // if (isset($profileArray['userid'])) {
  62. return Doo::conf()->APP_URL . 'project/index';
  63. // } else {
  64. // return Doo::conf()->APP_URL . 'project/welcome';
  65. // }
  66. // die();
  67. } else {
  68. $this->data['tips'] = '<div class="alert alert-error">
  69. <span data-icon="t" aria-hidden="true"></span> 帐号不存在或者密码错误
  70. </div>';
  71. // die();
  72. }
  73. } else {
  74. return Doo::conf()->APP_URL;
  75. }
  76. }
  77. $this->data['_token_'] = $this->generateFormHash($this->create_randomstr());
  78. $this->data['proName'] = $this->aconfig->getOne(array('select' => 'proName', 'asArray' => TRUE))['proName'];
  79. $this->data['ver'] = DOO::conf()->ver;
  80. $this->render('login', $this->data);
  81. }
  82. function Signout() {
  83. $this->auth->logout();
  84. return Doo::conf()->APP_URL;
  85. }
  86. function IsSessionHijacking() {
  87. $string = $_SERVER['HTTP_USER_AGENT'];
  88. if (!isset($_SESSION['randstr']))
  89. $_SESSION['randstr'] = $this->create_randomstr();
  90. $string .= $_SESSION['randstr'];
  91. $token = md5($string);
  92. $_SESSION['token'] = $token;
  93. if ($_SESSION['token'] == md5($_SERVER['HTTP_USER_AGENT'] . $_SESSION['randstr'])) {
  94. return FALSE;
  95. } else {
  96. return TRUE;
  97. }
  98. }
  99. function isLoggedIn() {
  100. if (isset($_SESSION['token']) && isset($_COOKIE['token'])) {
  101. if ($_SESSION['token'] != $_COOKIE['token']) {
  102. $this->Signout();
  103. setcookie('token', '-1', 0, '/', Doo::conf()->APP_URL, FALSE, TRUE);
  104. }
  105. return isset($_SESSION['uid']);
  106. }
  107. }
  108. function generateFormHash($salt) {
  109. $hash = sha1(mt_rand(1, 1000000) . $salt);
  110. $_SESSION['csrf_hash'] = $hash;
  111. return $hash;
  112. }
  113. function isValidFormHash($hash) {
  114. return $_SESSION['csrf_hash'] === $hash;
  115. }
  116. /**
  117. * 随机字符串函数
  118. * @param $password 密码
  119. * @param $random 随机数
  120. */
  121. function random($length, $chars = '0123456789') {
  122. $hash = '';
  123. $max = strlen($chars) - 1;
  124. for ($i = 0; $i < $length; $i++) {
  125. $hash .= $chars[mt_rand(0, $max)];
  126. }
  127. return $hash;
  128. }
  129. /**
  130. * 生成随机字符串
  131. * @param string $lenth 长度
  132. * @return string 字符串
  133. */
  134. function create_randomstr($lenth = 6) {
  135. return $this->random($lenth, '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ');
  136. }
  137. }
  138. ?>