1 | <?php namespace Arcanedev\SpamBlocker; |
||
14 | class SpamBlocker implements SpamBlockerContract |
||
15 | { |
||
16 | /* ------------------------------------------------------------------------------------------------ |
||
17 | | Properties |
||
18 | | ------------------------------------------------------------------------------------------------ |
||
19 | */ |
||
20 | /** |
||
21 | * The spammers source path. |
||
22 | * |
||
23 | * @var string|null |
||
24 | */ |
||
25 | protected $source; |
||
26 | |||
27 | /** |
||
28 | * The included spammers. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $includes = []; |
||
33 | |||
34 | /** |
||
35 | * The excluded spammers. |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $excludes = []; |
||
40 | |||
41 | /** |
||
42 | * The spammers collection. |
||
43 | * |
||
44 | * @var \Arcanedev\SpamBlocker\Entities\Spammers |
||
45 | */ |
||
46 | protected $spammers; |
||
47 | |||
48 | /** |
||
49 | * Cache key. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $cacheKey; |
||
54 | |||
55 | /** |
||
56 | * Cache expiration duration. |
||
57 | * |
||
58 | * @var int |
||
59 | */ |
||
60 | protected $cacheExpires; |
||
61 | |||
62 | /* ------------------------------------------------------------------------------------------------ |
||
63 | | Constructor |
||
64 | | ------------------------------------------------------------------------------------------------ |
||
65 | */ |
||
66 | /** |
||
67 | * SpamBlocker constructor. |
||
68 | * |
||
69 | * @param array $config |
||
70 | */ |
||
71 | 252 | public function __construct(array $config) |
|
81 | |||
82 | /* ------------------------------------------------------------------------------------------------ |
||
83 | | Getters & Setters |
||
84 | | ------------------------------------------------------------------------------------------------ |
||
85 | */ |
||
86 | /** |
||
87 | * Get the loaded spammers. |
||
88 | * |
||
89 | * @return \Arcanedev\SpamBlocker\Entities\Spammers |
||
90 | */ |
||
91 | 162 | public function spammers() |
|
95 | |||
96 | /** |
||
97 | * Get the spammer source file. |
||
98 | * |
||
99 | * @return string|null |
||
100 | */ |
||
101 | 18 | public function getSource() |
|
105 | |||
106 | /** |
||
107 | * Set the source path. |
||
108 | * |
||
109 | * @param string $source |
||
110 | * |
||
111 | * @return self |
||
112 | */ |
||
113 | 252 | public function setSource($source) |
|
121 | |||
122 | /** |
||
123 | * Get the included spammers. |
||
124 | * |
||
125 | * @return array |
||
126 | */ |
||
127 | 36 | public function getIncludes() |
|
131 | |||
132 | /** |
||
133 | * Set the included spammers. |
||
134 | * |
||
135 | * @param array $includes |
||
136 | * |
||
137 | * @return self |
||
138 | */ |
||
139 | 252 | public function setIncludes(array $includes) |
|
145 | |||
146 | /** |
||
147 | * Get the excluded spammers. |
||
148 | * |
||
149 | * @return array |
||
150 | */ |
||
151 | 36 | public function getExcludes() |
|
155 | |||
156 | /** |
||
157 | * Set the excluded spammers. |
||
158 | * |
||
159 | * @param array $excludes |
||
160 | * |
||
161 | * @return self |
||
162 | */ |
||
163 | 252 | public function setExcludes(array $excludes) |
|
169 | |||
170 | /* ------------------------------------------------------------------------------------------------ |
||
171 | | Main Functions |
||
172 | | ------------------------------------------------------------------------------------------------ |
||
173 | */ |
||
174 | /** |
||
175 | * Load spammers. |
||
176 | * |
||
177 | * @return self |
||
178 | */ |
||
179 | 252 | public function load() |
|
191 | |||
192 | /** |
||
193 | * Allow a host. |
||
194 | * |
||
195 | * @param string $host |
||
196 | * |
||
197 | * @return self |
||
198 | */ |
||
199 | 36 | public function allow($host) |
|
207 | |||
208 | /** |
||
209 | * Block a host. |
||
210 | * |
||
211 | * @param string $host |
||
212 | * |
||
213 | * @return self |
||
214 | */ |
||
215 | 54 | public function block($host) |
|
223 | |||
224 | /** |
||
225 | * Get all spammers (allowed and blocked one). |
||
226 | * |
||
227 | * @return \Arcanedev\SpamBlocker\Entities\Spammers |
||
228 | */ |
||
229 | 72 | public function all() |
|
235 | |||
236 | /** |
||
237 | * Check if the given host is allowed. |
||
238 | * |
||
239 | * @param string $host |
||
240 | * |
||
241 | * @return bool |
||
242 | */ |
||
243 | 36 | public function isAllowed($host) |
|
247 | |||
248 | /** |
||
249 | * Check if the given host is blocked. |
||
250 | * |
||
251 | * @param string $host |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | 72 | public function isBlocked($host) |
|
270 | |||
271 | /** |
||
272 | * Get a spammer. |
||
273 | * |
||
274 | * @param string $host |
||
275 | * |
||
276 | * @return \Arcanedev\SpamBlocker\Entities\Spammer|null |
||
277 | */ |
||
278 | 36 | public function getSpammer($host) |
|
282 | |||
283 | /** |
||
284 | * Reset the spammers. |
||
285 | * |
||
286 | * @return self |
||
287 | */ |
||
288 | 18 | public function reset() |
|
295 | |||
296 | /* ------------------------------------------------------------------------------------------------ |
||
297 | | Other Functions |
||
298 | | ------------------------------------------------------------------------------------------------ |
||
299 | */ |
||
300 | /** |
||
301 | * Get the full domain. |
||
302 | * |
||
303 | * @param string $host |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | 72 | protected function getFullDomain($host) |
|
313 | |||
314 | /** |
||
315 | * Get the root domain. |
||
316 | * |
||
317 | * @param string $domain |
||
318 | * |
||
319 | * @return string |
||
320 | */ |
||
321 | 72 | private function getRootDomain($domain) |
|
330 | |||
331 | /** |
||
332 | * Reset the cache. |
||
333 | * |
||
334 | * @return self |
||
335 | */ |
||
336 | 18 | protected function resetCache() |
|
342 | |||
343 | /** |
||
344 | * Cache the spammers. |
||
345 | * |
||
346 | * @param \Closure $callback |
||
347 | * |
||
348 | * @return \Arcanedev\SpamBlocker\Entities\Spammers |
||
349 | */ |
||
350 | 252 | private function cacheSpammers(\Closure $callback) |
|
354 | |||
355 | /** |
||
356 | * Check the source file. |
||
357 | */ |
||
358 | 252 | private function checkSource() |
|
366 | } |
||
367 |