|
1
|
|
|
<?php |
|
2
|
|
|
namespace Alister\ReservedNamesBundle\Services; |
|
3
|
|
|
|
|
4
|
|
|
class ReservedNames implements ReservedNamesInterface |
|
5
|
|
|
{ |
|
6
|
|
|
/** @var array of usernames to check against */ |
|
7
|
|
|
private $reservedNames; |
|
8
|
|
|
|
|
9
|
|
|
/** @var CleanUserNamesInterface class to return a 'clean' username */ |
|
10
|
|
|
private $cleanUsername; |
|
11
|
|
|
|
|
12
|
|
|
public function __construct(array $reservedNames, CleanUserNamesInterface $cleanUsername) |
|
13
|
|
|
{ |
|
14
|
|
|
if (empty($this->reservedNames)) { |
|
15
|
|
|
$this->reservedNames = $reservedNames; |
|
16
|
|
|
} |
|
17
|
|
|
$this->cleanUsername = $cleanUsername; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Allow the reserved username list to be extracted. |
|
22
|
|
|
* |
|
23
|
|
|
* The inbuild list is written all in lower-case, but any new ones are |
|
24
|
|
|
* down-cased as part of the bundle configuration. |
|
25
|
|
|
* |
|
26
|
|
|
* @return array list of usernames ['username' => 1, ...] |
|
27
|
|
|
*/ |
|
28
|
|
|
public function getReservedNames() |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->reservedNames; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* A ReservedName is one based on a list - we also strip numbers and '-_' chars. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $username [description] |
|
37
|
|
|
* |
|
38
|
|
|
* @return bool Is the username reserved? |
|
39
|
|
|
*/ |
|
40
|
|
|
public function isReserved($username) |
|
41
|
|
|
{ |
|
42
|
|
|
if (array_key_exists($username, $this->reservedNames)) { |
|
43
|
|
|
return true; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$cleanName = $this->cleanUsername->clean($username); |
|
47
|
|
|
if ($this->isTest($cleanName, true)) { |
|
48
|
|
|
return true; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if ($this->inReservedList($cleanName)) { |
|
52
|
|
|
return true; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// we've gotten here, so far, so good. One final test - trim 's's |
|
56
|
|
|
return $this->isReservedPlural($cleanName); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function inReservedList($username) |
|
60
|
|
|
{ |
|
61
|
|
|
return array_key_exists($username, $this->reservedNames); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* For our point of view, we are looking for anything with an 's'. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $username [description] |
|
68
|
|
|
* |
|
69
|
|
|
* @return bool Does it have an 's' at the end (with any 'noise chars' before it) |
|
70
|
|
|
*/ |
|
71
|
|
|
public function isReservedPlural($username) |
|
72
|
|
|
{ |
|
73
|
|
|
// finally, remove any trailing 's', or the usual other chars |
|
74
|
|
|
$cleanerName = rtrim($username, 's0123456789-_ '); |
|
75
|
|
|
|
|
76
|
|
|
return $this->inReservedList($cleanerName); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Convenience function - is the post-cleaned username start with 'test' ? |
|
81
|
|
|
* |
|
82
|
|
|
* @param string username to clean and check |
|
|
|
|
|
|
83
|
|
|
* |
|
84
|
|
|
* @return bool true if the first 4 'real' chars are 'test' |
|
85
|
|
|
*/ |
|
86
|
|
|
public function isTest($username, $isClean = false) |
|
87
|
|
|
{ |
|
88
|
|
|
if (!$isClean) { |
|
89
|
|
|
$username = $this->cleanUsername->clean($username); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/* |
|
93
|
|
|
* This direct-check-return is good for code complexity, |
|
94
|
|
|
* but doesn't obviously check the true and false cases. |
|
95
|
|
|
* With an explicit return true/false, we would prove that |
|
96
|
|
|
* both possibilities were covered. |
|
97
|
|
|
*/ |
|
98
|
|
|
return 0 === strpos($username, 'test'); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths