2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2008-03-16 23:13:31 +01:00
|
|
|
* Supports debugging and core error handling.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-08-19 07:41:58 +02:00
|
|
|
* Attaches custom methods to the default error handling hooks
|
|
|
|
* in PHP. Currently, two levels of error are supported:
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-08-19 08:03:57 +02:00
|
|
|
* - Notice
|
2008-03-16 23:13:31 +01:00
|
|
|
* - Warning
|
|
|
|
* - Error
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-03-16 23:13:31 +01:00
|
|
|
* Uncaught exceptions are currently passed to the debug
|
|
|
|
* reporter as standard PHP errors.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* Errors handled by this class are passed along to {@link SS_Log}.
|
|
|
|
* For configuration information, see the {@link SS_Log}
|
2009-08-19 07:41:58 +02:00
|
|
|
* class documentation.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-03-16 23:13:31 +01:00
|
|
|
* @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
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-08-19 07:41:58 +02:00
|
|
|
* @subpackage dev
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class Debug {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
|
|
|
* @var String indicating the file where errors are logged.
|
2008-08-11 06:02:32 +02:00
|
|
|
* Filename is relative to the site root.
|
2014-08-15 08:53:05 +02:00
|
|
|
* The named file will have a terse log sent to it, and the full log (an
|
2008-08-11 06:02:32 +02:00
|
|
|
* encoded file containing backtraces and things) will go to a file of a similar
|
|
|
|
* name, but with the suffix ".full" added.
|
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $log_errors_to = null;
|
2009-10-12 05:20:24 +02:00
|
|
|
|
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
|
|
|
* @var string The header of the message shown to users on the live site when a fatal error occurs.
|
2009-10-12 05:20:24 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $friendly_error_header = 'There has been an error';
|
2009-10-12 05:20:24 +02:00
|
|
|
|
2016-05-18 03:56:43 +02:00
|
|
|
/**
|
|
|
|
* Set to true to enable friendly errors to set a http response code corresponding to the error.
|
|
|
|
* If left false then error pages will be served as HTTP 200.
|
|
|
|
*
|
|
|
|
* Will be removed in 4.0, and fixed to on.
|
|
|
|
*
|
|
|
|
* @config
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $friendly_error_httpcode = false;
|
|
|
|
|
2009-10-12 05:20:24 +02:00
|
|
|
/**
|
2013-03-21 19:48:54 +01:00
|
|
|
* @config
|
|
|
|
* @var string The body of the message shown to users on the live site when a fatal error occurs.
|
2009-10-12 05:20:24 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $friendly_error_detail = 'The website server has not been able to respond to your request.';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Show the contents of val in a debug-friendly way.
|
|
|
|
* Debug::show() is intended to be equivalent to dprintr()
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function show($val, $showHeader = true) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!Director::isLive()) {
|
|
|
|
if($showHeader) {
|
|
|
|
$caller = Debug::caller();
|
2008-08-13 03:47:05 +02:00
|
|
|
if(Director::is_ajax() || Director::is_cli())
|
2012-09-26 23:34:00 +02:00
|
|
|
echo "Debug ($caller[class]$caller[type]$caller[function]() in " . basename($caller['file'])
|
|
|
|
. ":$caller[line])\n";
|
2014-08-15 08:53:05 +02:00
|
|
|
else
|
2012-09-26 23:34:00 +02:00
|
|
|
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";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
echo Debug::text($val);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-13 03:47:05 +02:00
|
|
|
if(!Director::is_ajax() && !Director::is_cli()) echo "</div>";
|
|
|
|
else echo "\n\n";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-11-15 02:56:43 +01:00
|
|
|
|
2014-07-05 04:37:06 +02:00
|
|
|
/**
|
|
|
|
* Returns the caller for a specific method
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-07-05 04:37:06 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-11-15 02:56:43 +01:00
|
|
|
public static function caller() {
|
|
|
|
$bt = debug_backtrace();
|
2016-09-19 23:03:22 +02:00
|
|
|
$caller = isset($bt[2]) ? $bt[2] : array();
|
2012-11-15 02:56:43 +01:00
|
|
|
$caller['line'] = $bt[1]['line'];
|
|
|
|
$caller['file'] = $bt[1]['file'];
|
|
|
|
if(!isset($caller['class'])) $caller['class'] = '';
|
|
|
|
if(!isset($caller['type'])) $caller['type'] = '';
|
2016-09-19 23:03:22 +02:00
|
|
|
if(!isset($caller['function'])) $caller['function'] = '';
|
2012-11-15 02:56:43 +01:00
|
|
|
return $caller;
|
|
|
|
}
|
|
|
|
|
2008-08-09 08:24:30 +02:00
|
|
|
/**
|
|
|
|
* Close out the show dumper
|
|
|
|
*
|
|
|
|
* @param mixed $val
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function endshow($val) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!Director::isLive()) {
|
|
|
|
$caller = Debug::caller();
|
2012-09-26 23:34:00 +02:00
|
|
|
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";
|
2007-07-19 12:40:28 +02:00
|
|
|
echo Debug::text($val);
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 08:24:30 +02:00
|
|
|
/**
|
|
|
|
* Quick dump of a variable.
|
|
|
|
*
|
|
|
|
* @param mixed $val
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function dump($val) {
|
2014-07-05 04:37:06 +02:00
|
|
|
self::create_debug_view()->writeVariable($val, self::caller());
|
2008-03-16 23:13:31 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-08-09 08:24:30 +02:00
|
|
|
/**
|
|
|
|
* ??
|
|
|
|
*
|
|
|
|
* @param unknown_type $val
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function text($val) {
|
2008-02-25 03:10:37 +01:00
|
|
|
if(is_object($val)) {
|
|
|
|
if(method_exists($val, 'hasMethod')) {
|
|
|
|
$hasDebugMethod = $val->hasMethod('debug');
|
2008-02-25 02:06:39 +01:00
|
|
|
} else {
|
2008-02-25 03:10:37 +01:00
|
|
|
$hasDebugMethod = method_exists($val, 'debug');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
if($hasDebugMethod) {
|
|
|
|
return $val->debug();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_array($val)) {
|
|
|
|
$result = "<ul>\n";
|
|
|
|
foreach($val as $k => $v) {
|
|
|
|
$result .= "<li>$k = " . Debug::text($v) . "</li>\n";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
$val = $result . "</ul>\n";
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
} else if (is_object($val)) {
|
|
|
|
$val = var_export($val, true);
|
2010-10-19 06:52:33 +02:00
|
|
|
} else if (is_bool($val)) {
|
|
|
|
$val = $val ? 'true' : 'false';
|
|
|
|
$val = '(bool) ' . $val;
|
2008-02-25 03:10:37 +01:00
|
|
|
} else {
|
2008-08-13 03:47:05 +02:00
|
|
|
if(!Director::is_cli() && !Director::is_ajax()) {
|
2012-09-26 23:34:00 +02:00
|
|
|
$val = "<pre style=\"font-family: Courier new\">" . htmlentities($val, ENT_COMPAT, 'UTF-8')
|
|
|
|
. "</pre>\n";
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
|
|
|
return $val;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 05:19:34 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Show a debugging message
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function message($message, $showHeader = true) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!Director::isLive()) {
|
|
|
|
$caller = Debug::caller();
|
|
|
|
$file = basename($caller['file']);
|
2008-08-09 08:18:32 +02:00
|
|
|
if(Director::is_cli()) {
|
|
|
|
if($showHeader) echo "Debug (line $caller[line] of $file):\n ";
|
2009-09-14 07:41:28 +02:00
|
|
|
echo $message . "\n";
|
2008-08-09 08:18:32 +02:00
|
|
|
} else {
|
2012-09-01 01:58:52 +02:00
|
|
|
echo "<p class=\"message warning\">\n";
|
2008-08-09 08:18:32 +02:00
|
|
|
if($showHeader) echo "<b>Debug (line $caller[line] of $file):</b>\n ";
|
2009-09-14 07:41:28 +02:00
|
|
|
echo Convert::raw2xml($message) . "</p>\n";
|
2008-08-09 08:18:32 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-07-27 05:52:40 +02:00
|
|
|
// Keep track of how many headers have been sent
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $headerCount = 0;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-07-27 05:52:40 +02:00
|
|
|
/**
|
|
|
|
* Send a debug message in an HTTP header. Only works if you are
|
|
|
|
* on Dev, and headers have not yet been sent.
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param string $msg
|
2009-07-27 05:52:40 +02:00
|
|
|
* @param string $prefix (optional)
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function header($msg, $prefix = null) {
|
2009-07-27 05:52:40 +02:00
|
|
|
if (Director::isDev() && !headers_sent()) {
|
|
|
|
self::$headerCount++;
|
|
|
|
header('SS-'.self::$headerCount.($prefix?'-'.$prefix:'').': '.$msg);
|
|
|
|
}
|
|
|
|
}
|
2008-08-09 08:24:30 +02:00
|
|
|
|
2008-08-11 00:52:52 +02:00
|
|
|
/**
|
|
|
|
* Log to a standard text file output.
|
|
|
|
*
|
|
|
|
* @param $message string to output
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function log($message) {
|
2013-10-10 11:18:03 +02:00
|
|
|
if (defined('BASE_PATH')) {
|
|
|
|
$path = BASE_PATH;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$path = dirname(__FILE__) . '/../..';
|
|
|
|
}
|
|
|
|
$file = $path . '/debug.log';
|
2008-08-11 00:52:52 +02:00
|
|
|
$now = date('r');
|
2013-10-10 11:18:03 +02:00
|
|
|
$content = "\n\n== $now ==\n$message\n";
|
|
|
|
file_put_contents($file, $content, FILE_APPEND);
|
2008-08-11 00:52:52 +02:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-08-13 23:49:52 +02:00
|
|
|
* Load error handlers into environment.
|
|
|
|
* Caution: The error levels default to E_ALL is the site is in dev-mode (set in main.php).
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function loadErrorHandlers() {
|
2008-08-13 01:50:28 +02:00
|
|
|
set_error_handler('errorHandler', error_reporting());
|
2008-02-25 03:10:37 +01:00
|
|
|
set_exception_handler('exceptionHandler');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function noticeHandler($errno, $errstr, $errfile, $errline, $errcontext) {
|
2009-08-19 08:03:57 +02:00
|
|
|
if(error_reporting() == 0) return;
|
2011-03-15 06:30:16 +01:00
|
|
|
ini_set('display_errors', 0);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-19 08:03:57 +02:00
|
|
|
// Send out the error details to the logger for writing
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
SS_Log::log(
|
2009-08-19 08:03:57 +02:00
|
|
|
array(
|
|
|
|
'errno' => $errno,
|
|
|
|
'errstr' => $errstr,
|
|
|
|
'errfile' => $errfile,
|
|
|
|
'errline' => $errline,
|
|
|
|
'errcontext' => $errcontext
|
|
|
|
),
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
SS_Log::NOTICE
|
2009-08-19 08:03:57 +02:00
|
|
|
);
|
2012-04-11 04:39:02 +02:00
|
|
|
|
2009-08-19 08:03:57 +02:00
|
|
|
if(Director::isDev()) {
|
2011-03-15 06:30:16 +01:00
|
|
|
return self::showError($errno, $errstr, $errfile, $errline, $errcontext, "Notice");
|
|
|
|
} else {
|
2013-02-18 15:43:52 +01:00
|
|
|
return false;
|
2009-08-19 08:03:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-09 08:24:30 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function warningHandler($errno, $errstr, $errfile, $errline, $errcontext) {
|
2009-08-19 05:55:23 +02:00
|
|
|
if(error_reporting() == 0) return;
|
2011-03-15 06:30:16 +01:00
|
|
|
ini_set('display_errors', 0);
|
|
|
|
|
2009-08-19 05:55:23 +02:00
|
|
|
// Send out the error details to the logger for writing
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
SS_Log::log(
|
2009-08-19 05:55:23 +02:00
|
|
|
array(
|
|
|
|
'errno' => $errno,
|
|
|
|
'errstr' => $errstr,
|
|
|
|
'errfile' => $errfile,
|
|
|
|
'errline' => $errline,
|
|
|
|
'errcontext' => $errcontext
|
|
|
|
),
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
SS_Log::WARN
|
2009-08-19 05:55:23 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(Director::isDev()) {
|
2011-03-15 06:30:16 +01:00
|
|
|
return self::showError($errno, $errstr, $errfile, $errline, $errcontext, "Warning");
|
|
|
|
} else {
|
2013-02-18 15:43:52 +01:00
|
|
|
return false;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 05:19:34 +02:00
|
|
|
|
2008-08-09 08:24:30 +02:00
|
|
|
/**
|
|
|
|
* Handle a fatal error, depending on the mode of the site (ie: Dev, Test, or Live).
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-08-09 08:24:30 +02:00
|
|
|
* 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
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function fatalHandler($errno, $errstr, $errfile, $errline, $errcontext) {
|
2011-03-15 06:30:16 +01:00
|
|
|
ini_set('display_errors', 0);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-08-19 05:55:23 +02:00
|
|
|
// Send out the error details to the logger for writing
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
SS_Log::log(
|
2009-08-19 05:55:23 +02:00
|
|
|
array(
|
|
|
|
'errno' => $errno,
|
|
|
|
'errstr' => $errstr,
|
|
|
|
'errfile' => $errfile,
|
|
|
|
'errline' => $errline,
|
|
|
|
'errcontext' => $errcontext
|
|
|
|
),
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
SS_Log::ERR
|
2009-08-19 05:55:23 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-11 06:02:32 +02:00
|
|
|
if(Director::isDev() || Director::is_cli()) {
|
2014-07-16 13:03:55 +02:00
|
|
|
self::showError($errno, $errstr, $errfile, $errline, $errcontext, "Error");
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2014-07-16 13:03:55 +02:00
|
|
|
self::friendlyError();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-07-16 13:03:55 +02:00
|
|
|
return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 08:24:30 +02:00
|
|
|
/**
|
|
|
|
* Render a user-facing error page, using the default HTML error template
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* rendered by {@link ErrorPage} if it exists. Doesn't use the standard {@link SS_HTTPResponse} class
|
2014-08-15 08:53:05 +02:00
|
|
|
* the keep dependencies minimal.
|
|
|
|
*
|
2008-10-01 16:43:43 +02:00
|
|
|
* @uses ErrorPage
|
2008-08-09 08:24:30 +02:00
|
|
|
*
|
2008-10-01 16:43:43 +02:00
|
|
|
* @param int $statusCode HTTP Status Code (Default: 500)
|
2012-09-26 23:34:00 +02:00
|
|
|
* @param string $friendlyErrorMessage User-focused error message. Should not contain code pointers
|
|
|
|
* or "tech-speak". Used in the HTTP Header and ajax responses.
|
2008-10-01 16:43:43 +02:00
|
|
|
* @param string $friendlyErrorDetail Detailed user-focused message. Is just used if no {@link ErrorPage} is found
|
2012-09-26 23:34:00 +02:00
|
|
|
* for this specific status code.
|
2008-10-01 16:43:43 +02:00
|
|
|
* @return string HTML error message for non-ajax requests, plaintext for ajax-request.
|
2008-08-09 08:24:30 +02:00
|
|
|
*/
|
2012-09-26 23:34:00 +02:00
|
|
|
public static function friendlyError($statusCode=500, $friendlyErrorMessage=null, $friendlyErrorDetail=null) {
|
2016-05-18 03:56:43 +02:00
|
|
|
// Ensure the error message complies with the HTTP 1.1 spec
|
2013-03-21 19:48:54 +01:00
|
|
|
if(!$friendlyErrorMessage) {
|
|
|
|
$friendlyErrorMessage = Config::inst()->get('Debug', 'friendly_error_header');
|
|
|
|
}
|
2016-05-18 03:56:43 +02:00
|
|
|
$friendlyErrorMessage = strip_tags(str_replace(array("\n", "\r"), '', $friendlyErrorMessage));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
if(!$friendlyErrorDetail) {
|
|
|
|
$friendlyErrorDetail = Config::inst()->get('Debug', 'friendly_error_detail');
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-11-21 03:31:30 +01:00
|
|
|
if(!headers_sent()) {
|
2016-05-18 03:56:43 +02:00
|
|
|
// Allow toggle between legacy behaviour and correctly setting HTTP response code
|
|
|
|
// In 4.0 this should be fixed to always set this response code.
|
|
|
|
if(Config::inst()->get('Debug', 'friendly_error_httpcode') || !Controller::has_curr()) {
|
|
|
|
header($_SERVER['SERVER_PROTOCOL'] . " $statusCode $friendlyErrorMessage");
|
2009-11-21 03:31:30 +01:00
|
|
|
}
|
|
|
|
}
|
2007-09-14 05:19:34 +02:00
|
|
|
|
2008-10-01 16:43:43 +02:00
|
|
|
if(Director::is_ajax()) {
|
|
|
|
echo $friendlyErrorMessage;
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2016-03-28 02:20:18 +02:00
|
|
|
if(!headers_sent()) header('Content-Type: text/html');
|
2011-03-18 03:01:09 +01:00
|
|
|
if(class_exists('ErrorPage')){
|
2011-03-22 09:30:34 +01:00
|
|
|
$errorFilePath = ErrorPage::get_filepath_for_errorcode(
|
2014-08-15 08:53:05 +02:00
|
|
|
$statusCode,
|
2011-03-22 09:30:34 +01:00
|
|
|
class_exists('Translatable') ? Translatable::get_current_locale() : null
|
|
|
|
);
|
2011-03-18 03:01:09 +01:00
|
|
|
if(file_exists($errorFilePath)) {
|
2014-09-12 13:21:11 +02:00
|
|
|
$content = file_get_contents($errorFilePath);
|
2011-03-18 03:01:09 +01:00
|
|
|
// $BaseURL is left dynamic in error-###.html, so that multi-domain sites don't get broken
|
|
|
|
echo str_replace('$BaseURL', Director::absoluteBaseURL(), $content);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2008-10-01 16:43:43 +02:00
|
|
|
$renderer = new DebugView();
|
|
|
|
$renderer->writeHeader();
|
|
|
|
$renderer->writeInfo("Website Error", $friendlyErrorMessage, $friendlyErrorDetail);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
if(Email::config()->admin_email) {
|
|
|
|
$mailto = Email::obfuscate(Email::config()->admin_email);
|
2008-10-01 16:43:43 +02:00
|
|
|
$renderer->writeParagraph('Contact an administrator: ' . $mailto . '');
|
|
|
|
}
|
|
|
|
|
|
|
|
$renderer->writeFooter();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2011-03-15 06:30:16 +01:00
|
|
|
return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-07-05 04:37:06 +02:00
|
|
|
|
2008-08-11 06:02:32 +02:00
|
|
|
/**
|
|
|
|
* Create an instance of an appropriate DebugView object.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-07-05 04:37:06 +02:00
|
|
|
* @return DebugView
|
2008-08-11 06:02:32 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function create_debug_view() {
|
2014-07-05 04:37:06 +02:00
|
|
|
$service = Director::is_cli() || Director::is_ajax()
|
|
|
|
? 'CliDebugView'
|
|
|
|
: 'DebugView';
|
|
|
|
return Injector::inst()->get($service);
|
2008-08-11 06:02:32 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2008-08-09 08:24:30 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function showError($errno, $errstr, $errfile, $errline, $errcontext, $errtype) {
|
2008-09-12 03:47:39 +02:00
|
|
|
if(!headers_sent()) {
|
2012-06-29 08:14:14 +02:00
|
|
|
$errText = "$errtype at line $errline of $errfile";
|
2008-09-12 03:47:39 +02:00
|
|
|
$errText = str_replace(array("\n","\r")," ",$errText);
|
2012-06-29 08:14:14 +02:00
|
|
|
|
2008-10-01 16:43:43 +02:00
|
|
|
if(!headers_sent()) header($_SERVER['SERVER_PROTOCOL'] . " 500 $errText");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-16 20:15:26 +02:00
|
|
|
// if error is displayed through ajax with CliDebugView, use plaintext output
|
2012-06-29 08:14:14 +02:00
|
|
|
if(Director::is_ajax()) {
|
|
|
|
header('Content-Type: text/plain');
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2008-09-12 03:47:39 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-16 20:12:07 +02:00
|
|
|
$reporter = self::create_debug_view();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-16 20:12:07 +02:00
|
|
|
// Coupling alert: This relies on knowledge of how the director gets its URL, it could be improved.
|
2014-03-04 23:47:02 +01:00
|
|
|
$httpRequest = null;
|
|
|
|
if(isset($_SERVER['REQUEST_URI'])) {
|
|
|
|
$httpRequest = $_SERVER['REQUEST_URI'];
|
|
|
|
} elseif(isset($_REQUEST['url'])) {
|
|
|
|
$httpRequest = $_REQUEST['url'];
|
|
|
|
}
|
2008-09-16 20:12:07 +02:00
|
|
|
if(isset($_SERVER['REQUEST_METHOD'])) $httpRequest = $_SERVER['REQUEST_METHOD'] . ' ' . $httpRequest;
|
2008-08-11 06:02:32 +02:00
|
|
|
|
2008-09-16 20:12:07 +02:00
|
|
|
$reporter->writeHeader($httpRequest);
|
|
|
|
$reporter->writeError($httpRequest, $errno, $errstr, $errfile, $errline, $errcontext);
|
2008-08-11 06:02:32 +02:00
|
|
|
|
2009-10-21 04:32:55 +02:00
|
|
|
if(file_exists($errfile)) {
|
|
|
|
$lines = file($errfile);
|
2008-08-13 05:54:50 +02:00
|
|
|
|
2009-10-21 04:32:55 +02:00
|
|
|
// Make the array 1-based
|
|
|
|
array_unshift($lines,"");
|
|
|
|
unset($lines[0]);
|
2008-08-11 06:02:32 +02:00
|
|
|
|
2009-10-21 04:32:55 +02:00
|
|
|
$offset = $errline-10;
|
|
|
|
$lines = array_slice($lines, $offset, 16, true);
|
|
|
|
$reporter->writeSourceFragment($lines, $errline);
|
|
|
|
}
|
2009-01-20 22:30:12 +01:00
|
|
|
$reporter->writeTrace(($errcontext ? $errcontext : debug_backtrace()));
|
2008-09-16 20:12:07 +02:00
|
|
|
$reporter->writeFooter();
|
2013-10-23 00:43:37 +02:00
|
|
|
return false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-09 08:24:30 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function showLines($errfile, $errline) {
|
2008-08-09 08:18:32 +02:00
|
|
|
$lines = file($errfile);
|
|
|
|
$offset = $errline-10;
|
|
|
|
$lines = array_slice($lines, $offset, 16);
|
|
|
|
echo '<pre>';
|
|
|
|
$offset++;
|
|
|
|
foreach($lines as $line) {
|
2010-12-08 00:53:42 +01:00
|
|
|
$line = htmlentities($line, ENT_COMPAT, 'UTF-8');
|
2008-08-09 08:18:32 +02:00
|
|
|
if ($offset == $errline) {
|
|
|
|
echo "<span>$offset</span> <span class=\"error\">$line</span>";
|
|
|
|
} else {
|
|
|
|
echo "<span>$offset</span> $line";
|
|
|
|
}
|
|
|
|
$offset++;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
echo '</pre>';
|
2008-08-09 08:18:32 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-11-08 00:46:00 +01:00
|
|
|
/**
|
|
|
|
* Check if the user has permissions to run URL debug tools,
|
|
|
|
* else redirect them to log in.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function require_developer_login() {
|
2007-11-08 00:46:00 +01:00
|
|
|
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');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// @TODO - Rewrite safely using DataList::filter
|
2007-11-08 00:46:00 +01:00
|
|
|
$memberID = $_SESSION['loggedInAs'];
|
2013-06-21 00:32:08 +02:00
|
|
|
$permission = DB::prepared_query('
|
|
|
|
SELECT "ID" FROM "Permission"
|
|
|
|
INNER JOIN "Group_Members" ON "Permission"."GroupID" = "Group_Members"."GroupID"
|
2014-08-15 08:53:05 +02:00
|
|
|
WHERE "Permission"."Code" = ?
|
|
|
|
AND "Permission"."Type" = ?
|
2013-06-21 00:32:08 +02:00
|
|
|
AND "Group_Members"."MemberID" = ?',
|
|
|
|
array(
|
|
|
|
'ADMIN', // Code
|
|
|
|
Permission::GRANT_PERMISSION, // Type
|
|
|
|
$memberID // MemberID
|
2007-11-08 00:46:00 +01:00
|
|
|
)
|
2013-06-21 00:32:08 +02:00
|
|
|
)->value();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
if($permission) return;
|
2007-11-08 00:46:00 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-11-08 00:46:00 +01:00
|
|
|
// This basically does the same as
|
2012-09-26 23:34:00 +02:00
|
|
|
// Security::permissionFailure(null, "You need to login with developer access to make use of debugging tools.")
|
2007-11-08 00:46:00 +01:00
|
|
|
// We have to do this because of how early this method is called in execution.
|
2012-09-26 23:34:00 +02:00
|
|
|
$_SESSION['Security']['Message']['message']
|
|
|
|
= "You need to login with developer access to make use of debugging tools.";
|
2007-11-08 00:46:00 +01:00
|
|
|
$_SESSION['Security']['Message']['type'] = 'warning';
|
|
|
|
$_SESSION['BackURL'] = $_SERVER['REQUEST_URI'];
|
2008-10-01 16:43:43 +02:00
|
|
|
header($_SERVER['SERVER_PROTOCOL'] . " 302 Found");
|
2011-03-16 04:13:14 +01:00
|
|
|
header("Location: " . Director::baseURL() . Security::login_url());
|
2007-11-08 00:46:00 +01:00
|
|
|
die();
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-09-22 18:03:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-08-09 08:24:30 +02:00
|
|
|
/**
|
|
|
|
* Generic callback, to catch uncaught exceptions when they bubble up to the top of the call chain.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* @ignore
|
2008-09-22 18:03:19 +02:00
|
|
|
* @param Exception $exception
|
2008-08-09 08:24:30 +02:00
|
|
|
*/
|
2008-02-25 03:10:37 +01:00
|
|
|
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();
|
2015-07-18 00:26:43 +02:00
|
|
|
Debug::fatalHandler($errno, $message, $file, $line, $context);
|
|
|
|
exit(1);
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
|
|
|
|
2008-08-09 08:24:30 +02:00
|
|
|
/**
|
|
|
|
* Generic callback to catch standard PHP runtime errors thrown by the interpreter
|
2014-03-15 12:52:01 +01:00
|
|
|
* or manually triggered with the user_error function. Any unknown error codes are treated as
|
|
|
|
* fatal errors.
|
|
|
|
* Caution: The error levels default to E_ALL if the site is in dev-mode (set in main.php).
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* @ignore
|
2008-09-22 18:03:19 +02:00
|
|
|
* @param int $errno
|
|
|
|
* @param string $errstr
|
|
|
|
* @param string $errfile
|
|
|
|
* @param int $errline
|
2008-08-09 08:24:30 +02:00
|
|
|
*/
|
2009-03-22 09:38:14 +01:00
|
|
|
function errorHandler($errno, $errstr, $errfile, $errline) {
|
2007-07-19 12:40:28 +02:00
|
|
|
switch($errno) {
|
2009-08-19 08:03:57 +02:00
|
|
|
case E_NOTICE:
|
|
|
|
case E_USER_NOTICE:
|
2012-03-27 11:54:13 +02:00
|
|
|
case E_DEPRECATED:
|
2012-03-24 04:34:10 +01:00
|
|
|
case E_USER_DEPRECATED:
|
2012-04-11 04:39:02 +02:00
|
|
|
case E_STRICT:
|
2011-03-15 06:30:16 +01:00
|
|
|
return Debug::noticeHandler($errno, $errstr, $errfile, $errline, debug_backtrace());
|
2014-03-15 12:52:01 +01:00
|
|
|
|
|
|
|
case E_WARNING:
|
|
|
|
case E_CORE_WARNING:
|
|
|
|
case E_USER_WARNING:
|
|
|
|
case E_RECOVERABLE_ERROR:
|
|
|
|
return Debug::warningHandler($errno, $errstr, $errfile, $errline, debug_backtrace());
|
|
|
|
|
|
|
|
case E_ERROR:
|
|
|
|
case E_CORE_ERROR:
|
|
|
|
case E_USER_ERROR:
|
|
|
|
default:
|
2015-07-20 11:33:55 +02:00
|
|
|
Debug::fatalHandler($errno, $errstr, $errfile, $errline, debug_backtrace());
|
|
|
|
exit(1);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|