1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Affenrakete;
|
4
|
|
|
|
5
|
|
|
/**
|
6
|
|
|
* INWX DDNS Manager
|
7
|
|
|
*
|
8
|
|
|
* @author Peter Siemer <[email protected]>
|
9
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 GNU Public License
|
10
|
|
|
* @link https://affenrakete.de
|
11
|
|
|
*
|
12
|
|
|
*/
|
13
|
|
|
class DDNS {
|
14
|
|
|
|
15
|
|
|
protected $iniFilePath = "./conf/";
|
16
|
|
|
protected $iniFileInwx = "inwx.ini";
|
17
|
|
|
protected $iniFileDomain = "";
|
18
|
|
|
protected $inwx = []; // {apiurl, username, password}
|
|
|
|
|
19
|
|
|
protected $domain = []; // [inwx] => {domain, subdomain}, [ddns] => {apikey}
|
|
|
|
|
20
|
|
|
protected $IP4 = []; // {oldip, newip, id}
|
|
|
|
|
21
|
|
|
protected $IP6 = []; // {oldip, newip, id}
|
|
|
|
|
22
|
|
|
protected $domrobot;
|
23
|
|
|
|
24
|
|
|
public function __construct($apidomain = null, $apikey = null) {
|
25
|
|
|
$this->setIniFileDomain($apidomain);
|
26
|
|
|
$this->readIni();
|
27
|
|
|
$this->checkAccess($apikey);
|
28
|
|
|
}
|
29
|
|
|
|
30
|
|
View Code Duplication |
protected function setIniFileDomain($apidomain = null) {
|
|
|
|
|
31
|
|
|
$domainReturn = [];
|
32
|
|
|
|
33
|
|
|
preg_match("/(?!.{253})((?!-)[A-Za-z0-9-]{1,63}(?<!-)\.){1,126}+[A-Za-z]{2,6}/", $apidomain, $domainReturn);
|
34
|
|
|
$domainReturn[0] = str_replace('.', '-', $domainReturn[0]);
|
35
|
|
|
|
36
|
|
|
$this->iniFileDomain = $domainReturn[0] . '.ini';
|
37
|
|
|
|
38
|
|
|
return;
|
39
|
|
|
}
|
40
|
|
|
|
41
|
|
View Code Duplication |
protected function precheck($str) {
|
|
|
|
|
42
|
|
|
$str = htmlspecialchars(trim($str), ENT_QUOTES, 'UTF-8');
|
43
|
|
|
|
44
|
|
|
if (empty($str))
|
45
|
|
|
exit('something went wrong 7');
|
|
|
|
|
46
|
|
|
|
47
|
|
|
return $str;
|
48
|
|
|
}
|
49
|
|
|
|
50
|
|
|
protected function readIni() {
|
51
|
|
|
// check if ini files exists
|
52
|
|
|
if (!file_exists($this->iniFilePath . $this->iniFileDomain))
|
53
|
|
|
exit('something went wrong 8');
|
|
|
|
|
54
|
|
|
if (!file_exists($this->iniFilePath . $this->iniFileInwx))
|
55
|
|
|
exit('something went wrong 1');
|
|
|
|
|
56
|
|
|
|
57
|
|
|
// read domain.ini
|
58
|
|
|
$ini = parse_ini_file($this->iniFilePath . $this->iniFileDomain, TRUE);
|
59
|
|
|
|
60
|
|
|
$this->domain['inwx']['domain'] = self::precheck($ini['inwx']['domain']);
|
61
|
|
|
$this->domain['inwx']['subdomain'] = self::precheck($ini['inwx']['subdomain']);
|
62
|
|
|
$this->domain['ddns']['apikey'] = self::precheck($ini['ddns']['apikey']);
|
63
|
|
|
$this->domain['log']['filepath'] = self::precheck($ini['log']['filepath']);
|
64
|
|
|
|
65
|
|
|
//read inwx.ini
|
66
|
|
|
$ini = parse_ini_file($this->iniFilePath . $this->iniFileInwx, TRUE);
|
67
|
|
|
|
68
|
|
|
$this->inwx['apiurl'] = self::precheck($ini['apiurl']);
|
69
|
|
|
$this->inwx['username'] = self::precheck($ini['username']);
|
70
|
|
|
$this->inwx['password'] = self::precheck($ini['password']);
|
71
|
|
|
|
72
|
|
|
return;
|
73
|
|
|
}
|
74
|
|
|
|
75
|
|
|
protected function checkAccess($apikey = null) {
|
76
|
|
View Code Duplication |
if ($apikey == null || $this->domain['ddns']['apikey'] !== $apikey)
|
|
|
|
|
77
|
|
|
exit('something went wrong 3');
|
|
|
|
|
78
|
|
|
|
79
|
|
|
return;
|
80
|
|
|
}
|
81
|
|
|
|
82
|
|
View Code Duplication |
protected function inwxLogin() {
|
|
|
|
|
83
|
|
|
// INWX Setup class
|
84
|
|
|
$this->domrobot = new \INWX\domrobot($this->inwx['apiurl']);
|
85
|
|
|
$this->domrobot->setDebug(false);
|
86
|
|
|
$this->domrobot->setLanguage('en');
|
87
|
|
|
|
88
|
|
|
// INWX Login
|
89
|
|
|
$result = $this->domrobot->login($this->inwx['username'], $this->inwx['password']);
|
90
|
|
|
|
91
|
|
|
if (DEBUG)
|
92
|
|
|
print_r($result);
|
93
|
|
|
|
94
|
|
|
// check result
|
95
|
|
|
if ($result['code'] != 1000) {
|
96
|
|
|
if (OUTPUT)
|
97
|
|
|
exit('badauth');
|
|
|
|
|
98
|
|
|
|
99
|
|
|
exit('something went wrong 4');
|
|
|
|
|
100
|
|
|
}
|
101
|
|
|
|
102
|
|
|
return;
|
103
|
|
|
}
|
104
|
|
|
|
105
|
|
View Code Duplication |
protected function inwxLogout() {
|
|
|
|
|
106
|
|
|
$result = $this->domrobot->logout();
|
107
|
|
|
|
108
|
|
|
if (DEBUG)
|
109
|
|
|
print_r($result);
|
110
|
|
|
|
111
|
|
|
// check result
|
112
|
|
|
if ($result['code'] != 1500) {
|
113
|
|
|
exit('something went wrong 5');
|
|
|
|
|
114
|
|
|
}
|
115
|
|
|
|
116
|
|
|
return;
|
117
|
|
|
}
|
118
|
|
|
|
119
|
|
View Code Duplication |
protected function inwxGetNameserverInfo() {
|
|
|
|
|
120
|
|
|
$object = "nameserver";
|
121
|
|
|
$methode = "info";
|
122
|
|
|
|
123
|
|
|
$params = array();
|
124
|
|
|
$params['domain'] = $this->domain['inwx']['domain'];
|
125
|
|
|
$params['name'] = $this->domain['inwx']['subdomain'];
|
126
|
|
|
|
127
|
|
|
$result = $this->domrobot->call($object, $methode, $params);
|
128
|
|
|
|
129
|
|
|
if (DEBUG)
|
130
|
|
|
print_r($result);
|
131
|
|
|
|
132
|
|
|
// check result
|
133
|
|
|
if ($result['code'] != 1000) {
|
134
|
|
|
exit('something went wrong 6');
|
|
|
|
|
135
|
|
|
}
|
136
|
|
|
|
137
|
|
|
foreach ($result["resData"]["record"] as $value) {
|
138
|
|
|
if ($value['type'] == "A") {
|
139
|
|
|
$this->IP4['id'] = $value['id'];
|
140
|
|
|
$this->IP4['oldip'] = $value['content'];
|
141
|
|
|
}
|
142
|
|
|
if ($value['type'] == "AAAA") {
|
143
|
|
|
$this->IP6['id'] = $value['id'];
|
144
|
|
|
$this->IP6['oldip'] = $value['content'];
|
145
|
|
|
}
|
146
|
|
|
}
|
147
|
|
|
|
148
|
|
|
return;
|
149
|
|
|
}
|
150
|
|
|
|
151
|
|
View Code Duplication |
protected function inwxSetNameserverInfo($type = null) {
|
|
|
|
|
152
|
|
|
$object = "nameserver";
|
153
|
|
|
$methode = "updateRecord";
|
154
|
|
|
|
155
|
|
|
$params = array();
|
156
|
|
|
|
157
|
|
|
if ($type == 'ipv4') {
|
158
|
|
|
$params['id'] = $this->IP4['id'];
|
159
|
|
|
$params['content'] = $this->IP4['newip'];
|
160
|
|
|
} elseif ($type == 'ipv6') {
|
161
|
|
|
$params['id'] = $this->IP6['id'];
|
162
|
|
|
$params['content'] = $this->IP6['newip'];
|
163
|
|
|
} else
|
164
|
|
|
exit('something went wrong 9');
|
|
|
|
|
165
|
|
|
|
166
|
|
|
$result = $this->domrobot->call($object, $methode, $params);
|
167
|
|
|
|
168
|
|
|
if (DEBUG)
|
169
|
|
|
print_r($result);
|
170
|
|
|
|
171
|
|
|
// check result
|
172
|
|
|
if ($result['code'] != 1000) {
|
173
|
|
|
exit('something went wrong 10');
|
|
|
|
|
174
|
|
|
}
|
175
|
|
|
|
176
|
|
|
if (OUTPUT)
|
177
|
|
|
echo('good');
|
178
|
|
|
|
179
|
|
|
return;
|
180
|
|
|
}
|
181
|
|
|
|
182
|
|
View Code Duplication |
public function updateIP($ipv4 = null, $ipv6 = null) {
|
|
|
|
|
183
|
|
|
$this->inwxLogin();
|
184
|
|
|
|
185
|
|
|
$this->inwxGetNameserverInfo();
|
186
|
|
|
|
187
|
|
|
if (filter_var($ipv4, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
|
188
|
|
|
$this->IP4['newip'] = $ipv4;
|
189
|
|
|
$this->inwxSetNameserverInfo('ipv4');
|
190
|
|
|
}
|
191
|
|
|
|
192
|
|
|
if (filter_var($ipv6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
|
193
|
|
|
$this->IP6['newip'] = $ipv6;
|
194
|
|
|
$this->inwxSetNameserverInfo('ipv6');
|
195
|
|
|
}
|
196
|
|
|
|
197
|
|
|
$this->inwxLogout();
|
198
|
|
|
|
199
|
|
|
return;
|
200
|
|
|
}
|
201
|
|
|
|
202
|
|
|
}
|
203
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.