1
|
|
|
<?php |
2
|
|
|
namespace Limonte; |
3
|
|
|
|
4
|
|
|
class AdblockParser |
5
|
|
|
{ |
6
|
|
|
private $rules; |
7
|
|
|
|
8
|
|
|
private $cacheFolder; |
9
|
|
|
|
10
|
|
|
private $cacheExpire = 1; // 1 day |
11
|
|
|
|
12
|
10 |
|
public function __construct($rules = []) |
13
|
|
|
{ |
14
|
10 |
|
$this->rules = []; |
15
|
10 |
|
$this->addRules($rules); |
16
|
10 |
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param string[] $rules |
20
|
|
|
*/ |
21
|
10 |
|
public function addRules($rules) |
22
|
|
|
{ |
23
|
10 |
|
foreach ($rules as $rule) { |
24
|
|
|
try { |
25
|
9 |
|
$this->rules[] = new AdblockRule($rule); |
26
|
9 |
|
} catch (InvalidRuleException $e) { |
27
|
|
|
// Skip invalid rules |
28
|
|
|
} |
29
|
10 |
|
} |
30
|
|
|
|
31
|
|
|
// Sort rules, eceptions first |
32
|
10 |
|
usort($this->rules, function ($a, $b) { |
33
|
4 |
|
return (int)$a->isException() < (int)$b->isException(); |
34
|
10 |
|
}); |
35
|
10 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string|array $path |
39
|
|
|
*/ |
40
|
3 |
|
public function loadRules($path) |
41
|
|
|
{ |
42
|
|
|
// single resource |
43
|
3 |
|
if (is_string($path)) { |
44
|
3 |
|
if (filter_var($path, FILTER_VALIDATE_URL)) { |
45
|
2 |
|
$content = $this->getCachedResource($path); |
46
|
2 |
|
} else { |
47
|
1 |
|
$content = @file_get_contents($path); |
48
|
|
|
} |
49
|
3 |
|
if ($content) { |
50
|
3 |
|
$rules = preg_split("/(\r\n|\n|\r)/", $content); |
51
|
3 |
|
$this->addRules($rules); |
52
|
3 |
|
} |
53
|
|
|
// array of resources |
54
|
3 |
|
} elseif (is_array($path)) { |
55
|
2 |
|
foreach ($path as $item) { |
56
|
2 |
|
$this->loadRules($item); |
57
|
2 |
|
} |
58
|
2 |
|
} |
59
|
3 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
1 |
|
public function getRules() |
65
|
|
|
{ |
66
|
1 |
|
return $this->rules; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $url |
71
|
|
|
* |
72
|
|
|
* @return integer |
73
|
|
|
*/ |
74
|
10 |
|
public function shouldBlock($url) |
75
|
|
|
{ |
76
|
10 |
|
$url = trim($url); |
77
|
|
|
|
78
|
10 |
|
if (filter_var($url, FILTER_VALIDATE_URL) === false) { |
79
|
1 |
|
throw new \Exception("Invalid URL"); |
80
|
|
|
} |
81
|
|
|
|
82
|
9 |
|
foreach ($this->rules as $rule) { |
83
|
9 |
|
if ($rule->isComment() || $rule->isHtml()) { |
84
|
3 |
|
continue; |
85
|
|
|
} |
86
|
|
|
|
87
|
9 |
|
if ($rule->matchUrl($url)) { |
88
|
9 |
|
if ($rule->isException()) { |
89
|
2 |
|
return false; |
90
|
|
|
} |
91
|
9 |
|
return true; |
92
|
|
|
} |
93
|
9 |
|
} |
94
|
|
|
|
95
|
6 |
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $url |
100
|
|
|
* |
101
|
|
|
* @return boolean |
102
|
|
|
*/ |
103
|
7 |
|
public function shouldNotBlock($url) |
104
|
|
|
{ |
105
|
7 |
|
return !$this->shouldBlock($url); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get cache folder |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
1 |
|
public function getCacheFolder() |
114
|
|
|
{ |
115
|
1 |
|
return $this->cacheFolder; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set cache folder |
120
|
|
|
* |
121
|
|
|
* @param string $cacheFolder |
122
|
|
|
*/ |
123
|
1 |
|
public function setCacheFolder($cacheFolder) |
124
|
|
|
{ |
125
|
1 |
|
$this->cacheFolder = rtrim($cacheFolder, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
126
|
1 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get cache expire (in days) |
130
|
|
|
* |
131
|
|
|
* @return integer |
132
|
|
|
*/ |
133
|
1 |
|
public function getCacheExpire() |
134
|
|
|
{ |
135
|
1 |
|
return $this->cacheExpire; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Set cache expire (in days) |
140
|
|
|
* |
141
|
|
|
* @param integer $expireInDays |
142
|
|
|
*/ |
143
|
1 |
|
public function setCacheExpire($expireInDays) |
144
|
|
|
{ |
145
|
1 |
|
$this->cacheExpire = $expireInDays; |
146
|
1 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Clear external resources cache |
150
|
|
|
*/ |
151
|
1 |
|
public function clearCache() |
152
|
|
|
{ |
153
|
1 |
|
if ($this->cacheFolder) { |
154
|
1 |
|
foreach (glob($this->cacheFolder . '*') as $file) { |
155
|
1 |
|
unlink($file); |
156
|
1 |
|
} |
157
|
1 |
|
} |
158
|
1 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param string $url |
162
|
|
|
* |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
2 |
|
private function getCachedResource($url) |
166
|
|
|
{ |
167
|
2 |
|
if (!$this->cacheFolder) { |
168
|
1 |
|
return @file_get_contents($url); |
169
|
|
|
} |
170
|
|
|
|
171
|
1 |
|
$cacheFile = $this->cacheFolder . basename($url) . md5($url); |
172
|
|
|
|
173
|
1 |
|
if (file_exists($cacheFile) && (filemtime($cacheFile) > (time() - 60 * 24 * $this->cacheExpire))) { |
174
|
|
|
// Cache file is less than five minutes old. |
175
|
|
|
// Don't bother refreshing, just use the file as-is. |
176
|
1 |
|
$content = @file_get_contents($cacheFile); |
177
|
1 |
|
} else { |
178
|
|
|
// Our cache is out-of-date, so load the data from our remote server, |
179
|
|
|
// and also save it over our cache for next time. |
180
|
1 |
|
$content = @file_get_contents($url); |
181
|
1 |
|
file_put_contents($cacheFile, $content, LOCK_EX); |
182
|
|
|
} |
183
|
|
|
|
184
|
1 |
|
return $content; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|