-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcron.php
More file actions
80 lines (69 loc) · 1.92 KB
/
cron.php
File metadata and controls
80 lines (69 loc) · 1.92 KB
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
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use dnsomatic\Factory;
// DNS-O-Matic Username
define('DNSOMATIC_USER', '');
// DNS-O-Matic Password
define('DNSOMATIC_PASS', '');
// DNS-O-Matic Hostname
define('DNSOMATIC_HOST', '');
// Output messages to stdout
define('VERBOSE', true);
// IP Address local cache
define('IP_CACHE', __DIR__ . '/ip.cache');
// Log file
define('LOG_FILE', __DIR__ . '/dnsomatic.log');
try {
if (VERBOSE) {
echo 'Starting..', PHP_EOL;
}
$checker = Factory::createChecker();
// Resolves current IP Address
$ipAddr = $checker->exec();
if (VERBOSE) {
echo 'Resolved IP Address: ', $ipAddr, PHP_EOL;
}
// Retrieves last IP Address from local cache
$cached = '';
if (is_file(IP_CACHE)) {
$cached = trim(file_get_contents(IP_CACHE));
if (VERBOSE) {
echo 'Cached IP Address: ', $cached, PHP_EOL;
}
}
// If resolved IP Address and cached IP Address don't match
if ($ipAddr !== $cached) {
if (VERBOSE) {
echo 'Updating DNS-O-Matic..', PHP_EOL;
}
// Update DNS-O-Matic with current IP Address
$updater = Factory::createUpdater(DNSOMATIC_USER, DNSOMATIC_PASS);
$updater
->setHostname(DNSOMATIC_HOST)
->setMyip($ipAddr);
$updater->exec();
// Stores current IP Address in local cache
file_put_contents(IP_CACHE, $ipAddr, LOCK_EX);
}
} catch (\Exception $exception) {
// Exceptions are sent to the log file
$logLine = sprintf(
'[%s] %s (%s:%d)',
date('d/m/Y H:i:s'),
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
);
if (VERBOSE) {
echo $logLine, PHP_EOL;
}
file_put_contents(
LOG_FILE,
$logLine . PHP_EOL,
FILE_APPEND | LOCK_EX
);
} finally {
if (VERBOSE) {
echo 'Done', PHP_EOL;
}
}