|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Sanitizers; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
|
6
|
|
|
|
|
7
|
|
|
class SanitizeUserName extends StringSanitizer |
|
8
|
|
|
{ |
|
9
|
|
|
public array $args; |
|
10
|
|
|
public $value; |
|
11
|
|
|
public array $values; |
|
12
|
|
|
|
|
13
|
27 |
|
public function __construct($value, array $args = [], array $values = []) |
|
14
|
|
|
{ |
|
15
|
27 |
|
$args = array_pad($args, 2, ''); // minimum of 2 args |
|
16
|
27 |
|
$this->args = $args; |
|
17
|
27 |
|
$this->value = $this->userValue($value); |
|
18
|
27 |
|
$this->values = $values; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
27 |
|
public function run(): string |
|
22
|
|
|
{ |
|
23
|
27 |
|
$value = $this->sanitizeValue($this->value()); // allow any valid name value |
|
24
|
27 |
|
if (defined('WP_IMPORTING')) { |
|
25
|
|
|
return $value; |
|
26
|
|
|
} |
|
27
|
27 |
|
if ('' !== $value) { // allow "0" |
|
28
|
19 |
|
return $value; |
|
29
|
|
|
} |
|
30
|
14 |
|
$fallback = Cast::toString($this->args[0]); // try the fallback value |
|
31
|
14 |
|
if ('current_user' === $fallback) { |
|
32
|
2 |
|
$fallback = $this->userValue(wp_get_current_user()); |
|
33
|
|
|
} |
|
34
|
14 |
|
return $this->sanitizeValue($fallback); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* \p{L} = any kind of letter from any language. |
|
39
|
|
|
* \p{M} = any character intended to be combined with another character (e.g. accents, umlauts, enclosing boxes, etc.). |
|
40
|
|
|
* \p{N} = any kind of numeric character in any script. |
|
41
|
|
|
* \p{Pf} = any kind of closing quote. |
|
42
|
|
|
* |
|
43
|
|
|
* @see https://www.regular-expressions.info/unicode.html |
|
44
|
|
|
*/ |
|
45
|
27 |
|
protected function sanitizeValue(string $value): string |
|
46
|
|
|
{ |
|
47
|
27 |
|
if ('' === $value) { |
|
48
|
14 |
|
return $value; |
|
49
|
|
|
} |
|
50
|
19 |
|
$name = strstr($value, '@', true) ?: $value; // prevent exposing user email |
|
51
|
19 |
|
$name = $this->kses($name); |
|
52
|
19 |
|
$name = html_entity_decode($name, ENT_QUOTES, 'UTF-8'); // &lt => < |
|
53
|
19 |
|
$name = wp_specialchars_decode($name); // < => < |
|
54
|
19 |
|
$name = wp_strip_all_tags($name); |
|
55
|
19 |
|
$name = preg_replace('/%([a-fA-F0-9][a-fA-F0-9])/', '', $name); // Remove percent-encoded characters. |
|
56
|
19 |
|
$name = preg_replace('/&.+?;/', '', $name); // Remove HTML entities. |
|
57
|
19 |
|
$name = preg_replace('/[^\p{L}\p{M}\p{N}\p{Pf}\'\.\,\- ]/u', '', $name); |
|
58
|
19 |
|
$name = sanitize_text_field($name); // Remove extra whitespace. |
|
59
|
19 |
|
return $name; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param mixed $user |
|
64
|
|
|
*/ |
|
65
|
27 |
|
protected function userValue($user): string |
|
66
|
|
|
{ |
|
67
|
27 |
|
if (!$user instanceof \WP_User) { |
|
68
|
19 |
|
return Cast::toString($user); |
|
69
|
|
|
} |
|
70
|
14 |
|
if (!$user->exists()) { |
|
71
|
12 |
|
return ''; |
|
72
|
|
|
} |
|
73
|
2 |
|
return $user->display_name ?: $user->user_nicename ?: $user->user_login; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|