From 1715619fda2aebd7ebae960e5fcbdf4b24ce4dca Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Thu, 16 May 2013 13:22:43 +0200 Subject: [ticket/11538] Add function phpbb_validate_colour for validating colours It will be possible to use this function via the validate_data() function interface that has already been used previously. Thus, this new function will extend the capabilities of validate_data() to checking hex color values. PHPBB3-11538 --- phpBB/includes/functions_user.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'phpBB/includes/functions_user.php') diff --git a/phpBB/includes/functions_user.php b/phpBB/includes/functions_user.php index 5a6a0b4a05..b7fdb7a6ad 100644 --- a/phpBB/includes/functions_user.php +++ b/phpBB/includes/functions_user.php @@ -1247,8 +1247,9 @@ function validate_data($data, $val_ary) { $function = array_shift($validate); array_unshift($validate, $data[$var]); + $function_prefix = (function_exists('phpbb_validate_' . $function)) ? 'phpbb_validate_' : 'validate'; - if ($result = call_user_func_array('validate_' . $function, $validate)) + if ($result = call_user_func_array($function_prefix . $function, $validate)) { // Since errors are checked later for their language file existence, we need to make sure custom errors are not adjusted. $error[] = (empty($user->lang[$result . '_' . strtoupper($var)])) ? $result : $result . '_' . strtoupper($var); @@ -1898,6 +1899,27 @@ function validate_jabber($jid) return false; } +/** +* Validate hex colour value +* +* @param string $colour The hex colour value +* @return bool/string Error message if colour value is incorrect, false if it fits the hex colour code +*/ +function phpbb_validate_colour($colour) +{ + if (empty($colour)) + { + return false; + } + + if (!preg_match('/^([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$/', $colour)) + { + return 'WRONG_DATA'; + } + + return false; +} + /** * Verifies whether a style ID corresponds to an active style. * -- cgit v1.2.1 From b49ce5eb3a54a6c256955d3510fe409a6f4511aa Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sun, 19 May 2013 11:29:11 +0200 Subject: [ticket/11538] Rename phpbb_validate_colour to phpbb_validate_hex_colour PHPBB3-11538 --- phpBB/includes/functions_user.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'phpBB/includes/functions_user.php') diff --git a/phpBB/includes/functions_user.php b/phpBB/includes/functions_user.php index b7fdb7a6ad..f8e1fcaa45 100644 --- a/phpBB/includes/functions_user.php +++ b/phpBB/includes/functions_user.php @@ -1903,9 +1903,10 @@ function validate_jabber($jid) * Validate hex colour value * * @param string $colour The hex colour value -* @return bool/string Error message if colour value is incorrect, false if it fits the hex colour code +* @return bool|string Error message if colour value is incorrect, false if it +* fits the hex colour code */ -function phpbb_validate_colour($colour) +function phpbb_validate_hex_colour($colour) { if (empty($colour)) { -- cgit v1.2.1 From cd1da92d8540d6b4aa0fa1ccf2bcafe68007288a Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sun, 19 May 2013 17:45:45 +0200 Subject: [ticket/11538] Add optional switch as argument to hex colour validation The value of $optional will decide whether an empty string will be treated as incorrect input or if it is allowed. The optional argument will default to false and therefore treat an empty string as incorrect unless explicitly told to not do so. PHPBB3-11538 --- phpBB/includes/functions_user.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'phpBB/includes/functions_user.php') diff --git a/phpBB/includes/functions_user.php b/phpBB/includes/functions_user.php index f8e1fcaa45..61972c3876 100644 --- a/phpBB/includes/functions_user.php +++ b/phpBB/includes/functions_user.php @@ -1903,14 +1903,16 @@ function validate_jabber($jid) * Validate hex colour value * * @param string $colour The hex colour value -* @return bool|string Error message if colour value is incorrect, false if it +* @param bool $optional Whether the colour value is optional. True if an empty +* string will be accepted as correct input, false if not. +* @return bool|string Error message if colour value is incorrect, false if it * fits the hex colour code */ -function phpbb_validate_hex_colour($colour) +function phpbb_validate_hex_colour($colour, $optional = false) { if (empty($colour)) { - return false; + return (($optional) ? false : 'WRONG_DATA'); } if (!preg_match('/^([0-9a-fA-F]{6}|[0-9a-fA-F]{3})$/', $colour)) -- cgit v1.2.1