1 | <?php |
||
58 | class Antibot implements LoggerAwareInterface |
||
59 | { |
||
60 | /** |
||
61 | * Session persistent, unique token |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $unique; |
||
66 | /** |
||
67 | * Antibot prefix |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | protected $prefix; |
||
72 | /** |
||
73 | * Unique signature |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | protected $signature; |
||
78 | /** |
||
79 | * Parameter prefix |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $parameterPrefix; |
||
84 | /** |
||
85 | * GET & POST data |
||
86 | * |
||
87 | * @var null|array |
||
88 | */ |
||
89 | protected $data = null; |
||
90 | /** |
||
91 | * Validators |
||
92 | * |
||
93 | * @var ValidatorInterface[] |
||
94 | */ |
||
95 | protected $validators = []; |
||
96 | /** |
||
97 | * Immutable instance |
||
98 | * |
||
99 | * @var bool |
||
100 | */ |
||
101 | protected $immutable = false; |
||
102 | /** |
||
103 | * Logger |
||
104 | * |
||
105 | * @var LoggerInterface |
||
106 | */ |
||
107 | protected $logger = null; |
||
108 | /** |
||
109 | * Default antibot prefix |
||
110 | * |
||
111 | * @var string |
||
112 | */ |
||
113 | const DEFAULT_PREFIX = 'antibot'; |
||
114 | |||
115 | /** |
||
116 | * Antibot constructor |
||
117 | * |
||
118 | * @param string $unique Session-persistent, unique key |
||
119 | * @param string $prefix Prefix |
||
120 | */ |
||
121 | 19 | public function __construct(string $unique, string $prefix = self::DEFAULT_PREFIX) |
|
127 | |||
128 | /** |
||
129 | * Return the session persistent, unique token |
||
130 | * |
||
131 | * @return string Session persistent, unique token |
||
132 | */ |
||
133 | 5 | public function getUnique(): string |
|
137 | |||
138 | /** |
||
139 | * Return the prefix |
||
140 | * |
||
141 | * @return string Prefix |
||
142 | */ |
||
143 | 2 | public function getPrefix(): string |
|
147 | |||
148 | /** |
||
149 | * Return the submitted Antibot data |
||
150 | * |
||
151 | * @return string[] Antibot data |
||
152 | */ |
||
153 | 5 | public function getData(): ?array |
|
159 | |||
160 | /** |
||
161 | * Return the parameter prefix |
||
162 | * |
||
163 | * @return string Parameter prefix |
||
164 | * @throws RuntimeException If Antibot needs to be initialized |
||
165 | */ |
||
166 | 5 | public function getParameterPrefix(): string |
|
172 | |||
173 | /** |
||
174 | * Add a validator |
||
175 | * |
||
176 | * @param ValidatorInterface $validator Validator |
||
177 | */ |
||
178 | 12 | public function addValidator(ValidatorInterface $validator): void |
|
183 | |||
184 | /** |
||
185 | * Validate a request |
||
186 | * |
||
187 | * @param ServerRequestInterface $request Request |
||
188 | * |
||
189 | * @return ValidationResult Validation result |
||
190 | */ |
||
191 | 11 | public function validate(ServerRequestInterface $request): ValidationResult |
|
230 | |||
231 | /** |
||
232 | * Create and return the raw armor input elements |
||
233 | * |
||
234 | * @param ServerRequestInterface $request Request |
||
235 | * |
||
236 | * @return InputElement[] Armor input elements |
||
237 | */ |
||
238 | 5 | public function armorInputs(ServerRequestInterface $request): array |
|
254 | |||
255 | /** |
||
256 | * Compare and sort validators |
||
257 | * |
||
258 | * @param ValidatorInterface $validator1 Validator 1 |
||
259 | * @param ValidatorInterface $validator2 Validator 2 |
||
260 | * |
||
261 | * @return int Sorting |
||
262 | */ |
||
263 | protected function sortValidators(ValidatorInterface $validator1, ValidatorInterface $validator2): int |
||
273 | |||
274 | /** |
||
275 | * Pre-validation initialization |
||
276 | * |
||
277 | * @param ServerRequestInterface $request Request |
||
278 | */ |
||
279 | 12 | protected function initialize(ServerRequestInterface $request): void |
|
289 | |||
290 | /** |
||
291 | * Calculate the unique signature |
||
292 | * |
||
293 | * @return string Signature |
||
294 | */ |
||
295 | 12 | protected function calculateSignature(): string |
|
301 | |||
302 | /** |
||
303 | * Extract the antibot data from GET and POST parameters |
||
304 | * |
||
305 | * @param ServerRequestInterface $request Request |
||
306 | */ |
||
307 | 12 | protected function extractData(ServerRequestInterface $request): void |
|
315 | |||
316 | /** |
||
317 | * Check whether this Antibot instance is immutable |
||
318 | * |
||
319 | * @throws RuntimeException If the Antibot instance is immutable |
||
320 | */ |
||
321 | 12 | protected function checkImmutable(): void |
|
330 | |||
331 | /** |
||
332 | * Check whether this Antibot instance is already initialized |
||
333 | * |
||
334 | * @throws RuntimeException If the Antibot instance still needs to be initialized |
||
335 | */ |
||
336 | 6 | protected function checkInitialized(): void |
|
346 | |||
347 | /** |
||
348 | * Return the logger |
||
349 | * |
||
350 | * @return LoggerInterface Logger |
||
351 | */ |
||
352 | 6 | public function getLogger(): LoggerInterface |
|
356 | |||
357 | /** |
||
358 | * Sets a logger instance on the object |
||
359 | * |
||
360 | * @param LoggerInterface $logger Logger |
||
361 | * |
||
362 | * @return void |
||
363 | */ |
||
364 | 12 | public function setLogger(LoggerInterface $logger): void |
|
368 | } |
||
369 |