From 86d8d65dd893db71d0b467d97c20ef64c2bc008d Mon Sep 17 00:00:00 2001 From: Ben Roberts Date: Sun, 21 Mar 2010 01:35:48 +0000 Subject: [PATCH] Improved request parser to support subdirectories Leading components in the request string are checked to see if they match template subdirectories before looking for valid page names. Index files are checked if the request string ends on a valid subdirectory If no valid page is found within a subdirectory, the last name is rechecked to see if it was a valid page file --- HandBrakeCluster/RequestParser.class.php | 38 +++++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/HandBrakeCluster/RequestParser.class.php b/HandBrakeCluster/RequestParser.class.php index 34371dc..17892ae 100644 --- a/HandBrakeCluster/RequestParser.class.php +++ b/HandBrakeCluster/RequestParser.class.php @@ -3,7 +3,7 @@ class HandBrakeCluster_RequestParser { private $request_string; - private $page = 'home'; + private $page = array(); private $vars = array(); public function __construct($request_string) { @@ -22,8 +22,38 @@ class HandBrakeCluster_RequestParser { return; } - // The first token is the page to execute - $this->page = array_shift($components); + // Read through the components list looking for elements matching known directories and files + // to determine which page this request is for + $base_dir = '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) { @@ -32,7 +62,7 @@ class HandBrakeCluster_RequestParser { } public function page() { - return $this->page; + return join('/', $this->page); } public function get($key) {