-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron.php
65 lines (60 loc) · 2.64 KB
/
cron.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
<?php declare(strict_types=1);
use Masterminds\HTML5;
function myip() : string {
$dom = (new HTML5())->loadHTMLFile('https://www.whatismyip.org/');
$ipElement = new DOMXPath($dom);
$response = $ipElement->query('//*[@class="myfullip"]');
if ($response->count() === 0) {
exit('can not determine ip (h2 not found)');
}
$ipText = $response->item(0)->textContent;
if (preg_match('/((\d+\.){3}(\d+))/', $ipText, $matches) === false) {
exit('can not determine ip');
}
return $matches[1];
}
require 'vendor/autoload.php';
(require 'config.php')(function(string $directadmin_url, string $username, string $password, array $hosts) {
$daDNSControlAPI = function(string $domain, $url) use ($directadmin_url, $username, $password) : string {
static $cache = [];
if (array_key_exists($domain, $cache) === false) {
$cache[$domain] = [];
} elseif (array_key_exists($url, $cache[$domain])) {
return $cache[$domain][$url];
}
return $cache[$domain][$url] = file_get_contents($directadmin_url . "/CMD_API_DNS_CONTROL?domain=" . $domain . $url, false, stream_context_create([
"http" => [
"header" => "Authorization: Basic " . base64_encode($username . ':' . $password)
]
]));
};
$createDomainDNSAPI = function(string $domain) use ($daDNSControlAPI) : callable {
$cmdApiDNSChange = function(string $url) use ($daDNSControlAPI, $domain) : bool {
return stripos($daDNSControlAPI($domain, $url), 'error=0') !== false;
};
return function(string $host, string $myip) use ($cmdApiDNSChange) : bool {
if ($cmdApiDNSChange("&action=select&arecs0=" . urlencode("name=" . $host . "&value=" . gethostbyname($host))) === false) {
return false;
}
if ($cmdApiDNSChange("&action=add&type=A&name=" . $host . "&value=" . $myip) === false) {
return false;
}
return true;
};
};
$myip = myip();
echo PHP_EOL . 'MYIP ' . $myip;
foreach ($hosts as $domain => $dnsHosts) {
echo PHP_EOL . 'DOMAIN: ' . $domain;
$domainDNSAPI = $createDomainDNSAPI($domain);
foreach ($dnsHosts as $host) {
if (preg_match('/^' . preg_quote($host, '/') . '.*' . preg_quote($myip, '/') . '$/m', $daDNSControlAPI($domain, '')) === 1) {
echo PHP_EOL . 'U2D: ' . $host;
} elseif ($domainDNSAPI($host, $myip) === false) {
echo PHP_EOL . 'ERR: ' . $host;
}
}
print(PHP_EOL . 'updated');
}
exit(PHP_EOL . 'done' . PHP_EOL);
});