From 89177306a2278255d6a2203b5fff4a839183d3cd Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sun, 24 Jun 2012 14:00:49 +0200 Subject: 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 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 inc/Input.class.php (limited to '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 -- cgit v1.2.3 From fd50d5c713878b03fdcd8d21f8209f968fe55646 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sun, 24 Jun 2012 14:35:23 +0200 Subject: added has() method to input class --- inc/Input.class.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'inc/Input.class.php') diff --git a/inc/Input.class.php b/inc/Input.class.php index f1967599f..7665c609e 100644 --- a/inc/Input.class.php +++ b/inc/Input.class.php @@ -27,6 +27,19 @@ class Input { $this->get = new GetInput(); } + /** + * Check if a parameter was set + * + * Basically a wrapper around isset + * + * @see isset + * @param $name Parameter name + * @return bool + */ + public function has($name) { + return isset($this->access[$name]); + } + /** * Access a request parameter without any type conversion * -- cgit v1.2.3 From c4e18ef950a64e41101f06da0c4ca2e45bb21fb7 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sun, 24 Jun 2012 15:16:47 +0200 Subject: added 3rd parameter to Input methods This allows to treat empty parameters as default --- inc/Input.class.php | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'inc/Input.class.php') diff --git a/inc/Input.class.php b/inc/Input.class.php index 7665c609e..878f29550 100644 --- a/inc/Input.class.php +++ b/inc/Input.class.php @@ -33,7 +33,7 @@ class Input { * Basically a wrapper around isset * * @see isset - * @param $name Parameter name + * @param string $name Parameter name * @return bool */ public function has($name) { @@ -45,10 +45,12 @@ class Input { * * @param string $name Parameter name * @param mixed $default Default to return if parameter isn't set + * @param bool $nonempty Return $default if parameter is set but empty() * @return mixed */ - public function param($name, $default = null) { + public function param($name, $default = null, $nonempty = false) { if(!isset($this->access[$name])) return $default; + if($nonempty && empty($this->access[$name])) return $default; return $this->access[$name]; } @@ -58,12 +60,13 @@ class Input { * 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 + * @param string $name Parameter name + * @param mixed $default Initialize parameter with if not set + * @param bool $nonempty Init with $default if parameter is set but empty() * @return &mixed */ - public function &ref($name, $default = '') { - if(!isset($this->access[$name])) { + public function &ref($name, $default = '', $nonempty = false) { + if(!isset($this->access[$name]) || ($nonempty && empty($this->access[$name]))) { $this->access[$name] = $default; } @@ -76,11 +79,13 @@ class Input { * * @param string $name Parameter name * @param mixed $default Default to return if parameter isn't set or is an array + * @param bool $nonempty Return $default if parameter is set but empty() * @return int */ - public function int($name, $default = 0) { + public function int($name, $default = 0, $nonempty = false) { if(!isset($this->access[$name])) return $default; if(is_array($this->access[$name])) return $default; + if($nonempty && empty($this->access[$name])) return $default; return (int) $this->access[$name]; } @@ -90,11 +95,13 @@ class Input { * * @param string $name Parameter name * @param mixed $default Default to return if parameter isn't set or is an array + * @param bool $nonempty Return $default if parameter is set but empty() * @return string */ - public function str($name, $default = '') { + public function str($name, $default = '', $nonempty = false) { if(!isset($this->access[$name])) return $default; if(is_array($this->access[$name])) return $default; + if($nonempty && empty($this->access[$name])) return $default; return (string) $this->access[$name]; } @@ -104,10 +111,12 @@ class Input { * * @param string $name Parameter name * @param mixed $default Default to return if parameter isn't set + * @param bool $nonempty Return $default if parameter is set but empty() * @return bool */ - public function bool($name, $default = '') { + public function bool($name, $default = '', $nonempty = false) { if(!isset($this->access[$name])) return $default; + if($nonempty && empty($this->access[$name])) return $default; return (bool) $this->access[$name]; } @@ -117,10 +126,12 @@ class Input { * * @param string $name Parameter name * @param mixed $default Default to return if parameter isn't set + * @param bool $nonempty Return $default if parameter is set but empty() * @return array */ - public function arr($name, $default = array()) { + public function arr($name, $default = array(), $nonempty = false) { if(!isset($this->access[$name])) return $default; + if($nonempty && empty($this->access[$name])) return $default; return (array) $this->access[$name]; } -- cgit v1.2.3 From a12aaeb7e914180a4c67ca7456771b6d99d9c535 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sun, 24 Jun 2012 16:54:43 +0200 Subject: allow setting values via input class --- inc/Input.class.php | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) (limited to 'inc/Input.class.php') diff --git a/inc/Input.class.php b/inc/Input.class.php index 878f29550..aa402c646 100644 --- a/inc/Input.class.php +++ b/inc/Input.class.php @@ -54,6 +54,16 @@ class Input { return $this->access[$name]; } + /** + * Sets a parameter + * + * @param string $name Parameter name + * @param mixed $value Value to set + */ + public function set($name, $value) { + $this->access[$name] = $value; + } + /** * Get a reference to a request parameter * @@ -61,13 +71,13 @@ class Input { * 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 + * @param mixed $default If parameter is not set, initialize with this value * @param bool $nonempty Init with $default if parameter is set but empty() * @return &mixed */ public function &ref($name, $default = '', $nonempty = false) { if(!isset($this->access[$name]) || ($nonempty && empty($this->access[$name]))) { - $this->access[$name] = $default; + $this->set($name, $default); } $ref = &$this->access[$name]; @@ -114,7 +124,7 @@ class Input { * @param bool $nonempty Return $default if parameter is set but empty() * @return bool */ - public function bool($name, $default = '', $nonempty = false) { + public function bool($name, $default = false, $nonempty = false) { if(!isset($this->access[$name])) return $default; if($nonempty && empty($this->access[$name])) return $default; @@ -152,10 +162,22 @@ class PostInput extends Input { unset ($this->post); unset ($this->get); } + + /** + * Sets a parameter in $_POST and $_REQUEST + * + * @param string $name Parameter name + * @param mixed $value Value to set + */ + public function set($name, $value) { + parent::set($name, $value); + $_REQUEST[$name] = $value; + } } /** * Internal class used for $_GET access in Input class + */ class GetInput extends Input { protected $access; @@ -168,4 +190,15 @@ class GetInput extends Input { unset ($this->post); unset ($this->get); } + + /** + * Sets a parameter in $_GET and $_REQUEST + * + * @param string $name Parameter name + * @param mixed $value Value to set + */ + public function set($name, $value) { + parent::set($name, $value); + $_REQUEST[$name] = $value; + } } \ No newline at end of file -- cgit v1.2.3 From d720a82c905d624b7fd40132514ae662a410c949 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Sun, 24 Jun 2012 20:09:35 +0200 Subject: remove() implemented for Input class --- inc/Input.class.php | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'inc/Input.class.php') diff --git a/inc/Input.class.php b/inc/Input.class.php index aa402c646..62c2688c8 100644 --- a/inc/Input.class.php +++ b/inc/Input.class.php @@ -30,7 +30,8 @@ class Input { /** * Check if a parameter was set * - * Basically a wrapper around isset + * Basically a wrapper around isset. When called on the $post and $get subclasses, + * the parameter is set to $_POST or $_GET and to $_REQUEST * * @see isset * @param string $name Parameter name @@ -40,6 +41,29 @@ class Input { return isset($this->access[$name]); } + /** + * Remove a parameter from the superglobals + * + * Basically a wrapper around unset. When NOT called on the $post and $get subclasses, + * the parameter will also be removed from $_POST or $_GET + * + * @see isset + * @param string $name Parameter name + * @return bool + */ + public function remove($name) { + if(isset($this->access[$name])) { + unset($this->access[$name]); + } + // also remove from sub classes + if(isset($this->post) && isset($_POST[$name])) { + unset($_POST[$name]); + } + if(isset($this->get) && isset($_GET[$name])) { + unset($_GET[$name]); + } + } + /** * Access a request parameter without any type conversion * -- cgit v1.2.3 From 591acd873d64abb271864b581c48ca419aa5d329 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Mon, 25 Jun 2012 00:37:21 +0200 Subject: some Input class fixes and unit tests --- inc/Input.class.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'inc/Input.class.php') diff --git a/inc/Input.class.php b/inc/Input.class.php index 62c2688c8..1ea5e031f 100644 --- a/inc/Input.class.php +++ b/inc/Input.class.php @@ -104,8 +104,7 @@ class Input { $this->set($name, $default); } - $ref = &$this->access[$name]; - return $ref; + return $this->access[$name]; } /** @@ -143,6 +142,8 @@ class Input { /** * Access a request parameter as bool * + * Note: $nonempty is here for interface consistency and makes not much sense for booleans + * * @param string $name Parameter name * @param mixed $default Default to return if parameter isn't set * @param bool $nonempty Return $default if parameter is set but empty() @@ -165,6 +166,7 @@ class Input { */ public function arr($name, $default = array(), $nonempty = false) { if(!isset($this->access[$name])) return $default; + if(!is_array($this->access[$name])) return $default; if($nonempty && empty($this->access[$name])) return $default; return (array) $this->access[$name]; @@ -183,8 +185,6 @@ class PostInput extends Input { */ function __construct() { $this->access = &$_POST; - unset ($this->post); - unset ($this->get); } /** @@ -211,8 +211,6 @@ class GetInput extends Input { */ function __construct() { $this->access = &$_GET; - unset ($this->post); - unset ($this->get); } /** -- cgit v1.2.3 From 5d0aaf958325f500ce69cfb79e69eb0d8f83fdeb Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Thu, 28 Jun 2012 17:17:24 +0200 Subject: treat empty string inputs as unset for int and bool --- inc/Input.class.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'inc/Input.class.php') diff --git a/inc/Input.class.php b/inc/Input.class.php index 1ea5e031f..f4174404a 100644 --- a/inc/Input.class.php +++ b/inc/Input.class.php @@ -118,6 +118,7 @@ class Input { public function int($name, $default = 0, $nonempty = false) { if(!isset($this->access[$name])) return $default; if(is_array($this->access[$name])) return $default; + if($this->access[$name] === '') return $default; if($nonempty && empty($this->access[$name])) return $default; return (int) $this->access[$name]; @@ -151,6 +152,8 @@ class Input { */ public function bool($name, $default = false, $nonempty = false) { if(!isset($this->access[$name])) return $default; + if(is_array($this->access[$name])) return $default; + if($this->access[$name] === '') return $default; if($nonempty && empty($this->access[$name])) return $default; return (bool) $this->access[$name]; @@ -223,4 +226,4 @@ class GetInput extends Input { parent::set($name, $value); $_REQUEST[$name] = $value; } -} \ No newline at end of file +} -- cgit v1.2.3 From d9e9c1bb60342ce88be150cc05de21dabfe130b2 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Thu, 9 Aug 2012 23:28:11 +0200 Subject: extract method for Input class makes it easier to access our do parameters --- inc/Input.class.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'inc/Input.class.php') diff --git a/inc/Input.class.php b/inc/Input.class.php index f4174404a..35aecdb45 100644 --- a/inc/Input.class.php +++ b/inc/Input.class.php @@ -175,6 +175,40 @@ class Input { return (array) $this->access[$name]; } + /** + * Create a simple key from an array key + * + * This is useful to access keys where the information is given as an array key or as a single array value. + * For example when the information was submitted as the name of a submit button. + * + * This function directly changes the access array. + * + * Eg. $_REQUEST['do']['save']='Speichern' becomes $_REQUEST['do'] = 'save' + * + * This function returns the $INPUT object itself for easy chaining + * + * @param $name + * @return Input + */ + public function extract($name){ + if(!isset($this->access[$name])) return $this; + if(!is_array($this->access[$name])) return $this; + $keys = array_keys($this->access[$name]); + if(!$keys){ + // this was an empty array + $this->remove($name); + return $this; + } + // get the first key + $value = array_shift($keys); + if($value === 0){ + // we had a numeric array, assume the value is not in the key + $value = array_shift($this->access[$name]); + } + + $this->set($name, $value); + return $this; + } } /** -- cgit v1.2.3