diff --git a/fortress/DataSanitizer.php b/fortress/DataSanitizer.php index fed0d9d..a100265 100644 --- a/fortress/DataSanitizer.php +++ b/fortress/DataSanitizer.php @@ -116,6 +116,7 @@ public function sanitizeField($name, $value){ case "purify": $sanitizedValue = $this->_purifier->purify($sanitizedValue); $processed = true; break; case "escape": $sanitizedValue = $this->escapeHtmlCharacters($sanitizedValue); $processed = true; break; case "purge" : $sanitizedValue = $this->purgeHtmlCharacters($sanitizedValue); $processed = true; break; + case "purgeuri" : $sanitizedValue = $this->purgeUriCharacters($sanitizedValue); $processed = true; break; case "raw" : $processed = true; break; default: break; } @@ -142,6 +143,32 @@ private function purgeHtmlCharacters($value){ else return filter_var($value, FILTER_SANITIZE_STRING); } + + /** Clean up characters unwanted for uri */ + private function purgeUriCharacters($value){ + // replace non letter or digits by - + $value = preg_replace('~[^\pL\d]+~u', '-', $value); + + // transliterate + $value = iconv('utf-8', 'us-ascii//TRANSLIT', $value); + + // remove unwanted characters + $value = preg_replace('~[^-\w]+~', '', $value); + + // trim + $value = trim($value, '-'); + + // remove duplicate - + $value = preg_replace('~-+~', '-', $value); + + // lowercase + $value = strtolower($value); + + if (empty($value)) + { + return 'n-a'; } -?> + return $value; + } +} \ No newline at end of file diff --git a/fortress/ServerSideValidator.php b/fortress/ServerSideValidator.php index 42a976f..8b1c812 100644 --- a/fortress/ServerSideValidator.php +++ b/fortress/ServerSideValidator.php @@ -156,6 +156,10 @@ private function generateSchemaRules(){ if ($validator_name == "not_member_of"){ $this->ruleWithMessage("notIn", $message_set, $field_name, $validator['values'], true); // Strict comparison } + // validate uri + if ($validator_name == "uri"){ + $this->ruleWithMessage("slug", $message_set, $field_name); + } } } }