非常簡單實用的控制器基類
-
/**
- * @desc 控制器基底類別
- * @date 2013-05-06
- * @author liudesheng
- */
- defined('SYS_PATH') || die('訪問非法');
- class controller
- {
- //當前控制器
- protected $_controller;
- //當前動作方法
- protected $_action;
- //權限數組
- protected $_permissions;
- //範本檔案
- private $_layout = 'layout';
-
- //建構子
- function __construct($controller,$action)
- {
- if('exception' != $controller){
- $this->_controller = $controller;
- $this->_action = $action;
-
- //登入檢查和存取權限控制部分,登入頁面不需要驗證
- $trust_action = util::c('trust_action');
- if(!isset($trust_action[$this->_controller]) || !in_array($this->_action,$trust_action[$ this->_controller])){
- $this->login();
- //$this->privilege();
- }
- $this->init();
- }else{//異常處理
- $this->exception($action);
- }
- }
-
- //初始化方法,用於繼承運算
- protected function init( ){}
-
- //異常處理方法
- private function exception($msg)
- {
- $this->showErr($msg,$layout);
- }
-
- //驗證登入
- private function login()
- {
- if(!$this->isLogin()){
- if($this->isAjax()){
- header('HTTP/1.1 403 Forbidden');
- header("Error-Json:{code:'login'}");
- exit();
- }else{
- $
- $ this->redirect('index','login');
- }
- }
- }
-
- //判斷是否登入
- protected final function isLogin()
- {
- $auth = isset($_COOKIE['auth'])?$_COOKIE['auth']:'';
- $isLogin = false;
- if($auth){
- $info = trim(file_get_contents('check.txt'));
- if(strcmp($auth,md5('steve'.$info.util::c('login_auth_suffix'))) == 0){
- $isLogin = true;
- }
- }
- return $isLogin;
- }
-
- //驗證權限
- private function privilege()
- {
- $this->getPermissions();
- if(!$this->isAllow()){
- if($this->isAjax()){
- header('HTTP/1.1 403 Forbidden') ;
- header( "Error-Json:{code:'access'}");
- exit();
- }else{
- $this->showErr('對不起,您沒有此權限');
- }
- }
- }
-
- //取得權限資訊
- protected final function getPermissions()
- {
- $privilege = $this-this> ['privilege'];
- $permissions_priv = util::c('permissions',$privilege);
- if(!isset($permissions_priv['city'])){
- $this-> cityPriv = 'all'; //為了簡化列表查詢,方便以後可能添加所有城市權限選擇
- }else{
- unset($permissions_priv['city']);
- }
- foreach( $permissions['common'] as $ct => $ac){
- if(isset($permissions_priv[$ct]) && 'all' == $permissions_priv[$ct])
- continue;
- if('all' == $ac)
- $permissions_priv[$ct] = 'all';
- else //這種情況必須是數組,節省資源,不做判斷了
- $permissions_priv [$ct] = isset($permissions_priv[$ct])?array_merge($permissions_priv[$ct],$ac):$ac;
- }
- $this->_permissions = $permissions_priv
- ;
- }
-
- //依權限類型判斷是否有權限
- protected final function isAllow($controller='',$action='')
- {
- if(!isset($this ->_permissions))
- $this->getPermissions();
- $allow = false;
- $ct = $controller?$controller:$this->_controller;
- $ac = $action ?$action:$this->_action;
- $permission_action = $this->_permissions[$ct];
- if($permission_action && ('all' == $permission_action || in_array($ac,$ permission_action) || 'any' == $action))
- $allow = true;
- return $allow;
- }
-
-
- //錯誤訊息頁
- protected function showErr($errMsg,$layout = null)
- {
- $this->title = "錯誤提示";
- $this->errMsg = $errMsg;
- $this->render( 'error',$layout);
- }
-
- //成功資訊頁
- protected function showSucc($msg,$skipUrl,$skipPage,$layout = null)
- {
- $this->title = "成功提示";
- $this->msg = $msg;
- $this->skipUrl = $skipUrl;
- $this->skipPage = $skipPage;
- $this->render('success',$layout);
- }
-
- //顯示有權限的連結
- protected function showPemissionLink($title,$ct,$ac,$param =array(),$wrap='')
- {
- if($wrap){
- $wrap_start = '';
- $wrap_end = ' '.$wrap.'>';
- }else{ $wrap_start = $wrap_end = ''; }
- if($this->isAllow($ct,$ac))
- echo $wrap_start,'',$title,'',$wrap_end;
- }
-
- // 視圖解析方法
- protected function render($template = null,$Layout = null)
- {
- !is_null($layout) && $this->_layout = $layout;
- !$template && $template = $this->_controller.'_'.$ this->_action;
- ob_start();
- include(MODULE_PATH.'views/'.$this->_layout.'.tpl.php');
- $content = ob_get_clean();
- if($this->staticFile){
- file_put_contents($this->staticFile,$content);
- }
- echo $content;
- exit;
- }
- echo $content;
- exit;
- }
-
- 受保護函數showHtml($html,$expire=3600,$path='')
- {
- 空($path) && $path=ROOT_PATH;
- $this->staticFile = sprintf ('%s%s.html',$path,$html);
- $mkhtml = intval($this->_G('mkhtml'));
- if(!$mkhtml){
- if(file_exists($this->staticFile)){
- $fmtime = filemtime($this->staticFile);
- if(time()-$fmtime include $this->staticFile;
- 退出;
- }
- }
- }
- }
-
- // //產生url
- protected function url($ct='',$ac='',$param = array(),$module='')
- {
- return $ GLOBALS['app']- >url($ct,$ac,$param,$module);
- }
-
- //url 截圖
- protected 函數重定向($ct=' ',$ac='', $ param = array())
- {
- header('位置:'.$this->url($ct,$ac,$param));
- exit();
- }
-
- //url跳轉
- protected function redirectUrl($url)
- {
- header('location:'.$url);
- exit();
- }
-
- //取得返回重定向url
- protected function getBru()
- {
- return $_COOKIE[util::c('bru_cookie_name')]? $_COOKIE[util::c('bru_cookie_name')]:$this->url();
- }
-
- // 是否是ajax請求
- protected function isAjax()
- {
- if(isset( $_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')
- 回傳true;
- 返回🎜>; > //回傳json資料
- protected function returnJson($data)
- {
- echo json_encode($data);
- exit();
- }
-
- // GET
- 受保護函數_G($name)
- {
- return isset($_GET[$name])?util::sanitize($_GET[$name]):'';
- }
- //POST
- 受保護函數_P($name)
- {
- if(!isset($_POST[$name]) || (is_string($_POST[$name]) && mb_strpos( $_POST[$name],'請輸入',0,'gbk') === 0)){
- return '';
- }else {
- return util::sanitize($ _POST[ $name]);
- }
- }
- //REQUEST
- 受保護函數_R($name)
- {
- return isset($_REQUEST[$name]) ?util: :sanitize($_REQUEST[$name]):'';
- }
- }
-
複製程式碼
|
複製程式碼