非常简单实用的控制器基类
-
/**
- * @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->admin['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;
- }
-
- 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 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]):'';
- }
- }
-
复制代码
|