From 89e06769f1103dc1c763103d4828f136adec8cec Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Thu, 28 Jan 2021 21:47:59 +0100 Subject: [PATCH] Add class for validating color codes --- MD_STD_IN.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/MD_STD_IN.php b/MD_STD_IN.php index e68c575..f2107cf 100644 --- a/MD_STD_IN.php +++ b/MD_STD_IN.php @@ -85,6 +85,27 @@ final class MD_STD_IN { } + /** + * String sanitization for 3 o4 6 characters RGB color codes (sans the leading #). + * + * @param mixed $input Input string. + * + * @return string + */ + public static function sanitize_rgb_color($input):string { + + $output = \filter_var($input, + FILTER_SANITIZE_STRING, + FILTER_FLAG_NO_ENCODE_QUOTES); + + if (!(preg_match('/^[a-zA-Z0-9]{3}$/', $output)) && !(preg_match('/^[a-zA-Z0-9]{6}$/', $output))) { + throw new MDInvalidColorCode("Invalid color code provided: " . $output); + } + + return $output; + + } + /** * Retrieves HTTP input texts from GET or POST variables, whatever is provided. * If neither is given, returns a provided default.