Very simple and practical controller base class
- /**
- * @ Desc controller base class
- * @ Date 2013-05-06
- * @ Author liudesheng
- */
- Defined ('sys _ path') | die ('Access invalid ');
- Class controller
- {
- // Current Controller
- Protected $ _ controller;
- // Current action method
- Protected $ _ action;
- // Permission array
- Protected $ _ permissions;
- // Template file
- Private $ _ layout = 'layout ';
- // Constructor
- Function _ construct ($ controller, $ action)
- {
- If ('exception '! = $ Controller ){
- $ This-> _ controller = $ controller;
- $ This-> _ action = $ action;
- // Logon check and access permission control. The logon page does not need to be verified.
- $ 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 for inherited operations
- Protected function init (){}
- // Exception handling method
- Private function exception ($ msg)
- {
- $ This-> showErr ($ msg, $ layout );
- }
- // Verify logon
- 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 on
- 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;
- }
- // Verify the permission
- 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 authorization ');
- }
- }
- }
- // Obtain permission information
- Protected final function getPermissions ()
- {
- $ Privilege = $ this-> admin ['privilege'];
- $ Permissions_priv = util: c ('permissions', $ privilege );
- If (! Isset ($ permissions_priv ['city']) {
- $ This-> cityPriv = 'all'; // to simplify list query, you may add all city permissions in the future.
- } 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 // in this case, it must be an array, which saves resources and makes no judgment.
- $ Permissions_priv [$ ct] = isset ($ permissions_priv [$ ct])? Array_merge ($ permissions_priv [$ ct], $ ac): $ ac;
- }
- $ This-> _ permissions = $ permissions_priv;
- }
- // Determine whether you have permissions 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 prompt ";
- $ This-> errMsg = $ errMsg;
- $ This-> render ('error', $ layout );
- }
- // Success Information Page
- Protected function showSucc ($ msg, $ skipUrl, $ skipPage, $ layout = null)
- {
- $ This-> title = "success prompt ";
- $ This-> msg = $ msg;
- $ This-> skipUrl = $ skipUrl;
- $ This-> skipPage = $ skipPage;
- $ This-> render ('success', $ layout );
- }
- // Display the link with permissions
- Protected function showPemissionLink ($ title, $ ct, $ ac, $ param = array (), $ wrap = '')
- {
- If ($ wrap ){
- $ Wrap_start = '<'. $ wrap. '> ';
- $ Wrap_end =' ';
- } Else {
- $ Wrap_start = $ wrap_end = '';
- }
- If ($ this-> isAllow ($ ct, $ ac ))
- Echo $ wrap_start, 'URL ($ ct, $ ac, $ param), '">', $ title,'', $ wrap_end;
- }
- // View resolution method
- 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 ('ymmd') = date ('ymmd', $ fmtime )){
- Include $ this-> staticFile;
- Exit;
- }
- }
- }
- }
- // Generate a url
- Protected function url ($ ct = '', $ ac ='', $ param = array (), $ module = '')
- {
- Return $ GLOBALS ['app']-> url ($ ct, $ ac, $ param, $ module );
- }
- // Url jump
- Protected function redirect ($ ct = '', $ ac ='', $ param = array ())
- {
- Header ('Location: '. $ this-> url ($ ct, $ ac, $ param ));
- Exit ();
- }
- // Url jump
- Protected function redirectUrl ($ url)
- {
- Header ('Location: '. $ url );
- Exit ();
- }
- // Obtain the back redirect url
- Protected function getBru ()
- {
- Return $ _ COOKIE [util: c ('bru _ cookie_name ')]? $ _ COOKIE [util: c ('bru _ cookie_name ')]: $ this-> url ();
- }
- // Whether it is an ajax request
- Protected function isAjax ()
- {
- If (isset ($ _ SERVER ['http _ X_REQUESTED_WITH ']) & $ _ SERVER ['http _ X_REQUESTED_WITH'] = 'xmlhttprequest ')
- Return true;
- Return false;
- }
- // Return a json Array
- 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], 'Enter it ', 0, 'gbk') = 0 )){
- Return '';
- } Else {
- Return util: sanitize ($ _ POST [$ name]);
- }
- }
- // REQUEST
- Protected function _ R ($ name)
- {
- Return isset ($ _ REQUEST [$ name])? Util: sanitize ($ _ REQUEST [$ name]): '';
- }
- }
|