mirror of
https://github.com/processwire/processwire.git
synced 2025-08-09 00:06:55 +02:00
Update $sanitizer->removeMB4() method to use a default replacement character (can be set in $options). Plus implement a shorter replacement function (version 2). Also some updates to the $sanitizer->validateFile() method.
This commit is contained in:
@@ -1548,7 +1548,7 @@ class PagesEditor extends Wire {
|
|||||||
* @param HookEvent $event
|
* @param HookEvent $event
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
protected function hookFieldtypeSleepValueStripMB4(HookEvent $event) {
|
public function hookFieldtypeSleepValueStripMB4(HookEvent $event) {
|
||||||
$event->return = $this->wire('sanitizer')->removeMB4($event->return);
|
$event->return = $this->wire('sanitizer')->removeMB4($event->return);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -3178,18 +3178,29 @@ class Sanitizer extends Wire {
|
|||||||
* #pw-group-strings
|
* #pw-group-strings
|
||||||
*
|
*
|
||||||
* @param string|array $value String or array containing strings
|
* @param string|array $value String or array containing strings
|
||||||
|
* @param array $options Options to modify behavior, 3.0.169+ only:
|
||||||
|
* - `replaceWith` (string): Replace MB4+ characters with this character, may not be blank (default='<27>')
|
||||||
|
* - `version` (int): Replacement method version (default=2)
|
||||||
* @return string|array|mixed
|
* @return string|array|mixed
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function removeMB4($value) {
|
public function removeMB4($value, array $options = array()) {
|
||||||
if(empty($value)) return $value;
|
$defaults = array(
|
||||||
|
'replaceWith' => "\xEF\xBF\xBD", // Default unicode replacement character: U+FFFD aka <20>
|
||||||
|
'version' => 2,
|
||||||
|
);
|
||||||
|
$options = array_merge($defaults, $options);
|
||||||
|
if($options['replaceWith'] === '') $options['replaceWidth'] = $defaults['replaceWith'];
|
||||||
if(is_array($value)) {
|
if(is_array($value)) {
|
||||||
|
if(!count($value)) return array();
|
||||||
// process array recursively, looking for strings to convert
|
// process array recursively, looking for strings to convert
|
||||||
foreach($value as $key => $val) {
|
foreach($value as $key => $val) {
|
||||||
if(empty($val)) continue;
|
if(is_string($val) || is_array($val)) $value[$key] = $this->removeMB4($val, $options);
|
||||||
if(is_string($val) || is_array($val)) $value[$key] = $this->removeMB4($val);
|
|
||||||
}
|
}
|
||||||
} else if(is_string($value)) {
|
} else if(is_string($value)) {
|
||||||
|
if($options['version'] >= 2) {
|
||||||
|
$value = preg_replace('/[\x{10000}-\x{10FFFF}]/u', $options['replaceWith'], $value);
|
||||||
|
} else {
|
||||||
if(strlen($value) > 3 && max(array_map('ord', str_split($value))) >= 240) {
|
if(strlen($value) > 3 && max(array_map('ord', str_split($value))) >= 240) {
|
||||||
// string contains 4-byte characters
|
// string contains 4-byte characters
|
||||||
$regex =
|
$regex =
|
||||||
@@ -3198,7 +3209,8 @@ class Sanitizer extends Wire {
|
|||||||
'|[\xF1-\xF3][\x80-\xBF]{3}' .
|
'|[\xF1-\xF3][\x80-\xBF]{3}' .
|
||||||
'|\xF4[\x80-\x8F][\x80-\xBF]{2}' .
|
'|\xF4[\x80-\x8F][\x80-\xBF]{2}' .
|
||||||
')!s';
|
')!s';
|
||||||
$value = preg_replace($regex, '', $value);
|
$value = preg_replace($regex, $options['replaceWith'], $value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// not a string or an array, leave as-is
|
// not a string or an array, leave as-is
|
||||||
@@ -4807,9 +4819,10 @@ class Sanitizer extends Wire {
|
|||||||
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
|
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
|
||||||
$validatorNames = array();
|
$validatorNames = array();
|
||||||
$validatorResults = array();
|
$validatorResults = array();
|
||||||
$isValid = null;
|
|
||||||
$getArray = $options['getArray'];
|
$getArray = $options['getArray'];
|
||||||
$dryrun = $options['dryrun'] || !empty($options['dryRun']);
|
$dryrun = $options['dryrun'] || !empty($options['dryRun']);
|
||||||
|
$numFailed = 0;
|
||||||
|
$numPassed = 0;
|
||||||
|
|
||||||
if(!strlen($extension) || (!$dryrun && !is_file($filename))) {
|
if(!strlen($extension) || (!$dryrun && !is_file($filename))) {
|
||||||
return $getArray ? array() : null;
|
return $getArray ? array() : null;
|
||||||
@@ -4857,11 +4870,11 @@ class Sanitizer extends Wire {
|
|||||||
// true (bool): file is valid as-is
|
// true (bool): file is valid as-is
|
||||||
// 1 (int): file is valid as a result of sanitization
|
// 1 (int): file is valid as a result of sanitization
|
||||||
// in either case, continue on to the next applicable FileValidator module
|
// in either case, continue on to the next applicable FileValidator module
|
||||||
continue;
|
$numPassed++;
|
||||||
}
|
|
||||||
|
|
||||||
|
} else {
|
||||||
// at this point we’ve determined file is not valid
|
// at this point we’ve determined file is not valid
|
||||||
$isValid = false;
|
$numFailed++;
|
||||||
|
|
||||||
// move errors to Sanitizer class so they can be retrieved
|
// move errors to Sanitizer class so they can be retrieved
|
||||||
foreach($validator->errors('clear array') as $error) {
|
foreach($validator->errors('clear array') as $error) {
|
||||||
@@ -4872,8 +4885,18 @@ class Sanitizer extends Wire {
|
|||||||
// unless we are returning an array of results, we can stop now for invalid files
|
// unless we are returning an array of results, we can stop now for invalid files
|
||||||
if(!$getArray) break;
|
if(!$getArray) break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $getArray ? $validatorResults : $isValid;
|
// return array result of all validations
|
||||||
|
if($getArray) return $validatorResults;
|
||||||
|
|
||||||
|
// return null if no validators could be used
|
||||||
|
if(!$numPassed && !$numFailed) return null;
|
||||||
|
|
||||||
|
// if passsed 1+ validators and failed 0, return true
|
||||||
|
if($numPassed > 0 && $numFailed === 0) return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**********************************************************************************************************************
|
/**********************************************************************************************************************
|
||||||
|
Reference in New Issue
Block a user