1 | <?php namespace Arcanedev\SpamBlocker; |
||
14 | class SpamBlocker implements SpamBlockerContract |
||
15 | { |
||
16 | /* ----------------------------------------------------------------- |
||
17 | | Properties |
||
18 | | ----------------------------------------------------------------- |
||
19 | */ |
||
20 | |||
21 | /** |
||
22 | * The spammers source path. |
||
23 | * |
||
24 | * @var string|null |
||
25 | */ |
||
26 | protected $source; |
||
27 | |||
28 | /** |
||
29 | * The included spammers. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $includes = []; |
||
34 | |||
35 | /** |
||
36 | * The excluded spammers. |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $excludes = []; |
||
41 | |||
42 | /** |
||
43 | * The spammers collection. |
||
44 | * |
||
45 | * @var \Arcanedev\SpamBlocker\Entities\SpammerCollection |
||
46 | */ |
||
47 | protected $spammers; |
||
48 | |||
49 | /** |
||
50 | * Cache key. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $cacheKey; |
||
55 | |||
56 | /** |
||
57 | * Cache expiration duration. |
||
58 | * |
||
59 | * @var int |
||
60 | */ |
||
61 | protected $cacheExpires; |
||
62 | |||
63 | /* ----------------------------------------------------------------- |
||
64 | | Constructor |
||
65 | | ----------------------------------------------------------------- |
||
66 | */ |
||
67 | |||
68 | /** |
||
69 | * SpamBlocker constructor. |
||
70 | * |
||
71 | * @param array $config |
||
72 | */ |
||
73 | 81 | public function __construct(array $config) |
|
83 | |||
84 | /* ----------------------------------------------------------------- |
||
85 | | Getters & Setters |
||
86 | | ----------------------------------------------------------------- |
||
87 | */ |
||
88 | |||
89 | /** |
||
90 | * Get the loaded spammers. |
||
91 | * |
||
92 | * @return \Arcanedev\SpamBlocker\Entities\SpammerCollection |
||
93 | */ |
||
94 | 51 | public function spammers() |
|
98 | |||
99 | /** |
||
100 | * Get the spammer source file. |
||
101 | * |
||
102 | * @return string|null |
||
103 | */ |
||
104 | 6 | public function getSource() |
|
108 | |||
109 | /** |
||
110 | * Set the source path. |
||
111 | * |
||
112 | * @param string $source |
||
113 | * |
||
114 | * @return self |
||
115 | */ |
||
116 | 81 | public function setSource($source) |
|
117 | { |
||
118 | 81 | if ( ! empty($source)) { |
|
119 | 81 | $this->source = $source; |
|
120 | } |
||
121 | |||
122 | 81 | return $this; |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Get the included spammers. |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | 12 | public function getIncludes() |
|
134 | |||
135 | /** |
||
136 | * Set the included spammers. |
||
137 | * |
||
138 | * @param array $includes |
||
139 | * |
||
140 | * @return self |
||
141 | */ |
||
142 | 81 | public function setIncludes(array $includes) |
|
148 | |||
149 | /** |
||
150 | * Get the excluded spammers. |
||
151 | * |
||
152 | * @return array |
||
153 | */ |
||
154 | 12 | public function getExcludes() |
|
158 | |||
159 | /** |
||
160 | * Set the excluded spammers. |
||
161 | * |
||
162 | * @param array $excludes |
||
163 | * |
||
164 | * @return self |
||
165 | */ |
||
166 | 81 | public function setExcludes(array $excludes) |
|
172 | |||
173 | /* ----------------------------------------------------------------- |
||
174 | | Main Methods |
||
175 | | ----------------------------------------------------------------- |
||
176 | */ |
||
177 | |||
178 | /** |
||
179 | * Load spammers. |
||
180 | * |
||
181 | * @return self |
||
182 | */ |
||
183 | 81 | public function load() |
|
184 | { |
||
185 | 81 | $this->checkSource(); |
|
186 | |||
187 | 81 | $this->spammers = $this->cacheSpammers(function () { |
|
188 | 81 | return SpammerCollection::load( |
|
189 | 81 | file($this->source, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) |
|
190 | ); |
||
191 | 81 | }); |
|
192 | |||
193 | 81 | return $this; |
|
194 | } |
||
195 | |||
196 | /** |
||
197 | * Allow a host. |
||
198 | * |
||
199 | * @param string $host |
||
200 | * |
||
201 | * @return self |
||
202 | */ |
||
203 | 12 | public function allow($host) |
|
204 | { |
||
205 | 12 | if ( ! empty($host)) { |
|
206 | 12 | $this->excludes[] = $host; |
|
207 | } |
||
208 | |||
209 | 12 | return $this; |
|
210 | } |
||
211 | |||
212 | /** |
||
213 | * Block a host. |
||
214 | * |
||
215 | * @param string $host |
||
216 | * |
||
217 | * @return self |
||
218 | */ |
||
219 | 18 | public function block($host) |
|
220 | { |
||
221 | 18 | if ( ! empty($host)) { |
|
222 | 18 | $this->includes[] = $host; |
|
223 | } |
||
224 | |||
225 | 18 | return $this; |
|
226 | } |
||
227 | |||
228 | /** |
||
229 | * Get all spammers (allowed and blocked ones). |
||
230 | * |
||
231 | * @return \Arcanedev\SpamBlocker\Entities\SpammerCollection |
||
232 | */ |
||
233 | 24 | public function all() |
|
239 | |||
240 | /** |
||
241 | * Check if the given host is allowed. |
||
242 | * |
||
243 | * @param string $host |
||
244 | * |
||
245 | * @return bool |
||
246 | */ |
||
247 | 12 | public function isAllowed($host) |
|
251 | |||
252 | /** |
||
253 | * Check if the given host is blocked. |
||
254 | * |
||
255 | * @param string $host |
||
256 | * |
||
257 | * @return bool |
||
258 | */ |
||
259 | 21 | public function isBlocked($host) |
|
274 | |||
275 | /** |
||
276 | * Get a spammer. |
||
277 | * |
||
278 | * @param string $host |
||
279 | * |
||
280 | * @return \Arcanedev\SpamBlocker\Entities\Spammer|null |
||
281 | */ |
||
282 | 12 | public function getSpammer($host) |
|
286 | |||
287 | /** |
||
288 | * Reset the spammers. |
||
289 | * |
||
290 | * @return self |
||
291 | */ |
||
292 | 6 | public function reset() |
|
299 | |||
300 | /* ----------------------------------------------------------------- |
||
301 | | Other Methods |
||
302 | | ----------------------------------------------------------------- |
||
303 | */ |
||
304 | |||
305 | /** |
||
306 | * Get the full domain. |
||
307 | * |
||
308 | * @param string $host |
||
309 | * |
||
310 | * @return string |
||
311 | */ |
||
312 | 21 | protected function getFullDomain($host) |
|
318 | |||
319 | /** |
||
320 | * Get the root domain. |
||
321 | * |
||
322 | * @param string $domain |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | 21 | private function getRootDomain($domain) |
|
335 | |||
336 | /** |
||
337 | * Reset the cache. |
||
338 | * |
||
339 | * @return self |
||
340 | */ |
||
341 | 6 | protected function resetCache() |
|
347 | |||
348 | /** |
||
349 | * Cache the spammers. |
||
350 | * |
||
351 | * @param \Closure $callback |
||
352 | * |
||
353 | * @return \Arcanedev\SpamBlocker\Entities\SpammerCollection |
||
354 | */ |
||
355 | 81 | private function cacheSpammers(Closure $callback) |
|
359 | |||
360 | /** |
||
361 | * Check the source file. |
||
362 | */ |
||
363 | 81 | private function checkSource() |
|
371 | } |
||
372 |