Project

General

Profile

Download (59.6 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
			$dyndnslck = lock("DDNS".$dnsID, LOCK_EX);
152

    
153
			if (!$dnsService) $this->_error(2);
154
			switch ($dnsService) {
155
			case 'freedns':
156
				if (!$dnsHost) $this->_error(5);
157
				break;
158
			case 'namecheap':
159
				if (!$dnsPass) $this->_error(4);
160
				if (!$dnsHost) $this->_error(5);
161
				break;
162
			case 'route53':
163
				if (!$dnsZoneID) $this->_error(8);
164
				if (!$dnsTTL) $this->_error(9);
165
				break;
166
			case 'custom':
167
				if (!$dnsUpdateURL) $this->_error(7);
168
				break;
169
			default:
170
				if (!$dnsUser) $this->_error(3);
171
				if (!$dnsPass) $this->_error(4);
172
				if (!$dnsHost) $this->_error(5);
173
			}
174
			
175
			switch ($dnsService) {
176
			case 'he-net-v6':
177
			case 'custom-v6':
178
				$this->_useIPv6 = true;
179
				break;
180
			default:
181
				$this->_useIPv6 = false;
182
			}
183
			$this->_dnsService = strtolower($dnsService);
184
			$this->_dnsUser = $dnsUser;
185
			$this->_dnsPass = $dnsPass;
186
			$this->_dnsHost = $dnsHost;
187
			$this->_dnsServer = $dnsServer;
188
			$this->_dnsPort = $dnsPort;
189
			$this->_dnsWildcard = $dnsWildcard;
190
			$this->_dnsMX = $dnsMX;
191
			$this->_dnsZoneID = $dnsZoneID;
192
			$this->_dnsTTL = $dnsTTL;
193
			$this->_if = get_failover_interface($dnsIf);
194
			$this->_checkIP();
195
			$this->_dnsUpdateURL = $dnsUpdateURL;
196
			$this->_dnsResultMatch = $dnsResultMatch;
197
			$this->_dnsRequestIf = get_failover_interface($dnsRequestIf);
198
			if ($this->_dnsVerboseLog)
199
				log_error("DynDNS ({$this->_dnsHost}): running get_failover_interface for {$dnsRequestIf}. found {$this->_dnsRequestIf}");
200
			$this->_dnsRequestIfIP = get_interface_ip($dnsRequestIf);
201
			$this->_dnsMaxCacheAgeDays = 25;
202
			$this->_dnsDummyUpdateDone = false;
203
			$this->_forceUpdateNeeded = $forceUpdate;
204
			
205
			// Ensure that we were able to lookup the IP
206
			if(!is_ipaddr($this->_dnsIP)) {
207
				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.");
208
				unlock($dyndnslck);
209
				return;
210
			}
211

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

    
266
			unlock($dyndnslck);
267
		}
268
			
269
		/*
270
		 * Private Function (added 12 July 05) [beta]
271
		 *   Send Update To Selected Service.
272
		 */
273
		function _update() {
274
		
275
			if ($this->_dnsVerboseLog)
276
				log_error("DynDNS ({$this->_dnsHost}): DynDns _update() starting.");
277
		
278
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53 ') {
279
				$ch = curl_init();
280
				curl_setopt($ch, CURLOPT_HEADER, 0);
281
				curl_setopt($ch, CURLOPT_USERAGENT, $this->_UserAgent);
282
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
283
				curl_setopt($ch, CURLOPT_INTERFACE, $this->_dnsRequestIfIP);
284
				curl_setopt($ch, CURLOPT_TIMEOUT, 120); // Completely empirical
285
			}
286

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

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

    
470
				case 'staticcling':
471
					$needsIP = FALSE;
472
					curl_setopt($ch, CURLOPT_URL, 'https://www.staticcling.org/update.html?login='.$this->_dnsUser.'&pass='.$this->_dnsPass);
473
					break;	                    
474
				case 'dnsomatic':
475
					/* Example syntax 
476
						https://username:password@updates.dnsomatic.com/nic/update?hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG
477
					*/
478
					$needsIP = FALSE;
479
					if ($this->_dnsVerboseLog)
480
						log_error("DNS-O-Matic: DNS update() starting.");
481
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
482
					/*
483
					Reference: https://www.dnsomatic.com/wiki/api
484
						DNS-O-Matic usernames are 3-25 characters.
485
						DNS-O-Matic passwords are 6-20 characters.
486
						All ASCII letters and numbers accepted.
487
						Dots, dashes, and underscores allowed, but not at the beginning or end of the string.
488
					Required: "rawurlencode" http://www.php.net/manual/en/function.rawurlencode.php
489
						Encodes the given string according to RFC 3986.
490
					*/
491
					$server = "https://" . rawurlencode($this->_dnsUser) . ":" . rawurlencode($this->_dnsPass) . "@updates.dnsomatic.com/nic/update?hostname=";
492
					if($this->_dnsServer)
493
						$server = $this->_dnsServer;
494
					if($this->_dnsPort)
495
						$port = ":" . $this->_dnsPort;
496
					curl_setopt($ch, CURLOPT_URL, $server . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NOCHG');
497
					break;
498
				case 'namecheap':
499
					/* Example:
500
						https://dynamicdns.park-your-domain.com/update?host=[host_name]&domain=[domain.com]&password=[domain_password]&ip=[your_ip]
501
					*/
502
					$needsIP = FALSE;
503
					if ($this->_dnsVerboseLog)
504
						log_error("Namecheap ({$this->_dnsHost}): DNS update() starting.");
505
					$dparts = explode(".", trim($this->_dnsHost));
506
					$domain_part_count = ($dparts[count($dparts)-1] == "uk") ? 3 : 2;
507
					$domain_offset = count($dparts) - $domain_part_count;
508
					$hostname = implode(".", array_slice($dparts, 0, $domain_offset));
509
					$domain = implode(".", array_slice($dparts, $domain_offset));
510
					$dnspass = trim($this->_dnsPass);
511
					$server = "https://dynamicdns.park-your-domain.com/update?host={$hostname}&domain={$domain}&password={$dnspass}&ip={$this->_dnsIP}";
512
					curl_setopt($ch, CURLOPT_URL, $server);
513
					break;
514
				case 'he-net':
515
				case 'he-net-v6':
516
					$needsIP = FALSE;
517
					if ($this->_dnsVerboseLog)
518
						log_error("HE.net ({$this->_dnsHost}): DNS update() starting.");
519
					$server = "https://dyn.dns.he.net/nic/update?";
520
					curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
521
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
522
					curl_setopt($ch, CURLOPT_URL, $server . 'hostname=' . $this->_dnsHost . '&password=' . $this->_dnsPass . '&myip=' . $this->_dnsIP);
523
					break;
524
				case 'he-net-tunnelbroker':
525
					$needsIP = FALSE;
526
					if ($this->_dnsVerboseLog)
527
						log_error("HE.net Tunnelbroker: DNS update() starting.");
528
					$server = "https://ipv4.tunnelbroker.net/ipv4_end.php?";
529
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser . ':' . $this->_dnsPass);
530
					curl_setopt($ch, CURLOPT_URL, $server . 'tid=' . $this->_dnsHost);
531
					break;
532
				case 'selfhost':
533
					$needsIP = FALSE;
534
					if ($this->_dnsVerboseLog)
535
						log_error("SelfHost: DNS update() starting.");
536
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
537
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
538
					$server = "https://carol.selfhost.de/nic/update";
539
					$port = "";
540
					if($this->_dnsServer)
541
						$server = $this->_dnsServer;
542
					if($this->_dnsPort)
543
						$port = ":" . $this->_dnsPort;
544
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
545
					break;
546
				case 'route53':
547
					if ($this->_dnsVerboseLog)
548
						log_error("Route53 ({$this->_dnsHost}): DNS update() starting.");
549
					
550
					/* Setting Variables */
551
					$hostname = "{$this->_dnsHost}.";
552
					$ZoneID = $this->_dnsZoneID;
553
					$AccessKeyId=$this->_dnsUser;
554
					$SecretAccessKey=$this->_dnsPass;
555
					$NewIP=$this->_dnsIP;
556
					$NewTTL=$this->_dnsTTL;
557

    
558
					/* Include Route 53 Library Class */
559
					require_once('/etc/inc/r53.class');
560

    
561
					/* Set Amazon AWS Credentials for this record */
562
					$r53 = new Route53($AccessKeyId, $SecretAccessKey);
563

    
564
					/* Function to find old values of records in Route 53 */
565
					if(!function_exists('Searchrecords')) {
566
						function SearchRecords($records, $name) {
567
							$result = array();
568
							foreach($records as $record) {
569
								if(strtolower($record['Name']) == strtolower($name)) {
570
									$result [] = $record;
571
								}
572
							}
573
							return ($result) ? $result : false;
574
						}
575
					}
576

    
577
					$records = $r53->listResourceRecordSets("/hostedzone/$ZoneID");
578

    
579
					/* Get IP for your hostname in Route 53 */
580
					if(false !== ($a_result = SearchRecords($records['ResourceRecordSets'], "$hostname"))) {
581
						$OldTTL=$a_result[0][TTL];
582
						$OldIP=$a_result[0][ResourceRecords][0];
583
					} else {
584
						$OldIP="";
585
					}
586

    
587
					/* Check if we need to update DNS Record */
588
					if ($OldIP !== $NewIP) {
589
						if(!empty($OldIP)) {
590
							/* Your Hostname already exists, deleting and creating it again */
591
							$changes = array();
592
							$changes[] = $r53->prepareChange(DELETE, $hostname, A, $OldTTL, $OldIP);
593
							$changes[] = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
594
							$result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
595
						} else {
596
							/* Your Hostname does not exist yet, creating it */
597
							$changes = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
598
							$result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
599
						}
600
					}
601
					$this->_checkStatus(0, $result);
602
					break;
603
				case 'custom':
604
				case 'custom-v6':
605
					if ($this->_dnsVerboseLog)
606
						log_error("Custom DDNS ({$this->_dnsHost}): DNS update() starting.");
607
					if (strstr($this->dnsUpdateURL, "%IP%")) {$needsIP = TRUE;} else {$needsIP = FALSE;}
608
					if ($this->_dnsUser != '') {
609
						if ($this->_curlIpresolveV4)
610
							curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
611
						if ($this->_curlSslVerifypeer)
612
							curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
613
						else
614
							curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
615
						curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
616
						curl_setopt($ch, CURLOPT_USERPWD, "{$this->_dnsUser}:{$this->_dnsPass}");
617
					}
618
					$server = str_replace("%IP%", $this->_dnsIP, $this->_dnsUpdateURL);
619
					if ($this->_dnsVerboseLog)
620
						log_error("Sending request to: ".$server);
621
					curl_setopt($ch, CURLOPT_URL, $server);
622
					break;
623
				case 'cloudflare':
624
					$needsIP = TRUE;
625
					$dnsServer ='www.cloudflare.com';
626
					$dnsHost = str_replace(' ','', $this->_dnsHost);
627
					$URL = "https://{$dnsServer}/api.html?a=DIUP&email={$this->_dnsUser}&tkn={$this->_dnsPass}&ip={$this->_dnsIP}&hosts={$dnsHost}";
628
					curl_setopt($ch, CURLOPT_URL, $URL);
629
					break;
630
				case 'eurodns':		
631
					$needsIP = TRUE;
632
					if ($this->_dnsVerboseLog)
633
						log_error("EuroDynDns ({$this->_dnsHost}) DNS update() starting.");
634
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
635
					$server = "https://update.eurodyndns.org/update/";
636
					$port = "";
637
					if($this->_dnsPort)
638
						$port = ":" . $this->_dnsPort;
639
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP);
640
					break;
641
				case 'gratisdns':
642
					$needsIP = FALSE;
643
					if ($this->_dnsVerboseLog)
644
						log_error("GratisDNS.dk ({$this->_dnsHost}): DNS update() starting.");
645
					$server = "https://ssl.gratisdns.dk/ddns.phtml";
646
					list($hostname, $domain) = explode(".", $this->_dnsHost, 2);
647
					curl_setopt($ch, CURLOPT_URL, $server . '?u=' . $this->_dnsUser . '&p=' . $this->_dnsPass . '&h=' . $this->_dnsHost . '&d=' . $domain);
648
					break;
649
				case 'ovh-dynhost':
650
					$needsIP = FALSE;
651
					if ($this->_dnsVerboseLog)
652
						log_error("OVH DynHOST: ({$this->_dnsHost}) DNS update() starting.");
653
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
654
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
655
					$server = "https://www.ovh.com/nic/update";
656
					$port = "";
657
					if($this->_dnsServer)
658
						$server = $this->_dnsServer;
659
					if($this->_dnsPort)
660
						$port = ":" . $this->_dnsPort;
661
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
662
					break;
663
				case 'citynetwork':
664
					$needsIP = TRUE;
665
					if ($this->_dnsVerboseLog)
666
						log_error("City Network: ({$this->_dnsHost}) DNS update() starting.");
667
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
668
					$server = 'https://dyndns.citynetwork.se/nic/update';
669
					$port = "";
670
					if($this->_dnsServer)
671
						$server = $this->_dnsServer;
672
					if($this->_dnsPort)
673
						$port = ":" . $this->_dnsPort;
674
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP);
675
					break;
676
				case 'dnsimple':
677
					/* Uses DNSimple's REST API
678
					   Requires username and Account API token passed in header
679
					   Piggybacks on Route 53's ZoneID field for DNSimple record ID
680
					   Data sent as JSON */
681
					$needsIP = TRUE;
682
					$server = 'https://api.dnsimple.com/v1/domains/';
683
					$token = $this->_dnsUser . ':' . $this->_dnsPass;
684
					$jsondata = '{"record":{"content":"' . $this->_dnsIP . '","ttl":"' . $this->_dnsTTL . '"}}';
685
					curl_setopt($ch, CURLOPT_HEADER, 1);
686
					curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
687
					curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json','Content-Type: application/json','X-DNSimple-Token: ' . $token));
688
					curl_setopt($ch, CURLOPT_URL, $server . $this->_dnsHost . '/records/' . $this->_dnsZoneID);
689
					curl_setopt($ch, CURLOPT_POSTFIELDS, $jsondata);
690
					break;
691
				default:
692
					break;
693
			}
694
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53') {
695
				$data = curl_exec($ch);
696
				$this->_checkStatus($ch, $data);
697
				@curl_close($ch);
698
			}
699
		}
700

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

    
1150
					// loop over the lines
1151
					for ($pos=0; ($successful_update || $pos == 0) && $pos < $lines; $pos++){
1152
						$resp = $data[$pos];
1153
						if (preg_match('/UAUTH/i', $resp)) {
1154
							$status = "DynDNS: The username specified is not authorized to update this hostname and domain.";
1155
						} else if (preg_match('/NOHOST/i', $resp)) {
1156
							$status = "DynDNS: No valid FQDN (fully qualified domain name) was specified";
1157
						} else if (preg_match('/INVLDHST/i', $resp)) {
1158
							$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.";
1159
						} else if (preg_match('/INVLDIP/i', $resp)) {
1160
							$status = "DynDNS: The IP address given is not valid.";
1161
						} else if (preg_match('/DUPHST/i', $resp)) {
1162
							$status = "DynDNS: Duplicate values exist for a record. Only single values for records are supported currently.";
1163
						} else if (preg_match('/NOUPDATE/i', $resp)) {
1164
							$status = "DynDNS: No changes made to the hostname (".strtok($resp,' ')."). Continual updates with no changes lead to blocked clients.";
1165
							$successful_update = true; //success if it is the same so that it saves
1166
						} else if (preg_match('/OK/i', $resp)) {
1167
							$status = "DynDNS: (Success) (".strtok($resp,' ').") IP Address for Changed Successfully!";
1168
							$successful_update = true;
1169
						} else {
1170
							$status = "DynDNS: (Unknown Response)";
1171
							log_error("DynDNS: PAYLOAD: {$resp}");
1172
							$this->_debug($resp);
1173
						}
1174
						log_error($status);
1175
					}
1176
					break;
1177
				case 'eurodns':
1178
					if (preg_match('/notfqdn/i', $data)) {
1179
						$status = "phpDynDNS: (Error) Not A FQDN!";
1180
					} else if (preg_match('/nochg/i', $data)) {
1181
						$status = "phpDynDNS: (Success) No Change In IP Address";
1182
						$successful_update = true;
1183
					} else if (preg_match('/good/i', $data)) {
1184
						$status = "phpDynDNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
1185
						$successful_update = true;
1186
					} else if (preg_match('/badauth/i', $data)) {
1187
						$status = "phpDynDNS: (Error) User Authorization Failed";
1188
					} else {
1189
						$status = "phpDynDNS: (Unknown Response)";
1190
						log_error("phpDynDNS: PAYLOAD: {$data}");
1191
						$this->_debug($data);
1192
					}
1193
					break;
1194
				case 'gratisdns':
1195
					if (preg_match('/Forkerte værdier/i', $data)) {
1196
					        $status = "phpDynDNS: (Error) Wrong values - Update could not be completed.";
1197
					} else if (preg_match('/Bruger login: Bruger eksistere ikke/i', $data)) {
1198
					        $status = "phpDynDNS: (Error) Unknown username - User does not exist.";
1199
					} else if (preg_match('/Bruger login: 1Fejl i kodeord/i', $data)) {
1200
					        $status = "phpDynDNS: (Error) Wrong password - Remember password is case sensitive.";
1201
					} else if (preg_match('/Domæne kan IKKE administreres af bruger/i', $data)) {
1202
					        $status = "phpDynDNS: (Error) User unable to administer the selected domain.";
1203
					} else if (preg_match('/OK/i', $data)) {
1204
					        $status = "phpDynDNS: (Success) IP Address Updated Successfully!";
1205
					        $successful_update = true;
1206
					} else {
1207
					        $status = "phpDynDNS: (Unknown Response)";
1208
					        log_error("phpDynDNS: PAYLOAD: {$data}");
1209
					        $this->_debug($data);
1210
					}
1211
					break;
1212
				case 'dnsimple':
1213
					/* Responds with HTTP 200 on success.
1214
					   Responds with HTTP 4xx on error.
1215
					   Returns JSON data as body */
1216
					$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
1217
					$header = substr($data, 0, $header_size);
1218
					$body = substr($data, $header_size);
1219
					if (preg_match("/Status: 200\s/i", $header)) {
1220
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
1221
						$successful_update = true;
1222
					} else if (preg_match("/Status: 4\d\d\s/i", $header)) {
1223
						$arrbody = json_decode($body, true);
1224
						$message = $arrbody['message'] . ".";
1225
						if (isset($arrbody['errors']['content'])) {
1226
							foreach($arrbody['errors']['content'] as $key => $content) {
1227
								$message .= " " . $content . ".";
1228
							}
1229
						}
1230
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) " . $message;
1231
					} else {
1232
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
1233
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$body}");
1234
						$this->_debug($body);
1235
					}
1236
					break;
1237
			}
1238

    
1239
			if($successful_update == true) {
1240
				/* Write WAN IP to cache file */
1241
				$wan_ip = $this->_checkIP();
1242
				conf_mount_rw();
1243
				if ($this->_useIPv6 == false && $wan_ip > 0) {
1244
					$currentTime = time();
1245
					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));
1246
					log_error("phpDynDNS: updating cache file {$this->_cacheFile}: {$wan_ip}");
1247
					@file_put_contents($this->_cacheFile, "{$wan_ip}:{$currentTime}");
1248
				} else
1249
					@unlink($this->_cacheFile);
1250
				if ($this->_useIPv6 == true && $wan_ip > 0) {
1251
					$currentTime = time();
1252
					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));
1253
					log_error("phpDynDNS: updating cache file {$this->_cacheFile_v6}: {$wan_ip}");
1254
					@file_put_contents($this->_cacheFile_v6, "{$wan_ip}|{$currentTime}");
1255
				} else
1256
					@unlink($this->_cacheFile_v6);
1257
				conf_mount_ro();
1258
			}
1259
			$this->status = $status;
1260
			log_error($status);
1261
		}
1262

    
1263
		/*
1264
		 * Private Function (added 12 July 05) [beta]
1265
		 *   Return Error, Set Last Error, and Die.
1266
		 */
1267
		function _error($errorNumber = '1') {
1268
			switch ($errorNumber) {
1269
				case 0:
1270
					break;
1271
				case 2:
1272
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
1273
					break;
1274
				case 3:
1275
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
1276
					break;
1277
				case 4:
1278
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
1279
					break;
1280
				case 5:
1281
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
1282
					break;
1283
				case 6:
1284
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
1285
					break;
1286
				case 7:
1287
					$error = 'phpDynDNS: (ERROR!) No Update URL Provided.';
1288
					break;
1289
				case 8:
1290
					$status = "Route 53: (Error) Invalid ZoneID";
1291
					break;
1292
				case 9:
1293
					$status = "Route 53: (Error) Invalid TTL";
1294
					break;  
1295
				case 10:
1296
					$error = "phpDynDNS ({$this->_dnsHost}): No change in my IP address and/or " . $this->_dnsMaxCacheAgeDays . " days has not passed. Not updating dynamic DNS entry.";
1297
					break;
1298
				default:
1299
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
1300
					/* FIXME: $data isn't in scope here */
1301
					/* $this->_debug($data); */
1302
					break;
1303
			}
1304
			$this->lastError = $error;
1305
			log_error($error);
1306
		}
1307

    
1308
		/*
1309
		 * Private Function (added 12 July 05) [beta]
1310
		 *   - Detect whether or not IP needs to be updated.
1311
		 *      | Written Specifically for pfSense (https://www.pfsense.org) may
1312
		 *      | work with other systems. pfSense base is FreeBSD.
1313
		 */
1314
		function _detectChange() {
1315
			global $debug;
1316

    
1317
			if ($debug)
1318
				log_error("DynDns ({$this->_dnsHost}): _detectChange() starting.");
1319
		
1320
			$currentTime = time();
1321

    
1322
			$wan_ip = $this->_checkIP();
1323
			if ($wan_ip == 0) {
1324
				log_error("DynDns ({$this->_dnsHost}): Current WAN IP could not be determined, skipping update process.");
1325
				return false;
1326
			}
1327
			$log_error = "DynDns ({$this->_dnsHost}): Current WAN IP: {$wan_ip} ";
1328

    
1329
			if ($this->_useIPv6 == true) {
1330
				if (file_exists($this->_cacheFile_v6)) {
1331
					$contents = file_get_contents($this->_cacheFile_v6);
1332
					list($cacheIP,$cacheTime) = explode('|', $contents);
1333
					$this->_debug($cacheIP.'/'.$cacheTime);
1334
					$initial = false;
1335
					$log_error .= "Cached IPv6: {$cacheIP} ";
1336
				} else {
1337
					conf_mount_rw();
1338
					$cacheIP = '::';
1339
					@file_put_contents($this->_cacheFile, "::|{$currentTime}");
1340
					conf_mount_ro();
1341
					$cacheTime = $currentTime;
1342
					$initial = true;
1343
					$log_error .= "No Cached IPv6 found.";
1344
				}
1345
			} else {
1346
				if (file_exists($this->_cacheFile)) {
1347
					$contents = file_get_contents($this->_cacheFile);
1348
					list($cacheIP,$cacheTime) = explode(':', $contents);
1349
					$this->_debug($cacheIP.'/'.$cacheTime);
1350
					$initial = false;
1351
					$log_error .= "Cached IP: {$cacheIP} ";
1352
				} else {
1353
					conf_mount_rw();
1354
					$cacheIP = '0.0.0.0';
1355
					@file_put_contents($this->_cacheFile, "0.0.0.0:{$currentTime}");
1356
					conf_mount_ro();
1357
					$cacheTime = $currentTime;
1358
					$initial = true;
1359
					$log_error .= "No Cached IP found.";
1360
				}
1361
			}
1362
			if ($this->_dnsVerboseLog)
1363
				log_error($log_error);
1364

    
1365
			// Convert seconds = days * hr/day * min/hr * sec/min
1366
			$maxCacheAgeSecs = $this->_dnsMaxCacheAgeDays * 24 * 60 * 60;
1367

    
1368
			$needs_updating = FALSE;
1369
			/* lets determine if the item needs updating */
1370
			if ($cacheIP != $wan_ip) {
1371
				$needs_updating = true;
1372
				$update_reason = "DynDns: cacheIP != wan_ip.  Updating. ";
1373
				$update_reason .= "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
1374
			}
1375
			if (($currentTime - $cacheTime) > $maxCacheAgeSecs) {
1376
				$needs_updating = true;
1377
				$this->_forceUpdateNeeded = true;
1378
				$update_reason = "DynDns: More than " . $this->_dnsMaxCacheAgeDays . " days.  Updating. ";
1379
				$update_reason .= "{$currentTime} - {$cacheTime} > {$maxCacheAgeSecs} ";
1380
			}
1381
			if ($initial == true) {
1382
				$needs_updating = true;
1383
				$update_reason .= "Initial update. ";
1384
			}
1385

    
1386
			/*   finally if we need updating then store the
1387
			 *   new cache value and return true
1388
			 */
1389
			if ($needs_updating == true) {
1390
				if ($this->_dnsVerboseLog)
1391
					log_error("DynDns ({$this->_dnsHost}): {$update_reason}");
1392
				return true;
1393
			}
1394

    
1395
			return false;			
1396
		}
1397

    
1398
		/*
1399
		 * Private Function (added 16 July 05) [beta]
1400
		 *   - Writes debug information to a file.
1401
		 *   - This function is only called when a unknown response
1402
		 *   - status is returned from a DynDNS service provider.
1403
		 */
1404
		function _debug($data) {
1405
			global $g;
1406

    
1407
			if (!$g['debug'])
1408
				return;
1409
			$string = date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data."\n";
1410
			conf_mount_rw();
1411
			$file = fopen($this->_debugFile, 'a');
1412
			fwrite($file, $string);
1413
			fclose($file);
1414
			conf_mount_ro();
1415
		}
1416
		function _checkIP() {
1417
			global $debug;
1418

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

    
1422
			if ($this->_useIPv6 == true) {
1423
				$ip_address = find_interface_ipv6($this->_if);
1424
				if (!is_ipaddrv6($ip_address))
1425
					return 0;
1426
			} else {
1427
				$ip_address = find_interface_ip($this->_if);
1428
				if (!is_ipaddr($ip_address))
1429
					return 0;
1430
			}
1431
			if ($this->_useIPv6 == false && is_private_ip($ip_address)) {
1432
				$hosttocheck = "checkip.dyndns.org";
1433
				$try = 0;
1434
				while ($try < 3) {
1435
					$checkip = gethostbyname($hosttocheck);
1436
					if (is_ipaddr($checkip))
1437
						break;
1438
					$try++;
1439
				}
1440
				if ($try >= 3) {
1441
					log_error("Dyndns debug information ({$this->_dnsHost}): Could not resolve {$hosttocheck} to IP using interface IP {$ip_address}.");
1442
					return 0;
1443
				}
1444
				$ip_ch = curl_init("http://{$checkip}");
1445
				curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
1446
				curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
1447
				curl_setopt($ip_ch, CURLOPT_INTERFACE, $ip_address);
1448
				curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
1449
				curl_setopt($ip_ch, CURLOPT_TIMEOUT, 120);
1450
				if ($this->_useIPv6 == false) {
1451
					curl_setopt($ip_ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
1452
				}
1453
				$ip_result_page = curl_exec($ip_ch);
1454
				curl_close($ip_ch);
1455
				$ip_result_decoded = urldecode($ip_result_page);
1456
				preg_match('/Current IP Address: (.*)<\/body>/', $ip_result_decoded, $matches);
1457
				$ip_address = trim($matches[1]);
1458
				if (is_ipaddr($ip_address)) {
1459
					if ($this->_dnsVerboseLog)
1460
						log_error("DynDns ({$this->_dnsHost}): {$ip_address} extracted from {$hosttocheck}");
1461
				} else {
1462
					log_error("DynDns ({$this->_dnsHost}): IP address could not be extracted from {$hosttocheck}");
1463
					return 0;
1464
				}
1465
			} else {
1466
				if ($this->_dnsVerboseLog)
1467
					log_error("DynDns ({$this->_dnsHost}): {$ip_address} extracted from local system.");
1468
			}
1469
			$this->_dnsIP = $ip_address;
1470

    
1471
			return $ip_address;
1472
		}
1473

    
1474
	}
1475

    
1476
?>
(17-17/68)