LoginController.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. session_start(); // starts new or resumes existing session
  3. session_regenerate_id(true); // regenerates SESSIONID to prevent hijacking
  4. Doo::loadModelAt('auser', 'admin');
  5. Doo::loadClass('PasswordHash');
  6. /*
  7. * To change this license header, choose License Headers in Project Properties.
  8. * To change this template file, choose Tools | Templates
  9. * and open the template in the editor.
  10. */
  11. class LoginController extends DooController {
  12. private $data, $auser, $__ph;
  13. public function __construct() {
  14. $this->auser = new AUser();
  15. $this->__ph = new PasswordHash(8, FALSE);
  16. $this->data['rootUrl'] = Doo::conf()->APP_URL;
  17. }
  18. // function signUp() {
  19. // $this->render('admin-login', $this->data, TRUE);
  20. // }
  21. function signIn() {
  22. $this->data['tips'] = '';
  23. // 密码采用PHPASS
  24. // 防止跨站采用user_agent随机串
  25. // 重复提交CRSF_FORM
  26. // 自动登录 可采用登录后生成一个可验证字符串,要求输入密码可通过网上登录查看(从客户端点击)加验证码
  27. // if ($this->auth->isLoggedIn())
  28. // return Doo::conf()->APP_URL . 'project/welcome';
  29. if (isset($_POST['muser']) && isset($_POST['mpasswd'])) {
  30. if ($this->isValidFormHash($_POST['tokenform'])) {
  31. $retval = $this->checkLogin($_POST['muser'], $_POST['mpasswd']);
  32. if (isset($retval['auid'])) {
  33. $_SESSION['auid'] = $retval['auid'];
  34. $_SESSION['aname'] = $retval['auname'];
  35. $_SESSION['token'] = sha1($this->randomPassword() . $_SESSION['auid']);
  36. setcookie('token', $_SESSION['token'], 0, '/', Doo::conf()->APP_URL, FALSE, TRUE);
  37. return Doo::conf()->APP_URL . 'manage/user/list';
  38. } else {
  39. $this->data['tips'] = '<div class="alert alert-danger">
  40. <span data-icon="t" aria-hidden="true"></span> 帐号不存在或者密码错误。
  41. </div>';
  42. // die();
  43. }
  44. } else {
  45. return Doo::conf()->APP_URL . 'manage';
  46. }
  47. }
  48. $this->data['_token_'] = $this->generateFormHash($this->randomPassword());
  49. $this->render('admin-login', $this->data, TRUE);
  50. }
  51. function signOut() {
  52. session_destroy();
  53. setcookie('token', '-1', 0, '/', Doo::conf()->APP_URL, FALSE, TRUE);
  54. return Doo::conf()->APP_URL . 'manage';
  55. }
  56. function checkLogin($name, $passwd) {
  57. $auserArray = $this->auser->getOne(array('where' => 'auname=?', 'param' => array($name), 'asArray' => TRUE));
  58. if (isset($auserArray) && $auserArray && $this->__ph->CheckPassword($passwd, $auserArray['aupass'])) {
  59. return $auserArray;
  60. } else {
  61. return FALSE;
  62. }
  63. }
  64. public function randomPassword() {
  65. $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
  66. $pass = array(); //remember to declare $pass as an array
  67. $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
  68. for ($i = 0; $i < 8; $i++) {
  69. $n = rand(0, $alphaLength);
  70. $pass[] = $alphabet[$n];
  71. }
  72. return implode($pass); //turn the array into a string
  73. }
  74. function generateFormHash($salt) {
  75. $hash = sha1(mt_rand(1, 1000000) . $salt);
  76. $_SESSION['csrf_hash'] = $hash;
  77. return $hash;
  78. }
  79. function isValidFormHash($hash) {
  80. return $_SESSION['csrf_hash'] === $hash;
  81. }
  82. }