Project

General

Profile

Download (67.4 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
	/*
3
	 * PHP.updateDNS (pfSense version)
4
	 *
5
	 * +====================================================+
6
	 *  Services Supported:
7
	 *    - DynDns (dyndns.org) [dynamic, static, custom]
8
	 *    - No-IP (no-ip.com)
9
	 *    - EasyDNS (easydns.com)
10
	 *    - DHS (www.dhs.org)
11
	 *    - HN (hn.org) -- incomplete checking!
12
	 *    - DynS (dyns.org)
13
	 *    - ZoneEdit (zoneedit.com)
14
	 *    - FreeDNS (freedns.afraid.org)
15
	 *    - FreeDNS IPv6 (freedns.afraid.org)
16
	 *    - Loopia (loopia.se)
17
	 *    - StaticCling (staticcling.org)
18
	 *    - DNSexit (dnsexit.com)
19
	 *    - OpenDNS (opendns.com)
20
	 *    - Namecheap (namecheap.com)
21
	 *    - HE.net (dns.he.net)
22
	 *    - HE.net IPv6 (dns.he.net)
23
	 *    - HE.net Tunnelbroker IP update (ipv4.tunnelbroker.net)
24
	 *    - SelfHost (selfhost.de)
25
	 *    - Amazon Route 53 (aws.amazon.com)
26
	 *    - DNS-O-Matic (dnsomatic.com)
27
	 *    - Custom DDNS (any URL)
28
	 *    - Custom DDNS IPv6 (any URL)
29
	 *    - CloudFlare (www.cloudflare.com)
30
	 *    - Eurodns (eurodns.com)
31
	 *    - GratisDNS (gratisdns.dk)
32
	 *    - City Network (citynetwork.se)
33
	 *    - GleSYS (glesys.com)
34
	 *    - DNSimple (dnsimple.com)
35
	 *    - Google Domains (domains.google.com)
36
	 *    - DNS Made Easy (www.dnsmadeeasy.com)
37
	 *    - SPDNS (spdns.de)
38
	 *    - SPDNS IPv6 (spdns.de)
39
	 * +----------------------------------------------------+
40
	 *  Requirements:
41
	 *    - PHP version 4.0.2 or higher with the CURL Library and the PCRE Library
42
	 * +----------------------------------------------------+
43
	 *  Public Functions
44
	 *    - updatedns()
45
	 *
46
	 *  Private Functions
47
	 *    - _update()
48
	 *    - _checkStatus()
49
	 *    - _error()
50
	 *    - _detectChange()
51
	 *    - _debug()
52
	 *    - _checkIP()
53
	 * +----------------------------------------------------+
54
	 *  DynDNS Dynamic  - Last Tested: 12 July 2005
55
	 *  DynDNS Static   - Last Tested: NEVER
56
	 *  DynDNS Custom   - Last Tested: NEVER
57
	 *  No-IP           - Last Tested: 20 July 2008
58
	 *  HN.org          - Last Tested: 12 July 2005
59
	 *  EasyDNS         - Last Tested: 20 July 2008
60
	 *  DHS             - Last Tested: 12 July 2005
61
	 *  ZoneEdit        - Last Tested: NEVER
62
	 *  Dyns            - Last Tested: NEVER
63
	 *  ODS             - Last Tested: 02 August 2005
64
	 *  FreeDNS         - Last Tested: 01 May 2016
65
	 *  FreeDNS IPv6    - Last Tested: 01 May 2016
66
	 *  Loopia          - Last Tested: NEVER
67
	 *  StaticCling     - Last Tested: 27 April 2006
68
	 *  DNSexit         - Last Tested: 20 July 2008
69
	 *  OpenDNS         - Last Tested: 4 August 2008
70
	 *  Namecheap       - Last Tested: 31 August 2010
71
	 *  HE.net          - Last Tested: 7 July 2013
72
	 *  HE.net IPv6     - Last Tested: 7 July 2013
73
	 *  HE.net Tunnel   - Last Tested: 28 June 2011
74
	 *  SelfHost        - Last Tested: 26 December 2011
75
	 *  Amazon Route 53 - Last tested: 01 April 2012
76
	 *  DNS-O-Matic     - Last Tested: 9 September 2010
77
	 *  CloudFlare      - Last Tested: 30 May 2013
78
	 *  Eurodns         - Last Tested: 27 June 2013
79
	 *  GratisDNS       - Last Tested: 15 August 2012
80
	 *  OVH DynHOST     - Last Tested: NEVER
81
	 *  City Network    - Last Tested: 13 November 2013
82
	 *  GleSYS          - Last Tested: 3 February 2015
83
	 *  DNSimple        - Last Tested: 09 February 2015
84
	 *  Google Domains  - Last Tested: 27 April 2015
85
	 *  DNS Made Easy   - Last Tested: 27 April 2015
86
	 *  SPDNS           - Last Tested: 04 December 2015
87
	 *  SPDNS IPv6      - Last Tested: 04 December 2015
88
	 * +====================================================+
89
	 *
90
	 * @author 	E.Kristensen
91
	 * @link    	http://www.idylldesigns.com/projects/phpdns/
92
	 * @version 	0.8
93
	 * @updated	13 October 05 at 21:02:42 GMT
94
	 *
95
	 * DNSexit/OpenDNS support and multiwan extension for pfSense by Ermal Luçi
96
	 * Custom DNS support by Matt Corallo
97
	 *
98
	 */
99

    
100
	class updatedns {
101
		var $_cacheFile;
102
		var $_cacheFile_v6;
103
		var $_debugFile;
104
		var $_UserAgent = 'User-Agent: phpDynDNS/0.7';
105
		var $_errorVerbosity = 0;
106
		var $_dnsService;
107
		var $_dnsUser;
108
		var $_dnsPass;
109
		var $_dnsHost;
110
		var $_dnsDomain;
111
		var $_FQDN;
112
		var $_dnsIP;
113
		var $_dnsWildcard;
114
		var $_dnsMX;
115
		var $_dnsBackMX;
116
		var $_dnsServer;
117
		var $_dnsPort;
118
		var $_dnsUpdateURL;
119
		var $_dnsZoneID;
120
		var $_dnsTTL;
121
		var $status;
122
		var $_debugID;
123
		var $_if;
124
		var $_dnsResultMatch;
125
		var $_dnsRequestIf;
126
		var $_dnsRequestIfIP;
127
		var $_dnsVerboseLog;
128
		var $_curlIpresolveV4;
129
		var $_curlSslVerifypeer;
130
		var $_dnsMaxCacheAgeDays;
131
		var $_dnsDummyUpdateDone;
132
		var $_forceUpdateNeeded;
133
		var $_useIPv6;
134

    
135
		/*
136
		 * Public Constructor Function (added 12 July 05) [beta]
137
		 *   - Gets the dice rolling for the update.
138
		 *   - $dnsResultMatch should only be used with $dnsService = 'custom'
139
		 *   -  $dnsResultMatch is parsed for '%IP%', which is the IP the provider was updated to,
140
		 *   -  it is otherwise expected to be exactly identical to what is returned by the Provider.
141
		 *   - $dnsUser, and $dnsPass indicate HTTP Auth for custom DNS, if they are needed in the URL (GET Variables), include them in $dnsUpdateURL.
142
		 *   - $For custom requests, $dnsUpdateURL is parsed for '%IP%', which is replaced with the new IP.
143
		 */
144
		function updatedns ($dnsService = '', $dnsHost = '', $dnsDomain = '', $dnsUser = '', $dnsPass = '',
145
					$dnsWildcard = 'OFF', $dnsMX = '', $dnsIf = '', $dnsBackMX = '',
146
					$dnsServer = '', $dnsPort = '', $dnsUpdateURL = '', $forceUpdate = false,
147
					$dnsZoneID ='', $dnsTTL='', $dnsResultMatch = '', $dnsRequestIf = '',
148
					$dnsID = '', $dnsVerboseLog = false, $curlIpresolveV4 = false, $curlSslVerifypeer = true) {
149

    
150
			global $config, $g;
151

    
152
			if ($dnsService == "namecheap") {
153
				$this->_FQDN = $dnsHost . "." . $dnsDomain;
154
			} else {
155
				$this->_FQDN = $dnsHost;
156
			}
157

    
158
			$this->_cacheFile = "{$g['conf_path']}/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($this->_FQDN) . "{$dnsID}.cache";
159
			$this->_cacheFile_v6 = "{$g['conf_path']}/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($this->_FQDN) . "{$dnsID}_v6.cache";
160
			$this->_debugFile = "{$g['varetc_path']}/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($this->_FQDN) . "{$dnsID}.debug";
161

    
162
			$this->_curlIpresolveV4 = $curlIpresolveV4;
163
			$this->_curlSslVerifypeer = $curlSslVerifypeer;
164
			$this->_dnsVerboseLog = $dnsVerboseLog;
165
			if ($this->_dnsVerboseLog) {
166
				log_error(gettext("Dynamic DNS: updatedns() starting"));
167
			}
168

    
169
			$dyndnslck = lock("DDNS".$dnsID, LOCK_EX);
170

    
171
			if (!$dnsService) $this->_error(2);
172
			switch ($dnsService) {
173
			case 'freedns':
174
			case 'freedns-v6':
175
				if (!$dnsHost) $this->_error(5);
176
				break;
177
			case 'namecheap':
178
				if (!$dnsPass) $this->_error(4);
179
				if (!$dnsHost) $this->_error(5);
180
				if (!$dnsDomain) $this->_error(5);
181
				break;
182
			case 'route53':
183
				if (!$dnsZoneID) $this->_error(8);
184
				if (!$dnsTTL) $this->_error(9);
185
				break;
186
			case 'custom':
187
				if (!$dnsUpdateURL) $this->_error(7);
188
				break;
189
			default:
190
				if (!$dnsUser) $this->_error(3);
191
				if (!$dnsPass) $this->_error(4);
192
				if (!$dnsHost) $this->_error(5);
193
			}
194

    
195
			switch ($dnsService) {
196
				case 'he-net-v6':
197
				case 'custom-v6':
198
				case 'spdns-v6':
199
				case 'freedns-v6':
200
					$this->_useIPv6 = true;
201
					break;
202
				default:
203
					$this->_useIPv6 = false;
204
			}
205
			$this->_dnsService = strtolower($dnsService);
206
			$this->_dnsUser = $dnsUser;
207
			$this->_dnsPass = $dnsPass;
208
			$this->_dnsHost = $dnsHost;
209
			$this->_dnsDomain = $dnsDomain;
210
			$this->_dnsServer = $dnsServer;
211
			$this->_dnsPort = $dnsPort;
212
			$this->_dnsWildcard = $dnsWildcard;
213
			$this->_dnsMX = $dnsMX;
214
			$this->_dnsZoneID = $dnsZoneID;
215
			$this->_dnsTTL = $dnsTTL;
216
			$this->_if = get_failover_interface($dnsIf);
217
			$this->_checkIP();
218
			$this->_dnsUpdateURL = $dnsUpdateURL;
219
			$this->_dnsResultMatch = $dnsResultMatch;
220
			$this->_dnsRequestIf = get_failover_interface($dnsRequestIf);
221
			if ($this->_dnsVerboseLog) {
222
				log_error(sprintf(gettext('Dynamic DNS (%1$s): running get_failover_interface for %2$s. found %3$s'), $this->_FQDN, $dnsRequestIf, $this->_dnsRequestIf));
223
			}
224
			$this->_dnsRequestIfIP = get_interface_ip($dnsRequestIf);
225
			$this->_dnsMaxCacheAgeDays = 25;
226
			$this->_dnsDummyUpdateDone = false;
227
			$this->_forceUpdateNeeded = $forceUpdate;
228

    
229
			// Ensure that we were able to lookup the IP
230
			if (!is_ipaddr($this->_dnsIP)) {
231
				log_error(sprintf(gettext('Dynamic DNS (%1$s) There was an error trying to determine the public IP for interface - %2$s (%3$s %4$s).'), $this->_FQDN, $dnsIf, $this->_if, $this->_dnsIP));
232
				unlock($dyndnslck);
233
				return;
234
			}
235

    
236
			$this->_debugID = rand(1000000, 9999999);
237

    
238
			if ($forceUpdate == false && $this->_detectChange() == false) {
239
				$this->_error(10);
240
			} else {
241
				switch ($this->_dnsService) {
242
					case 'glesys':
243
					case 'dnsomatic':
244
					case 'dyndns':
245
					case 'dyndns-static':
246
					case 'dyndns-custom':
247
					case 'dhs':
248
					case 'noip':
249
					case 'noip-free':
250
					case 'easydns':
251
					case 'hn':
252
					case 'zoneedit':
253
					case 'dyns':
254
					case 'ods':
255
					case 'freedns':
256
					case 'freedns-v6':
257
					case 'loopia':
258
					case 'staticcling':
259
					case 'dnsexit':
260
					case 'custom':
261
					case 'custom-v6':
262
					case 'opendns':
263
					case 'namecheap':
264
					case 'he-net':
265
					case 'he-net-v6':
266
					case 'selfhost':
267
					case 'he-net-tunnelbroker':
268
					case 'route53':
269
					case 'cloudflare':
270
					case 'eurodns':
271
					case 'gratisdns':
272
					case 'ovh-dynhost':
273
					case 'citynetwork':
274
					case 'dnsimple':
275
					case 'googledomains':
276
					case 'dnsmadeeasy':
277
					case 'spdns':
278
					case 'spdns-v6':
279
						$this->_update();
280
						if ($this->_dnsDummyUpdateDone == true) {
281
							// If a dummy update was needed, then sleep a while and do the update again to put the proper address back.
282
							// Some providers (e.g. No-IP free accounts) need to have at least 1 address change every month.
283
							// If the address has not changed recently, or the user did "Force Update", then the code does
284
							// a dummy address change for providers like this.
285
							sleep(10);
286
							$this->_update();
287
						}
288
						break;
289
					default:
290
						$this->_error(6);
291
						break;
292
				}
293
			}
294

    
295
			unlock($dyndnslck);
296
		}
297

    
298
		/*
299
		 * Private Function (added 12 July 05) [beta]
300
		 *   Send Update To Selected Service.
301
		 */
302
		function _update() {
303

    
304
			if ($this->_dnsVerboseLog) {
305
				log_error(sprintf(gettext('Dynamic DNS %1$s (%2$s): _update() starting.'), $this->_dnsService, $this->_FQDN));
306
			}
307

    
308
			if (strstr($this->_dnsRequestIf, "_vip")) {
309
				$parentif = get_configured_vip_interface($this->_dnsRequestIf);
310
				$realparentif = convert_friendly_interface_to_real_interface_name($parentif);
311
			} else {
312
				$realparentif = $this->_dnsRequestIf;
313
			}
314

    
315
			$ch = curl_init();
316

    
317
			if ($this->_useIPv6 == false) {
318
				curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
319
			}
320

    
321
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53 ') {
322
				curl_setopt($ch, CURLOPT_HEADER, 0);
323
				curl_setopt($ch, CURLOPT_USERAGENT, $this->_UserAgent);
324
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
325
				curl_setopt($ch, CURLOPT_INTERFACE, 'if!' . $realparentif);
326
				curl_setopt($ch, CURLOPT_TIMEOUT, 120); // Completely empirical
327
			}
328

    
329
			switch ($this->_dnsService) {
330
				case 'glesys':
331
					$needsIP = TRUE;
332
					$server = 'https://api.glesys.com/domain/updaterecord/format/json';
333
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
334
					$post_data['recordid'] = $this->_FQDN;
335
					$post_data['data'] = $this->_dnsIP;
336
					curl_setopt($ch, CURLOPT_URL, $server);
337
					curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
338
					break;
339
				case 'dyndns':
340
				case 'dyndns-static':
341
				case 'dyndns-custom':
342
					$needsIP = FALSE;
343
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") {
344
						$this->_dnsWildcard = "ON";
345
					}
346
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
347
					$server = "https://members.dyndns.org/nic/update";
348
					$port = "";
349
					if ($this->_dnsServer) {
350
						$server = $this->_dnsServer;
351
					}
352
					if ($this->_dnsPort) {
353
						$port = ":" . $this->_dnsPort;
354
					}
355
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
356
					break;
357
				case 'dhs':
358
					// DHS is disabled in the GUI because the following doesn't work.
359
					$needsIP = TRUE;
360
					$post_data['hostscmd'] = 'edit';
361
					$post_data['hostscmdstage'] = '2';
362
					$post_data['type'] = '4';
363
					$post_data['updatetype'] = 'Online';
364
					$post_data['mx'] = $this->_dnsMX;
365
					$post_data['mx2'] = '';
366
					$post_data['txt'] = '';
367
					$post_data['offline_url'] = '';
368
					$post_data['cloak'] = 'Y';
369
					$post_data['cloak_title'] = '';
370
					$post_data['ip'] = $this->_dnsIP;
371
					$post_data['domain'] = 'dyn.dhs.org';
372
					$post_data['hostname'] = $this->_dnsHost;
373
					$post_data['submit'] = 'Update';
374
					$server = "https://members.dhs.org/nic/hosts";
375
					$port = "";
376
					if ($this->_dnsServer) {
377
						$server = $this->_dnsServer;
378
					}
379
					if ($this->_dnsPort) {
380
						$port = ":" . $this->_dnsPort;
381
					}
382
					curl_setopt($ch, CURLOPT_URL, $server . $port);
383
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
384
					curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
385
					break;
386
				case 'noip':
387
				case 'noip-free':
388
					$needsIP = TRUE;
389
					$server = "https://dynupdate.no-ip.com/ducupdate.php";
390
					$port = "";
391
					if ($this->_dnsServer) {
392
						$server = $this->_dnsServer;
393
					}
394
					if ($this->_dnsPort) {
395
						$port = ":" . $this->_dnsPort;
396
					}
397
					if (($this->_dnsService == "noip-free") &&
398
					    ($this->_forceUpdateNeeded == true) &&
399
					    ($this->_dnsDummyUpdateDone == false)) {
400
						// Update the IP to a dummy value to force No-IP free accounts to see a change.
401
						$iptoset = "192.168.1.1";
402
						$this->_dnsDummyUpdateDone = true;
403
						log_error(sprintf(gettext('Dynamic DNS %1$s (%2$s): Processing dummy update on No-IP free account. IP temporarily set to %3$s'), $this->_dnsService, $this->_dnsHost, $iptoset));
404
					} else {
405
						$iptoset = $this->_dnsIP;
406
					}
407
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?username=' . urlencode($this->_dnsUser) . '&pass=' . urlencode($this->_dnsPass) . '&h[]=' . $this->_dnsHost.'&ip=' . $iptoset);
408
					break;
409
				case 'easydns':
410
					$needsIP = TRUE;
411
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
412
					$server = "https://members.easydns.com/dyn/dyndns.php";
413
					$port = "";
414
					if ($this->_dnsServer) {
415
						$server = $this->_dnsServer;
416
					}
417
					if ($this->_dnsPort) {
418
						$port = ":" . $this->_dnsPort;
419
					}
420
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard=' . $this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=' . $this->_dnsBackMX);
421
					break;
422
				case 'hn':
423
					$needsIP = TRUE;
424
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
425
					$server = "http://dup.hn.org/vanity/update";
426
					$port = "";
427
					if ($this->_dnsServer) {
428
						$server = $this->_dnsServer;
429
					}
430
					if ($this->_dnsPort) {
431
						$port = ":" . $this->_dnsPort;
432
					}
433
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?ver=1&IP=' . $this->_dnsIP);
434
					break;
435
				case 'zoneedit':
436
					$needsIP = FALSE;
437
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
438

    
439
					$server = "https://dynamic.zoneedit.com/auth/dynamic.html";
440
					$port = "";
441
					if ($this->_dnsServer) {
442
						$server = $this->_dnsServer;
443
					}
444
					if ($this->_dnsPort) {
445
						$port = ":" . $this->_dnsPort;
446
					}
447
					curl_setopt($ch, CURLOPT_URL, "{$server}{$port}?host=" .$this->_dnsHost);
448
					break;
449
				case 'dyns':
450
					$needsIP = FALSE;
451
					$server = "http://www.dyns.net/postscript011.php";
452
					$port = "";
453
					if ($this->_dnsServer) {
454
						$server = $this->_dnsServer;
455
					}
456
					if ($this->_dnsPort) {
457
						$port = ":" . $this->_dnsPort;
458
					}
459
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?username=' . urlencode($this->_dnsUser) . '&password=' . $this->_dnsPass . '&host=' . $this->_dnsHost);
460
					break;
461
				case 'ods':
462
					$needsIP = FALSE;
463
					$misc_errno = 0;
464
					$misc_error = "";
465
					$server = "ods.org";
466
					$port = "";
467
					if ($this->_dnsServer) {
468
						$server = $this->_dnsServer;
469
					}
470
					if ($this->_dnsPort) {
471
						$port = ":" . $this->_dnsPort;
472
					}
473
					$this->con['socket'] = fsockopen("{$server}{$port}", "7070", $misc_errno, $misc_error, 30);
474
					/* Check that we have connected */
475
					if (!$this->con['socket']) {
476
						print "error! could not connect.";
477
						break;
478
					}
479
					/* Here is the loop. Read the incoming data (from the socket connection) */
480
					while (!feof($this->con['socket'])) {
481
						$this->con['buffer']['all'] = trim(fgets($this->con['socket'], 4096));
482
						$code = substr($this->con['buffer']['all'], 0, 3);
483
						sleep(1);
484
						switch ($code) {
485
							case 100:
486
								fputs($this->con['socket'], "LOGIN ".$this->_dnsUser." ".$this->_dnsPass."\n");
487
								break;
488
							case 225:
489
								fputs($this->con['socket'], "DELRR ".$this->_dnsHost." A\n");
490
								break;
491
							case 901:
492
								fputs($this->con['socket'], "ADDRR ".$this->_dnsHost." A ".$this->_dnsIP."\n");
493
								break;
494
							case 795:
495
								fputs($this->con['socket'], "QUIT\n");
496
								break;
497
						}
498
					}
499
					$this->_checkStatus(0, $code);
500
					break;
501
				case 'freedns':
502
				case 'freedns-v6':
503
					$needIP = FALSE;
504
					curl_setopt($ch, CURLOPT_URL, 'https://freedns.afraid.org/dynamic/update.php?' . $this->_dnsPass);
505
					break;
506
				case 'dnsexit':
507
					$needsIP = TRUE;
508
					curl_setopt($ch, CURLOPT_URL, 'https://www.dnsexit.com/RemoteUpdate.sv?login='.$this->_dnsUser. '&password='.$this->_dnsPass.'&host='.$this->_dnsHost.'&myip='.$this->_dnsIP);
509
					break;
510
				case 'loopia':
511
					$needsIP = TRUE;
512
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
513
					curl_setopt($ch, CURLOPT_URL, 'https://dns.loopia.se/XDynDNSServer/XDynDNS.php?hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP);
514
					break;
515
				case 'opendns':
516
					$needsIP = FALSE;
517
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
518
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
519
					$server = "https://updates.opendns.com/nic/update?hostname=". $this->_dnsHost;
520
					$port = "";
521
					if ($this->_dnsServer) {
522
						$server = $this->_dnsServer;
523
					}
524
					if ($this->_dnsPort) {
525
						$port = ":" . $this->_dnsPort;
526
					}
527
					curl_setopt($ch, CURLOPT_URL, $server .$port);
528
					break;
529

    
530
				case 'staticcling':
531
					$needsIP = FALSE;
532
					curl_setopt($ch, CURLOPT_URL, 'https://www.staticcling.org/update.html?login='.$this->_dnsUser.'&pass='.$this->_dnsPass);
533
					break;
534
				case 'dnsomatic':
535
					/* Example syntax
536
						https://username:password@updates.dnsomatic.com/nic/update?hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG
537
					*/
538
					$needsIP = FALSE;
539
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") {
540
						$this->_dnsWildcard = "ON";
541
					}
542
					/*
543
					Reference: https://www.dnsomatic.com/wiki/api
544
						DNS-O-Matic usernames are 3-25 characters.
545
						DNS-O-Matic passwords are 6-20 characters.
546
						All ASCII letters and numbers accepted.
547
						Dots, dashes, and underscores allowed, but not at the beginning or end of the string.
548
					Required: "rawurlencode" http://www.php.net/manual/en/function.rawurlencode.php
549
						Encodes the given string according to RFC 3986.
550
					*/
551
					$server = "https://" . rawurlencode($this->_dnsUser) . ":" . rawurlencode($this->_dnsPass) . "@updates.dnsomatic.com/nic/update?hostname=";
552
					if ($this->_dnsServer) {
553
						$server = $this->_dnsServer;
554
					}
555
					if ($this->_dnsPort) {
556
						$port = ":" . $this->_dnsPort;
557
					}
558
					curl_setopt($ch, CURLOPT_URL, $server . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NOCHG');
559
					break;
560
				case 'namecheap':
561
					/* Example:
562
						https://dynamicdns.park-your-domain.com/update?host=[host_name]&domain=[domain.com]&password=[domain_password]&ip=[your_ip]
563
					*/
564
					$needsIP = FALSE;
565
					$dnspass = trim($this->_dnsPass);
566
					$server = "https://dynamicdns.park-your-domain.com/update?host={$this->_dnsHost}&domain={$this->_dnsDomain}&password={$dnspass}&ip={$this->_dnsIP}";
567
					curl_setopt($ch, CURLOPT_URL, $server);
568
					break;
569
				case 'he-net':
570
				case 'he-net-v6':
571
					$needsIP = FALSE;
572
					$server = "https://dyn.dns.he.net/nic/update?";
573
					curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
574
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
575
					curl_setopt($ch, CURLOPT_URL, $server . 'hostname=' . $this->_dnsHost . '&password=' . $this->_dnsPass . '&myip=' . $this->_dnsIP);
576
					break;
577
				case 'he-net-tunnelbroker':
578
					$needsIP = FALSE;
579
					$server = "https://ipv4.tunnelbroker.net/ipv4_end.php?";
580
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser . ':' . $this->_dnsPass);
581
					curl_setopt($ch, CURLOPT_URL, $server . 'tid=' . $this->_dnsHost);
582
					break;
583
				case 'selfhost':
584
					$needsIP = FALSE;
585
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") {
586
						$this->_dnsWildcard = "ON";
587
					}
588
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
589
					$server = "https://carol.selfhost.de/nic/update";
590
					$port = "";
591
					if ($this->_dnsServer) {
592
						$server = $this->_dnsServer;
593
					}
594
					if ($this->_dnsPort) {
595
						$port = ":" . $this->_dnsPort;
596
					}
597
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
598
					break;
599
				case 'route53':
600

    
601
					/* Setting Variables */
602
					$hostname = "{$this->_dnsHost}.";
603
					$ZoneID = trim($this->_dnsZoneID);
604
					$AccessKeyId = $this->_dnsUser;
605
					$SecretAccessKey = $this->_dnsPass;
606
					$NewIP = $this->_dnsIP;
607
					$NewTTL = $this->_dnsTTL;
608

    
609
					/* Include Route 53 Library Class */
610
					require_once('/etc/inc/r53.class');
611

    
612
					/* Set Amazon AWS Credentials for this record */
613
					$r53 = new Route53($AccessKeyId, $SecretAccessKey);
614

    
615
					/* Function to find old values of records in Route 53 */
616
					if (!function_exists('Searchrecords')) {
617
						function SearchRecords($records, $name) {
618
							$result = array();
619
							foreach ($records as $record) {
620
								if (strtolower($record['Name']) == strtolower($name)) {
621
									$result [] = $record;
622
								}
623
							}
624
							return ($result) ? $result : false;
625
						}
626
					}
627

    
628
					$records = $r53->listResourceRecordSets("/hostedzone/$ZoneID");
629

    
630
					/* Get IP for your hostname in Route 53 */
631
					if (false !== ($a_result = SearchRecords($records['ResourceRecordSets'], "$hostname"))) {
632
						$OldTTL = $a_result[0][TTL];
633
						$OldIP = $a_result[0][ResourceRecords][0];
634
					} else {
635
						$OldIP = "";
636
					}
637

    
638
					/* Check if we need to update DNS Record */
639
					if ($OldIP !== $NewIP || $OldTTL !== $NewTTL) {
640
						if (!empty($OldIP)) {
641
							/* Your Hostname already exists, deleting and creating it again */
642
							$changes = array();
643
							$changes[] = $r53->prepareChange(DELETE, $hostname, A, $OldTTL, $OldIP);
644
							$changes[] = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
645
							$result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
646
						} else {
647
							/* Your Hostname does not exist yet, creating it */
648
							$changes = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
649
							$result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
650
						}
651
					}
652
					$this->_checkStatus(0, $result);
653
					break;
654
				case 'custom':
655
				case 'custom-v6':
656
					if (strstr($this->dnsUpdateURL, "%IP%")) {$needsIP = TRUE;} else {$needsIP = FALSE;}
657
					if ($this->_dnsUser != '') {
658
						if ($this->_curlIpresolveV4) {
659
							curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
660
						}
661
						if ($this->_curlSslVerifypeer) {
662
							curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
663
						} else {
664
							curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
665
						}
666
						curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
667
						curl_setopt($ch, CURLOPT_USERPWD, "{$this->_dnsUser}:{$this->_dnsPass}");
668
					}
669
					$server = str_replace("%IP%", $this->_dnsIP, $this->_dnsUpdateURL);
670
					if ($this->_dnsVerboseLog) {
671
						log_error(sprintf(gettext("Sending request to: %s"), $server));
672
					}
673
					curl_setopt($ch, CURLOPT_URL, $server);
674
					break;
675
				case 'cloudflare':
676
					$needsIP = TRUE;
677
					$dnsServer ='api.cloudflare.com';
678
					$dnsHost = str_replace(' ', '', $this->_dnsHost);
679
					$host_names = explode(".", $dnsHost);
680
					$bottom_host_name = $host_names[count($host_names)-2] . "." . $host_names[count($host_names)-1];
681

    
682
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
683
					curl_setopt($ch, CURLOPT_HTTPHEADER, array(
684
						'X-Auth-Email: '.$this->_dnsUser.'',
685
						'X-Auth-Key: '.$this->_dnsPass.'',
686
						'Content-Type: application/json'
687
					));
688

    
689
					// Get zone ID
690
					$getZoneId = "https://{$dnsServer}/client/v4/zones/?name={$bottom_host_name}";
691
					curl_setopt($ch, CURLOPT_URL, $getZoneId);
692
					$output = json_decode(curl_exec($ch));
693
					$zone = $output->result[0]->id;
694
					if ($zone) { // If zone ID was found get host ID
695
						$getHostId = "https://{$dnsServer}/client/v4/zones/{$zone}/dns_records?name={$this->_dnsHost}";
696
						curl_setopt($ch, CURLOPT_URL, $getHostId);
697
						$output = json_decode(curl_exec($ch));
698
						$host = $output->result[0]->id;
699
						if ($host) { // If host ID was found update host
700
							$hostData = array(
701
								"content" => "{$this->_dnsIP}",
702
								"type" => "A",
703
								"name" => "{$this->_dnsHost}"
704
							);
705
							$data_json = json_encode($hostData);
706
							$updateHostId = "https://{$dnsServer}/client/v4/zones/{$zone}/dns_records/{$host}";
707
							curl_setopt($ch, CURLOPT_URL, $updateHostId);
708
							curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
709
							curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
710
						}
711
					}
712
					break;
713
				case 'eurodns':
714
					$needsIP = TRUE;
715
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
716
					$server = "https://update.eurodyndns.org/update/";
717
					$port = "";
718
					if ($this->_dnsPort) {
719
						$port = ":" . $this->_dnsPort;
720
					}
721
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP);
722
					break;
723
				case 'gratisdns':
724
					$needsIP = TRUE;
725
					$server = "https://ssl.gratisdns.dk/ddns.phtml";
726
					$host = trim($this->_dnsHost);
727
					$hostnames = explode(".", $host);
728
					$hostnames_count = count($hostnames);
729
					if ($hostnames_count > 2) {
730
						$domain = $hostnames[$hostnames_count-2] . "." . $hostnames[$hostnames_count-1];
731
					} else {
732
						$domain = $host;
733
					}
734
					curl_setopt($ch, CURLOPT_URL, $server . '?u=' . $this->_dnsUser . '&p=' . $this->_dnsPass . '&h=' . $host . '&d=' . $domain . '&i=' . $this->_dnsIP);
735
					break;
736
				case 'ovh-dynhost':
737
					$needsIP = FALSE;
738
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
739
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
740
					$server = "https://www.ovh.com/nic/update";
741
					$port = "";
742
					if ($this->_dnsServer) {
743
						$server = $this->_dnsServer;
744
					}
745
					if ($this->_dnsPort) {
746
						$port = ":" . $this->_dnsPort;
747
					}
748
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
749
					break;
750
				case 'citynetwork':
751
					$needsIP = TRUE;
752
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
753
					$server = 'https://dyndns.citynetwork.se/nic/update';
754
					$port = "";
755
					if ($this->_dnsServer) {
756
						$server = $this->_dnsServer;
757
					}
758
					if ($this->_dnsPort) {
759
						$port = ":" . $this->_dnsPort;
760
					}
761
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP);
762
					break;
763
				case 'dnsimple':
764
					/* Uses DNSimple's REST API
765
					   Requires username and Account API token passed in header
766
					   Piggybacks on Route 53's ZoneID field for DNSimple record ID
767
					   Data sent as JSON */
768
					$needsIP = TRUE;
769
					$server = 'https://api.dnsimple.com/v1/domains/';
770
					$token = $this->_dnsUser . ':' . $this->_dnsPass;
771
					$jsondata = '{"record":{"content":"' . $this->_dnsIP . '","ttl":"' . $this->_dnsTTL . '"}}';
772
					curl_setopt($ch, CURLOPT_HEADER, 1);
773
					curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
774
					curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json', 'X-DNSimple-Token: ' . $token));
775
					curl_setopt($ch, CURLOPT_URL, $server . $this->_dnsHost . '/records/' . $this->_dnsZoneID);
776
					curl_setopt($ch, CURLOPT_POSTFIELDS, $jsondata);
777
					break;
778
				case 'googledomains':
779
					$needsIP = FALSE;
780
					$post_data['username:password'] = $this->_dnsUser . ':' . $this->_dnsPass;
781
					$post_data['hostname'] = $this->_dnsHost;
782
					$post_data['myip'] = $this->_dnsIP;
783
					$post_data['offline'] = 'no';
784
					$server = "https://domains.google.com/nic/update";
785
					$port = "";
786
					curl_setopt($ch, CURLOPT_URL, 'https://domains.google.com/nic/update');
787
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
788
					curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
789
					break;
790
				case 'dnsmadeeasy':
791
					$needsIP = TRUE;
792
					$server = "https://cp.dnsmadeeasy.com/servlet/updateip";
793
					curl_setopt($ch, CURLOPT_URL, $server . '?username=' . $this->_dnsUser . '&password=' . $this->_dnsPass . '&id=' . $this->_dnsHost . '&ip=' . $this->_dnsIP);
794
					break;
795
				case 'spdns':
796
				case 'spdns-v6':
797
					$needsIP = FALSE;
798
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
799
					$server = "https://update.spdns.de/nic/update";
800
					$port = "";
801
					if ($this->_dnsServer) {
802
						$server = $this->_dnsServer;
803
					}
804
					if ($this->_dnsPort) {
805
						$port = ":" . $this->_dnsPort;
806
					}
807
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP);
808
					break;
809
				default:
810
					break;
811
			}
812
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53') {
813
				$data = curl_exec($ch);
814
				$this->_checkStatus($ch, $data);
815
				@curl_close($ch);
816
			}
817
		}
818

    
819
		/*
820
		 * Private Function (added 12 July 2005) [beta]
821
		 *   Retrieve Update Status
822
		 */
823
		function _checkStatus($ch, $data) {
824
			if ($this->_dnsVerboseLog) {
825
				log_error(sprintf(gettext('Dynamic DNS %1$s (%2$s): _checkStatus() starting.'), $this->_dnsService, $this->_FQDN));
826
			}
827
			$successful_update = false;
828
			$success_str = "(" . gettext("Success") . ") ";
829
			$error_str = "(" . gettext("Error") . ") ";
830
			$status_intro = "phpDynDNS ({$this->_dnsHost}): ";
831

    
832
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53' && @curl_error($ch)) {
833
				$status = gettext("Curl error occurred:") . " " . curl_error($ch);
834
				log_error($status);
835
				$this->status = $status;
836
				return;
837
			}
838
			switch ($this->_dnsService) {
839
				case 'glesys':
840
					$status_intro = "GleSYS ({$this->_dnsHost}): ";
841
					if (preg_match('/Record updated/i', $data)) {
842
						$status = $status_intro . $success_str . gettext("IP Address Changed Successfully!") . " (" . $this->_dnsIP . ")";
843
						$successful_update = true;
844
					} else {
845
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
846
						log_error($status_intro . gettext("PAYLOAD:") . " {$data}");
847
						$this->_debug($data);
848
					}
849
					break;
850
				case 'dnsomatic':
851
					$status_intro = "DNS-O-Matic ({$this->_dnsHost}): ";
852
					if (preg_match('/badauth/i', $data)) {
853
						$status = $status_intro . gettext("The DNS-O-Matic username or password specified are incorrect. No updates will be distributed to services until this is resolved.");
854
					} else if (preg_match('/notfqdn /i', $data)) {
855
						$status = $status_intro . gettext("The hostname specified is not a fully-qualified domain name. If no hostnames included, notfqdn will be returned once.");
856
					} else if (preg_match('/nohost/i', $data)) {
857
						$status = $status_intro . gettext("The hostname passed could not be matched to any services configured. The service field will be blank in the return code.");
858
					} else if (preg_match('/numhost/i', $data)) {
859
						$status = $status_intro . gettext("Up to 20 hosts my be updated. numhost is returned if attempting to update more than 20 or update a round-robin.");
860
					} else if (preg_match('/abuse/i', $data)) {
861
						$status = $status_intro . gettext("The hostname is blocked for update abuse.");
862
					} else if (preg_match('/good/i', $data)) {
863
						$status = $status_intro . $success_str . gettext("IP Address Changed Successfully!") . " (" . $this->_dnsIP . ")";
864
						$successful_update = true;
865
					} else if (preg_match('/dnserr/i', $data)) {
866
						$status = $status_intro . gettext("DNS error encountered. Stop updating for 30 minutes.");
867
					} else {
868
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
869
						log_error($status_intro . gettext("PAYLOAD:") . " {$data}");
870
						$this->_debug($data);
871
					}
872
					break;
873
				case 'citynetwork':
874
					if (preg_match('/notfqdn/i', $data)) {
875
						$status = $status_intro . $error_str . gettext("Not A FQDN!");
876
					} else if (preg_match('/nohost/i', $data)) {
877
						$status = $status_intro . $error_str . gettext("No such host");
878
					} else if (preg_match('/nochg/i', $data)) {
879
						$status = $status_intro . $success_str . gettext("No Change In IP Address");
880
						$successful_update = true;
881
					} else if (preg_match('/good/i', $data)) {
882
						$status = $status_intro . $success_str . gettext("IP Address Changed Successfully!") . " (" . $this->_dnsIP . ")";
883
						$successful_update = true;
884
					} else if (preg_match('/badauth/i', $data)) {
885
						$status = $status_intro . $error_str . gettext("User Authorization Failed");
886
					} else {
887
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
888
						log_error($status_intro . gettext("PAYLOAD:") . " {$data}");
889
						$this->_debug($data);
890
					}
891
					break;
892
				case 'ovh-dynhost':
893
				case 'dyndns':
894
					if (preg_match('/notfqdn/i', $data)) {
895
						$status = $status_intro . $error_str . gettext("Not A FQDN!");
896
					} else if (preg_match('/nochg/i', $data)) {
897
						$status = $status_intro . $success_str . gettext("No Change In IP Address");
898
						$successful_update = true;
899
					} else if (preg_match('/good/i', $data)) {
900
						$status = $status_intro . $success_str . gettext("IP Address Changed Successfully!") . " (" . $this->_dnsIP . ")";
901
						$successful_update = true;
902
					} else if (preg_match('/noauth/i', $data)) {
903
						$status = $status_intro . $error_str . gettext("User Authorization Failed");
904
					} else {
905
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
906
						log_error($status_intro . gettext("PAYLOAD:") . " {$data}");
907
						$this->_debug($data);
908
					}
909
					break;
910
				case 'dyndns-static':
911
					if (preg_match('/notfqdn/i', $data)) {
912
						$status = $status_intro . $error_str . gettext("Not A FQDN!");
913
					} else if (preg_match('/nochg/i', $data)) {
914
						$status = $status_intro . $success_str . gettext("No Change In IP Address");
915
						$successful_update = true;
916
					} else if (preg_match('/good/i', $data)) {
917
						$status = $status_intro . $success_str . gettext("IP Address Changed Successfully!");
918
						$successful_update = true;
919
					} else if (preg_match('/noauth/i', $data)) {
920
						$status = $status_intro . $error_str . gettext("User Authorization Failed");
921
					} else {
922
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
923
						log_error($status_intro . gettext("PAYLOAD:") . " {$data}");
924
						$this->_debug($data);
925
					}
926
					break;
927
				case 'dyndns-custom':
928
					if (preg_match('/notfqdn/i', $data)) {
929
						$status = $status_intro . $error_str . gettext("Not A FQDN!");
930
					} else if (preg_match('/nochg/i', $data)) {
931
						$status = $status_intro . $success_str . gettext("No Change In IP Address");
932
						$successful_update = true;
933
					} else if (preg_match('/good/i', $data)) {
934
						$status = $status_intro . $success_str . gettext("IP Address Changed Successfully!");
935
						$successful_update = true;
936
					} else if (preg_match('/noauth/i', $data)) {
937
						$status = $status_intro . $error_str . gettext("User Authorization Failed");
938
					} else {
939
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
940
						log_error($status_intro . gettext("PAYLOAD:") . " {$data}");
941
						$this->_debug($data);
942
					}
943
					break;
944
				case 'dhs':
945
					break;
946
				case 'noip':
947
				case 'noip-free':
948
					list($ip, $code) = explode(":", $data);
949
					switch ($code) {
950
						case 0:
951
							$status = $status_intro . $success_str . gettext("IP address is current, no update performed.");
952
							$successful_update = true;
953
							break;
954
						case 1:
955
							$status = $status_intro . $success_str . gettext("DNS hostname update successful.");
956
							$successful_update = true;
957
							break;
958
						case 2:
959
							$status = $status_intro . $error_str . gettext("Hostname supplied does not exist.");
960
							break;
961
						case 3:
962
							$status = $status_intro . $error_str . gettext("Invalid Username.");
963
							break;
964
						case 4:
965
							$status = $status_intro . $error_str . gettext("Invalid Password.");
966
							break;
967
						case 5:
968
							$status = $status_intro . $error_str . gettext("Too many updates sent.");
969
							break;
970
						case 6:
971
							$status = $status_intro . $error_str . gettext("Account disabled due to violation of No-IP terms of service.");
972
							break;
973
						case 7:
974
							$status = $status_intro . $error_str . gettext("Invalid IP. IP Address submitted is improperly formatted or is a private IP address or is on a blacklist.");
975
							break;
976
						case 8:
977
							$status = $status_intro . $error_str . gettext("Disabled / Locked Hostname.");
978
							break;
979
						case 9:
980
							$status = $status_intro . $error_str . gettext("Host updated is configured as a web redirect and no update was performed.");
981
							break;
982
						case 10:
983
							$status = $status_intro . $error_str . gettext("Group supplied does not exist.");
984
							break;
985
						case 11:
986
							$status = $status_intro . $success_str . gettext("DNS group update is successful.");
987
							$successful_update = true;
988
							break;
989
						case 12:
990
							$status = $status_intro . $success_str . gettext("DNS group is current, no update performed.");
991
							$successful_update = true;
992
							break;
993
						case 13:
994
							$status = $status_intro . $error_str . gettext("Update client support not available for supplied hostname or group.");
995
							break;
996
						case 14:
997
							$status = $status_intro . $error_str . gettext("Hostname supplied does not have offline settings configured.");
998
							break;
999
						case 99:
1000
							$status = $status_intro . $error_str . gettext("Client disabled. Client should exit and not perform any more updates without user intervention.");
1001
							break;
1002
						case 100:
1003
							$status = $status_intro . $error_str . gettext("Client disabled. Client should exit and not perform any more updates without user intervention.");
1004
							break;
1005
						default:
1006
							$status = $status_intro . "(" . gettext("Unknown Response") . ")";
1007
							$this->_debug(gettext("Unknown Response:") . " " . $data);
1008
							break;
1009
					}
1010
					break;
1011
				case 'easydns':
1012
					if (preg_match('/NOACCESS/i', $data)) {
1013
						$status = $status_intro . $error_str . gettext("Authentication Failed: Username and/or Password was Incorrect.");
1014
					} else if (preg_match('/NOSERVICE/i', $data)) {
1015
						$status = $status_intro . $error_str . gettext("No Service: Dynamic DNS Service has been disabled for this domain.");
1016
					} else if (preg_match('/ILLEGAL INPUT/i', $data)) {
1017
						$status = $status_intro . $error_str . gettext("Illegal Input: Self-Explanatory");
1018
					} else if (preg_match('/TOOSOON/i', $data)) {
1019
						$status = $status_intro . $error_str . gettext("Too Soon: Not Enough Time Has Elapsed Since Last Update");
1020
					} else if (preg_match('/NOERROR/i', $data)) {
1021
						$status = $status_intro . $success_str . gettext("IP Updated Successfully!");
1022
						$successful_update = true;
1023
					} else {
1024
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
1025
						log_error($status_intro . gettext("PAYLOAD:") . " " . $data);
1026
						$this->_debug($data);
1027
					}
1028
					break;
1029
				case 'hn':
1030
					/* FIXME: add checks */
1031
					break;
1032
				case 'zoneedit':
1033
					if (preg_match('/799/i', $data)) {
1034
						$status = $status_intro . "(" . gettext("Error 799") . ") " . gettext("Update Failed!");
1035
					} else if (preg_match('/700/i', $data)) {
1036
						$status = $status_intro . "(" . gettext("Error 700") . ") " . gettext("Update Failed!");
1037
					} else if (preg_match('/200/i', $data)) {
1038
						$status = $status_intro . $success_str . gettext("IP Address Updated Successfully!");
1039
						$successful_update = true;
1040
					} else if (preg_match('/201/i', $data)) {
1041
						$status = $status_intro . $success_str . gettext("IP Address Updated Successfully!");
1042
						$successful_update = true;
1043
					} else {
1044
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
1045
						log_error($status_intro . gettext("PAYLOAD:") . " " . $data);
1046
						$this->_debug($data);
1047
					}
1048
					break;
1049
				case 'dyns':
1050
					if (preg_match("/400/i", $data)) {
1051
						$status = $status_intro . $error_str . gettext("Bad Request - The URL was malformed. Required parameters were not provided.");
1052
					} else if (preg_match('/402/i', $data)) {
1053
						$status = $status_intro . $error_str . gettext("Update Too Soon - Attempted to update too quickly since last change.");
1054
					} else if (preg_match('/403/i', $data)) {
1055
						$status = $status_intro . $error_str . gettext("Database Error - There was a server-sided database error.");
1056
					} else if (preg_match('/405/i', $data)) {
1057
						$status = $status_intro . $error_str . sprintf(gettext("Hostname Error - The hostname (%s) doesn't belong to user (%s)."), $this->_dnsHost, $this->_dnsUser);
1058
					} else if (preg_match('/200/i', $data)) {
1059
						$status = $status_intro . $success_str . gettext("IP Address Updated Successfully!");
1060
						$successful_update = true;
1061
					} else {
1062
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
1063
						log_error($status_intro . gettext("PAYLOAD:") . " " . $data);
1064
						$this->_debug($data);
1065
					}
1066
					break;
1067
				case 'ods':
1068
					if (preg_match("/299/i", $data)) {
1069
						$status = $status_intro . $success_str . gettext("IP Address Updated Successfully!");
1070
						$successful_update = true;
1071
					} else {
1072
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
1073
						log_error($status_intro . gettext("PAYLOAD:") . " " . $data);
1074
						$this->_debug($data);
1075
					}
1076
					break;
1077
				case 'freedns':
1078
				case 'freedns-v6':
1079
					if (preg_match("/has not changed./i", $data)) {
1080
						$status = $status_intro . $success_str . gettext("No Change In IP Address");
1081
						$successful_update = true;
1082
					} else if (preg_match("/Updated/i", $data)) {
1083
						$status = $status_intro . $success_str . gettext("IP Address Changed Successfully!");
1084
						$successful_update = true;
1085
					} else {
1086
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
1087
						log_error($status_intro . gettext("PAYLOAD:") . " " . $data);
1088
						$this->_debug($data);
1089
					}
1090
					break;
1091
				case 'dnsexit':
1092
					if (preg_match("/is the same/i", $data)) {
1093
						$status = $status_intro . $success_str . gettext("No Change In IP Address");
1094
						$successful_update = true;
1095
					} else if (preg_match("/Success/i", $data)) {
1096
						$status = $status_intro . $success_str . gettext("IP Address Changed Successfully!");
1097
						$successful_update = true;
1098
					} else {
1099
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
1100
						log_error($status_intro . gettext("PAYLOAD:") . " " . $data);
1101
						$this->_debug($data);
1102
					}
1103
					break;
1104
				case 'loopia':
1105
					if (preg_match("/nochg/i", $data)) {
1106
						$status = $status_intro . $success_str . gettext("No Change In IP Address");
1107
						$successful_update = true;
1108
					} else if (preg_match("/good/i", $data)) {
1109
						$status = $status_intro . $success_str . gettext("IP Address Changed Successfully!");
1110
						$successful_update = true;
1111
					} else if (preg_match('/badauth/i', $data)) {
1112
						$status = $status_intro . $error_str . gettext("User Authorization Failed");
1113
					} else {
1114
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
1115
						log_error($status_intro . gettext("PAYLOAD:") . " " . $data);
1116
						$this->_debug($data);
1117
					}
1118
					break;
1119
				case 'opendns':
1120
					if (preg_match('/badauth/i', $data)) {
1121
						$status = $status_intro . $error_str . gettext("Not a valid username or password!");
1122
					} else if (preg_match('/nohost/i', $data)) {
1123
						$status = $status_intro . $error_str . gettext("Hostname specified does not exist.");
1124
						$successful_update = true;
1125
					} else if (preg_match('/good/i', $data)) {
1126
						$status = $status_intro . $success_str . gettext("IP Address Changed Successfully!") . " (" . $this->_dnsIP . ")";
1127
						$successful_update = true;
1128
					} else if (preg_match('/yours/i', $data)) {
1129
						$status = $status_intro . $error_str . gettext("Hostname specified exists, but not under the username specified.");
1130
					} else if (preg_match('/abuse/i', $data)) {
1131
						$status = $status_intro . $error_str . gettext("Updating too frequently, considered abuse.");
1132
					} else {
1133
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
1134
						log_error($status_intro . gettext("PAYLOAD:") . " " . $data);
1135
						$this->_debug($data);
1136
					}
1137
					break;
1138
				case 'staticcling':
1139
					if (preg_match("/invalid ip/i", $data)) {
1140
						$status = $status_intro . $error_str . gettext("Bad Request - The IP provided was invalid.");
1141
					} else if (preg_match('/required info missing/i', $data)) {
1142
						$status = $status_intro . $error_str . gettext("Bad Request - Required parameters were not provided.");
1143
					} else if (preg_match('/invalid characters/i', $data)) {
1144
						$status = $status_intro . $error_str . gettext("Bad Request - Illegal characters in either the username or the password.");
1145
					} else if (preg_match('/bad password/i', $data)) {
1146
						$status = $status_intro . $error_str . gettext("Invalid password.");
1147
					} else if (preg_match('/account locked/i', $data)) {
1148
						$status = $status_intro . $error_str . gettext("This account has been administratively locked.");
1149
					} else if (preg_match('/update too frequent/i', $data)) {
1150
						$status = $status_intro . $error_str . gettext("Updating too frequently.");
1151
					} else if (preg_match('/DB error/i', $data)) {
1152
						$status = $status_intro . $error_str . gettext("Server side error.");
1153
					} else if (preg_match('/success/i', $data)) {
1154
						$status = $status_intro . $success_str . gettext("IP Address Updated Successfully!");
1155
						$successful_update = true;
1156
					} else {
1157
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
1158
						log_error($status_intro . gettext("PAYLOAD:") . " " . $data);
1159
						$this->_debug($data);
1160
					}
1161
					break;
1162
				case 'namecheap':
1163
					$tmp = str_replace("^M", "", $data);
1164
					$ncresponse = @xml2array($tmp);
1165
					if (preg_match("/internal server error/i", $data)) {
1166
						$status = $status_intro . $error_str . gettext("Server side error.");
1167
					} else if (preg_match("/request is badly formed/i", $data)) {
1168
						$status = $status_intro . $error_str . gettext("Badly Formed Request (check the settings).");
1169
					} else if ($ncresponse['interface-response']['ErrCount'] === "0") {
1170
						$status = $status_intro . $success_str . gettext("IP Address Updated Successfully!");
1171
						$successful_update = true;
1172
					} else if (is_numeric($ncresponse['interface-response']['ErrCount']) && ($ncresponse['interface-response']['ErrCount'] > 0)) {
1173
						$status = $status_intro . $error_str . implode(", ", $ncresponse["interface-response"]["errors"]);
1174
						$successful_update = true;
1175
					} else {
1176
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
1177
						log_error($status_intro . gettext("PAYLOAD:") . " " . $data);
1178
						$this->_debug($data);
1179
					}
1180
					break;
1181

    
1182
				case 'he-net':
1183
				case 'he-net-v6':
1184
					if (preg_match("/badip/i", $data)) {
1185
						$status = $status_intro . $error_str . gettext("Bad Request - The IP provided was invalid.");
1186
					} else if (preg_match('/nohost/i', $data)) {
1187
						$status = $status_intro . $error_str . gettext("Bad Request - A hostname was not provided.");
1188
					} else if (preg_match('/badauth/i', $data)) {
1189
						$status = $status_intro . $error_str . gettext("Invalid username or password.");
1190
					} else if (preg_match('/good/i', $data)) {
1191
						$status = $status_intro . $success_str . gettext("IP Address Updated Successfully!");
1192
						$successful_update = true;
1193
					} else if (preg_match('/nochg/i', $data)) {
1194
						$status = $status_intro . $success_str . gettext("No Change In IP Address.");
1195
						$successful_update = true;
1196
					} else {
1197
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
1198
						log_error($status_intro . gettext("PAYLOAD:") . " " . $data);
1199
						$this->_debug($data);
1200
					}
1201
					break;
1202
				case 'he-net-tunnelbroker':
1203
					/*
1204
					-ERROR: Missing parameter(s).
1205
					-ERROR: Invalid API key or password
1206
					-ERROR: Tunnel not found
1207
					-ERROR: Another tunnel exists for this IP.
1208
					-ERROR: This tunnel is already associated with this IP address
1209
					+OK: Tunnel endpoint updated to: x.x.x.x
1210
					*/
1211
					if (preg_match("/Missing parameter/i", $data)) {
1212
						$status = $status_intro . $error_str . gettext("Bad Request - Missing/Invalid Parameters.");
1213
					} else if (preg_match('/Tunnel not found/i', $data)) {
1214
						$status = $status_intro . $error_str . gettext("Bad Request - Invalid Tunnel ID.");
1215
					} else if (preg_match('/Invalid API key or password/i', $data)) {
1216
						$status = $status_intro . $error_str . gettext("Invalid username or password.");
1217
					} else if (preg_match('/OK:/i', $data)) {
1218
						$status = $status_intro . $success_str . gettext("IP Address Updated Successfully!");
1219
						$successful_update = true;
1220
					} else if (preg_match('/This tunnel is already associated with this IP address/i', $data)) {
1221
						$status = $status_intro . $success_str . gettext("No Change In IP Address.");
1222
						$successful_update = true;
1223
					} else {
1224
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
1225
						log_error($status_intro . gettext("PAYLOAD:") . " " . $data);
1226
						$this->_debug($data);
1227
					}
1228
					break;
1229
				case 'selfhost':
1230
					if (preg_match('/notfqdn/i', $data)) {
1231
						$status = $status_intro . $error_str . gettext("Not A FQDN!");
1232
					} else if (preg_match('/nochg/i', $data)) {
1233
						$status = $status_intro . $success_str . gettext("No Change In IP Address.");
1234
						$successful_update = true;
1235
					} else if (preg_match('/good/i', $data)) {
1236
						$status = $status_intro . $success_str . gettext("IP Address Changed Successfully!") . " (" . $this->_dnsIP . ")";
1237
						$successful_update = true;
1238
					} else if (preg_match('/noauth/i', $data)) {
1239
						$status = $status_intro . $error_str . gettext("User Authorization Failed");
1240
					} else {
1241
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
1242
						log_error($status_intro . gettext("PAYLOAD:") . " " . $data);
1243
						$this->_debug($data);
1244
					}
1245
					break;
1246
				case 'route53':
1247
					$successful_update = true;
1248
					break;
1249
				case 'custom':
1250
				case 'custom-v6':
1251
					$successful_update = false;
1252
					if ($this->_dnsResultMatch == "") {
1253
						$successful_update = true;
1254
					} else {
1255
						$this->_dnsResultMatch = str_replace("%IP%", $this->_dnsIP, $this->_dnsResultMatch);
1256
						$matches = preg_split("/(?<!\\\\)\\|/", $this->_dnsResultMatch);
1257
						foreach ($matches as $match) {
1258
							$match= str_replace("\\|", "|", $match);
1259
							if (strcmp($match, trim($data, "\t\n\r")) == 0) {
1260
								$successful_update = true;
1261
							}
1262
						}
1263
						unset ($matches);
1264
					}
1265
					if ($successful_update == true) {
1266
						$status = $status_intro . $success_str . gettext("IP Address Updated Successfully!");
1267
					} else {
1268
						$status = $status_intro . $error_str . gettext("Result did not match.") . " [" . $data . "]";
1269
					}
1270
					break;
1271
				case 'cloudflare':
1272
					$output = json_decode($data);
1273
					if ($output->result->content === $this->_dnsIP) {
1274
						$status = $status_intro . $success_str . sprintf(gettext('%1$s updated to %2$s'), $this->_dnsHost, $this->_dnsIP);
1275
						$successful_update = true;
1276
					} elseif ($output->errors[0]->code === 9103) {
1277
						$status = $status_intro . $error_str . gettext("Invalid Credentials! Don't forget to use API Key for password field with CloudFlare.");
1278
					} elseif (($output->success) && (!$output->result[0]->id)) {
1279
						$status = $status_intro . $error_str . gettext("Zone or Host ID was not found, check the hostname.");
1280
					} else {
1281
						$status = $status_intro . gettext("UNKNOWN ERROR") . " - " . $output->errors[0]->message;
1282
						log_error($status_intro . gettext("PAYLOAD:") . " " . $data);
1283
					}
1284
					break;
1285
				case 'eurodns':
1286
					if (preg_match('/notfqdn/i', $data)) {
1287
						$status = $status_intro . $error_str . gettext("Not A FQDN!");
1288
					} else if (preg_match('/nochg/i', $data)) {
1289
						$status = $status_intro . $success_str . gettext("No Change In IP Address");
1290
						$successful_update = true;
1291
					} else if (preg_match('/good/i', $data)) {
1292
						$status = $status_intro . $success_str . gettext("IP Address Changed Successfully!") . " (" . $this->_dnsIP . ")";
1293
						$successful_update = true;
1294
					} else if (preg_match('/badauth/i', $data)) {
1295
						$status = $status_intro . $error_str . gettext("User Authorization Failed");
1296
					} else {
1297
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
1298
						log_error($status_intro . gettext("PAYLOAD:") . " " . $data);
1299
						$this->_debug($data);
1300
					}
1301
					break;
1302
				case 'gratisdns':
1303
					if (preg_match('/Forkerte værdier/i', $data)) {
1304
						$status = $status_intro . $error_str . gettext("Wrong values - Update could not be completed.");
1305
					} else if (preg_match('/Bruger login: Bruger eksistere ikke/i', $data)) {
1306
						$status = $status_intro . $error_str . gettext("Unknown username - User does not exist.");
1307
					} else if (preg_match('/Bruger login: 1Fejl i kodeord/i', $data)) {
1308
						$status = $status_intro . $error_str . gettext("Wrong password - Remember password is case sensitive.");
1309
					} else if (preg_match('/Domæne kan IKKE administreres af bruger/i', $data)) {
1310
						$status = $status_intro . $error_str . gettext("User unable to administer the selected domain.");
1311
					} else if (preg_match('/OK/i', $data)) {
1312
						$status = $status_intro . $success_str . gettext("IP Address Updated Successfully!");
1313
						$successful_update = true;
1314
					} else {
1315
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
1316
						log_error($status_intro . gettext("PAYLOAD:") . " " . $data);
1317
						$this->_debug($data);
1318
					}
1319
					break;
1320
				case 'dnsimple':
1321
					/* Responds with HTTP 200 on success.
1322
					   Responds with HTTP 4xx on error.
1323
					   Returns JSON data as body */
1324
					$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
1325
					$header = substr($data, 0, $header_size);
1326
					$body = substr($data, $header_size);
1327
					if (preg_match("/Status: 200\s/i", $header)) {
1328
						$status = $status_intro . $success_str . gettext("IP Address Updated Successfully!");
1329
						$successful_update = true;
1330
					} else if (preg_match("/Status: 4\d\d\s/i", $header)) {
1331
						$arrbody = json_decode($body, true);
1332
						$message = $arrbody['message'] . ".";
1333
						if (isset($arrbody['errors']['content'])) {
1334
							foreach ($arrbody['errors']['content'] as $key => $content) {
1335
								$message .= " " . $content . ".";
1336
							}
1337
						}
1338
						$status = $status_intro . $error_str . $message;
1339
					} else {
1340
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
1341
						log_error($status_intro . gettext("PAYLOAD:") . " " . $body);
1342
						$this->_debug($body);
1343
					}
1344
					break;
1345
				case 'googledomains':
1346
					if (preg_match('/notfqdn/i', $data)) {
1347
						$status = $status_intro . $error_str . gettext("Not A FQDN");
1348
					} else if (preg_match('/nochg/i', $data)) {
1349
						$status = $status_intro . $success_str . gettext("No Change In IP Address");
1350
						$successful_update = true;
1351
					} else if (preg_match('/good/i', $data)) {
1352
						$status = $status_intro . $success_str . gettext("IP Address Changed Successfully!") . " (" . $this->_dnsIP . ")";
1353
						$successful_update = true;
1354
					} else if (preg_match('/badauth/i', $data)) {
1355
						$status = $status_intro . $error_str . gettext("User Authorization Failed");
1356
					} else if (preg_match('/nohost/i', $data)) {
1357
						$status = $status_intro . $error_str . gettext("Hostname does not exist or DynDNS not enabled");
1358
					} else if (preg_match('/badagent/i', $data)) {
1359
						$status = $status_intro . $error_str . gettext("Bad request");
1360
					} else if (preg_match('/abuse/i', $data)) {
1361
						$status = $status_intro . $error_str . gettext("Dynamic DNS access has been blocked!");
1362
					} else if (preg_match('/911/i', $data)) {
1363
						$status = $status_intro . $error_str . gettext("Error on Google's end, retry in 5 minutes");
1364
					} else {
1365
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
1366
						log_error($status_intro . gettext("PAYLOAD:") . " " . $data);
1367
						$this->_debug($data);
1368
					}
1369
					break;
1370
				case 'dnsmadeeasy':
1371
					switch ($data) {
1372
						case 'success':
1373
							$status = $status_intro . $success_str . gettext("IP Address Changed Successfully!") . " (" . $this->_dnsIP . ")";
1374
							$successful_update = true;
1375
							break;
1376
						case 'error-auth':
1377
							$status = $status_intro . $error_str . gettext("Invalid username or password");
1378
							break;
1379
						case 'error-auth-suspend':
1380
							$status = $status_intro . $error_str . gettext("Account suspended");
1381
							break;
1382
						case 'error-auth-voided':
1383
							$status = $status_intro . $error_str . gettext("Account revoked");
1384
							break;
1385
						case 'error-record-invalid':
1386
							$status = $status_intro . $error_str . gettext("Record does not exist in the system. Unable to update record");
1387
							break;
1388
						case 'error-record-auth':
1389
							$status = $status_intro . $error_str . gettext("User does not have access to this record");
1390
							break;
1391
						case 'error-record-ip-same':
1392
							$status = $status_intro . $success_str . gettext("No Change In IP Address");
1393
							$successful_update = true;
1394
							break;
1395
						case 'error-system':
1396
							$status = $status_intro . $error_str . gettext("General system error recognized by the system");
1397
							break;
1398
						case 'error':
1399
							$status = $status_intro . $error_str . gettext("General system error unrecognized by the system");
1400
							break;
1401
						default:
1402
							$status = $status_intro . "(" . gettext("Unknown Response") . ")";
1403
							log_error($status_intro . gettext("PAYLOAD:") . " " . $data);
1404
							$this->_debug($data);
1405
							break;
1406
					}
1407
					break;
1408
				case 'spdns':
1409
				case 'spdns-v6':
1410
					if (preg_match('/notfqdn/i', $data)) {
1411
						$status = $status_intro . $error_str . gettext("Not A FQDN!");
1412
					} else if (preg_match('/nohost/i', $data)) {
1413
						$status = $status_intro . $error_str . gettext("No such host");
1414
					} else if (preg_match('/nochg/i', $data)) {
1415
						$status = $status_intro . $success_str . gettext("No Change In IP Address");
1416
						$successful_update = true;
1417
					} else if (preg_match('/good/i', $data)) {
1418
						$status = $status_intro . $success_str . gettext("IP Address Changed Successfully!") . " (" . $this->_dnsIP . ")";
1419
						$successful_update = true;
1420
					} else if (preg_match('/badauth/i', $data)) {
1421
						$status = $status_intro . $error_str . gettext("User Authorization Failed");
1422
					} else {
1423
						$status = $status_intro . "(" . gettext("Unknown Response") . ")";
1424
						log_error($status_intro . gettext("PAYLOAD:") . " " . $data);
1425
						$this->_debug($data);
1426
					}
1427
					break;
1428
			}
1429

    
1430
			if ($successful_update == true) {
1431
				/* Write WAN IP to cache file */
1432
				$wan_ip = $this->_checkIP();
1433
				conf_mount_rw();
1434
				if ($this->_useIPv6 == false && $wan_ip > 0) {
1435
					$currentTime = time();
1436
					notify_all_remote(sprintf(gettext('DynDNS updated IP Address on %1$s (%2$s) to %3$s'), convert_real_interface_to_friendly_descr($this->_if), $this->_if, $wan_ip));
1437
					log_error(sprintf(gettext('phpDynDNS: updating cache file %1$s: %2$s'), $this->_cacheFile, $wan_ip));
1438
					@file_put_contents($this->_cacheFile, "{$wan_ip}:{$currentTime}");
1439
				} else {
1440
					@unlink($this->_cacheFile);
1441
				}
1442
				if ($this->_useIPv6 == true && $wan_ip > 0) {
1443
					$currentTime = time();
1444
					notify_all_remote(sprintf(gettext("DynDNS updated IPv6 Address on %s (%s) to %s"), convert_real_interface_to_friendly_descr($this->_if), $this->_if, $wan_ip));
1445
					log_error(sprintf(gettext('phpDynDNS: updating cache file %1$s: %2$s'), $this->_cacheFile_v6, $wan_ip));
1446
					@file_put_contents($this->_cacheFile_v6, "{$wan_ip}|{$currentTime}");
1447
				} else {
1448
					@unlink($this->_cacheFile_v6);
1449
				}
1450
				conf_mount_ro();
1451
			}
1452
			$this->status = $status;
1453
			log_error($status);
1454
		}
1455

    
1456
		/*
1457
		 * Private Function (added 12 July 05) [beta]
1458
		 *   Return Error, Set Last Error, and Die.
1459
		 */
1460
		function _error($errorNumber = '1') {
1461
			$err_str = 'phpDynDNS: (' . gettext('ERROR!') . ') ';
1462
			$err_str_r53 = 'Route 53: (' . gettext('Error') . ') ';
1463
			switch ($errorNumber) {
1464
				case 0:
1465
					break;
1466
				case 2:
1467
					$error = $err_str . gettext('No Dynamic DNS Service provider was selected.');
1468
					break;
1469
				case 3:
1470
					$error = $err_str . gettext('No Username Provided.');
1471
					break;
1472
				case 4:
1473
					$error = $err_str . gettext('No Password Provided.');
1474
					break;
1475
				case 5:
1476
					$error = $err_str . gettext('No Hostname Provided.');
1477
					break;
1478
				case 6:
1479
					$error = $err_str . gettext('The Dynamic DNS Service provided is not yet supported.');
1480
					break;
1481
				case 7:
1482
					$error = $err_str . gettext('No Update URL Provided.');
1483
					break;
1484
				case 8:
1485
					$status = $err_str_r53 . gettext("Invalid ZoneID");
1486
					break;
1487
				case 9:
1488
					$status = $err_str_r53 . gettext("Invalid TTL");
1489
					break;
1490
				case 10:
1491
					$error = "phpDynDNS ({$this->_FQDN}): " . sprintf(gettext("No change in my IP address and/or %s days has not passed. Not updating dynamic DNS entry."), $this->_dnsMaxCacheAgeDays);
1492
					break;
1493
				default:
1494
					$error = $err_str . gettext('Unknown Response.');
1495
					/* FIXME: $data isn't in scope here */
1496
					/* $this->_debug($data); */
1497
					break;
1498
			}
1499
			$this->lastError = $error;
1500
			log_error($error);
1501
		}
1502

    
1503
		/*
1504
		 * Private Function (added 12 July 05) [beta]
1505
		 *   - Detect whether or not IP needs to be updated.
1506
		 *      | Written Specifically for pfSense (https://www.pfsense.org) may
1507
		 *      | work with other systems. pfSense base is FreeBSD.
1508
		 */
1509
		function _detectChange() {
1510
			global $debug;
1511

    
1512
			if ($debug) {
1513
				log_error(sprintf(gettext('Dynamic DNS %1$s (%2$s): _detectChange() starting.'), $this->_dnsService, $this->_FQDN));
1514
			}
1515

    
1516
			$currentTime = time();
1517

    
1518
			$wan_ip = $this->_checkIP();
1519
			if ($wan_ip == 0) {
1520
				log_error(sprintf(gettext("Dynamic Dns (%s): Current WAN IP could not be determined, skipping update process."), $this->_FQDN));
1521
				return false;
1522
			}
1523
			$log_error = sprintf(gettext('Dynamic Dns (%1$s): Current WAN IP: %2$s'), $this->_FQDN, $wan_ip) . " ";
1524

    
1525
			if ($this->_useIPv6 == true) {
1526
				if (file_exists($this->_cacheFile_v6)) {
1527
					$contents = file_get_contents($this->_cacheFile_v6);
1528
					list($cacheIP, $cacheTime) = explode('|', $contents);
1529
					$this->_debug($cacheIP.'/'.$cacheTime);
1530
					$initial = false;
1531
					$log_error .= sprintf(gettext("Cached IPv6: %s"), $cacheIP);
1532
				} else {
1533
					conf_mount_rw();
1534
					$cacheIP = '::';
1535
					@file_put_contents($this->_cacheFile, "::|{$currentTime}");
1536
					conf_mount_ro();
1537
					$cacheTime = $currentTime;
1538
					$initial = true;
1539
					$log_error .= gettext("No Cached IPv6 found.");
1540
				}
1541
			} else {
1542
				if (file_exists($this->_cacheFile)) {
1543
					$contents = file_get_contents($this->_cacheFile);
1544
					list($cacheIP, $cacheTime) = explode(':', $contents);
1545
					$this->_debug($cacheIP.'/'.$cacheTime);
1546
					$initial = false;
1547
					$log_error .= sprintf(gettext("Cached IP: %s"), $cacheIP);
1548
				} else {
1549
					conf_mount_rw();
1550
					$cacheIP = '0.0.0.0';
1551
					@file_put_contents($this->_cacheFile, "0.0.0.0:{$currentTime}");
1552
					conf_mount_ro();
1553
					$cacheTime = $currentTime;
1554
					$initial = true;
1555
					$log_error .= gettext("No Cached IP found.");
1556
				}
1557
			}
1558
			if ($this->_dnsVerboseLog) {
1559
				log_error($log_error);
1560
			}
1561

    
1562
			// Convert seconds = days * hr/day * min/hr * sec/min
1563
			$maxCacheAgeSecs = $this->_dnsMaxCacheAgeDays * 24 * 60 * 60;
1564

    
1565
			$needs_updating = FALSE;
1566
			/* lets determine if the item needs updating */
1567
			if ($cacheIP != $wan_ip) {
1568
				$needs_updating = true;
1569
				$update_reason = gettext("Dynamic Dns: cacheIP != wan_ip. Updating.") . " ";
1570
				$update_reason .= sprintf(gettext('Cached IP: %1$s WAN IP: %2$s'), $cacheIP, $wan_ip) . " ";
1571
			}
1572
			if (($currentTime - $cacheTime) > $maxCacheAgeSecs) {
1573
				$needs_updating = true;
1574
				$this->_forceUpdateNeeded = true;
1575
				$update_reason = sprintf(gettext("Dynamic Dns: More than %s days. Updating."), $this->_dnsMaxCacheAgeDays);
1576
				$update_reason .= " {$currentTime} - {$cacheTime} > {$maxCacheAgeSecs} ";
1577
			}
1578
			if ($initial == true) {
1579
				$needs_updating = true;
1580
				$update_reason .= gettext("Initial update.");
1581
			}
1582

    
1583
			/*   finally if we need updating then store the
1584
			 *   new cache value and return true
1585
			 */
1586
			if ($needs_updating == true) {
1587
				if ($this->_dnsVerboseLog) {
1588
					log_error("DynDns ({$this->_FQDN}): {$update_reason}");
1589
				}
1590
				return true;
1591
			}
1592

    
1593
			return false;
1594
		}
1595

    
1596
		/*
1597
		 * Private Function (added 16 July 05) [beta]
1598
		 *   - Writes debug information to a file.
1599
		 *   - This function is only called when a unknown response
1600
		 *   - status is returned from a DynDNS service provider.
1601
		 */
1602
		function _debug($data) {
1603
			global $g;
1604

    
1605
			if (!$g['debug']) {
1606
				return;
1607
			}
1608
			$string = date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data."\n";
1609
			conf_mount_rw();
1610
			$file = fopen($this->_debugFile, 'a');
1611
			fwrite($file, $string);
1612
			fclose($file);
1613
			conf_mount_ro();
1614
		}
1615
		function _checkIP() {
1616
			global $debug;
1617

    
1618
			if ($debug) {
1619
				log_error(sprintf(gettext('Dynamic DNS %1$s (%2$s): _checkIP() starting.'), $this->_dnsService, $this->_FQDN));
1620
			}
1621

    
1622
			if ($this->_useIPv6 == true) {
1623
				$ip_address = get_interface_ipv6($this->_if);
1624
				if (!is_ipaddrv6($ip_address)) {
1625
					return 0;
1626
				}
1627
			} else {
1628
				$ip_address = get_interface_ip($this->_if);
1629
				if (!is_ipaddr($ip_address)) {
1630
					return 0;
1631
				}
1632
			}
1633
			if ($this->_useIPv6 == false && is_private_ip($ip_address)) {
1634
				$hosttocheck = "checkip.dyndns.org";
1635
				$try = 0;
1636
				while ($try < 3) {
1637
					$checkip = gethostbyname($hosttocheck);
1638
					if (is_ipaddr($checkip)) {
1639
						break;
1640
					}
1641
					$try++;
1642
				}
1643
				if ($try >= 3) {
1644
					log_error(sprintf(gettext('Dynamic DNS %1$s debug information (%2$s): Could not resolve %3$s to IP using interface IP %4$s.'), $this->_dnsService, $this->_FQDN, $hosttocheck, $ip_address));
1645
					return 0;
1646
				}
1647
				$ip_ch = curl_init("http://{$checkip}");
1648
				curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
1649
				curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
1650
				curl_setopt($ip_ch, CURLOPT_INTERFACE, 'host!' . $ip_address);
1651
				curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
1652
				curl_setopt($ip_ch, CURLOPT_TIMEOUT, 120);
1653
				if ($this->_useIPv6 == false) {
1654
					curl_setopt($ip_ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
1655
				}
1656
				$ip_result_page = curl_exec($ip_ch);
1657
				curl_close($ip_ch);
1658
				$ip_result_decoded = urldecode($ip_result_page);
1659
				preg_match('/Current IP Address: (.*)<\/body>/', $ip_result_decoded, $matches);
1660
				$ip_address = trim($matches[1]);
1661
				if (is_ipaddr($ip_address)) {
1662
					if ($this->_dnsVerboseLog) {
1663
						log_error(sprintf(gettext('Dynamic DNS %1$s (%2$s): %3$s extracted from %4$s'), $this->_dnsService, $this->_FQDN, $ip_address, $hosttocheck));
1664
					}
1665
				} else {
1666
					log_error(sprintf(gettext('Dynamic DNS %1$s (%2$s): IP address could not be extracted from %3$s'), $this->_dnsService, $this->_FQDN, $hosttocheck));
1667
					return 0;
1668
				}
1669
			} else {
1670
				if ($this->_dnsVerboseLog) {
1671
					log_error(sprintf(gettext('Dynamic DNS %1$s (%2$s): %3$s extracted from local system.'), $this->_dnsService, $this->_FQDN, $ip_address));
1672
				}
1673
			}
1674
			$this->_dnsIP = $ip_address;
1675

    
1676
			return $ip_address;
1677
		}
1678

    
1679
	}
1680

    
1681
?>
(16-16/65)