-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.php
executable file
·91 lines (74 loc) · 2.5 KB
/
run.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
#!/usr/bin/env php
<?php
require_once(__DIR__ . '/vendor/autoload.php');
require_once(__DIR__ . '/bind.php');
require_once(__DIR__ . '/config.php');
$api = new MyDNSHostAPI($config['api']);
$api->setAuthUserKey($config['user'], $config['apikey']);
$api->domainAdmin($config['isAdmin']);
$domains = $api->getDomains();
$errors = [];
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($config['zones'], RecursiveDirectoryIterator::SKIP_DOTS));
foreach ($it as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) == "db" || pathinfo($file, PATHINFO_EXTENSION) == "zone") {
$domain = pathinfo($file, PATHINFO_FILENAME);
if (!isset($domains[$domain])) {
echo 'Creating Domain: ', $domain;
$result = $config['isAdmin'] ? $api->createDomain($domain, $config['newOwner']) : $api->createDomain($domain);
if (isset($result['error'])) {
$errors[$domain] = 'Unable to create: ' . $result['error'];
if (isset($result['errorData'])) {
$errors[$domain] .= ' :: ' . $result['errorData'];
}
echo ' - Error!', "\n";
continue;
} else {
echo ' - Success!', "\n";
}
}
echo 'Importing Domain: ', $domain;
$bind = new Bind($domain, '', $file);
$bind->parseZoneFile();
$changed = false;
// Check if NAMESERVERS need changing.
$domainInfo = $bind->getDomainInfo();
$oldNS = [];
foreach ($domainInfo['NS'][''] as $r) {
$oldNS[] = $r['Address'];
}
if (count(array_diff($oldNS, $config['nameservers'])) > 0 || count(array_diff($config['nameservers'], $oldNS)) > 0) {
$changed = true;
$bind->unsetRecord('', 'NS');
foreach ($config['nameservers'] as $ns) {
$bind->setRecord('', 'NS', $ns);
}
}
$soa = $bind->getSOA();
if ($soa['Nameserver'] != $config['nameservers'][0]) {
$soa['Nameserver'] = $config['nameservers'][0];
$changed = true;
}
if ($changed) {
$soa['Serial'] = $bind->getNextSerial();
$bind->setSOA($soa);
echo ' (Zone has been changed.)';
}
$zonedata = implode("\n", $bind->getParsedZoneFile());
$result = $api->importZone($domain, $zonedata);
if (isset($result['error'])) {
$errors[$domain] = 'Unable to import: ' . $result['error'];
if (isset($result['errorData'])) {
$errors[$domain] .= ' :: ' . $result['errorData'];
}
echo ' - Error!', "\n";
continue;
} else {
echo ' - Success!', "\n";
}
}
}
echo 'Done.', "\n";
if (count($errors) > 0) {
echo 'There was errors with the following domains: ', "\n";
print_r($errors);
}