In module input validation logic
This commit is contained in:
parent
8cd9c31508
commit
94a9e08661
3 changed files with 408 additions and 14 deletions
|
@ -9,7 +9,7 @@
|
|||
* Redistribution of these files must retain the above copyright notice.
|
||||
*
|
||||
* @author Josh Sherman <pickles@joshtronic.com>
|
||||
* @copyright Copyright 2007-2012, Josh Sherman
|
||||
* @copyright Copyright 2007-2013, Josh Sherman
|
||||
* @license http://www.opensource.org/licenses/mit-license.html
|
||||
* @package PICKLES
|
||||
* @link https://github.com/joshtronic/pickles
|
||||
|
@ -113,6 +113,18 @@ class Module extends Object
|
|||
*/
|
||||
protected $session = null;
|
||||
|
||||
/**
|
||||
* AJAX
|
||||
*
|
||||
* Whether or not the module is being called via AJAX. This determines if
|
||||
* errors should be returned as JSON or if it should use the Error class
|
||||
* which can be interrogated from within a template.
|
||||
*
|
||||
* @access protected
|
||||
* @var boolean, false (not AJAX) by default
|
||||
*/
|
||||
protected $ajax = false;
|
||||
|
||||
/**
|
||||
* Method
|
||||
*
|
||||
|
@ -123,6 +135,16 @@ class Module extends Object
|
|||
*/
|
||||
protected $method = null;
|
||||
|
||||
/**
|
||||
* Validate
|
||||
*
|
||||
* Variables to validate.
|
||||
*
|
||||
* @access protected
|
||||
* @var array, null by default
|
||||
*/
|
||||
protected $get = null;
|
||||
|
||||
/**
|
||||
* Hash
|
||||
*
|
||||
|
@ -180,8 +202,9 @@ class Module extends Object
|
|||
* controller (the registration page calls the login page in this manner.
|
||||
*
|
||||
* @param boolean $autorun optional flag to autorun __default()
|
||||
* @param boolean $valiate optional flag to disable input validation during autorun
|
||||
*/
|
||||
public function __construct($autorun = false)
|
||||
public function __construct($autorun = false, $validate = true)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
|
@ -190,6 +213,16 @@ class Module extends Object
|
|||
|
||||
if ($autorun === true)
|
||||
{
|
||||
if ($validate === true)
|
||||
{
|
||||
$errors = $this->__validate();
|
||||
|
||||
if ($errors !== false)
|
||||
{
|
||||
exit('Errors encountered, this is a @todo for form validation when calling modules from inside of modules');
|
||||
}
|
||||
}
|
||||
|
||||
$this->__default();
|
||||
}
|
||||
}
|
||||
|
@ -269,6 +302,157 @@ class Module extends Object
|
|||
throw new Exception('Only Controller can perform setRequest()');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate
|
||||
*/
|
||||
public function __validate()
|
||||
{
|
||||
$errors = array();
|
||||
|
||||
if ($this->validate !== false)
|
||||
{
|
||||
switch (strtoupper($this->method))
|
||||
{
|
||||
case 'GET': $global = &$_GET; break;
|
||||
case 'POST': $global = &$_POST; break;
|
||||
default: $global = &$_REQUEST; break;
|
||||
}
|
||||
|
||||
foreach ($this->validate as $variable => $rules)
|
||||
{
|
||||
if (!is_array($rules))
|
||||
{
|
||||
$variable = $rules;
|
||||
$rules = true;
|
||||
}
|
||||
|
||||
if (isset($global[$variable]) && !String::isEmpty($global[$variable]))
|
||||
{
|
||||
$value = $global[$variable];
|
||||
|
||||
if (is_array($rules))
|
||||
{
|
||||
foreach ($rules as $rule => $message)
|
||||
{
|
||||
$rule = explode(':', $rule);
|
||||
|
||||
switch (strtolower($rule[0]))
|
||||
{
|
||||
// @todo case 'alpha':
|
||||
// @todo case 'alphanumeric':
|
||||
// @todo case 'date':
|
||||
|
||||
// {{{ Checks using filter_var()
|
||||
|
||||
case 'filter':
|
||||
if (count($rule) < 2)
|
||||
{
|
||||
throw new Exception('Invalid validation rule, expected: "validate:boolean|email|float|int|ip|regex|url".');
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (strtolower($rule[1]))
|
||||
{
|
||||
case 'boolean':
|
||||
case 'email':
|
||||
case 'float':
|
||||
case 'int':
|
||||
case 'ip':
|
||||
case 'regex':
|
||||
case 'url':
|
||||
$filter = constant('FILTER_VALIDATE_' . strtoupper($rule[1]));
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Exception('Invalid filter, expecting boolean, email, float, int, ip, regex or url.');
|
||||
break;
|
||||
}
|
||||
|
||||
if (!filter_var($value, $filter))
|
||||
{
|
||||
$errors[] = $message;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
// }}}
|
||||
// {{{ Checks using strlen()
|
||||
|
||||
case 'length':
|
||||
if (count($rule) < 3)
|
||||
{
|
||||
throw new Exception('Invalid validation rule, expected: "length:<|<=|==|!=|>=|>:integer".');
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!filter_var($rule[2], FILTER_VALIDATE_INT))
|
||||
{
|
||||
throw new Exception('Invalid length value, expecting an integer.');
|
||||
}
|
||||
else
|
||||
{
|
||||
$length = strlen($value);
|
||||
|
||||
switch ($rule[1])
|
||||
{
|
||||
case '<': $valid = $length < $rule[2]; break;
|
||||
case '<=': $valid = $length <= $rule[2]; break;
|
||||
case '==': $valid = $length == $rule[2]; break;
|
||||
case '!=': $valid = $length != $rule[2]; break;
|
||||
case '>=': $valid = $length >= $rule[2]; break;
|
||||
case '>': $valid = $length > $rule[2]; break;
|
||||
|
||||
default:
|
||||
throw new Exception('Invalid operator, expecting <, <=, ==, !=, >= or >.');
|
||||
break;
|
||||
}
|
||||
|
||||
if ($valid === true)
|
||||
{
|
||||
$errors[] = $message;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
// }}}
|
||||
|
||||
// @todo case 'range':
|
||||
|
||||
// {{{ Checks using preg_match()
|
||||
|
||||
case 'regex':
|
||||
if (count($rule) < 2)
|
||||
{
|
||||
throw new Exception('Invalid validation rule, expected: "regex:string".');
|
||||
}
|
||||
else
|
||||
{
|
||||
if (preg_match($rule[1], $value))
|
||||
{
|
||||
$errors[] = $message;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
// }}}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$errors[] = 'The ' . $variable . ' field is required.';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $errors == array() ? false : $errors;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue