Very simple and practical controller base class
- /**
- * @desc controller base class
- * @date 2013-05-06
- * @author liudesheng
- */
- defined('SYS_PATH') || die('Access Illegal');
- class controller
- {
- //Current controller
- protected $_controller;
- //Current action method
- protected $_action;
- //Permission array
- protected $_permissions;
- //Template file
- private $_layout = 'layout';
- //Constructor function
- function __construct($controller,$action )
- {
- if('exception' != $controller){
- $this->_controller = $controller;
- $this->_action = $action;
-
- //Login check and access permission control part, login The page does not require verification
- $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{// Exception handling
- $this->exception($action);
- }
- }
-
- //Initialization method, used for inheritance operations
- protected function init(){}
-
- //Exception handling method
- private function exception($ msg)
- {
- $this->showErr($msg,$layout);
- }
-
- //Verify login
- 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');
- }
- }
- }
-
- //Determine whether to log in
- 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;
- }
-
- //Verification Permissions
- 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('Sorry, you do not have this permission ');
- }
- }
- }
-
- //Get permission information
- protected final function getPermissions()
- {
- $privilege = $this->admin['privilege'];
- $permissions_priv = util::c( 'permissions',$privilege);
- if(!isset($permissions_priv['city'])){
- $this->cityPriv = 'all'; //In order to simplify the list query, it is possible to add all city permissions in the future Select
- }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 //This case must be an array , save resources and don’t make judgments
- $permissions_priv[$ct] = isset($permissions_priv[$ct])?array_merge($permissions_priv[$ct],$ac):$ac;
- }
- $this-> _permissions = $permissions_priv;
- }
-
- //Determine whether there is permission based on the permission type
- 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;
- }
-
-
- //Error message page
- protected function showErr($errMsg,$layout = null)
- {
- $this->title = "Error message" ;
- $this->errMsg = $errMsg;
- $this->render('error',$layout);
- }
-
- //Success information page
- protected function showSucc($msg,$skipUrl,$skipPage ,$layout = null)
- {
- $this->title = "Success Tip";
- $this->msg = $msg;
- $this->skipUrl = $skipUrl;
- $this->skipPage = $skipPage;
- $this->render('success',$layout);
- }
-
- //Show permissioned links
- protected function showPemissionLink($title,$ct,$ac,$param=array( ),$wrap='')
- {
- if($wrap){
- $wrap_start = '<'.$wrap.'>';
- $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;
- }
-
- protected function showHtml($html,$expire=3600,$path='')
- {
- empty($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 < $expire && date('Ymd') == date('Ymd',$fmtime)){
- include $this->staticFile;
- exit;
- }
- }
- }
- }
-
- //生成url
- protected function url($ct='',$ac='',$param = array(),$module='')
- {
- return $GLOBALS['app']->url($ct,$ac,$param,$module);
- }
-
- //url跳转
- protected function redirect($ct='',$ac='',$param = array())
- {
- header('location:'.$this->url($ct,$ac,$param));
- exit();
- }
-
- //url跳转
- protected function redirectUrl($url)
- {
- header('location:'.$url);
- exit();
- }
-
- //获取back redirect 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')
- return true;
- return false;
- }
-
- //返回json数组
- protected function returnJson($data)
- {
- echo json_encode($data);
- exit();
- }
-
- //GET
- protected function _G($name)
- {
- return isset($_GET[$name])?util::sanitize($_GET[$name]):'';
- }
- //POST
- protected function _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
- protected function _R($name)
- {
- return isset($_REQUEST[$name])?util::sanitize($_REQUEST[$name]):'';
- }
- }
-
复制代码
|