mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-07 23:16:13 +02:00
[ticket/11201] Move validate_profile_field() to type class
PHPBB3-11201
This commit is contained in:
parent
b3803d563a
commit
ee78aed2f1
@ -88,123 +88,6 @@ class profilefields
|
|||||||
$this->db->sql_freeresult($result);
|
$this->db->sql_freeresult($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Validate entered profile field data
|
|
||||||
* @access public
|
|
||||||
*/
|
|
||||||
function validate_profile_field($field_type, &$field_value, $field_data)
|
|
||||||
{
|
|
||||||
switch ($field_type)
|
|
||||||
{
|
|
||||||
case FIELD_DATE:
|
|
||||||
$field_validate = explode('-', $field_value);
|
|
||||||
|
|
||||||
$day = (isset($field_validate[0])) ? (int) $field_validate[0] : 0;
|
|
||||||
$month = (isset($field_validate[1])) ? (int) $field_validate[1] : 0;
|
|
||||||
$year = (isset($field_validate[2])) ? (int) $field_validate[2] : 0;
|
|
||||||
|
|
||||||
if ((!$day || !$month || !$year) && !$field_data['field_required'])
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((!$day || !$month || !$year) && $field_data['field_required'])
|
|
||||||
{
|
|
||||||
return 'FIELD_REQUIRED';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($day < 0 || $day > 31 || $month < 0 || $month > 12 || ($year < 1901 && $year > 0) || $year > gmdate('Y', time()) + 50)
|
|
||||||
{
|
|
||||||
return 'FIELD_INVALID_DATE';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (checkdate($month, $day, $year) === false)
|
|
||||||
{
|
|
||||||
return 'FIELD_INVALID_DATE';
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FIELD_BOOL:
|
|
||||||
$field_value = (bool) $field_value;
|
|
||||||
|
|
||||||
if (!$field_value && $field_data['field_required'])
|
|
||||||
{
|
|
||||||
return 'FIELD_REQUIRED';
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FIELD_INT:
|
|
||||||
if (trim($field_value) === '' && !$field_data['field_required'])
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$field_value = (int) $field_value;
|
|
||||||
|
|
||||||
if ($field_value < $field_data['field_minlen'])
|
|
||||||
{
|
|
||||||
return 'FIELD_TOO_SMALL';
|
|
||||||
}
|
|
||||||
else if ($field_value > $field_data['field_maxlen'])
|
|
||||||
{
|
|
||||||
return 'FIELD_TOO_LARGE';
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FIELD_DROPDOWN:
|
|
||||||
$field_value = (int) $field_value;
|
|
||||||
|
|
||||||
// retrieve option lang data if necessary
|
|
||||||
if (!isset($this->options_lang[$field_data['field_id']]) || !isset($this->options_lang[$field_data['field_id']][$field_data['lang_id']]) || !sizeof($this->options_lang[$file_data['field_id']][$field_data['lang_id']]))
|
|
||||||
{
|
|
||||||
$this->get_option_lang($field_data['field_id'], $field_data['lang_id'], FIELD_DROPDOWN, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isset($this->options_lang[$field_data['field_id']][$field_data['lang_id']][$field_value]))
|
|
||||||
{
|
|
||||||
return 'FIELD_INVALID_VALUE';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($field_value == $field_data['field_novalue'] && $field_data['field_required'])
|
|
||||||
{
|
|
||||||
return 'FIELD_REQUIRED';
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FIELD_STRING:
|
|
||||||
case FIELD_TEXT:
|
|
||||||
if (trim($field_value) === '' && !$field_data['field_required'])
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else if (trim($field_value) === '' && $field_data['field_required'])
|
|
||||||
{
|
|
||||||
return 'FIELD_REQUIRED';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($field_data['field_minlen'] && utf8_strlen($field_value) < $field_data['field_minlen'])
|
|
||||||
{
|
|
||||||
return 'FIELD_TOO_SHORT';
|
|
||||||
}
|
|
||||||
else if ($field_data['field_maxlen'] && utf8_strlen($field_value) > $field_data['field_maxlen'])
|
|
||||||
{
|
|
||||||
return 'FIELD_TOO_LONG';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($field_data['field_validation']) && $field_data['field_validation'] != '.*')
|
|
||||||
{
|
|
||||||
$field_validate = ($field_type == FIELD_STRING) ? $field_value : bbcode_nl2br($field_value);
|
|
||||||
if (!preg_match('#^' . str_replace('\\\\', '\\', $field_data['field_validation']) . '$#i', $field_validate))
|
|
||||||
{
|
|
||||||
return 'FIELD_INVALID_CHARS';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build profile cache, used for display
|
* Build profile cache, used for display
|
||||||
* @access private
|
* @access private
|
||||||
@ -305,7 +188,7 @@ class profilefields
|
|||||||
$cp_data['pf_' . $row['field_ident']] = $profile_field->get_profile_field($row);
|
$cp_data['pf_' . $row['field_ident']] = $profile_field->get_profile_field($row);
|
||||||
$check_value = $cp_data['pf_' . $row['field_ident']];
|
$check_value = $cp_data['pf_' . $row['field_ident']];
|
||||||
|
|
||||||
if (($cp_result = $this->validate_profile_field($row['field_type'], $check_value, $row)) !== false)
|
if (($cp_result = $profile_field->validate_profile_field($check_value, $row)) !== false)
|
||||||
{
|
{
|
||||||
// If not and only showing common error messages, use this one
|
// If not and only showing common error messages, use this one
|
||||||
$error = '';
|
$error = '';
|
||||||
|
@ -79,4 +79,19 @@ class type_bool implements type_interface
|
|||||||
return $this->request->variable($var_name, (int) $profile_row['field_default_value']);
|
return $this->request->variable($var_name, (int) $profile_row['field_default_value']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function validate_profile_field(&$field_value, $field_data)
|
||||||
|
{
|
||||||
|
$field_value = (bool) $field_value;
|
||||||
|
|
||||||
|
if (!$field_value && $field_data['field_required'])
|
||||||
|
{
|
||||||
|
return 'FIELD_REQUIRED';
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -95,4 +95,38 @@ class type_date implements type_interface
|
|||||||
|
|
||||||
return sprintf('%2d-%2d-%4d', $day, $month, $year);
|
return sprintf('%2d-%2d-%4d', $day, $month, $year);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function validate_profile_field(&$field_value, $field_data)
|
||||||
|
{
|
||||||
|
$field_validate = explode('-', $field_value);
|
||||||
|
|
||||||
|
$day = (isset($field_validate[0])) ? (int) $field_validate[0] : 0;
|
||||||
|
$month = (isset($field_validate[1])) ? (int) $field_validate[1] : 0;
|
||||||
|
$year = (isset($field_validate[2])) ? (int) $field_validate[2] : 0;
|
||||||
|
|
||||||
|
if ((!$day || !$month || !$year) && !$field_data['field_required'])
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((!$day || !$month || !$year) && $field_data['field_required'])
|
||||||
|
{
|
||||||
|
return 'FIELD_REQUIRED';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($day < 0 || $day > 31 || $month < 0 || $month > 12 || ($year < 1901 && $year > 0) || $year > gmdate('Y', time()) + 50)
|
||||||
|
{
|
||||||
|
return 'FIELD_INVALID_DATE';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (checkdate($month, $day, $year) === false)
|
||||||
|
{
|
||||||
|
return 'FIELD_INVALID_DATE';
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,4 +74,30 @@ class type_dropdown implements type_interface
|
|||||||
$var_name = 'pf_' . $profile_row['field_ident'];
|
$var_name = 'pf_' . $profile_row['field_ident'];
|
||||||
return $this->request->variable($var_name, (int) $profile_row['field_default_value']);
|
return $this->request->variable($var_name, (int) $profile_row['field_default_value']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function validate_profile_field(&$field_value, $field_data)
|
||||||
|
{
|
||||||
|
$field_value = (int) $field_value;
|
||||||
|
|
||||||
|
// retrieve option lang data if necessary
|
||||||
|
if (!isset($this->profilefields->options_lang[$field_data['field_id']]) || !isset($this->profilefields->options_lang[$field_data['field_id']][$field_data['lang_id']]) || !sizeof($this->profilefields->options_lang[$file_data['field_id']][$field_data['lang_id']]))
|
||||||
|
{
|
||||||
|
$this->profilefields->get_option_lang($field_data['field_id'], $field_data['lang_id'], FIELD_DROPDOWN, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isset($this->profilefields->options_lang[$field_data['field_id']][$field_data['lang_id']][$field_value]))
|
||||||
|
{
|
||||||
|
return 'FIELD_INVALID_VALUE';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($field_value == $field_data['field_novalue'] && $field_data['field_required'])
|
||||||
|
{
|
||||||
|
return 'FIELD_REQUIRED';
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,4 +65,28 @@ class type_int implements type_interface
|
|||||||
return $this->request->variable($var_name, (int) $profile_row['field_default_value']);
|
return $this->request->variable($var_name, (int) $profile_row['field_default_value']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function validate_profile_field(&$field_value, $field_data)
|
||||||
|
{
|
||||||
|
if (trim($field_value) === '' && !$field_data['field_required'])
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$field_value = (int) $field_value;
|
||||||
|
|
||||||
|
if ($field_value < $field_data['field_minlen'])
|
||||||
|
{
|
||||||
|
return 'FIELD_TOO_SMALL';
|
||||||
|
}
|
||||||
|
else if ($field_value > $field_data['field_maxlen'])
|
||||||
|
{
|
||||||
|
return 'FIELD_TOO_LARGE';
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -44,4 +44,13 @@ interface type_interface
|
|||||||
* @return mixed Submitted value of the profile field
|
* @return mixed Submitted value of the profile field
|
||||||
*/
|
*/
|
||||||
public function get_profile_field($profile_row);
|
public function get_profile_field($profile_row);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate entered profile field data
|
||||||
|
*
|
||||||
|
* @param mixed $field_value Field value to validate
|
||||||
|
* @param array $field_data Array with requirements of the field
|
||||||
|
* @return mixed String with key of the error language string, false otherwise
|
||||||
|
*/
|
||||||
|
public function validate_profile_field(&$field_value, $field_data);
|
||||||
}
|
}
|
||||||
|
@ -58,4 +58,12 @@ class type_string extends type_string_common implements type_interface
|
|||||||
$var_name = 'pf_' . $profile_row['field_ident'];
|
$var_name = 'pf_' . $profile_row['field_ident'];
|
||||||
return $this->request->variable($var_name, (string) $profile_row['field_default_value'], true);
|
return $this->request->variable($var_name, (string) $profile_row['field_default_value'], true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function validate_profile_field(&$field_value, $field_data)
|
||||||
|
{
|
||||||
|
return $this->validate_string_profile_field('string', &$field_value, $field_data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,4 +27,44 @@ abstract class type_string_common
|
|||||||
|
|
||||||
return $validate_options;
|
return $validate_options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate entered profile field data
|
||||||
|
*
|
||||||
|
* @param string $field_type Field type (string or text)
|
||||||
|
* @param mixed $field_value Field value to validate
|
||||||
|
* @param array $field_data Array with requirements of the field
|
||||||
|
* @return mixed String with key of the error language string, false otherwise
|
||||||
|
*/
|
||||||
|
public function validate_string_profile_field($field_type, &$field_value, $field_data)
|
||||||
|
{
|
||||||
|
if (trim($field_value) === '' && !$field_data['field_required'])
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else if (trim($field_value) === '' && $field_data['field_required'])
|
||||||
|
{
|
||||||
|
return 'FIELD_REQUIRED';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($field_data['field_minlen'] && utf8_strlen($field_value) < $field_data['field_minlen'])
|
||||||
|
{
|
||||||
|
return 'FIELD_TOO_SHORT';
|
||||||
|
}
|
||||||
|
else if ($field_data['field_maxlen'] && utf8_strlen($field_value) > $field_data['field_maxlen'])
|
||||||
|
{
|
||||||
|
return 'FIELD_TOO_LONG';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($field_data['field_validation']) && $field_data['field_validation'] != '.*')
|
||||||
|
{
|
||||||
|
$field_validate = ($field_type != 'text') ? $field_value : bbcode_nl2br($field_value);
|
||||||
|
if (!preg_match('#^' . str_replace('\\\\', '\\', $field_data['field_validation']) . '$#i', $field_validate))
|
||||||
|
{
|
||||||
|
return 'FIELD_INVALID_CHARS';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,4 +58,12 @@ class type_text extends type_string_common implements type_interface
|
|||||||
$var_name = 'pf_' . $profile_row['field_ident'];
|
$var_name = 'pf_' . $profile_row['field_ident'];
|
||||||
return $this->request->variable($var_name, (string) $profile_row['field_default_value'], true);
|
return $this->request->variable($var_name, (string) $profile_row['field_default_value'], true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
public function validate_profile_field(&$field_value, $field_data)
|
||||||
|
{
|
||||||
|
return $this->validate_string_profile_field('text', &$field_value, $field_data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user