From 45bffa6f722805fd25057f7ba5fb39f658f0c6a3 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 1 Oct 2021 13:25:11 -0400 Subject: [PATCH] Add new Template::isValidUrlSegmentStr($value) method that validates if given URL segment string $value matches the rules defined for that template. --- wire/core/Template.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/wire/core/Template.php b/wire/core/Template.php index 490c7521..1616b161 100644 --- a/wire/core/Template.php +++ b/wire/core/Template.php @@ -852,6 +852,42 @@ class Template extends WireData implements Saveable, Exportable { return $value; } + + /** + * Is the given URL segment string allowed according to this template’s settings? + * + * #pw-group-URLs + * + * @param string $urlSegmentStr + * @return bool + * @since 3.0.186 + * + */ + public function isValidUrlSegmentStr($urlSegmentStr) { + + $rules = $this->urlSegments(); + $valid = false; + + if(is_array($rules)) { + // only specific URL segments are allowed + $urlSegmentStr = trim($urlSegmentStr, '/'); + foreach($rules as $rule) { + if(stripos($rule, 'regex:') === 0) { + $regex = '{' . trim(substr($rule, 6)) . '}'; + $valid = preg_match($regex, $urlSegmentStr); + } else if($urlSegmentStr === $rule) { + $valid = true; + } + if($valid) break; + } + } else if($rules > 0 || $this->name === 'admin') { + // all URL segments are allowed + $valid = true; + } + + return $valid; + } + /** * Set the flags for this Template