Update Core\Secury

Change the logic of the replInvalidChars() method.
This commit is contained in:
Visman 2022-05-05 16:37:28 +07:00
parent 3f7d5c996b
commit 91478fc53d

View file

@ -31,9 +31,11 @@ class Secury
) {
throw new InvalidArgumentException('Algorithm and salt can not be empty');
}
if (! \in_array($hmac['algo'], \hash_hmac_algos(), true)) {
throw new UnexpectedValueException('Algorithm not supported');
}
$this->hmac = $hmac;
}
@ -71,9 +73,10 @@ class Secury
*/
public function randomPass(int $len): string
{
$key = \random_bytes($len);
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
$key = \random_bytes($len);
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
$result = '';
for ($i = 0; $i < $len; ++$i) {
$result .= $chars[\ord($key[$i]) % 64];
}
@ -82,23 +85,33 @@ class Secury
}
/**
* Replacing invalid UTF-8 characters and remove control characters
* For string: Replacing invalid UTF-8 characters and remove control characters
* For other scalar or null: unchanged
*/
public function replInvalidChars(/* mixed */ $data) /* : mixed */
{
if (\is_array($data)) {
return \array_map([$this, 'replInvalidChars'], $data);
} elseif (\is_int($data)) {
} elseif (\is_string($data)) {
// Replacing invalid UTF-8 characters
// slow, small memory
//$data = mb_convert_encoding((string) $data, 'UTF-8', 'UTF-8');
// fast, large memory
$data = \htmlspecialchars_decode(\htmlspecialchars((string) $data, \ENT_SUBSTITUTE, 'UTF-8'));
// Canonical Decomposition followed by Canonical Composition
$data = Normalizer::normalize($data, Normalizer::FORM_C);
// Remove control characters
return \preg_replace('%(?:[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]|\xC2[\x80-\x9F])%', '', $data);
} elseif (
null === $data
|| \is_scalar($data)
) {
return $data;
} else {
throw new InvalidArgumentException('Unexpected variable type: ' . \gettype($data));
}
// Replacing invalid UTF-8 characters
// slow, small memory
//$data = mb_convert_encoding((string) $data, 'UTF-8', 'UTF-8');
// fast, large memory
$data = \htmlspecialchars_decode(\htmlspecialchars((string) $data, \ENT_SUBSTITUTE, 'UTF-8'));
// Canonical Decomposition followed by Canonical Composition
$data = Normalizer::normalize($data, Normalizer::FORM_C);
// Remove control characters
return \preg_replace('%(?:[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]|\xC2[\x80-\x9F])%', '', $data);
}
}