From 998a9d958f6d5b47e611fe2844ec05d690d88879 Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Sun, 16 Jan 2011 22:46:34 +0000 Subject: [PATCH] Imported Page/RequestParser classes for page templating. Added formatFilesize method to Main class --- .../lib/SihnonFramework/Exceptions.class.php | 2 + source/lib/SihnonFramework/Main.class.php | 23 +++- source/lib/SihnonFramework/Page.class.php | 74 +++++++++++ .../SihnonFramework/RequestParser.class.php | 123 ++++++++++++++++++ 4 files changed, 220 insertions(+), 2 deletions(-) create mode 100644 source/lib/SihnonFramework/Page.class.php create mode 100644 source/lib/SihnonFramework/RequestParser.class.php diff --git a/source/lib/SihnonFramework/Exceptions.class.php b/source/lib/SihnonFramework/Exceptions.class.php index c3dc0fb..e2ad85a 100644 --- a/source/lib/SihnonFramework/Exceptions.class.php +++ b/source/lib/SihnonFramework/Exceptions.class.php @@ -21,4 +21,6 @@ class SihnonFramework_Exception_CacheObjectNotFound extends SihnonFramework_E class SihnonFramework_Exception_InvalidPluginName extends SihnonFramework_Exception {}; +class SihnonFramework_Exception_FileNotFound extends SihnonFramework_Exception {}; + ?> diff --git a/source/lib/SihnonFramework/Main.class.php b/source/lib/SihnonFramework/Main.class.php index 8d5a5e8..914a569 100644 --- a/source/lib/SihnonFramework/Main.class.php +++ b/source/lib/SihnonFramework/Main.class.php @@ -185,12 +185,12 @@ class SihnonFramework_Main { $subclass_dir_prefix .= DIRECTORY_SEPARATOR; } - self::$autoload_classes[] = array( + array_unshift(self::$autoload_classes, array( 'base' => $base, 'base_dir_prefix' => $base_dir_prefix, 'subclass' => $subclass, 'subclass_dir_prefix' => $subclass_dir_prefix, - ); + )); } /** @@ -264,6 +264,25 @@ class SihnonFramework_Main { return $result; } + public static function formatFilesize($bytes) { + if (is_null($bytes)) { + return 'unknown'; + } + + $labels = array('B', 'KB', 'MB', 'GB', 'TB'); + $limits = array(1, 1024, 1024*1024, 1024*1024*1024, 1024*1024*1024*1024); + + $size = $bytes; + $ptr = count($labels) - 1; + while ($ptr >= 0 && $bytes < $limits[$ptr]) { + --$ptr; + } + + $size = round($bytes / $limits[$ptr], 2) . ' ' . $labels[$ptr]; + + return $size; + } + } SihnonFramework_Main::initialise(); diff --git a/source/lib/SihnonFramework/Page.class.php b/source/lib/SihnonFramework/Page.class.php new file mode 100644 index 0000000..9d0f401 --- /dev/null +++ b/source/lib/SihnonFramework/Page.class.php @@ -0,0 +1,74 @@ +smarty = $smarty; + $this->request = $request; + $this->page = $request->page(); + } + + public function page() { + return $this->page; + } + + public function template_filename() { + return $this->page . '.tpl'; + } + + public function evaluate($template_variables = array()) { + $code_filename = $this->page . '.php'; + $template_filename = $this->template_filename(); + + try { + $this->render($template_filename, $code_filename, $template_variables); + } catch (MediaListing_Exception_AbortEntirePage $e) { + return false; + } catch (MediaListing_Exception_FileNotFound $e) { + $this->render('errors/404.tpl', 'errors/404.php'); + } catch (MediaListing_Exception $e) { + $this->render('errors/unhandled-exception.tpl', 'errors/unhandled-exception.php', array( + 'exception' => $e, + )); + } + + return true; + } + + protected function render($template_filename, $code_filename = null, $template_variables = array()) { + if ( ! $this->smarty->template_exists($template_filename)) { + throw new MediaListing_Exception_FileNotFound($template_filename); + } + + // Copy all the template variables into the namespace for this function, + // so that they are readily available to the template + foreach ($template_variables as $__k => $__v) { + $$__k = $__v; + } + + // Include the template code file, which will do all the work for this page + $real_code_filename = '../source/pages/' . $code_filename; + if ($code_filename && file_exists($real_code_filename)) { + include $real_code_filename; + } + + // Now execute the template itself, which will render the results of the code file + $this->smarty->assign('page_content', $this->smarty->fetch($template_filename)); + } + + public static function redirect($relative_url) { + $absolute_url = MediaListing_Main::instance()->absoluteUrl($relative_url); + + header("Location: $absolute_url"); + + throw new MediaListing_Exception_AbortEntirePage(); + } + +}; + +?> diff --git a/source/lib/SihnonFramework/RequestParser.class.php b/source/lib/SihnonFramework/RequestParser.class.php new file mode 100644 index 0000000..8532939 --- /dev/null +++ b/source/lib/SihnonFramework/RequestParser.class.php @@ -0,0 +1,123 @@ +request_string = $request_string; + + $this->parse(); + } + + public function parse() { + if (!$this->request_string) { + $this->page = array('home'); + return; + } + + $components = explode('/', $this->request_string); + if (!$components) { + return; + } + + // Read through the components list looking for elements matching known directories and files + // to determine which page this request is for + $base_dir = '../source/templates'; + while (true) { + if ($components && ! $components[0]) { + // Skip over any empty components before we find a page + array_shift($components); + } + + if ($components && is_dir($base_dir . '/' . $components[0])) { + $base_dir .= '/' . $components[0]; + array_push($this->page, array_shift($components)); + } elseif ($components && is_file($base_dir . '/' . $components[0] . '.tpl')) { + // We have found a valid page, so break the loop here, + // leaving the remaining components as key/value pairs + array_push($this->page, array_shift($components)); + break; + } else { + // See if we've already seen a component and assumed it referred to a dir when a file of the same name exists + if ($this->page && is_file($base_dir . '.tpl')) { + break; + } elseif ( ! $components && is_file($base_dir . '/index.tpl')) { + // The last component in the path was a valid directory, and a directory index exists + array_push($this->page, 'index'); + break; + } else { + // No valid page was found, so display an error page + $this->page = array('404'); + return; + } + } + } + + // The subsequent tokens are parameters for this page in key value pairs + while ($components) { + // If the url ended with a trailing slash, the last element will be null + $last_element = $components[count($components) - 1]; + if ($last_element == "") { + array_pop($components); + } + + $this->vars[array_shift($components)] = $components ? array_shift($components) : null; + } + } + + public function page() { + return join('/', $this->page); + } + + public function exists($key) { + return isset($this->vars[$key]); + } + + public function get($key, $default = null) { + if (isset($this->vars[$key])) { + return $this->vars[$key]; + } + + if (is_string($default) && preg_match('/^[a-zA-Z_]+_Exception/', $default) && class_exists($default) && is_subclass_of($default, MediaListing_Exception)) { + throw new $default(); + } + + return $default; + } + + public function getRemainder($after, $hasValue) { + $include = false; + $result = array(); + foreach ($this->vars as $var => $value) { + if ($var == $after) { + $include = true; + if ($hasValue) { + continue; + } else { + $result[] = $value; + } + } elseif ($include) { + $result[] = $var; + $result[] = $value; + } else { + continue; + } + } + + if ($result && ( ! $result[count($result) - 1])) { + array_pop($result); + } + + return $result; + } + + public function request_string() { + return $this->request_string; + } + +}; + +?>