From 2fa2b6ad03be6428dec44c89144096c052d08470 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Wed, 24 Sep 2014 23:57:39 -0400 Subject: [PATCH] Started gutting logic from the Controller and Display classes. --- src/classes/Controller.php | 207 ------------------------------------- src/classes/Display.php | 95 ++++------------- src/pickles.php | 18 ---- 3 files changed, 21 insertions(+), 299 deletions(-) diff --git a/src/classes/Controller.php b/src/classes/Controller.php index 54d0d3f..015df27 100644 --- a/src/classes/Controller.php +++ b/src/classes/Controller.php @@ -38,55 +38,8 @@ class Controller extends Object { parent::__construct(); - // Generate a generic "site down" message if the site is set to be disabled 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 ' -

Down for Maintenance

-

- ' . $_SERVER['SERVER_NAME'] . ' is currently down for maintenance. - Please check back in a few minutes. -

-

Additionally, a custom maintenance template was not found.

-
- Powered by PICKLES - '; - } - - 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 $lowercase_request = strtolower($_REQUEST['request']); @@ -132,109 +85,6 @@ class Controller extends Object 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 $profiler = $this->config->pickles['profiler']; $profiler = $profiler === true || stripos($profiler, 'timers') !== false; @@ -242,16 +92,6 @@ class Controller extends Object $default_method = '__default'; $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 // @todo Seems a bit redundant, refactor if ($default_method == $role_method || method_exists($module, $default_method)) @@ -331,28 +171,6 @@ class Controller extends Object 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(' -

Not Found

-

The requested URL /' . $request . ' was not found on this server.

-

Additionally, a custom error template was not found.

-
- Powered by PICKLES - '); - } - } - $display = new Display($module); } @@ -384,30 +202,5 @@ class Controller extends Object 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; - } } diff --git a/src/classes/Display.php b/src/classes/Display.php index 2efc838..66e72bc 100644 --- a/src/classes/Display.php +++ b/src/classes/Display.php @@ -49,9 +49,8 @@ class Display extends Object $this->module->response = [$this->module->response]; } - $return_json = false; - $return_template = false; - $return_xml = false; + $return_json = false; + $return_xml = false; foreach ($this->module->output as $return) { @@ -60,7 +59,7 @@ class Display extends Object } // 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.'); } @@ -85,83 +84,31 @@ class Display extends Object 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 - $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'); - } - } + $response['response'] = $this->module->response; } - if (!$loaded) + if ($return_json) { - if (!$return_template || !$this->module->templates) - { - $meta = [ - 'status' => $this->module->status, - 'message' => $this->module->message, - ]; - - $response = [ - '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'])); - } + 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 - $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', '//U'], '', $buffer); - } - - return $buffer; + return ob_get_clean(); } catch (Exception $e) { diff --git a/src/pickles.php b/src/pickles.php index accdec6..ae2e4ad 100644 --- a/src/pickles.php +++ b/src/pickles.php @@ -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