summaryrefslogtreecommitdiff
path: root/inc/Input.class.php
diff options
context:
space:
mode:
Diffstat (limited to 'inc/Input.class.php')
-rw-r--r--inc/Input.class.php67
1 files changed, 51 insertions, 16 deletions
diff --git a/inc/Input.class.php b/inc/Input.class.php
index e7eef1c29..199994d8d 100644
--- a/inc/Input.class.php
+++ b/inc/Input.class.php
@@ -21,6 +21,11 @@ class Input {
protected $access;
/**
+ * @var Callable
+ */
+ protected $filter;
+
+ /**
* Intilizes the Input class and it subcomponents
*/
function __construct() {
@@ -31,6 +36,32 @@ class Input {
}
/**
+ * Apply 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 accepts one string parameter and returns 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
*
* Basically a wrapper around isset. When called on the $post and $get subclasses,
@@ -52,7 +83,6 @@ class Input {
*
* @see isset
* @param string $name Parameter name
- * @return bool
*/
public function remove($name) {
if(isset($this->access[$name])) {
@@ -77,8 +107,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;
}
/**
@@ -100,7 +131,7 @@ class Input {
* @param string $name Parameter name
* @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
+ * @return mixed (reference)
*/
public function &ref($name, $default = '', $nonempty = false) {
if(!isset($this->access[$name]) || ($nonempty && empty($this->access[$name]))) {
@@ -114,33 +145,35 @@ class Input {
* 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
+ * @param int $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, $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;
}
/**
* 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
+ * @param string $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 = '', $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 +191,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 +210,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;
}
/**
@@ -210,7 +245,7 @@ class Input {
*
* This function returns the $INPUT object itself for easy chaining
*
- * @param $name
+ * @param string $name
* @return Input
*/
public function extract($name){