Skip to content
This repository has been archived by the owner on Apr 28, 2024. It is now read-only.

added uri validation support in server side validation #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion fortress/DataSanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
}
4 changes: 4 additions & 0 deletions fortress/ServerSideValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
Expand Down