LoginController.php 4.9 KB

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