1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* INWX DDNS Manager |
4
|
|
|
* |
5
|
|
|
* @author Peter Siemer <[email protected]> |
6
|
|
|
* @license https://opensource.org/licenses/GPL-3.0 GNU Public License |
7
|
|
|
* @link https://affenrakete.de |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
header('Content-type: text/plain; charset=utf-8'); // Set UFT-8 Header |
11
|
|
|
|
12
|
|
|
define("DEBUG", false); |
13
|
|
|
define("OUTPUT", true); |
14
|
|
|
|
15
|
|
|
//error_reporting(E_ALL); |
|
|
|
|
16
|
|
|
//ini_set("display_errors", 1); |
17
|
|
|
|
18
|
|
|
require_once('./libraries/domrobot.php'); |
19
|
|
|
|
20
|
|
View Code Duplication |
function precheck($str, $checkEmpty = false) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
$str = htmlspecialchars(trim($str), ENT_QUOTES, 'UTF-8'); |
23
|
|
|
|
24
|
|
|
if($checkEmpty && empty($str)) |
25
|
|
|
exit('something went wrong 7'); |
|
|
|
|
26
|
|
|
|
27
|
|
|
return $str; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
class DDNS |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
protected $iniFilePath = "./conf/"; |
33
|
|
|
protected $iniFileInwx = "inwx.ini"; |
34
|
|
|
protected $iniFileDomain = ""; |
35
|
|
|
|
36
|
|
|
protected $inwx = []; // {apiurl, username, password} |
|
|
|
|
37
|
|
|
protected $domain = []; // [inwx] => {domain, subdomain}, [ddns] => {apikey} |
|
|
|
|
38
|
|
|
protected $IP4 = []; // {oldip, newip, id} |
|
|
|
|
39
|
|
|
protected $IP6 = []; // {oldip, newip, id} |
|
|
|
|
40
|
|
|
|
41
|
|
|
protected $domrobot; |
42
|
|
|
|
43
|
|
|
public function __construct($apidomain = null, $apikey = null) |
44
|
|
|
{ |
45
|
|
|
$this->setIniFileDomain($apidomain); |
46
|
|
|
$this->readIni(); |
47
|
|
|
$this->checkAccess($apikey); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
protected function setIniFileDomain($apidomain = null) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
preg_match("/(?!.{253})((?!-)[A-Za-z0-9-]{1,63}(?<!-)\.){1,126}+[A-Za-z]{2,6}/", $apidomain, $domainReturn); |
53
|
|
|
$domainReturn[0] = str_replace('.', '-', $domainReturn[0]); |
54
|
|
|
|
55
|
|
|
$this->iniFileDomain = $domainReturn[0] . '.ini'; |
56
|
|
|
|
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function readIni() |
61
|
|
|
{ |
62
|
|
|
// check if ini files exists |
63
|
|
|
if(!file_exists($this->iniFilePath . $this->iniFileDomain)) |
64
|
|
|
exit('something went wrong 8'); |
|
|
|
|
65
|
|
|
if(!file_exists($this->iniFilePath . $this->iniFileInwx)) |
66
|
|
|
exit('something went wrong 1'); |
|
|
|
|
67
|
|
|
|
68
|
|
|
// read domain.ini |
69
|
|
|
$ini = parse_ini_file($this->iniFilePath . $this->iniFileDomain, TRUE); |
70
|
|
|
|
71
|
|
|
$this->domain['inwx']['domain'] = precheck($ini['inwx']['domain'], true); |
72
|
|
|
$this->domain['inwx']['subdomain'] = precheck($ini['inwx']['subdomain'], true); |
73
|
|
|
$this->domain['ddns']['apikey'] = precheck($ini['ddns']['apikey'], true); |
74
|
|
|
$this->domain['log']['filepath'] = precheck($ini['log']['filepath'], true); |
75
|
|
|
|
76
|
|
|
//read inwx.ini |
77
|
|
|
$ini = parse_ini_file($this->iniFilePath . $this->iniFileInwx, TRUE); |
78
|
|
|
|
79
|
|
|
$this->inwx['apiurl'] = precheck($ini['apiurl'], true); |
80
|
|
|
$this->inwx['username'] = precheck($ini['username'], true); |
81
|
|
|
$this->inwx['password'] = precheck($ini['password'], true); |
82
|
|
|
|
83
|
|
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function checkAccess($apikey = null) |
87
|
|
|
{ |
88
|
|
View Code Duplication |
if($apikey == null || $this->domain['ddns']['apikey'] !== $apikey) |
|
|
|
|
89
|
|
|
exit('something went wrong 3'); |
|
|
|
|
90
|
|
|
|
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
View Code Duplication |
protected function inwxLogin() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
// INWX Setup class |
97
|
|
|
$this->domrobot = new INWX\domrobot($this->inwx['apiurl']); |
98
|
|
|
$this->domrobot->setDebug(false); |
99
|
|
|
$this->domrobot->setLanguage('en'); |
100
|
|
|
|
101
|
|
|
// INWX Login |
102
|
|
|
$result = $this->domrobot->login($this->inwx['username'], $this->inwx['password']); |
103
|
|
|
|
104
|
|
|
if(DEBUG) |
105
|
|
|
print_r($result); |
106
|
|
|
|
107
|
|
|
// check result |
108
|
|
|
if ($result['code'] != 1000) |
109
|
|
|
{ |
110
|
|
|
if(OUTPUT) |
111
|
|
|
exit('badauth'); |
|
|
|
|
112
|
|
|
|
113
|
|
|
exit('something went wrong 4'); |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
View Code Duplication |
protected function inwxLogout() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$result = $this->domrobot->logout(); |
122
|
|
|
|
123
|
|
|
if(DEBUG) |
124
|
|
|
print_r($result); |
125
|
|
|
|
126
|
|
|
// check result |
127
|
|
|
if ($result['code'] != 1500) |
128
|
|
|
{ |
129
|
|
|
exit('something went wrong 5'); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
View Code Duplication |
protected function inwxGetNameserverInfo() |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
$object = "nameserver"; |
138
|
|
|
$methode = "info"; |
139
|
|
|
|
140
|
|
|
$params = array(); |
141
|
|
|
$params['domain'] = $this->domain['inwx']['domain']; |
142
|
|
|
$params['name'] = $this->domain['inwx']['subdomain']; |
143
|
|
|
|
144
|
|
|
$result = $this->domrobot->call($object, $methode, $params); |
145
|
|
|
|
146
|
|
|
if(DEBUG) |
147
|
|
|
print_r($result); |
148
|
|
|
|
149
|
|
|
// check result |
150
|
|
|
if ($result['code'] != 1000) |
151
|
|
|
{ |
152
|
|
|
exit('something went wrong 6'); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
foreach($result["resData"]["record"] as $value) |
156
|
|
|
{ |
157
|
|
|
if($value['type'] == "A") |
158
|
|
|
{ |
159
|
|
|
$this->IP4['id'] = $value['id']; |
160
|
|
|
$this->IP4['oldip'] = $value['content']; |
161
|
|
|
} |
162
|
|
|
if($value['type'] == "AAAA") |
163
|
|
|
{ |
164
|
|
|
$this->IP6['id'] = $value['id']; |
165
|
|
|
$this->IP6['oldip'] = $value['content']; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
View Code Duplication |
protected function inwxSetNameserverInfo($type = null) |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
$object = "nameserver"; |
175
|
|
|
$methode = "updateRecord"; |
176
|
|
|
|
177
|
|
|
$params = array(); |
178
|
|
|
|
179
|
|
|
if($type == 'ipv4') |
180
|
|
|
{ |
181
|
|
|
$params['id'] = $this->IP4['id']; |
182
|
|
|
$params['content'] = $this->IP4['newip']; |
183
|
|
|
} |
184
|
|
|
elseif($type == 'ipv6') |
185
|
|
|
{ |
186
|
|
|
$params['id'] = $this->IP6['id']; |
187
|
|
|
$params['content'] = $this->IP6['newip']; |
188
|
|
|
} |
189
|
|
|
else |
190
|
|
|
exit('something went wrong 9'); |
|
|
|
|
191
|
|
|
|
192
|
|
|
$result = $this->domrobot->call($object, $methode, $params); |
193
|
|
|
|
194
|
|
|
if(DEBUG) |
195
|
|
|
print_r($result); |
196
|
|
|
|
197
|
|
|
// check result |
198
|
|
|
if ($result['code'] != 1000) |
199
|
|
|
{ |
200
|
|
|
exit('something went wrong 10'); |
|
|
|
|
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if(OUTPUT) |
204
|
|
|
echo('good'); |
205
|
|
|
|
206
|
|
|
return; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
View Code Duplication |
public function updateIP($ipv4 = null, $ipv6 = null) |
|
|
|
|
210
|
|
|
{ |
211
|
|
|
$this->inwxLogin(); |
212
|
|
|
|
213
|
|
|
$this->inwxGetNameserverInfo(); |
214
|
|
|
|
215
|
|
|
if(filter_var($ipv4, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) |
216
|
|
|
{ |
217
|
|
|
$this->IP4['newip'] = $ipv4; |
218
|
|
|
$this->inwxSetNameserverInfo('ipv4'); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
if(filter_var($ipv6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) |
222
|
|
|
{ |
223
|
|
|
$this->IP6['newip'] = $ipv6; |
224
|
|
|
$this->inwxSetNameserverInfo('ipv6'); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$this->inwxLogout(); |
228
|
|
|
|
229
|
|
|
return; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$input['domain'] = (isset($_GET['domain'])) ? precheck($_GET['domain'], true) : null; |
234
|
|
|
$input['password'] = (isset($_GET['password'])) ? precheck($_GET['password'], true) : null; |
235
|
|
|
$input['ipv4'] = (isset($_GET['ipv4'])) ? precheck($_GET['ipv4'], false) : null; |
236
|
|
|
$input['ipv6'] = (isset($_GET['ipv6'])) ? precheck($_GET['ipv6'], false) : null; |
237
|
|
|
|
238
|
|
|
$ddns = new DDNS($input['domain'], $input['password']); |
239
|
|
|
$ddns->updateIP($input['ipv4'], $input['ipv6']); |
240
|
|
|
|
241
|
|
|
?> |
|
|
|
|
242
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.