Started gutting logic from the Controller and Display classes.
This commit is contained in:
parent
1381fd82a0
commit
2fa2b6ad03
3 changed files with 21 additions and 299 deletions
|
@ -38,55 +38,8 @@ class Controller extends Object
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
// Generate a generic "site down" message if the site is set to be disabled
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// @todo Clean this up to be just a single sanity check
|
|
||||||
if (isset($this->config->pickles['disabled']) && $this->config->pickles['disabled'])
|
|
||||||
{
|
|
||||||
$custom_template = SITE_TEMPLATE_PATH . '__shared/maintenance.phtml';
|
|
||||||
|
|
||||||
if (file_exists($custom_template))
|
|
||||||
{
|
|
||||||
require_once $custom_template;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
echo '
|
|
||||||
<h1>Down for Maintenance</h1>
|
|
||||||
<p>
|
|
||||||
' . $_SERVER['SERVER_NAME'] . ' is currently down for maintenance.
|
|
||||||
Please check back in a few minutes.
|
|
||||||
</p>
|
|
||||||
<p>Additionally, a custom maintenance template was not found.</p>
|
|
||||||
<hr>
|
|
||||||
<em>Powered by <a href="https://github.com/joshtronic/pickles">PICKLES</a></em>
|
|
||||||
';
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Exception();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Checks for attributes passed in the URI
|
|
||||||
if (strstr($_REQUEST['request'], ':'))
|
|
||||||
{
|
|
||||||
$parts = explode('/', $_REQUEST['request']);
|
|
||||||
$_REQUEST['request'] = '';
|
|
||||||
|
|
||||||
foreach ($parts as $part)
|
|
||||||
{
|
|
||||||
if (strstr($part, ':'))
|
|
||||||
{
|
|
||||||
list($variable, $value) = explode(':', $part);
|
|
||||||
Browser::set($variable, $value);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$_REQUEST['request'] .= ($_REQUEST['request'] ? '/' : '') . $part;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Catches requests that aren't lowercase
|
// Catches requests that aren't lowercase
|
||||||
$lowercase_request = strtolower($_REQUEST['request']);
|
$lowercase_request = strtolower($_REQUEST['request']);
|
||||||
|
|
||||||
|
@ -132,109 +85,6 @@ class Controller extends Object
|
||||||
throw new Exception();
|
throw new Exception();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validates security level
|
|
||||||
if ($module->security)
|
|
||||||
{
|
|
||||||
$is_authenticated = false;
|
|
||||||
|
|
||||||
if (is_array($module->security))
|
|
||||||
{
|
|
||||||
$module_security = $module->security;
|
|
||||||
$security_check_class = 'isLevel';
|
|
||||||
|
|
||||||
// Checks the type and validates it
|
|
||||||
if (isset($module_security['type']))
|
|
||||||
{
|
|
||||||
$security_check_type = strtoupper($module_security['type']);
|
|
||||||
|
|
||||||
if (in_array($security_check_type, ['IS', 'HAS', 'BETWEEN']))
|
|
||||||
{
|
|
||||||
$security_check_class = $security_check_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
unset($module_security['type']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$module_security_levels = [];
|
|
||||||
|
|
||||||
// If there's a level(s) key use it
|
|
||||||
foreach (['level', 'levels'] as $security_level_key)
|
|
||||||
{
|
|
||||||
if (isset($module_security[$security_level_key]))
|
|
||||||
{
|
|
||||||
if (is_array($module_security[$security_level_key]))
|
|
||||||
{
|
|
||||||
$module_security_levels = array_merge($module_security_levels, $module_security[$security_level_key]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$module_security_levels[] = $module_security[$security_level_key];
|
|
||||||
}
|
|
||||||
|
|
||||||
unset($module_security[$security_level_key]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assume everything left in the array is a level and add it to the array
|
|
||||||
array_merge($module_security_levels, $module_security);
|
|
||||||
$security_level_count = count($module_security_levels);
|
|
||||||
|
|
||||||
switch ($security_check_class)
|
|
||||||
{
|
|
||||||
// @todo Thinking of removing this?
|
|
||||||
case 'BETWEEN':
|
|
||||||
if ($security_level_count == 2)
|
|
||||||
{
|
|
||||||
$is_authenticated = Security::betweenLevel($module_security_levels[0], array_pop($module_security_levels));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'HAS':
|
|
||||||
if ($security_level_count)
|
|
||||||
{
|
|
||||||
$is_authenticated = Security::hasLevel($module_security_levels);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'IS':
|
|
||||||
if ($security_level_count)
|
|
||||||
{
|
|
||||||
$is_authenticated = Security::isLevel($module_security_levels);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$is_authenticated = Security::isLevel($module->security);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$is_authenticated)
|
|
||||||
{
|
|
||||||
if ($_SERVER['REQUEST_METHOD'] == 'POST')
|
|
||||||
{
|
|
||||||
// @todo Perhaps I could force a logout / redirect to the login page
|
|
||||||
Browser::status(401);
|
|
||||||
|
|
||||||
throw new Exception(json_encode([
|
|
||||||
'status' => 401,
|
|
||||||
'message' => 'You are not properly authenticated, try logging out and back in.',
|
|
||||||
]));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Sets variable for the destination
|
|
||||||
$_SESSION['__pickles']['login']['destination'] = $_REQUEST['request'] ? $_REQUEST['request'] : '/';
|
|
||||||
|
|
||||||
// Redirect to login page
|
|
||||||
Browser::redirect('/login');
|
|
||||||
|
|
||||||
// Resolves testing error due to undefined $output
|
|
||||||
$output = '';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gets the profiler status
|
// Gets the profiler status
|
||||||
$profiler = $this->config->pickles['profiler'];
|
$profiler = $this->config->pickles['profiler'];
|
||||||
$profiler = $profiler === true || stripos($profiler, 'timers') !== false;
|
$profiler = $profiler === true || stripos($profiler, 'timers') !== false;
|
||||||
|
@ -242,16 +92,6 @@ class Controller extends Object
|
||||||
$default_method = '__default';
|
$default_method = '__default';
|
||||||
$role_method = null;
|
$role_method = null;
|
||||||
|
|
||||||
if (isset($_SESSION['__pickles']['security']['role']) && !String::isEmpty($_SESSION['__pickles']['security']['role']))
|
|
||||||
{
|
|
||||||
$role_method = '__default_' . $_SESSION['__pickles']['security']['role'];
|
|
||||||
|
|
||||||
if (method_exists($module, $role_method))
|
|
||||||
{
|
|
||||||
$default_method = $role_method;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Attempts to execute the default method
|
// Attempts to execute the default method
|
||||||
// @todo Seems a bit redundant, refactor
|
// @todo Seems a bit redundant, refactor
|
||||||
if ($default_method == $role_method || method_exists($module, $default_method))
|
if ($default_method == $role_method || method_exists($module, $default_method))
|
||||||
|
@ -331,28 +171,6 @@ class Controller extends Object
|
||||||
Profiler::timer('module ' . $default_method);
|
Profiler::timer('module ' . $default_method);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks if we have any templates
|
|
||||||
$parent_template = $module->template;
|
|
||||||
$template_exists = $this->validateTemplates($module, $parent_template);
|
|
||||||
|
|
||||||
// No templates? 404 that shit
|
|
||||||
if (!$module_exists && !$template_exists)
|
|
||||||
{
|
|
||||||
Browser::status(404);
|
|
||||||
$_REQUEST['request'] = '__shared/404';
|
|
||||||
|
|
||||||
if (!$this->validateTemplates($module, $parent_template))
|
|
||||||
{
|
|
||||||
throw new Exception('
|
|
||||||
<h1>Not Found</h1>
|
|
||||||
<p>The requested URL /' . $request . ' was not found on this server.</p>
|
|
||||||
<p>Additionally, a custom error template was not found.</p>
|
|
||||||
<hr>
|
|
||||||
<em>Powered by <a href="https://github.com/joshtronic/pickles">PICKLES</a></em>
|
|
||||||
');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$display = new Display($module);
|
$display = new Display($module);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -384,30 +202,5 @@ class Controller extends Object
|
||||||
Profiler::report();
|
Profiler::report();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// @todo Document me
|
|
||||||
private function validateTemplates(&$module, $parent_template)
|
|
||||||
{
|
|
||||||
$templates = [
|
|
||||||
SITE_TEMPLATE_PATH . '__shared/' . $parent_template . '.phtml',
|
|
||||||
SITE_TEMPLATE_PATH . $_REQUEST['request'] . '.phtml',
|
|
||||||
];
|
|
||||||
|
|
||||||
$module->template = [];
|
|
||||||
$child_exists = file_exists($templates[1]);
|
|
||||||
|
|
||||||
if (file_exists($templates[0]) && $child_exists)
|
|
||||||
{
|
|
||||||
$module->template = $templates;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
elseif ($child_exists)
|
|
||||||
{
|
|
||||||
$module->template = [$templates[1]];
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,9 +49,8 @@ class Display extends Object
|
||||||
$this->module->response = [$this->module->response];
|
$this->module->response = [$this->module->response];
|
||||||
}
|
}
|
||||||
|
|
||||||
$return_json = false;
|
$return_json = false;
|
||||||
$return_template = false;
|
$return_xml = false;
|
||||||
$return_xml = false;
|
|
||||||
|
|
||||||
foreach ($this->module->output as $return)
|
foreach ($this->module->output as $return)
|
||||||
{
|
{
|
||||||
|
@ -60,7 +59,7 @@ class Display extends Object
|
||||||
}
|
}
|
||||||
|
|
||||||
// Makes sure the return type is valid
|
// Makes sure the return type is valid
|
||||||
if (!$return_json && !$return_template && !$return_xml)
|
if (!$return_json && !$return_xml)
|
||||||
{
|
{
|
||||||
throw new Exception('Invalid return type.');
|
throw new Exception('Invalid return type.');
|
||||||
}
|
}
|
||||||
|
@ -85,83 +84,31 @@ class Display extends Object
|
||||||
throw new Exception('Requested URI contains PHPSESSID, redirecting.');
|
throw new Exception('Requested URI contains PHPSESSID, redirecting.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$loaded = false;
|
$response = [
|
||||||
|
'meta' => [
|
||||||
|
'status' => $this->module->status,
|
||||||
|
'message' => $this->module->message,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
if ($return_template && $this->module->templates)
|
if ($this->module->response)
|
||||||
{
|
{
|
||||||
// Exposes some objects and variables to the local scope of the template
|
$response['response'] = $this->module->response;
|
||||||
$this->request = $this->js_file = $_REQUEST['request'];
|
|
||||||
$this->css_class = strtr($this->request, '/', '-');
|
|
||||||
|
|
||||||
$this->dynamic = new $dynamic_class();
|
|
||||||
$this->form = new $form_class();
|
|
||||||
$this->html = new $html_class();
|
|
||||||
|
|
||||||
// Checks for the parent template and tries to load it
|
|
||||||
if ($this->module->template)
|
|
||||||
{
|
|
||||||
$profiler = $this->config->pickles['profiler'];
|
|
||||||
$profiler = $profiler === true || stripos($profiler, 'timers') !== false;
|
|
||||||
|
|
||||||
// Starts a timer for the loading of the template
|
|
||||||
if ($profiler)
|
|
||||||
{
|
|
||||||
Profiler::timer('loading template');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assigns old variable
|
|
||||||
$required_template = $this->module->templates[0];
|
|
||||||
$this->module->template = end($this->module->templates);
|
|
||||||
$loaded = require_once $required_template;
|
|
||||||
|
|
||||||
// Stops the template loading timer
|
|
||||||
if ($profiler)
|
|
||||||
{
|
|
||||||
Profiler::timer('loading template');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$loaded)
|
if ($return_json)
|
||||||
{
|
{
|
||||||
if (!$return_template || !$this->module->templates)
|
header('Content-type: application/json');
|
||||||
{
|
$pretty = isset($_REQUEST['pretty']) ? JSON_PRETTY_PRINT : false;
|
||||||
$meta = [
|
echo json_encode($response, $pretty);
|
||||||
'status' => $this->module->status,
|
}
|
||||||
'message' => $this->module->message,
|
elseif ($return_xml)
|
||||||
];
|
{
|
||||||
|
header('Content-type: text/xml');
|
||||||
$response = [
|
echo Convert::arrayToXML($response, isset($_REQUEST['pretty']));
|
||||||
'meta' => $meta,
|
|
||||||
'response' => $this->module->response,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($return_json)
|
|
||||||
{
|
|
||||||
header('Content-type: application/json');
|
|
||||||
$pretty = isset($_REQUEST['pretty']) ? JSON_PRETTY_PRINT : false;
|
|
||||||
echo json_encode($response, $pretty);
|
|
||||||
}
|
|
||||||
elseif ($return_xml)
|
|
||||||
{
|
|
||||||
header('Content-type: text/xml');
|
|
||||||
echo Convert::arrayToXML($response, isset($_REQUEST['pretty']));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Grabs the buffer so we can massage it a bit
|
return ob_get_clean();
|
||||||
$buffer = ob_get_clean();
|
|
||||||
|
|
||||||
// Kills any whitespace and HTML comments in templates
|
|
||||||
if ($loaded)
|
|
||||||
{
|
|
||||||
// The BSA exception is because their system sucks and demands
|
|
||||||
// there be comments present
|
|
||||||
$buffer = preg_replace(['/^[\s]+/m', '/<!--(?:(?!BuySellAds).)+-->/U'], '', $buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $buffer;
|
|
||||||
}
|
}
|
||||||
catch (Exception $e)
|
catch (Exception $e)
|
||||||
{
|
{
|
||||||
|
|
|
@ -132,24 +132,6 @@ if (is_array($config->php) && count($config->php))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Starts session handling (old)
|
|
||||||
if (isset($config->pickles['session']))
|
|
||||||
{
|
|
||||||
if (session_id() == '' && $config->pickles['session'] !== false)
|
|
||||||
{
|
|
||||||
new Session();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Starts session handling (new)
|
|
||||||
if (isset($config->pickles['sessions']))
|
|
||||||
{
|
|
||||||
if (session_id() == '' && $config->pickles['sessions'] !== false)
|
|
||||||
{
|
|
||||||
new Session();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
// {{{ Defaults some internals for ease of use
|
// {{{ Defaults some internals for ease of use
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue