From 37abef5f8744159697aa2dc18cc468477f2ca965 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Tue, 23 Sep 2014 20:08:09 +0200 Subject: added filter method to INPUT class the filter() function can be chained between the accessor and the value function to get a filtered value. When no filter allable is given in the filter() function, stripctl() is used to strip all control chars (ASCII<32) Examples: $INPUT->post->filter()->str('foobar'); $INPUT->get->filter('myfilter')->int('baz'); --- inc/Input.class.php | 58 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 11 deletions(-) (limited to 'inc') diff --git a/inc/Input.class.php b/inc/Input.class.php index e7eef1c29..c1dc820ec 100644 --- a/inc/Input.class.php +++ b/inc/Input.class.php @@ -20,6 +20,11 @@ class Input { protected $access; + /** + * @var Callable + */ + protected $filter; + /** * Intilizes the Input class and it subcomponents */ @@ -30,6 +35,32 @@ class Input { $this->server = new ServerInput(); } + /** + * Applied the set filter to the given value + * + * @param string $data + * @return string + */ + protected function applyfilter($data){ + if(!$this->filter) return $data; + return call_user_func($this->filter, $data); + } + + /** + * Return a filtered copy of the input object + * + * Expects a callable that accept one string parameter and returnes a filtered string. + * + * @param Callable|string $filter + * @return Input + */ + public function filter($filter='stripctl'){ + $this->filter = $filter; + $clone = clone $this; + $this->filter = ''; + return $clone; + } + /** * Check if a parameter was set * @@ -77,8 +108,9 @@ class Input { */ 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]; + $value = $this->applyfilter($this->access[$name]); + if($nonempty && empty($value)) return $default; + return $value; } /** @@ -121,10 +153,11 @@ 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; + $value = $this->applyfilter($this->access[$name]); + if($value === '') return $default; + if($nonempty && empty($value)) return $default; - return (int) $this->access[$name]; + return (int) $value; } /** @@ -138,9 +171,10 @@ class Input { 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; + $value = $this->applyfilter($this->access[$name]); + if($nonempty && empty($value)) return $default; - return (string) $this->access[$name]; + return (string) $value; } /** @@ -158,7 +192,8 @@ class Input { public function valid($name, $valids, $default = null) { if(!isset($this->access[$name])) return $default; if(is_array($this->access[$name])) return $default; // we don't allow arrays - $found = array_search($this->access[$name], $valids); + $value = $this->applyfilter($this->access[$name]); + $found = array_search($value, $valids); if($found !== false) return $valids[$found]; // return the valid value for type safety return $default; } @@ -176,10 +211,11 @@ 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; + $value = $this->applyfilter($this->access[$name]); + if($value === '') return $default; + if($nonempty && empty($value)) return $default; - return (bool) $this->access[$name]; + return (bool) $value; } /** -- cgit v1.2.3 From 1dc0e65fa0933a1ad6ef9a73c35feb81c96f2961 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Fri, 26 Sep 2014 11:04:32 +0200 Subject: fixed typos in docblock comments --- inc/Input.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'inc') diff --git a/inc/Input.class.php b/inc/Input.class.php index c1dc820ec..94da2a10e 100644 --- a/inc/Input.class.php +++ b/inc/Input.class.php @@ -36,7 +36,7 @@ class Input { } /** - * Applied the set filter to the given value + * Apply the set filter to the given value * * @param string $data * @return string @@ -49,7 +49,7 @@ class Input { /** * Return a filtered copy of the input object * - * Expects a callable that accept one string parameter and returnes a filtered string. + * Expects a callable that accepts one string parameter and returns a filtered string * * @param Callable|string $filter * @return Input -- cgit v1.2.3