Skip to content

Instantly share code, notes, and snippets.

@mbunge
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbunge/52da3148fdbba3805d95 to your computer and use it in GitHub Desktop.
Save mbunge/52da3148fdbba3805d95 to your computer and use it in GitHub Desktop.
Simple input class to fetch input data without usage of global vars like $_POST. Get in touch and follow at twitter http://twitter.com/makk_eightbit! Thank you!
<?php
/**
* @author Marco Bunge <marco_bunge@web.de>
* @copyright 2015 Marco Bunge
* Get in touch and follow at twitter http://twitter.com/makk_eightbit! Thank you!
*/
/**
* Class Input
*/
class Input
{
/**
* @var array
*/
private $inputInternal = array();
/**
* Allowed escape chars for deep sanitize
* @var array
*/
private $defaultEscapeChars = array('"', '\\', '(', ')', '\'');
/**
* magic method for receiving any type of input input (exp: $_POST -> Input::getPost() or Input::post())
*
* If single input is an array, you need to deactivate filtering: Input::post(false)
* If you want to get only a few input elements, just add an array with input names: Input::post(true, array('password', 'email'))
*
* @param $name
* @param array $arguments
* @return array|bool|mixed
*/
public function __call($name, array $arguments = array())
{
$name = strtolower($name);
//suggest inputType
//inputType could be Request::get{InputType} or Request::inputType
$inputType = strpos($name, 'get') === 0 && strlen($name) > 3 ? substr($name, 3, strlen($name)) : $name;
$filtered = array_key_exists(0, $arguments) ? $arguments[0] : true;
$only = array_key_exists(1, $arguments) ? $arguments[1] : array();
$result = $this->getInput($inputType, $filtered, $only);
return $result;
}
/**
* Get input by inputType.
* $only is an array of keys in input and reduce result to giver keys
* if filtered is set to true input will return filtered by server default filter, otherwise raw input will returned
*
* @param string $inputType
* @param bool $filtered
* @param array $only
* @return array
*/
public function getInput($inputType, $filtered = true, array $only = array())
{
$phpInputVarName = '_' . strtoupper($inputType);
$phpInputType = 'INPUT' . $phpInputVarName;
$result = array();
if (array_key_exists($phpInputVarName, $GLOBALS) && defined($phpInputType)) {
if (!$this->hasInputInternal($phpInputType)) {
$input = $GLOBALS[$phpInputVarName];
$inputFilter = array();
foreach ($input as $key => $value) {
$inputFilter[$key] = FILTER_DEFAULT;
}
$filterInputType = constant($phpInputType);
//Fallback since INPUT_SESSION and INPUT_REQUEST not implemented yet
$filteredInput =
$filterInputType === INPUT_SESSION || $filterInputType === INPUT_REQUEST ?
filter_var_array($input, $inputFilter, true) :
filter_input_array(constant($phpInputType), $inputFilter, true);
if($input === null){
$input = array();
}
if($filteredInput === null){
$filteredInput = array();
}
$this->setInputInternal($phpInputType, $input, $filteredInput);
}
$input = $this->getInputInternal($phpInputType);
$result = $filtered === true ? $input['filtered'] : $input['raw'];
}
if (count($only) > 0) {
$result = $this->reduceArray($result, $only);
}
$result = $this->sanitizeDeep($result, $this->defaultEscapeChars, '');
//sanitize deep
return $result;
}
/**
* @param $input
* @param array $chars
* @param string $defaultReplacement
* @return array
*/
public function sanitizeDeep($input, array $chars = array(), $defaultReplacement = ''){
if(is_array($input)){
foreach($input as $key => $value){
$newValue = $value;
if(is_array($newValue)){
$newValue = $this->sanitizeDeep($newValue, $chars, $defaultReplacement);
}
if(is_string($newValue)){
$newValue = str_replace($chars, $defaultReplacement, $newValue);
}
$input[$key] = $newValue;
}
}
return $input;
}
/**
* @param $inputType
* @return bool
*/
protected function hasInputInternal($inputType)
{
return array_key_exists($inputType, $this->inputInternal);
}
/**
* @param $inputType
* @return bool
*/
protected function getInputInternal($inputType)
{
$inputs = false;
if ($this->hasInputInternal($inputType)) {
$inputs = $this->inputInternal[$inputType];
}
return $inputs;
}
/**
* @param $inputType
* @param array $raw
* @param array $filtered
*/
protected function setInputInternal($inputType, array $raw = array(), array $filtered = array())
{
$this->inputInternal[$inputType] = array(
'raw' => $raw,
'filtered' => $filtered
);
}
/**
* @param $data
* @param array $definition
* @param bool $add_empty
* @return mixed
*/
public function filterInput($data, array $definition = array(), $add_empty = true)
{
return filter_var_array($data, $definition, $add_empty);
}
/**
* @param array $array
* @param array $keys
* @return array
*/
protected function reduceArray(array $array, array $keys = array())
{
$result = $array;
if (is_array($keys)) {
$reducedArray = array();
foreach ($keys as $key) {
$value = array_key_exists($key, $result) ? $result[$key] : null;
$reducedArray[$key] = $value;
}
$result = $reducedArray;
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment