BaseController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. session_start(); // starts new or resumes existing session
  3. session_regenerate_id(true); // regenerates SESSIONID to prevent hijacking
  4. /**
  5. * 核心控制
  6. * @author cp
  7. */
  8. Doo::loadClass('PasswordHash');
  9. class BaseController extends DooController {
  10. public $data;
  11. public $userinfo;
  12. public $__ph;
  13. function __construct() {
  14. $this->__ph = new PasswordHash(8, FALSE);
  15. $this->data['rootUrl'] = Doo::conf()->APP_URL;
  16. }
  17. public function login() {
  18. $this->data['message'] = '';
  19. $this->data['_token_'] = $this->generateFormHash($this->create_randomstr());
  20. $this->render('login-fc', $this->data);
  21. }
  22. /**
  23. * 进入后台
  24. */
  25. function do_login() {
  26. if(!isset($_POST['name']) || empty($_POST['name'])){
  27. $this->data['message'] = '帐号不能为空';
  28. $this->data['_token_'] = $this->generateFormHash($this->create_randomstr());
  29. $this->render('login-fc',$this->data);die;
  30. }
  31. if(!isset($_POST['password']) || empty($_POST['password'])){
  32. $this->data['message'] = '密码不能为空';
  33. $this->data['_token_'] = $this->generateFormHash($this->create_randomstr());
  34. $this->render('login-fc',$this->data);die;
  35. }
  36. if (!$this->isValidFormHash($_POST['tokenform'])) {
  37. $this->data['message'] = '';
  38. $this->data['_token_'] = $this->generateFormHash($this->create_randomstr());
  39. $this->render('login-fc',$this->data);die;
  40. }
  41. $data ['name'] = $_POST['name'];
  42. $data ['password'] = $_POST['password'];
  43. $user = $this->checkLogin($data);
  44. if (!empty ( $user )) {
  45. $userinfo = $this->authcode ( $user ['username'], "tr" );
  46. setcookie ( "jlzf_main", $userinfo, time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  47. return '/reportlist';
  48. // header ( 'Content-Type:text/html;charset=utf-8' );
  49. // @header ( "Location: " . COOKIE_WEB_SITE . "/m/userlist" );
  50. }else{
  51. $this->data['message'] = '账号或密码错误!';
  52. $this->render('login-fc',$this->data);die;
  53. // header ( 'Content-Type:text/html;charset=utf-8' );
  54. // @header ( "Location: " . COOKIE_WEB_SITE . "/m/login" );
  55. }
  56. }
  57. function checkLogin($data) {
  58. Doo::loadModel('fcuser');
  59. $users = new Fcuser();
  60. $userArray = $users->getRowByuser($data['name']);
  61. if (isset($userArray) && $userArray && $this->__ph->CheckPassword($data['password'], $userArray['password'])) {
  62. return $userArray;
  63. } else {
  64. return FALSE;
  65. }
  66. }
  67. // public function updatePassword(){
  68. // Doo::loadModel('fcuser');
  69. // $users = new Fcuser();
  70. // $userlist = $users->find(array('asArray' => TRUE));
  71. // foreach($userlist as $k => $v){
  72. // $users1 = new Fcuser();
  73. // $users1->id = $v['id'];
  74. // $users1->password = $this->__ph->HashPassword($v['password']);
  75. // $users1->update();
  76. // }
  77. // echo 'ok';
  78. // exit;
  79. // }
  80. /**
  81. * FC系统登录
  82. */
  83. function admin_init(){
  84. //是否已经登录纵横
  85. if (isset ( $_COOKIE ['jlzf_main'] )) {
  86. $userinfo=$this->authcode ($_COOKIE ['jlzf_main']);
  87. Doo::loadModel('fcuser');
  88. $users = new fcuser();
  89. $this->userinfo=$users->getRowByuser($userinfo);
  90. }else{
  91. setcookie ( "jlzf_main", "", time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  92. //无权限进入后台
  93. header ( 'Content-Type:text/html;charset=utf-8' );
  94. @header ( "Location: /login");
  95. }
  96. if (!empty ( $this->userinfo )) {
  97. //跳转前台登录
  98. $userinfo = $this->authcode ( $this->userinfo['username'], "tr" );
  99. //header ( 'Content-Type:text/html;charset=utf-8' );
  100. //@header ( "Location: /" );
  101. setcookie ( "jlzf_main", $userinfo, time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  102. return $this->userinfo;
  103. } else {
  104. setcookie ( "jlzf_main", "", time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  105. //无权限进入后台
  106. header ( 'Content-Type:text/html;charset=utf-8' );
  107. @header ( "Location: /login");
  108. }
  109. }
  110. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  111. $ckey_length = 4;
  112. if (! $key) {
  113. $key = "ZHKT";
  114. }
  115. $key = md5 ( $key );
  116. $keya = md5 ( substr ( $key, 0, 16 ) );
  117. $keyb = md5 ( substr ( $key, 16, 16 ) );
  118. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
  119. $cryptkey = $keya . md5 ( $keya . $keyc );
  120. $key_length = strlen ( $cryptkey );
  121. $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
  122. $string_length = strlen ( $string );
  123. $result = '';
  124. $box = range ( 0, 255 );
  125. $rndkey = array ();
  126. for($i = 0; $i <= 255; $i ++) {
  127. $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
  128. }
  129. for($j = $i = 0; $i < 256; $i ++) {
  130. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  131. $tmp = $box [$i];
  132. $box [$i] = $box [$j];
  133. $box [$j] = $tmp;
  134. }
  135. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  136. $a = ($a + 1) % 256;
  137. $j = ($j + $box [$a]) % 256;
  138. $tmp = $box [$a];
  139. $box [$a] = $box [$j];
  140. $box [$j] = $tmp;
  141. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  142. }
  143. if ($operation == 'DECODE') {
  144. if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
  145. return substr ( $result, 26 );
  146. } else {
  147. return '';
  148. }
  149. } else {
  150. return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
  151. }
  152. }
  153. function generateFormHash($salt)
  154. {
  155. $hash = sha1(mt_rand(1, 1000000) . $salt);
  156. $_SESSION['csrf_hash'] = $hash;
  157. return $hash;
  158. }
  159. function isValidFormHash($hash)
  160. {
  161. return $_SESSION['csrf_hash'] === $hash;
  162. }
  163. /**
  164. * 生成随机字符串
  165. * @param string $lenth 长度
  166. * @return string 字符串
  167. */
  168. function create_randomstr($lenth = 6)
  169. {
  170. return $this->random($lenth, '123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ');
  171. }
  172. }
  173. ?>