Controller base class

Source: Internet
Author: User
Controller base class
Very simple and practical controller base class

  1. /**
  2. * @ Desc controller base class
  3. * @ Date 2013-05-06
  4. * @ Author liudesheng
  5. */
  6. Defined ('sys _ path') | die ('Access invalid ');
  7. Class controller
  8. {
  9. // Current Controller
  10. Protected $ _ controller;
  11. // Current action method
  12. Protected $ _ action;
  13. // Permission array
  14. Protected $ _ permissions;
  15. // Template file
  16. Private $ _ layout = 'layout ';
  17. // Constructor
  18. Function _ construct ($ controller, $ action)
  19. {
  20. If ('exception '! = $ Controller ){
  21. $ This-> _ controller = $ controller;
  22. $ This-> _ action = $ action;
  23. // Logon check and access permission control. The logon page does not need to be verified.
  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 {// exception handling
  31. $ This-> exception ($ action );
  32. }
  33. }
  34. // Initialization method for inherited operations
  35. Protected function init (){}
  36. // Exception handling method
  37. Private function exception ($ msg)
  38. {
  39. $ This-> showErr ($ msg, $ layout );
  40. }
  41. // Verify logon
  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. $ This-> redirect ('index', 'login ');
  51. }
  52. }
  53. }
  54. // Determine whether to log on
  55. Protected final function isLogin ()
  56. {
  57. $ Auth = isset ($ _ COOKIE ['auth'])? $ _ COOKIE ['auth']: '';
  58. $ IsLogin = false;
  59. If ($ auth ){
  60. $ Info = trim(file_get_contents('check.txt '));
  61. If (strcmp ($ auth, md5 ('Steve '. $ info. util: c ('login _ auth_suffix') = 0 ){
  62. $ IsLogin = true;
  63. }
  64. }
  65. Return $ isLogin;
  66. }
  67. // Verify the permission
  68. Private function privilege ()
  69. {
  70. $ This-> getPermissions ();
  71. If (! $ This-> isAllow ()){
  72. If ($ this-> isAjax ()){
  73. Header ('http/1.1 403 Forbidden ');
  74. Header ("Error-Json: {code: 'access '}");
  75. Exit ();
  76. } Else {
  77. $ This-> showErr ('sorry, you do not have this authorization ');
  78. }
  79. }
  80. }
  81. // Obtain permission information
  82. Protected final function getPermissions ()
  83. {
  84. $ Privilege = $ this-> admin ['privilege'];
  85. $ Permissions_priv = util: c ('permissions', $ privilege );
  86. If (! Isset ($ permissions_priv ['city']) {
  87. $ This-> cityPriv = 'all'; // to simplify list query, you may add all city permissions in the future.
  88. } Else {
  89. Unset ($ permissions_priv ['city']);
  90. }
  91. Foreach ($ permissions ['common'] as $ ct => $ ac ){
  92. If (isset ($ permissions_priv [$ ct]) & 'all' ==$ permissions_priv [$ ct])
  93. Continue;
  94. If ('all' = $ ac)
  95. $ Permissions_priv [$ ct] = 'all ';
  96. Else // in this case, it must be an array, which saves resources and makes no judgment.
  97. $ Permissions_priv [$ ct] = isset ($ permissions_priv [$ ct])? Array_merge ($ permissions_priv [$ ct], $ ac): $ ac;
  98. }
  99. $ This-> _ permissions = $ permissions_priv;
  100. }
  101. // Determine whether you have permissions based on the permission type
  102. Protected final function isAllow ($ controller = '', $ action = '')
  103. {
  104. If (! Isset ($ this-> _ permissions ))
  105. $ This-> getPermissions ();
  106. $ Allow = false;
  107. $ Ct = $ controller? $ Controller: $ this-> _ controller;
  108. $ Ac = $ action? $ Action: $ this-> _ action;
  109. $ Permission_action = $ this-> _ permissions [$ ct];
  110. If ($ permission_action & ('all' = $ permission_action | in_array ($ ac, $ permission_action) | 'any' = $ action ))
  111. $ Allow = true;
  112. Return $ allow;
  113. }
  114. // Error message page
  115. Protected function showErr ($ errMsg, $ layout = null)
  116. {
  117. $ This-> title = "error prompt ";
  118. $ This-> errMsg = $ errMsg;
  119. $ This-> render ('error', $ layout );
  120. }
  121. // Success Information Page
  122. Protected function showSucc ($ msg, $ skipUrl, $ skipPage, $ layout = null)
  123. {
  124. $ This-> title = "success prompt ";
  125. $ This-> msg = $ msg;
  126. $ This-> skipUrl = $ skipUrl;
  127. $ This-> skipPage = $ skipPage;
  128. $ This-> render ('success', $ layout );
  129. }
  130. // Display the link with permissions
  131. Protected function showPemissionLink ($ title, $ ct, $ ac, $ param = array (), $ wrap = '')
  132. {
  133. If ($ wrap ){
  134. $ Wrap_start = '<'. $ wrap. '> ';
  135. $ Wrap_end =' ';
  136. } Else {
  137. $ Wrap_start = $ wrap_end = '';
  138. }
  139. If ($ this-> isAllow ($ ct, $ ac ))
  140. Echo $ wrap_start, 'URL ($ ct, $ ac, $ param), '">', $ title,'', $ wrap_end;
  141. }
  142. // View resolution method
  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. Protected function showHtml ($ html, $ expire = 3600, $ path = '')
  157. {
  158. Empty ($ path) & $ path = ROOT_PATH;
  159. $ This-> staticFile = sprintf('{s}s.html ', $ path, $ html );
  160. $ Mkhtml = intval ($ this-> _ G ('mkhtml '));
  161. If (! $ Mkhtml ){
  162. If (file_exists ($ this-> staticFile )){
  163. $ Fmtime = filemtime ($ this-> staticFile );
  164. If (time ()-$ fmtime <$ expire & date ('ymmd') = date ('ymmd', $ fmtime )){
  165. Include $ this-> staticFile;
  166. Exit;
  167. }
  168. }
  169. }
  170. }
  171. // Generate a url
  172. Protected function url ($ ct = '', $ ac ='', $ param = array (), $ module = '')
  173. {
  174. Return $ GLOBALS ['app']-> url ($ ct, $ ac, $ param, $ module );
  175. }
  176. // Url jump
  177. Protected function redirect ($ ct = '', $ ac ='', $ param = array ())
  178. {
  179. Header ('Location: '. $ this-> url ($ ct, $ ac, $ param ));
  180. Exit ();
  181. }
  182. // Url jump
  183. Protected function redirectUrl ($ url)
  184. {
  185. Header ('Location: '. $ url );
  186. Exit ();
  187. }
  188. // Obtain the back redirect url
  189. Protected function getBru ()
  190. {
  191. Return $ _ COOKIE [util: c ('bru _ cookie_name ')]? $ _ COOKIE [util: c ('bru _ cookie_name ')]: $ this-> url ();
  192. }
  193. // Whether it is an ajax request
  194. Protected function isAjax ()
  195. {
  196. If (isset ($ _ SERVER ['http _ X_REQUESTED_WITH ']) & $ _ SERVER ['http _ X_REQUESTED_WITH'] = 'xmlhttprequest ')
  197. Return true;
  198. Return false;
  199. }
  200. // Return a json Array
  201. Protected function returnJson ($ data)
  202. {
  203. Echo json_encode ($ data );
  204. Exit ();
  205. }
  206. // GET
  207. Protected function _ G ($ name)
  208. {
  209. Return isset ($ _ GET [$ name])? Util: sanitize ($ _ GET [$ name]): '';
  210. }
  211. // POST
  212. Protected function _ P ($ name)
  213. {
  214. If (! Isset ($ _ POST [$ name]) | (is_string ($ _ POST [$ name]) & mb_strpos ($ _ POST [$ name], 'Enter it ', 0, 'gbk') = 0 )){
  215. Return '';
  216. } Else {
  217. Return util: sanitize ($ _ POST [$ name]);
  218. }
  219. }
  220. // REQUEST
  221. Protected function _ R ($ name)
  222. {
  223. Return isset ($ _ REQUEST [$ name])? Util: sanitize ($ _ REQUEST [$ name]): '';
  224. }
  225. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.