[ticket/12251] Add string validation for a-zA-Z0-9 and underscore

PHPBB3-12251
This commit is contained in:
Joas Schilling 2014-03-06 12:28:36 +01:00
parent 50165fe920
commit 600e5f6c11
3 changed files with 18 additions and 14 deletions

View file

@ -39,6 +39,7 @@ $lang = array_merge($lang, array(
'ADDED_PROFILE_FIELD' => 'Successfully added custom profile field.', 'ADDED_PROFILE_FIELD' => 'Successfully added custom profile field.',
'ALPHA_ONLY' => 'Alphanumeric only', 'ALPHA_ONLY' => 'Alphanumeric only',
'ALPHA_SPACERS' => 'Alphanumeric and spacers', 'ALPHA_SPACERS' => 'Alphanumeric and spacers',
'ALPHA_UNDERSCORE' => 'Alphanumeric and underscores',
'ALWAYS_TODAY' => 'Always the current date', 'ALWAYS_TODAY' => 'Always the current date',
'BOOL_ENTRIES_EXPLAIN' => 'Enter your options now', 'BOOL_ENTRIES_EXPLAIN' => 'Enter your options now',

View file

@ -205,9 +205,11 @@ $lang = array_merge($lang, array(
), ),
'FIELD_TOO_SMALL' => 'The value of “%2$s” is too small, a minimum value of %1$d is required.', 'FIELD_TOO_SMALL' => 'The value of “%2$s” is too small, a minimum value of %1$d is required.',
'FIELD_TOO_LARGE' => 'The value of “%2$s” is too large, a maximum value of %1$d is allowed.', 'FIELD_TOO_LARGE' => 'The value of “%2$s” is too large, a maximum value of %1$d is allowed.',
'FIELD_INVALID_CHARS_INVALID' => 'The field “%s” has invalid characters.',
'FIELD_INVALID_CHARS_NUMBERS_ONLY' => 'The field “%s” has invalid characters, only numbers are allowed.', 'FIELD_INVALID_CHARS_NUMBERS_ONLY' => 'The field “%s” has invalid characters, only numbers are allowed.',
'FIELD_INVALID_CHARS_ALPHA_ONLY' => 'The field “%s” has invalid characters, only alphanumeric characters are allowed.', 'FIELD_INVALID_CHARS_ALPHA_ONLY' => 'The field “%s” has invalid characters, only alphanumeric characters are allowed.',
'FIELD_INVALID_CHARS_SPACERS_ONLY' => 'The field “%s” has invalid characters, only alphanumeric, space or -+_[] characters are allowed.', 'FIELD_INVALID_CHARS_ALPHA_SPACERS' => 'The field “%s” has invalid characters, only alphanumeric, space or -+_[] characters are allowed.',
'FIELD_INVALID_CHARS_ALPHA_UNDERSCORE' => 'The field “%s” has invalid characters, only alphanumeric or _ characters are allowed.',
'FIELD_INVALID_DATE' => 'The field “%s” has an invalid date.', 'FIELD_INVALID_DATE' => 'The field “%s” has an invalid date.',
'FIELD_INVALID_URL' => 'The field “%s” has an invalid url.', 'FIELD_INVALID_URL' => 'The field “%s” has an invalid url.',
'FIELD_INVALID_VALUE' => 'The field “%s” has an invalid value.', 'FIELD_INVALID_VALUE' => 'The field “%s” has an invalid value.',

View file

@ -11,15 +11,21 @@ namespace phpbb\profilefields\type;
abstract class type_string_common extends type_base abstract class type_string_common extends type_base
{ {
protected $validation_options = array(
'CHARS_ANY' => '.*',
'NUMBERS_ONLY' => '[0-9]+',
'ALPHA_ONLY' => '[\w]+',
'ALPHA_UNDERSCORE' => '[\w_]+',
'ALPHA_SPACERS' => '[\w_\+\. \-\[\]]+',
);
/** /**
* Return possible validation options * Return possible validation options
*/ */
function validate_options($field_data) public function validate_options($field_data)
{ {
$validate_ary = array('CHARS_ANY' => '.*', 'NUMBERS_ONLY' => '[0-9]+', 'ALPHA_ONLY' => '[\w]+', 'ALPHA_SPACERS' => '[\w_\+\. \-\[\]]+');
$validate_options = ''; $validate_options = '';
foreach ($validate_ary as $lang => $value) foreach ($this->validation_options as $lang => $value)
{ {
$selected = ($field_data['field_validation'] == $value) ? ' selected="selected"' : ''; $selected = ($field_data['field_validation'] == $value) ? ' selected="selected"' : '';
$validate_options .= '<option value="' . $value . '"' . $selected . '>' . $this->user->lang[$lang] . '</option>'; $validate_options .= '<option value="' . $value . '"' . $selected . '>' . $this->user->lang[$lang] . '</option>';
@ -69,17 +75,12 @@ abstract class type_string_common extends type_base
$field_validate = ($field_type != 'text') ? $field_value : bbcode_nl2br($field_value); $field_validate = ($field_type != 'text') ? $field_value : bbcode_nl2br($field_value);
if (!preg_match('#^' . str_replace('\\\\', '\\', $field_data['field_validation']) . '$#i', $field_validate)) if (!preg_match('#^' . str_replace('\\\\', '\\', $field_data['field_validation']) . '$#i', $field_validate))
{ {
switch ($field_data['field_validation']) $validation = array_search($field_data['field_validation'], $this->validation_options);
if ($validation)
{ {
case '[0-9]+': return $this->user->lang('FIELD_INVALID_CHARS_' . $validation, $this->get_field_name($field_data['lang_name']));
return $this->user->lang('FIELD_INVALID_CHARS_NUMBERS_ONLY', $this->get_field_name($field_data['lang_name']));
case '[\w]+':
return $this->user->lang('FIELD_INVALID_CHARS_ALPHA_ONLY', $this->get_field_name($field_data['lang_name']));
case '[\w_\+\. \-\[\]]+':
return $this->user->lang('FIELD_INVALID_CHARS_SPACERS_ONLY', $this->get_field_name($field_data['lang_name']));
} }
return $this->user->lang('FIELD_INVALID_CHARS_INVALID', $this->get_field_name($field_data['lang_name']));
} }
} }