*/ class LSformRule_password extends LSformRule { // CLI parameters autocompleters protected static $cli_params_autocompleters = array( 'minlength' => array('LScli', 'autocomplete_int'), 'maxlength' => array('LScli', 'autocomplete_int'), 'prohibitedValues' => null, 'regex' => null, 'minValidRegex' => array('LScli', 'autocomplete_int'), ); /** * Validate form element value * * @param mixed $value The value to validate * @param array $options Validation options * - 'minLength' : la maximun length * - 'maxLength' : la minimum length * - 'prohibitedValues' : array of prohibited values * - 'regex' : one or multiple regex to match * - 'minValidRegex' : minimum number of regex that must matched * @param LSformElement &$formElement The related LSformElement object * * @return boolean True if value is valid, False otherwise */ public static function validate($value, $options, &$formElement) { $errors = array(); $maxLength = LSconfig :: get('params.maxLength', null, 'int', $options); if(!is_null($maxLength) && $maxLength != 0 && strlen($value) > $maxLength) { $errors[] = getFData(_("Password is too long (maximum: %{maxLength})."), $maxLength); } $minLength = LSconfig :: get('params.minLength', null, 'int', $options); if(!is_null($minLength) && $minLength != 0 && strlen($value) < $minLength) { $errors[] = getFData(_("Password is too short (minimum: %{minLength})."), $minLength); } $regex = ensureIsArray(LSconfig :: get('params.regex', null, null, $options)); if($regex) { $minValidRegex = LSconfig :: get('params.minValidRegex', count($regex), 'int', $options); if ($minValidRegex == 0 || $minValidRegex > count($regex)) $minValidRegex = count($regex); self :: log_debug("password must match with $minValidRegex regex on ".count($regex)); $valid = 0; foreach($regex as $r) { if ($r[0] != '/') { LSerror :: addErrorCode('LSformRule_password_01'); continue; } if (preg_match($r, $value)) { self :: log_debug("password match with regex '$r'"); $valid++; } else self :: log_debug("password does not match with regex '$r'"); } if ($valid < $minValidRegex) { $errors[] = getFData( _("Password match with only %{valid} rule(s) (at least %{minValidRegex} are required)."), array( 'valid' => $valid, 'minValidRegex' => $minValidRegex ) ); } } $prohibitedValues = ensureIsArray(LSconfig :: get('params.prohibitedValues', null, null, $options)); if(in_array($value, $prohibitedValues)) { $errors[] = _("This password is prohibited."); } if ($errors) throw new LSformRuleException($errors); return true; } } /* * Error Codes */ LSerror :: defineError('LSformRule_password_01', ___("LSformRule_password : Invalid regex configured : %{regex}. You must use PCRE (begining by '/' caracter).") );