Base for a static organization website

ExceptionRenderer.php 9.6KB

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