From 89177306a2278255d6a2203b5fff4a839183d3cd Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sun, 24 Jun 2012 14:00:49 +0200 Subject: [PATCH] Introducing a $_REQUEST/POST/GET wrapper This new wrapper ensures types are correct and accessed parameters are actually set (with custom default fallbacks). The wrapper is available in the global $INPUT variable. It accesses $_REQUEST by default. If POST or GET is required, the post and get members can be used: $INPUT->int('foo',false); // access $_REQUEST['foo'], default false $INPUT->post->int('foo'); // access $_POST['foo'], default 0 $INPUT->get->int('foo'); // access $_GET['foo'], default 0 The codebase still needs to be updated to make use of this. --- inc/Input.class.php | 147 ++++++++++++++++++++++++++++++++++++++++++++ inc/init.php | 4 ++ inc/load.php | 1 + 3 files changed, 152 insertions(+) create mode 100644 inc/Input.class.php diff --git a/inc/Input.class.php b/inc/Input.class.php new file mode 100644 index 000000000..f1967599f --- /dev/null +++ b/inc/Input.class.php @@ -0,0 +1,147 @@ + + */ +class Input { + + /** @var PostInput Access $_POST parameters */ + public $post; + /** @var GetInput Access $_GET parameters */ + public $get; + + protected $access; + + /** + * Intilizes the Input class and it subcomponents + */ + function __construct() { + $this->access = &$_REQUEST; + $this->post = new PostInput(); + $this->get = new GetInput(); + } + + /** + * Access a request parameter without any type conversion + * + * @param string $name Parameter name + * @param mixed $default Default to return if parameter isn't set + * @return mixed + */ + public function param($name, $default = null) { + if(!isset($this->access[$name])) return $default; + return $this->access[$name]; + } + + /** + * Get a reference to a request parameter + * + * This avoids copying data in memory, when the parameter is not set it will be created + * and intialized with the given $default value before a reference is returned + * + * @param string $name Parameter name + * @param mixed $default Initialize parameter with if not set + * @return &mixed + */ + public function &ref($name, $default = '') { + if(!isset($this->access[$name])) { + $this->access[$name] = $default; + } + + $ref = &$this->access[$name]; + return $ref; + } + + /** + * Access a request parameter as int + * + * @param string $name Parameter name + * @param mixed $default Default to return if parameter isn't set or is an array + * @return int + */ + public function int($name, $default = 0) { + if(!isset($this->access[$name])) return $default; + if(is_array($this->access[$name])) return $default; + + return (int) $this->access[$name]; + } + + /** + * Access a request parameter as string + * + * @param string $name Parameter name + * @param mixed $default Default to return if parameter isn't set or is an array + * @return string + */ + public function str($name, $default = '') { + if(!isset($this->access[$name])) return $default; + if(is_array($this->access[$name])) return $default; + + return (string) $this->access[$name]; + } + + /** + * Access a request parameter as bool + * + * @param string $name Parameter name + * @param mixed $default Default to return if parameter isn't set + * @return bool + */ + public function bool($name, $default = '') { + if(!isset($this->access[$name])) return $default; + + return (bool) $this->access[$name]; + } + + /** + * Access a request parameter as array + * + * @param string $name Parameter name + * @param mixed $default Default to return if parameter isn't set + * @return array + */ + public function arr($name, $default = array()) { + if(!isset($this->access[$name])) return $default; + + return (array) $this->access[$name]; + } + +} + +/** + * Internal class used for $_POST access in Input class + */ +class PostInput extends Input { + protected $access; + + /** + * Initialize the $access array, remove subclass members + */ + function __construct() { + $this->access = &$_POST; + unset ($this->post); + unset ($this->get); + } +} + +/** + * Internal class used for $_GET access in Input class + */ +class GetInput extends Input { + protected $access; + + /** + * Initialize the $access array, remove subclass members + */ + function __construct() { + $this->access = &$_GET; + unset ($this->post); + unset ($this->get); + } +} \ No newline at end of file diff --git a/inc/init.php b/inc/init.php index 403fbe4ab..1907aea09 100644 --- a/inc/init.php +++ b/inc/init.php @@ -197,6 +197,10 @@ if (empty($plugin_controller_class)) $plugin_controller_class = 'Doku_Plugin_Con // load libraries require_once(DOKU_INC.'inc/load.php'); +// input handle class +global $INPUT; +$INPUT = new Input(); + // initialize plugin controller $plugin_controller = new $plugin_controller_class(); diff --git a/inc/load.php b/inc/load.php index 7a410e452..b676518e7 100644 --- a/inc/load.php +++ b/inc/load.php @@ -62,6 +62,7 @@ function load_autoload($name){ 'Doku_Event' => DOKU_INC.'inc/events.php', 'Doku_Event_Handler' => DOKU_INC.'inc/events.php', 'EmailAddressValidator' => DOKU_INC.'inc/EmailAddressValidator.php', + 'Input' => DOKU_INC.'inc/Input.class.php', 'JpegMeta' => DOKU_INC.'inc/JpegMeta.php', 'SimplePie' => DOKU_INC.'inc/SimplePie.php', 'FeedParser' => DOKU_INC.'inc/FeedParser.php',