-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync-remote-cfg.php
54 lines (47 loc) · 1.46 KB
/
sync-remote-cfg.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
#!/usr/bin/php
<?php
$id = gethostname();
$id = substr($id, strpos($id, "-") + 1);
$ch = curl_init("http://config.logbox.cloud/remote/cfg.php?id=$id");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
$json = json_decode($result);
$cfgPath = __DIR__ . "/www/logbox/cfg.dat";
$remoteNeedsUpdate = false;
$sourceNeedsUpdate = false;
if(isset($json->error)) {
$remoteNeedsUpdate = true;
}
if(file_exists($cfgPath)) {
echo "Source file exists." . PHP_EOL;
$mtime = filemtime($cfgPath);
if($json->mtime > $mtime + 5) {
echo "Server has been modified." . PHP_EOL;
$sourceNeedsUpdate = true;
}
elseif($json->mtime < $mtime - 240) {
echo "Source has been modified." . PHP_EOL;
$remoteNeedsUpdate = true;
}
}
else {
echo "Source file does not exist." . PHP_EOL;
$sourceNeedsUpdate = true;
}
if($remoteNeedsUpdate) {
echo "Updating remote..." . PHP_EOL;
curl_setopt($ch, CURLOPT_POSTFIELDS, [
"cfg-$id" => new CURLFile($cfgPath, "text/plain", "cfg-$id"),
]);
echo curl_exec($ch);
}
if($sourceNeedsUpdate) {
echo "Updating source..." . PHP_EOL;
copy($cfgPath, $cfgPath . ".old");
file_put_contents($cfgPath, $json->serialized);
}
if(!$remoteNeedsUpdate
&& !$sourceNeedsUpdate) {
echo "Everything up to date." . PHP_EOL;
}