From 48a80058e4617573dae7f28f4b984a54379b0974 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 2 Jul 2021 12:29:51 -0400 Subject: [PATCH] Add $sanitizer->textdomain() method --- wire/core/Sanitizer.php | 57 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/wire/core/Sanitizer.php b/wire/core/Sanitizer.php index 45d2c159..ec70da1b 100644 --- a/wire/core/Sanitizer.php +++ b/wire/core/Sanitizer.php @@ -293,6 +293,7 @@ class Sanitizer extends Wire { 'templateName' => 's', 'text' => 's', 'textarea' => 's', + 'textdomain' => 's', 'trim' => 's', 'truncate' => 's', 'unentities' => 's', @@ -3583,6 +3584,62 @@ class Sanitizer extends Wire { return empty($value) ? null : $value; } + /** + * Sanitize as language textdomain + * + * #pw-internal + * + * @param string $value + * @return string + * @since 3.0.181 + * + */ + public function textdomain($value) { + + $value = $this->line($value, 1024); + $value = trim(strtolower($value)); + $slash = false; + $dot = false; + + if(!strlen($value)) return $value; + + if(strpos($value, '\\') !== false) { + $value = str_replace('\\', '/', $value); + } + + if(strpos($value, '/') !== false) { + $slash = true; + $config = $this->wire()->config; + $value = str_replace(ltrim($config->paths->root, '/'), '', $value); + $value = trim($value, '/'); + } + + if(strpos($value, '.') !== false) { + $dot = true; + while(strpos($value, '..') !== false) { + $value = str_replace('..', '.', $value); + } + } + + if($dot) { + $value = str_replace(array('/.', './', '-.', '.-'), array('/', '/', '.', '.'), $value); + } + + if($slash || $dot) { + $value = str_replace(array('/', '.'), array('--', '-'), $value); + } + + while(strpos($value, '---') !== false) { + $value = str_replace('---', '--', $value); + } + + if(!ctype_alnum(str_replace(array('-', '_'), '', $value))) { + $value = preg_replace('/[^-_a-z0-9]/', '_', $value); + } + + return trim($value, '-'); + } + /** * Validate that given value matches regex pattern. *