1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* antibot |
5
|
|
|
* |
6
|
|
|
* @category Jkphl |
7
|
|
|
* @package Jkphl\Antibot |
8
|
|
|
* @subpackage Jkphl\Antibot\Ports\Validators |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2018 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2018 Joschi Kuphal <[email protected]> |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Jkphl\Antibot\Ports\Validators; |
38
|
|
|
|
39
|
|
|
use Jkphl\Antibot\Domain\Antibot; |
40
|
|
|
use Jkphl\Antibot\Domain\Exceptions\BlacklistValidationException; |
41
|
|
|
use Jkphl\Antibot\Infrastructure\Model\AbstractLookupValidator; |
42
|
|
|
use Jkphl\Antibot\Ports\Contract\LookupStrategyInterface; |
43
|
|
|
use Jkphl\Antibot\Ports\Exceptions\InvalidArgumentException; |
44
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Parameter Blacklist Validator |
48
|
|
|
* |
49
|
|
|
* @package Jkphl\Antibot |
50
|
|
|
* @subpackage Jkphl\Antibot\Ports\Validators |
51
|
|
|
*/ |
52
|
|
|
class ParameterBlacklistValidator extends AbstractLookupValidator |
53
|
|
|
{ |
54
|
|
|
/** |
55
|
|
|
* Parameter name |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
protected $parameter; |
60
|
|
|
/** |
61
|
|
|
* Parameter type |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
protected $type; |
66
|
|
|
/** |
67
|
|
|
* Use $_GET parameter |
68
|
|
|
* |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
const GET = 'GET'; |
72
|
|
|
/** |
73
|
|
|
* Use $_POST parameter |
74
|
|
|
* |
75
|
|
|
* @var string |
76
|
|
|
*/ |
77
|
|
|
const POST = 'POST'; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Constructor |
81
|
|
|
* |
82
|
|
|
* @param LookupStrategyInterface $strategy Lookup strategy |
83
|
|
|
* @param string $parameter Parameter name |
84
|
|
|
* @param string $type Parameter type |
85
|
|
|
*/ |
86
|
|
|
public function __construct(LookupStrategyInterface $strategy, string $parameter, string $type = self::GET) |
87
|
|
|
{ |
88
|
|
|
parent::__construct($strategy); |
89
|
|
|
$this->parameter = $parameter; |
90
|
|
|
$this->type = $type; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Validate a request |
95
|
|
|
* |
96
|
|
|
* @param ServerRequestInterface $request Request |
97
|
|
|
* @param Antibot $antibot Antibot instance |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
* @throws InvalidArgumentException If the parameter name is invalid |
101
|
|
|
*/ |
102
|
|
|
public function validate(ServerRequestInterface $request, Antibot $antibot): bool |
103
|
|
|
{ |
104
|
|
|
// If the parameter name is invalid |
105
|
|
|
if (empty($this->parameter)) { |
106
|
|
|
throw new InvalidArgumentException( |
107
|
|
|
InvalidArgumentException::INVALID_PARAMETER_NAME_STR, |
108
|
|
|
InvalidArgumentException::INVALID_PARAMETER_NAME |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$params = $this->getParams($request); |
113
|
|
|
|
114
|
|
|
// If the parameter is present |
115
|
|
|
if (array_key_exists($this->parameter, $params) |
116
|
|
|
&& $this->strategy->lookup($params[$this->parameter])) { |
117
|
|
|
throw new BlacklistValidationException($this->parameter); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Return the parameter stack to use |
125
|
|
|
* |
126
|
|
|
* @param ServerRequestInterface $request |
127
|
|
|
* |
128
|
|
|
* @return array Parameters |
129
|
|
|
* @throws InvalidArgumentException If the parameter type is invalid |
130
|
|
|
*/ |
131
|
|
|
protected function getParams(ServerRequestInterface $request): array |
132
|
|
|
{ |
133
|
|
|
switch ($this->type) { |
134
|
|
|
case static::GET: |
135
|
|
|
$params = $request->getQueryParams(); |
136
|
|
|
break; |
137
|
|
|
case static::POST: |
138
|
|
|
$params = $request->getParsedBody(); |
139
|
|
|
break; |
140
|
|
|
default: |
141
|
|
|
throw new InvalidArgumentException( |
142
|
|
|
sprintf(InvalidArgumentException::INVALID_PARAMETER_TYPE_STR, $this->type), |
143
|
|
|
InvalidArgumentException::INVALID_PARAMETER_TYPE |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $params; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|