Reworked error handling.
This commit is contained in:
parent
69b14085b4
commit
d5a2753c20
1 changed files with 67 additions and 109 deletions
|
@ -38,18 +38,10 @@ class Router extends Object
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
|
$response = new Response();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Catches requests that aren't lowercase
|
|
||||||
$lowercase_request = strtolower($_REQUEST['request']);
|
|
||||||
|
|
||||||
if ($_REQUEST['request'] != $lowercase_request)
|
|
||||||
{
|
|
||||||
// @todo Rework the Browser class to handle the 301 (perhaps redirect301()) to not break other code
|
|
||||||
header('Location: ' . substr_replace($_SERVER['REQUEST_URI'], $lowercase_request, 1, strlen($lowercase_request)), true, 301);
|
|
||||||
throw new Exception();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Grabs the requested page
|
// Grabs the requested page
|
||||||
$request = $_REQUEST['request'];
|
$request = $_REQUEST['request'];
|
||||||
$components = explode('/', $request);
|
$components = explode('/', $request);
|
||||||
|
@ -70,121 +62,87 @@ class Router extends Object
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Creates our class name
|
||||||
array_unshift($nouns, $version);
|
array_unshift($nouns, $version);
|
||||||
|
|
||||||
$class = implode('_', $nouns);
|
$class = implode('_', $nouns);
|
||||||
|
|
||||||
|
// Creates our filename
|
||||||
array_unshift($nouns, SITE_MODULE_PATH);
|
array_unshift($nouns, SITE_MODULE_PATH);
|
||||||
|
|
||||||
$filename = implode('/', $nouns) . '.php';
|
$filename = implode('/', $nouns) . '.php';
|
||||||
|
|
||||||
if (file_exists($filename))
|
if (!file_exists($filename))
|
||||||
{
|
{
|
||||||
if (class_exists($class))
|
throw new Exception('Cannot find the file ' . $filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!class_exists($class))
|
||||||
|
{
|
||||||
|
throw new Exception('Cannot find the class ' . $class);
|
||||||
|
}
|
||||||
|
|
||||||
|
$resource = new $class($uids);
|
||||||
|
|
||||||
|
// Determines if we need to serve over HTTP or HTTPS
|
||||||
|
if ($resource->secure == false && isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'])
|
||||||
|
{
|
||||||
|
throw new Exception('This resource expects HTTPS communication.');
|
||||||
|
}
|
||||||
|
elseif ($resource->secure == true && (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == false))
|
||||||
|
{
|
||||||
|
throw new Exception('This resource expects HTTP communication.');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gets the profiler status
|
||||||
|
$profiler = $this->config->pickles['profiler'];
|
||||||
|
$profiler = $profiler === true || stripos($profiler, 'timers') !== false;
|
||||||
|
|
||||||
|
$method = strtolower($_SERVER['REQUEST_METHOD']);
|
||||||
|
|
||||||
|
if (!method_exists($resource, $method))
|
||||||
|
{
|
||||||
|
throw new Exception('Cannot find the method ' . $class . '::' . $method);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Starts a timer before the resource is executed
|
||||||
|
if ($profiler)
|
||||||
|
{
|
||||||
|
Profiler::timer('resource ' . $method);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($resource->validate)
|
||||||
|
{
|
||||||
|
$validation_errors = $resource->__validate();
|
||||||
|
|
||||||
|
if ($validation_errors)
|
||||||
{
|
{
|
||||||
$resource = new $class($uids);
|
$response->status = 400;
|
||||||
|
$response->message = implode(' ', $validation_errors);
|
||||||
// Determines if we need to serve over HTTP or HTTPS
|
|
||||||
if ($resource->secure == false && isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'])
|
|
||||||
{
|
|
||||||
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], true, 301);
|
|
||||||
throw new Exception();
|
|
||||||
}
|
|
||||||
elseif ($resource->secure == true && (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] == false))
|
|
||||||
{
|
|
||||||
header('Location: https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], true, 301);
|
|
||||||
throw new Exception();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Checks for the PHPSESSID in the query string
|
|
||||||
if (stripos($_SERVER['REQUEST_URI'], '?PHPSESSID=') === false)
|
|
||||||
{
|
|
||||||
// XHTML compliancy stuff
|
|
||||||
// @todo Wonder if this could be yanked now that we're in HTML5 land
|
|
||||||
ini_set('arg_separator.output', '&');
|
|
||||||
ini_set('url_rewriter.tags', 'a=href,area=href,frame=src,input=src,fieldset=');
|
|
||||||
|
|
||||||
// @todo Will want to generate the header based on if we're pushing documentation or API
|
|
||||||
header('Content-type: text/html; charset=UTF-8');
|
|
||||||
// header('Content-type: application/json');
|
|
||||||
//header('Content-type: application/json; charset=UTF-8');
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Redirect so Google knows to index the page without the session ID
|
|
||||||
list($request_uri, $phpsessid) = explode('?PHPSESSID=', $_SERVER['REQUEST_URI'], 2);
|
|
||||||
header('HTTP/1.1 301 Moved Permanently');
|
|
||||||
header('Location: ' . $request_uri);
|
|
||||||
|
|
||||||
throw new Exception('Requested URI contains PHPSESSID, redirecting.');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gets the profiler status
|
|
||||||
$profiler = $this->config->pickles['profiler'];
|
|
||||||
$profiler = $profiler === true || stripos($profiler, 'timers') !== false;
|
|
||||||
|
|
||||||
$method = strtolower($_SERVER['REQUEST_METHOD']);
|
|
||||||
|
|
||||||
if (method_exists($resource, $method))
|
|
||||||
{
|
|
||||||
// Starts a timer before the resource is executed
|
|
||||||
if ($profiler)
|
|
||||||
{
|
|
||||||
Profiler::timer('resource ' . $method);
|
|
||||||
}
|
|
||||||
|
|
||||||
$response = new Response();
|
|
||||||
|
|
||||||
if ($resource->validate)
|
|
||||||
{
|
|
||||||
$validation_errors = $resource->__validate();
|
|
||||||
|
|
||||||
if ($validation_errors)
|
|
||||||
{
|
|
||||||
$response->status = 400;
|
|
||||||
$response->message = implode(' ', $validation_errors);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($response->status == 200)
|
|
||||||
{
|
|
||||||
$resource_return = $resource->$method();
|
|
||||||
|
|
||||||
if ($resource_return)
|
|
||||||
{
|
|
||||||
$response->response = $resource_return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stops the resource timer
|
|
||||||
if ($profiler)
|
|
||||||
{
|
|
||||||
Profiler::timer('resource ' . $method);
|
|
||||||
}
|
|
||||||
|
|
||||||
$response->respond();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new Exception('Missing method');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new Exception('Missing class');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
if ($response->status == 200)
|
||||||
{
|
{
|
||||||
throw new Exception('Missing file');
|
$resource_return = $resource->$method();
|
||||||
|
|
||||||
|
if ($resource_return)
|
||||||
|
{
|
||||||
|
$response->response = $resource_return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stops the resource timer
|
||||||
|
if ($profiler)
|
||||||
|
{
|
||||||
|
Profiler::timer('resource ' . $method);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception $e)
|
catch (Exception $e)
|
||||||
{
|
{
|
||||||
// @todo
|
$response->status = 500;
|
||||||
exit('fuuuu');
|
$response->message = $e->getMessage();
|
||||||
$output = $e->getMessage();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$response->respond();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue