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
					$server = "https://carol.selfhost.de/nic/update";
575
					$port = "";
576
					if ($this->_dnsServer) {
577
						$server = $this->_dnsServer;
578
					}
579
					if ($this->_dnsPort) {
580
						$port = ":" . $this->_dnsPort;
581
					}
582
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
583
					break;
584
				case 'route53':
585
					if ($this->_dnsVerboseLog) {
586
						log_error("Route53 ({$this->_dnsHost}): DNS update() starting.");
587
					}
588

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1377
			$currentTime = time();
1378

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

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

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

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

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

    
1453
			return false;
1454
		}
1455

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

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

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

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

    
1536
			return $ip_address;
1537
		}
1538

    
1539
	}
1540

    
1541
?>
(17-17/68)