BaseController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * 核心控制
  4. * @author cp
  5. */
  6. class BaseController extends DooController {
  7. public $data;
  8. public $userinfo;
  9. function __construct() {
  10. $this->data['rootUrl'] = Doo::conf()->APP_URL;
  11. }
  12. public function clogin() {
  13. $this->data['message'] = '';
  14. $this->render('common-login', $this->data);
  15. }
  16. public function alogin() {
  17. $this->render('admin-login', $this->data);
  18. }
  19. /**
  20. * 进入后台
  21. */
  22. function do_login() {
  23. $data ['name'] = $_POST['name'];
  24. $data ['password'] = $_POST['password'];
  25. Doo::loadModel('adminuser');
  26. $users = new Adminuser();
  27. $user = $users->getRowByUsername($data);
  28. if (!empty ( $user )) {
  29. $userinfo = $this->authcode ( $user ['username'], "tr" );
  30. setcookie ( "auth_main", $userinfo, time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  31. return '/m/userlist';
  32. // header ( 'Content-Type:text/html;charset=utf-8' );
  33. // @header ( "Location: " . COOKIE_WEB_SITE . "/m/userlist" );
  34. }else{
  35. return '/m/login';
  36. // header ( 'Content-Type:text/html;charset=utf-8' );
  37. // @header ( "Location: " . COOKIE_WEB_SITE . "/m/login" );
  38. }
  39. }
  40. /**
  41. * 后台登录
  42. */
  43. function admin_init(){
  44. //是否已经登录纵横
  45. $rs=array();
  46. if (isset ( $_COOKIE ['auth_main'] )) {
  47. $userinfo=$this->authcode ($_COOKIE ['auth_main']);
  48. Doo::loadModel('adminuser');
  49. $users = new Adminuser();
  50. $this->userinfo=$users->getRowByuser($userinfo);
  51. }else{
  52. setcookie ( "auth_main", "", time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  53. //无权限进入后台
  54. header ( 'Content-Type:text/html;charset=utf-8' );
  55. @header ( "Location: /m/login");
  56. }
  57. if (!empty ( $this->userinfo )) {
  58. //跳转前台登录
  59. $userinfo = $this->authcode ( $this->userinfo['username'], "tr" );
  60. //header ( 'Content-Type:text/html;charset=utf-8' );
  61. //@header ( "Location: /" );
  62. setcookie ( "auth_main", $userinfo, time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  63. } else {
  64. setcookie ( "auth_main", "", time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  65. //无权限进入后台
  66. header ( 'Content-Type:text/html;charset=utf-8' );
  67. @header ( "Location: /m/login");
  68. }
  69. }
  70. public function do_clogin() {
  71. if(!isset($_POST['name']) || empty($_POST['name'])){
  72. $this->data['message'] = '帐号不能为空';
  73. $this->render('common-login',$this->data);die;
  74. }
  75. if(!isset($_POST['password']) || empty($_POST['password'])){
  76. $this->data['message'] = '密码不能为空';
  77. $this->render('common-login',$this->data);die;
  78. }
  79. $data ['name'] = $_POST['name'];
  80. $data ['password'] = $_POST['password'];
  81. Doo::loadModel('commonuser');
  82. $users = new Commonuser();
  83. $user = $users->getRowByUsername($data);
  84. if (!empty ( $user )) {
  85. $userinfo = $this->authcode ( $user ['username'], "tr" );
  86. setcookie ( "common_main", $userinfo, time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  87. header ( 'Content-Type:text/html;charset=utf-8' );
  88. @header ( "Location: /common/classlist");
  89. // return '/common/classlist';
  90. }else{
  91. $this->data['message'] = '账号或密码错误!';
  92. $this->render('common-login',$this->data);die;
  93. }
  94. }
  95. /**
  96. * 查询登录
  97. */
  98. function common_init(){
  99. if (isset ( $_COOKIE ['common_main'] )) {
  100. $userinfo=$this->authcode ($_COOKIE ['common_main']);
  101. Doo::loadModel('commonuser');
  102. $users = new Commonuser();
  103. $this->userinfo=$users->getRowByUser($userinfo);
  104. }else{
  105. setcookie ( "common_main", "", time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  106. //无权限进入后台
  107. header ( 'Content-Type:text/html;charset=utf-8' );
  108. @header ( "Location: /common/login");
  109. }
  110. if (!empty ( $this->userinfo )) {
  111. //跳转前台登录
  112. $userinfo = $this->authcode ( $this->userinfo['username'], "tr" );
  113. //header ( 'Content-Type:text/html;charset=utf-8' );
  114. //@header ( "Location: /" );
  115. setcookie ( "common_main", $userinfo, time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  116. return $this->userinfo['username'];
  117. } else {
  118. setcookie ( "common_main", "", time () + 3600 * 12, "/", COOKIE_WEB_SITE );
  119. //无权限进入后台
  120. header ( 'Content-Type:text/html;charset=utf-8' );
  121. @header ( "Location: /common/login");
  122. }
  123. }
  124. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
  125. $ckey_length = 4;
  126. if (! $key) {
  127. $key = "ZHKT";
  128. }
  129. $key = md5 ( $key );
  130. $keya = md5 ( substr ( $key, 0, 16 ) );
  131. $keyb = md5 ( substr ( $key, 16, 16 ) );
  132. $keyc = $ckey_length ? ($operation == 'DECODE' ? substr ( $string, 0, $ckey_length ) : substr ( md5 ( microtime () ), - $ckey_length )) : '';
  133. $cryptkey = $keya . md5 ( $keya . $keyc );
  134. $key_length = strlen ( $cryptkey );
  135. $string = $operation == 'DECODE' ? base64_decode ( substr ( $string, $ckey_length ) ) : sprintf ( '%010d', $expiry ? $expiry + time () : 0 ) . substr ( md5 ( $string . $keyb ), 0, 16 ) . $string;
  136. $string_length = strlen ( $string );
  137. $result = '';
  138. $box = range ( 0, 255 );
  139. $rndkey = array ();
  140. for($i = 0; $i <= 255; $i ++) {
  141. $rndkey [$i] = ord ( $cryptkey [$i % $key_length] );
  142. }
  143. for($j = $i = 0; $i < 256; $i ++) {
  144. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  145. $tmp = $box [$i];
  146. $box [$i] = $box [$j];
  147. $box [$j] = $tmp;
  148. }
  149. for($a = $j = $i = 0; $i < $string_length; $i ++) {
  150. $a = ($a + 1) % 256;
  151. $j = ($j + $box [$a]) % 256;
  152. $tmp = $box [$a];
  153. $box [$a] = $box [$j];
  154. $box [$j] = $tmp;
  155. $result .= chr ( ord ( $string [$i] ) ^ ($box [($box [$a] + $box [$j]) % 256]) );
  156. }
  157. if ($operation == 'DECODE') {
  158. if ((substr ( $result, 0, 10 ) == 0 || substr ( $result, 0, 10 ) - time () > 0) && substr ( $result, 10, 16 ) == substr ( md5 ( substr ( $result, 26 ) . $keyb ), 0, 16 )) {
  159. return substr ( $result, 26 );
  160. } else {
  161. return '';
  162. }
  163. } else {
  164. return $keyc . str_replace ( '=', '', base64_encode ( $result ) );
  165. }
  166. }
  167. // 检测手机格式
  168. function checkMobile($mobile) {
  169. if(preg_match("/^1[34578]{1}\d{9}$/",$mobile)){
  170. return TRUE;
  171. } else {
  172. return FALSE;
  173. }
  174. }
  175. function checkMail($mail) {
  176. //if (preg_match("/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9\-\.])+/", $mail))
  177. if (filter_var($mail, FILTER_VALIDATE_EMAIL))
  178. return true;
  179. else
  180. return false;
  181. }
  182. }
  183. ?>