From c4687bdfdb78b30fe8b9db25540b1d1cb25ae530 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Tue, 18 Feb 2020 12:31:13 +0100 Subject: [PATCH] LSformRules : clean code by using LSconfig :: get() helper --- ...rmRule_LSformElement_select_validValue.php | 4 +- .../class/class.LSformRule_alphanumeric.php | 7 +-- .../class/class.LSformRule_callable.php | 24 ++++--- .../class/class.LSformRule_compare.php | 11 ++-- .../includes/class/class.LSformRule_date.php | 7 ++- .../class.LSformRule_differentPassword.php | 13 ++-- .../includes/class/class.LSformRule_email.php | 10 ++- .../class/class.LSformRule_filesize.php | 28 ++++----- .../class/class.LSformRule_imagefile.php | 14 +++-- .../class/class.LSformRule_imagesize.php | 43 ++++++------- .../class/class.LSformRule_inarray.php | 11 ++-- .../class/class.LSformRule_integer.php | 18 +++--- .../class/class.LSformRule_lettersonly.php | 4 +- .../class/class.LSformRule_maxlength.php | 9 +-- .../class/class.LSformRule_mimetype.php | 32 +++++----- .../class/class.LSformRule_minlength.php | 11 ++-- .../class/class.LSformRule_nonzero.php | 6 +- .../class/class.LSformRule_nopunctuation.php | 4 +- .../class/class.LSformRule_numeric.php | 4 +- .../class/class.LSformRule_password.php | 63 +++++++++---------- .../class/class.LSformRule_rangelength.php | 15 ++--- .../includes/class/class.LSformRule_regex.php | 21 +++---- .../class/class.LSformRule_required.php | 4 +- .../class/class.LSformRule_ssh_pub_key.php | 4 +- .../class.LSformRule_telephonenumber.php | 4 +- 25 files changed, 184 insertions(+), 187 deletions(-) diff --git a/public_html/includes/class/class.LSformRule_LSformElement_select_validValue.php b/public_html/includes/class/class.LSformRule_LSformElement_select_validValue.php index ec1cb119..ea05f89a 100644 --- a/public_html/includes/class/class.LSformRule_LSformElement_select_validValue.php +++ b/public_html/includes/class/class.LSformRule_LSformElement_select_validValue.php @@ -26,7 +26,7 @@ * @author Benjamin Renard */ class LSformRule_LSformElement_select_validValue extends LSformRule { - + /** * Validate value * @@ -35,7 +35,7 @@ class LSformRule_LSformElement_select_validValue extends LSformRule { * @param object $formElement The related formElement object * * @return boolean true if the value is valide, false if not - */ + */ public static function validate($value,$option,$formElement) { $ret = $formElement -> isValidValue($value); if ($ret===False) return False; diff --git a/public_html/includes/class/class.LSformRule_alphanumeric.php b/public_html/includes/class/class.LSformRule_alphanumeric.php index 72a8147a..bbc958e8 100644 --- a/public_html/includes/class/class.LSformRule_alphanumeric.php +++ b/public_html/includes/class/class.LSformRule_alphanumeric.php @@ -26,7 +26,7 @@ * @author Benjamin Renard */ class LSformRule_alphanumeric extends LSformRule { - + /** * Vérification de la valeur. * @@ -39,8 +39,7 @@ class LSformRule_alphanumeric extends LSformRule { */ public static function validate ($value,$options=array(),$formElement) { - - if (isset($options['params']['withAccents']) && $options['params']['withAccents'] == true){ + if (LSconfig :: get('params.withAccents', false, 'bool', $options)) { $regex = '/(*UTF8)^[0-9\p{L}]+$/'; } else { @@ -50,6 +49,6 @@ class LSformRule_alphanumeric extends LSformRule { return LSformRule_regex :: validate($value,$regex,$formElement); } - + } diff --git a/public_html/includes/class/class.LSformRule_callable.php b/public_html/includes/class/class.LSformRule_callable.php index 041bde0b..2c55010f 100644 --- a/public_html/includes/class/class.LSformRule_callable.php +++ b/public_html/includes/class/class.LSformRule_callable.php @@ -26,7 +26,7 @@ * @author Benjamin Renard */ class LSformRule_callable extends LSformRule { - + /** * Check the value using the callable object * @@ -40,15 +40,21 @@ class LSformRule_callable extends LSformRule { * @param object $formElement The LSformElement object * * @return boolean true if the value is valid, false otherwise - */ + */ public static function validate($value,$options,$formElement) { - if (is_callable($options['params']['callable'])) { - return call_user_func_array($options['params']['callable'],array($value,$options['params'],&$formElement)); - } - else { - LSerror :: addErrorCode('LSformRule_callable_01'); - return False; - } + $callable = LSconfig :: get('params.callable', null, null, $options); + if (is_callable($callable)) + return call_user_func_array( + $callable, + array( + $value, + LSconfig :: get('params', array(), null, $options), + &$formElement + ) + ); + + LSerror :: addErrorCode('LSformRule_callable_01'); + return False; } } diff --git a/public_html/includes/class/class.LSformRule_compare.php b/public_html/includes/class/class.LSformRule_compare.php index 5942743f..9fd5ec87 100644 --- a/public_html/includes/class/class.LSformRule_compare.php +++ b/public_html/includes/class/class.LSformRule_compare.php @@ -26,7 +26,7 @@ * @author Benjamin Renard */ class LSformRule_compare extends LSformRule { - + /** * Retourne l'operateur de comparaison. * @@ -61,18 +61,19 @@ class LSformRule_compare extends LSformRule { * Vérification des valeurs. * * @param string $values Valeurs à vérifier - * @param array $options Options de validation : + * @param array $options Options de validation : * - Operateur : $options['params']['operator'] * @param object $formElement L'objet formElement attaché * * @return boolean true si la valeur est valide, false sinon */ public static function validate ($values,$options=array(),$formElement) { - if (!isset($options['params']['operator'])) { + $operator = LSconfig :: get('params.operator', null, 'string', $options); + if (!$operator) { LSerror :: addErrorCode('LSformRule_01',array('type' => 'compare', 'param' => 'operator')); return; } - $operator = self :: _findOperator($options['params']['operator']); + $operator = self :: _findOperator($operator); if ('==' != $operator && '!=' != $operator) { $compareFn = create_function('$a, $b', 'return floatval($a) ' . $operator . ' floatval($b);'); } @@ -81,6 +82,6 @@ class LSformRule_compare extends LSformRule { } return $compareFn($values[0], $values[1]); } - + } diff --git a/public_html/includes/class/class.LSformRule_date.php b/public_html/includes/class/class.LSformRule_date.php index 8c1693ca..67a173c6 100644 --- a/public_html/includes/class/class.LSformRule_date.php +++ b/public_html/includes/class/class.LSformRule_date.php @@ -26,7 +26,7 @@ * @author Benjamin Renard */ class LSformRule_date extends LSformRule { - + /** * Validation de données * @@ -38,11 +38,12 @@ class LSformRule_date extends LSformRule { * @return boolean True si les données sont valide, False sinon. */ public static function validate($value,$options=NULL,$formElement) { - if (!isset($options['params']['format'])) { + $format = LSconfig :: get('params.format', null, 'string', $options); + if (is_null($format)) { LSerror :: addErrorCode('LSformRule_date_01'); return; } - $date = strptime($value,$options['params']['format']); + $date = strptime($value, $format); if(is_array($date)) { $res = mktime($date['tm_hour'],$date['tm_min'],$date['tm_sec'],$date['tm_mon']+1,$date['tm_mday'],$date['tm_year']+1900); if ((is_int($res)) && ($res != -1) && ($res !== False)) { diff --git a/public_html/includes/class/class.LSformRule_differentPassword.php b/public_html/includes/class/class.LSformRule_differentPassword.php index bfc3b120..da221234 100644 --- a/public_html/includes/class/class.LSformRule_differentPassword.php +++ b/public_html/includes/class/class.LSformRule_differentPassword.php @@ -31,17 +31,18 @@ class LSformRule_differentPassword extends LSformRule { * Check the value * * @param string $values Value to check - * @param array $options Validation options : - * - Other attribute : $options['params']['otherAttributes'] + * @param array $options Validation options : + * - Other attribute : $options['params']['otherPasswordAttributes'] * @param object $formElement The linked LSformElement object * * @return boolean true si la valeur est valide, false sinon */ public static function validate($value, $options, $formElement) { - if (is_array($options) && isset($options['params']['otherPasswordAttributes'])) { + $otherPasswordAttributes = LSconfig :: get('params.otherPasswordAttributes', null, null, $options); + if (!is_null($otherPasswordAttributes)) { // Make sure otherPasswordAttributes is an array - if (!is_array($options['params']['otherPasswordAttributes'])) - $options['params']['otherPasswordAttributes'] = array($options['params']['otherPasswordAttributes']); + if (!is_array($otherPasswordAttributes)) + $otherPasswordAttributes = array($otherPasswordAttributes); // Load LSattr_ldap_password if (!LSsession :: loadLSclass("LSattr_ldap_password")) { @@ -50,7 +51,7 @@ class LSformRule_differentPassword extends LSformRule { } // Iter on otherPasswordAttributes to check password does not match - foreach($options['params']['otherPasswordAttributes'] as $attr) { + foreach($otherPasswordAttributes as $attr) { // Check attribute exist if (!isset($formElement -> attr_html -> attribute -> ldapObject -> attrs[$attr])) { LSerror :: addErrorCode('LSformRule_differentPassword_03', $attr); diff --git a/public_html/includes/class/class.LSformRule_email.php b/public_html/includes/class/class.LSformRule_email.php index 9ca53311..a7316a9f 100644 --- a/public_html/includes/class/class.LSformRule_email.php +++ b/public_html/includes/class/class.LSformRule_email.php @@ -26,19 +26,23 @@ * @author Benjamin Renard */ class LSformRule_email extends LSformRule { - + /** * Vérification de la valeur. * * @param string $value Valeur à vérifier - * @param array $options Options de validation : + * @param array $options Options de validation : * - Check domain : $options['params']['checkDomain'] * @param object $formElement L'objet formElement attaché * * @return boolean true si la valeur est valide, false sinon */ public static function validate($value,$options=array(),$formElement) { - return checkEmail($value,$options['params']['domain'],$options['params']['checkDomain']); + return checkEmail( + $value, + LSconfig :: get('params.domain', NULL, 'string', $options), + LSconfig :: get('params.checkDomain', true, 'bool', $options) + ); } } diff --git a/public_html/includes/class/class.LSformRule_filesize.php b/public_html/includes/class/class.LSformRule_filesize.php index 8b91a5b7..fbec3534 100644 --- a/public_html/includes/class/class.LSformRule_filesize.php +++ b/public_html/includes/class/class.LSformRule_filesize.php @@ -31,7 +31,7 @@ class LSformRule_filesize extends LSformRule { * Vérification de la valeur. * * @param string $values Valeur à vérifier - * @param array $options Options de validation : + * @param array $options Options de validation : * - Taille max (en octet) : $options['params']['maxSize'] * - Taille min (en octet) : $options['params']['minSize'] * @param object $formElement L'objet formElement attaché @@ -40,23 +40,19 @@ class LSformRule_filesize extends LSformRule { */ public static function validate ($value,$options,$formElement) { $file = LSsession :: getTmpFile($value); - - $size = filesize($file); - - if (is_int($options['params']['maxSize'])) { - if ($size > $options['params']['maxSize']) { - return; - } - } - if (is_int($options['params']['minSize'])) { - if ($size < $options['params']['minSize']) { - return; - } - } - + $size = filesize($file); + + $maxSize = LSconfig :: get('params.maxSize', null, 'int', $options); + if (is_int($maxSize) && $size > $maxSize) + return; + + $minSize = LSconfig :: get('params.minSize', null, 'int', $options); + if (is_int($minSize) && $size < $minSize) + return; + return true; } - + } diff --git a/public_html/includes/class/class.LSformRule_imagefile.php b/public_html/includes/class/class.LSformRule_imagefile.php index 7f8b6bba..e1a3beea 100644 --- a/public_html/includes/class/class.LSformRule_imagefile.php +++ b/public_html/includes/class/class.LSformRule_imagefile.php @@ -33,7 +33,7 @@ class LSformRule_imagefile extends LSformRule { * Vérification de la valeur. * * @param string $values Valeur à vérifier - * @param array $options Options de validation : + * @param array $options Options de validation : * - Type MIME : $options['params']['mimeType'] * - Type MIME (regex) : $options['params']['mimeTypeRegEx'] * @param object $formElement L'objet formElement attaché @@ -42,19 +42,21 @@ class LSformRule_imagefile extends LSformRule { */ public static function validate ($value,$options,$formElement) { $file = LSsession :: getTmpFile($value); - + $mimetype = mime_content_type($file); - - if ( (!isset($options['params']['mimeType'])) && (!isset($options['params']['mimeTypeRegEx'])) ) { + + $mimeType = LSconfig :: get('params.mimeType', null, null, $options); + $mimeTypeRegEx = LSconfig :: get('params.mimeTypeRegEx', null, null, $options); + if ( is_null($mimeType) && is_null($mimeTypeRegEx)) { $options = array( 'params' => array( 'mimeTypeRegEx' => '/image\/.*/' ) ); } - + return LSformRule_mimetype :: validate($value,$options,$formElement); } - + } diff --git a/public_html/includes/class/class.LSformRule_imagesize.php b/public_html/includes/class/class.LSformRule_imagesize.php index 9d367fea..56c2a46c 100644 --- a/public_html/includes/class/class.LSformRule_imagesize.php +++ b/public_html/includes/class/class.LSformRule_imagesize.php @@ -31,7 +31,7 @@ class LSformRule_imagesize extends LSformRule { * Vérification de la valeur. * * @param string $values Valeur à vérifier - * @param array $options Options de validation : + * @param array $options Options de validation : * - Largeur max : $options['params']['maxWidth'] * - Largeur min : $options['params']['minWidth'] * - Hauteur max : $options['params']['maxHeight'] @@ -43,30 +43,25 @@ class LSformRule_imagesize extends LSformRule { public static function validate ($value,$options,$formElement) { $file = LSsession :: getTmpFile($value); list($width, $height, $type, $attr) = getimagesize($file); - - if (is_int($options['params']['maxWidth'])) { - if ($width > $options['params']['maxWidth']) { - return; - } - } - if (is_int($options['params']['minWidth'])) { - if ($width < $options['params']['minWidth']) { - return; - } - } - if (is_int($options['params']['maxHeight'])) { - if ($height > $options['params']['maxHeight']) { - return; - } - } - if (is_int($options['params']['minHeight'])) { - if ($height < $options['params']['minHeight']) { - return; - } - } - + + $maxWidth = LSconfig :: get('params.maxWidth', null, 'int', $options); + if (is_int($maxWidth) && $width > $maxWidth) + return; + + $minWidth = LSconfig :: get('params.minWidth', null, 'int', $options); + if (is_int($minWidth) && $width < $minWidth) + return; + + $maxHeight = LSconfig :: get('params.maxHeight', null, 'int', $options); + if (is_int($maxHeight) && $height > $maxHeight) + return; + + $minHeight = LSconfig :: get('params.minHeight', null, 'int', $options); + if (is_int($minHeight) && $height < $minHeight) + return; + return true; } - + } diff --git a/public_html/includes/class/class.LSformRule_inarray.php b/public_html/includes/class/class.LSformRule_inarray.php index 53928ea3..b2cb586a 100644 --- a/public_html/includes/class/class.LSformRule_inarray.php +++ b/public_html/includes/class/class.LSformRule_inarray.php @@ -26,23 +26,24 @@ * @author Benjamin Renard */ class LSformRule_inarray extends LSformRule { - + /** * Vérification de la valeur. * * @param string $values Valeur à vérifier - * @param array $options Options de validation : + * @param array $options Options de validation : * - Regex : $option['params']['possible_values'] ou $option * @param object $formElement L'objet formElement attaché * * @return boolean true si la valeur est valide, false sinon - */ + */ public static function validate($value,$option,$formElement) { - if (!isset($option['params']['possible_values']) || !is_array($option['params']['possible_values'])) { + $possible_values = LSconfig :: get('params.possible_values', null, null, $options); + if (!is_array($possible_values)) { LSerror :: addErrorCode('LSformRule_inarray_01'); return; } - if (!in_array($value,$option['params']['possible_values'])) + if (!in_array($value, $possible_values)) return false; return true; } diff --git a/public_html/includes/class/class.LSformRule_integer.php b/public_html/includes/class/class.LSformRule_integer.php index 94c48735..145b278e 100644 --- a/public_html/includes/class/class.LSformRule_integer.php +++ b/public_html/includes/class/class.LSformRule_integer.php @@ -26,7 +26,7 @@ * @author Benjamin Renard */ class LSformRule_integer extends LSformRule{ - + /** * Verification value. * @@ -41,16 +41,18 @@ class LSformRule_integer extends LSformRule{ * @return boolean true if the value is valided, false otherwise */ public static function validate ($value,$options=array(),$formElement) { - if($options['params']['max'] && $value > $options['params']['max']) { + $max = LSconfig :: get('params.max', null, 'int', $options); + if(is_int($max) && $value > $max) return; - } - if($options['params']['min'] && $value < $options['params']['min']) { + + $min = LSconfig :: get('params.min', null, 'int', $options); + if(is_int($min) && $value < $min) return; - } - if($options['params']['negative']) { + + if(LSconfig :: get('params.negative', false, 'bool', $options)) { $regex = '/^-[0-9]*$/'; } - elseif($options['params']['positive']) { + elseif(LSconfig :: get('params.positive', false, 'bool', $options)) { $regex = '/^[0-9]*$/'; } else { @@ -59,6 +61,6 @@ class LSformRule_integer extends LSformRule{ LSsession :: loadLSclass('LSformRule_regex'); return LSformRule_regex :: validate($value,$regex,$formElement); } - + } diff --git a/public_html/includes/class/class.LSformRule_lettersonly.php b/public_html/includes/class/class.LSformRule_lettersonly.php index 544a0625..33c2e4ca 100644 --- a/public_html/includes/class/class.LSformRule_lettersonly.php +++ b/public_html/includes/class/class.LSformRule_lettersonly.php @@ -26,7 +26,7 @@ * @author Benjamin Renard */ class LSformRule_lettersonly extends LSformRule { - + /** * Vérification de la valeur. * @@ -41,6 +41,6 @@ class LSformRule_lettersonly extends LSformRule { LSsession :: loadLSclass('LSformRule_regex'); return LSformRule_regex :: validate($value,$regex,$formElement); } - + } diff --git a/public_html/includes/class/class.LSformRule_maxlength.php b/public_html/includes/class/class.LSformRule_maxlength.php index ff18d026..f3d91ef5 100644 --- a/public_html/includes/class/class.LSformRule_maxlength.php +++ b/public_html/includes/class/class.LSformRule_maxlength.php @@ -31,19 +31,20 @@ class LSformRule_maxlength extends LSformRule { * Vérification de la valeur. * * @param string $values Valeur à vérifier - * @param array $options Options de validation : + * @param array $options Options de validation : * - Longueur max : $options['params']['limit'] * @param object $formElement L'objet formElement attaché * * @return boolean true si la valeur est valide, false sinon */ public static function validate ($value,$options,$formElement) { - if(!isset($options['params']['limit'])) { + $limit = LSconfig :: get('params.limit', null, 'int', $options); + if(is_null($limit)) { LSerror :: addErrorCode('LSformRule_01',array('type' => 'maxlength', 'param' => 'limit')); return; } - return (strlen($value)<=$options['params']['limit']); + return (strlen($value) <= $limit); } - + } diff --git a/public_html/includes/class/class.LSformRule_mimetype.php b/public_html/includes/class/class.LSformRule_mimetype.php index 70c7b57e..e391c210 100644 --- a/public_html/includes/class/class.LSformRule_mimetype.php +++ b/public_html/includes/class/class.LSformRule_mimetype.php @@ -31,7 +31,7 @@ class LSformRule_mimetype extends LSformRule { * Vérification de la valeur. * * @param string $values Valeur à vérifier - * @param array $options Options de validation : + * @param array $options Options de validation : * - Type MIME : $options['params']['mimeType'] * - Type MIME (regex) : $options['params']['mimeTypeRegEx'] * @param object $formElement L'objet formElement attaché @@ -40,30 +40,26 @@ class LSformRule_mimetype extends LSformRule { */ public static function validate ($value,$options,$formElement) { $file = LSsession :: getTmpFile($value); - - $mimetype = mime_content_type($file); - - if (isset($options['params']['mimeType'])) { - if (is_array($options['params']['mimeType'])) { - if (!in_array($mimetype,$options['params']['mimeType'])) { + $real_mimetype = mime_content_type($file); + + $mimetype = LSconfig :: get('params.mimeType', null, null, $options); + if (!is_null($mimetype)) { + if (is_array($mimetype)) { + if (!in_array($real_mimetype, $mimetype)) return; - } } else { - if ($mimetype != $options['params']['mimeType']) { + if ($real_mimetype != $mimetype) return; - } } } - - if (isset($options['params']['mimeTypeRegEx'])) { - if (!preg_match($options['params']['mimeTypeRegEx'], $mimetype)) { - return false; - } - } - + + $mimeTypeRegEx = LSconfig :: get('params.mimeTypeRegEx', null, 'string', $options); + if (is_string($mimeTypeRegEx) && !preg_match($mimeTypeRegEx, $real_mimetype)) + return false; + return true; } - + } diff --git a/public_html/includes/class/class.LSformRule_minlength.php b/public_html/includes/class/class.LSformRule_minlength.php index 7da02f2c..0028cf5a 100644 --- a/public_html/includes/class/class.LSformRule_minlength.php +++ b/public_html/includes/class/class.LSformRule_minlength.php @@ -31,19 +31,20 @@ class LSformRule_minlength extends LSformRule { * Vérification de la valeur. * * @param string $values Valeur à vérifier - * @param array $options Options de validation : + * @param array $options Options de validation : * - Longueur min : $options['params']['limit'] * @param object $formElement L'objet formElement attaché * * @return boolean true si la valeur est valide, false sinon - */ + */ public static function validate ($value,$options,$formElement) { - if(!isset($options['params']['limit'])) { + $limit = LSconfig :: get('params.limit', null, 'int', $options); + if(is_null($limit)) { LSerror :: addErrorCode('LSformRule_01',array('type' => 'minlength', 'param' => 'limit')); return; } - return (strlen($value)>=$options['params']['limit']); + return (strlen($value) >= $limit); } - + } diff --git a/public_html/includes/class/class.LSformRule_nonzero.php b/public_html/includes/class/class.LSformRule_nonzero.php index c6f63c33..65c1364a 100644 --- a/public_html/includes/class/class.LSformRule_nonzero.php +++ b/public_html/includes/class/class.LSformRule_nonzero.php @@ -26,7 +26,7 @@ * @author Benjamin Renard */ class LSformRule_nonzero extends LSformRule { - + /** * Vérification de la valeur. * @@ -35,12 +35,12 @@ class LSformRule_nonzero extends LSformRule { * @param object $formElement L'objet formElement attaché * * @return boolean true si la valeur est valide, false sinon - */ + */ public static function validate ($value,$options,$formElement) { $regex = '/^-?[1-9][0-9]*/'; LSsession :: loadLSclass('LSformRule_regex'); return LSformRule_regex :: validate($value,$regex,$formElement); } - + } diff --git a/public_html/includes/class/class.LSformRule_nopunctuation.php b/public_html/includes/class/class.LSformRule_nopunctuation.php index 729ee1a6..1db24188 100644 --- a/public_html/includes/class/class.LSformRule_nopunctuation.php +++ b/public_html/includes/class/class.LSformRule_nopunctuation.php @@ -26,7 +26,7 @@ * @author Benjamin Renard */ class LSformRule_nopunctuation extends LSformRule { - + /** * Vérification de la valeur. * @@ -41,6 +41,6 @@ class LSformRule_nopunctuation extends LSformRule { LSsession :: loadLSclass('LSformRule_regex'); return LSformRule_regex :: validate($value,$regex,$formElement); } - + } diff --git a/public_html/includes/class/class.LSformRule_numeric.php b/public_html/includes/class/class.LSformRule_numeric.php index fe5310da..a0d39344 100644 --- a/public_html/includes/class/class.LSformRule_numeric.php +++ b/public_html/includes/class/class.LSformRule_numeric.php @@ -26,7 +26,7 @@ * @author Benjamin Renard */ class LSformRule_numeric extends LSformRule{ - + /** * Vérification de la valeur. * @@ -41,6 +41,6 @@ class LSformRule_numeric extends LSformRule{ LSsession :: loadLSclass('LSformRule_regex'); return LSformRule_regex :: validate($value,$regex,$formElement); } - + } diff --git a/public_html/includes/class/class.LSformRule_password.php b/public_html/includes/class/class.LSformRule_password.php index 44ffaa77..a1489d68 100644 --- a/public_html/includes/class/class.LSformRule_password.php +++ b/public_html/includes/class/class.LSformRule_password.php @@ -26,7 +26,7 @@ * @author Benjamin Renard */ class LSformRule_password extends LSformRule { - + /** * Vérification de la valeur. * @@ -42,52 +42,45 @@ class LSformRule_password extends LSformRule { * @param object $formElement L'objet formElement attaché * * @return boolean true si la valeur est valide, false sinon - */ + */ public static function validate ($value,$options=array(),$formElement) { - if(isset($options['params']['maxLength'])) { - if (strlen($value)>$options['params']['maxLength']) - return; - } - - if(isset($options['params']['minLength'])) { - if (strlen($value)<$options['params']['minLength']) - return; - } - - if(isset($options['params']['regex'])) { - if (!is_array($options['params']['regex'])) { - $options['params']['regex']=array($options['params']['regex']); - } - if (isset($options['params']['minValidRegex'])) { - $options['params']['minValidRegex']=(int)$options['params']['minValidRegex']; - if ($options['params']['minValidRegex']==0 || $options['params']['minValidRegex']>count($options['params']['regex'])) { - $options['params']['minValidRegex']=count($options['params']['regex']); - } - } - else { - $options['params']['minValidRegex']=count($options['params']['regex']); - } + $maxLength = LSconfig :: get('params.maxLength', null, 'int', $options); + if(is_int($maxLength) && strlen($value) > $maxLength) + return; + + $minLength = LSconfig :: get('params.minLength', null, 'int', $options); + if(is_int($minLength) && strlen($value) < $minLength) + return; + + $regex = LSconfig :: get('params.regex', null, null, $options); + if(!is_null($regex)) { + if (!is_array($regex)) + $regex = array($regex); + + $minValidRegex = LSconfig :: get('params.minValidRegex', count($regex), 'int', $options); + if ($minValidRegex == 0 || $minValidRegex > count($regex)) + $minValidRegex = count($regex); + $valid=0; - foreach($options['params']['regex'] as $regex) { - if ($regex[0]!='/') { + foreach($regex as $r) { + if ($r[0] != '/') { LSerror :: addErrorCode('LSformRule_password_01'); continue; } - if (preg_match($regex,$value)) + if (preg_match($r, $value)) $valid++; } - if ($valid<$options['params']['minValidRegex']) + if ($valid < $minValidRegex) return; } - if(isset($options['params']['prohibitedValues']) && is_array($options['params']['prohibitedValues'])) { - if (in_array($value,$options['params']['prohibitedValues'])) - return; - } - + $prohibitedValues = LSconfig :: get('params.prohibitedValues', null, null, $options); + if(is_array($prohibitedValues) && in_array($value, $prohibitedValues)) + return; + return true; } - + } diff --git a/public_html/includes/class/class.LSformRule_rangelength.php b/public_html/includes/class/class.LSformRule_rangelength.php index 6af03383..3e35817f 100644 --- a/public_html/includes/class/class.LSformRule_rangelength.php +++ b/public_html/includes/class/class.LSformRule_rangelength.php @@ -26,26 +26,27 @@ * @author Benjamin Renard */ class LSformRule_rangelength extends LSformRule { - + /** * Vérification de la valeur. * * @param string $values Valeur à vérifier - * @param array $options Options de validation : + * @param array $options Options de validation : * - Longueur min : $options['params']['limits'][0] * - Longueur max : $options['params']['limits'][1] * @param object $formElement L'objet formElement attaché * * @return boolean true si la valeur est valide, false sinon - */ + */ public static function validate ($value,$options=array(),$formElement) { - if(!isset($options['params']['limits'])) { + $limits = LSconfig :: get('params.limits', null, null, $options); + if(!is_array($limits) || count($limits) != 2) { LSerror :: addErrorCode('LSformRule_01',array('type' => 'rangelength', 'param' => 'limits')); return; } - $len=strlen($value); - return ($len >= $options['params']['limits'][0] && $len <= $options['params']['limits'][1]); + $len = strlen($value); + return ($len >= $limits[0] && $len <= $limits[1]); } - + } diff --git a/public_html/includes/class/class.LSformRule_regex.php b/public_html/includes/class/class.LSformRule_regex.php index 3f4d54d5..bc64ab7c 100644 --- a/public_html/includes/class/class.LSformRule_regex.php +++ b/public_html/includes/class/class.LSformRule_regex.php @@ -26,34 +26,31 @@ * @author Benjamin Renard */ class LSformRule_regex extends LSformRule { - + /** * Vérification de la valeur. * * @param string $values Valeur à vérifier - * @param array $options Options de validation : + * @param array $options Options de validation : * - Regex : $options['params']['regex'] ou $options * @param object $formElement L'objet formElement attaché * * @return boolean true si la valeur est valide, false sinon - */ - public static function validate($value,$options,$formElement) { + */ + public static function validate($value, $options, $formElement) { if (is_array($options)) { - if (isset($options['params']['regex'])) { - $regex=$options['params']['regex']; - } - else { + $regex = LSconfig :: get('params.regex', null, 'string', $options); + if (!is_string($regex)) { LSerror :: addErrorCode('LSformRule_regex_01'); return; } } else { - $regex=$options; + $regex = $options; } - if (!preg_match($regex, $value)) { + if (!preg_match($regex, $value)) return false; - } - return true; + return true; } } diff --git a/public_html/includes/class/class.LSformRule_required.php b/public_html/includes/class/class.LSformRule_required.php index 4a48d92d..12a47d78 100644 --- a/public_html/includes/class/class.LSformRule_required.php +++ b/public_html/includes/class/class.LSformRule_required.php @@ -35,10 +35,10 @@ class LSformRule_required extends LSformRule { * @param object $formElement L'objet formElement attaché * * @return boolean true si la valeur est valide, false sinon - */ + */ public static function validate ($value,$options=NULL,$formElement) { return ((string)$value != ''); } - + } diff --git a/public_html/includes/class/class.LSformRule_ssh_pub_key.php b/public_html/includes/class/class.LSformRule_ssh_pub_key.php index ca0bdeb7..50eb1dc9 100644 --- a/public_html/includes/class/class.LSformRule_ssh_pub_key.php +++ b/public_html/includes/class/class.LSformRule_ssh_pub_key.php @@ -26,7 +26,7 @@ * @author Benjamin Renard */ class LSformRule_ssh_pub_key extends LSformRule { - + /** * Validate SSH public key value * @@ -35,7 +35,7 @@ class LSformRule_ssh_pub_key extends LSformRule { * @param object $formElement The related formElement object * * @return boolean true if the value is valide, false if not - */ + */ public static function validate($value,$options,$formElement) { if (preg_match('/^(ssh-[a-z0-9]+) +([^ ]+) +(.*)$/', $value, $m)) { $data=@base64_decode($m[2]); diff --git a/public_html/includes/class/class.LSformRule_telephonenumber.php b/public_html/includes/class/class.LSformRule_telephonenumber.php index 8fe433f8..c610a9dc 100644 --- a/public_html/includes/class/class.LSformRule_telephonenumber.php +++ b/public_html/includes/class/class.LSformRule_telephonenumber.php @@ -26,12 +26,12 @@ * @author Benjamin Renard */ class LSformRule_telephonenumber extends LSformRule { - + /** * Vérification de la valeur. * * @param string $value Valeur à vérifier - * @param array $options Options de validation : + * @param array $options Options de validation : * @param object $formElement L'objet formElement attaché * * @return boolean true si la valeur est valide, false sinon