首頁 > 後端開發 > php教程 > 控制器基類

控制器基類

WBOY
發布: 2016-07-25 08:47:57
原創
1396 人瀏覽過
複製程式碼
非常簡單實用的控制器基類
  1. /**
  2. * @desc 控制器基底類別
  3. * @date 2013-05-06
  4. * @author liudesheng
  5. */
  6. defined('SYS_PATH') || die('訪問非法');
  7. class controller
  8. {
  9. //當前控制器
  10. protected $_controller;
  11. //當前動作方法
  12. protected $_action;
  13. //權限數組
  14. protected $_permissions;
  15. //範本檔案
  16. private $_layout = 'layout';
  17. //建構子
  18. function __construct($controller,$action)
  19. {
  20. if('exception' != $controller){
  21. $this->_controller = $controller;
  22. $this->_action = $action;
  23. //登入檢查和存取權限控制部分,登入頁面不需要驗證
  24. $trust_action = util::c('trust_action');
  25. if(!isset($trust_action[$this->_controller]) || !in_array($this->_action,$trust_action[$ this->_controller])){
  26. $this->login();
  27. //$this->privilege();
  28. }
  29. $this->init();
  30. }else{//異常處理
  31. $this->exception($action);
  32. }
  33. }
  34. //初始化方法,用於繼承運算
  35. protected function init( ){}
  36. //異常處理方法
  37. private function exception($msg)
  38. {
  39. $this->showErr($msg,$layout);
  40. }
  41. //驗證登入
  42. private function login()
  43. {
  44. if(!$this->isLogin()){
  45. if($this->isAjax()){
  46. header('HTTP/1.1 403 Forbidden');
  47. header("Error-Json:{code:'login'}");
  48. exit();
  49. }else{
  50. $
  51. $ this->redirect('index','login');
  52. }
  53. }
  54. }
  55. //判斷是否登入
  56. protected final function isLogin()
  57. {
  58. $auth = isset($_COOKIE['auth'])?$_COOKIE['auth']:'';
  59. $isLogin = false;
  60. if($auth){
  61. $info = trim(file_get_contents('check.txt'));
  62. if(strcmp($auth,md5('steve'.$info.util::c('login_auth_suffix'))) == 0){
  63. $isLogin = true;
  64. }
  65. }
  66. return $isLogin;
  67. }
  68. //驗證權限
  69. private function privilege()
  70. {
  71. $this->getPermissions();
  72. if(!$this->isAllow()){
  73. if($this->isAjax()){
  74. header('HTTP/1.1 403 Forbidden') ;
  75. header( "Error-Json:{code:'access'}");
  76. exit();
  77. }else{
  78. $this->showErr('對不起,您沒有此權限');
  79. }
  80. }
  81. }
  82. //取得權限資訊
  83. protected final function getPermissions()
  84. {
  85. $privilege = $this-this> ['privilege'];
  86. $permissions_priv = util::c('permissions',$privilege);
  87. if(!isset($permissions_priv['city'])){
  88. $this-> cityPriv = 'all'; //為了簡化列表查詢,方便以後可能添加所有城市權限選擇
  89. }else{
  90. unset($permissions_priv['city']);
  91. }
  92. foreach( $permissions['common'] as $ct => $ac){
  93. if(isset($permissions_priv[$ct]) && 'all' == $permissions_priv[$ct])
  94. continue;
  95. if('all' == $ac)
  96. $permissions_priv[$ct] = 'all';
  97. else //這種情況必須是數組,節省資源,不做判斷了
  98. $permissions_priv [$ct] = isset($permissions_priv[$ct])?array_merge($permissions_priv[$ct],$ac):$ac;
  99. }
  100. $this->_permissions = $permissions_priv
  101. ;
  102. }
  103. //依權限類型判斷是否有權限
  104. protected final function isAllow($controller='',$action='')
  105. {
  106. if(!isset($this ->_permissions))
  107. $this->getPermissions();
  108. $allow = false;
  109. $ct = $controller?$controller:$this->_controller;
  110. $ac = $action ?$action:$this->_action;
  111. $permission_action = $this->_permissions[$ct];
  112. if($permission_action && ('all' == $permission_action || in_array($ac,$ permission_action) || 'any' == $action))
  113. $allow = true;
  114. return $allow;
  115. }
  116. //錯誤訊息頁
  117. protected function showErr($errMsg,$layout = null)
  118. {
  119. $this->title = "錯誤提示";
  120. $this->errMsg = $errMsg;
  121. $this->render( 'error',$layout);
  122. }
  123. //成功資訊頁
  124. protected function showSucc($msg,$skipUrl,$skipPage,$layout = null)
  125. {
  126. $this->title = "成功提示";
  127. $this->msg = $msg;
  128. $this->skipUrl = $skipUrl;
  129. $this->skipPage = $skipPage;
  130. $this->render('success',$layout);
  131. }
  132. //顯示有權限的連結
  133. protected function showPemissionLink($title,$ct,$ac,$param =array(),$wrap='')
  134. {
  135. if($wrap){
  136. $wrap_start = '';
  137. $wrap_end = ' '.$wrap.'>';
  138. }else{ $wrap_start = $wrap_end = ''; }
  139. if($this->isAllow($ct,$ac))
  140. echo $wrap_start,'',$title,'',$wrap_end;
  141. }
  142. // 視圖解析方法
  143. protected function render($template = null,$Layout = null)
  144. {
  145. !is_null($layout) && $this->_layout = $layout;
  146. !$template && $template = $this->_controller.'_'.$ this->_action;
  147. ob_start();
  148. include(MODULE_PATH.'views/'.$this->_layout.'.tpl.php');
  149. $content = ob_get_clean();
  150. if($this->staticFile){
  151. file_put_contents($this->staticFile,$content);
  152. }
  153. echo $content;
  154. exit;
  155. }
  156. echo $content;
  157. exit;
  158. }
  159. 受保護函數showHtml($html,$expire=3600,$path='')
  160. {
  161. 空($path) && $path=ROOT_PATH;
  162. $this->staticFile = sprintf ('%s%s.html',$path,$html);
  163. $mkhtml = intval($this->_G('mkhtml'));
  164. if(!$mkhtml){
  165. if(file_exists($this->staticFile)){
  166. $fmtime = filemtime($this->staticFile);
  167. if(time()-$fmtime include $this->staticFile;
  168. 退出;
  169. }
  170. }
  171. }
  172. }
  173. // //產生url
  174. protected function url($ct='',$ac='',$param = array(),$module='')
  175. {
  176. return $ GLOBALS['app']- >url($ct,$ac,$param,$module);
  177. }
  178. //url 截圖
  179. protected 函數重定向($ct=' ',$ac='', $ param = array())
  180. {
  181. header('位置:'.$this->url($ct,$ac,$param));
  182. exit();
  183. }
  184. //url跳轉
  185. protected function redirectUrl($url)
  186. {
  187. header('location:'.$url);
  188. exit();
  189. }
  190. //取得返回重定向url
  191. protected function getBru()
  192. {
  193. return $_COOKIE[util::c('bru_cookie_name')]? $_COOKIE[util::c('bru_cookie_name')]:$this->url();
  194. }
  195. // 是否是ajax請求
  196. protected function isAjax()
  197. {
  198. if(isset( $_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')
  199. 回傳true;
  200. 返回🎜>; > //回傳json資料
  201. protected function returnJson($data)
  202. {
  203. echo json_encode($data);
  204. exit();
  205. }
  206. // GET
  207. 受保護函數_G($name)
  208. {
  209. return isset($_GET[$name])?util::sanitize($_GET[$name]):'';
  210. }
  211. //POST
  212. 受保護函數_P($name)
  213. {
  214. if(!isset($_POST[$name]) || (is_string($_POST[$name]) && mb_strpos( $_POST[$name],'請輸入',0,'gbk') === 0)){
  215. return '';
  216. }else {
  217. return util::sanitize($ _POST[ $name]);
  218. }
  219. }
  220. //REQUEST
  221. 受保護函數_R($name)
  222. {
  223. return isset($_REQUEST[$name]) ?util: :sanitize($_REQUEST[$name]):'';
  224. }
  225. }
複製程式碼


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板