summaryrefslogtreecommitdiff
path: root/lib/phptal/PHPTAL/ExceptionHandler.php
diff options
context:
space:
mode:
authoremkael <emkael@tlen.pl>2016-10-31 21:58:33 +0100
committeremkael <emkael@tlen.pl>2016-10-31 21:59:22 +0100
commitd216b3147bc3f37cf2337acab5767c6a4f74aa2e (patch)
tree6090989e5071db101a1112131e2b075a02dccbc4 /lib/phptal/PHPTAL/ExceptionHandler.php
parentb23bfbb17d1d5f6852a1690f246a84c2d38ae969 (diff)
* PHPTAL library
Diffstat (limited to 'lib/phptal/PHPTAL/ExceptionHandler.php')
-rw-r--r--lib/phptal/PHPTAL/ExceptionHandler.php81
1 files changed, 81 insertions, 0 deletions
diff --git a/lib/phptal/PHPTAL/ExceptionHandler.php b/lib/phptal/PHPTAL/ExceptionHandler.php
new file mode 100644
index 0000000..dca7bb7
--- /dev/null
+++ b/lib/phptal/PHPTAL/ExceptionHandler.php
@@ -0,0 +1,81 @@
+<?php
+/**
+ * PHPTAL templating engine
+ *
+ * PHP Version 5
+ *
+ * @category HTML
+ * @package PHPTAL
+ * @author Kornel LesiƄski <kornel@aardvarkmedia.co.uk>
+ * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
+ * @version SVN: $Id: $
+ * @link http://phptal.org/
+ */
+
+class PHPTAL_ExceptionHandler
+{
+ private $encoding;
+ function __construct($encoding)
+ {
+ $this->encoding = $encoding;
+ }
+
+ /**
+ * PHP's default exception handler allows error pages to be indexed and can reveal too much information,
+ * so if possible PHPTAL sets up its own handler to fix this.
+ *
+ * Doesn't change exception handler if non-default one is set.
+ *
+ * @param Exception e exception to re-throw and display
+ *
+ * @return void
+ * @throws Exception
+ */
+ public static function handleException(Exception $e, $encoding)
+ {
+ // PHPTAL's handler is only useful on fresh HTTP response
+ if (PHP_SAPI !== 'cli' && !headers_sent()) {
+ $old_exception_handler = set_exception_handler(array(new PHPTAL_ExceptionHandler($encoding), '_defaultExceptionHandler'));
+
+ if ($old_exception_handler !== NULL) {
+ restore_exception_handler(); // if there's user's exception handler, let it work
+ }
+ }
+ throw $e; // throws instead of outputting immediately to support user's try/catch
+ }
+
+
+ /**
+ * Generates simple error page. Sets appropriate HTTP status to prevent page being indexed.
+ *
+ * @param Exception e exception to display
+ */
+ public function _defaultExceptionHandler($e)
+ {
+ if (!headers_sent()) {
+ header('HTTP/1.1 500 PHPTAL Exception');
+ header('Content-Type:text/html;charset='.$this->encoding);
+ }
+
+ $line = $e->getFile();
+ if ($e->getLine()) {
+ $line .= ' line '.$e->getLine();
+ }
+
+ if (ini_get('display_errors')) {
+ $title = get_class($e).': '.htmlspecialchars($e->getMessage());
+ $body = "<p><strong>\n".htmlspecialchars($e->getMessage()).'</strong></p>' .
+ '<p>In '.htmlspecialchars($line)."</p><pre>\n".htmlspecialchars($e->getTraceAsString()).'</pre>';
+ } else {
+ $title = "PHPTAL Exception";
+ $body = "<p>This page cannot be displayed.</p><hr/>" .
+ "<p><small>Enable <code>display_errors</code> to see detailed message.</small></p>";
+ }
+
+ echo "<!DOCTYPE html><html xmlns='http://www.w3.org/1999/xhtml'><head><style>body{font-family:sans-serif}</style><title>\n";
+ echo $title.'</title></head><body><h1>PHPTAL Exception</h1>'.$body;
+ error_log($e->getMessage().' in '.$line);
+ echo '</body></html>'.str_repeat(' ', 100)."\n"; // IE won't display error pages < 512b
+ exit(1);
+ }
+}