These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Ballen\Plexity\Support; |
||
3 | |||
4 | use \Ballen\Plexity\Plexity; |
||
5 | |||
6 | /** |
||
7 | * Plexity |
||
8 | * |
||
9 | * Plexity (Password Complexity) is a password complexity library that |
||
10 | * enables you to set "rules" for a password (or any other kind of string) that |
||
11 | * you can then check against in your application. |
||
12 | * |
||
13 | * @author Bobby Allen <[email protected]> |
||
14 | * @license http://opensource.org/licenses/MIT |
||
15 | * @link https://github.com/allebb/passplexity |
||
16 | * @link http://bobbyallen.me |
||
17 | * |
||
18 | */ |
||
19 | class Validator |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * The Plexity object (contains the validation configuration) |
||
24 | * @var \Ballen\Plexity\Plexity |
||
25 | */ |
||
26 | private $configuration; |
||
27 | |||
28 | /** |
||
29 | * Numeric values list |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $numbers = [ |
||
33 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 |
||
34 | ]; |
||
35 | |||
36 | /** |
||
37 | * Special Character list |
||
38 | * @see https://www.owasp.org/index.php/Password_special_characters |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $specialCharacters = [ |
||
42 | ' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '.', |
||
43 | '/', ':', ';', '<', '=', '>', '?', '@', '[', ']', '\\', '^', '_', '`', |
||
44 | '{', '|', '}', '~', |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * Validates all the configured rules and responds as requested. |
||
49 | * @return boolean |
||
50 | * @throws \Ballen\Plexity\Exceptions\ValidationException |
||
51 | */ |
||
52 | 22 | public function validate(Plexity $configuration) |
|
53 | { |
||
54 | 22 | $this->configuration = $configuration; |
|
55 | 22 | $this->checkMinimumLength(); |
|
56 | 20 | $this->checkMaximumLength(); |
|
57 | 18 | $this->checkLowerCase(); |
|
58 | 17 | $this->checkUpperCase(); |
|
59 | 16 | $this->checkNumericCharacters(); |
|
60 | 14 | $this->checkSpecialCharacters(); |
|
61 | 10 | $this->checkNotIn(); |
|
62 | 9 | return true; |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * Checks the minimum length requirement. |
||
67 | * @throws \Ballen\Plexity\Exceptions\ValidationException |
||
68 | */ |
||
69 | 22 | View Code Duplication | public function checkMinimumLength() |
70 | { |
||
71 | 22 | if ($this->configuration->rules()->get(Plexity::RULE_LENGTH_MIN) > 0) { |
|
72 | 6 | if (!$this->validateLengthMin()) { |
|
73 | 2 | throw new \Ballen\Plexity\Exceptions\ValidationException('The length does not meet the minimum length requirements.'); |
|
74 | } |
||
75 | 4 | } |
|
76 | 20 | } |
|
77 | |||
78 | /** |
||
79 | * Checks the minimum maximum length requirement. |
||
80 | * @throws \Ballen\Plexity\Exceptions\ValidationException |
||
81 | */ |
||
82 | 20 | View Code Duplication | public function checkMaximumLength() |
83 | { |
||
84 | 20 | if ($this->configuration->rules()->get(Plexity::RULE_LENGTH_MAX) > 0) { |
|
85 | 5 | if (!$this->validateLengthMax()) { |
|
86 | 2 | throw new \Ballen\Plexity\Exceptions\ValidationException('The length exceeds the maximum length requirements.'); |
|
87 | } |
||
88 | 3 | } |
|
89 | 18 | } |
|
90 | |||
91 | /** |
||
92 | * Checks the lowercase character(s) requirement. |
||
93 | * @throws \Ballen\Plexity\Exceptions\ValidationException |
||
94 | */ |
||
95 | 18 | public function checkLowerCase() |
|
96 | { |
||
97 | 18 | if ($this->configuration->rules()->get(Plexity::RULE_LOWER)) { |
|
98 | 2 | if (!$this->validateLowerCase()) { |
|
99 | 1 | throw new \Ballen\Plexity\Exceptions\ValidationException('The string failed to meet the lower case requirements.'); |
|
100 | } |
||
101 | 1 | } |
|
102 | 17 | } |
|
103 | |||
104 | /** |
||
105 | * Checks the upper case character(s) requirement. |
||
106 | * @throws \Ballen\Plexity\Exceptions\ValidationException |
||
107 | */ |
||
108 | 17 | public function checkUpperCase() |
|
109 | { |
||
110 | 17 | if ($this->configuration->rules()->get(Plexity::RULE_UPPER)) { |
|
111 | 2 | if (!$this->validateUpperCase()) { |
|
112 | 1 | throw new \Ballen\Plexity\Exceptions\ValidationException('The string failed to meet the upper case requirements.'); |
|
113 | } |
||
114 | 1 | } |
|
115 | 16 | } |
|
116 | |||
117 | /** |
||
118 | * Checks the numeric character(s) requirement. |
||
119 | * @throws \Ballen\Plexity\Exceptions\ValidationException |
||
120 | */ |
||
121 | 16 | View Code Duplication | public function checkNumericCharacters() |
122 | { |
||
123 | 16 | if ($this->configuration->rules()->get(Plexity::RULE_NUMERIC) > 0) { |
|
124 | 4 | if (!$this->validateNumericCharacters()) { |
|
125 | 2 | throw new \Ballen\Plexity\Exceptions\ValidationException('The string failed to meet the numeric character requirements.'); |
|
126 | } |
||
127 | 2 | } |
|
128 | 14 | } |
|
129 | |||
130 | /** |
||
131 | * Checks the special character(s) requirement. |
||
132 | * @throws \Ballen\Plexity\Exceptions\ValidationException |
||
133 | */ |
||
134 | 14 | View Code Duplication | public function checkSpecialCharacters() |
135 | { |
||
136 | 14 | if ($this->configuration->rules()->get(Plexity::RULE_SPECIAL) > 0) { |
|
137 | 4 | if (!$this->validateSpecialCharacters()) { |
|
138 | throw new \Ballen\Plexity\Exceptions\ValidationException('The string failed to meet the special character requirements.'); |
||
139 | } |
||
140 | } |
||
141 | 10 | } |
|
142 | |||
143 | 10 | View Code Duplication | public function checkNotIn() |
144 | { |
||
145 | 10 | if (count($this->configuration->rules()->get(Plexity::RULE_NOT_IN)) > 0) { |
|
146 | 2 | if (!$this->validateNotIn()) { |
|
147 | 1 | throw new \Ballen\Plexity\Exceptions\ValidationException('The string exists in the list of disallowed values requirements.'); |
|
148 | } |
||
149 | 1 | } |
|
150 | 9 | } |
|
151 | |||
152 | /** |
||
153 | * Validates the upper case requirements. |
||
154 | * @return boolean |
||
155 | */ |
||
156 | 2 | private function validateUpperCase() |
|
157 | { |
||
158 | 2 | return (bool) preg_match("/[A-Z]/", $this->configuration->checkString()); |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * Validates the lower case requirements. |
||
163 | * @return boolean |
||
164 | */ |
||
165 | 2 | private function validateLowerCase() |
|
166 | { |
||
167 | 2 | return (bool) preg_match("/[a-z]/", $this->configuration->checkString()); |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * Validates the special character requirements. |
||
172 | * @return boolean |
||
173 | */ |
||
174 | 4 | View Code Duplication | private function validateSpecialCharacters() |
175 | { |
||
176 | 4 | if ($this->countOccurences($this->special_characters, $this->configuration->checkString()) >= $this->configuration->rules()->get(Plexity::RULE_SPECIAL)) { |
|
0 ignored issues
–
show
|
|||
177 | return true; |
||
178 | } |
||
179 | return false; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Validates the numeric case requirements. |
||
184 | * @return boolean |
||
185 | */ |
||
186 | 4 | View Code Duplication | private function validateNumericCharacters() |
187 | { |
||
188 | 4 | if ($this->countOccurences($this->numbers, $this->configuration->checkString()) >= $this->configuration->rules()->get(Plexity::RULE_NUMERIC)) { |
|
189 | 2 | return true; |
|
190 | } |
||
191 | 2 | return false; |
|
192 | } |
||
193 | |||
194 | /** |
||
195 | * Validates the minimum string length requirements. |
||
196 | * @return boolean |
||
197 | */ |
||
198 | 6 | View Code Duplication | private function validateLengthMin() |
199 | { |
||
200 | 6 | if (strlen($this->configuration->checkString()) >= $this->configuration->rules()->get(Plexity::RULE_LENGTH_MIN)) { |
|
201 | 4 | return true; |
|
202 | } |
||
203 | 2 | return false; |
|
204 | } |
||
205 | |||
206 | /** |
||
207 | * Validates the maximum string length requirements. |
||
208 | * @return boolean |
||
209 | */ |
||
210 | 5 | View Code Duplication | private function validateLengthMax() |
211 | { |
||
212 | 5 | if (strlen($this->configuration->checkString()) <= $this->configuration->rules()->get(Plexity::RULE_LENGTH_MAX)) { |
|
213 | 3 | return true; |
|
214 | } |
||
215 | 2 | return false; |
|
216 | } |
||
217 | |||
218 | /** |
||
219 | * Validates the not_in requirements. |
||
220 | * @return boolean |
||
221 | */ |
||
222 | 2 | View Code Duplication | private function validateNotIn() |
223 | { |
||
224 | 2 | if (in_array($this->configuration->checkString(), $this->configuration->rules()->get(Plexity::RULE_NOT_IN))) { |
|
225 | 1 | return false; |
|
226 | } |
||
227 | 1 | return true; |
|
228 | } |
||
229 | |||
230 | /** |
||
231 | * Count the number of occurences of a character or string in a string. |
||
232 | * @param array $needles The character/string to count occurences of. |
||
233 | * @param string $haystack The string to check against. |
||
234 | * @return int The number of occurences. |
||
235 | */ |
||
236 | 4 | private function countOccurences(array $needles, $haystack) |
|
237 | { |
||
238 | 4 | $count = 0; |
|
239 | 4 | foreach ($needles as $char) { |
|
240 | 4 | $count += substr_count($haystack, $char); |
|
241 | 4 | } |
|
242 | 4 | return $count; |
|
243 | } |
||
244 | } |
||
245 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.