1
|
|
|
<?php |
2
|
|
|
namespace Limonte; |
3
|
|
|
|
4
|
|
|
class AdblockRule |
5
|
|
|
{ |
6
|
|
|
private $rule; |
7
|
|
|
|
8
|
|
|
private $regex; |
9
|
|
|
|
10
|
|
|
private $isComment = false; |
11
|
|
|
|
12
|
|
|
private $isHtml = false; |
13
|
|
|
|
14
|
|
|
private $isException = false; |
15
|
|
|
|
16
|
17 |
|
public function __construct($rule) |
17
|
|
|
{ |
18
|
17 |
|
$this->rule = $rule; |
19
|
|
|
|
20
|
17 |
|
if (Str::startsWith($this->rule, '@@')) { |
21
|
4 |
|
$this->isException = true; |
22
|
4 |
|
$this->rule = mb_substr($this->rule, 2); |
23
|
4 |
|
} |
24
|
|
|
|
25
|
|
|
// comment |
26
|
17 |
|
if (Str::startsWith($rule, '!') || Str::startsWith($rule, '[Adblock')) { |
27
|
5 |
|
$this->isComment = true; |
28
|
|
|
|
29
|
|
|
// HTML rule |
30
|
17 |
|
} elseif (Str::contains($rule, '##') || Str::contains($rule, '#@#')) { |
31
|
2 |
|
$this->isHtml = true; |
32
|
|
|
|
33
|
|
|
// URI rule |
34
|
2 |
|
} else { |
35
|
17 |
|
$this->makeRegex(); |
36
|
|
|
} |
37
|
16 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $url |
41
|
|
|
* |
42
|
|
|
* @return boolean |
43
|
|
|
*/ |
44
|
10 |
|
public function matchUrl($url) |
45
|
|
|
{ |
46
|
10 |
|
return (boolean)preg_match( |
47
|
10 |
|
'/' . $this->getRegex() . '/', |
48
|
|
|
$url |
49
|
10 |
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
15 |
|
public function getRegex() |
56
|
|
|
{ |
57
|
15 |
|
return $this->regex; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return boolean |
62
|
|
|
*/ |
63
|
10 |
|
public function isComment() |
64
|
|
|
{ |
65
|
10 |
|
return $this->isComment; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return boolean |
70
|
|
|
*/ |
71
|
9 |
|
public function isHtml() |
72
|
|
|
{ |
73
|
9 |
|
return $this->isHtml; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return boolean |
78
|
|
|
*/ |
79
|
9 |
|
public function isException() |
80
|
|
|
{ |
81
|
9 |
|
return $this->isException; |
82
|
|
|
} |
83
|
|
|
|
84
|
17 |
|
private function makeRegex() |
85
|
|
|
{ |
86
|
17 |
|
if (empty($this->rule)) { |
87
|
3 |
|
throw new InvalidRuleException("Empty rule"); |
88
|
|
|
} |
89
|
|
|
|
90
|
17 |
|
$regex = $this->rule; |
91
|
|
|
|
92
|
|
|
// Check if the rule isn't already regexp |
93
|
17 |
|
if (Str::startsWith($regex, '/') && Str::endsWith($regex, '/')) { |
94
|
2 |
|
$this->regex = mb_substr($this->rule, 1, mb_strlen($this->rule) - 2); |
95
|
|
|
|
96
|
2 |
|
if (empty($this->regex)) { |
97
|
1 |
|
throw new InvalidRuleException("Empty rule"); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// escape special regex characters |
104
|
15 |
|
$regex = preg_replace("/([\\\.\$\+\?\{\}\(\)\[\]\/])/", "\\\\$1", $this->rule); |
105
|
|
|
|
106
|
|
|
// Separator character ^ matches anything but a letter, a digit, or |
107
|
|
|
// one of the following: _ - . %. The end of the address is also |
108
|
|
|
// accepted as separator. |
109
|
15 |
|
$regex = str_replace("^", "([^\w\d_\-.%]|$)", $regex); |
110
|
|
|
|
111
|
|
|
// * symbol |
112
|
15 |
|
$regex = str_replace("*", ".*", $regex); |
113
|
|
|
|
114
|
|
|
// | in the end means the end of the address |
115
|
15 |
|
if (Str::endsWith($regex, '|')) { |
116
|
2 |
|
$regex = mb_substr($regex, 0, mb_strlen($regex) - 1) . '$'; |
117
|
2 |
|
} |
118
|
|
|
|
119
|
|
|
// || in the beginning means beginning of the domain name |
120
|
15 |
|
if (Str::startsWith($regex, '||')) { |
121
|
5 |
|
if (mb_strlen($regex) > 2) { |
122
|
|
|
// http://tools.ietf.org/html/rfc3986#appendix-B |
123
|
5 |
|
$regex = "^([^:\/?#]+:)?(\/\/([^\/?#]*\.)?)?" . mb_substr($regex, 2); |
124
|
5 |
|
} |
125
|
|
|
// | in the beginning means start of the address |
126
|
15 |
|
} elseif (Str::startsWith($regex, '|')) { |
127
|
4 |
|
$regex = '^' . mb_substr($regex, 1); |
128
|
4 |
|
} |
129
|
|
|
|
130
|
|
|
// other | symbols should be escaped |
131
|
15 |
|
$regex = preg_replace("/\|(?![\$])/", "\|$1", $regex); |
132
|
|
|
|
133
|
15 |
|
$this->regex = $regex; |
134
|
15 |
|
} |
135
|
|
|
} |
136
|
|
|
|