LoginController.php 5.5 KB

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