Project

General

Profile

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

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

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

    
139
			global $config, $g;
140

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

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

    
152
			$dyndnslck = lock("DDNS".$dnsID, LOCK_EX);
153

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

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

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

    
214
			$this->_debugID = rand(1000000, 9999999);
215

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

    
268
			unlock($dyndnslck);
269
		}
270

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

    
277
			if ($this->_dnsVerboseLog) {
278
				log_error("DynDNS ({$this->_dnsHost}): DynDns _update() starting.");
279
			}
280

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

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

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

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

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

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

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

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

    
617
					$records = $r53->listResourceRecordSets("/hostedzone/$ZoneID");
618

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

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

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

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

    
1205
					// loop over the lines
1206
					for ($pos=0; ($successful_update || $pos == 0) && $pos < $lines; $pos++) {
1207
						$resp = $data[$pos];
1208
						if (preg_match('/UAUTH/i', $resp)) {
1209
							$status = "DynDNS: The username specified is not authorized to update this hostname and domain.";
1210
						} else if (preg_match('/NOHOST/i', $resp)) {
1211
							$status = "DynDNS: No valid FQDN (fully qualified domain name) was specified";
1212
						} else if (preg_match('/INVLDHST/i', $resp)) {
1213
							$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.";
1214
						} else if (preg_match('/INVLDIP/i', $resp)) {
1215
							$status = "DynDNS: The IP address given is not valid.";
1216
						} else if (preg_match('/DUPHST/i', $resp)) {
1217
							$status = "DynDNS: Duplicate values exist for a record. Only single values for records are supported currently.";
1218
						} else if (preg_match('/NOUPDATE/i', $resp)) {
1219
							$status = "DynDNS: No changes made to the hostname (".strtok($resp,' ')."). Continual updates with no changes lead to blocked clients.";
1220
							$successful_update = true; //success if it is the same so that it saves
1221
						} else if (preg_match('/OK/i', $resp)) {
1222
							$status = "DynDNS: (Success) (".strtok($resp,' ').") IP Address for Changed Successfully!";
1223
							$successful_update = true;
1224
						} else {
1225
							$status = "DynDNS: (Unknown Response)";
1226
							log_error("DynDNS: PAYLOAD: {$resp}");
1227
							$this->_debug($resp);
1228
						}
1229
						log_error($status);
1230
					}
1231
					break;
1232
				case 'eurodns':
1233
					if (preg_match('/notfqdn/i', $data)) {
1234
						$status = "phpDynDNS: (Error) Not A FQDN!";
1235
					} else if (preg_match('/nochg/i', $data)) {
1236
						$status = "phpDynDNS: (Success) No Change In IP Address";
1237
						$successful_update = true;
1238
					} else if (preg_match('/good/i', $data)) {
1239
						$status = "phpDynDNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
1240
						$successful_update = true;
1241
					} else if (preg_match('/badauth/i', $data)) {
1242
						$status = "phpDynDNS: (Error) User Authorization Failed";
1243
					} else {
1244
						$status = "phpDynDNS: (Unknown Response)";
1245
						log_error("phpDynDNS: PAYLOAD: {$data}");
1246
						$this->_debug($data);
1247
					}
1248
					break;
1249
				case 'gratisdns':
1250
					if (preg_match('/Forkerte værdier/i', $data)) {
1251
						$status = "phpDynDNS: (Error) Wrong values - Update could not be completed.";
1252
					} else if (preg_match('/Bruger login: Bruger eksistere ikke/i', $data)) {
1253
						$status = "phpDynDNS: (Error) Unknown username - User does not exist.";
1254
					} else if (preg_match('/Bruger login: 1Fejl i kodeord/i', $data)) {
1255
						$status = "phpDynDNS: (Error) Wrong password - Remember password is case sensitive.";
1256
					} else if (preg_match('/Domæne kan IKKE administreres af bruger/i', $data)) {
1257
						$status = "phpDynDNS: (Error) User unable to administer the selected domain.";
1258
					} else if (preg_match('/OK/i', $data)) {
1259
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
1260
						$successful_update = true;
1261
					} else {
1262
						$status = "phpDynDNS: (Unknown Response)";
1263
						log_error("phpDynDNS: PAYLOAD: {$data}");
1264
						$this->_debug($data);
1265
					}
1266
					break;
1267
				case 'dnsimple':
1268
					/* Responds with HTTP 200 on success.
1269
					   Responds with HTTP 4xx on error.
1270
					   Returns JSON data as body */
1271
					$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
1272
					$header = substr($data, 0, $header_size);
1273
					$body = substr($data, $header_size);
1274
					if (preg_match("/Status: 200\s/i", $header)) {
1275
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
1276
						$successful_update = true;
1277
					} else if (preg_match("/Status: 4\d\d\s/i", $header)) {
1278
						$arrbody = json_decode($body, true);
1279
						$message = $arrbody['message'] . ".";
1280
						if (isset($arrbody['errors']['content'])) {
1281
							foreach ($arrbody['errors']['content'] as $key => $content) {
1282
								$message .= " " . $content . ".";
1283
							}
1284
						}
1285
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) " . $message;
1286
					} else {
1287
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
1288
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$body}");
1289
						$this->_debug($body);
1290
					}
1291
					break;
1292
			}
1293

    
1294
			if ($successful_update == true) {
1295
				/* Write WAN IP to cache file */
1296
				$wan_ip = $this->_checkIP();
1297
				conf_mount_rw();
1298
				if ($this->_useIPv6 == false && $wan_ip > 0) {
1299
					$currentTime = time();
1300
					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));
1301
					log_error("phpDynDNS: updating cache file {$this->_cacheFile}: {$wan_ip}");
1302
					@file_put_contents($this->_cacheFile, "{$wan_ip}:{$currentTime}");
1303
				} else {
1304
					@unlink($this->_cacheFile);
1305
				}
1306
				if ($this->_useIPv6 == true && $wan_ip > 0) {
1307
					$currentTime = time();
1308
					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));
1309
					log_error("phpDynDNS: updating cache file {$this->_cacheFile_v6}: {$wan_ip}");
1310
					@file_put_contents($this->_cacheFile_v6, "{$wan_ip}|{$currentTime}");
1311
				} else {
1312
					@unlink($this->_cacheFile_v6);
1313
				}
1314
				conf_mount_ro();
1315
			}
1316
			$this->status = $status;
1317
			log_error($status);
1318
		}
1319

    
1320
		/*
1321
		 * Private Function (added 12 July 05) [beta]
1322
		 *   Return Error, Set Last Error, and Die.
1323
		 */
1324
		function _error($errorNumber = '1') {
1325
			switch ($errorNumber) {
1326
				case 0:
1327
					break;
1328
				case 2:
1329
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
1330
					break;
1331
				case 3:
1332
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
1333
					break;
1334
				case 4:
1335
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
1336
					break;
1337
				case 5:
1338
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
1339
					break;
1340
				case 6:
1341
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
1342
					break;
1343
				case 7:
1344
					$error = 'phpDynDNS: (ERROR!) No Update URL Provided.';
1345
					break;
1346
				case 8:
1347
					$status = "Route 53: (Error) Invalid ZoneID";
1348
					break;
1349
				case 9:
1350
					$status = "Route 53: (Error) Invalid TTL";
1351
					break;
1352
				case 10:
1353
					$error = "phpDynDNS ({$this->_dnsHost}): No change in my IP address and/or " . $this->_dnsMaxCacheAgeDays . " days has not passed. Not updating dynamic DNS entry.";
1354
					break;
1355
				default:
1356
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
1357
					/* FIXME: $data isn't in scope here */
1358
					/* $this->_debug($data); */
1359
					break;
1360
			}
1361
			$this->lastError = $error;
1362
			log_error($error);
1363
		}
1364

    
1365
		/*
1366
		 * Private Function (added 12 July 05) [beta]
1367
		 *   - Detect whether or not IP needs to be updated.
1368
		 *      | Written Specifically for pfSense (https://www.pfsense.org) may
1369
		 *      | work with other systems. pfSense base is FreeBSD.
1370
		 */
1371
		function _detectChange() {
1372
			global $debug;
1373

    
1374
			if ($debug) {
1375
				log_error("DynDns ({$this->_dnsHost}): _detectChange() starting.");
1376
			}
1377

    
1378
			$currentTime = time();
1379

    
1380
			$wan_ip = $this->_checkIP();
1381
			if ($wan_ip == 0) {
1382
				log_error("DynDns ({$this->_dnsHost}): Current WAN IP could not be determined, skipping update process.");
1383
				return false;
1384
			}
1385
			$log_error = "DynDns ({$this->_dnsHost}): Current WAN IP: {$wan_ip} ";
1386

    
1387
			if ($this->_useIPv6 == true) {
1388
				if (file_exists($this->_cacheFile_v6)) {
1389
					$contents = file_get_contents($this->_cacheFile_v6);
1390
					list($cacheIP,$cacheTime) = explode('|', $contents);
1391
					$this->_debug($cacheIP.'/'.$cacheTime);
1392
					$initial = false;
1393
					$log_error .= "Cached IPv6: {$cacheIP} ";
1394
				} else {
1395
					conf_mount_rw();
1396
					$cacheIP = '::';
1397
					@file_put_contents($this->_cacheFile, "::|{$currentTime}");
1398
					conf_mount_ro();
1399
					$cacheTime = $currentTime;
1400
					$initial = true;
1401
					$log_error .= "No Cached IPv6 found.";
1402
				}
1403
			} else {
1404
				if (file_exists($this->_cacheFile)) {
1405
					$contents = file_get_contents($this->_cacheFile);
1406
					list($cacheIP,$cacheTime) = explode(':', $contents);
1407
					$this->_debug($cacheIP.'/'.$cacheTime);
1408
					$initial = false;
1409
					$log_error .= "Cached IP: {$cacheIP} ";
1410
				} else {
1411
					conf_mount_rw();
1412
					$cacheIP = '0.0.0.0';
1413
					@file_put_contents($this->_cacheFile, "0.0.0.0:{$currentTime}");
1414
					conf_mount_ro();
1415
					$cacheTime = $currentTime;
1416
					$initial = true;
1417
					$log_error .= "No Cached IP found.";
1418
				}
1419
			}
1420
			if ($this->_dnsVerboseLog) {
1421
				log_error($log_error);
1422
			}
1423

    
1424
			// Convert seconds = days * hr/day * min/hr * sec/min
1425
			$maxCacheAgeSecs = $this->_dnsMaxCacheAgeDays * 24 * 60 * 60;
1426

    
1427
			$needs_updating = FALSE;
1428
			/* lets determine if the item needs updating */
1429
			if ($cacheIP != $wan_ip) {
1430
				$needs_updating = true;
1431
				$update_reason = "DynDns: cacheIP != wan_ip.  Updating. ";
1432
				$update_reason .= "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
1433
			}
1434
			if (($currentTime - $cacheTime) > $maxCacheAgeSecs) {
1435
				$needs_updating = true;
1436
				$this->_forceUpdateNeeded = true;
1437
				$update_reason = "DynDns: More than " . $this->_dnsMaxCacheAgeDays . " days.  Updating. ";
1438
				$update_reason .= "{$currentTime} - {$cacheTime} > {$maxCacheAgeSecs} ";
1439
			}
1440
			if ($initial == true) {
1441
				$needs_updating = true;
1442
				$update_reason .= "Initial update. ";
1443
			}
1444

    
1445
			/*   finally if we need updating then store the
1446
			 *   new cache value and return true
1447
			 */
1448
			if ($needs_updating == true) {
1449
				if ($this->_dnsVerboseLog)
1450
					log_error("DynDns ({$this->_dnsHost}): {$update_reason}");
1451
				return true;
1452
			}
1453

    
1454
			return false;
1455
		}
1456

    
1457
		/*
1458
		 * Private Function (added 16 July 05) [beta]
1459
		 *   - Writes debug information to a file.
1460
		 *   - This function is only called when a unknown response
1461
		 *   - status is returned from a DynDNS service provider.
1462
		 */
1463
		function _debug($data) {
1464
			global $g;
1465

    
1466
			if (!$g['debug']) {
1467
				return;
1468
			}
1469
			$string = date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data."\n";
1470
			conf_mount_rw();
1471
			$file = fopen($this->_debugFile, 'a');
1472
			fwrite($file, $string);
1473
			fclose($file);
1474
			conf_mount_ro();
1475
		}
1476
		function _checkIP() {
1477
			global $debug;
1478

    
1479
			if ($debug) {
1480
				log_error("DynDns ({$this->_dnsHost}): _checkIP() starting.");
1481
			}
1482

    
1483
			if ($this->_useIPv6 == true) {
1484
				$ip_address = find_interface_ipv6($this->_if);
1485
				if (!is_ipaddrv6($ip_address)) {
1486
					return 0;
1487
				}
1488
			} else {
1489
				$ip_address = find_interface_ip($this->_if);
1490
				if (!is_ipaddr($ip_address)) {
1491
					return 0;
1492
				}
1493
			}
1494
			if ($this->_useIPv6 == false && is_private_ip($ip_address)) {
1495
				$hosttocheck = "checkip.dyndns.org";
1496
				$try = 0;
1497
				while ($try < 3) {
1498
					$checkip = gethostbyname($hosttocheck);
1499
					if (is_ipaddr($checkip)) {
1500
						break;
1501
					}
1502
					$try++;
1503
				}
1504
				if ($try >= 3) {
1505
					log_error("Dyndns debug information ({$this->_dnsHost}): Could not resolve {$hosttocheck} to IP using interface IP {$ip_address}.");
1506
					return 0;
1507
				}
1508
				$ip_ch = curl_init("http://{$checkip}");
1509
				curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
1510
				curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
1511
				curl_setopt($ip_ch, CURLOPT_INTERFACE, $ip_address);
1512
				curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
1513
				curl_setopt($ip_ch, CURLOPT_TIMEOUT, 120);
1514
				if ($this->_useIPv6 == false) {
1515
					curl_setopt($ip_ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
1516
				}
1517
				$ip_result_page = curl_exec($ip_ch);
1518
				curl_close($ip_ch);
1519
				$ip_result_decoded = urldecode($ip_result_page);
1520
				preg_match('/Current IP Address: (.*)<\/body>/', $ip_result_decoded, $matches);
1521
				$ip_address = trim($matches[1]);
1522
				if (is_ipaddr($ip_address)) {
1523
					if ($this->_dnsVerboseLog) {
1524
						log_error("DynDns ({$this->_dnsHost}): {$ip_address} extracted from {$hosttocheck}");
1525
					}
1526
				} else {
1527
					log_error("DynDns ({$this->_dnsHost}): IP address could not be extracted from {$hosttocheck}");
1528
					return 0;
1529
				}
1530
			} else {
1531
				if ($this->_dnsVerboseLog) {
1532
					log_error("DynDns ({$this->_dnsHost}): {$ip_address} extracted from local system.");
1533
				}
1534
			}
1535
			$this->_dnsIP = $ip_address;
1536

    
1537
			return $ip_address;
1538
		}
1539

    
1540
	}
1541

    
1542
?>
(16-16/67)