Exphp代码走读(三)
Controller 类
1 <?php 2 namespace System\Core 3 4 5 class Controller { 6 public $Cache; 7 public $Session; 8 public $View; 9 10 private $_requestName; 11 private $_requestMethod; 12 13 public function __construct(){ 14 $this->safescan(); 15 16 Global $App; 17 18 $this->Cache = $App->Cache; 19 $this->Session = $App->Session; 20 21 $this->View = new View($this->Session->getSessionData()); 22 23 $userRole = global_item(‘app_user_role‘); 24 25 if(empty($userRole)){ 26 return; 27 } 28 29 $className = get_class($this); 30 31 if(in_array($className,global_item(‘app_controller_guest‘))){ 32 return; 33 } 34 35 $isUserRole = ‘is‘ . $userRole; 36 37 if(!$this->Session->$isUserRole()){ 38 if($this->Session->isLogin()){ 39 $this->showError(‘你没有访问权限!‘); 40 }else{ 41 $this->showMessage(‘你还没有登录,请先登录!‘,‘/account/login‘); 42 } 43 44 } 45 } 46 47 public function setControllerParams($requestName,$requestMethod){ 48 $this->_requestName = $requestName; 49 $this->_requestMethod = $requestMethod; 50 51 $this->View->setRequestName($requestName); 52 $this->View->setRequestMethod($requestMethod); 53 54 $this->View->setViewName($requestName . ‘_‘ . $requestMethod); 55 } 56 57 58 public function getRequestName(){ 59 return $this->_requestName; 60 } 61 62 public function getRequestMethod(){ 63 return $this->_requestMethod; 64 } 65 66 public function getPageNumber(){ 67 $page = numf(isset($_GET[‘page‘]) ? $_GET[‘page‘] : (isset($_GET[‘p‘])?$_GET[‘p‘] : 0 )); 68 69 return ($page < 1) ? 1 : $page; 70 } 71 72 public function getPageSize($defaultValue = 25){ 73 return config_item(‘cfg_default_pagesize‘,$defaultValue); 74 } 75 76 public function getPageStart($page = 1, $pageSize = 25){ 77 return ($page - 1 ) * $pageSize; 78 } 79 80 public function getPageExtract($pageSize = 0){ 81 $page == $this->getPageNumber(); 82 $pageSize = empty($pageSize) ? $this->getPageSize : $pageSize; 83 $pageStart = $this->getPageStart($page,$pageSize); 84 85 return array(‘page‘=>$page,‘pageSize‘=>$pageSize,‘pageStart‘=>$pageStart); 86 } 87 88 public function getPageSearchWords($string,$delimiter = ‘|‘){ 89 $string = preg_replace(‘/[^\w\@\-\.]+/u‘,‘ ‘,$string); 90 $string = trim($string); 91 $string = preg_replace(‘/\s+/u‘,$delimiter,$string); 92 return $string; 93 } 94 95 96 public function isAjaxRequest(){ 97 return global_item(‘isAjaxRequest‘); 98 } 99 100 public function isRequestApp($requestApp){ 101 return $requestApp == global_item(‘requestApp‘); 102 } 103 104 public function isRequestWebApp(){ 105 return $this->isRequestApp(‘Web‘); 106 } 107 108 109 public function isPostSubmitRequest($isverifyRequestHash = TRUE,$isReturn = false){ 110 if($_SERVER[‘REQUEST_METHOD‘]==‘POST‘ && ($_POST[‘submit‘] || $_POST[‘postSubmit‘])){ 111 if($isVerifyRequestHash){ 112 $now = $this->Session->getTimestamp(); 113 $requestHash = isset($_POST[‘requestHash‘]) ? $_POST[‘requestHash‘] : $_POST[‘formHash‘]; 114 if(empty($requestHash)){ 115 $requestTime = 0; 116 }else{ 117 $requestTime = $this->Session->getRequestHash($requestHash); 118 } 119 120 if(($now - $requestTime) > 86400){ 121 if($isReturn){ 122 return false; 123 }else{ 124 $this->showError(‘服务器错误,请求不合法‘); 125 } 126 } else { 127 return true; 128 } 129 } 130 return true; 131 } else { 132 return false; 133 } 134 } 135 } 136 137 public function isValidRequest($requestHash = ‘‘, $isReturn = FALSE){ 138 $now = $this->Session->getTimestamp(); 139 if(empty($requestHash)){ 140 $requestTime = 0; 141 }else{ 142 $requestTime = $this->Session->getRequestHash($requestHash); 143 } 144 145 if(($now - $requestTime) > 86400){ 146 if($isReturn){ 147 return false; 148 }else{ 149 $this->showError(‘服务器错误,请求不合法!‘); 150 } 151 } else { 152 return true; 153 } 154 } 155 156 157 public function checkLoginState($forward = ‘‘,$isReturn = false,$tplName=‘account_login‘){ 158 if($this->Session->isLogin()){ 159 return true; 160 }else{ 161 if($isReturn){ 162 return false; 163 }else{ 164 if($this->isAjaxRequest()){ 165 $responseScript = ‘$("body").exRequest({requestUrl:"/account/login?requestForward=‘ . urlencode ($forward) . ‘")‘; 166 $this->View->setAjaxResponseScript($responseScript); 167 $this->showMessage(‘请先登录‘); 168 } else { 169 $this->showMessage(‘你还没有登录‘,‘/account/login?format=‘.urlencode($forward),1); 170 } 171 exit; 172 } 173 } 174 } 175 176 177 public function tryAjaxOutput($ajaxData = null){ 178 if($this->isAjaxRequest()){ 179 $this->View->ajaxOutput($ajaxData); 180 } 181 } 182 183 public show404($message = ‘出错啦!你访问的页面不存在!‘,$messageCode = 400){ 184 $this->showMessage($message,‘/‘,9,‘show_message‘,404); 185 } 186 187 188 //$message,提示信息 189 //$forwardUrl,跳转的URL 190 //$forwardSecond,页面的等待时间 191 //$messageTemplate,使用的模板 192 //$messageCode,HTTP状态码,3位 193 public showMessage($message,$forwardUrl = ‘‘,$forwardSecond = 2,$messageTemplate = ‘show_message‘,$messageCode = 200){ 194 195 $forwardUrl = trim($forwardUrl); 196 if(!empty($forwardUrl) && strtolower(substr($forwardUrl,0,11)) == ‘javascript:‘){ 197 $forwardUrl = ‘‘; 198 } 199 200 201 if(!empty($forwardUrl) && empty($forwardSecond)){ 202 203 header(‘Location: ‘.$forwardUrl,true,$messageCode); 204 205 } else{ 206 $title = ‘提示:‘; 207 if(strpos($message,‘|‘)!==false){ 208 list($title,$message) = explode(‘|‘,$message,2); 209 } 210 $pageTitle = $title; 211 212 if($messageCode == 404){ 213 $httpServerProtocol = getsrv(‘SERVER_PROTOCOL‘); 214 if(empty($httpServerProtocol)){ 215 $httpServerProtocol = ‘HTTP/1.1‘; 216 } 217 218 header($httpServerProtocol . ‘ 404 Not Found‘,true,404); 219 header(‘Status: 404 Not Found‘,true,404); 220 221 $title = ‘404‘; 222 $pageTitle = ‘出错啦‘; 223 if(empty($message) || (defined(‘DBUG‘) && !DBUG)){ 224 $message = ‘出错啦,你访问的页面不存在‘; 225 } 226 } 227 228 $forwardMessage = $message; 229 $forwardLink = ‘‘; 230 $forwardScript = ‘‘; 231 232 if(!empty($forwardUrl)){ 233 $forwardUrlTitle = ‘新‘; 234 if(strpos($forwardUrl,‘|‘)!==false && strpos($forwardUrl,‘|‘) < strpos($forwardUrl,‘/‘)){ 235 list($forwardUrlTitle,$forwardUrl) = explode(‘|‘,$forwardUrl,2); 236 } 237 $forwardLink = ‘<a href="‘ . $forwardUrl . ‘">稍后转入‘ . $forwardUrlTitle . ‘页面..</a>‘; 238 $forwardScript = $forwardSecond > 0 ? ‘<script>setTimeout("window.location.href=\‘‘. $forwardUrl . ‘\‘;",‘.($forwardSecond).‘);</script>‘ : ‘‘; 239 } 240 241 $this->View->setTitle($title); 242 $this->View->setPageTitle($pageTiTle); 243 244 $this->View->addData(‘isShowMessage‘,$messageCode == 200); 245 246 $this->View->addData(‘message‘,$message); 247 $this->View->addData(‘messageCode‘,$messageCode); 248 $this->View->addData(‘forwardMessage‘,$forwardMessage); 249 250 $this->tryAjaxOutput(); 251 252 $this->View->display($messageTemplate); 253 } 254 exit(1); 255 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。