| 1 |
<?php |
| 2 |
class PMXE_Input { |
| 3 |
protected $filters = array('stripslashes'); |
| 4 |
|
| 5 |
public function read($inputArray, $paramName, $default = NULL) { |
| 6 |
if (is_array($paramName) and ! is_null($default)) { |
| 7 |
throw new Exception('Either array of parameter names with default values as the only argument or param name and default value as seperate arguments are expected.'); |
| 8 |
} |
| 9 |
if (is_array($paramName)) { |
| 10 |
foreach ($paramName as $param => $def) { |
| 11 |
if (isset($inputArray[$param])) { |
| 12 |
$paramName[$param] = $this->applyFilters($inputArray[$param]); |
| 13 |
} |
| 14 |
} |
| 15 |
return $paramName; |
| 16 |
} else { |
| 17 |
return isset($inputArray[$paramName]) ? $this->applyFilters($inputArray[$paramName]) : $default; |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
public function get($paramName, $default = NULL) { |
| 22 |
$this->addFilter('htmlspecialchars'); |
| 23 |
$this->addFilter('strip_tags'); |
| 24 |
$this->addFilter('esc_sql'); |
| 25 |
$result = $this->read($_GET, $paramName, $default); |
| 26 |
$this->removeFilter('htmlspecialchars'); |
| 27 |
$this->removeFilter('strip_tags'); |
| 28 |
$this->removeFilter('esc_sql'); |
| 29 |
return $result; |
| 30 |
} |
| 31 |
|
| 32 |
public function post($paramName, $default = NULL) { |
| 33 |
return $this->read($_POST, $paramName, $default); |
| 34 |
} |
| 35 |
|
| 36 |
public function cookie($paramName, $default = NULL) { |
| 37 |
return $this->read($_COOKIE, $paramName, $default); |
| 38 |
} |
| 39 |
|
| 40 |
public function request($paramName, $default = NULL) { |
| 41 |
return $this->read($_GET + $_POST + $_COOKIE, $paramName, $default); |
| 42 |
} |
| 43 |
|
| 44 |
public function getpost($paramName, $default = NULL) { |
| 45 |
return $this->read($_GET + $_POST, $paramName, $default); |
| 46 |
} |
| 47 |
|
| 48 |
public function server($paramName, $default = NULL) { |
| 49 |
return $this->read($_SERVER, $paramName, $default); |
| 50 |
} |
| 51 |
|
| 52 |
public function addFilter($callback) { |
| 53 |
if ( ! is_callable($callback)) { |
| 54 |
throw new Exception(get_class($this) . '::' . __METHOD__ . ' parameter must be a proper callback function reference.'); |
| 55 |
} |
| 56 |
if ( ! in_array($callback, $this->filters)) { |
| 57 |
$this->filters[] = $callback; |
| 58 |
} |
| 59 |
return $this; |
| 60 |
} |
| 61 |
|
| 62 |
public function removeFilter($callback) { |
| 63 |
$this->filters = array_diff($this->filters, array($callback)); |
| 64 |
return $this; |
| 65 |
} |
| 66 |
|
| 67 |
protected function applyFilters($val) { |
| 68 |
if (is_array($val)) { |
| 69 |
foreach ($val as $k => $v) { |
| 70 |
$val[$k] = $this->applyFilters($v); |
| 71 |
} |
| 72 |
} else { |
| 73 |
foreach ($this->filters as $filter) { |
| 74 |
$val = call_user_func($filter, $val); |
| 75 |
} |
| 76 |
} |
| 77 |
return $val; |
| 78 |
} |
| 79 |
} |