Project

General

Profile

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

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

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

    
146
			global $config, $g;
147

    
148
			$this->_cacheFile = "{$g['conf_path']}/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . "{$dnsID}.cache";
149
			$this->_cacheFile_v6 = "{$g['conf_path']}/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . "{$dnsID}_v6.cache";
150
			$this->_debugFile = "{$g['varetc_path']}/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . "{$dnsID}.debug";
151

    
152
			$this->_curlIpresolveV4 = $curlIpresolveV4;
153
			$this->_curlSslVerifypeer = $curlSslVerifypeer;
154
			$this->_dnsVerboseLog = $dnsVerboseLog;
155
			if ($this->_dnsVerboseLog) {
156
				log_error("DynDns: updatedns() starting");
157
			}
158

    
159
			$dyndnslck = lock("DDNS".$dnsID, LOCK_EX);
160

    
161
			if (!$dnsService) $this->_error(2);
162
			switch ($dnsService) {
163
			case 'freedns':
164
				if (!$dnsHost) $this->_error(5);
165
				break;
166
			case 'namecheap':
167
				if (!$dnsPass) $this->_error(4);
168
				if (!$dnsHost) $this->_error(5);
169
				break;
170
			case 'route53':
171
				if (!$dnsZoneID) $this->_error(8);
172
				if (!$dnsTTL) $this->_error(9);
173
				break;
174
			case 'custom':
175
				if (!$dnsUpdateURL) $this->_error(7);
176
				break;
177
			default:
178
				if (!$dnsUser) $this->_error(3);
179
				if (!$dnsPass) $this->_error(4);
180
				if (!$dnsHost) $this->_error(5);
181
			}
182

    
183
			switch ($dnsService) {
184
				case 'he-net-v6':
185
				case 'custom-v6':
186
				case 'spdns-v6':
187
					$this->_useIPv6 = true;
188
					break;
189
				default:
190
					$this->_useIPv6 = false;
191
			}
192
			$this->_dnsService = strtolower($dnsService);
193
			$this->_dnsUser = $dnsUser;
194
			$this->_dnsPass = $dnsPass;
195
			$this->_dnsHost = $dnsHost;
196
			$this->_dnsServer = $dnsServer;
197
			$this->_dnsPort = $dnsPort;
198
			$this->_dnsWildcard = $dnsWildcard;
199
			$this->_dnsMX = $dnsMX;
200
			$this->_dnsZoneID = $dnsZoneID;
201
			$this->_dnsTTL = $dnsTTL;
202
			$this->_if = get_failover_interface($dnsIf);
203
			$this->_checkIP();
204
			$this->_dnsUpdateURL = $dnsUpdateURL;
205
			$this->_dnsResultMatch = $dnsResultMatch;
206
			$this->_dnsRequestIf = get_failover_interface($dnsRequestIf);
207
			if ($this->_dnsVerboseLog) {
208
				log_error("DynDNS ({$this->_dnsHost}): running get_failover_interface for {$dnsRequestIf}. found {$this->_dnsRequestIf}");
209
			}
210
			$this->_dnsRequestIfIP = get_interface_ip($dnsRequestIf);
211
			$this->_dnsMaxCacheAgeDays = 25;
212
			$this->_dnsDummyUpdateDone = false;
213
			$this->_forceUpdateNeeded = $forceUpdate;
214

    
215
			// Ensure that we were able to lookup the IP
216
			if (!is_ipaddr($this->_dnsIP)) {
217
				log_error("DynDNS ({$this->_dnsHost}) There was an error trying to determine the public IP for interface - {$dnsIf}({$this->_if}). Probably interface is not a WAN interface.");
218
				unlock($dyndnslck);
219
				return;
220
			}
221

    
222
			$this->_debugID = rand(1000000, 9999999);
223

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

    
280
			unlock($dyndnslck);
281
		}
282

    
283
		/*
284
		 * Private Function (added 12 July 05) [beta]
285
		 *   Send Update To Selected Service.
286
		 */
287
		function _update() {
288

    
289
			if ($this->_dnsVerboseLog) {
290
				log_error("DynDNS ({$this->_dnsHost}): DynDns _update() starting.");
291
			}
292

    
293
			if (strstr($this->_dnsRequestIf, "_vip")) {
294
				$parentif = link_carp_interface_to_parent($this->_dnsRequestIf);
295
				$realparentif = convert_friendly_interface_to_real_interface_name($parentif);
296
			} else {
297
				$realparentif = $this->_dnsRequestIf;
298
			}
299

    
300
			$ch = curl_init();
301

    
302
			if ($this->_useIPv6 == false) {
303
				curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
304
			}
305

    
306
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53 ') {
307
				curl_setopt($ch, CURLOPT_HEADER, 0);
308
				curl_setopt($ch, CURLOPT_USERAGENT, $this->_UserAgent);
309
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
310
				curl_setopt($ch, CURLOPT_INTERFACE, 'if!' . $realparentif);
311
				curl_setopt($ch, CURLOPT_TIMEOUT, 120); // Completely empirical
312
			}
313

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

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

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

    
614
					/* Setting Variables */
615
					$hostname = "{$this->_dnsHost}.";
616
					$ZoneID = $this->_dnsZoneID;
617
					$AccessKeyId = $this->_dnsUser;
618
					$SecretAccessKey = $this->_dnsPass;
619
					$NewIP = $this->_dnsIP;
620
					$NewTTL = $this->_dnsTTL;
621

    
622
					/* Include Route 53 Library Class */
623
					require_once('/etc/inc/r53.class');
624

    
625
					/* Set Amazon AWS Credentials for this record */
626
					$r53 = new Route53($AccessKeyId, $SecretAccessKey);
627

    
628
					/* Function to find old values of records in Route 53 */
629
					if (!function_exists('Searchrecords')) {
630
						function SearchRecords($records, $name) {
631
							$result = array();
632
							foreach ($records as $record) {
633
								if (strtolower($record['Name']) == strtolower($name)) {
634
									$result [] = $record;
635
								}
636
							}
637
							return ($result) ? $result : false;
638
						}
639
					}
640

    
641
					$records = $r53->listResourceRecordSets("/hostedzone/$ZoneID");
642

    
643
					/* Get IP for your hostname in Route 53 */
644
					if (false !== ($a_result = SearchRecords($records['ResourceRecordSets'], "$hostname"))) {
645
						$OldTTL = $a_result[0][TTL];
646
						$OldIP = $a_result[0][ResourceRecords][0];
647
					} else {
648
						$OldIP = "";
649
					}
650

    
651
					/* Check if we need to update DNS Record */
652
					if ($OldIP !== $NewIP) {
653
						if (!empty($OldIP)) {
654
							/* Your Hostname already exists, deleting and creating it again */
655
							$changes = array();
656
							$changes[] = $r53->prepareChange(DELETE, $hostname, A, $OldTTL, $OldIP);
657
							$changes[] = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
658
							$result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
659
						} else {
660
							/* Your Hostname does not exist yet, creating it */
661
							$changes = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
662
							$result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
663
						}
664
					}
665
					$this->_checkStatus(0, $result);
666
					break;
667
				case 'custom':
668
				case 'custom-v6':
669
					if ($this->_dnsVerboseLog) {
670
						log_error("Custom DDNS ({$this->_dnsHost}): DNS update() starting.");
671
					}
672
					if (strstr($this->dnsUpdateURL, "%IP%")) {$needsIP = TRUE;} else {$needsIP = FALSE;}
673
					if ($this->_dnsUser != '') {
674
						if ($this->_curlIpresolveV4) {
675
							curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
676
						}
677
						if ($this->_curlSslVerifypeer) {
678
							curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
679
						} else {
680
							curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
681
						}
682
						curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
683
						curl_setopt($ch, CURLOPT_USERPWD, "{$this->_dnsUser}:{$this->_dnsPass}");
684
					}
685
					$server = str_replace("%IP%", $this->_dnsIP, $this->_dnsUpdateURL);
686
					if ($this->_dnsVerboseLog) {
687
						log_error("Sending request to: ".$server);
688
					}
689
					curl_setopt($ch, CURLOPT_URL, $server);
690
					break;
691
				case 'cloudflare':
692
					$needsIP = TRUE;
693
					$dnsServer ='api.cloudflare.com';
694
					$dnsHost = str_replace(' ', '', $this->_dnsHost);
695
					$host_names = explode(".", $dnsHost);
696
					$bottom_host_name = $host_names[count($host_names)-2] . "." . $host_names[count($host_names)-1];
697

    
698
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
699
					curl_setopt($ch, CURLOPT_HTTPHEADER, array(
700
						'X-Auth-Email: '.$this->_dnsUser.'',
701
						'X-Auth-Key: '.$this->_dnsPass.'',
702
						'Content-Type: application/json'
703
					));
704

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

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

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

    
1463
			if ($successful_update == true) {
1464
				/* Write WAN IP to cache file */
1465
				$wan_ip = $this->_checkIP();
1466
				conf_mount_rw();
1467
				if ($this->_useIPv6 == false && $wan_ip > 0) {
1468
					$currentTime = time();
1469
					notify_all_remote(sprintf(gettext("DynDNS updated IP Address on %s (%s) to %s"), convert_real_interface_to_friendly_descr($this->_if), $this->_if, $wan_ip));
1470
					log_error("phpDynDNS: updating cache file {$this->_cacheFile}: {$wan_ip}");
1471
					@file_put_contents($this->_cacheFile, "{$wan_ip}:{$currentTime}");
1472
				} else {
1473
					@unlink($this->_cacheFile);
1474
				}
1475
				if ($this->_useIPv6 == true && $wan_ip > 0) {
1476
					$currentTime = time();
1477
					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));
1478
					log_error("phpDynDNS: updating cache file {$this->_cacheFile_v6}: {$wan_ip}");
1479
					@file_put_contents($this->_cacheFile_v6, "{$wan_ip}|{$currentTime}");
1480
				} else {
1481
					@unlink($this->_cacheFile_v6);
1482
				}
1483
				conf_mount_ro();
1484
			}
1485
			$this->status = $status;
1486
			log_error($status);
1487
		}
1488

    
1489
		/*
1490
		 * Private Function (added 12 July 05) [beta]
1491
		 *   Return Error, Set Last Error, and Die.
1492
		 */
1493
		function _error($errorNumber = '1') {
1494
			switch ($errorNumber) {
1495
				case 0:
1496
					break;
1497
				case 2:
1498
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
1499
					break;
1500
				case 3:
1501
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
1502
					break;
1503
				case 4:
1504
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
1505
					break;
1506
				case 5:
1507
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
1508
					break;
1509
				case 6:
1510
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
1511
					break;
1512
				case 7:
1513
					$error = 'phpDynDNS: (ERROR!) No Update URL Provided.';
1514
					break;
1515
				case 8:
1516
					$status = "Route 53: (Error) Invalid ZoneID";
1517
					break;
1518
				case 9:
1519
					$status = "Route 53: (Error) Invalid TTL";
1520
					break;
1521
				case 10:
1522
					$error = "phpDynDNS ({$this->_dnsHost}): No change in my IP address and/or " . $this->_dnsMaxCacheAgeDays . " days has not passed. Not updating dynamic DNS entry.";
1523
					break;
1524
				default:
1525
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
1526
					/* FIXME: $data isn't in scope here */
1527
					/* $this->_debug($data); */
1528
					break;
1529
			}
1530
			$this->lastError = $error;
1531
			log_error($error);
1532
		}
1533

    
1534
		/*
1535
		 * Private Function (added 12 July 05) [beta]
1536
		 *   - Detect whether or not IP needs to be updated.
1537
		 *      | Written Specifically for pfSense (https://www.pfsense.org) may
1538
		 *      | work with other systems. pfSense base is FreeBSD.
1539
		 */
1540
		function _detectChange() {
1541
			global $debug;
1542

    
1543
			if ($debug) {
1544
				log_error("DynDns ({$this->_dnsHost}): _detectChange() starting.");
1545
			}
1546

    
1547
			$currentTime = time();
1548

    
1549
			$wan_ip = $this->_checkIP();
1550
			if ($wan_ip == 0) {
1551
				log_error("DynDns ({$this->_dnsHost}): Current WAN IP could not be determined, skipping update process.");
1552
				return false;
1553
			}
1554
			$log_error = "DynDns ({$this->_dnsHost}): Current WAN IP: {$wan_ip} ";
1555

    
1556
			if ($this->_useIPv6 == true) {
1557
				if (file_exists($this->_cacheFile_v6)) {
1558
					$contents = file_get_contents($this->_cacheFile_v6);
1559
					list($cacheIP, $cacheTime) = explode('|', $contents);
1560
					$this->_debug($cacheIP.'/'.$cacheTime);
1561
					$initial = false;
1562
					$log_error .= "Cached IPv6: {$cacheIP} ";
1563
				} else {
1564
					conf_mount_rw();
1565
					$cacheIP = '::';
1566
					@file_put_contents($this->_cacheFile, "::|{$currentTime}");
1567
					conf_mount_ro();
1568
					$cacheTime = $currentTime;
1569
					$initial = true;
1570
					$log_error .= "No Cached IPv6 found.";
1571
				}
1572
			} else {
1573
				if (file_exists($this->_cacheFile)) {
1574
					$contents = file_get_contents($this->_cacheFile);
1575
					list($cacheIP, $cacheTime) = explode(':', $contents);
1576
					$this->_debug($cacheIP.'/'.$cacheTime);
1577
					$initial = false;
1578
					$log_error .= "Cached IP: {$cacheIP} ";
1579
				} else {
1580
					conf_mount_rw();
1581
					$cacheIP = '0.0.0.0';
1582
					@file_put_contents($this->_cacheFile, "0.0.0.0:{$currentTime}");
1583
					conf_mount_ro();
1584
					$cacheTime = $currentTime;
1585
					$initial = true;
1586
					$log_error .= "No Cached IP found.";
1587
				}
1588
			}
1589
			if ($this->_dnsVerboseLog) {
1590
				log_error($log_error);
1591
			}
1592

    
1593
			// Convert seconds = days * hr/day * min/hr * sec/min
1594
			$maxCacheAgeSecs = $this->_dnsMaxCacheAgeDays * 24 * 60 * 60;
1595

    
1596
			$needs_updating = FALSE;
1597
			/* lets determine if the item needs updating */
1598
			if ($cacheIP != $wan_ip) {
1599
				$needs_updating = true;
1600
				$update_reason = "DynDns: cacheIP != wan_ip.  Updating. ";
1601
				$update_reason .= "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
1602
			}
1603
			if (($currentTime - $cacheTime) > $maxCacheAgeSecs) {
1604
				$needs_updating = true;
1605
				$this->_forceUpdateNeeded = true;
1606
				$update_reason = "DynDns: More than " . $this->_dnsMaxCacheAgeDays . " days.  Updating. ";
1607
				$update_reason .= "{$currentTime} - {$cacheTime} > {$maxCacheAgeSecs} ";
1608
			}
1609
			if ($initial == true) {
1610
				$needs_updating = true;
1611
				$update_reason .= "Initial update. ";
1612
			}
1613

    
1614
			/*   finally if we need updating then store the
1615
			 *   new cache value and return true
1616
			 */
1617
			if ($needs_updating == true) {
1618
				if ($this->_dnsVerboseLog) {
1619
					log_error("DynDns ({$this->_dnsHost}): {$update_reason}");
1620
				}
1621
				return true;
1622
			}
1623

    
1624
			return false;
1625
		}
1626

    
1627
		/*
1628
		 * Private Function (added 16 July 05) [beta]
1629
		 *   - Writes debug information to a file.
1630
		 *   - This function is only called when a unknown response
1631
		 *   - status is returned from a DynDNS service provider.
1632
		 */
1633
		function _debug($data) {
1634
			global $g;
1635

    
1636
			if (!$g['debug']) {
1637
				return;
1638
			}
1639
			$string = date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data."\n";
1640
			conf_mount_rw();
1641
			$file = fopen($this->_debugFile, 'a');
1642
			fwrite($file, $string);
1643
			fclose($file);
1644
			conf_mount_ro();
1645
		}
1646
		function _checkIP() {
1647
			global $debug;
1648

    
1649
			if ($debug) {
1650
				log_error("DynDns ({$this->_dnsHost}): _checkIP() starting.");
1651
			}
1652

    
1653
			if ($this->_useIPv6 == true) {
1654
				$ip_address = find_interface_ipv6($this->_if);
1655
				if (!is_ipaddrv6($ip_address)) {
1656
					return 0;
1657
				}
1658
			} else {
1659
				$ip_address = find_interface_ip($this->_if);
1660
				if (!is_ipaddr($ip_address)) {
1661
					return 0;
1662
				}
1663
			}
1664
			if ($this->_useIPv6 == false && is_private_ip($ip_address)) {
1665
				$hosttocheck = "checkip.dyndns.org";
1666
				$try = 0;
1667
				while ($try < 3) {
1668
					$checkip = gethostbyname($hosttocheck);
1669
					if (is_ipaddr($checkip)) {
1670
						break;
1671
					}
1672
					$try++;
1673
				}
1674
				if ($try >= 3) {
1675
					log_error("Dyndns debug information ({$this->_dnsHost}): Could not resolve {$hosttocheck} to IP using interface IP {$ip_address}.");
1676
					return 0;
1677
				}
1678
				$ip_ch = curl_init("http://{$checkip}");
1679
				curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
1680
				curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
1681
				curl_setopt($ip_ch, CURLOPT_INTERFACE, 'host!' . $ip_address);
1682
				curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
1683
				curl_setopt($ip_ch, CURLOPT_TIMEOUT, 120);
1684
				if ($this->_useIPv6 == false) {
1685
					curl_setopt($ip_ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
1686
				}
1687
				$ip_result_page = curl_exec($ip_ch);
1688
				curl_close($ip_ch);
1689
				$ip_result_decoded = urldecode($ip_result_page);
1690
				preg_match('/Current IP Address: (.*)<\/body>/', $ip_result_decoded, $matches);
1691
				$ip_address = trim($matches[1]);
1692
				if (is_ipaddr($ip_address)) {
1693
					if ($this->_dnsVerboseLog) {
1694
						log_error("DynDns ({$this->_dnsHost}): {$ip_address} extracted from {$hosttocheck}");
1695
					}
1696
				} else {
1697
					log_error("DynDns ({$this->_dnsHost}): IP address could not be extracted from {$hosttocheck}");
1698
					return 0;
1699
				}
1700
			} else {
1701
				if ($this->_dnsVerboseLog) {
1702
					log_error("DynDns ({$this->_dnsHost}): {$ip_address} extracted from local system.");
1703
				}
1704
			}
1705
			$this->_dnsIP = $ip_address;
1706

    
1707
			return $ip_address;
1708
		}
1709

    
1710
	}
1711

    
1712
?>
(16-16/65)