mirror of
https://github.com/atlanticbiomedical/portal-legacy.git
synced 2025-07-02 01:47:28 -04:00
initial commit
This commit is contained in:
85
lib/symfony/validator/sfCallbackValidator.class.php
Executable file
85
lib/symfony/validator/sfCallbackValidator.class.php
Executable file
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the symfony package.
|
||||
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* sfCallbackValidator allows you to use a custom callback function or method to
|
||||
* validate the input. The function should return true on valid and false on invalid
|
||||
* and should be callable using is_callable().
|
||||
*
|
||||
* <b>Required parameters:</b>
|
||||
*
|
||||
* # <b>callback</b> - [none] - A valid callback function or Class::method array.
|
||||
* When using class/method specify it as an array in yml file as [class, method]
|
||||
*
|
||||
* <b>Optional parameters:</b>
|
||||
*
|
||||
* # <b>invalid_error</b> - [Invalid input] - An error message to use when the
|
||||
* input fails the callback check
|
||||
*
|
||||
* @package symfony
|
||||
* @subpackage validator
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* @version SVN: $Id: sfCallbackValidator.class.php 3329 2007-01-23 08:29:34Z fabien $
|
||||
*/
|
||||
class sfCallbackValidator extends sfValidator
|
||||
{
|
||||
/**
|
||||
* Executes this validator.
|
||||
*
|
||||
* @param string A parameter value
|
||||
* @param string An error message reference
|
||||
*
|
||||
* @return boolean true, if this validator executes successfully, otherwise false
|
||||
*/
|
||||
public function execute(&$value, &$error)
|
||||
{
|
||||
$callback = $this->getParameterHolder()->get('callback');
|
||||
|
||||
if (!call_user_func($callback, $value))
|
||||
{
|
||||
$error = $this->getParameterHolder()->get('invalid_error');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes this validator.
|
||||
*
|
||||
* @param sfContext The current application context
|
||||
* @param array An associative array of initialization parameters
|
||||
*
|
||||
* @return boolean true, if initialization completes successfully, otherwise false
|
||||
*/
|
||||
public function initialize($context, $parameters = null)
|
||||
{
|
||||
// initialize parent
|
||||
parent::initialize($context);
|
||||
|
||||
// set defaults
|
||||
$this->getParameterHolder()->set('callback', null);
|
||||
$this->getParameterHolder()->set('invalid_error', 'Invalid input');
|
||||
|
||||
$this->getParameterHolder()->add($parameters);
|
||||
|
||||
// check parameters
|
||||
if (!is_callable($this->getParameterHolder()->get('callback')))
|
||||
{
|
||||
// no pattern specified
|
||||
$error = 'Callback function must be a valid callback using is_callable()';
|
||||
|
||||
throw new sfValidatorException($error);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
61
lib/symfony/validator/sfCompareValidator.class.php
Executable file
61
lib/symfony/validator/sfCompareValidator.class.php
Executable file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the symfony package.
|
||||
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* sfCompareValidator checks the equality of two different request parameters.
|
||||
*
|
||||
* passwordValidator:
|
||||
* class: sfCompareValidator
|
||||
* param:
|
||||
* check: password2
|
||||
* compare_error: The passwords you entered do not match. Please try again.
|
||||
*
|
||||
* @package symfony
|
||||
* @subpackage validator
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* @version SVN: $Id: sfCompareValidator.class.php 3233 2007-01-11 21:01:08Z fabien $
|
||||
*/
|
||||
class sfCompareValidator extends sfValidator
|
||||
{
|
||||
/**
|
||||
* Executes this validator.
|
||||
*
|
||||
* @param mixed A file or parameter value/array
|
||||
* @param error An error message reference
|
||||
*
|
||||
* @return bool true, if this validator executes successfully, otherwise false
|
||||
*/
|
||||
public function execute(&$value, &$error)
|
||||
{
|
||||
$check_param = $this->getParameterHolder()->get('check');
|
||||
$check_value = $this->getContext()->getRequest()->getParameter($check_param);
|
||||
|
||||
if ($value !== $check_value)
|
||||
{
|
||||
$error = $this->getParameterHolder()->get('compare_error');
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function initialize($context, $parameters = null)
|
||||
{
|
||||
// initialize parent
|
||||
parent::initialize($context);
|
||||
|
||||
// set defaults
|
||||
$this->getParameterHolder()->set('compare_error', 'Invalid input');
|
||||
|
||||
$this->getParameterHolder()->add($parameters);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
137
lib/symfony/validator/sfDateValidator.class.php
Executable file
137
lib/symfony/validator/sfDateValidator.class.php
Executable file
@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the symfony package.
|
||||
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* (c) 2004-2006 Sean Kerr.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* sfDateValidator verifies a parameter is of a date format.
|
||||
*
|
||||
* @package symfony
|
||||
* @subpackage validator
|
||||
* @author Nick Lane <nick.lane@internode.on.net>
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* @author Sean Kerr <skerr@mojavi.org>
|
||||
* @version SVN: $Id: sfDateValidator.class.php 3233 2007-01-11 21:01:08Z fabien $
|
||||
*/
|
||||
class sfDateValidator extends sfValidator
|
||||
{
|
||||
/**
|
||||
* Execute this validator.
|
||||
*
|
||||
* @param mixed A file or parameter value/array
|
||||
* @param error An error message reference
|
||||
*
|
||||
* @return bool true, if this validator executes successfully, otherwise false
|
||||
*/
|
||||
public function execute(&$value, &$error)
|
||||
{
|
||||
$culture = $this->getContext()->getUser()->getCulture();
|
||||
|
||||
// Validate the given date
|
||||
$value1 = $this->getValidDate($value, $culture);
|
||||
if (!$value1)
|
||||
{
|
||||
$error = $this->getParameter('date_error');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Is there a compare to do?
|
||||
$compareDateParam = $this->getParameter('compare');
|
||||
$compareDate = $this->getContext()->getRequest()->getParameter($compareDateParam);
|
||||
|
||||
// If the compare date is given
|
||||
if ($compareDate)
|
||||
{
|
||||
$operator = trim($this->getParameter('operator', '=='), '\'" ');
|
||||
$value2 = $this->getValidDate($compareDate, $culture);
|
||||
|
||||
// If the check date is valid, compare it. Otherwise ignore the comparison
|
||||
if ($value2)
|
||||
{
|
||||
$valid = false;
|
||||
switch ($operator)
|
||||
{
|
||||
case '>':
|
||||
$valid = $value1 > $value2;
|
||||
break;
|
||||
case '>=':
|
||||
$valid = $value1 >= $value2;
|
||||
break;
|
||||
case '==':
|
||||
$valid = $value1 == $value2;
|
||||
break;
|
||||
case '<=':
|
||||
$valid = $value1 <= $value2;
|
||||
break;
|
||||
case '<':
|
||||
$valid = $value1 < $value2;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new sfValidatorException(sprintf('Invalid date comparison operator "%s"', $operator));
|
||||
}
|
||||
|
||||
if (!$valid)
|
||||
{
|
||||
$error = $this->getParameter('compare_error');
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given date into a Unix timestamp.
|
||||
*
|
||||
* Returns null if the date is invalid
|
||||
*
|
||||
* @param $value Date to convert
|
||||
* @param $culture Language culture to use
|
||||
*/
|
||||
protected function getValidDate($value, $culture)
|
||||
{
|
||||
// Use the language culture date format
|
||||
$result = sfI18N::getDateForCulture($value, $culture);
|
||||
list($d, $m, $y) = $result;
|
||||
|
||||
// Make sure the date is a valid gregorian calendar date also
|
||||
if ($result === null || !checkdate($m, $d, $y))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return strtotime("$y-$m-$d 00:00");
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the validator.
|
||||
*
|
||||
* @param sfContext The current application context
|
||||
* @param array An associative array of initialization parameters
|
||||
*
|
||||
* @return bool true, if initialization completes successfully, otherwise false
|
||||
*/
|
||||
public function initialize($context, $parameters = null)
|
||||
{
|
||||
// Initialize parent
|
||||
parent::initialize($context, $parameters);
|
||||
|
||||
// Set defaults
|
||||
$this->getParameterHolder()->set('date_error', 'Invalid date');
|
||||
$this->getParameterHolder()->set('compare_error', 'Compare failed');
|
||||
|
||||
$this->getParameterHolder()->add($parameters);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
116
lib/symfony/validator/sfEmailValidator.class.php
Executable file
116
lib/symfony/validator/sfEmailValidator.class.php
Executable file
@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the symfony package.
|
||||
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* (c) 2004-2006 Sean Kerr.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* sfEmailValidator verifies a parameter contains a value that qualifies as an
|
||||
* email address.
|
||||
*
|
||||
* @package symfony
|
||||
* @subpackage validator
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* @author Sean Kerr <skerr@mojavi.org>
|
||||
* @version SVN: $Id: sfEmailValidator.class.php 3233 2007-01-11 21:01:08Z fabien $
|
||||
*/
|
||||
class sfEmailValidator extends sfValidator
|
||||
{
|
||||
/**
|
||||
* Executes this validator.
|
||||
*
|
||||
* @param mixed A file or parameter value/array
|
||||
* @param error An error message reference
|
||||
*
|
||||
* @return bool true, if this validator executes successfully, otherwise false
|
||||
*/
|
||||
public function execute(&$value, &$error)
|
||||
{
|
||||
$strict = $this->getParameterHolder()->get('strict');
|
||||
if ($strict == true)
|
||||
{
|
||||
$re = '/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i';
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Cal Henderson: http://iamcal.com/publish/articles/php/parsing_email/pdf/
|
||||
* The long regular expression below is made by the following code
|
||||
* fragment:
|
||||
*
|
||||
* $qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
|
||||
* $dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
|
||||
* $atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'
|
||||
* . '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
|
||||
* $quoted_pair = '\\x5c\\x00-\\x7f';
|
||||
* $domain_literal = "\\x5b($dtext|$quoted_pair)*\\x5d";
|
||||
* $quoted_string = "\\x22($qtext|$quoted_pair)*\\x22";
|
||||
* $domain_ref = $atom;
|
||||
* $sub_domain = "($domain_ref|$domain_literal)";
|
||||
* $word = "($atom|$quoted_string)";
|
||||
* $domain = "$sub_domain(\\x2e$sub_domain)*";
|
||||
* $local_part = "$word(\\x2e$word)*";
|
||||
* $addr_spec = "$local_part\\x40$domain";
|
||||
*/
|
||||
|
||||
$re = '/^([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-'
|
||||
.'\\x5d\\x7f-\\xff]+|\\x22([^\\x0d\\x22\\x5c\\x80-\\xff]|\\x5c\\x00-'
|
||||
.'\\x7f)*\\x22)(\\x2e([^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-'
|
||||
.'\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x22([^\\x0d\\x22\\x5c\\x80'
|
||||
.'-\\xff]|\\x5c\\x00-\\x7f)*\\x22))*\\x40([^\\x00-\\x20\\x22\\x28\\x29'
|
||||
.'\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+|\\x5b([^'
|
||||
.'\\x0d\\x5b-\\x5d\\x80-\\xff]|\\x5c\\x00-\\x7f)*\\x5d)(\\x2e([^\\x00-'
|
||||
.'\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-'
|
||||
.'\\xff]+|\\x5b([^\\x0d\\x5b-\\x5d\\x80-\\xff]|\\x5c\\x00-\\x7f)*'
|
||||
.'\\x5d))*$/'
|
||||
;
|
||||
}
|
||||
|
||||
if (!preg_match($re, $value))
|
||||
{
|
||||
$error = $this->getParameterHolder()->get('email_error');
|
||||
return false;
|
||||
}
|
||||
|
||||
$checkDomain = $this->getParameterHolder()->get('check_domain');
|
||||
if ($checkDomain && function_exists('checkdnsrr'))
|
||||
{
|
||||
$tokens = explode('@', $value);
|
||||
if (!checkdnsrr($tokens[1], 'MX') && !checkdnsrr($tokens[1], 'A'))
|
||||
{
|
||||
$error = $this->getParameterHolder()->get('email_error');
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes this validator.
|
||||
*
|
||||
* @param sfContext The current application context
|
||||
* @param array An associative array of initialization parameters
|
||||
*
|
||||
* @return bool true, if initialization completes successfully, otherwise false
|
||||
*/
|
||||
public function initialize($context, $parameters = null)
|
||||
{
|
||||
// initialize parent
|
||||
parent::initialize($context);
|
||||
|
||||
// set defaults
|
||||
$this->getParameterHolder()->set('strict', true);
|
||||
$this->getParameterHolder()->set('check_domain', false);
|
||||
$this->getParameterHolder()->set('email_error', 'Invalid input');
|
||||
|
||||
$this->getParameterHolder()->add($parameters);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
111
lib/symfony/validator/sfFileValidator.class.php
Executable file
111
lib/symfony/validator/sfFileValidator.class.php
Executable file
@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the symfony package.
|
||||
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* sfFileValidator allows you to apply constraints to file upload.
|
||||
*
|
||||
* <b>Optional parameters:</b>
|
||||
*
|
||||
* # <b>max_size</b> - [none] - Maximum file size length.
|
||||
* # <b>max_size_error</b> - [File is too large] - An error message to use when
|
||||
* file is too large.
|
||||
* # <b>mime_types</b> - [none] - An array of mime types the file
|
||||
* is allowed to match.
|
||||
* # <b>mime_types_error</b> - [Invalid mime type] - An error message to use when
|
||||
* file mime type does not match a value
|
||||
* listed in the mime types array.
|
||||
*
|
||||
* @package symfony
|
||||
* @subpackage validator
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* @version SVN: $Id: sfFileValidator.class.php 3233 2007-01-11 21:01:08Z fabien $
|
||||
*/
|
||||
class sfFileValidator extends sfValidator
|
||||
{
|
||||
/**
|
||||
* Executes this validator.
|
||||
*
|
||||
* @param mixed A file or parameter value/array
|
||||
* @param error An error message reference
|
||||
*
|
||||
* @return bool true, if this validator executes successfully, otherwise false
|
||||
*/
|
||||
public function execute(&$value, &$error)
|
||||
{
|
||||
$request = $this->getContext()->getRequest();
|
||||
|
||||
// file too large?
|
||||
$max_size = $this->getParameter('max_size');
|
||||
if ($max_size !== null && $max_size < $value['size'])
|
||||
{
|
||||
$error = $this->getParameter('max_size_error');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// supported mime types formats
|
||||
$mime_types = $this->getParameter('mime_types');
|
||||
if ($mime_types !== null && !in_array($value['type'], $mime_types))
|
||||
{
|
||||
$error = $this->getParameter('mime_types_error');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes this validator.
|
||||
*
|
||||
* @param sfContext The current application context
|
||||
* @param array An associative array of initialization parameters
|
||||
*
|
||||
* @return bool true, if initialization completes successfully, otherwise false
|
||||
*/
|
||||
public function initialize($context, $parameters = null)
|
||||
{
|
||||
// initialize parent
|
||||
parent::initialize($context);
|
||||
|
||||
// set defaults
|
||||
$this->getParameterHolder()->set('max_size', null);
|
||||
$this->getParameterHolder()->set('max_size_error', 'File is too large');
|
||||
$this->getParameterHolder()->set('mime_types', null);
|
||||
$this->getParameterHolder()->set('mime_types_error', 'Invalid mime type');
|
||||
|
||||
$this->getParameterHolder()->add($parameters);
|
||||
|
||||
// pre-defined categories
|
||||
$categories = array(
|
||||
'@web_images' => array(
|
||||
'image/jpeg',
|
||||
'image/pjpeg',
|
||||
'image/png',
|
||||
'image/x-png',
|
||||
'image/gif',
|
||||
),
|
||||
);
|
||||
|
||||
if (!is_array($this->getParameter('mime_types')))
|
||||
{
|
||||
if (isset($categories[$this->getParameter('mime_types')]))
|
||||
{
|
||||
$this->setParameter('mime_types', $categories[$this->getParameter('mime_types')]);
|
||||
}
|
||||
}
|
||||
elseif ($this->getParameter('mime_types', null))
|
||||
{
|
||||
$this->setParameter('mime_types', $this->getParameter('mime_types'));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
67
lib/symfony/validator/sfHtmlValidator.class.php
Executable file
67
lib/symfony/validator/sfHtmlValidator.class.php
Executable file
@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the symfony package.
|
||||
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* (c) 2004-2006 Sean Kerr.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @package symfony
|
||||
* @subpackage validator
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* @author Sean Kerr <skerr@mojavi.org>
|
||||
* @version SVN: $Id: sfHtmlValidator.class.php 3233 2007-01-11 21:01:08Z fabien $
|
||||
*/
|
||||
class sfHtmlValidator extends sfValidator
|
||||
{
|
||||
/**
|
||||
* Executes this validator.
|
||||
*
|
||||
* @param mixed A file or parameter value/array
|
||||
* @param error An error message reference
|
||||
*
|
||||
* @return bool true, if this validator executes successfully, otherwise false
|
||||
*/
|
||||
public function execute(&$value, &$error)
|
||||
{
|
||||
if (trim(strip_tags($value)) == '')
|
||||
{
|
||||
// If page contains an object or an image, it's ok
|
||||
if (preg_match('/<img/i', $value) || preg_match('/<object/i', $value))
|
||||
return true;
|
||||
else
|
||||
{
|
||||
$error = $this->getParameterHolder()->get('html_error');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes this validator.
|
||||
*
|
||||
* @param sfContext The current application context
|
||||
* @param array An associative array of initialization parameters
|
||||
*
|
||||
* @return bool true, if initialization completes successfully, otherwise false
|
||||
*/
|
||||
public function initialize($context, $parameters = null)
|
||||
{
|
||||
// initialize parent
|
||||
parent::initialize($context);
|
||||
|
||||
// set defaults
|
||||
$this->getParameterHolder()->set('html_error', 'Invalid input');
|
||||
|
||||
$this->getParameterHolder()->add($parameters);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
159
lib/symfony/validator/sfNumberValidator.class.php
Executable file
159
lib/symfony/validator/sfNumberValidator.class.php
Executable file
@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the symfony package.
|
||||
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* (c) 2004-2006 Sean Kerr.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* sfNumberValidator verifies a parameter is a number and allows you to apply
|
||||
* size constraints.
|
||||
*
|
||||
* <b>Optional parameters:</b>
|
||||
*
|
||||
* # <b>max</b> - [none] - Maximum number size.
|
||||
* # <b>max_error</b> - [Input is too large] - An error message to use when
|
||||
* input is too large.
|
||||
* # <b>min</b> - [none] - Minimum number size.
|
||||
* # <b>min_error</b> - [Input is too small] - An error message to use when
|
||||
* input is too small.
|
||||
* # <b>nan_error</b> - [Input is not a number] - Default error message when
|
||||
* input is not a number.
|
||||
* # <b>type</b> - [Any] - Type of number (Any, Float).
|
||||
* # <b>type_error</b> - [Input is not a number] - An error message to use when
|
||||
* input is not a number.
|
||||
*
|
||||
* @package symfony
|
||||
* @subpackage validator
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* @author Sean Kerr <skerr@mojavi.org>
|
||||
* @version SVN: $Id: sfNumberValidator.class.php 3233 2007-01-11 21:01:08Z fabien $
|
||||
*/
|
||||
class sfNumberValidator extends sfValidator
|
||||
{
|
||||
/**
|
||||
* Executes this validator.
|
||||
*
|
||||
* @param mixed A file or parameter value/array
|
||||
* @param error An error message reference
|
||||
*
|
||||
* @return bool true, if this validator executes successfully, otherwise false
|
||||
*/
|
||||
public function execute(&$value, &$error)
|
||||
{
|
||||
if (!is_numeric($value))
|
||||
{
|
||||
// it's NaN, what nerve!
|
||||
$error = $this->getParameterHolder()->get('nan_error');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$type = strtolower($this->getParameterHolder()->get('type'));
|
||||
|
||||
switch ($type)
|
||||
{
|
||||
case "decimal":
|
||||
case "float":
|
||||
{
|
||||
if (substr_count($value, '.') != 1)
|
||||
{
|
||||
// value isn't a float, shazbot!
|
||||
$error = $this->getParameterHolder()->get('type_error');
|
||||
return false;
|
||||
}
|
||||
|
||||
// cast our value to a float
|
||||
$value = (float) $value;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case "int":
|
||||
case "integer":
|
||||
{
|
||||
// Note: (Both 3 AND 3.0 are BOTH considered integers and 3.1 is not)
|
||||
if ((float) $value != (int) $value)
|
||||
{
|
||||
// is not an integer.
|
||||
$error = $this->getParameterHolder()->get('type_error');
|
||||
return false;
|
||||
}
|
||||
|
||||
// cast our value to an integer
|
||||
$value = (int) $value;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$min = $this->getParameterHolder()->get('min');
|
||||
|
||||
if ($min !== null && $value < $min)
|
||||
{
|
||||
// too small
|
||||
$error = $this->getParameterHolder()->get('min_error');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$max = $this->getParameterHolder()->get('max');
|
||||
|
||||
if ($max !== null && $value > $max)
|
||||
{
|
||||
// too large
|
||||
$error = $this->getParameterHolder()->get('max_error');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes this validator.
|
||||
*
|
||||
* @param sfContext The current application context
|
||||
* @param array An associative array of initialization parameters
|
||||
*
|
||||
* @return bool true, if initialization completes successfully, otherwise false
|
||||
*/
|
||||
public function initialize($context, $parameters = null)
|
||||
{
|
||||
// initialize parent
|
||||
parent::initialize($context);
|
||||
|
||||
// set defaults
|
||||
$this->getParameterHolder()->set('max', null);
|
||||
$this->getParameterHolder()->set('max_error', 'Input is too large');
|
||||
$this->getParameterHolder()->set('min', null);
|
||||
$this->getParameterHolder()->set('min_error', 'Input is too small');
|
||||
$this->getParameterHolder()->set('nan_error', 'Input is not a number');
|
||||
$this->getParameterHolder()->set('type', 'any');
|
||||
$this->getParameterHolder()->set('type_error', 'Input is not a number');
|
||||
|
||||
$this->getParameterHolder()->add($parameters);
|
||||
|
||||
// check user-specified parameters
|
||||
$type = strtolower($this->getParameterHolder()->get('type'));
|
||||
|
||||
// array of allowed types
|
||||
$allowed_types = array('any', 'decimal', 'float', 'int', 'integer');
|
||||
|
||||
if (!in_array(strtolower($type), $allowed_types))
|
||||
{
|
||||
// unknown type
|
||||
$error = 'Unknown number type "%s" in NumberValidator';
|
||||
$error = sprintf($error, $this->getParameterHolder()->get('type'));
|
||||
|
||||
throw new sfValidatorException($error);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
92
lib/symfony/validator/sfRegexValidator.class.php
Executable file
92
lib/symfony/validator/sfRegexValidator.class.php
Executable file
@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the symfony package.
|
||||
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* (c) 2004-2006 Sean Kerr.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* sfRegexValidator allows you to match a value against a regular expression
|
||||
* pattern.
|
||||
*
|
||||
* <b>Required parameters:</b>
|
||||
*
|
||||
* # <b>pattern</b> - [none] - A PCRE, preg_match() style regular expression
|
||||
* pattern.
|
||||
*
|
||||
* <b>Optional parameters:</b>
|
||||
*
|
||||
* # <b>match</b> - [true] - Indicates that the pattern must be
|
||||
* matched or must not match.
|
||||
* # <b>match_error</b> - [Invalid input] - An error message to use when the
|
||||
* input does not meet the regex
|
||||
* specifications.
|
||||
*
|
||||
* @package symfony
|
||||
* @subpackage validator
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* @author Sean Kerr <skerr@mojavi.org>
|
||||
* @version SVN: $Id: sfRegexValidator.class.php 3233 2007-01-11 21:01:08Z fabien $
|
||||
*/
|
||||
class sfRegexValidator extends sfValidator
|
||||
{
|
||||
/**
|
||||
* Executes this validator.
|
||||
*
|
||||
* @param string A parameter value
|
||||
* @param string An error message reference
|
||||
*
|
||||
* @return bool true, if this validator executes successfully, otherwise false
|
||||
*/
|
||||
public function execute(&$value, &$error)
|
||||
{
|
||||
$match = $this->getParameterHolder()->get('match');
|
||||
$pattern = $this->getParameterHolder()->get('pattern');
|
||||
|
||||
if (($match && !preg_match($pattern, $value)) ||
|
||||
(!$match && preg_match($pattern, $value)))
|
||||
{
|
||||
$error = $this->getParameterHolder()->get('match_error');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes this validator.
|
||||
*
|
||||
* @param sfContext The current application context
|
||||
* @param array An associative array of initialization parameters
|
||||
*
|
||||
* @return bool true, if initialization completes successfully, otherwise false
|
||||
*/
|
||||
public function initialize($context, $parameters = null)
|
||||
{
|
||||
// initialize parent
|
||||
parent::initialize($context);
|
||||
|
||||
// set defaults
|
||||
$this->getParameterHolder()->set('match', true);
|
||||
$this->getParameterHolder()->set('match_error', 'Invalid input');
|
||||
$this->getParameterHolder()->set('pattern', null);
|
||||
|
||||
$this->getParameterHolder()->add($parameters);
|
||||
|
||||
// check parameters
|
||||
if ($this->getParameterHolder()->get('pattern') == null)
|
||||
{
|
||||
// no pattern specified
|
||||
$error = 'Please specify a PCRE regular expression pattern for your registered RegexValidator';
|
||||
|
||||
throw new sfValidatorException($error);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
138
lib/symfony/validator/sfStringValidator.class.php
Executable file
138
lib/symfony/validator/sfStringValidator.class.php
Executable file
@ -0,0 +1,138 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the symfony package.
|
||||
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* (c) 2004-2006 Sean Kerr.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* sfStringValidator allows you to apply string-related constraints to a
|
||||
* parameter.
|
||||
*
|
||||
* <b>Optional parameters:</b>
|
||||
*
|
||||
* # <b>insensitive</b> - [false] - Whether or not the value check
|
||||
* against the array of values is
|
||||
* case-insensitive. <b>Note:</b>
|
||||
* When using this option, values
|
||||
* in the values array must be
|
||||
* entered in lower-case.
|
||||
* # <b>max</b> - [none] - Maximum string length.
|
||||
* # <b>max_error</b> - [Input is too long] - An error message to use when
|
||||
* input is too long.
|
||||
* # <b>min</b> - [none] - Minimum string length.
|
||||
* # <b>min_error</b> - [Input is too short] - An error message to use when
|
||||
* input is too short.
|
||||
* # <b>values</b> - [none] - An array of values the input
|
||||
* is allowed to match.
|
||||
* # <b>values_error</b> - [Invalid selection] - An error message to use when
|
||||
* input does not match a value
|
||||
* listed in the values array.
|
||||
*
|
||||
* @package symfony
|
||||
* @subpackage validator
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* @author Sean Kerr <skerr@mojavi.org>
|
||||
* @version SVN: $Id: sfStringValidator.class.php 3233 2007-01-11 21:01:08Z fabien $
|
||||
*/
|
||||
class sfStringValidator extends sfValidator
|
||||
{
|
||||
/**
|
||||
* Executes this validator.
|
||||
*
|
||||
* @param mixed A parameter value
|
||||
* @param error An error message reference
|
||||
*
|
||||
* @return bool true, if this validator executes successfully, otherwise false
|
||||
*/
|
||||
public function execute(&$value, &$error)
|
||||
{
|
||||
$decodedValue = sfToolkit::isUTF8($value) && function_exists('utf8_decode') ? utf8_decode($value) : $value;
|
||||
|
||||
$min = $this->getParameterHolder()->get('min');
|
||||
if ($min !== null && strlen(trim($decodedValue)) < $min)
|
||||
{
|
||||
// too short
|
||||
$error = $this->getParameterHolder()->get('min_error');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$max = $this->getParameterHolder()->get('max');
|
||||
if ($max !== null && strlen(trim($decodedValue)) > $max)
|
||||
{
|
||||
// too long
|
||||
$error = $this->getParameterHolder()->get('max_error');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$values = $this->getParameterHolder()->get('values');
|
||||
if ($values !== null)
|
||||
{
|
||||
if ($this->getParameterHolder()->get('insensitive'))
|
||||
{
|
||||
$value = strtolower($value);
|
||||
$found = false;
|
||||
foreach ($values as $avalue)
|
||||
{
|
||||
if ($value == strtolower($avalue))
|
||||
{
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$found)
|
||||
{
|
||||
// can't find a match
|
||||
$error = $this->getParameterHolder()->get('values_error');
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!in_array($value, (array) $values))
|
||||
{
|
||||
// can't find a match
|
||||
$error = $this->getParameterHolder()->get('values_error');
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes this validator.
|
||||
*
|
||||
* @param sfContext The current application context
|
||||
* @param array An associative array of initialization parameters
|
||||
*
|
||||
* @return bool true, if initialization completes successfully, otherwise false
|
||||
*/
|
||||
public function initialize($context, $parameters = null)
|
||||
{
|
||||
// initialize parent
|
||||
parent::initialize($context);
|
||||
|
||||
// set defaults
|
||||
$this->getParameterHolder()->set('insensitive', false);
|
||||
$this->getParameterHolder()->set('max', null);
|
||||
$this->getParameterHolder()->set('max_error', 'Input is too long');
|
||||
$this->getParameterHolder()->set('min', null);
|
||||
$this->getParameterHolder()->set('min_error', 'Input is too short');
|
||||
$this->getParameterHolder()->set('values', null);
|
||||
$this->getParameterHolder()->set('values_error', 'Invalid selection');
|
||||
|
||||
$this->getParameterHolder()->add($parameters);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
62
lib/symfony/validator/sfUrlValidator.class.php
Executable file
62
lib/symfony/validator/sfUrlValidator.class.php
Executable file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the symfony package.
|
||||
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* sfUrlValidator verifies a parameter contains a value that qualifies as a valid URL.
|
||||
*
|
||||
* @package symfony
|
||||
* @subpackage validator
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* @version SVN: $Id: sfUrlValidator.class.php 3345 2007-01-29 10:25:09Z fabien $
|
||||
*/
|
||||
class sfUrlValidator extends sfValidator
|
||||
{
|
||||
/**
|
||||
* Executes this validator.
|
||||
*
|
||||
* @param mixed A file or parameter value/array
|
||||
* @param error An error message reference
|
||||
*
|
||||
* @return bool true, if this validator executes successfully, otherwise false
|
||||
*/
|
||||
public function execute(&$value, &$error)
|
||||
{
|
||||
$re = '/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)/i';
|
||||
|
||||
if (!preg_match($re, $value))
|
||||
{
|
||||
$error = $this->getParameterHolder()->get('url_error');
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes this validator.
|
||||
*
|
||||
* @param sfContext The current application context
|
||||
* @param array An associative array of initialization parameters
|
||||
*
|
||||
* @return bool true, if initialization completes successfully, otherwise false
|
||||
*/
|
||||
public function initialize($context, $parameters = null)
|
||||
{
|
||||
// initialize parent
|
||||
parent::initialize($context);
|
||||
|
||||
// set defaults
|
||||
$this->getParameterHolder()->set('url_error', 'Invalid input');
|
||||
|
||||
$this->getParameterHolder()->add($parameters);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
113
lib/symfony/validator/sfValidator.class.php
Executable file
113
lib/symfony/validator/sfValidator.class.php
Executable file
@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the symfony package.
|
||||
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* (c) 2004-2006 Sean Kerr.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* sfValidator allows you to apply constraints to user entered parameters.
|
||||
*
|
||||
* @package symfony
|
||||
* @subpackage validator
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* @author Sean Kerr <skerr@mojavi.org>
|
||||
* @version SVN: $Id: sfValidator.class.php 3250 2007-01-12 20:09:11Z fabien $
|
||||
*/
|
||||
abstract class sfValidator
|
||||
{
|
||||
protected
|
||||
$parameterHolder = null,
|
||||
$context = null;
|
||||
|
||||
/**
|
||||
* Executes this validator.
|
||||
*
|
||||
* @param mixed A file or parameter value/array
|
||||
* @param string An error message reference
|
||||
*
|
||||
* @return bool true, if this validator executes successfully, otherwise false
|
||||
*/
|
||||
abstract function execute(&$value, &$error);
|
||||
|
||||
/**
|
||||
* Retrieves the current application context.
|
||||
*
|
||||
* @return sfContext The current sfContext instance
|
||||
*/
|
||||
public final function getContext()
|
||||
{
|
||||
return $this->context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes this validator.
|
||||
*
|
||||
* @param sfContext The current application context
|
||||
* @param array An associative array of initialization parameters
|
||||
*
|
||||
* @return bool true, if initialization completes successfully, otherwise false
|
||||
*/
|
||||
public function initialize($context, $parameters = array())
|
||||
{
|
||||
$this->context = $context;
|
||||
|
||||
$this->parameterHolder = new sfParameterHolder();
|
||||
$this->parameterHolder->add($parameters);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the parameters from the validator.
|
||||
*
|
||||
* @return sfParameterHolder List of parameters
|
||||
*/
|
||||
public function getParameterHolder()
|
||||
{
|
||||
return $this->parameterHolder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a parameter from the validator.
|
||||
*
|
||||
* @param string Parameter name
|
||||
* @param mixed A default parameter value
|
||||
* @param string A parameter namespace
|
||||
*
|
||||
* @return mixed A parameter value
|
||||
*/
|
||||
public function getParameter($name, $default = null, $ns = null)
|
||||
{
|
||||
return $this->parameterHolder->get($name, $default, $ns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether or not a parameter exist for the validator.
|
||||
*
|
||||
* @param string A parameter name
|
||||
* @param string A parameter namespace
|
||||
*
|
||||
* @return boolean true, if parameter exists, otherwise false
|
||||
*/
|
||||
public function hasParameter($name, $ns = null)
|
||||
{
|
||||
return $this->parameterHolder->has($name, $ns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a parameter for the validator.
|
||||
*
|
||||
* @param string A parameter name
|
||||
* @param mixed A parameter value
|
||||
* @param string A parameter namespace
|
||||
*/
|
||||
public function setParameter($name, $value, $ns = null)
|
||||
{
|
||||
$this->parameterHolder->set($name, $value, $ns);
|
||||
}
|
||||
}
|
310
lib/symfony/validator/sfValidatorManager.class.php
Executable file
310
lib/symfony/validator/sfValidatorManager.class.php
Executable file
@ -0,0 +1,310 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the symfony package.
|
||||
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* (c) 2004-2006 Sean Kerr.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
/**
|
||||
* sfValidatorManager provides management for request parameters and their
|
||||
* associated validators.
|
||||
*
|
||||
* @package symfony
|
||||
* @subpackage validator
|
||||
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
||||
* @author Sean Kerr <skerr@mojavi.org>
|
||||
* @version SVN: $Id: sfValidatorManager.class.php 4282 2007-06-20 11:32:49Z fabien $
|
||||
*/
|
||||
class sfValidatorManager
|
||||
{
|
||||
protected
|
||||
$groups = array(),
|
||||
$names = array(),
|
||||
$request = null;
|
||||
|
||||
/**
|
||||
* Clears this validator manager so it can be reused.
|
||||
*/
|
||||
public function clear()
|
||||
{
|
||||
$this->groups = null;
|
||||
$this->groups = array();
|
||||
$this->names = null;
|
||||
$this->names = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes all validators and determine the validation status.
|
||||
*
|
||||
* @return bool true, if validation completed successfully, otherwise false
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
if (sfConfig::get('sf_logging_enabled'))
|
||||
{
|
||||
sfContext::getInstance()->getLogger()->info('{sfValidator} validation execution');
|
||||
}
|
||||
|
||||
$retval = true;
|
||||
|
||||
// loop through the names and start our validation
|
||||
// if 1 or more groups exist, we'll have to do a second pass
|
||||
$pass = 1;
|
||||
|
||||
while (true)
|
||||
{
|
||||
foreach ($this->names as $name => &$data)
|
||||
{
|
||||
if (isset($data['_is_parent']))
|
||||
{
|
||||
// this is a parent
|
||||
foreach ($data as $subname => &$subdata)
|
||||
{
|
||||
if ($subname == '_is_parent')
|
||||
{
|
||||
// this isn't an actual index, but more of a flag
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($subdata['validation_status'] == true && !$this->validate($subname, $subdata, $name))
|
||||
{
|
||||
// validation failed
|
||||
$retval = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// single parameter
|
||||
if ($data['validation_status'] == true && !$this->validate($name, $data, null))
|
||||
{
|
||||
// validation failed
|
||||
$retval = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count($this->groups) == 0 || $pass == 2)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// increase our pass indicator
|
||||
++$pass;
|
||||
}
|
||||
|
||||
return $retval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes this validator manager.
|
||||
*
|
||||
* @param sfContext A sfContext instance
|
||||
*/
|
||||
public function initialize($context)
|
||||
{
|
||||
$this->request = $context->getRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a file or parameter.
|
||||
*
|
||||
* @param string A file or parameter name
|
||||
* @param bool The required status
|
||||
* @param string A required error message
|
||||
* @param string A group name
|
||||
* @param string A parent array
|
||||
*/
|
||||
public function registerName($name, $required = true, $message = 'Required', $parent = null, $group = null, $isFile = false)
|
||||
{
|
||||
// create the entry
|
||||
$entry = array();
|
||||
$entry['group'] = null;
|
||||
$entry['is_file'] = $isFile;
|
||||
$entry['required'] = $required;
|
||||
$entry['required_msg'] = $message;
|
||||
$entry['validation_status'] = true;
|
||||
$entry['validators'] = array();
|
||||
|
||||
if ($parent != null)
|
||||
{
|
||||
// this parameter has a parent array
|
||||
if (!isset($this->names[$parent]))
|
||||
{
|
||||
// create the parent array
|
||||
$this->names[$parent] = array('_is_parent' => true);
|
||||
}
|
||||
|
||||
// register this parameter
|
||||
$this->names[$parent][$name] =& $entry;
|
||||
}
|
||||
else
|
||||
{
|
||||
// no parent
|
||||
|
||||
// register this parameter
|
||||
$this->names[$name] =& $entry;
|
||||
}
|
||||
|
||||
if ($group != null)
|
||||
{
|
||||
// set group
|
||||
if (!isset($this->groups[$group]))
|
||||
{
|
||||
// create our group
|
||||
$this->groups[$group] = array('_force' => false);
|
||||
}
|
||||
|
||||
// add this file/parameter name to the group
|
||||
$this->groups[$group][] = $name;
|
||||
|
||||
// add a reference back to the group array to the file/param array
|
||||
$entry['group'] =& $this->groups[$group];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a validator for a file or parameter.
|
||||
*
|
||||
* @param string A file or parameter name
|
||||
* @param Validator A validator implementation instance
|
||||
* @param string A parent array name
|
||||
*/
|
||||
public function registerValidator($name, $validator, $parent = null)
|
||||
{
|
||||
if ($parent != null)
|
||||
{
|
||||
// this parameter has a parent
|
||||
$this->names[$parent][$name]['validators'][] = $validator;
|
||||
}
|
||||
else
|
||||
{
|
||||
// no parent
|
||||
$this->names[$name]['validators'][] = $validator;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates a file or parameter.
|
||||
*
|
||||
* @param string A file or parameter name
|
||||
* @param array Data associated with the file or parameter
|
||||
* @param string A parent name
|
||||
*
|
||||
* @return bool true, if validation completes successfully, otherwise false
|
||||
*/
|
||||
protected function validate(&$name, &$data, $parent)
|
||||
{
|
||||
// get defaults
|
||||
$error = null;
|
||||
$errorName = null;
|
||||
$force = null !== $data['group'] ? $data['group']['_force'] : false;
|
||||
$retval = true;
|
||||
$value = null;
|
||||
|
||||
// get our parameter value
|
||||
if ($parent == null)
|
||||
{
|
||||
// normal file/parameter
|
||||
$errorName = $name;
|
||||
|
||||
if ($data['is_file'])
|
||||
{
|
||||
// file
|
||||
$value = $this->request->getFile($name);
|
||||
}
|
||||
else
|
||||
{
|
||||
// parameter
|
||||
$value = $this->request->getParameterHolder()->get($name);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// we have a parent
|
||||
$errorName = $parent.'{'.$name.'}';
|
||||
|
||||
if ($data['is_file'])
|
||||
{
|
||||
// file
|
||||
$parent = $this->request->getFile($parent.'['.$name.']');
|
||||
|
||||
if ($parent != null)
|
||||
{
|
||||
$value = $parent;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// parameter
|
||||
$parent = $this->request->getParameterHolder()->get($parent);
|
||||
|
||||
if ($parent != null && isset($parent[$name]))
|
||||
{
|
||||
$value = $parent[$name];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// now for the dirty work
|
||||
if (
|
||||
($data['is_file'] && !$value['name'])
|
||||
||
|
||||
(!$data['is_file'] && (is_array($value) ? sfToolkit::isArrayValuesEmpty($value) : ($value === null || strlen($value) == 0)))
|
||||
)
|
||||
{
|
||||
if ($data['required'] || $force)
|
||||
{
|
||||
// it's empty!
|
||||
$error = $data['required_msg'];
|
||||
$retval = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// we don't have to validate it
|
||||
$retval = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// time for the fun
|
||||
$error = null;
|
||||
|
||||
// get group force status
|
||||
if ($data['group'] != null)
|
||||
{
|
||||
// we set this because we do have a value for a parameter in this group
|
||||
$data['group']['_force'] = true;
|
||||
}
|
||||
|
||||
if (count($data['validators']) > 0)
|
||||
{
|
||||
// loop through our validators
|
||||
foreach ($data['validators'] as $validator)
|
||||
{
|
||||
if (!$validator->execute($value, $error))
|
||||
{
|
||||
$retval = false;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$retval)
|
||||
{
|
||||
// set validation status
|
||||
$data['validation_status'] = false;
|
||||
|
||||
// set the request error
|
||||
$this->request->setError($errorName, $error);
|
||||
}
|
||||
|
||||
return $retval;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user