Debug.php 16.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
<?php
/**
 * Supports debugging and core error handling.
 *
 * Attaches custom methods to the default error handling hooks
 * in PHP. Currently, two levels of error are supported:
 *
 * - Notice
 * - Warning
 * - Error
 *
 * Uncaught exceptions are currently passed to the debug
 * reporter as standard PHP errors.
 *
 * Errors handled by this class are passed along to {@link SS_Log}.
 * For configuration information, see the {@link SS_Log}
 * class documentation.
 *
 * @todo add support for user defined config: Debug::die_on_notice(true | false)
 * @todo better way of figuring out the error context to display in highlighted source
 *
 * @package framework
 * @subpackage dev
 */
class Debug {

	/**
	 * @config
	 * @var String indicating the file where errors are logged.
	 * Filename is relative to the site root.
	 * The named file will have a terse log sent to it, and the full log (an
	 * encoded file containing backtraces and things) will go to a file of a similar
	 * name, but with the suffix ".full" added.
	 */
	private static $log_errors_to = null;

	/**
	 * @config
	 * @var string The header of the message shown to users on the live site when a fatal error occurs.
	 */
	private static $friendly_error_header = 'There has been an error';

	/**
	 * @config
	 * @var string The body of the message shown to users on the live site when a fatal error occurs.
	 */
	private static $friendly_error_detail = 'The website server has not been able to respond to your request.';

	/**
	 * Show the contents of val in a debug-friendly way.
	 * Debug::show() is intended to be equivalent to dprintr()
	 */
	public static function show($val, $showHeader = true) {
		if(!Director::isLive()) {
			if($showHeader) {
				$caller = Debug::caller();
				if(Director::is_ajax() || Director::is_cli())
					echo "Debug ($caller[class]$caller[type]$caller[function]() in " . basename($caller['file'])
						. ":$caller[line])\n";
				else
					echo "<div style=\"background-color: white; text-align: left;\">\n<hr>\n"
						. "<h3>Debug <span style=\"font-size: 65%\">($caller[class]$caller[type]$caller[function]()"
						. " \nin " . basename($caller['file']) . ":$caller[line])</span>\n</h3>\n";
			}

			echo Debug::text($val);

			if(!Director::is_ajax() && !Director::is_cli()) echo "</div>";
			else echo "\n\n";
		}

	}

	/**
	 * Returns the caller for a specific method
	 *
	 * @return array
	 */
	public static function caller() {
		$bt = debug_backtrace();
		$caller = $bt[2];
		$caller['line'] = $bt[1]['line'];
		$caller['file'] = $bt[1]['file'];
		if(!isset($caller['class'])) $caller['class'] = '';
		if(!isset($caller['type'])) $caller['type'] = '';
		return $caller;
	}

	/**
	 * Close out the show dumper
	 *
	 * @param mixed $val
	 */
	public static function endshow($val) {
		if(!Director::isLive()) {
			$caller = Debug::caller();
			echo "<hr>\n<h3>Debug \n<span style=\"font-size: 65%\">($caller[class]$caller[type]$caller[function]()"
				. " \nin " . basename($caller['file']) . ":$caller[line])</span>\n</h3>\n";
			echo Debug::text($val);
			die();
		}
	}

	/**
	 * Quick dump of a variable.
	 *
	 * @param mixed $val
	 */
	public static function dump($val) {
		self::create_debug_view()->writeVariable($val, self::caller());
	}

	/**
	 * ??
	 *
	 * @param unknown_type $val
	 * @return unknown
	 */
	public static function text($val) {
		if(is_object($val)) {
			if(method_exists($val, 'hasMethod')) {
				$hasDebugMethod = $val->hasMethod('debug');
			} else {
				$hasDebugMethod = method_exists($val, 'debug');
			}

			if($hasDebugMethod) {
				$debug = $val->debug();
				// Conditional not necessary after 3.2. See https://github.com/silverstripe/silverstripe-framework/pull/4034
				if ($debug instanceof ViewableData_Debugger) {
					$debug = $debug->forTemplate();
				}
				return $debug;
			}
		}

		if(is_array($val)) {
			$result = "<ul>\n";
			foreach($val as $k => $v) {
				$result .= "<li>$k = " . Debug::text($v) . "</li>\n";
			}
			$val = $result . "</ul>\n";

		} else if (is_object($val)) {
			$val = var_export($val, true);
		} else if (is_bool($val)) {
			$val = $val ? 'true' : 'false';
			$val = '(bool) ' . $val;
		} else {
			if(!Director::is_cli() && !Director::is_ajax()) {
				$val = "<pre style=\"font-family: Courier new\">" . htmlentities($val, ENT_COMPAT, 'UTF-8')
					. "</pre>\n";
			}
		}

		return $val;
	}

	/**
	 * Show a debugging message
	 */
	public static function message($message, $showHeader = true) {
		if(!Director::isLive()) {
			$caller = Debug::caller();
			$file = basename($caller['file']);
			if(Director::is_cli()) {
				if($showHeader) echo "Debug (line $caller[line] of $file):\n ";
				echo $message . "\n";
			} else {
				echo "<p class=\"message warning\">\n";
				if($showHeader) echo "<b>Debug (line $caller[line] of $file):</b>\n ";
				echo Convert::raw2xml($message) . "</p>\n";
			}
		}
	}

	// Keep track of how many headers have been sent
	private static $headerCount = 0;

	/**
	 * Send a debug message in an HTTP header. Only works if you are
	 * on Dev, and headers have not yet been sent.
	 *
	 * @param string $msg
	 * @param string $prefix (optional)
	 * @return void
	 */
	public static function header($msg, $prefix = null) {
		if (Director::isDev() && !headers_sent()) {
			self::$headerCount++;
			header('SS-'.self::$headerCount.($prefix?'-'.$prefix:'').': '.$msg);
		}
	}

	/**
	 * Log to a standard text file output.
	 *
	 * @param $message string to output
	 */
	public static function log($message) {
		if (defined('BASE_PATH')) {
			$path = BASE_PATH;
		}
		else {
			$path = dirname(__FILE__) . '/../..';
		}
		$file = $path . '/debug.log';
		$now = date('r');
		$content = "\n\n== $now ==\n$message\n";
		file_put_contents($file, $content, FILE_APPEND);
	}

	/**
	 * Load error handlers into environment.
	 * Caution: The error levels default to E_ALL is the site is in dev-mode (set in main.php).
	 */
	public static function loadErrorHandlers() {
		set_error_handler('errorHandler', error_reporting());
		set_exception_handler('exceptionHandler');
	}

	public static function noticeHandler($errno, $errstr, $errfile, $errline, $errcontext) {
		if(error_reporting() == 0) return;
		ini_set('display_errors', 0);

		// Send out the error details to the logger for writing
		SS_Log::log(
			array(
				'errno' => $errno,
				'errstr' => $errstr,
				'errfile' => $errfile,
				'errline' => $errline,
				'errcontext' => $errcontext
			),
			SS_Log::NOTICE
		);

		if(Director::isDev()) {
			return self::showError($errno, $errstr, $errfile, $errline, $errcontext, "Notice");
		} else {
			return false;
		}
	}

	/**
	 * Handle a non-fatal warning error thrown by PHP interpreter.
	 *
	 * @param unknown_type $errno
	 * @param unknown_type $errstr
	 * @param unknown_type $errfile
	 * @param unknown_type $errline
	 * @param unknown_type $errcontext
	 */
	public static function warningHandler($errno, $errstr, $errfile, $errline, $errcontext) {
		if(error_reporting() == 0) return;
		ini_set('display_errors', 0);

		// Send out the error details to the logger for writing
		SS_Log::log(
			array(
				'errno' => $errno,
				'errstr' => $errstr,
				'errfile' => $errfile,
				'errline' => $errline,
				'errcontext' => $errcontext
			),
			SS_Log::WARN
		);

		if(Director::isDev()) {
			return self::showError($errno, $errstr, $errfile, $errline, $errcontext, "Warning");
		} else {
			return false;
		}
	}

	/**
	 * Handle a fatal error, depending on the mode of the site (ie: Dev, Test, or Live).
	 *
	 * Runtime execution dies immediately once the error is generated.
	 *
	 * @param unknown_type $errno
	 * @param unknown_type $errstr
	 * @param unknown_type $errfile
	 * @param unknown_type $errline
	 * @param unknown_type $errcontext
	 */
	public static function fatalHandler($errno, $errstr, $errfile, $errline, $errcontext) {
		ini_set('display_errors', 0);

		// Send out the error details to the logger for writing
		SS_Log::log(
			array(
				'errno' => $errno,
				'errstr' => $errstr,
				'errfile' => $errfile,
				'errline' => $errline,
				'errcontext' => $errcontext
			),
			SS_Log::ERR
		);

		if(Director::isDev() || Director::is_cli()) {
			self::showError($errno, $errstr, $errfile, $errline, $errcontext, "Error");
		} else {
			self::friendlyError();
		}
		return false;
	}

	/**
	 * Render a user-facing error page, using the default HTML error template
	 * rendered by {@link ErrorPage} if it exists. Doesn't use the standard {@link SS_HTTPResponse} class
	 * the keep dependencies minimal.
	 *
	 * @uses ErrorPage
	 *
	 * @param int $statusCode HTTP Status Code (Default: 500)
	 * @param string $friendlyErrorMessage User-focused error message. Should not contain code pointers
	 *                                     or "tech-speak". Used in the HTTP Header and ajax responses.
	 * @param string $friendlyErrorDetail Detailed user-focused message. Is just used if no {@link ErrorPage} is found
	 *                                    for this specific status code.
	 * @return string HTML error message for non-ajax requests, plaintext for ajax-request.
	 */
	public static function friendlyError($statusCode=500, $friendlyErrorMessage=null, $friendlyErrorDetail=null) {
		if(!$friendlyErrorMessage) {
			$friendlyErrorMessage = Config::inst()->get('Debug', 'friendly_error_header');
		}

		if(!$friendlyErrorDetail) {
			$friendlyErrorDetail = Config::inst()->get('Debug', 'friendly_error_detail');
		}

		if(!headers_sent()) {
			$currController = Controller::has_curr() ? Controller::curr() : null;
			// Ensure the error message complies with the HTTP 1.1 spec
			$msg = strip_tags(str_replace(array("\n", "\r"), '', $friendlyErrorMessage));
			if($currController) {
				$response = $currController->getResponse();
				$response->setStatusCode($statusCode, $msg);
			} else {
				header($_SERVER['SERVER_PROTOCOL'] . " $statusCode $msg");
			}
		}

		if(Director::is_ajax()) {
			echo $friendlyErrorMessage;
		} else {
			if(class_exists('ErrorPage')){
				$errorFilePath = ErrorPage::get_filepath_for_errorcode(
					$statusCode,
					class_exists('Translatable') ? Translatable::get_current_locale() : null
				);
				if(file_exists($errorFilePath)) {
					$content = file_get_contents($errorFilePath);
					if(!headers_sent()) header('Content-Type: text/html');
					// $BaseURL is left dynamic in error-###.html, so that multi-domain sites don't get broken
					echo str_replace('$BaseURL', Director::absoluteBaseURL(), $content);
				}
			} else {
				$renderer = new DebugView();
				$renderer->writeHeader();
				$renderer->writeInfo("Website Error", $friendlyErrorMessage, $friendlyErrorDetail);

				if(Email::config()->admin_email) {
					$mailto = Email::obfuscate(Email::config()->admin_email);
					$renderer->writeParagraph('Contact an administrator: ' . $mailto . '');
				}

				$renderer->writeFooter();
			}
		}
		return false;
	}

	/**
	 * Create an instance of an appropriate DebugView object.
	 *
	 * @return DebugView
	 */
	public static function create_debug_view() {
		$service = Director::is_cli() || Director::is_ajax()
			? 'CliDebugView'
			: 'DebugView';
		return Injector::inst()->get($service);
	}

	/**
	 * Render a developer facing error page, showing the stack trace and details
	 * of the code where the error occured.
	 *
	 * @param unknown_type $errno
	 * @param unknown_type $errstr
	 * @param unknown_type $errfile
	 * @param unknown_type $errline
	 * @param unknown_type $errcontext
	 */
	public static function showError($errno, $errstr, $errfile, $errline, $errcontext, $errtype) {
		if(!headers_sent()) {
			$errText = "$errtype at line $errline of $errfile";
			$errText = str_replace(array("\n","\r")," ",$errText);

			if(!headers_sent()) header($_SERVER['SERVER_PROTOCOL'] . " 500 $errText");

			// if error is displayed through ajax with CliDebugView, use plaintext output
			if(Director::is_ajax()) {
				header('Content-Type: text/plain');
			}
		}

		$reporter = self::create_debug_view();

		// Coupling alert: This relies on knowledge of how the director gets its URL, it could be improved.
		$httpRequest = null;
		if(isset($_SERVER['REQUEST_URI'])) {
			$httpRequest = $_SERVER['REQUEST_URI'];
		} elseif(isset($_REQUEST['url'])) {
			$httpRequest = $_REQUEST['url'];
		}
		if(isset($_SERVER['REQUEST_METHOD'])) $httpRequest = $_SERVER['REQUEST_METHOD'] . ' ' . $httpRequest;

		$reporter->writeHeader($httpRequest);
		$reporter->writeError($httpRequest, $errno, $errstr, $errfile, $errline, $errcontext);

		if(file_exists($errfile)) {
			$lines = file($errfile);

			// Make the array 1-based
			array_unshift($lines,"");
			unset($lines[0]);

			$offset = $errline-10;
			$lines = array_slice($lines, $offset, 16, true);
			$reporter->writeSourceFragment($lines, $errline);
		}
		$reporter->writeTrace(($errcontext ? $errcontext : debug_backtrace()));
		$reporter->writeFooter();
	}

	/**
	 * Utility method to render a snippet of PHP source code, from selected file
	 * and highlighting the given line number.
	 *
	 * @param string $errfile
	 * @param int $errline
	 */
	public static function showLines($errfile, $errline) {
		$lines = file($errfile);
		$offset = $errline-10;
		$lines = array_slice($lines, $offset, 16);
		echo '<pre>';
		$offset++;
		foreach($lines as $line) {
			$line = htmlentities($line, ENT_COMPAT, 'UTF-8');
			if ($offset == $errline) {
				echo "<span>$offset</span> <span class=\"error\">$line</span>";
			} else {
				echo "<span>$offset</span> $line";
			}
			$offset++;
		}
		echo '</pre>';
	}

	/**
	 * Check if the user has permissions to run URL debug tools,
	 * else redirect them to log in.
	 */
	public static function require_developer_login() {
		if(Director::isDev())	{
			return;
		}
		if(isset($_SESSION['loggedInAs'])) {
			// We have to do some raw SQL here, because this method is called in Object::defineMethods().
			// This means we have to be careful about what objects we create, as we don't want Object::defineMethods()
			// being called again.
			// This basically calls Permission::checkMember($_SESSION['loggedInAs'], 'ADMIN');

			$memberID = $_SESSION['loggedInAs'];

			$groups = DB::query("SELECT \"GroupID\" from \"Group_Members\" WHERE \"MemberID\" = " . $memberID);
			$groupCSV = implode($groups->column(), ',');

			$permission = DB::query("
				SELECT \"ID\"
				FROM \"Permission\"
				WHERE (
					\"Code\" = 'ADMIN'
					AND \"Type\" = " . Permission::GRANT_PERMISSION . "
					AND \"GroupID\" IN ($groupCSV)
				)
			")->value();

			if($permission) {
				return;
			}
		}

		// This basically does the same as
		// Security::permissionFailure(null, "You need to login with developer access to make use of debugging tools.")
		// We have to do this because of how early this method is called in execution.
		$_SESSION['Security']['Message']['message']
			= "You need to login with developer access to make use of debugging tools.";
		$_SESSION['Security']['Message']['type'] =  'warning';
		$_SESSION['BackURL'] = $_SERVER['REQUEST_URI'];
		header($_SERVER['SERVER_PROTOCOL'] . " 302 Found");
		header("Location: " . Director::baseURL() . Security::login_url());
		die();
	}
}










/**
 * Generic callback, to catch uncaught exceptions when they bubble up to the top of the call chain.
 *
 * @ignore
 * @param Exception $exception
 */
function exceptionHandler($exception) {
	$errno = E_USER_ERROR;
	$type = get_class($exception);
	$message = "Uncaught " . $type . ": " . $exception->getMessage();
	$file = $exception->getFile();
	$line = $exception->getLine();
	$context = $exception->getTrace();
	return Debug::fatalHandler($errno, $message, $file, $line, $context);
}

/**
 * Generic callback to catch standard PHP runtime errors thrown by the interpreter
 * or manually triggered with the user_error function.
 * Caution: The error levels default to E_ALL is the site is in dev-mode (set in main.php).
 *
 * @ignore
 * @param int $errno
 * @param string $errstr
 * @param string $errfile
 * @param int $errline
 */
function errorHandler($errno, $errstr, $errfile, $errline) {
	switch($errno) {
		case E_ERROR:
		case E_CORE_ERROR:
		case E_USER_ERROR:
			return Debug::fatalHandler($errno, $errstr, $errfile, $errline, debug_backtrace());

		case E_WARNING:
		case E_CORE_WARNING:
		case E_USER_WARNING:
			return Debug::warningHandler($errno, $errstr, $errfile, $errline, debug_backtrace());

		case E_NOTICE:
		case E_USER_NOTICE:
		case E_DEPRECATED:
		case E_USER_DEPRECATED:
		case E_STRICT:
			return Debug::noticeHandler($errno, $errstr, $errfile, $errline, debug_backtrace());
	}
}