Example #1
0
 /**
  * @param string $message The message of the exception.
  * @param int    $code    The numeric code of the exception.
  */
 public function __construct($message, $code = 0)
 {
     parent::__construct((string) $message, (int) $code);
 }
Example #2
0
/**
 * Prints out the thrown exception in a more readable manner for a person using
 * a web browser.
 *
 * @param	SpoonException $exception
 */
function exceptionHandler($exception)
{
    // fetch trace stack
    $trace = $exception->getTrace();
    // specific name
    $name = is_callable(array($exception, 'getName')) ? $exception->getName() : get_class($exception);
    // spoon type exception
    if (is_callable(array($exception, 'getName')) && strtolower(substr($exception->getName(), 0, 5)) == 'spoon' && $exception->getCode() != 0) {
        $documentation = '&raquo; <a href="http://www.spoon-library.com/exceptions/detail/' . $exception->getCode() . '">view documentation</a>';
    }
    // request uri?
    if (!isset($_SERVER['HTTP_HOST'])) {
        $_SERVER['HTTP_HOST'] = '';
    }
    if (!isset($_SERVER['REQUEST_URI'])) {
        $_SERVER['REQUEST_URI'] = '';
    }
    // user agent
    $userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'unknown';
    // generate output
    $output = '
	<html>
		<head>
			<title>' . $name . '</title>
		</head>
		<body style="background-color: #F2F2F2; color: #0000000, font-family: Verdana, Tahoma, Arial; font-size 10px; margin: 0; padding: 0;">
			<table width="100%">
				<tr>
					<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">&nbsp;</td>
					<td style="width: 550px">
						<table width="550px;">
							<tr>
								<td style="background-color: #EEEEEE; border: 1px solid #B2B2B2;">
									<h1 style="font-size: 12px; margin: 5px 5px 12px 5px; padding: 0 0 5px 0; color: #000000; font-family: Verdana, Tahoma, Arial; border-bottom: 1px solid #999999;">' . $name . ' &raquo; Main</h1>
									<table width="550px">
										<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Message</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $exception->getMessage() . '</td>
										</tr>
										<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">File</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . wordwrap($exception->getFile(), 70, '<br />', true) . '</td>
										</tr>
										<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Line</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $exception->getLine() . '</td>
										</tr>
										<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Date</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . date('r') . '</td>
										</tr>
										<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">URL</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . "\n";
    // request URL
    $output .= '							<a href="http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '">http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '</a>' . "\n";
    $output .= '						</td>
										</tr>
										<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Referring URL</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . "\n";
    // referring URL
    if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != '') {
        $output .= '						<a href="' . $_SERVER['HTTP_REFERER'] . '">' . $_SERVER['HTTP_REFERER'] . '</a>' . "\n";
    } else {
        $output .= '						Unknown Referrer' . "\n";
    }
    $output .= '						</td>
										</tr>
										<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Request Method</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $_SERVER['REQUEST_METHOD'] . '</td>
										</tr>
										<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">User-agent</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $userAgent . '</td>
										</tr>';
    // no documentation ?
    if (isset($documentation)) {
        $output .= '				<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Documentation</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $documentation . '</td>
										</tr>';
    }
    // we know about the last error
    if (error_get_last() !== null) {
        // define message
        $error = error_get_last();
        // show output
        $output .= '				<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">PHP error</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $error['message'] . '</td>
										</tr>';
    }
    // continue output
    $output .= '				</table>
								</td>
							<tr>
							<tr>
								<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">&nbsp;</td>
							</tr>
							<!-- variables -->
							<tr>
								<td style="background-color: #EEEEEE; border: 1px solid #B2B2B2;">
									<h1 style="font-size: 12px; margin: 5px 5px 12px 5px; padding: 0 0 5px 0; color: #000000; font-family: Verdana, Tahoma, Arial; border-bottom: 1px solid #999999;">' . $name . ' &raquo; Variables</h1>
									<table width="550px;">' . "\n";
    // $_GET has items
    if (isset($_GET)) {
        $output .= '				<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">$_GET</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">
												<pre style="font-family: Courier; margin-bottom: 10px;">' . print_r($_GET, true) . '</pre>
											</td>
										</tr>' . "\n";
    }
    // $_POST has items
    if (isset($_POST)) {
        $output .= '				<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">$_POST</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">
												<pre style="font-family: Courier; margin-bottom: 10px;">' . print_r($_POST, true) . '</pre>
											</td>
										</tr>';
    }
    // $_SESSION has items
    if (isset($_SESSION)) {
        $output .= '				<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">$_SESSION</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">
												<pre style="font-family: Courier; margin-bottom: 10px;">' . print_r($_SESSION, true) . '</pre>
											</td>
										</tr>';
    }
    // $_COOKIE has items
    if (isset($_COOKIE)) {
        $output .= '				<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">$_COOKIE</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">
												<pre style="font-family: Courier; margin-bottom: 10px;">' . print_r($_COOKIE, true) . '</pre>
											</td>
										</tr>';
    }
    // $_FILES has items
    if (isset($_FILES)) {
        $output .= '				<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">$_FILES</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">
												<pre style="font-family: Courier; margin-bottom: 10px;">' . print_r($_FILES, true) . '</pre>
											</td>
										</tr>';
    }
    $output .= '				</table>
								</td>
							</tr>
							<tr>
								<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">&nbsp;</td>
							</tr>
							<!-- stack -->
							<tr>
								<td style="background-color: #EEEEEE; border: 1px solid #B2B2B2;">
									<h1 style="font-size: 12px; margin: 5px 5px 12px 5px; padding: 0 0 5px 0; color: #000000; font-family: Verdana, Tahoma, Arial; border-bottom: 1px solid #999999;">' . $name . ' &raquo; Trace</h1>
									<table width="550px;">' . "\n";
    // trace has items
    if (count($exception->getTrace()) != 0) {
        // fetch entire stack
        $entireTraceStack = $exception->getTrace();
        // loop elements
        foreach ($entireTraceStack as $traceStack) {
            // open defintion list
            $output .= '			<tr>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">
												<table width="550px;">
													<tr>
														<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">File</th>
														<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . (isset($traceStack['file']) ? wordwrap($traceStack['file'], 70, '<br />', true) : 'Unknown') . '
														</td>
													</tr>
													<tr>
														<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Line</th>
														<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . (isset($traceStack['line']) ? $traceStack['line'] : 'Unknown') . '
														</td>
													</tr>';
            // class & function
            if (isset($traceStack['class'])) {
                $output .= '					<tr>
														<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Class</th>
														<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $traceStack['class'] . '
														</td>
													</tr>';
            }
            if (isset($traceStack['function'])) {
                $output .= '					<tr>
														<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Function</th>
														<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $traceStack['function'] . '													</td>
													</tr>';
            }
            // function arguments
            if (isset($traceStack['args']) && count($traceStack['args']) != 0) {
                // argument title
                $output .= '					<tr>
														<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Argument(s)</th>
														<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">
															<pre style="font-family: Courier; margin-bottom: 10px;">' . print_r($traceStack['args'], true) . '</pre>
														</td>
													</tr>';
            }
            // close defintion list
            $output .= '					</table>
											</td>
										</tr>';
        }
    } else {
        $output .= '				<tr>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">No trace available.</td>
										</tr>';
    }
    // continue output generation
    $output .= '				</table>
								</td>
							</tr>
						</table>
					</td>
					<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">&nbsp;</td>
				</tr>
			</table>
		</body>
	</html>
	';
    // obfuscate
    if (is_callable(array($exception, 'getObfuscate')) && count($exception->getObfuscate()) != 0) {
        $output = str_replace($exception->getObfuscate(), '***', $output);
    }
    // custom callback?
    if (SPOON_EXCEPTION_CALLBACK != '') {
        // function
        if (!strpos(SPOON_EXCEPTION_CALLBACK, '::')) {
            // function actually has been defined
            if (function_exists(SPOON_EXCEPTION_CALLBACK)) {
                call_user_func_array(SPOON_EXCEPTION_CALLBACK, array($exception, $output));
            } else {
                exit('The function stored in SPOON_EXCEPTION_CALLBACK (' . SPOON_EXCEPTION_CALLBACK . ') could not be found.');
            }
        } else {
            // method
            $method = explode('::', SPOON_EXCEPTION_CALLBACK);
            // 2 parameters and exists
            if (count($method) == 2 && is_callable(array($method[0], $method[1]))) {
                call_user_func_array(array($method[0], $method[1]), array($exception, $output));
            } else {
                exit('The method stored in SPOON_EXCEPTION_CALLBACK (' . SPOON_EXCEPTION_CALLBACK . ') cound not be found.');
            }
        }
    } else {
        // debugging enabled (show output)
        if (SPOON_DEBUG) {
            echo $output;
        } else {
            echo SPOON_DEBUG_MESSAGE;
        }
    }
    // mail it?
    if (SPOON_DEBUG_EMAIL != '') {
        // e-mail headers
        $headers = "MIME-Version: 1.0\n";
        $headers .= "Content-type: text/html; charset=iso-8859-15\n";
        $headers .= "X-Priority: 3\n";
        $headers .= "X-MSMail-Priority: Normal\n";
        $headers .= "X-Mailer: SpoonLibrary Webmail\n";
        $headers .= "From: Spoon Library <*****@*****.**>\n";
        // send email
        @mail(SPOON_DEBUG_EMAIL, 'Exception Occured', $output, $headers);
    }
    // stop script execution
    exit;
}
Example #3
0
/**
 * Prints out the thrown exception in a more readable manner for a person using
 * a web browser.
 *
 * @param	SpoonException $exception
 */
function exceptionHandler($exception)
{
    // fetch trace stack
    $trace = $exception->getTrace();
    // specific name
    $name = method_exists($exception, 'getName') ? $exception->getName() : get_class($exception);
    // spoon type exception
    if (method_exists($exception, 'getName') && strtolower(substr($exception->getName(), 0, 5)) == 'spoon' && $exception->getCode() != 0) {
        $documentation = '&raquo; <a href="http://www.spoon-library.com/exceptions/detail/' . $exception->getCode() . '">view documentation</a>';
    }
    // request uri?
    if (!isset($_SERVER['HTTP_HOST'])) {
        $_SERVER['HTTP_HOST'] = '';
    }
    if (!isset($_SERVER['REQUEST_URI'])) {
        $_SERVER['REQUEST_URI'] = '';
    }
    if (!isset($_SERVER['REQUEST_METHOD'])) {
        $_SERVER['REQUEST_METHOD'] = '';
    }
    // user agent
    $userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '<i>(Unknown)</i>';
    // generate output
    $output = '
	<html>
		<head>
			<title>' . $name . '</title>
		</head>
		<body style="background-color: #F2F2F2; color: #0000000, font-family: Verdana, Tahoma, Arial; font-size 10px; margin: 0; padding: 0;">
			<table width="100%">
				<tr>
					<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">&nbsp;</td>
					<td style="width: 550px">
						<table width="550px;">
							<tr>
								<td style="background-color: #EEEEEE; border: 1px solid #B2B2B2;">
									<h1 style="font-size: 12px; margin: 5px 5px 12px 5px; padding: 0 0 5px 0; color: #000000; font-family: Verdana, Tahoma, Arial; border-bottom: 1px solid #999999;">' . $name . ' &raquo; Main</h1>
									<table width="550px">
										<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Message</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $exception->getMessage() . '</td>
										</tr>
										<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">File</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . dimCommonPathPrefix($exception->getFile()) . '</td>
										</tr>
										<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Line</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $exception->getLine() . '</td>
										</tr>
										<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Date</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . date('r') . '</td>
										</tr>
										<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">URL</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . "\n";
    // request URL
    $output .= '							<a href="http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '">http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '</a>' . "\n";
    $output .= '						</td>
										</tr>
										<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Referring URL</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . "\n";
    // referring URL
    if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != '') {
        $output .= '						<a href="' . $_SERVER['HTTP_REFERER'] . '">' . $_SERVER['HTTP_REFERER'] . '</a>' . "\n";
    } else {
        $output .= '						<i>(Unknown)</i>' . "\n";
    }
    $output .= '						</td>
										</tr>
										<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Request Method</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $_SERVER['REQUEST_METHOD'] . '</td>
										</tr>
										<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">User-agent</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $userAgent . '</td>
										</tr>';
    // no documentation ?
    if (isset($documentation)) {
        $output .= '				<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Documentation</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $documentation . '</td>
										</tr>';
    }
    // we know about the last error
    if (error_get_last() !== null) {
        // define message
        $error = error_get_last();
        // show output
        $output .= '				<tr>
											<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">PHP error</th>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $error['message'] . '</td>
										</tr>';
    }
    $output .= '				</table>
								</td>
							</tr>
							<tr>
								<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">&nbsp;</td>
							</tr>
							<!-- stack -->
							<tr>
								<td style="background-color: #EEEEEE; border: 1px solid #B2B2B2;">
									<h1 style="font-size: 12px; margin: 5px 5px 12px 5px; padding: 0 0 5px 0; color: #000000; font-family: Verdana, Tahoma, Arial; border-bottom: 1px solid #999999;">' . $name . ' &raquo; Trace</h1>
									<table width="550px;">' . "\n";
    // trace has items
    if (count($exception->getTrace()) != 0) {
        // fetch entire stack
        $entireTraceStack = $exception->getTrace();
        // loop elements
        foreach ($entireTraceStack as $traceStack) {
            // open defintion list
            $output .= '			<tr>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">
												<table width="550px;" style="border-top: 1px dotted; padding-top: 1ex;">
													<tr>
														<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">File</th>
														<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . (isset($traceStack['file']) ? dimCommonPathPrefix($traceStack['file']) : '<i>(Unknown)</i>') . '
														</td>
													</tr>
													<tr>
														<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Line</th>
														<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . (isset($traceStack['line']) ? $traceStack['line'] : '<i>(Unknown)</i>') . '
														</td>
													</tr>';
            // class & function
            if (isset($traceStack['class'])) {
                $docUrl = 'http://www.google.com/search?btnI=&ie=utf-8&sourceid=navclient&q=' . urlencode(sprintf('site:php.net/manual OR site:www.spoon-library.com/docs class "%s"', $traceStack['class']));
                $link = sprintf('<a href="%s">%s</a>', htmlspecialchars($docUrl), htmlspecialchars($traceStack['class']));
                $output .= '					<tr>
														<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Class</th>
														<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $link . '</td>
													</tr>';
            }
            if (isset($traceStack['function'])) {
                $docUrl = isset($traceStack['class']) ? 'http://www.google.com/search?btnI=&ie=utf-8&sourceid=navclient&q=' . urlencode(sprintf('site:php.net/manual OR site:www.spoon-library.com/docs "%s::%s"', $traceStack['class'], $traceStack['function'])) : 'http://www.google.com/search?btnI=&ie=utf-8&sourceid=navclient&q=' . urlencode(sprintf('site:php.net/manual OR site:www.spoon-library.com/docs "%s"', $traceStack['function']));
                $link = sprintf('<a href="%s">%s</a>', htmlspecialchars($docUrl), htmlspecialchars($traceStack['function']));
                $output .= '					<tr>
														<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Function</th>
														<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">' . $link . '</td>
													</tr>';
            }
            // function arguments
            if (isset($traceStack['args']) && count($traceStack['args']) != 0) {
                // argument title
                $output .= '					<tr>
														<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 0 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">Argument(s)</th>
														<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">
															<pre style="font-family: Courier; margin-bottom: 10px;">' . exceptionHandlerDumper($traceStack['args']) . '</pre>
														</td>
													</tr>';
            }
            // close defintion list
            $output .= '					</table>
											</td>
										</tr>';
        }
    } else {
        $output .= '				<tr>
											<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">No trace available.</td>
										</tr>';
    }
    // output the superglobal variables, if any
    $hasVars = false;
    foreach (array('GET', 'POST', 'COOKIE', 'FILES') as $superGlobal) {
        $hasVars |= count($GLOBALS['_' . $superGlobal]);
    }
    if ($hasVars) {
        $output .= '				</table>
									</td>
								<tr>
								<tr>
									<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">&nbsp;</td>
								</tr>
								<!-- variables -->
								<tr>
									<td style="background-color: #EEEEEE; border: 1px solid #B2B2B2;">
										<h1 style="font-size: 12px; margin: 5px 5px 12px 5px; padding: 0 0 5px 0; color: #000000; font-family: Verdana, Tahoma, Arial; border-bottom: 1px solid #999999;">' . $name . ' &raquo; Variables</h1>
										<table width="550px;">' . "\n";
        foreach (array('GET', 'POST', 'COOKIE', 'FILES') as $superGlobal) {
            if (!empty($GLOBALS['_' . $superGlobal])) {
                $output .= '				<tr>
													<th width="110px" style="vertical-align: top; text-align: left; font-weight: 700; padding: 0 10px 0 10px; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">$_' . $superGlobal . '</th>
													<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">
														<pre style="font-family: Courier; margin-bottom: 10px;">' . exceptionHandlerDumper($GLOBALS['_' . $superGlobal]) . '</pre>
													</td>
												</tr>' . "\n";
            }
        }
    }
    // continue output generation
    $output .= '				</table>
								</td>
							</tr>
						</table>
					</td>
					<td style="vertical-align: top; font-family: Verdana, Tahoma, Arial; font-size: 10px; color: #000000;">&nbsp;</td>
				</tr>
			</table>
		</body>
	</html>
	';
    // obfuscate
    if (method_exists($exception, 'getObfuscate') && count($exception->getObfuscate()) != 0) {
        $output = str_replace($exception->getObfuscate(), '***', $output);
    }
    // custom callback?
    if (Spoon::getExceptionCallback() != '') {
        // function
        if (!strpos(Spoon::getExceptionCallback(), '::')) {
            // function actually has been defined
            if (function_exists(Spoon::getExceptionCallback())) {
                call_user_func_array(Spoon::getExceptionCallback(), array($exception, $output));
            } else {
                exit('The exception callback function (' . Spoon::getExceptionCallback() . ') could not be found.');
            }
        } else {
            // method
            $method = explode('::', Spoon::getExceptionCallback());
            // 2 parameters and exists
            if (count($method) == 2 && is_callable(array($method[0], $method[1]))) {
                call_user_func_array(array($method[0], $method[1]), array($exception, $output));
            } else {
                exit('The exception callback function (' . Spoon::getExceptionCallback() . ') cound not be found.');
            }
        }
    } else {
        // on CLI we have no use for 2000 lines long report, show short info
        if (Spoon::inCli()) {
            echo $name . "\n";
            echo 'Message: ' . $exception->getMessage() . "\n";
            echo 'File: ' . $exception->getFile() . "\n";
            echo 'Line: ' . $exception->getLine() . "\n";
        } elseif (Spoon::getDebug()) {
            echo $output;
        } else {
            echo Spoon::getDebugMessage();
        }
    }
    // mail it?
    if (Spoon::getDebugEmail() != '') {
        // e-mail headers
        $headers = "MIME-Version: 1.0\n";
        $headers .= "Content-type: text/html; charset=iso-8859-15\n";
        $headers .= "X-Priority: 3\n";
        $headers .= "X-MSMail-Priority: Normal\n";
        $headers .= "X-Mailer: SpoonLibrary Webmail\n";
        $headers .= "From: Spoon Library <*****@*****.**>\n";
        // send email
        @mail(Spoon::getDebugEmail(), 'Exception Occured', $output, $headers);
    }
    // stop script execution
    exit;
}
Example #4
0
/**
 * Prints out the thrown exception in a more readable manner
 *
 * @return	void
 * @param	SpoonException $exception
 */
function exceptionHandler($exception)
{
    // fetch trace stack
    $trace = $exception->getTrace();
    // class & function exist and are spoon related
    if (isset($trace[0]['class']) && isset($trace[0]['function']) && strtolower(substr($trace[0]['class'], 0, 5)) == 'spoon') {
        $documentationURL = strtolower($trace[0]['class']) . '/' . strtolower($trace[0]['function']);
        $documentation = '&raquo; <a href="http://docs.spoon-library.be/' . $documentationURL . '">view documentation</a>';
    }
    // specific name
    $name = method_exists($exception, 'getName') ? $exception->getName() : 'UnknownException';
    // request uri?
    if (!isset($_SERVER['HTTP_HOST'])) {
        $_SERVER['HTTP_HOST'] = '';
    }
    if (!isset($_SERVER['REQUEST_URI'])) {
        $_SERVER['REQUEST_URI'] = '';
    }
    // generate output
    $output = '
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
			<title>' . $name . '</title>
			<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
			<style type="text/css">
			body
			{
				background-color: #f2f2f2;
				color: #000;
				font-family: verdana, tahoma, arial;
				font-size: 10px;
				margin: 10px;
				padding: 0;
			}

			#container
			{
				margin: 0 auto;
				width: 550px;
			}

			#container-main, #container-stack, #container-variables
			{
				background-color: #eee;
				border: 1px solid #b2b2b2;
				margin: 0 0 10px 0;
			}

			#main, #stack, #variables
			{
				margin: 10px 10px 10px 10px;
			}

			#main h1, #stack h1, #variables h1
			{
				font-size: 12px;
				margin: 0 0 10px 0;
				padding: 0;
			}

			#main dl, #stack dl, #variables dl
			{
				border-top: 1px solid #999;
				margin: 0;
				padding: 5px 0 0 0;
			}

			#main dt, #stack dt, #variables dt
			{
				float: left;
				font-weight: bold;
				margin: 0;
				padding: 0;
				text-align: right;
			}

			#main dd, #stack dd, #variables dd
			{
				margin: 0 0 5px 100px;
				padding: 0;
			}

			#main dd pre, #stack dd pre, #variables dd pre
			{
				font-family: verdana, tahoma, arial;
				font-size: 10px;
				margin: 0;
				padding: 0;
			}
			</style>
		</head>

		<body>
			<div id="container">

				<!-- main -->
				<div id="container-main">
					<div id="main">
						<h1>' . $name . ' &raquo; Main</h1>
						<dl>
							<dt>Message</dt>
								<dd>' . $exception->getMessage() . '</dd>
							<dt>File</dt>
								<dd>' . wordwrap($exception->getFile(), 70, '<br />', true) . '</dd>
							<dt>Line</dt>
								<dd>' . $exception->getLine() . '</dd>
							<dt>Date</dt>
								<dd>' . date('r') . '</dd>
							<dt>URL</dt>
								<dd>';
    // request url
    $output .= '<a href="http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '">http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . '</a>';
    $output .= '</dd>
							<dt>Referring URL</dt>
								<dd>';
    // referring url
    $output .= isset($_SERVER['HTTP_REFERER']) ? '<a href="' . $_SERVER['HTTP_REFERER'] . '">' . $_SERVER['HTTP_REFERER'] . '</a>' : 'Unknown Referrer';
    $output .= '</dd>
							<dt>Request Method</dt>
								<dd>' . $_SERVER['REQUEST_METHOD'] . '</dd>';
    // no documentation ?
    if (isset($documentation)) {
        $output .= '<dt>Documentation</dt>
													<dd>' . $documentation . '</dd>';
    }
    // continue output
    $output .= '
						</dl>
					</div>
				</div>

				<!-- variables -->
					<div id="container-variables">
						<div id="variables">
							<h1>' . $name . ' &raquo; Variables</h1>';
    // $_GET has items
    if (isset($_GET)) {
        // open defition list
        $output .= "<dl>\r\n";
        // title + array
        $output .= "<dt>\$_GET</dt>\r\n<dd><pre>" . print_r($_GET, true) . "</pre></dd>\r\n";
        // close definition list
        $output .= "</dl>\r\n";
    }
    // $_POST has items
    if (isset($_POST)) {
        // open defition list
        $output .= "<dl>\r\n";
        // title + array
        $output .= "<dt>\$_POST</dt>\r\n<dd><pre>" . print_r($_POST, true) . "</pre></dd>\r\n";
        // close definition list
        $output .= "</dl>\r\n";
    }
    // $_SESSION has items
    if (isset($_SESSION)) {
        // open defition list
        $output .= "<dl>\r\n";
        // title + array
        $output .= "<dt>\$_SESSION</dt>\r\n<dd><pre>" . print_r($_SESSION, true) . "</pre></dd>\r\n";
        // close definition list
        $output .= "</dl>\r\n";
    }
    // $_COOKIE has items
    if (isset($_COOKIE)) {
        // open defition list
        $output .= "<dl>\r\n";
        // title + array
        $output .= "<dt>\$_COOKIE</dt>\r\n<dd><pre>" . print_r($_COOKIE, true) . "</pre></dd>\r\n";
        // close definition list
        $output .= "</dl>\r\n";
    }
    // $_FILES has items
    if (isset($_FILES)) {
        // open defition list
        $output .= "<dl>\r\n";
        // title + array
        $output .= "<dt>\$_FILES</dt>\r\n<dd><pre>" . print_r($_FILES, true) . "</pre></dd>\r\n";
        // close definition list
        $output .= "</dl>\r\n";
    }
    $output .= '
						</div>
					</div>

				<!-- stack -->
				<div id="container-stack">
					<div id="stack">
						<h1>' . $name . ' &raquo; Trace</h1>';
    // trace has items
    if (count($exception->getTrace()) != 0) {
        // fetch entire stack
        $entireTraceStack = $exception->getTrace();
        // loop elements
        foreach ($entireTraceStack as $traceStack) {
            // open defintion list
            $output .= "<dl>\r\n";
            // file & line
            $output .= "<dt>File</dt>\r\n";
            $output .= '<dd>' . (isset($traceStack['file']) ? wordwrap($traceStack['file'], 70, '<br />', true) : 'Unknown') . "</dd>\r\n";
            $output .= "<dt>Line</dt>\r\n";
            $output .= '<dd>' . (isset($traceStack['line']) ? $traceStack['line'] : 'Unknown') . "</dd>\r\n";
            // class & function
            if (isset($traceStack['class'])) {
                $output .= "<dt>Class</dt>\r\n<dd>" . $traceStack['class'] . "</dd>\r\n";
            }
            if (isset($traceStack['function'])) {
                $output .= "<dt>Function</dt>\r\n<dd>" . $traceStack['function'] . "</dd>\r\n";
            }
            // function arguments
            if (isset($traceStack['args']) && count($traceStack['args']) != 0) {
                // argument title
                $output .= "<dt>Argument(s)</dt>\r\n<dd><pre>" . print_r($traceStack['args'], true) . "</pre></dd>\r\n";
            }
            // close defintion list
            $output .= "</dl>\r\n";
        }
    } else {
        $output .= 'No trace available.';
    }
    // continue output generation
    $output .= '
						</div>
					</div>
				</div>
			</body>
		</html>
		';
    // obfuscate
    if (method_exists($exception, 'getObfuscate') && count($exception->getObfuscate()) != 0) {
        $output = str_replace($exception->getObfuscate(), '***', $output);
    }
    // debugging enabled (show output)
    if (SPOON_DEBUG) {
        echo $output;
    } else {
        echo SPOON_DEBUG_MESSAGE;
    }
    // mail it?
    if (SPOON_DEBUG_EMAIL != '') {
        // e-mail headers
        $headers = "MIME-Version: 1.0\n";
        $headers .= "Content-type: text/html; charset=iso-8859-15\n";
        $headers .= "X-Priority: 3\n";
        $headers .= "X-MSMail-Priority: Normal\n";
        $headers .= "X-Mailer: SpoonLibrary Webmail\n";
        $headers .= "From: Spoon Library <*****@*****.**>\n";
        // send email
        @mail(SPOON_DEBUG_EMAIL, 'Exception Occured', $output, $headers);
    }
    // stop script execution
    exit;
}