ErrorHandler.php 8.24 KB
Newer Older
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

Qiang Xue committed
8
namespace yii\base;
Qiang Xue committed
9

10 11
use Yii;

Qiang Xue committed
12
/**
Qiang Xue committed
13
 * ErrorHandler handles uncaught PHP errors and exceptions.
Qiang Xue committed
14
 *
Qiang Xue committed
15 16 17
 * ErrorHandler displays these errors using appropriate views based on the
 * nature of the errors and the mode the application runs at.
 *
Qiang Xue committed
18
 * @author Qiang Xue <qiang.xue@gmail.com>
resurtm committed
19
 * @author Timur Ruziev <resurtm@gmail.com>
Qiang Xue committed
20
 * @since 2.0
Qiang Xue committed
21
 */
22
class ErrorHandler extends Component
Qiang Xue committed
23 24 25 26
{
	/**
	 * @var integer maximum number of source code lines to be displayed. Defaults to 25.
	 */
Qiang Xue committed
27
	public $maxSourceLines = 25;
Qiang Xue committed
28 29 30 31 32 33 34
	/**
	 * @var integer maximum number of trace source code lines to be displayed. Defaults to 10.
	 */
	public $maxTraceSourceLines = 10;
	/**
	 * @var boolean whether to discard any existing page output before error display. Defaults to true.
	 */
Qiang Xue committed
35
	public $discardExistingOutput = true;
Qiang Xue committed
36
	/**
37 38 39 40
	 * @var string the route (e.g. 'site/error') to the controller action that will be used
	 * to display external errors. Inside the action, it can retrieve the error information
	 * by Yii::$app->errorHandler->error. This property defaults to null, meaning ErrorHandler
	 * will handle the error display.
Qiang Xue committed
41 42
	 */
	public $errorAction;
Qiang Xue committed
43
	/**
44
	 * @var string the path of the view file for rendering exceptions and errors.
Qiang Xue committed
45
	 */
resurtm committed
46 47 48 49 50
	public $mainView = '@yii/views/errorHandler/main.php';
	/**
	 * @var string the path of the view file for rendering exceptions and errors call stack element.
	 */
	public $callStackItemView = '@yii/views/errorHandler/callStackItem.php';
51 52 53 54
	/**
	 * @var string the path of the view file for rendering previous exceptions.
	 */
	public $previousExceptionView = '@yii/views/errorHandler/previousException.php';
Qiang Xue committed
55
	/**
56
	 * @var \Exception the exception that is being handled currently.
Qiang Xue committed
57
	 */
Qiang Xue committed
58
	public $exception;
Qiang Xue committed
59

Qiang Xue committed
60

Qiang Xue committed
61
	/**
62 63
	 * Handles exception.
	 * @param \Exception $exception to be handled.
Qiang Xue committed
64
	 */
Qiang Xue committed
65
	public function handle($exception)
Qiang Xue committed
66
	{
Qiang Xue committed
67
		$this->exception = $exception;
Qiang Xue committed
68 69 70
		if ($this->discardExistingOutput) {
			$this->clearOutput();
		}
Qiang Xue committed
71
		$this->renderException($exception);
Qiang Xue committed
72 73
	}

Alexander Makarov committed
74
	/**
75 76
	 * Renders exception.
	 * @param \Exception $exception to be handled.
Alexander Makarov committed
77
	 */
Qiang Xue committed
78
	protected function renderException($exception)
Qiang Xue committed
79
	{
Qiang Xue committed
80
		if ($this->errorAction !== null) {
81 82 83 84
			Yii::$app->runAction($this->errorAction);
		} elseif (!(Yii::$app instanceof \yii\web\Application)) {
			Yii::$app->renderException($exception);
		} else {
Qiang Xue committed
85
			if (!headers_sent()) {
86 87 88 89 90
				if ($exception instanceof HttpException) {
					header('HTTP/1.0 ' . $exception->statusCode . ' ' . $exception->getName());
				} else {
					header('HTTP/1.0 500 ' . get_class($exception));
				}
Qiang Xue committed
91 92
			}
			if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest') {
93
				Yii::$app->renderException($exception);
Qiang Xue committed
94
			} else {
95 96
				// if there is an error during error rendering it's useful to
				// display PHP error in debug mode instead of a blank screen
resurtm committed
97
				if (YII_DEBUG) {
98
					ini_set('display_errors', 1);
99
				}
resurtm committed
100

101
				$view = new View();
102 103 104 105 106
				$request = '';
				foreach (array('GET', 'POST', 'SERVER', 'FILES', 'COOKIE', 'SESSION', 'ENV') as $name) {
					if (!empty($GLOBALS['_' . $name])) {
						$request .= '$_' . $name . ' = ' . var_export($GLOBALS['_' . $name], true) . ";\n\n";
					}
resurtm committed
107
				}
108
				$request = rtrim($request, "\n\n");
resurtm committed
109
				echo $view->renderFile($this->mainView, array(
110
					'exception' => $exception,
resurtm committed
111 112
					'request' => $request,
				), $this);
Qiang Xue committed
113 114 115 116 117
			}
		}
	}

	/**
118 119
	 * Converts special characters to HTML entities.
	 * @param string $text to encode.
resurtm committed
120
	 * @return string encoded original text.
Qiang Xue committed
121
	 */
122
	public function htmlEncode($text)
Qiang Xue committed
123
	{
124
		return htmlspecialchars($text, ENT_QUOTES, Yii::$app->charset);
Qiang Xue committed
125 126
	}

Alexander Makarov committed
127
	/**
128
	 * Removes all output echoed before calling this method.
Alexander Makarov committed
129
	 */
Qiang Xue committed
130 131 132 133 134
	public function clearOutput()
	{
		// the following manual level counting is to deal with zlib.output_compression set to On
		for ($level = ob_get_level(); $level > 0; --$level) {
			@ob_end_clean();
Qiang Xue committed
135
		}
Qiang Xue committed
136
	}
resurtm committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

	/**
	 * Adds informational links to the given PHP type/class.
	 * @param string $code type/class name to be linkified.
	 * @return string linkified with HTML type/class name.
	 */
	public function addTypeLinks($code)
	{
		$html = '';
		if (strpos($code, '\\') !== false) {
			// namespaced class
			foreach (explode('\\', $code) as $part) {
				$html .= '<a href="http://yiiframework.com/doc/api/2.0/' . $this->htmlEncode($part) . '" target="_blank">' . $this->htmlEncode($part) . '</a>\\';
			}
			$html = rtrim($html, '\\');
		}
		return $html;
	}

	/**
	 * Creates HTML containing link to the page with the information on given HTTP status code.
	 * @param integer $statusCode to be used to generate information link.
159
	 * @param string $statusDescription Description to display after the the status code.
resurtm committed
160 161
	 * @return string generated HTML with HTTP status code information.
	 */
162
	public function createHttpStatusLink($statusCode, $statusDescription)
resurtm committed
163
	{
164
		return '<a href="http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#' . (int)$statusCode .'" target="_blank">HTTP ' . (int)$statusCode . ' &ndash; ' . $statusDescription . '</a>';
resurtm committed
165 166
	}

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	/**
	 * Renders the previous exception stack for a given Exception.
	 * @param \Exception $exception the exception whose precursors should be rendered.
	 * @return string HTML content of the rendered previous exceptions.
	 * Empty string if there are none.
	 */
	public function renderPreviousExceptions($exception)
	{
		if (($previous = $exception->getPrevious()) === null) {
			return '';
		}
		$view = new View();
		return $view->renderFile($this->previousExceptionView, array(
			'exception' => $previous,
			'previousHtml' => $this->renderPreviousExceptions($previous),
		), $this);
	}

resurtm committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	/**
	 * Renders a single call stack element.
	 * @param string $file name where call has happened.
	 * @param integer $line number on which call has happened.
	 * @param integer $index number of the call stack element.
	 * @return string HTML content of the rendered call stack element.
	 */
	public function renderCallStackItem($file, $line, $index)
	{
		$line--; // adjust line number from one-based to zero-based
		$lines = @file($file);
		if ($line < 0 || $lines === false || ($lineCount = count($lines)) < $line + 1) {
			return '';
		}

		$half = (int)(($index == 0 ? $this->maxSourceLines : $this->maxTraceSourceLines) / 2);
		$begin = $line - $half > 0 ? $line - $half : 0;
		$end = $line + $half < $lineCount ? $line + $half : $lineCount - 1;

		$view = new View();
		return $view->renderFile($this->callStackItemView, array(
			'file' => $file,
			'line' => $line,
			'index' => $index,
			'lines' => $lines,
			'begin' => $begin,
			'end' => $end,
		), $this);
	}

	/**
	 * Determines whether given name of the file belongs to the framework.
	 * @param string $file name to be checked.
	 * @return boolean whether given name of the file belongs to the framework.
	 */
	public function isCoreFile($file)
	{
		return $file === 'unknown' || strpos(realpath($file), YII_PATH . DIRECTORY_SEPARATOR) === 0;
	}

	/**
	 * Creates string containing HTML link which refers to the home page of determined web-server software
	 * and its full name.
	 * @return string server software information hyperlink.
	 */
	public function createServerInformationLink()
	{
		static $serverUrls = array(
			'http://httpd.apache.org/' => array('apache'),
			'http://nginx.org/' => array('nginx'),
			'http://lighttpd.net/' => array('lighttpd'),
			'http://gwan.com/' => array('g-wan', 'gwan'),
			'http://iis.net/' => array('iis', 'services'),
			'http://php.net/manual/en/features.commandline.webserver.php' => array('development'),
		);
		if (isset($_SERVER['SERVER_SOFTWARE'])) {
			foreach ($serverUrls as $url => $keywords) {
				foreach ($keywords as $keyword) {
					if (stripos($_SERVER['SERVER_SOFTWARE'], $keyword) !== false ) {
						return '<a href="' . $url . '" target="_blank">' . $this->htmlEncode($_SERVER['SERVER_SOFTWARE']) . '</a>';
					}
				}
			}
		}
		return '';
	}

	/**
	 * Creates string containing HTML link which refers to the page with the current version
	 * of the framework and version number text.
	 * @return string framework version information hyperlink.
	 */
	public function createFrameworkVersionLink()
	{
		return '<a href="http://github.com/yiisoft/yii2/" target="_blank">' . $this->htmlEncode(Yii::getVersion()) . '</a>';
	}
Qiang Xue committed
261
}