PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/error.php

https://github.com/msadouni/cakephp2x
PHP | 435 lines | 223 code | 44 blank | 168 comment | 16 complexity | d06e350592e79cb2a4ed288665c4e90d MD5 | raw file
  1. <?php
  2. /**
  3. * Error handler
  4. *
  5. * Provides Error Capturing for Framework errors.
  6. *
  7. * PHP Version 5.x
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package cake
  18. * @subpackage cake.cake.libs
  19. * @since CakePHP(tm) v 0.10.5.1732
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. App::import('Controller', 'App');
  23. /**
  24. * Error Handling Controller
  25. *
  26. * Controller used by ErrorHandler to render error views.
  27. *
  28. * @package cake
  29. * @subpackage cake.cake.libs
  30. */
  31. class CakeErrorController extends AppController {
  32. public $name = 'CakeError';
  33. /**
  34. * Uses Property
  35. *
  36. * @var array
  37. */
  38. public $uses = array();
  39. /**
  40. * __construct
  41. *
  42. * @access public
  43. * @return void
  44. */
  45. public function __construct() {
  46. parent::__construct();
  47. $this->_set(Router::getPaths());
  48. $this->params = Router::getParams();
  49. $this->constructClasses();
  50. $this->Component->initialize($this);
  51. $this->_set(array('cacheAction' => false, 'viewPath' => 'errors'));
  52. }
  53. }
  54. /**
  55. * Error Handler.
  56. *
  57. * Captures and handles all cakeError() calls.
  58. * Displays helpful framework errors when debug > 1.
  59. * When debug < 1 cakeError() will render 404 or 500 errors.
  60. *
  61. * @package cake
  62. * @subpackage cake.cake.libs
  63. */
  64. class ErrorHandler extends Object {
  65. /**
  66. * Controller instance.
  67. *
  68. * @var Controller
  69. * @access public
  70. */
  71. public $controller = null;
  72. /**
  73. * Class constructor.
  74. *
  75. * @param string $method Method producing the error
  76. * @param array $messages Error messages
  77. */
  78. public function __construct($method, $messages) {
  79. App::import('Core', 'Sanitize');
  80. static $__previousError = null;
  81. if ($__previousError != array($method, $messages)) {
  82. $__previousError = array($method, $messages);
  83. $this->controller = new CakeErrorController();
  84. } else {
  85. $this->controller = new Controller();
  86. $this->controller->viewPath = 'errors';
  87. }
  88. $options = array('escape' => false);
  89. $messages = Sanitize::clean($messages, $options);
  90. if (!isset($messages[0])) {
  91. $messages = array($messages);
  92. }
  93. if (method_exists($this->controller, 'apperror')) {
  94. return $this->controller->appError($method, $messages);
  95. }
  96. if (!in_array(strtolower($method), array_map('strtolower', get_class_methods($this)))) {
  97. $method = 'error';
  98. }
  99. if ($method !== 'error') {
  100. if (Configure::read('debug') == 0) {
  101. $parentClass = get_parent_class($this);
  102. if (strtolower($parentClass) != 'errorhandler') {
  103. $method = 'error404';
  104. }
  105. $parentMethods = get_class_methods($parentClass);
  106. if (in_array($method, $parentMethods)) {
  107. $method = 'error404';
  108. }
  109. if (isset($code) && $code == 500) {
  110. $method = 'error500';
  111. }
  112. }
  113. }
  114. $this->dispatchMethod($method, $messages);
  115. $this->_stop();
  116. }
  117. /**
  118. * Displays an error page (e.g. 404 Not found).
  119. *
  120. * @param array $params Parameters for controller
  121. * @access public
  122. */
  123. public function error($params) {
  124. extract($params, EXTR_OVERWRITE);
  125. $this->controller->set(array(
  126. 'code' => $code,
  127. 'name' => $name,
  128. 'message' => $message,
  129. 'title' => $code . ' ' . $name
  130. ));
  131. $this->_outputMessage('error404');
  132. }
  133. /**
  134. * Convenience method to display a 404 page.
  135. *
  136. * @param array $params Parameters for controller
  137. * @access public
  138. */
  139. public function error404($params) {
  140. extract($params, EXTR_OVERWRITE);
  141. if (!isset($url)) {
  142. $url = $this->controller->here;
  143. }
  144. $url = Router::normalize($url);
  145. header("HTTP/1.0 404 Not Found");
  146. $this->controller->set(array(
  147. 'code' => '404',
  148. 'name' => __('Not Found', true),
  149. 'message' => h($url),
  150. 'base' => $this->controller->base
  151. ));
  152. $this->_outputMessage('error404');
  153. }
  154. /**
  155. * Renders the Missing Controller web page.
  156. *
  157. * @param array $params Parameters for controller
  158. * @access public
  159. */
  160. public function missingController($params) {
  161. extract($params, EXTR_OVERWRITE);
  162. $controllerName = str_replace('Controller', '', $className);
  163. $this->controller->set(array(
  164. 'controller' => $className,
  165. 'controllerName' => $controllerName,
  166. 'title' => __('Missing Controller', true)
  167. ));
  168. $this->_outputMessage('missingController');
  169. }
  170. /**
  171. * Renders the Missing Action web page.
  172. *
  173. * @param array $params Parameters for controller
  174. * @access public
  175. */
  176. public function missingAction($params) {
  177. extract($params, EXTR_OVERWRITE);
  178. $controllerName = str_replace('Controller', '', $className);
  179. $this->controller->set(array(
  180. 'controller' => $className,
  181. 'controllerName' => $controllerName,
  182. 'action' => $action,
  183. 'title' => __('Missing Method in Controller', true)
  184. ));
  185. $this->_outputMessage('missingAction');
  186. }
  187. /**
  188. * Renders the Private Action web page.
  189. *
  190. * @param array $params Parameters for controller
  191. * @access public
  192. */
  193. public function privateAction($params) {
  194. extract($params, EXTR_OVERWRITE);
  195. $this->controller->set(array(
  196. 'controller' => $className,
  197. 'action' => $action,
  198. 'title' => __('Trying to access private method in class', true)
  199. ));
  200. $this->_outputMessage('privateAction');
  201. }
  202. /**
  203. * Renders the Missing Table web page.
  204. *
  205. * @param array $params Parameters for controller
  206. * @access public
  207. */
  208. public function missingTable($params) {
  209. extract($params, EXTR_OVERWRITE);
  210. $this->controller->set(array(
  211. 'model' => $className,
  212. 'table' => $table,
  213. 'title' => __('Missing Database Table', true)
  214. ));
  215. $this->_outputMessage('missingTable');
  216. }
  217. /**
  218. * Renders the Missing Database web page.
  219. *
  220. * @param array $params Parameters for controller
  221. * @access public
  222. */
  223. public function missingDatabase($params = array()) {
  224. $this->controller->set(array(
  225. 'title' => __('Scaffold Missing Database Connection', true)
  226. ));
  227. $this->_outputMessage('missingScaffolddb');
  228. }
  229. /**
  230. * Renders the Missing View web page.
  231. *
  232. * @param array $params Parameters for controller
  233. * @access public
  234. */
  235. public function missingView($params) {
  236. extract($params, EXTR_OVERWRITE);
  237. $this->controller->set(array(
  238. 'controller' => $className,
  239. 'action' => $action,
  240. 'file' => $file,
  241. 'title' => __('Missing View', true)
  242. ));
  243. $this->_outputMessage('missingView');
  244. }
  245. /**
  246. * Renders the Missing Layout web page.
  247. *
  248. * @param array $params Parameters for controller
  249. * @access public
  250. */
  251. public function missingLayout($params) {
  252. extract($params, EXTR_OVERWRITE);
  253. $this->controller->layout = 'default';
  254. $this->controller->set(array(
  255. 'file' => $file,
  256. 'title' => __('Missing Layout', true)
  257. ));
  258. $this->_outputMessage('missingLayout');
  259. }
  260. /**
  261. * Renders the Database Connection web page.
  262. *
  263. * @param array $params Parameters for controller
  264. * @access public
  265. */
  266. public function missingConnection($params) {
  267. extract($params, EXTR_OVERWRITE);
  268. $this->controller->set(array(
  269. 'model' => $className,
  270. 'title' => __('Missing Database Connection', true)
  271. ));
  272. $this->_outputMessage('missingConnection');
  273. }
  274. /**
  275. * Renders the Missing Helper file web page.
  276. *
  277. * @param array $params Parameters for controller
  278. * @access public
  279. */
  280. public function missingHelperFile($params) {
  281. extract($params, EXTR_OVERWRITE);
  282. $this->controller->set(array(
  283. 'helperClass' => Inflector::camelize($helper) . "Helper",
  284. 'file' => $file,
  285. 'title' => __('Missing Helper File', true)
  286. ));
  287. $this->_outputMessage('missingHelperFile');
  288. }
  289. /**
  290. * Renders the Missing Helper class web page.
  291. *
  292. * @param array $params Parameters for controller
  293. * @access public
  294. */
  295. public function missingHelperClass($params) {
  296. extract($params, EXTR_OVERWRITE);
  297. $this->controller->set(array(
  298. 'helperClass' => Inflector::camelize($helper) . "Helper",
  299. 'file' => $file,
  300. 'title' => __('Missing Helper Class', true)
  301. ));
  302. $this->_outputMessage('missingHelperClass');
  303. }
  304. /**
  305. * Renders the Missing Behavior file web page.
  306. *
  307. * @param array $params Parameters for controller
  308. * @access public
  309. */
  310. function missingBehaviorFile($params) {
  311. extract($params, EXTR_OVERWRITE);
  312. $this->controller->set(array(
  313. 'behaviorClass' => Inflector::camelize($behavior) . "Behavior",
  314. 'file' => $file,
  315. 'title' => __('Missing Behavior File', true)
  316. ));
  317. $this->_outputMessage('missingBehaviorFile');
  318. }
  319. /**
  320. * Renders the Missing Behavior class web page.
  321. *
  322. * @param array $params Parameters for controller
  323. * @access public
  324. */
  325. function missingBehaviorClass($params) {
  326. extract($params, EXTR_OVERWRITE);
  327. $this->controller->set(array(
  328. 'behaviorClass' => Inflector::camelize($behavior) . "Behavior",
  329. 'file' => $file,
  330. 'title' => __('Missing Behavior Class', true)
  331. ));
  332. $this->_outputMessage('missingBehaviorClass');
  333. }
  334. /**
  335. * Renders the Missing Component file web page.
  336. *
  337. * @param array $params Parameters for controller
  338. * @access public
  339. */
  340. public function missingComponentFile($params) {
  341. extract($params, EXTR_OVERWRITE);
  342. $this->controller->set(array(
  343. 'controller' => $className,
  344. 'component' => $component,
  345. 'file' => $file,
  346. 'title' => __('Missing Component File', true)
  347. ));
  348. $this->_outputMessage('missingComponentFile');
  349. }
  350. /**
  351. * Renders the Missing Component class web page.
  352. *
  353. * @param array $params Parameters for controller
  354. * @access public
  355. */
  356. public function missingComponentClass($params) {
  357. extract($params, EXTR_OVERWRITE);
  358. $this->controller->set(array(
  359. 'controller' => $className,
  360. 'component' => $component,
  361. 'file' => $file,
  362. 'title' => __('Missing Component Class', true)
  363. ));
  364. $this->_outputMessage('missingComponentClass');
  365. }
  366. /**
  367. * Renders the Missing Model class web page.
  368. *
  369. * @param unknown_type $params Parameters for controller
  370. * @access public
  371. */
  372. public function missingModel($params) {
  373. extract($params, EXTR_OVERWRITE);
  374. $this->controller->set(array(
  375. 'model' => $className,
  376. 'title' => __('Missing Model', true)
  377. ));
  378. $this->_outputMessage('missingModel');
  379. }
  380. /**
  381. * Output message
  382. *
  383. * @access protected
  384. */
  385. protected function _outputMessage($template) {
  386. $this->controller->render($template);
  387. $this->controller->afterFilter();
  388. echo $this->controller->output;
  389. }
  390. }
  391. ?>