|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nabble\SemaltBlocker; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* The `update` method is called from the Blocker class every week to grab latest domain list from GitHub. |
|
7
|
|
|
*/ |
|
8
|
|
|
class Updater |
|
9
|
|
|
{ |
|
10
|
|
|
public static $ttl = 604800; // = 60 * 60 * 24 * 7 = 7 days |
|
11
|
|
|
public static $updateUrl = 'https://raw.githubusercontent.com/nabble/semalt-blocker/master/domains/blocked'; |
|
12
|
|
|
|
|
13
|
|
|
private static $blocklist = './../../domains/blocked'; |
|
14
|
|
|
|
|
15
|
|
|
////////////////////////////////////////// |
|
16
|
|
|
// PUBLIC API // |
|
17
|
|
|
////////////////////////////////////////// |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Try to update the blocked domains list. |
|
21
|
|
|
* |
|
22
|
|
|
* @param bool $force |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function update($force = false) |
|
25
|
|
|
{ |
|
26
|
|
|
if (!defined('SEMALT_UNIT_TESTING') && !self::isWritable()) { |
|
27
|
|
|
return; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
if (!$force && !self::isOutdated()) { |
|
31
|
|
|
return; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
self::doUpdate(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function getNewDomainList() |
|
41
|
|
|
{ |
|
42
|
|
|
if (function_exists('curl_init')) { |
|
43
|
|
|
$curl = curl_init(); |
|
44
|
|
|
curl_setopt_array($curl, [ |
|
45
|
|
|
CURLOPT_RETURNTRANSFER => 1, |
|
46
|
|
|
CURLOPT_URL => self::$updateUrl, |
|
47
|
|
|
]); |
|
48
|
|
|
$domains = curl_exec($curl); |
|
49
|
|
|
curl_close($curl); |
|
50
|
|
|
} else { |
|
51
|
|
|
$domains = @file_get_contents(self::$updateUrl); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $domains; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function getBlocklistFilename() |
|
61
|
|
|
{ |
|
62
|
|
|
return __DIR__ . DIRECTORY_SEPARATOR . static::$blocklist; |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
////////////////////////////////////////// |
|
66
|
|
|
// PRIVATE FUNCTIONS // |
|
67
|
|
|
////////////////////////////////////////// |
|
68
|
|
|
|
|
69
|
|
|
private static function doUpdate() |
|
70
|
|
|
{ |
|
71
|
|
|
$domains = self::getNewDomainList(); |
|
72
|
|
|
|
|
73
|
|
|
// Don't panic if updating the file throws an error of some kind |
|
74
|
|
|
if (trim($domains) !== '') { |
|
75
|
|
|
@file_put_contents(self::getBlocklistFilename(), $domains); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
private static function isWritable() |
|
83
|
|
|
{ |
|
84
|
|
|
return is_writable(self::getBlocklistFilename()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
|
|
private static function isOutdated() |
|
91
|
|
|
{ |
|
92
|
|
|
return filemtime(self::getBlocklistFilename()) < (time() - self::$ttl); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: