Project

General

Profile

Download (62.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
	 *    - DHSDns (dhs.org)
9
	 *    - No-IP (no-ip.com)
10
	 *    - EasyDNS (easydns.com)
11
	 *    - DHS (www.dhs.org)
12
	 *    - HN (hn.org) -- incomplete checking!
13
	 *    - DynS (dyns.org)
14
	 *    - ZoneEdit (zoneedit.com)
15
	 *    - FreeDNS (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
	 * +----------------------------------------------------+
37
	 *  Requirements:
38
	 *    - PHP version 4.0.2 or higher with the CURL Library and the PCRE Library
39
	 * +----------------------------------------------------+
40
	 *  Public Functions
41
	 *    - updatedns()
42
	 *
43
	 *  Private Functions
44
	 *    - _update()
45
	 *    - _checkStatus()
46
	 *    - _error()
47
	 *    - _detectChange()
48
	 *    - _debug()
49
	 *    - _checkIP()
50
	 * +----------------------------------------------------+
51
	 *  DynDNS Dynamic  - Last Tested: 12 July 2005
52
	 *  DynDNS Static   - Last Tested: NEVER
53
	 *  DynDNS Custom   - Last Tested: NEVER
54
	 *  No-IP           - Last Tested: 20 July 2008
55
	 *  HN.org          - Last Tested: 12 July 2005
56
	 *  EasyDNS         - Last Tested: 20 July 2008
57
	 *  DHS             - Last Tested: 12 July 2005
58
	 *  ZoneEdit        - Last Tested: NEVER
59
	 *  Dyns            - Last Tested: NEVER
60
	 *  ODS             - Last Tested: 02 August 2005
61
	 *  FreeDNS         - Last Tested: 23 Feb 2011
62
	 *  Loopia          - Last Tested: NEVER
63
	 *  StaticCling     - Last Tested: 27 April 2006
64
	 *  DNSexit         - Last Tested: 20 July 2008
65
	 *  OpenDNS         - Last Tested: 4 August 2008
66
	 *  Namecheap       - Last Tested: 31 August 2010
67
	 *  HE.net          - Last Tested: 7 July 2013
68
	 *  HE.net IPv6     - Last Tested: 7 July 2013
69
	 *  HE.net Tunnel   - Last Tested: 28 June 2011
70
	 *  SelfHost        - Last Tested: 26 December 2011
71
	 *  Amazon Route 53 - Last tested: 01 April 2012
72
	 *  DNS-O-Matic     - Last Tested: 9 September 2010
73
	 *  CloudFlare      - Last Tested: 30 May 2013
74
	 *  Eurodns         - Last Tested: 27 June 2013
75
	 *  GratisDNS       - Last Tested: 15 August 2012
76
	 *  OVH DynHOST     - Last Tested: NEVER
77
	 *  City Network    - Last Tested: 13 November 2013
78
	 *  GleSYS          - Last Tested: 3 February 2015
79
	 *  DNSimple        - Last Tested: 09 February 2015
80
	 *  Google Domains  - Last Tested: 27 April 2015
81
	 * +====================================================+
82
	 *
83
	 * @author 	E.Kristensen
84
	 * @link    	http://www.idylldesigns.com/projects/phpdns/
85
	 * @version 	0.8
86
	 * @updated	13 October 05 at 21:02:42 GMT
87
	 *
88
	 * DNSexit/OpenDNS support and multiwan extension for pfSense by Ermal Luci
89
	 * Custom DNS support by Matt Corallo
90
	 *
91
	 */
92

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

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

    
141
			global $config, $g;
142

    
143
			$this->_cacheFile = "{$g['conf_path']}/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . "{$dnsID}.cache";
144
			$this->_cacheFile_v6 = "{$g['conf_path']}/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . "{$dnsID}_v6.cache";
145
			$this->_debugFile = "{$g['varetc_path']}/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . "{$dnsID}.debug";
146

    
147
			$this->_curlIpresolveV4 = $curlIpresolveV4;
148
			$this->_curlSslVerifypeer = $curlSslVerifypeer;
149
			$this->_dnsVerboseLog = $dnsVerboseLog;
150
			if ($this->_dnsVerboseLog) {
151
				log_error("DynDns: updatedns() starting");
152
			}
153

    
154
			$dyndnslck = lock("DDNS".$dnsID, LOCK_EX);
155

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

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

    
209
			// Ensure that we were able to lookup the IP
210
			if (!is_ipaddr($this->_dnsIP)) {
211
				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.");
212
				unlock($dyndnslck);
213
				return;
214
			}
215

    
216
			$this->_debugID = rand(1000000, 9999999);
217

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

    
271
			unlock($dyndnslck);
272
		}
273

    
274
		/*
275
		 * Private Function (added 12 July 05) [beta]
276
		 *   Send Update To Selected Service.
277
		 */
278
		function _update() {
279

    
280
			if ($this->_dnsVerboseLog) {
281
				log_error("DynDNS ({$this->_dnsHost}): DynDns _update() starting.");
282
			}
283

    
284
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53 ') {
285
				$ch = curl_init();
286
				curl_setopt($ch, CURLOPT_HEADER, 0);
287
				curl_setopt($ch, CURLOPT_USERAGENT, $this->_UserAgent);
288
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
289
				curl_setopt($ch, CURLOPT_INTERFACE, $this->_dnsRequestIfIP);
290
				curl_setopt($ch, CURLOPT_TIMEOUT, 120); // Completely empirical
291
			}
292

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

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

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

    
592
					/* Setting Variables */
593
					$hostname = "{$this->_dnsHost}.";
594
					$ZoneID = $this->_dnsZoneID;
595
					$AccessKeyId=$this->_dnsUser;
596
					$SecretAccessKey=$this->_dnsPass;
597
					$NewIP=$this->_dnsIP;
598
					$NewTTL=$this->_dnsTTL;
599

    
600
					/* Include Route 53 Library Class */
601
					require_once('/etc/inc/r53.class');
602

    
603
					/* Set Amazon AWS Credentials for this record */
604
					$r53 = new Route53($AccessKeyId, $SecretAccessKey);
605

    
606
					/* Function to find old values of records in Route 53 */
607
					if (!function_exists('Searchrecords')) {
608
						function SearchRecords($records, $name) {
609
							$result = array();
610
							foreach ($records as $record) {
611
								if (strtolower($record['Name']) == strtolower($name)) {
612
									$result [] = $record;
613
								}
614
							}
615
							return ($result) ? $result : false;
616
						}
617
					}
618

    
619
					$records = $r53->listResourceRecordSets("/hostedzone/$ZoneID");
620

    
621
					/* Get IP for your hostname in Route 53 */
622
					if (false !== ($a_result = SearchRecords($records['ResourceRecordSets'], "$hostname"))) {
623
						$OldTTL=$a_result[0][TTL];
624
						$OldIP=$a_result[0][ResourceRecords][0];
625
					} else {
626
						$OldIP="";
627
					}
628

    
629
					/* Check if we need to update DNS Record */
630
					if ($OldIP !== $NewIP) {
631
						if (!empty($OldIP)) {
632
							/* Your Hostname already exists, deleting and creating it again */
633
							$changes = array();
634
							$changes[] = $r53->prepareChange(DELETE, $hostname, A, $OldTTL, $OldIP);
635
							$changes[] = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
636
							$result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
637
						} else {
638
							/* Your Hostname does not exist yet, creating it */
639
							$changes = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
640
							$result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
641
						}
642
					}
643
					$this->_checkStatus(0, $result);
644
					break;
645
				case 'custom':
646
				case 'custom-v6':
647
					if ($this->_dnsVerboseLog) {
648
						log_error("Custom DDNS ({$this->_dnsHost}): DNS update() starting.");
649
					}
650
					if (strstr($this->dnsUpdateURL, "%IP%")) {$needsIP = TRUE;} else {$needsIP = FALSE;}
651
					if ($this->_dnsUser != '') {
652
						if ($this->_curlIpresolveV4) {
653
							curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
654
						}
655
						if ($this->_curlSslVerifypeer) {
656
							curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
657
						} else {
658
							curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
659
						}
660
						curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
661
						curl_setopt($ch, CURLOPT_USERPWD, "{$this->_dnsUser}:{$this->_dnsPass}");
662
					}
663
					$server = str_replace("%IP%", $this->_dnsIP, $this->_dnsUpdateURL);
664
					if ($this->_dnsVerboseLog) {
665
						log_error("Sending request to: ".$server);
666
					}
667
					curl_setopt($ch, CURLOPT_URL, $server);
668
					break;
669
				case 'cloudflare':
670
					$needsIP = TRUE;
671
					$dnsServer ='www.cloudflare.com';
672
					$dnsHost = str_replace(' ','', $this->_dnsHost);
673
					$URL = "https://{$dnsServer}/api.html?a=DIUP&email={$this->_dnsUser}&tkn={$this->_dnsPass}&ip={$this->_dnsIP}&hosts={$dnsHost}";
674
					curl_setopt($ch, CURLOPT_URL, $URL);
675
					break;
676
				case 'eurodns':
677
					$needsIP = TRUE;
678
					if ($this->_dnsVerboseLog) {
679
						log_error("EuroDynDns ({$this->_dnsHost}) DNS update() starting.");
680
					}
681
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
682
					$server = "https://update.eurodyndns.org/update/";
683
					$port = "";
684
					if ($this->_dnsPort) {
685
						$port = ":" . $this->_dnsPort;
686
					}
687
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP);
688
					break;
689
				case 'gratisdns':
690
					$needsIP = FALSE;
691
					if ($this->_dnsVerboseLog) {
692
						log_error("GratisDNS.dk ({$this->_dnsHost}): DNS update() starting.");
693
					}
694
					$server = "https://ssl.gratisdns.dk/ddns.phtml";
695
					list($hostname, $domain) = explode(".", $this->_dnsHost, 2);
696
					curl_setopt($ch, CURLOPT_URL, $server . '?u=' . $this->_dnsUser . '&p=' . $this->_dnsPass . '&h=' . $this->_dnsHost . '&d=' . $domain);
697
					break;
698
				case 'ovh-dynhost':
699
					$needsIP = FALSE;
700
					if ($this->_dnsVerboseLog) {
701
						log_error("OVH DynHOST: ({$this->_dnsHost}) DNS update() starting.");
702
					}
703
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
704
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
705
					$server = "https://www.ovh.com/nic/update";
706
					$port = "";
707
					if ($this->_dnsServer) {
708
						$server = $this->_dnsServer;
709
					}
710
					if ($this->_dnsPort) {
711
						$port = ":" . $this->_dnsPort;
712
					}
713
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
714
					break;
715
				case 'citynetwork':
716
					$needsIP = TRUE;
717
					if ($this->_dnsVerboseLog) {
718
						log_error("City Network: ({$this->_dnsHost}) DNS update() starting.");
719
					}
720
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
721
					$server = 'https://dyndns.citynetwork.se/nic/update';
722
					$port = "";
723
					if ($this->_dnsServer) {
724
						$server = $this->_dnsServer;
725
					}
726
					if ($this->_dnsPort) {
727
						$port = ":" . $this->_dnsPort;
728
					}
729
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP);
730
					break;
731
				case 'dnsimple':
732
					/* Uses DNSimple's REST API
733
					   Requires username and Account API token passed in header
734
					   Piggybacks on Route 53's ZoneID field for DNSimple record ID
735
					   Data sent as JSON */
736
					$needsIP = TRUE;
737
					$server = 'https://api.dnsimple.com/v1/domains/';
738
					$token = $this->_dnsUser . ':' . $this->_dnsPass;
739
					$jsondata = '{"record":{"content":"' . $this->_dnsIP . '","ttl":"' . $this->_dnsTTL . '"}}';
740
					curl_setopt($ch, CURLOPT_HEADER, 1);
741
					curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
742
					curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json','Content-Type: application/json','X-DNSimple-Token: ' . $token));
743
					curl_setopt($ch, CURLOPT_URL, $server . $this->_dnsHost . '/records/' . $this->_dnsZoneID);
744
					curl_setopt($ch, CURLOPT_POSTFIELDS, $jsondata);
745
					break;
746
                case 'googledomains':
747
					$needsIP = FALSE;
748
					if ($this->_dnsVerboseLog) {
749
						log_error("Google Domains: ({$this->_dnsHost}) DNS update() starting.");
750
					}
751
					$post_data['username:password'] = $this->_dnsUser . ':' . $this->_dnsPass;
752
					$post_data['hostname'] = $this->_dnsHost;
753
					$post_data['myip'] = $this->_dnsIP;
754
					$post_data['offline'] = 'no';
755

    
756
                    $server = "https://domains.google.com/nic/update";
757
					$port = "";
758
                    curl_setopt($ch, CURLOPT_SSLVERSION, 6);
759
                    curl_setopt($ch, CURLOPT_USERAGENT, 'Chrome/41.0');
760
					curl_setopt($ch, CURLOPT_URL, 'https://domains.google.com/nic/update');
761
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
762
					curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
763
					break;
764
				default:
765
					break;
766
			}
767
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53') {
768
				$data = curl_exec($ch);
769
				$this->_checkStatus($ch, $data);
770
				@curl_close($ch);
771
			}
772
		}
773

    
774
		/*
775
		 * Private Function (added 12 July 2005) [beta]
776
		 *   Retrieve Update Status
777
		 */
778
		function _checkStatus($ch, $data) {
779
			if ($this->_dnsVerboseLog) {
780
				log_error("DynDNS ({$this->_dnsHost}): DynDns _checkStatus() starting.");
781
				log_error("DynDNS ({$this->_dnsHost}): Current Service: {$this->_dnsService}");
782
			}
783
			$successful_update = false;
784
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53' && @curl_error($ch)) {
785
				$status = "Curl error occurred: " . curl_error($ch);
786
				log_error($status);
787
				$this->status = $status;
788
				return;
789
			}
790
			switch ($this->_dnsService) {
791
				case 'glesys':
792
					if (preg_match('/Record updated/i', $data)) {
793
						$status = "GleSYS ({$this->_dnsHost}): (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
794
						$successful_update = true;
795
					} else {
796
						$status = "GleSYS ({$this->_dnsHost}): (Unknown Response)";
797
						log_error("GleSYS ({$this->_dnsHost}): PAYLOAD: {$data}");
798
						$this->_debug($data);
799
					}
800
					break;
801
				case 'dnsomatic':
802
					if (preg_match('/badauth/i', $data)) {
803
						$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.";
804
					} else if (preg_match('/notfqdn /i', $data)) {
805
						$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.";
806
					} else if (preg_match('/nohost/i', $data)) {
807
						$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.";
808
					} else if (preg_match('/numhost/i', $data)) {
809
						$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.";
810
					} else if (preg_match('/abuse/i', $data)) {
811
						$status = "DNS-O-Matic ({$this->_dnsHost}): The hostname is blocked for update abuse.";
812
					} else if (preg_match('/good/i', $data)) {
813
						$status = "DNS-O-Matic ({$this->_dnsHost}): (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
814
						$successful_update = true;
815
					} else if (preg_match('/dnserr/i', $data)) {
816
						$status = "DNS-O-Matic ({$this->_dnsHost}): DNS error encountered. Stop updating for 30 minutes.";
817
					} else {
818
						$status = "DNS-O-Matic ({$this->_dnsHost}): (Unknown Response)";
819
						log_error("DNS-O-Matic ({$this->_dnsHost}): PAYLOAD: {$data}");
820
						$this->_debug($data);
821
					}
822
					break;
823
				case 'citynetwork':
824
					if (preg_match('/notfqdn/i', $data)) {
825
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Not A FQDN!";
826
					} else if (preg_match('/nohost/i', $data)) {
827
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) No such host";
828
					} else if (preg_match('/nochg/i', $data)) {
829
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) No Change In IP Address";
830
						$successful_update = true;
831
					} else if (preg_match('/good/i', $data)) {
832
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
833
						$successful_update = true;
834
					} else if (preg_match('/badauth/i', $data)) {
835
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) User Authorization Failed";
836
					} else {
837
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
838
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
839
						$this->_debug($data);
840
					}
841
					break;
842
				case 'ovh-dynhost':
843
				case 'dyndns':
844
					if (preg_match('/notfqdn/i', $data)) {
845
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Not A FQDN!";
846
					} else if (preg_match('/nochg/i', $data)) {
847
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) No Change In IP Address";
848
						$successful_update = true;
849
					} else if (preg_match('/good/i', $data)) {
850
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
851
						$successful_update = true;
852
					} else if (preg_match('/noauth/i', $data)) {
853
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) User Authorization Failed";
854
					} else {
855
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
856
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
857
						$this->_debug($data);
858
					}
859
					break;
860
				case 'dyndns-static':
861
					if (preg_match('/notfqdn/i', $data)) {
862
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Not A FQDN!";
863
					} else if (preg_match('/nochg/i', $data)) {
864
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) No Change In IP Address";
865
						$successful_update = true;
866
					} else if (preg_match('/good/i', $data)) {
867
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully!";
868
						$successful_update = true;
869
					} else if (preg_match('/noauth/i', $data)) {
870
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) User Authorization Failed";
871
					} else {
872
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
873
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
874
						$this->_debug($data);
875
					}
876
					break;
877
				case 'dyndns-custom':
878
					if (preg_match('/notfqdn/i', $data)) {
879
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Not A FQDN!";
880
					} else if (preg_match('/nochg/i', $data)) {
881
						$status = "phpDynDNS: (Success) No Change In IP Address";
882
						$successful_update = true;
883
					} else if (preg_match('/good/i', $data)) {
884
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully!";
885
						$successful_update = true;
886
					} else if (preg_match('/noauth/i', $data)) {
887
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) User Authorization Failed";
888
					} else {
889
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
890
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
891
						$this->_debug($data);
892
					}
893
					break;
894
				case 'dhs':
895
					break;
896
				case 'noip':
897
				case 'noip-free':
898
					list($ip,$code) = explode(":",$data);
899
					switch ($code) {
900
						case 0:
901
							$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP address is current, no update performed.";
902
							$successful_update = true;
903
							break;
904
						case 1:
905
							$status = "phpDynDNS ({$this->_dnsHost}): (Success) DNS hostname update successful.";
906
							$successful_update = true;
907
							break;
908
						case 2:
909
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Hostname supplied does not exist.";
910
							break;
911
						case 3:
912
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Invalid Username.";
913
							break;
914
						case 4:
915
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Invalid Password.";
916
							break;
917
						case 5:
918
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) To many updates sent.";
919
							break;
920
						case 6:
921
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Account disabled due to violation of No-IP terms of service.";
922
							break;
923
						case 7:
924
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Invalid IP. IP Address submitted is improperly formatted or is a private IP address or is on a blacklist.";
925
							break;
926
						case 8:
927
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Disabled / Locked Hostname.";
928
							break;
929
						case 9:
930
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Host updated is configured as a web redirect and no update was performed.";
931
							break;
932
						case 10:
933
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Group supplied does not exist.";
934
							break;
935
						case 11:
936
							$status = "phpDynDNS ({$this->_dnsHost}): (Success) DNS group update is successful.";
937
							$successful_update = true;
938
							break;
939
						case 12:
940
							$status = "phpDynDNS ({$this->_dnsHost}): (Success) DNS group is current, no update performed.";
941
							$successful_update = true;
942
							break;
943
						case 13:
944
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Update client support not available for supplied hostname or group.";
945
							break;
946
						case 14:
947
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Hostname supplied does not have offline settings configured.";
948
							break;
949
						case 99:
950
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
951
							break;
952
						case 100:
953
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
954
							break;
955
						default:
956
							$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
957
							$this->_debug("Unknown Response: ".$data);
958
							break;
959
					}
960
					break;
961
				case 'easydns':
962
					if (preg_match('/NOACCESS/i', $data)) {
963
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Authentication Failed: Username and/or Password was Incorrect.";
964
					} else if (preg_match('/NOSERVICE/i', $data)) {
965
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) No Service: Dynamic DNS Service has been disabled for this domain.";
966
					} else if (preg_match('/ILLEGAL INPUT/i', $data)) {
967
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Illegal Input: Self-Explanatory";
968
					} else if (preg_match('/TOOSOON/i', $data)) {
969
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Too Soon: Not Enough Time Has Elapsed Since Last Update";
970
					} else if (preg_match('/NOERROR/i', $data)) {
971
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Updated Successfully!";
972
						$successful_update = true;
973
					} else {
974
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
975
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
976
						$this->_debug($data);
977
					}
978
					break;
979
				case 'hn':
980
					/* FIXME: add checks */
981
					break;
982
				case 'zoneedit':
983
					if (preg_match('/799/i', $data)) {
984
						$status = "phpDynDNS ({$this->_dnsHost}): (Error 799) Update Failed!";
985
					} else if (preg_match('/700/i', $data)) {
986
						$status = "phpDynDNS ({$this->_dnsHost}): (Error 700) Update Failed!";
987
					} else if (preg_match('/200/i', $data)) {
988
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
989
						$successful_update = true;
990
					} else if (preg_match('/201/i', $data)) {
991
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
992
						$successful_update = true;
993
					} else {
994
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
995
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
996
						$this->_debug($data);
997
					}
998
					break;
999
				case 'dyns':
1000
					if (preg_match("/400/i", $data)) {
1001
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Bad Request - The URL was malformed. Required parameters were not provided.";
1002
					} else if (preg_match('/402/i', $data)) {
1003
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Update Too Soon - You have tried updating to quickly since last change.";
1004
					} else if (preg_match('/403/i', $data)) {
1005
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Database Error - There was a server-sided database error.";
1006
					} else if (preg_match('/405/i', $data)) {
1007
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Hostname Error - The hostname (".$this->_dnsHost.") doesn't belong to you.";
1008
					} else if (preg_match('/200/i', $data)) {
1009
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
1010
						$successful_update = true;
1011
					} else {
1012
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
1013
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
1014
						$this->_debug($data);
1015
					}
1016
					break;
1017
				case 'ods':
1018
					if (preg_match("/299/i", $data)) {
1019
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
1020
						$successful_update = true;
1021
					} else {
1022
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
1023
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
1024
						$this->_debug($data);
1025
					}
1026
					break;
1027
				case 'freedns':
1028
					if (preg_match("/has not changed./i", $data)) {
1029
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) No Change In IP Address";
1030
						$successful_update = true;
1031
					} else if (preg_match("/Updated/i", $data)) {
1032
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully!";
1033
						$successful_update = true;
1034
					} else {
1035
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
1036
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
1037
						$this->_debug($data);
1038
					}
1039
					break;
1040
				case 'dnsexit':
1041
					if (preg_match("/is the same/i", $data)) {
1042
						$status = "phpDynDns ({$this->_dnsHost}): (Success) No Change In IP Address";
1043
						$successful_update = true;
1044
					} else if (preg_match("/Success/i", $data)) {
1045
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully!";
1046
						$successful_update = true;
1047
					} else {
1048
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
1049
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
1050
						$this->_debug($data);
1051
					}
1052
					break;
1053
				case 'loopia':
1054
					if (preg_match("/nochg/i", $data)) {
1055
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) No Change In IP Address";
1056
						$successful_update = true;
1057
					} else if (preg_match("/good/i", $data)) {
1058
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully!";
1059
						$successful_update = true;
1060
					} else if (preg_match('/badauth/i', $data)) {
1061
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) User Authorization Failed";
1062
					} else {
1063
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
1064
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
1065
						$this->_debug($data);
1066
					}
1067
					break;
1068
				case 'opendns':
1069
					if (preg_match('/badauth/i', $data)) {
1070
						$status = "phpDynDNS({$this->_dnsHost}): (Error) Not a valid username or password!";
1071
					} else if (preg_match('/nohost/i', $data)) {
1072
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Hostname you are trying to update does not exist.";
1073
						$successful_update = true;
1074
					} else if (preg_match('/good/i', $data)) {
1075
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
1076
						$successful_update = true;
1077
					} else if (preg_match('/yours/i', $data)) {
1078
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) hostname specified exists, but not under the username specified.";
1079
					} else if (preg_match('/abuse/i', $data)) {
1080
						$status = "phpDynDns ({$this->_dnsHost}): (Error) Updating too frequently, considered abuse.";
1081
					} else {
1082
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
1083
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
1084
						$this->_debug($data);
1085
					}
1086
					break;
1087
				case 'staticcling':
1088
					if (preg_match("/invalid ip/i", $data)) {
1089
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Bad Request - The IP provided was invalid.";
1090
					} else if (preg_match('/required info missing/i', $data)) {
1091
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Bad Request - Required parameters were not provided.";
1092
					} else if (preg_match('/invalid characters/i', $data)) {
1093
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Bad Request - Illegal characters in either the username or the password.";
1094
					} else if (preg_match('/bad password/i', $data)) {
1095
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Invalid password.";
1096
					} else if (preg_match('/account locked/i', $data)) {
1097
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) This account has been administratively locked.";
1098
					} else if (preg_match('/update too frequent/i', $data)) {
1099
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Updating too frequently.";
1100
					} else if (preg_match('/DB error/i', $data)) {
1101
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Server side error.";
1102
					} else if (preg_match('/success/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 'namecheap':
1112
					$tmp = str_replace("^M", "", $data);
1113
					$ncresponse = @xml2array($tmp);
1114
					if (preg_match("/internal server error/i", $data)) {
1115
						$status = "phpDynDNS: (Error) Server side error.";
1116
					} else if (preg_match("/request is badly formed/i", $data)) {
1117
						$status = "phpDynDNS: (Error) Badly Formed Request (check your settings).";
1118
					} else if ($ncresponse['interface-response']['ErrCount'] === "0") {
1119
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
1120
						$successful_update = true;
1121
					} else if (is_numeric($ncresponse['interface-response']['ErrCount']) && ($ncresponse['interface-response']['ErrCount'] > 0)) {
1122
						$status = "phpDynDNS: (Error) " . implode(", ", $ncresponse["interface-response"]["errors"]);
1123
						$successful_update = true;
1124
					} else {
1125
						$status = "phpDynDNS: (Unknown Response)";
1126
						log_error("phpDynDNS: PAYLOAD: {$data}");
1127
						$this->_debug($data);
1128
					}
1129
					break;
1130

    
1131
				case 'he-net':
1132
				case 'he-net-v6':
1133
					if (preg_match("/badip/i", $data)) {
1134
						$status = "phpDynDNS: (Error) Bad Request - The IP provided was invalid.";
1135
					} else if (preg_match('/nohost/i', $data)) {
1136
						$status = "phpDynDNS: (Error) Bad Request - A hostname was not provided.";
1137
					} else if (preg_match('/badauth/i', $data)) {
1138
						$status = "phpDynDNS: (Error) Invalid username or password.";
1139
					} else if (preg_match('/good/i', $data)) {
1140
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
1141
						$successful_update = true;
1142
					} else if (preg_match('/nochg/i', $data)) {
1143
						$status = "phpDynDNS: (Success) No Change In IP Address.";
1144
						$successful_update = true;
1145
					} else {
1146
						$status = "phpDynDNS: (Unknown Response)";
1147
						log_error("phpDynDNS: PAYLOAD: {$data}");
1148
						$this->_debug($data);
1149
					}
1150
					break;
1151
				case 'he-net-tunnelbroker':
1152
					/*
1153
					-ERROR: Missing parameter(s).
1154
					-ERROR: Invalid API key or password
1155
					-ERROR: Tunnel not found
1156
					-ERROR: Another tunnel exists for this IP.
1157
					-ERROR: This tunnel is already associated with this IP address
1158
					+OK: Tunnel endpoint updated to: x.x.x.x
1159
					*/
1160
					if (preg_match("/Missing parameter/i", $data)) {
1161
						$status = "phpDynDNS: (Error) Bad Request - Missing/Invalid Parameters.";
1162
					} else if (preg_match('/Tunnel not found/i', $data)) {
1163
						$status = "phpDynDNS: (Error) Bad Request - Invalid Tunnel ID.";
1164
					} else if (preg_match('/Invalid API key or password/i', $data)) {
1165
						$status = "phpDynDNS: (Error) Invalid username or password.";
1166
					} else if (preg_match('/OK:/i', $data)) {
1167
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
1168
						$successful_update = true;
1169
					} else if (preg_match('/This tunnel is already associated with this IP address/i', $data)) {
1170
						$status = "phpDynDNS: (Success) No Change In IP Address.";
1171
						$successful_update = true;
1172
					} else {
1173
						$status = "phpDynDNS: (Unknown Response)";
1174
						log_error("phpDynDNS: PAYLOAD: {$data}");
1175
						$this->_debug($data);
1176
					}
1177
					break;
1178
				case 'selfhost':
1179
					if (preg_match('/notfqdn/i', $data)) {
1180
						$status = "phpDynDNS: (Error) Not A FQDN!";
1181
					} else if (preg_match('/nochg/i', $data)) {
1182
						$status = "phpDynDNS: (Success) No Change In IP Address";
1183
						$successful_update = true;
1184
					} else if (preg_match('/good/i', $data)) {
1185
						$status = "phpDynDNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
1186
						$successful_update = true;
1187
					} else if (preg_match('/noauth/i', $data)) {
1188
						$status = "phpDynDNS: (Error) User Authorization Failed";
1189
					} else {
1190
						$status = "phpDynDNS: (Unknown Response)";
1191
						log_error("phpDynDNS: PAYLOAD: {$data}");
1192
						$this->_debug($data);
1193
					}
1194
					break;
1195
				case 'route53':
1196
					$successful_update = true;
1197
					break;
1198
				case 'custom':
1199
				case 'custom-v6':
1200
					$successful_update = false;
1201
					if ($this->_dnsResultMatch == "") {
1202
						$successful_update = true;
1203
					} else {
1204
						$this->_dnsResultMatch = str_replace("%IP%", $this->_dnsIP, $this->_dnsResultMatch);
1205
						$matches = preg_split("/(?<!\\\\)\\|/", $this->_dnsResultMatch);
1206
						foreach ($matches as $match) {
1207
							$match= str_replace("\\|", "|", $match);
1208
							if (strcmp($match, trim($data, "\t\n\r")) == 0) {
1209
								$successful_update = true;
1210
							}
1211
						}
1212
						unset ($matches);
1213
					}
1214
					if ($successful_update == true) {
1215
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
1216
					} else {
1217
						$status = "phpDynDNS: (Error) Result did not match.";
1218
					}
1219
					break;
1220
				case 'cloudflare':
1221
					// receive multiple results
1222
					$data = explode("\n",$data);
1223
					$lines = count($data)-1;
1224

    
1225
					// loop over the lines
1226
					for ($pos=0; ($successful_update || $pos == 0) && $pos < $lines; $pos++) {
1227
						$resp = $data[$pos];
1228
						if (preg_match('/UAUTH/i', $resp)) {
1229
							$status = "DynDNS: The username specified is not authorized to update this hostname and domain.";
1230
						} else if (preg_match('/NOHOST/i', $resp)) {
1231
							$status = "DynDNS: No valid FQDN (fully qualified domain name) was specified";
1232
						} else if (preg_match('/INVLDHST/i', $resp)) {
1233
							$status = "DynDNS: An invalid hostname was specified. This may be due to the fact the hostname has not been created in the system. Creating new host names via clients is not supported.";
1234
						} else if (preg_match('/INVLDIP/i', $resp)) {
1235
							$status = "DynDNS: The IP address given is not valid.";
1236
						} else if (preg_match('/DUPHST/i', $resp)) {
1237
							$status = "DynDNS: Duplicate values exist for a record. Only single values for records are supported currently.";
1238
						} else if (preg_match('/NOUPDATE/i', $resp)) {
1239
							$status = "DynDNS: No changes made to the hostname (".strtok($resp,' ')."). Continual updates with no changes lead to blocked clients.";
1240
							$successful_update = true; //success if it is the same so that it saves
1241
						} else if (preg_match('/OK/i', $resp)) {
1242
							$status = "DynDNS: (Success) (".strtok($resp,' ').") IP Address for Changed Successfully!";
1243
							$successful_update = true;
1244
						} else {
1245
							$status = "DynDNS: (Unknown Response)";
1246
							log_error("DynDNS: PAYLOAD: {$resp}");
1247
							$this->_debug($resp);
1248
						}
1249
						log_error($status);
1250
					}
1251
					break;
1252
				case 'eurodns':
1253
					if (preg_match('/notfqdn/i', $data)) {
1254
						$status = "phpDynDNS: (Error) Not A FQDN!";
1255
					} else if (preg_match('/nochg/i', $data)) {
1256
						$status = "phpDynDNS: (Success) No Change In IP Address";
1257
						$successful_update = true;
1258
					} else if (preg_match('/good/i', $data)) {
1259
						$status = "phpDynDNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
1260
						$successful_update = true;
1261
					} else if (preg_match('/badauth/i', $data)) {
1262
						$status = "phpDynDNS: (Error) User Authorization Failed";
1263
					} else {
1264
						$status = "phpDynDNS: (Unknown Response)";
1265
						log_error("phpDynDNS: PAYLOAD: {$data}");
1266
						$this->_debug($data);
1267
					}
1268
					break;
1269
				case 'gratisdns':
1270
					if (preg_match('/Forkerte værdier/i', $data)) {
1271
						$status = "phpDynDNS: (Error) Wrong values - Update could not be completed.";
1272
					} else if (preg_match('/Bruger login: Bruger eksistere ikke/i', $data)) {
1273
						$status = "phpDynDNS: (Error) Unknown username - User does not exist.";
1274
					} else if (preg_match('/Bruger login: 1Fejl i kodeord/i', $data)) {
1275
						$status = "phpDynDNS: (Error) Wrong password - Remember password is case sensitive.";
1276
					} else if (preg_match('/Domæne kan IKKE administreres af bruger/i', $data)) {
1277
						$status = "phpDynDNS: (Error) User unable to administer the selected domain.";
1278
					} else if (preg_match('/OK/i', $data)) {
1279
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
1280
						$successful_update = true;
1281
					} else {
1282
						$status = "phpDynDNS: (Unknown Response)";
1283
						log_error("phpDynDNS: PAYLOAD: {$data}");
1284
						$this->_debug($data);
1285
					}
1286
					break;
1287
				case 'dnsimple':
1288
					/* Responds with HTTP 200 on success.
1289
					   Responds with HTTP 4xx on error.
1290
					   Returns JSON data as body */
1291
					$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
1292
					$header = substr($data, 0, $header_size);
1293
					$body = substr($data, $header_size);
1294
					if (preg_match("/Status: 200\s/i", $header)) {
1295
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
1296
						$successful_update = true;
1297
					} else if (preg_match("/Status: 4\d\d\s/i", $header)) {
1298
						$arrbody = json_decode($body, true);
1299
						$message = $arrbody['message'] . ".";
1300
						if (isset($arrbody['errors']['content'])) {
1301
							foreach ($arrbody['errors']['content'] as $key => $content) {
1302
								$message .= " " . $content . ".";
1303
							}
1304
						}
1305
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) " . $message;
1306
					} else {
1307
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
1308
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$body}");
1309
						$this->_debug($body);
1310
					}
1311
					break;
1312
                case 'googledomains':
1313
					if (preg_match('/notfqdn/i', $data)) {
1314
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Not A FQDN";
1315
					} else if (preg_match('/nochg/i', $data)) {
1316
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) No Change In IP Address";
1317
						$successful_update = true;
1318
					} else if (preg_match('/good/i', $data)) {
1319
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
1320
						$successful_update = true;
1321
					} else if (preg_match('/badauth/i', $data)) {
1322
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) User Authorization Failed";
1323
                    } else if (preg_match('/nohost/i', $data)) {
1324
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Hostname does not exist or DynDNS not enabled";
1325
                    } else if (preg_match('/badagent/i', $data)) {
1326
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Bad request";
1327
                    } else if (preg_match('/abuse/i', $data)) {
1328
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Dynamic DNS access has been blocked!";
1329
                    } else if (preg_match('/911/i', $data)) {
1330
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Error on Google's end, retry in 5 minutes";
1331
					} else {
1332
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
1333
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
1334
						$this->_debug($data);
1335
					}
1336
					break;
1337
			}
1338

    
1339
			if ($successful_update == true) {
1340
				/* Write WAN IP to cache file */
1341
				$wan_ip = $this->_checkIP();
1342
				conf_mount_rw();
1343
				if ($this->_useIPv6 == false && $wan_ip > 0) {
1344
					$currentTime = time();
1345
					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));
1346
					log_error("phpDynDNS: updating cache file {$this->_cacheFile}: {$wan_ip}");
1347
					@file_put_contents($this->_cacheFile, "{$wan_ip}:{$currentTime}");
1348
				} else {
1349
					@unlink($this->_cacheFile);
1350
				}
1351
				if ($this->_useIPv6 == true && $wan_ip > 0) {
1352
					$currentTime = time();
1353
					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));
1354
					log_error("phpDynDNS: updating cache file {$this->_cacheFile_v6}: {$wan_ip}");
1355
					@file_put_contents($this->_cacheFile_v6, "{$wan_ip}|{$currentTime}");
1356
				} else {
1357
					@unlink($this->_cacheFile_v6);
1358
				}
1359
				conf_mount_ro();
1360
			}
1361
			$this->status = $status;
1362
			log_error($status);
1363
		}
1364

    
1365
		/*
1366
		 * Private Function (added 12 July 05) [beta]
1367
		 *   Return Error, Set Last Error, and Die.
1368
		 */
1369
		function _error($errorNumber = '1') {
1370
			switch ($errorNumber) {
1371
				case 0:
1372
					break;
1373
				case 2:
1374
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
1375
					break;
1376
				case 3:
1377
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
1378
					break;
1379
				case 4:
1380
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
1381
					break;
1382
				case 5:
1383
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
1384
					break;
1385
				case 6:
1386
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
1387
					break;
1388
				case 7:
1389
					$error = 'phpDynDNS: (ERROR!) No Update URL Provided.';
1390
					break;
1391
				case 8:
1392
					$status = "Route 53: (Error) Invalid ZoneID";
1393
					break;
1394
				case 9:
1395
					$status = "Route 53: (Error) Invalid TTL";
1396
					break;
1397
				case 10:
1398
					$error = "phpDynDNS ({$this->_dnsHost}): No change in my IP address and/or " . $this->_dnsMaxCacheAgeDays . " days has not passed. Not updating dynamic DNS entry.";
1399
					break;
1400
				default:
1401
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
1402
					/* FIXME: $data isn't in scope here */
1403
					/* $this->_debug($data); */
1404
					break;
1405
			}
1406
			$this->lastError = $error;
1407
			log_error($error);
1408
		}
1409

    
1410
		/*
1411
		 * Private Function (added 12 July 05) [beta]
1412
		 *   - Detect whether or not IP needs to be updated.
1413
		 *      | Written Specifically for pfSense (https://www.pfsense.org) may
1414
		 *      | work with other systems. pfSense base is FreeBSD.
1415
		 */
1416
		function _detectChange() {
1417
			global $debug;
1418

    
1419
			if ($debug) {
1420
				log_error("DynDns ({$this->_dnsHost}): _detectChange() starting.");
1421
			}
1422

    
1423
			$currentTime = time();
1424

    
1425
			$wan_ip = $this->_checkIP();
1426
			if ($wan_ip == 0) {
1427
				log_error("DynDns ({$this->_dnsHost}): Current WAN IP could not be determined, skipping update process.");
1428
				return false;
1429
			}
1430
			$log_error = "DynDns ({$this->_dnsHost}): Current WAN IP: {$wan_ip} ";
1431

    
1432
			if ($this->_useIPv6 == true) {
1433
				if (file_exists($this->_cacheFile_v6)) {
1434
					$contents = file_get_contents($this->_cacheFile_v6);
1435
					list($cacheIP,$cacheTime) = explode('|', $contents);
1436
					$this->_debug($cacheIP.'/'.$cacheTime);
1437
					$initial = false;
1438
					$log_error .= "Cached IPv6: {$cacheIP} ";
1439
				} else {
1440
					conf_mount_rw();
1441
					$cacheIP = '::';
1442
					@file_put_contents($this->_cacheFile, "::|{$currentTime}");
1443
					conf_mount_ro();
1444
					$cacheTime = $currentTime;
1445
					$initial = true;
1446
					$log_error .= "No Cached IPv6 found.";
1447
				}
1448
			} else {
1449
				if (file_exists($this->_cacheFile)) {
1450
					$contents = file_get_contents($this->_cacheFile);
1451
					list($cacheIP,$cacheTime) = explode(':', $contents);
1452
					$this->_debug($cacheIP.'/'.$cacheTime);
1453
					$initial = false;
1454
					$log_error .= "Cached IP: {$cacheIP} ";
1455
				} else {
1456
					conf_mount_rw();
1457
					$cacheIP = '0.0.0.0';
1458
					@file_put_contents($this->_cacheFile, "0.0.0.0:{$currentTime}");
1459
					conf_mount_ro();
1460
					$cacheTime = $currentTime;
1461
					$initial = true;
1462
					$log_error .= "No Cached IP found.";
1463
				}
1464
			}
1465
			if ($this->_dnsVerboseLog) {
1466
				log_error($log_error);
1467
			}
1468

    
1469
			// Convert seconds = days * hr/day * min/hr * sec/min
1470
			$maxCacheAgeSecs = $this->_dnsMaxCacheAgeDays * 24 * 60 * 60;
1471

    
1472
			$needs_updating = FALSE;
1473
			/* lets determine if the item needs updating */
1474
			if ($cacheIP != $wan_ip) {
1475
				$needs_updating = true;
1476
				$update_reason = "DynDns: cacheIP != wan_ip.  Updating. ";
1477
				$update_reason .= "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
1478
			}
1479
			if (($currentTime - $cacheTime) > $maxCacheAgeSecs) {
1480
				$needs_updating = true;
1481
				$this->_forceUpdateNeeded = true;
1482
				$update_reason = "DynDns: More than " . $this->_dnsMaxCacheAgeDays . " days.  Updating. ";
1483
				$update_reason .= "{$currentTime} - {$cacheTime} > {$maxCacheAgeSecs} ";
1484
			}
1485
			if ($initial == true) {
1486
				$needs_updating = true;
1487
				$update_reason .= "Initial update. ";
1488
			}
1489

    
1490
			/*   finally if we need updating then store the
1491
			 *   new cache value and return true
1492
			 */
1493
			if ($needs_updating == true) {
1494
				if ($this->_dnsVerboseLog)
1495
					log_error("DynDns ({$this->_dnsHost}): {$update_reason}");
1496
				return true;
1497
			}
1498

    
1499
			return false;
1500
		}
1501

    
1502
		/*
1503
		 * Private Function (added 16 July 05) [beta]
1504
		 *   - Writes debug information to a file.
1505
		 *   - This function is only called when a unknown response
1506
		 *   - status is returned from a DynDNS service provider.
1507
		 */
1508
		function _debug($data) {
1509
			global $g;
1510

    
1511
			if (!$g['debug']) {
1512
				return;
1513
			}
1514
			$string = date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data."\n";
1515
			conf_mount_rw();
1516
			$file = fopen($this->_debugFile, 'a');
1517
			fwrite($file, $string);
1518
			fclose($file);
1519
			conf_mount_ro();
1520
		}
1521
		function _checkIP() {
1522
			global $debug;
1523

    
1524
			if ($debug) {
1525
				log_error("DynDns ({$this->_dnsHost}): _checkIP() starting.");
1526
			}
1527

    
1528
			if ($this->_useIPv6 == true) {
1529
				$ip_address = find_interface_ipv6($this->_if);
1530
				if (!is_ipaddrv6($ip_address)) {
1531
					return 0;
1532
				}
1533
			} else {
1534
				$ip_address = find_interface_ip($this->_if);
1535
				if (!is_ipaddr($ip_address)) {
1536
					return 0;
1537
				}
1538
			}
1539
			if ($this->_useIPv6 == false && is_private_ip($ip_address)) {
1540
				$hosttocheck = "checkip.dyndns.org";
1541
				$try = 0;
1542
				while ($try < 3) {
1543
					$checkip = gethostbyname($hosttocheck);
1544
					if (is_ipaddr($checkip)) {
1545
						break;
1546
					}
1547
					$try++;
1548
				}
1549
				if ($try >= 3) {
1550
					log_error("Dyndns debug information ({$this->_dnsHost}): Could not resolve {$hosttocheck} to IP using interface IP {$ip_address}.");
1551
					return 0;
1552
				}
1553
				$ip_ch = curl_init("http://{$checkip}");
1554
				curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
1555
				curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
1556
				curl_setopt($ip_ch, CURLOPT_INTERFACE, $ip_address);
1557
				curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
1558
				curl_setopt($ip_ch, CURLOPT_TIMEOUT, 120);
1559
				if ($this->_useIPv6 == false) {
1560
					curl_setopt($ip_ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
1561
				}
1562
				$ip_result_page = curl_exec($ip_ch);
1563
				curl_close($ip_ch);
1564
				$ip_result_decoded = urldecode($ip_result_page);
1565
				preg_match('/Current IP Address: (.*)<\/body>/', $ip_result_decoded, $matches);
1566
				$ip_address = trim($matches[1]);
1567
				if (is_ipaddr($ip_address)) {
1568
					if ($this->_dnsVerboseLog) {
1569
						log_error("DynDns ({$this->_dnsHost}): {$ip_address} extracted from {$hosttocheck}");
1570
					}
1571
				} else {
1572
					log_error("DynDns ({$this->_dnsHost}): IP address could not be extracted from {$hosttocheck}");
1573
					return 0;
1574
				}
1575
			} else {
1576
				if ($this->_dnsVerboseLog) {
1577
					log_error("DynDns ({$this->_dnsHost}): {$ip_address} extracted from local system.");
1578
				}
1579
			}
1580
			$this->_dnsIP = $ip_address;
1581

    
1582
			return $ip_address;
1583
		}
1584

    
1585
	}
1586

    
1587
?>
(16-16/67)