Controller.class.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <?php
  2. /**
  3. * The controller object handles user actions.
  4. *
  5. * @author Ironpilot
  6. * @copyright Copywrite (c) 2011, STAPLE CODE
  7. *
  8. * This file is part of the STAPLE Framework.
  9. *
  10. * The STAPLE Framework is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by the
  12. * Free Software Foundation, either version 3 of the License, or (at your option)
  13. * any later version.
  14. *
  15. * The STAPLE Framework is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  18. * more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. abstract class Staple_Controller
  24. {
  25. protected $openMethods = array();
  26. protected $accessLevels = array();
  27. protected $open = false;
  28. /**
  29. *
  30. * The $view property holds the view object for the controller.
  31. * @var Staple_View
  32. */
  33. public $view;
  34. /**
  35. * Holds and instance of a Staple Layout object.
  36. * @var Staple_Layout
  37. */
  38. public $layout;
  39. /**
  40. *
  41. * Controller constructor creates an instance of Staple_View and saves it in the $view
  42. * property. It then calls the overridable method _start() for additional boot time
  43. * procedures.
  44. */
  45. public function __construct()
  46. {
  47. //Set default access levels
  48. $methods = get_class_methods(get_class($this));
  49. foreach($methods as $acMeth)
  50. {
  51. if(substr($acMeth, 0,1) != '_')
  52. {
  53. $this->accessLevels[$acMeth] = 1;
  54. }
  55. }
  56. //Create a view object
  57. $this->view = new Staple_View();
  58. //Assign the default layout to the controller, if specified in config.
  59. $settings = Staple_Config::get('layout');
  60. if(array_key_exists('default', $settings))
  61. {
  62. if($settings['default'] != '')
  63. {
  64. $this->_setLayout($settings['default']);
  65. $pageSettings = Staple_Config::get('page');
  66. if(array_key_exists('title', $pageSettings))
  67. {
  68. $this->layout->setTitle($pageSettings['title']);
  69. }
  70. }
  71. }
  72. }
  73. /**
  74. *
  75. * Enter description here ...
  76. */
  77. public function __wakeup()
  78. {
  79. $methods = get_class_methods(get_class($this));
  80. foreach($methods as $acMeth)
  81. {
  82. if(substr($acMeth, 0,1) != '_')
  83. {
  84. if(!array_key_exists($acMeth, $this->accessLevels))
  85. {
  86. $this->accessLevels[$acMeth] = 1;
  87. }
  88. }
  89. }
  90. //Erase the view and start from scratch
  91. $this->view = new Staple_View();
  92. //Reset the view inside the layout
  93. if($this->layout instanceof Staple_Layout)
  94. $this->layout->setView($this->view);
  95. }
  96. /**
  97. * Overridable and empty method allows for additional boot time procedures.
  98. * This method is called by the front controller.
  99. */
  100. public function _start()
  101. {
  102. }
  103. /**
  104. * The default action on any controller must be defined.
  105. */
  106. abstract public function index();
  107. /*----------------------------------------Auth Functions----------------------------------------*/
  108. /**
  109. * Returns a boolean true or false whether the method requires authentication
  110. * before being dispatched from the front controller.
  111. * @param string $method
  112. * @return bool
  113. */
  114. public function _auth($method)
  115. {
  116. $method = (string)$method;
  117. if(!ctype_alnum($method))
  118. {
  119. throw new Exception('Authentication Validation Error', Staple_Error::AUTH_ERROR);
  120. }
  121. else
  122. {
  123. if(method_exists($this,$method))
  124. {
  125. //Is the controller completely open?
  126. if($this->open === true)
  127. {
  128. return true;
  129. }
  130. elseif(array_search($method, $this->openMethods) !== FALSE) //Is the requested method open?
  131. {
  132. return true;
  133. }
  134. elseif(Staple_Auth::get()->isAuthed() && Staple_Auth::get()->getAuthLevel() >= $this->_authLevel($method)) //Does the authed user have the required access level?
  135. {
  136. return true;
  137. }
  138. }
  139. else
  140. {
  141. throw new Exception('Authentication Validation Error', Staple_Error::AUTH_ERROR);
  142. }
  143. }
  144. return false;
  145. }
  146. /**
  147. *
  148. * Returns the access level required for this method.
  149. * @param string | array $method
  150. * @throws Exception
  151. */
  152. public function _authLevel($method)
  153. {
  154. $method = (string)$method;
  155. if(!ctype_alnum($method))
  156. {
  157. throw new Exception('Authentication Validation Error: Invalid Method', Staple_Error::AUTH_ERROR);
  158. }
  159. else
  160. {
  161. if(method_exists($this,$method))
  162. {
  163. if(array_key_exists($method, $this->accessLevels) === true)
  164. {
  165. return (int)$this->accessLevels[$method];
  166. }
  167. else
  168. {
  169. //return default auth level if non assigned.
  170. return 1;
  171. //throw new Exception('Authentication Validation Error: No Auth Level', Staple_Error::AUTH_ERROR);
  172. }
  173. }
  174. else
  175. {
  176. throw new Exception('Authentication Validation Error: Method Not Found', Staple_Error::AUTH_ERROR);
  177. }
  178. }
  179. return 1;
  180. }
  181. /**
  182. *
  183. * Replaces the default permission level with the specified permission level. All method
  184. * specific access levels will be overwritten. This should be called in controller startup.
  185. * @param int $level
  186. */
  187. protected function _requiredControllerAccessLevel($level)
  188. {
  189. $methods = get_class_methods(get_class($this));
  190. foreach($methods as $acMeth)
  191. {
  192. if(substr($acMeth, 0,1) != '_')
  193. {
  194. $this->accessLevels[$acMeth] = (int)$level;
  195. }
  196. }
  197. $this->openMethods = array();
  198. }
  199. /**
  200. * Specifies the required access level for the specified action. This should be called
  201. * in the Controller startup.
  202. * @param string $for
  203. * @param int $level
  204. * @throws Exception
  205. */
  206. protected function _requiredActionAccessLevel($for,$level)
  207. {
  208. $level = (int)$level;
  209. $for = (string)$for;
  210. if(!ctype_alnum($for))
  211. {
  212. throw new Exception('Cannot change method permissions.', Staple_Error::AUTH_ERROR);
  213. }
  214. else
  215. {
  216. if(method_exists($this, $for))
  217. {
  218. if($level < 0)
  219. {
  220. throw new Exception('Cannot change method permissions.', Staple_Error::AUTH_ERROR);
  221. }
  222. else
  223. {
  224. if($level == 0)
  225. {
  226. $this->_openMethod($for);
  227. if(array_key_exists($for, $this->accessLevels))
  228. {
  229. unset($this->accessLevels[$for]);
  230. }
  231. }
  232. else
  233. {
  234. $this->accessLevels[$for] = $level;
  235. }
  236. }
  237. }
  238. else
  239. {
  240. throw new Exception('Cannot change method permissions on a non-existant method.', Staple_Error::AUTH_ERROR);
  241. }
  242. }
  243. }
  244. /**
  245. * Sent a string it allows one method to be accessed without authentication. When sent
  246. * an array, it allows all the values method names without authentication.
  247. * @param string | array $method
  248. * @throws Exception
  249. * @return bool
  250. */
  251. protected function _openMethod($method)
  252. {
  253. if(is_array($method))
  254. {
  255. foreach($method as $mName)
  256. {
  257. if(!ctype_alnum($mName))
  258. {
  259. throw new Exception('Cannot change method permissions.', Staple_Error::AUTH_ERROR);
  260. }
  261. else
  262. {
  263. if(array_search($mName, $this->openMethods) === false)
  264. {
  265. $this->openMethods[] = $mName;
  266. $this->accessLevels[$mName] = 0;
  267. }
  268. }
  269. }
  270. return true;
  271. }
  272. else
  273. {
  274. if(!ctype_alnum($method))
  275. {
  276. throw new Exception('Cannot change method permissions.', Staple_Error::AUTH_ERROR);
  277. }
  278. else
  279. {
  280. if(array_search($method, $this->openMethods) === false)
  281. {
  282. $this->openMethods[] = $method;
  283. $this->accessLevels[$method] = 0;
  284. return true;
  285. }
  286. }
  287. }
  288. return false;
  289. }
  290. /**
  291. * This function opens the entire controller to be accessed without authentication.
  292. */
  293. protected function _openAll()
  294. {
  295. $methods = get_class_methods(get_class($this));
  296. foreach($methods as $acMeth)
  297. {
  298. if(substr($acMeth, 0,1) != '_')
  299. {
  300. $this->accessLevels[$acMeth] = 0;
  301. }
  302. }
  303. $this->open = true;
  304. }
  305. /*----------------------------------------Layout Functions----------------------------------------*/
  306. /**
  307. *
  308. * Sets up a new layout object and associates the controllers view with the layout. Accepts a
  309. * string name for the layout to load.
  310. * @param string $layout
  311. */
  312. public function _setLayout($layout)
  313. {
  314. $this->layout = new Staple_Layout($layout);
  315. $this->layout->setView($this->view);
  316. }
  317. /**
  318. * Removes the layout object from the controller.
  319. */
  320. public function _removeLayout()
  321. {
  322. $this->layout = NULL;
  323. }
  324. /*----------------------------------------Helpers----------------------------------------*/
  325. /**
  326. *
  327. * This function accepts a routing string to redirect the application internally. A
  328. * redirect of this sort clears the output buffer and redraws the header, proceeding
  329. * as if the redirected controller/action was called directly.
  330. * @param mixed $to
  331. */
  332. protected function _redirect($to)
  333. {
  334. Staple_Main::get()->redirect($to);
  335. $this->view->noRender();
  336. }
  337. /**
  338. *
  339. * If an array is supplied, a link is created to a controller/action. If a string is
  340. * supplied, a file link is specified.
  341. * @param string | array $link
  342. * @param array $get
  343. */
  344. protected function _link($link,array $get = array())
  345. {
  346. return Staple_Link::get($link,$get);
  347. }
  348. /**
  349. * @see Staple_View::escape()
  350. * @param string $estring
  351. * @param boolean $strip
  352. */
  353. public static function _escape($estring, $strip = false)
  354. {
  355. return Staple_View::escape($estring,$strip);
  356. }
  357. }