Test Failed
Pull Request — master (#7)
by
unknown
08:47
created

AdblockParser::initRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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