From 2f527230d01f4fcd2d140dae462ae7bbd00a78a5 Mon Sep 17 00:00:00 2001 From: avl Date: Tue, 16 Sep 2008 16:51:41 +0300 Subject: [PATCH] Custom Error Page --- ipf/http/response/notfound.php | 9 +++ ipf/http/response/servererror.php | 4 +- ipf/http/response/servererrordebug.php | 2 - ipf/router.php | 96 ++++++++++++-------------- 4 files changed, 57 insertions(+), 54 deletions(-) diff --git a/ipf/http/response/notfound.php b/ipf/http/response/notfound.php index 4abbd24..f965ab2 100644 --- a/ipf/http/response/notfound.php +++ b/ipf/http/response/notfound.php @@ -4,6 +4,15 @@ class IPF_HTTP_Response_NotFound extends IPF_HTTP_Response { function __construct($content='404 Not Found', $mimetype=null) { + try{ + $context = array( + 'title'=>'404 Not Found', + 'query_string'=>$_SERVER['QUERY_STRING'], + 'MEDIA_URL'=>IPF::get('media_url'), + 'ADMIN_MEDIA_URL'=>IPF::get('admin_media_url'), + ); + $content = IPF_Shortcuts::RenderToString('404.html', $context); + }catch(IPF_Exception $e){} parent::__construct($content, $mimetype); $this->status_code = 404; } diff --git a/ipf/http/response/servererror.php b/ipf/http/response/servererror.php index 7eeea86..80a57a4 100644 --- a/ipf/http/response/servererror.php +++ b/ipf/http/response/servererror.php @@ -2,9 +2,9 @@ class IPF_HTTP_Response_ServerError extends IPF_HTTP_Response { - function __construct($content='Server Error', $mimetype=null) + function __construct($e, $mimetype=null) { - parent::__construct($content, $mimetype); + parent::__construct('

500 Server Error

Our apologies…

Please return later

', $mimetype); $this->status_code = 500; } } diff --git a/ipf/http/response/servererrordebug.php b/ipf/http/response/servererrordebug.php index f300c73..04986f2 100644 --- a/ipf/http/response/servererrordebug.php +++ b/ipf/http/response/servererrordebug.php @@ -9,8 +9,6 @@ class IPF_HTTP_Response_ServerErrorDebug extends IPF_HTTP_Response } } - - function IPF_HTTP_Response_ServerErrorDebug_Pretty($e) { $o = create_function('$in','return htmlspecialchars($in);'); diff --git a/ipf/router.php b/ipf/router.php index a8c7a4f..5bb0c5a 100644 --- a/ipf/router.php +++ b/ipf/router.php @@ -2,42 +2,54 @@ class IPF_Router { + public static function response500($e){ + if (IPF::get('debug')) + return new IPF_HTTP_Response_ServerErrorDebug($e); + else + return new IPF_HTTP_Response_ServerError($e); + } + public static function dispatch($query='') { - $query = preg_replace('#^(/)+#', '/', '/'.$query); - $req = new IPF_HTTP_Request($query); - $middleware = array(); - foreach (IPF::get('middlewares', array()) as $mw) { - $middleware[] = new $mw(); - } - $skip = false; - foreach ($middleware as $mw) { - if (method_exists($mw, 'processRequest')) { - $response = $mw->processRequest($req); - if ($response !== false) { - // $response is a response - $response->render($req->method != 'HEAD' and !defined('IN_UNIT_TESTS')); - $skip = true; - break; - } - } - } - if ($skip === false) { - $response = IPF_Router::match($req); - if (!empty($req->response_vary_on)) { - $response->headers['Vary'] = $req->response_vary_on; + try{ + $query = preg_replace('#^(/)+#', '/', '/'.$query); + $req = new IPF_HTTP_Request($query); + $middleware = array(); + foreach (IPF::get('middlewares', array()) as $mw) { + $middleware[] = new $mw(); } - $middleware = array_reverse($middleware); + $skip = false; foreach ($middleware as $mw) { - if (method_exists($mw, 'processResponse')) { - $response = $mw->processResponse($req, $response); - } + if (method_exists($mw, 'processRequest')) { + $response = $mw->processRequest($req); + if ($response !== false) { + // $response is a response + $response->render($req->method != 'HEAD' and !defined('IN_UNIT_TESTS')); + $skip = true; + break; + } + } + } + if ($skip === false) { + $response = IPF_Router::match($req); + if (!empty($req->response_vary_on)) { + $response->headers['Vary'] = $req->response_vary_on; + } + $middleware = array_reverse($middleware); + foreach ($middleware as $mw) { + if (method_exists($mw, 'processResponse')) { + $response = $mw->processResponse($req, $response); + } + } + //var_dump($response); + $response->render($req->method != 'HEAD'); } - //var_dump($response); - $response->render($req->method != 'HEAD'); + return array($req, $response); + } catch (IPF_Exception $e) { + $response = IPF_Router::response500($e); + $response->render(); } - return array($req, $response); - } + } public static function match($req) { @@ -45,34 +57,18 @@ class IPF_Router $prefix = $url['prefix']; foreach ($url['urls'] as $suburl){ $regex = $prefix.$suburl['regex']; - //print "Regex = $regex
"; - //print "Query = {$req->query}
"; $match = array(); if (preg_match($regex, $req->query, $match)) { try{ IPF::loadFunction($suburl['func']); $r = $suburl['func']($req, $match); - if (!is_a($r,'IPF_HTTP_Response')) - return new IPF_HTTP_Response_ServerError('function '.$suburl['func'].'() must return IPF_HTTP_Response instance'); + if (!is_a($r,'IPF_HTTP_Response')){ + return IPF_Router::response500(new IPF_Exception('function '.$suburl['func'].'() must return IPF_HTTP_Response instance')); + } return $r; } catch (IPF_Exception $e) { - return new IPF_HTTP_Response_ServerErrorDebug($e); - } - /* - try{ - IPF::loadFunction($suburl['func']); - return $suburl['func']($req, $match); - } catch (IPF_HTTP_Error404 $e) { - // Need to add a 404 error handler - // something like IPF::get('404_handler', 'class::method') - } catch (Exception $e) { - if (IPF::get('debug', false) == true) { - return new IPF_HTTP_Response_ServerErrorDebug($e); - } else { - return new IPF_HTTP_Response_ServerError($e); - } + return IPF_Router::response500($e); } - */ } } } -- 2.49.0