1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Whitelist; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Whitelist\Definition\IDefinition; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Main class for checking values against a whitelist. It provides a method to |
10
|
|
|
* set up the whitelist and a method to match arbitrary string against the |
11
|
|
|
* whitelist. |
12
|
|
|
* |
13
|
|
|
* @author Sam Stenvall <[email protected]> |
14
|
|
|
* @copyright Copyright © Sam Stenvall 2014- |
15
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
16
|
|
|
*/ |
17
|
|
|
class Check |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var IDefinition[] the whitelist definitions |
22
|
|
|
*/ |
23
|
|
|
private $_definitions = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Parses the whitelist definitions into respective objects |
27
|
|
|
* |
28
|
|
|
* @param array $whitelist list of definition strings |
29
|
|
|
* |
30
|
|
|
* @throws \InvalidArgumentException if the definition type couldn't be |
31
|
|
|
* determined |
32
|
|
|
*/ |
33
|
|
|
public function whitelist(array $whitelist) |
34
|
|
|
{ |
35
|
|
|
$this->_definitions = []; |
36
|
|
|
|
37
|
|
|
foreach ($whitelist as $definition) { |
38
|
|
|
// Pre-configured object |
39
|
|
|
if (is_object($definition)) { |
40
|
|
|
if ($definition instanceof Definition\IDefinition) { |
41
|
|
|
$definitionObject = $definition; |
42
|
|
|
} else { |
43
|
|
|
throw new InvalidArgumentException('Definition objects must implement IDefinition'); |
44
|
|
|
} |
45
|
|
|
} // IPv4 address |
46
|
|
|
elseif (preg_match('/[a-z:\/]/', $definition) === 0) { |
47
|
|
|
$definitionObject = new Definition\IPv4Address($definition); |
48
|
|
|
} // IPv4 CIDR notation |
49
|
|
|
elseif (preg_match('/[a-z:]/', $definition) === 0) { |
50
|
|
|
$definitionObject = new Definition\IPv4CIDR($definition); |
51
|
|
|
} // IPv6 address |
52
|
|
|
elseif (preg_match('/^[0-9a-f:]+$/', $definition)) { |
53
|
|
|
$definitionObject = new Definition\IPv6Address($definition); |
54
|
|
|
} // IPv6 CIDR notation |
55
|
|
|
elseif (preg_match('/^[0-9a-f:\/]+$/', $definition)) { |
56
|
|
|
$definitionObject = new Definition\IPv6CIDR($definition); |
57
|
|
|
} // Wildcard domain |
58
|
|
|
elseif (preg_match('/^\*\.[\w\.\-]+$/', $definition)) { |
59
|
|
|
$definitionObject = new Definition\WildcardDomain($definition); |
60
|
|
|
} // Domain |
61
|
|
|
elseif (preg_match('/^[\w\.\-]+$/', $definition)) { |
62
|
|
|
$definitionObject = new Definition\Domain($definition); |
63
|
|
|
} else { |
64
|
|
|
throw new InvalidArgumentException('Unable to parse definition "'.$definition.'"'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->_definitions[] = $definitionObject; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Checks the specified value against all configured definitions and |
73
|
|
|
* returns true if at least one definition considers it a match |
74
|
|
|
* |
75
|
|
|
* @param string $value the value to be checked |
76
|
|
|
* |
77
|
|
|
* @return boolean |
78
|
|
|
*/ |
79
|
|
|
public function check($value) |
80
|
|
|
{ |
81
|
|
|
foreach ($this->_definitions as $definition) { |
82
|
|
|
if ($definition->match($value)) { |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|