Added better error handling
* Nice HTML output for errors * Cleaned up some @todo's while creating some new ones * Added a method for setting the request variable for the module that only the Controller can use * Fixed that nasty infinite loop, even though it's not ideal yet.
This commit is contained in:
parent
a3e16f6006
commit
b1d7ef7645
2 changed files with 109 additions and 52 deletions
|
@ -19,9 +19,9 @@
|
|||
* Controller Class
|
||||
*
|
||||
* The heavy lifter of PICKLES, makes the calls to get the session and
|
||||
* configuration loaded. Loads modules, serves up user authentication when
|
||||
* the module asks for it, and loads the viewer that the module requested.
|
||||
* Default values are present to make things easier on the user.
|
||||
* configuration loaded. Loads modules, serves up user authentication when the
|
||||
* module asks for it, and loads the viewer that the module requested. Default
|
||||
* values are present to make things easier on the user.
|
||||
*
|
||||
* @usage <code>new Controller($config);</code>
|
||||
*/
|
||||
|
@ -30,33 +30,24 @@ class Controller extends Object
|
|||
/**
|
||||
* Constructor
|
||||
*
|
||||
* To make life a bit easier when using PICKLES, the Controller logic
|
||||
* is executed automatically via use of a constructor.
|
||||
* To make life a bit easier when using PICKLES, the Controller logic is
|
||||
* executed automatically via use of a constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
// Generate a generic "site down" message
|
||||
if ($this->config->site['disabled']) {
|
||||
exit('
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>' . $_SERVER['SERVER_NAME'] . '</title>
|
||||
<style>
|
||||
html{background:#eee;font-family:Verdana;width:100%}
|
||||
body{background:#fff;padding:20px;-moz-border-radius:20px;-webkit-border-radius:20px;width:550px;margin:0 auto;margin-top:100px;text-align:center}
|
||||
h1{font-size:1.5em;color:#3a6422;text-shadow:#070d04 1px 1px 1px;margin:0}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>' . $_SERVER['SERVER_NAME'] . ' is currently down for maintenance</h1>
|
||||
</body>
|
||||
</html>
|
||||
');
|
||||
if ($this->config->site['disabled'])
|
||||
{
|
||||
$this->fatalError($_SERVER['SERVER_NAME'] . ' is currently<br />down for maintenance');
|
||||
}
|
||||
|
||||
// Ack, not sure what page to load, throw an error
|
||||
if (!isset($_REQUEST['request']) && $this->config->module['default'] == null)
|
||||
{
|
||||
$this->fatalError('Unable complete this request because no URI was specified and there is no default module specified in config.ini');
|
||||
}
|
||||
|
||||
// Loads the requested module's information
|
||||
if (isset($_REQUEST['request']) && trim($_REQUEST['request']) != '')
|
||||
{
|
||||
|
@ -72,8 +63,7 @@ class Controller extends Object
|
|||
// Checks if an ID (integer) was passed in
|
||||
if (preg_match('/^\d*$/', $last_part) == 1)
|
||||
{
|
||||
// @todo what variable should this be?
|
||||
$_REQUEST['id'] = $last_part;
|
||||
$requested_id = $last_part;
|
||||
array_pop($request);
|
||||
}
|
||||
else
|
||||
|
@ -166,8 +156,8 @@ class Controller extends Object
|
|||
$display = new $display_class($module->template, $template_basename);
|
||||
|
||||
// If there's no valid module or template redirect
|
||||
// @todo can cause infinite loop...
|
||||
if (!$module_exists && !$display->templateExists())
|
||||
// @todo The == 1 portion needs to be refactored as it's not mandatory to use a parent template
|
||||
if (!$module_exists && (!$display->templateExists() || $display->templateExists() == 1))
|
||||
{
|
||||
header('Location: /', 404);
|
||||
}
|
||||
|
@ -177,6 +167,11 @@ class Controller extends Object
|
|||
// Attempts to execute the default method
|
||||
if (method_exists($module, '__default'))
|
||||
{
|
||||
if (isset($requested_id))
|
||||
{
|
||||
$module->setRequest(array('id' => $requested_id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Note to Self: When building in caching will need to let the
|
||||
* module know to use the cache, either passing in a variable
|
||||
|
@ -190,6 +185,36 @@ class Controller extends Object
|
|||
// Renders the content
|
||||
$display->render();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fatal Error
|
||||
*
|
||||
* Displays a friendly error to the user via HTML then exits. This method
|
||||
* is used internally and not meant for use by site level code.
|
||||
*
|
||||
* @access private
|
||||
* @param string $message the message to be displayed to the user
|
||||
*/
|
||||
private function fatalError($message)
|
||||
{
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Error - <?=$_SERVER['SERVER_NAME'];?></title>
|
||||
<style>
|
||||
html{background:#eee;font-family:Verdana;width:100%}
|
||||
body{background:#ff9c9c;padding:20px;-moz-border-radius:20px;-webkit-border-radius:20px;width:550px;margin:0 auto;margin-top:100px;text-align:center;border:3px solid #890f0f}
|
||||
h1{font-size:1.5em;color:#600;text-shadow:#a86767 2px 2px 2px;margin:0}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1><?=$message;?></h1>
|
||||
</body>
|
||||
</html>
|
||||
<?php
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
@ -18,13 +18,12 @@
|
|||
/**
|
||||
* Module Class
|
||||
*
|
||||
* This is a parent class that all PICKLES modules should be extending.
|
||||
* Each module can specify it's own meta data and whether or not a user
|
||||
* must be properly authenticated to view the page. Currently any pages
|
||||
* without a template are treated as pages being requested via AJAX and the
|
||||
* return will be JSON encoded. In the future this may need to be changed
|
||||
* out for logic that allows the requested module to specify what display
|
||||
* type(s) it can use.
|
||||
* This is a parent class that all PICKLES modules should be extending. Each
|
||||
* module can specify it's own meta data and whether or not a user must be
|
||||
* properly authenticated to view the page. Currently any pages without a
|
||||
* template are treated as pages being requested via AJAX and the return will
|
||||
* be JSON encoded. In the future this may need to be changed out for logic
|
||||
* that allows the requested module to specify what display type(s) it can use.
|
||||
*/
|
||||
class Module extends Object
|
||||
{
|
||||
|
@ -92,8 +91,8 @@ class Module extends Object
|
|||
/**
|
||||
* AJAX
|
||||
*
|
||||
* Whether or not the page must be loaded via AJAX and if so, what
|
||||
* pages are allowed to access it and the request method.
|
||||
* Whether or not the page must be loaded via AJAX and if so, what pages
|
||||
* are allowed to access it and the request method.
|
||||
*
|
||||
* @access protected
|
||||
* @var boolean or array, null by default
|
||||
|
@ -104,8 +103,8 @@ class Module extends Object
|
|||
/**
|
||||
* Default display engine
|
||||
*
|
||||
* Defaults to null but could be set to Smarty, JSON, XML or RSS. Value
|
||||
* is overwritten by the config value if not set by the module.
|
||||
* Defaults to null but could be set to Smarty, JSON, XML or RSS. Value is
|
||||
* overwritten by the config value if not set by the module.
|
||||
*
|
||||
* @access protected
|
||||
* @var string, null by default
|
||||
|
@ -115,24 +114,35 @@ class Module extends Object
|
|||
/**
|
||||
* Default template
|
||||
*
|
||||
* Defaults to null but could be set to any valid template basename.
|
||||
* The value is overwritten by the config value if not set by the
|
||||
* module. The display engine determines what the file extension
|
||||
* should be.
|
||||
* Defaults to null but could be set to any valid template basename. The
|
||||
* value is overwritten by the config value if not set by the module. The
|
||||
* display engine determines what the file extension should be.
|
||||
*
|
||||
* @access protected
|
||||
* @var string, null by default
|
||||
*/
|
||||
protected $template = null;
|
||||
|
||||
/**
|
||||
* Request Data
|
||||
*
|
||||
* Modules should not interact with $_REQUEST, $_POST or $_GET directly as
|
||||
* the contents could be unsafe. The Controller cleanses this data and sets
|
||||
* it into this variable for safe access by the module.
|
||||
*
|
||||
* @access protected
|
||||
* @var array, null by default
|
||||
* @todo Currently the super globals are not being cleared out
|
||||
*/
|
||||
protected $request = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* The constructor does nothing by default but can be passed a boolean
|
||||
* variable to tell it to automatically run the __default() method.
|
||||
* This is typically used when a module is called outside of the scope
|
||||
* of the controller (the registration page calls the login page in
|
||||
* this manner.
|
||||
* variable to tell it to automatically run the __default() method. This is
|
||||
* typically used when a module is called outside of the scope of the
|
||||
* controller (the registration page calls the login page in this manner.
|
||||
*
|
||||
* @param boolean $autorun optional flag to autorun __default()
|
||||
*/
|
||||
|
@ -152,10 +162,10 @@ class Module extends Object
|
|||
* Default "Magic" Method
|
||||
*
|
||||
* This function is overloaded by the module. The __default() method is
|
||||
* where you want to place any code that needs to be executed at
|
||||
* runtime. The reason the code isn't in the constructor is because the
|
||||
* module must be instantiated before the code is executed so that the
|
||||
* controller script is aware of the authentication requirements.
|
||||
* where you want to place any code that needs to be executed at runtime.
|
||||
* The reason the code isn't in the constructor is because the module must
|
||||
* be instantiated before the code is executed so that the controller
|
||||
* script is aware of the authentication requirements.
|
||||
*/
|
||||
public function __default()
|
||||
{
|
||||
|
@ -180,8 +190,8 @@ class Module extends Object
|
|||
/**
|
||||
* Magic Getter Method
|
||||
*
|
||||
* Attempts to load the module variable. If it's not set, will attempt
|
||||
* to load from the config.
|
||||
* Attempts to load the module variable. If it's not set, will attempt to
|
||||
* load from the config.
|
||||
*
|
||||
* @param string $name name of the variable requested
|
||||
* @return mixed value of the variable or boolean false
|
||||
|
@ -217,6 +227,28 @@ class Module extends Object
|
|||
|
||||
return $this->$name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Request
|
||||
*
|
||||
* @param array $request data to be loaded into the request variable
|
||||
* @return boolean whether or not the assignment was successful
|
||||
*/
|
||||
public function setRequest($request)
|
||||
{
|
||||
$backtrace = debug_backtrace();
|
||||
|
||||
if ($backtrace[1]['class'] == 'Controller')
|
||||
{
|
||||
$this->request = $request;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
trigger_error('Only Controller can perform setRequest()', E_USER_ERROR);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue