|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bee4\RobotsTxt; |
|
4
|
|
|
|
|
5
|
|
|
use Bee4\RobotsTxt\Exception\DuplicateRuleException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Rules |
|
9
|
|
|
* Represent a collection of Rules |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright Bee4 2015 |
|
12
|
|
|
* @author Stephane HULARD <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class Rules |
|
15
|
|
|
{ |
|
16
|
|
|
const DEFAULT_UA = '*'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The collection of rules |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $collection = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Default rule used if robots.txt is empty |
|
26
|
|
|
* @var Rule |
|
27
|
|
|
*/ |
|
28
|
|
|
private $defaultRule; |
|
29
|
|
|
|
|
30
|
4 |
|
public function __construct() |
|
31
|
|
|
{ |
|
32
|
4 |
|
$this->defaultRule = new Rule(); |
|
33
|
4 |
|
$this->add(self::DEFAULT_UA, $this->defaultRule); |
|
34
|
4 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Add a new rule to the collection |
|
38
|
|
|
* @param string $userAgent |
|
39
|
|
|
* @param Rule $rule |
|
40
|
|
|
* @return Rules |
|
41
|
|
|
*/ |
|
42
|
4 |
|
public function add($userAgent, Rule $rule) |
|
43
|
|
|
{ |
|
44
|
4 |
|
$userAgent = $this->handleUa($userAgent); |
|
45
|
4 |
|
if (isset($this->collection[$userAgent]) && |
|
46
|
4 |
|
$this->collection[$userAgent] !== $this->defaultRule ) { |
|
47
|
|
|
throw (new DuplicateRuleException( |
|
48
|
|
|
'You can\'t add 2 rules for the same UserAgent' |
|
49
|
|
|
)) |
|
50
|
|
|
->setRule($rule) |
|
51
|
|
|
->setUserAgent($userAgent); |
|
52
|
|
|
} |
|
53
|
4 |
|
$this->collection[$userAgent] = $rule; |
|
54
|
|
|
|
|
55
|
4 |
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Check if the URL match for the given UA or not |
|
60
|
|
|
* @param string $userAgent |
|
61
|
|
|
* @param string $url |
|
62
|
|
|
* @return boolean |
|
63
|
|
|
*/ |
|
64
|
|
|
public function match($userAgent, $url) |
|
65
|
|
|
{ |
|
66
|
|
|
if (($rule = $this->get($userAgent)) === null) { |
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
return $rule->match($url); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Retrieve rules for a given UA |
|
74
|
|
|
* @param string $userAgent |
|
75
|
|
|
* @return null|Rule |
|
76
|
|
|
*/ |
|
77
|
2 |
|
public function get($userAgent) |
|
78
|
|
|
{ |
|
79
|
2 |
|
$item = null; |
|
80
|
2 |
|
$iterator = new \ArrayIterator($this->collection); |
|
81
|
2 |
|
iterator_apply( |
|
82
|
|
|
$iterator, |
|
83
|
2 |
|
function (\ArrayIterator $iterator, $userAgent) use (&$item) { |
|
84
|
2 |
|
if ($iterator->key() != Rules::DEFAULT_UA && |
|
85
|
2 |
|
preg_match($iterator->key(), $userAgent) === 1 ) { |
|
86
|
|
|
$item = $iterator->current(); |
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
2 |
|
return true; |
|
90
|
2 |
|
}, |
|
91
|
2 |
|
[$iterator, $userAgent] |
|
92
|
|
|
); |
|
93
|
|
|
|
|
94
|
2 |
|
return $item!==null? |
|
95
|
|
|
$item: |
|
96
|
2 |
|
(isset($this->collection[self::DEFAULT_UA])? |
|
97
|
2 |
|
$this->collection[self::DEFAULT_UA]: |
|
98
|
2 |
|
null); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Update the UA to make a valid regexp |
|
103
|
|
|
* @param string $userAgent |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
4 |
|
private function handleUa($userAgent) |
|
107
|
|
|
{ |
|
108
|
4 |
|
if ($userAgent == self::DEFAULT_UA) { |
|
109
|
4 |
|
return $userAgent; |
|
110
|
|
|
} |
|
111
|
|
|
return '/^'.preg_quote($userAgent).'.*/i'; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|