forked from blchinezu/rolist-hosts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.php
109 lines (91 loc) · 2.61 KB
/
update.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
class Updater {
/**
* Where to store the Adblock file
*
* @var string
*/
private $adblockFile = 'adblock-list.txt';
/**
* From where to get the Adblock file
*
* @var string
*/
private $adblockUrl = 'https://www.zoso.ro/pages/rolist.txt';
/**
* Where to store the hosts file
*
* @var string
*/
private $hostsFile = 'hosts';
/**
* Do it
*/
public function __construct() {
$this->log('Get Adblock file from online');
$onlineContents = trim(file_get_contents($this->adblockUrl));
if (empty($onlineContents)) {
$this->log('Adblock source is empty!');
return;
}
$this->log('Load old adblock file');
$offlineContents = $this->loadFile($this->adblockFile);
if ($onlineContents != $offlineContents) {
$this->log('Load old hosts');
$oldHosts = $this->loadFile($this->hostsFile);
$this->log('Generate new hosts');
$newHosts = $this->generateHosts($onlineContents);
if ($oldHosts != $newHosts) {
$this->log('Write new hosts');
file_put_contents($this->hostsFile, $newHosts);
file_put_contents($this->adblockFile, $onlineContents); # should move it upper
$this->log('Done');
}
} else {
$this->log('Adblock source is unchanged.');
return;
}
}
/**
* Generate hosts file content from an Adblock source
*
* @param string $source Source contents
*
* @return string Hosts contents
*/
private function generateHosts($source) {
$hosts = array();
// Get only domains
preg_match_all('/\n\|\|(.*)\^/', $source, $matches);
// Keep unique values
$matches = array_unique($matches[1]);
// Sort
asort($matches);
// Build
return '0.0.0.0 ' . implode("\n0.0.0.0 ", $matches);
}
/**
* Return the contents of a file or an empty string if it doesn't exist
*
* @param string $path File path
*
* @return string File contents
*/
private function loadFile($path) {
if (file_exists($path)) {
$contents = trim(file_get_contents($path));
} else {
$contents = '';
}
return $contents;
}
/**
* Show a log message
*
* @param string $message Message
*/
private function log($message) {
echo "[" . date('Y-m-d H:i:s') . '] ' . $message . "\n";
}
}
new Updater;