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