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 }
时间: 2024-12-20 21:27:08

Exphp代码走读(三)的相关文章

Exphp代码走读(二)

App.class.php 1 <?php 2 namespace System\Core; 3 use System\Driver; 4 5 class App{ 6 public $DB; 7 public $Cache; 8 public $Session; 9 public $Controller; 10 11 public function __construct(){ 12 DBUG ? set_error_handler(array($this,'errorHandler')) :

Exphp代码走读

Main.php文件 <?php if(!defined('APP')){ exit('APP does not defined'); } //必须先定义APP常量 if(DBUG==1){ ini_set('display_errors',1); error_reporting(E_ALL ^ E_NOTICE); }else{ ini_set('display_errors',0); error_reporting(0); } //判断DEBUG是否设置为1,为1开启错误提示,其余关闭错误提

Netty工具类HashedWheelTimer源码走读(三)

接上一篇( http://my.oschina.net/haogrgr/blog/490266 ) 8. Worker代码走读. //主要负责累加tick, 执行到期任务等. private final class Worker implements Runnable {     private final Set<Timeout> unprocessedTimeouts = new HashSet<Timeout>();     private long tick;     @O

研磨设计模式解析及python代码实现——(三)适配器模式(Adapter)

一.适配器模式定义 将一个类的接口转换成另外一个接口,适配器模式使得原本由于接口不兼容,而不能在一起工作的哪些类能够在一起工作. 二.python 实现 1 import string 2 import cPickle as p 3 import datetime 4 import os 5 class LogModel: 6 logId="" 7 operateUser="" 8 operateTime="" 9 logContent=&quo

YbSoftwareFactory 代码生成插件【二十五】:Razor视图中以全局方式调用后台方法输出页面代码的三种方法

上一篇介绍了 MVC中实现动态自定义路由 的实现,本篇将介绍Razor视图中以全局方式调用后台方法输出页面代码的三种方法. 框架最新的升级实现了一个页面部件功能,其实就是通过后台方法查询数据库内容,把查询结果的 HTML 代码呈现到 Razor 视图中,考虑到灵活性,需要能在任意 Razor 视图中调用该方法,这样任意 Razor 页面都能以统一的方式方便地共享该页面部件的 HTML 内容,这对于代码的重用性和可维护性都是非常有必要的. 为实现上述要求,本文介绍如下可供选择的三种方式.   1.

WebRTCDemo.apk代码走读(三):音频接收流程

收到音频包 UdpSocketManagerPosixImpl::Run UdpSocketManagerPosixImpl::Process UdpSocketPosix::HasIncoming(recvfrom) UdpTransportImpl::IncomingRTPCallback UdpTransportImpl::IncomingRTPFunction VoiceChannelTransport::IncomingRTPPacket VoENetworkImpl::Receive

爬虫代码实现三:打通爬虫项目的下载、解析、存储流程

1.新建一个存储接口IStoreService package com.dajiangtai.djt_spider.service; import com.dajiangtai.djt_spider.entity.Page; /** * 数据存储接口 * @author Administrator * */public interface IStoreService { public void store(Page page);} 2.新建一个存储接口实现类ConsoleStoreService

(代码)三帧差分运动目标检测

上一篇文章介绍有原理,本文给出参考代码,注意,这里的代码仅仅有参考意义,并没有考虑工程实际中的效率,内存耗费等问题,望谅解. int CallTime = 0;//定义调用次数计数器 IplImage* BackGroundImage;//上一帧灰度图 IplImage* DiffImage_1;//上一帧差分图的二值化图 void ThreeFrmDiff(IplImage* pColorIn) { CallTime++; if(CallTime > 10)//防止溢出 { CallTime

codeblocks中对代码的三种测试时间方法

嘻--这几天周赛啥的卡时间太紧,别的队友优化常数太屌,所以只能学习下这个代码测试时间的方法了,上网找了两种方法,然后再加上队友的方法,三种都搬上来了. 第一种: #include<time.h> int main() { clock_t startTime=clock(); { //这里是我们要测试的代码; } clock_t endTime=clock(); cout<<"运行时间为::"<<endTime-startTime<<&qu