<commit_before><?php
/**
 *
 * PHP 5
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the below copyright notice.
 *
 * @copyright     Copyright 2013, <NAME> (http://frankfoerster.com)
 * @link          http://github.com/frankfoerster/wasabi
 * @package       Wasabi
 * @subpackage    Wasabi.Plugin.Core.Lib.Error
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('ExceptionRenderer', 'Error');

class CoreExceptionRenderer extends ExceptionRenderer {

	/**
	 * @param Exception $exception
	 * @return Controller
	 */
	protected function _getController($exception) {
		if (!$request = Router::getRequest(true)) {
			$request = new CakeRequest();
		}
		$response = new CakeResponse();

		if (method_exists($exception, 'responseHeader')) {
			$response->header($exception->responseHeader());
		}

		try {
			$exceptionType = get_class($exception);
			switch ($exceptionType) {
				case 'MissingControllerException':
					App::uses('BackendErrorController', 'Core.Controller');
					$controller = new BackendErrorController($request, $response);
					break;
				default:
					App::uses('FrontendErrorController', 'Core.Controller');
					$controller = new FrontendErrorController($request, $response);
			}
			$controller->constructClasses();
			$controller->startupProcess();
		} catch (Exception $e) {
			if (!empty($controller) && $controller->Components->enabled('RequestHandler')) {
				$controller->RequestHandler->startup($controller);
			}
		}
		if (empty($controller)) {
			$controller = new Controller($request, $response);
			$controller->viewPath = 'Errors';
		}
		return $controller;
	}

	protected function _outputMessage($template) {
		try {
			$this->controller->plugin = 'Core';
			$this->controller->render($template, 'Core.default');
			$this->controller->afterFilter();
			$this->controller->response->send();
		} catch (MissingViewException $e) {
			$attributes = $e->getAttributes();
			if (isset($attributes['file']) && strpos($attributes['file'], 'error500') !== false) {
				$this->_outputMessageSafe('error500');
			} else {
				$this->_outputMessage('error500');
			}
		} catch (Exception $e) {
			var_dump($e);
			$this->_outputMessageSafe('error500');
		}
	}

	/**
	 * A safer way to render error messages, replaces all helpers, with basics
	 * and doesn't call component methods.
	 *
	 * @param string $template The template to render
	 * @return void
	 */
	protected function _outputMessageSafe($template) {
		$this->controller->layoutPath = null;
		$this->controller->subDir = null;
		$this->controller->viewPath = 'BackendError/';

		$view = new View($this->controller);
		$this->controller->response->body($view->render($template, 'Core.default'));
		$this->controller->response->type('html');
		$this->controller->response->send();<commit_msg>better differentiate between backend and frontend errors<commit_after><?php
/**
 *
 * PHP 5
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the below copyright notice.
 *
 * @copyright     Copyright 2013, <NAME> (http://frankfoerster.com)
 * @link          http://github.com/frankfoerster/wasabi
 * @package       Wasabi
 * @subpackage    Wasabi.Plugin.Core.Lib.Error
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('ExceptionRenderer', 'Error');

class CoreExceptionRenderer extends ExceptionRenderer {

	/**
	 * @param Exception $exception
	 * @return Controller
	 */
	protected function _getController($exception) {
		if (!$request = Router::getRequest(true)) {
			$request = new CakeRequest();
		}
		$response = new CakeResponse();

		if (method_exists($exception, 'responseHeader')) {
			$response->header($exception->responseHeader());
		}

		try {
			if (strpos($request->url, Configure::read('Wasabi.backend_prefix') . '/') === 0 || (
				strpos($request->url, Configure::read('Wasabi.backend_prefix')) === 0 &&
				strlen($request->url) == strlen(Configure::read('Wasabi.backend_prefix'))
			)) {
				App::uses('BackendErrorController', 'Core.Controller');
				$controller = new BackendErrorController($request, $response);
			} else {
				App::uses('FrontendErrorController', 'Core.Controller');
				$controller = new FrontendErrorController($request, $response);
			}
			$controller->constructClasses();
			$controller->startupProcess();
		} catch (Exception $e) {
			if (!empty($controller) && $controller->Components->enabled('RequestHandler')) {
				$controller->RequestHandler->startup($controller);
			}
		}
		if (empty($controller)) {
			$controller = new Controller($request, $response);
			$controller->viewPath = 'Errors';
		}
		return $controller;
	}

	protected function _outputMessage($template) {
		try {
			if ($this->controller->name === 'BackendError') {
				$this->controller->plugin = 'Core';
				$this->controller->set(array(
					'title_for_layout' => 'Error'
				));
				$this->controller->render($template, 'Core.default');
			} else {
				$this->controller->render($template);
			}
			$this->controller->afterFilter();
			$this->controller->response->send();
		} catch (MissingViewException $e) {
			$attributes = $e->getAttributes();
			if (isset($attributes['file']) && strpos($attributes['file'], 'error500') !== false) {
				$this->_outputMessageSafe('error500');
			} else {
				$this->_outputMessage('error500');
			}
		} catch (Exception $e) {
			$this->_outputMessageSafe('error500');
		}
	}

	/**
	 * A safer way to render error messages, replaces all helpers, with basics
	 * and doesn't call component methods.
	 *
	 * @param string $template The template to render
	 * @return void
	 */
	protected function _outputMessageSafe($template) {
		$this->controller->layoutPath = null;
		$this->controller->subDir = null;
		$this->controller->plugin = null;
		if ($this->controller->name === 'BackendError') {
			$this->controller->plugin = 'Core';
			$this->controller->viewPath = 'BackendErrors';
			$this->controller->set(array(
				'title_for_layout' => 'Error'
			));
		} else {
			$this->controller->viewPath = 'Errors';
		}

		$view = new View($this->controller);
		$this->controller->response->body($view->render($template));
		$this->controller->response->type('html');
		$this->controller->response->send();
{'fraction_non_alphanumeric': 0.17006045179764556, 'fraction_numerical': 0.005727012408526885, 'mean_word_length': 4.269907795473596, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 104, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6880466', 'n_tokens_mistral': 2094, 'n_tokens_neox': 1805, 'n_words': 364}