-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDDNSProvider.class.php
124 lines (101 loc) · 2.82 KB
/
DDNSProvider.class.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/** Prevent direct access to this file */
if (defined('APPLICATION') === false) {
die('Direct access not permitted!');
}
/** Require dependencies */
require_once 'FroxlorAPI.class.php';
class DDNSProvider
{
/** @var Config */
private $config;
/** @var FroxlorAPI */
private $api;
/** @var string */
private $domainName;
/** @var int */
private $domainTtl;
/**
* Creates a new instance of this class.
* @param Config $config Configuration to use.
* @return void
*/
public function __construct(Config $config)
{
$this->config = $config;
$this->api = new FroxlorAPI(
$this->config->get('froxlor.endpoint'),
$this->config->get('froxlor.api_key'),
$this->config->get('froxlor.api_secret')
);
}
public function use(string $user, string $token, string $domain): bool
{
$users = array_keys($this->config->get('users'));
/** Check if the user exists. */
if (in_array($user, $users) === false) {
return false;
}
/** Read user properties */
$userToken = $this->config->get("users.${user}.token");
$userDomains = $this->config->get("users.${user}.domains");
/** Verify token */
if (strcmp($userToken, $token) !== 0) {
return false;
}
/** Verify domain */
if (in_array($domain, array_keys($userDomains)) === false) {
return false;
}
$this->domainName = $domain;
$this->domainTtl = $userDomains[$domain];
return true;
}
public function updateIp(string $host, string $ip)
{
if (isset($this->domainName) === false) {
die('ERROR: Make sure to select a zone before trying to update the IP address.');
}
/** Get current entry IDs and delete them */
$entryIds = $this->getCurrentEntry($host);
foreach ($entryIds as $id) {
$this->api->request('DomainZones.delete', ['entry_id' => $id, 'domainname' => $this->domainName]);
}
/** Create the new record with the current ID */
$this->api->request('DomainZones.add', [
'domainname' => $this->domainName,
'record' => $host,
'type' => 'A',
'content' => $ip,
'ttl' => $this->domainTtl
]);
}
/**
* Finds all A records for the current host and returns their IDs.
* @param string $host Host to check for.
* @return array Array of all IDs.
*/
private function getCurrentEntry(string $host): array
{
$this->api->request('DomainZones.listing', ['domainname' => $this->domainName]);
/** Check if an error occoured */
if (empty($this->api->getLastError()) === false) {
return [];
}
$response = $this->api->getLastResponse();
/** If no entry exists, simply skip as there is nothing to query for */
$count = $response['count'];
if ($count === 0) {
return [];
}
$entries = $response['list'];
$ids = [];
foreach ($entries as $entry) {
if (strcmp($host, $entry['record']) !== 0 || strcmp('A', $entry['type']) !== 0) {
continue;
}
$ids[] = $entry['id'];
}
return $ids;
}
}