1 | <?php |
||
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 = []) |
|
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() |
|
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) |
|
107 | |||
108 | /** |
||
109 | * Get cache folder |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | 1 | public function getCacheFolder() |
|
117 | |||
118 | /** |
||
119 | * Set cache folder |
||
120 | * |
||
121 | * @param string $cacheFolder |
||
122 | */ |
||
123 | 1 | public function setCacheFolder($cacheFolder) |
|
127 | |||
128 | /** |
||
129 | * Get cache expire (in days) |
||
130 | * |
||
131 | * @return integer |
||
132 | */ |
||
133 | 1 | public function getCacheExpire() |
|
137 | |||
138 | /** |
||
139 | * Set cache expire (in days) |
||
140 | * |
||
141 | * @param integer $expireInDays |
||
142 | */ |
||
143 | 1 | public function setCacheExpire($expireInDays) |
|
147 | |||
148 | /** |
||
149 | * Clear external resources cache |
||
150 | */ |
||
151 | 1 | public function clearCache() |
|
159 | |||
160 | /** |
||
161 | * @param string $url |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | 2 | private function getCachedResource($url) |
|
186 | } |
||
187 |