ex.php,Exphp代码走读(三)

1 <?php2 namespace System\Core3

4

5 classController {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 functiongetRequestName(){59 return $this->_requestName;60 }61

62 public functiongetRequestMethod(){63 return $this->_requestMethod;64 }65

66 public functiongetPageNumber(){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 functionisAjaxRequest(){97 return global_item(‘isAjaxRequest‘);98 }99

100 public function isRequestApp($requestApp){101 return $requestApp == global_item(‘requestApp‘);102 }103

104 public functionisRequestWebApp(){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,跳转的URL190 //$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 = ‘稍后转入‘ . $forwardUrlTitle . ‘页面..‘;238 $forwardScript = $forwardSecond > 0 ? ‘‘ : ‘‘;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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值