From cbb75e7064254ce18e56f3004e40e11e4093a15c Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Mon, 4 Feb 2013 23:34:09 -0500 Subject: [PATCH] Added @geoffoliver attributes. Geoff likes to exploit the URI and inject key/value pairs into it. It's not something I do, but I was intrigued enough to implement it. Basic usage is /path/to/page/id:123/spam:eggs. The variables are removed from the request variable as to not bork module loading and are made available via Browser::get('variable'); --- classes/Browser.php | 68 +++++++++++++++++++++++++++++++- classes/Controller.php | 22 ++++++++++- jar.php | 88 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 173 insertions(+), 5 deletions(-) diff --git a/classes/Browser.php b/classes/Browser.php index 17fb69f..e9a0f87 100644 --- a/classes/Browser.php +++ b/classes/Browser.php @@ -9,7 +9,7 @@ * Redistribution of these files must retain the above copyright notice. * * @author Josh Sherman - * @copyright Copyright 2007-2012, Josh Sherman + * @copyright Copyright 2007-2013, Josh Sherman * @license http://www.opensource.org/licenses/mit-license.html * @package PICKLES * @link https://github.com/joshtronic/pickles @@ -21,8 +21,72 @@ * Just a simple collection of static functions to accomplish some of the more * redundant browser-related tasks. */ -class Browser +class Browser extends Object { + /** + * Attributes + * + * Variables passed on the query string as /var:value/ + * + * @access private + * @var array + */ + private $attributes = array(); + + /** + * Get instance of the object + * + * Let's the parent class do all the work + * + * @static + * @param string $class name of the class to instantiate + * @return object self::$instance instance of the Config class + */ + public static function getInstance($class = 'Browser') + { + return parent::getInstance($class); + } + + /** + * Set browser variable + * + * Sets a variable in the attributes array for easier access later. + * + * @static + * @param string $variable name of the variable to set + * @param mixed $value the value to set to the variable + * @return boolean true + */ + public static function set($variable, $value) + { + $browser = Browser::getInstance(); + $browser->attributes[$variable] = $value; + + return true; + } + + /** + * Get browser variable + * + * Gets a variable passed in from the browser. Currently only supports + * the custom attribute URI format /$variable:$value/. + * + * @static + * @param string $variable name of the variable to get + * @return mixed the value of the variable or boolean false if not set + */ + public static function get($variable) + { + $browser = Browser::getInstance(); + + if (isset($browser->attributes[$variable])) + { + return $browser->attributes[$variable]; + } + + return false; + } + /** * Is Mobile * diff --git a/classes/Controller.php b/classes/Controller.php index 46713e4..e1f2224 100644 --- a/classes/Controller.php +++ b/classes/Controller.php @@ -9,7 +9,7 @@ * Redistribution of these files must retain the above copyright notice. * * @author Josh Sherman - * @copyright Copyright 2007-2012, Josh Sherman + * @copyright Copyright 2007-2013, Josh Sherman * @license http://www.opensource.org/licenses/mit-license.html * @package PICKLES * @link https://github.com/joshtronic/pickles @@ -55,6 +55,26 @@ class Controller extends Object Error::fatal($_SERVER['SERVER_NAME'] . ' is currently
down for maintenance'); } + // 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; + } + } + } + $_REQUEST['request'] = trim($_REQUEST['request']); // Checks the passed request for validity diff --git a/jar.php b/jar.php index 3b6f9bc..cc795ad 100755 --- a/jar.php +++ b/jar.php @@ -314,7 +314,7 @@ class API_Tinychat extends API_Common * Redistribution of these files must retain the above copyright notice. * * @author Josh Sherman - * @copyright Copyright 2007-2012, Josh Sherman + * @copyright Copyright 2007-2013, Josh Sherman * @license http://www.opensource.org/licenses/mit-license.html * @package PICKLES * @link https://github.com/joshtronic/pickles @@ -326,8 +326,72 @@ class API_Tinychat extends API_Common * Just a simple collection of static functions to accomplish some of the more * redundant browser-related tasks. */ -class Browser +class Browser extends Object { + /** + * Attributes + * + * Variables passed on the query string as /var:value/ + * + * @access private + * @var array + */ + private $attributes = array(); + + /** + * Get instance of the object + * + * Let's the parent class do all the work + * + * @static + * @param string $class name of the class to instantiate + * @return object self::$instance instance of the Config class + */ + public static function getInstance($class = 'Browser') + { + return parent::getInstance($class); + } + + /** + * Set browser variable + * + * Sets a variable in the attributes array for easier access later. + * + * @static + * @param string $variable name of the variable to set + * @param mixed $value the value to set to the variable + * @return boolean true + */ + public static function set($variable, $value) + { + $browser = Browser::getInstance(); + $browser->attributes[$variable] = $value; + + return true; + } + + /** + * Get browser variable + * + * Gets a variable passed in from the browser. Currently only supports + * the custom attribute URI format /$variable:$value/. + * + * @static + * @param string $variable name of the variable to get + * @return mixed the value of the variable or boolean false if not set + */ + public static function get($variable) + { + $browser = Browser::getInstance(); + + if (isset($browser->attributes[$variable])) + { + return $browser->attributes[$variable]; + } + + return false; + } + /** * Is Mobile * @@ -1083,6 +1147,26 @@ class Controller extends Object Error::fatal($_SERVER['SERVER_NAME'] . ' is currently
down for maintenance'); } + // 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; + } + } + } + $_REQUEST['request'] = trim($_REQUEST['request']); // Checks the passed request for validity