PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Error/ExceptionRenderer.php

https://bitbucket.org/ManiAdil/jardinorient
PHP | 308 lines | 159 code | 23 blank | 126 comment | 28 complexity | a780cf0abe0f6296e7fc449526fa6d16 MD5 | raw file
  1. <?php
  2. /**
  3. * Exception Renderer
  4. *
  5. * Provides Exception rendering features. Which allow exceptions to be rendered
  6. * as HTML pages.
  7. *
  8. * PHP 5
  9. *
  10. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  11. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. *
  13. * Licensed under The MIT License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link http://cakephp.org CakePHP(tm) Project
  18. * @package Cake.Error
  19. * @since CakePHP(tm) v 2.0
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. App::uses('Sanitize', 'Utility');
  23. App::uses('Router', 'Routing');
  24. App::uses('CakeResponse', 'Network');
  25. App::uses('Controller', 'Controller');
  26. /**
  27. * Exception Renderer.
  28. *
  29. * Captures and handles all unhandled exceptions. Displays helpful framework errors when debug > 1.
  30. * When debug < 1 a CakeException will render 404 or 500 errors. If an uncaught exception is thrown
  31. * and it is a type that ExceptionHandler does not know about it will be treated as a 500 error.
  32. *
  33. * ### Implementing application specific exception rendering
  34. *
  35. * You can implement application specific exception handling in one of a few ways:
  36. *
  37. * - Create a AppController::appError();
  38. * - Create a subclass of ExceptionRenderer and configure it to be the `Exception.renderer`
  39. *
  40. * #### Using AppController::appError();
  41. *
  42. * This controller method is called instead of the default exception handling. It receives the
  43. * thrown exception as its only argument. You should implement your error handling in that method.
  44. *
  45. * #### Using a subclass of ExceptionRenderer
  46. *
  47. * Using a subclass of ExceptionRenderer gives you full control over how Exceptions are rendered, you
  48. * can configure your class in your core.php, with `Configure::write('Exception.renderer', 'MyClass');`
  49. * You should place any custom exception renderers in `app/Lib/Error`.
  50. *
  51. * @package Cake.Error
  52. */
  53. class ExceptionRenderer {
  54. /**
  55. * Controller instance.
  56. *
  57. * @var Controller
  58. */
  59. public $controller = null;
  60. /**
  61. * template to render for CakeException
  62. *
  63. * @var string
  64. */
  65. public $template = '';
  66. /**
  67. * The method corresponding to the Exception this object is for.
  68. *
  69. * @var string
  70. */
  71. public $method = '';
  72. /**
  73. * The exception being handled.
  74. *
  75. * @var Exception
  76. */
  77. public $error = null;
  78. /**
  79. * Creates the controller to perform rendering on the error response.
  80. * If the error is a CakeException it will be converted to either a 400 or a 500
  81. * code error depending on the code used to construct the error.
  82. *
  83. * @param Exception $exception Exception
  84. * @return mixed Return void or value returned by controller's `appError()` function
  85. */
  86. public function __construct(Exception $exception) {
  87. $this->controller = $this->_getController($exception);
  88. if (method_exists($this->controller, 'apperror')) {
  89. return $this->controller->appError($exception);
  90. }
  91. $method = $template = Inflector::variable(str_replace('Exception', '', get_class($exception)));
  92. $code = $exception->getCode();
  93. $methodExists = method_exists($this, $method);
  94. if ($exception instanceof CakeException && !$methodExists) {
  95. $method = '_cakeError';
  96. if (empty($template) || $template == 'internalError') {
  97. $template = 'error500';
  98. }
  99. } elseif ($exception instanceof PDOException) {
  100. $method = 'pdoError';
  101. $template = 'pdo_error';
  102. $code = 500;
  103. } elseif (!$methodExists) {
  104. $method = 'error500';
  105. if ($code >= 400 && $code < 500) {
  106. $method = 'error400';
  107. }
  108. }
  109. $isNotDebug = !Configure::read('debug');
  110. if ($isNotDebug && $method == '_cakeError') {
  111. $method = 'error400';
  112. }
  113. if ($isNotDebug && $code == 500) {
  114. $method = 'error500';
  115. }
  116. $this->template = $template;
  117. $this->method = $method;
  118. $this->error = $exception;
  119. }
  120. /**
  121. * Get the controller instance to handle the exception.
  122. * Override this method in subclasses to customize the controller used.
  123. * This method returns the built in `CakeErrorController` normally, or if an error is repeated
  124. * a bare controller will be used.
  125. *
  126. * @param Exception $exception The exception to get a controller for.
  127. * @return Controller
  128. */
  129. protected function _getController($exception) {
  130. App::uses('AppController', 'Controller');
  131. App::uses('CakeErrorController', 'Controller');
  132. if (!$request = Router::getRequest(true)) {
  133. $request = new CakeRequest();
  134. }
  135. $response = new CakeResponse();
  136. if (method_exists($exception, 'responseHeader')) {
  137. $response->header($exception->responseHeader());
  138. }
  139. try {
  140. $controller = new CakeErrorController($request, $response);
  141. $controller->startupProcess();
  142. } catch (Exception $e) {
  143. if (!empty($controller) && $controller->Components->enabled('RequestHandler')) {
  144. $controller->RequestHandler->startup($controller);
  145. }
  146. }
  147. if (empty($controller)) {
  148. $controller = new Controller($request, $response);
  149. $controller->viewPath = 'Errors';
  150. }
  151. return $controller;
  152. }
  153. /**
  154. * Renders the response for the exception.
  155. *
  156. * @return void
  157. */
  158. public function render() {
  159. if ($this->method) {
  160. call_user_func_array(array($this, $this->method), array($this->error));
  161. }
  162. }
  163. /**
  164. * Generic handler for the internal framework errors CakePHP can generate.
  165. *
  166. * @param CakeException $error
  167. * @return void
  168. */
  169. protected function _cakeError(CakeException $error) {
  170. $url = $this->controller->request->here();
  171. $code = ($error->getCode() >= 400 && $error->getCode() < 506) ? $error->getCode() : 500;
  172. $this->controller->response->statusCode($code);
  173. $this->controller->set(array(
  174. 'code' => $code,
  175. 'url' => h($url),
  176. 'name' => h($error->getMessage()),
  177. 'error' => $error,
  178. '_serialize' => array('code', 'url', 'name')
  179. ));
  180. $this->controller->set($error->getAttributes());
  181. $this->_outputMessage($this->template);
  182. }
  183. /**
  184. * Convenience method to display a 400 series page.
  185. *
  186. * @param Exception $error
  187. * @return void
  188. */
  189. public function error400($error) {
  190. $message = $error->getMessage();
  191. if (!Configure::read('debug') && $error instanceof CakeException) {
  192. $message = __d('cake', 'Not Found');
  193. }
  194. $url = $this->controller->request->here();
  195. $this->controller->response->statusCode($error->getCode());
  196. $this->controller->set(array(
  197. 'name' => h($message),
  198. 'url' => h($url),
  199. 'error' => $error,
  200. '_serialize' => array('name', 'url')
  201. ));
  202. $this->_outputMessage('error400');
  203. }
  204. /**
  205. * Convenience method to display a 500 page.
  206. *
  207. * @param Exception $error
  208. * @return void
  209. */
  210. public function error500($error) {
  211. $message = $error->getMessage();
  212. if (!Configure::read('debug')) {
  213. $message = __d('cake', 'An Internal Error Has Occurred.');
  214. }
  215. $url = $this->controller->request->here();
  216. $code = ($error->getCode() > 500 && $error->getCode() < 506) ? $error->getCode() : 500;
  217. $this->controller->response->statusCode($code);
  218. $this->controller->set(array(
  219. 'name' => h($message),
  220. 'message' => h($url),
  221. 'error' => $error,
  222. '_serialize' => array('name', 'message')
  223. ));
  224. $this->_outputMessage('error500');
  225. }
  226. /**
  227. * Convenience method to display a PDOException.
  228. *
  229. * @param PDOException $error
  230. * @return void
  231. */
  232. public function pdoError(PDOException $error) {
  233. $url = $this->controller->request->here();
  234. $code = 500;
  235. $this->controller->response->statusCode($code);
  236. $this->controller->set(array(
  237. 'code' => $code,
  238. 'url' => h($url),
  239. 'name' => h($error->getMessage()),
  240. 'error' => $error,
  241. '_serialize' => array('code', 'url', 'name', 'error')
  242. ));
  243. $this->_outputMessage($this->template);
  244. }
  245. /**
  246. * Generate the response using the controller object.
  247. *
  248. * @param string $template The template to render.
  249. * @return void
  250. */
  251. protected function _outputMessage($template) {
  252. try {
  253. $this->controller->render($template);
  254. $this->controller->afterFilter();
  255. $this->controller->response->send();
  256. } catch (MissingViewException $e) {
  257. $attributes = $e->getAttributes();
  258. if (isset($attributes['file']) && strpos($attributes['file'], 'error500') !== false) {
  259. $this->_outputMessageSafe('error500');
  260. } else {
  261. $this->_outputMessage('error500');
  262. }
  263. } catch (Exception $e) {
  264. $this->_outputMessageSafe('error500');
  265. }
  266. }
  267. /**
  268. * A safer way to render error messages, replaces all helpers, with basics
  269. * and doesn't call component methods.
  270. *
  271. * @param string $template The template to render
  272. * @return void
  273. */
  274. protected function _outputMessageSafe($template) {
  275. $this->controller->layoutPath = null;
  276. $this->controller->subDir = null;
  277. $this->controller->viewPath = 'Errors/';
  278. $this->controller->layout = 'error';
  279. $this->controller->helpers = array('Form', 'Html', 'Session');
  280. $view = new View($this->controller);
  281. $this->controller->response->body($view->render($template, 'error'));
  282. $this->controller->response->type('html');
  283. $this->controller->response->send();
  284. }
  285. }