LoginController.php 5.4 KB

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