Project

General

Profile

Download (50.3 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 Tunnelbroker IP update (ipv4.tunnelbroker.net)
23
	 *    - SelfHost (selfhost.de)
24
	 *    - Amazon Route 53 (aws.amazon.com)
25
	 *    - DNS-O-Matic (dnsomatic.com)
26
	 *    - Custom DDNS (any URL)
27
	 *    - CloudFlare (www.cloudflare.com)
28
	 * +----------------------------------------------------+
29
	 *  Requirements:
30
	 *    - PHP version 4.0.2 or higher with the CURL Library and the PCRE Library
31
	 * +----------------------------------------------------+
32
	 *  Public Functions
33
	 *    - updatedns()
34
	 *
35
	 *  Private Functions
36
	 *    - _update()
37
	 *    - _checkStatus()
38
	 *    - _error()
39
	 *    - _detectChange()
40
	 *    - _debug()
41
	 *    - _checkIP()
42
	 * +----------------------------------------------------+
43
	 *  DynDNS Dynamic - Last Tested: 12 July 2005
44
	 *  DynDNS Static  - Last Tested: NEVER
45
	 *  DynDNS Custom  - Last Tested: NEVER
46
	 *  No-IP          - Last Tested: 20 July 2008
47
	 *  HN.org         - Last Tested: 12 July 2005
48
	 *  EasyDNS        - Last Tested: 20 July 2008
49
	 *  DHS            - Last Tested: 12 July 2005
50
	 *  ZoneEdit       - Last Tested: NEVER
51
	 *  Dyns           - Last Tested: NEVER
52
	 *  ODS            - Last Tested: 02 August 2005
53
	 *  FreeDNS        - Last Tested: 23 Feb 2011
54
	 *  Loopia         - Last Tested: NEVER
55
	 *  StaticCling    - Last Tested: 27 April 2006
56
	 *  DNSexit	   - Last Tested: 20 July 2008
57
	 *  OpenDNS	   - Last Tested: 4 August 2008
58
	 *  Namecheap	   - Last Tested: 31 August 2010
59
	 *  HE.net         - Last Tested: NEVER
60
	 *  HE.net Tunnel  - Last Tested: 28 June 2011
61
	 *  SelfHost       - Last Tested: 26 December 2011
62
	 *  Amazon Route 53 - Last tested: 01 April 2012
63
	 *  DNS-O-Matic	   - Last Tested: 9 September 2010
64
	 *  CloudFlare     - Last Tested: 30 May 2013
65
	 * +====================================================+
66
	 *
67
	 * @author 	E.Kristensen
68
	 * @link    	http://www.idylldesigns.com/projects/phpdns/
69
	 * @version 	0.8
70
	 * @updated	13 October 05 at 21:02:42 GMT
71
	 *
72
	 * DNSexit/OpenDNS support and multiwan extension for pfSense by Ermal Luci
73
	 * Custom DNS support by Matt Corallo
74
	 *
75
	 */
76

    
77
	class updatedns {
78
		var $_cacheFile;
79
		var $_debugFile;
80
		var $_UserAgent = 'User-Agent: phpDynDNS/0.7';
81
		var $_errorVerbosity = 0;
82
		var $_dnsService;
83
		var $_dnsUser;
84
		var $_dnsPass;
85
		var $_dnsHost;
86
		var $_dnsIP;
87
		var $_dnsWildcard;
88
		var $_dnsMX;
89
		var $_dnsBackMX;
90
		var $_dnsServer;
91
		var $_dnsPort;
92
		var $_dnsUpdateURL;
93
		var $_dnsZoneID;
94
		var $_dnsTTL;
95
		var $status;
96
		var $_debugID;
97
		var $_if;
98
		var $_dnsResultMatch;
99
		var $_dnsRequestIf;
100
		var $_dnsRequestIfIP;
101
		var $_dnsVerboseLog;
102
		var $_curlIpresolveV4;
103
		var $_curlSslVerifypeer;
104
		var $_dnsMaxCacheAgeDays;
105
		var $_dnsDummyUpdateDone;
106
		var $_forceUpdateNeeded;
107
		
108
		/* 
109
		 * Public Constructor Function (added 12 July 05) [beta]
110
		 *   - Gets the dice rolling for the update. 
111
		 *   - $dnsResultMatch should only be used with $dnsService = 'custom'
112
		 *   -  $dnsResultMatch is parsed for '%IP%', which is the IP the provider was updated to, 
113
		 *   -  it is otherwise expected to be exactly identical to what is returned by the Provider.
114
		 *   - $dnsUser, and $dnsPass indicate HTTP Auth for custom DNS, if they are needed in the URL (GET Variables), include them in $dnsUpdateURL.
115
		 *   - $For custom requests, $dnsUpdateURL is parsed for '%IP%', which is replaced with the new IP.
116
		 */
117
		function updatedns ($dnsService = '', $dnsHost = '', $dnsUser = '', $dnsPass = '',
118
				    $dnsWildcard = 'OFF', $dnsMX = '', $dnsIf = '', $dnsBackMX = '',
119
				    $dnsServer = '', $dnsPort = '', $dnsUpdateURL = '', $forceUpdate = false,
120
				    $dnsZoneID ='', $dnsTTL='', $dnsResultMatch = '', $dnsRequestIf = '',
121
				    $dnsID = '', $dnsVerboseLog = false, $curlIpresolveV4 = false, $curlSslVerifypeer = true) {
122
			
123
			global $config, $g;
124
			
125
			$this->_cacheFile = "{$g['conf_path']}/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . "{$dnsID}.cache";
126
			$this->_debugFile = "{$g['varetc_path']}/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . "{$dnsID}.debug";
127

    
128
			$this->_curlIpresolveV4 = $curlIpresolveV4;
129
			$this->_curlSslVerifypeer = $curlSslVerifypeer;
130
			$this->_dnsVerboseLog = $dnsVerboseLog;
131
			if ($this->_dnsVerboseLog)
132
				log_error("DynDns: updatedns() starting");
133

    
134
			$dyndnslck = lock("DDNS".$dnsID, LOCK_EX);
135

    
136
			if (!$dnsService) $this->_error(2);
137
			switch ($dnsService) {
138
			case 'freedns':
139
				if (!$dnsHost) $this->_error(5);
140
				break;
141
			case 'namecheap':
142
				if (!$dnsPass) $this->_error(4);
143
				if (!$dnsHost) $this->_error(5);
144
				break;
145
			case 'route53':
146
				if (!$dnsZoneID) $this->_error(8);
147
				if (!$dnsTTL) $this->_error(9);
148
				break;
149
			case 'custom':
150
				if (!$dnsUpdateURL) $this->_error(7);
151
				break;
152
			default:
153
				if (!$dnsUser) $this->_error(3);
154
				if (!$dnsPass) $this->_error(4);
155
				if (!$dnsHost) $this->_error(5);
156
			}
157
			
158
			$this->_dnsService = strtolower($dnsService);
159
			$this->_dnsUser = $dnsUser;
160
			$this->_dnsPass = $dnsPass;
161
			$this->_dnsHost = $dnsHost;
162
			$this->_dnsServer = $dnsServer;
163
			$this->_dnsPort = $dnsPort;
164
			$this->_dnsWildcard = $dnsWildcard;
165
			$this->_dnsMX = $dnsMX;
166
			$this->_dnsZoneID = $dnsZoneID;
167
			$this->_dnsTTL = $dnsTTL;
168
			$this->_if = get_failover_interface($dnsIf);
169
			$this->_checkIP();
170
			$this->_dnsUpdateURL = $dnsUpdateURL;
171
			$this->_dnsResultMatch = $dnsResultMatch;
172
			$this->_dnsRequestIf = get_failover_interface($dnsRequestIf);
173
			if ($this->_dnsVerboseLog)
174
				log_error("DynDNS ({$this->_dnsHost}): running get_failover_interface for {$dnsRequestIf}. found {$this->_dnsRequestIf}");
175
			$this->_dnsRequestIfIP = get_interface_ip($dnsRequestIf);
176
			$this->_dnsMaxCacheAgeDays = 25;
177
			$this->_dnsDummyUpdateDone = false;
178
			$this->_forceUpdateNeeded = $forceUpdate;
179
			
180
			// Ensure that we were able to lookup the IP
181
			if(!is_ipaddr($this->_dnsIP)) {
182
				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.");
183
				unlock($dyndnslck);
184
				return;
185
			}
186

    
187
			$this->_debugID = rand(1000000, 9999999);
188
			
189
			if ($forceUpdate == false && $this->_detectChange() == false) {
190
				$this->_error(10);
191
			} else {
192
				switch ($this->_dnsService) {
193
				case 'dnsomatic':
194
				case 'dyndns':
195
				case 'dyndns-static':
196
				case 'dyndns-custom':
197
				case 'dhs':
198
				case 'noip':
199
				case 'noip-free':
200
				case 'easydns':
201
				case 'hn':
202
				case 'zoneedit':
203
				case 'dyns':
204
				case 'ods':
205
				case 'freedns':
206
				case 'loopia':
207
				case 'staticcling':
208
				case 'dnsexit':
209
				case 'custom':
210
				case 'opendns':
211
				case 'namecheap':
212
				case 'he-net':
213
				case 'selfhost':
214
				case 'he-net-tunnelbroker':
215
				case 'route53':
216
				case 'cloudflare':
217
					$this->_update();
218
					if($this->_dnsDummyUpdateDone == true) {
219
						// If a dummy update was needed, then sleep a while and do the update again to put the proper address back.
220
						// Some providers (e.g. No-IP free accounts) need to have at least 1 address change every month.
221
						// If the address has not changed recently, or the user did "Force Update", then the code does
222
						// a dummy address change for providers like this.
223
						sleep(10);
224
						$this->_update();
225
					}
226
					break;
227
				default:
228
					$this->_error(6);
229
					break;
230
				}
231
			}
232

    
233
			unlock($dyndnslck);
234
		}
235
			
236
		/*
237
		 * Private Function (added 12 July 05) [beta]
238
		 *   Send Update To Selected Service.
239
		 */
240
		function _update() {
241
		
242
			if ($this->_dnsVerboseLog)
243
				log_error("DynDNS ({$this->_dnsHost}): DynDns _update() starting.");
244
		
245
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53 ') {
246
				$ch = curl_init();
247
				curl_setopt($ch, CURLOPT_HEADER, 0);
248
				curl_setopt($ch, CURLOPT_USERAGENT, $this->_UserAgent);
249
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
250
				curl_setopt($ch, CURLOPT_INTERFACE, $this->_dnsRequestIfIP);
251
				curl_setopt($ch, CURLOPT_TIMEOUT, 120); // Completely empirical
252
			}
253

    
254
			switch ($this->_dnsService) {
255
				case 'dyndns':
256
				case 'dyndns-static':
257
				case 'dyndns-custom':
258
					$needsIP = FALSE;
259
					if ($this->_dnsVerboseLog)
260
						log_error("DynDNS: ({$this->_dnsHost}) DNS update() starting.");
261
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
262
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
263
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
264
					$server = "https://members.dyndns.org/nic/update";
265
					$port = "";
266
					if($this->_dnsServer)
267
						$server = $this->_dnsServer;
268
					if($this->_dnsPort)
269
						$port = ":" . $this->_dnsPort;
270
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
271
					break;
272
				case 'dhs':
273
					$needsIP = TRUE;
274
					$post_data['hostscmd'] = 'edit';
275
					$post_data['hostscmdstage'] = '2';
276
					$post_data['type'] = '4';
277
					$post_data['updatetype'] = 'Online';
278
					$post_data['mx'] = $this->_dnsMX;
279
					$post_data['mx2'] = '';
280
					$post_data['txt'] = '';
281
					$post_data['offline_url'] = '';
282
					$post_data['cloak'] = 'Y';
283
					$post_data['cloak_title'] = '';
284
					$post_data['ip'] = $this->_dnsIP;
285
					$post_data['domain'] = 'dyn.dhs.org';
286
					$post_data['hostname'] = $this->_dnsHost;
287
					$post_data['submit'] = 'Update';
288
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
289
					$server = "https://members.dhs.org/nic/hosts";
290
					$port = "";
291
					if($this->_dnsServer)
292
						$server = $this->_dnsServer;
293
					if($this->_dnsPort)
294
						$port = ":" . $this->_dnsPort;					
295
					curl_setopt($ch, CURLOPT_URL, '{$server}{$port}');
296
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
297
					curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
298
					break;
299
				case 'noip':
300
				case 'noip-free':
301
					$needsIP = TRUE;
302
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
303
					$server = "http://dynupdate.no-ip.com/ducupdate.php";
304
					$port = "";
305
					if($this->_dnsServer)
306
						$server = $this->_dnsServer;
307
					if($this->_dnsPort)
308
						$port = ":" . $this->_dnsPort;
309
					if(($this->_dnsService == "noip-free") && 
310
					   ($this->_forceUpdateNeeded == true) && 
311
					   ($this->_dnsDummyUpdateDone == false)) {
312
						// Update the IP to a dummy value to force No-IP free accounts to see a change.
313
						$iptoset = "192.168.1.1";
314
						$this->_dnsDummyUpdateDone = true;
315
						log_error("DynDNS ({$this->_dnsHost}): Processing dummy update on No-IP free account. IP temporarily set to " . $iptoset);
316
					} else {
317
						$iptoset = $this->_dnsIP;
318
					}
319
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?username=' . urlencode($this->_dnsUser) . '&pass=' . urlencode($this->_dnsPass) . '&hostname=' . $this->_dnsHost.'&ip=' . $iptoset);
320
					break;
321
				case 'easydns':
322
					$needsIP = TRUE;
323
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
324
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
325
					$server = "http://members.easydns.com/dyn/dyndns.php";
326
					$port = "";
327
					if($this->_dnsServer)
328
						$server = $this->_dnsServer;
329
					if($this->_dnsPort)
330
						$port = ":" . $this->_dnsPort;
331
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard=' . $this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=' . $this->_dnsBackMX);
332
					break;
333
				case 'hn':
334
					$needsIP = TRUE;
335
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
336
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
337
					$server = "http://dup.hn.org/vanity/update";
338
					$port = "";
339
					if($this->_dnsServer)
340
						$server = $this->_dnsServer;
341
					if($this->_dnsPort)
342
						$port = ":" . $this->_dnsPort;
343
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?ver=1&IP=' . $this->_dnsIP);
344
					break;
345
				case 'zoneedit':
346
					$needsIP = FALSE;
347
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
348
					curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
349
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
350

    
351
					$server = "https://dynamic.zoneedit.com/auth/dynamic.html";
352
					$port = "";
353
					if($this->_dnsServer)
354
						$server = $this->_dnsServer;
355
					if($this->_dnsPort)
356
						$port = ":" . $this->_dnsPort;
357
					curl_setopt($ch, CURLOPT_URL, "{$server}{$port}?host=" .$this->_dnsHost);
358
					break;
359
				case 'dyns':
360
					$needsIP = FALSE;
361
					$server = "http://www.dyns.cx/postscript011.php";
362
					$port = "";
363
					if($this->_dnsServer)
364
						$server = $this->_dnsServer;
365
					if($this->_dnsPort)
366
						$port = ":" . $this->_dnsPort;					
367
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?username=' . urlencode($this->_dnsUser) . '&password=' . $this->_dnsPass . '&host=' . $this->_dnsHost);
368
					break;
369
				case 'ods':
370
					$needsIP = FALSE;
371
					$misc_errno = 0;
372
					$misc_error = "";
373
					$server = "ods.org";
374
					$port = "";
375
					if($this->_dnsServer)
376
						$server = $this->_dnsServer;
377
					if($this->_dnsPort)
378
						$port = ":" . $this->_dnsPort;						
379
					$this->con['socket'] = fsockopen("{$server}{$port}", "7070", $misc_errno, $misc_error, 30);
380
					/* Check that we have connected */
381
					if (!$this->con['socket']) {
382
						print "error! could not connect.";
383
						break;
384
					}
385
					/* Here is the loop. Read the incoming data (from the socket connection) */
386
					while (!feof($this->con['socket'])) {
387
						$this->con['buffer']['all'] = trim(fgets($this->con['socket'], 4096));
388
						$code = substr($this->con['buffer']['all'], 0, 3);
389
						sleep(1);
390
						switch($code) {
391
							case 100:
392
								fputs($this->con['socket'], "LOGIN ".$this->_dnsUser." ".$this->_dnsPass."\n");
393
								break;
394
							case 225:
395
								fputs($this->con['socket'], "DELRR ".$this->_dnsHost." A\n");
396
								break;
397
							case 901:
398
								fputs($this->con['socket'], "ADDRR ".$this->_dnsHost." A ".$this->_dnsIP."\n");
399
								break;
400
							case 795:
401
								fputs($this->con['socket'], "QUIT\n");
402
								break;
403
						}
404
					}
405
					$this->_checkStatus(0, $code);
406
					break;
407
				case 'freedns':
408
					$needIP = FALSE;
409
					curl_setopt($ch, CURLOPT_URL, 'http://freedns.afraid.org/dynamic/update.php?' . $this->_dnsPass);
410
					break;
411
				case 'dnsexit':
412
					$needsIP = TRUE;
413
					curl_setopt($ch, CURLOPT_URL, 'http://www.dnsexit.com/RemoteUpdate.sv?login='.$this->_dnsUser. '&password='.$this->_dnsPass.'&host='.$this->_dnsHost.'&myip='.$this->_dnsIP);
414
					break;
415
				case 'loopia':
416
					$needsIP = TRUE;
417
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
418
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
419
					curl_setopt($ch, CURLOPT_URL, 'https://dns.loopia.se/XDynDNSServer/XDynDNS.php?hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP);
420
					break;
421
				case 'opendns':
422
					$needsIP = FALSE;
423
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
424
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
425
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
426
					$server = "https://updates.opendns.com/nic/update?hostname=". $this->_dnsHost;
427
					$port = "";
428
					if($this->_dnsServer)
429
						$server = $this->_dnsServer;
430
					if($this->_dnsPort)
431
						$port = ":" . $this->_dnsPort;
432
					curl_setopt($ch, CURLOPT_URL, $server .$port);
433
					break;
434

    
435
				case 'staticcling':
436
					$needsIP = FALSE;
437
					curl_setopt($ch, CURLOPT_URL, 'http://www.staticcling.org/update.html?login='.$this->_dnsUser.'&pass='.$this->_dnsPass);
438
					break;	                    
439
				case 'dnsomatic':
440
					/* Example syntax 
441
						https://username:password@updates.dnsomatic.com/nic/update?hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG
442
					*/
443
					$needsIP = FALSE;
444
					if ($this->_dnsVerboseLog)
445
						log_error("DNS-O-Matic: DNS update() starting.");
446
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
447
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
448
					/*
449
					Reference: https://www.dnsomatic.com/wiki/api
450
						DNS-O-Matic usernames are 3-25 characters.
451
						DNS-O-Matic passwords are 6-20 characters.
452
						All ASCII letters and numbers accepted.
453
						Dots, dashes, and underscores allowed, but not at the beginning or end of the string.
454
					Required: "rawurlencode" http://www.php.net/manual/en/function.rawurlencode.php
455
						Encodes the given string according to RFC 3986.
456
					*/
457
					$server = "https://" . rawurlencode($this->_dnsUser) . ":" . rawurlencode($this->_dnsPass) . "@updates.dnsomatic.com/nic/update?hostname=";
458
					if($this->_dnsServer)
459
						$server = $this->_dnsServer;
460
					if($this->_dnsPort)
461
						$port = ":" . $this->_dnsPort;
462
					curl_setopt($ch, CURLOPT_URL, $server . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NOCHG');
463
					break;
464
				case 'namecheap':
465
					/* Example:
466
						https://dynamicdns.park-your-domain.com/update?host=[host_name]&domain=[domain.com]&password=[domain_password]&ip=[your_ip]
467
					*/
468
					$needsIP = FALSE;
469
					if ($this->_dnsVerboseLog)
470
						log_error("Namecheap ({$this->_dnsHost}): DNS update() starting.");
471
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
472
					$dparts = explode(".", trim($this->_dnsHost));
473
					$domain_part_count = ($dparts[count($dparts)-1] == "uk") ? 3 : 2;
474
					$domain_offset = count($dparts) - $domain_part_count;
475
					$hostname = implode(".", array_slice($dparts, 0, $domain_offset));
476
					$domain = implode(".", array_slice($dparts, $domain_offset));
477
					$dnspass = trim($this->_dnsPass);
478
					$server = "https://dynamicdns.park-your-domain.com/update?host={$hostname}&domain={$domain}&password={$dnspass}&ip={$this->_dnsIP}";
479
					curl_setopt($ch, CURLOPT_URL, $server);
480
					break;
481
				case 'he-net':
482
					$needsIP = FALSE;
483
					if ($this->_dnsVerboseLog)
484
						log_error("HE.net ({$this->_dnsHost}): DNS update() starting.");
485
					$server = "https://dyn.dns.he.net/nic/update?";
486
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
487
					curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
488
					curl_setopt($ch, CURLOPT_URL, $server . 'hostname=' . $this->_dnsHost . '&password=' . $this->_dnsPass . '&myip=' . $this->_dnsIP);
489
					break;
490
				case 'he-net-tunnelbroker':
491
					$needsIP = FALSE;
492
					if ($this->_dnsVerboseLog)
493
						log_error("HE.net Tunnelbroker: DNS update() starting.");
494
					$server = "https://ipv4.tunnelbroker.net/ipv4_end.php?";
495
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
496
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser . ':' . $this->_dnsPass);
497
					curl_setopt($ch, CURLOPT_URL, $server . 'tid=' . $this->_dnsHost);
498
					break;
499
				case 'selfhost':
500
					$needsIP = FALSE;
501
					if ($this->_dnsVerboseLog)
502
						log_error("SelfHost: DNS update() starting.");
503
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
504
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
505
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
506
					$server = "https://carol.selfhost.de/nic/update";
507
					$port = "";
508
					if($this->_dnsServer)
509
						$server = $this->_dnsServer;
510
					if($this->_dnsPort)
511
						$port = ":" . $this->_dnsPort;
512
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
513
					break;
514
				case 'route53':
515
					if ($this->_dnsVerboseLog)
516
						log_error("Route53 ({$this->_dnsHost}): DNS update() starting.");
517
					
518
					/* Setting Variables */
519
					$hostname = "{$this->_dnsHost}.";
520
					$ZoneID = $this->_dnsZoneID;
521
					$AccessKeyId=$this->_dnsUser;
522
					$SecretAccessKey=$this->_dnsPass;
523
					$NewIP=$this->_dnsIP;
524
					$NewTTL=$this->_dnsTTL;
525

    
526
					/* Include Route 53 Library Class */
527
					require_once('/etc/inc/r53.class');
528

    
529
					/* Set Amazon AWS Credentials for this record */
530
					$r53 = new Route53($AccessKeyId, $SecretAccessKey);
531

    
532
					/* Function to find old values of records in Route 53 */
533
					if(!function_exists('Searchrecords')) {
534
						function SearchRecords($records, $name) {
535
							$result = array();
536
							foreach($records as $record) {
537
								if(strtolower($record['Name']) == strtolower($name)) {
538
									$result [] = $record;
539
								}
540
							}
541
							return ($result) ? $result : false;
542
						}
543
					}
544

    
545
					$records = $r53->listResourceRecordSets("/hostedzone/$ZoneID");
546

    
547
					/* Get IP for your hostname in Route 53 */
548
					if(false !== ($a_result = SearchRecords($records['ResourceRecordSets'], "$hostname"))) {
549
						$OldTTL=$a_result[0][TTL];
550
						$OldIP=$a_result[0][ResourceRecords][0];
551
					} else {
552
						$OldIP="";
553
					}
554

    
555
					/* Check if we need to update DNS Record */
556
					if ($OldIP !== $NewIP) {
557
						if(!empty($OldIP)) {
558
							/* Your Hostname already exists, deleting and creating it again */
559
							$changes = array();
560
							$changes[] = $r53->prepareChange(DELETE, $hostname, A, $OldTTL, $OldIP);
561
							$changes[] = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
562
							$result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
563
						} else {
564
							/* Your Hostname does not exist yet, creating it */
565
							$changes = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
566
							$result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
567
						}
568
					}
569
					$this->_checkStatus(0, $result);
570
					break;
571
				case 'custom':
572
					if ($this->_dnsVerboseLog)
573
						log_error("Custom DDNS ({$this->_dnsHost}): DNS update() starting.");
574
					if (strstr($this->dnsUpdateURL, "%IP%")) {$needsIP = TRUE;} else {$needsIP = FALSE;}
575
					if ($this->_dnsUser != '') {
576
						if ($this->_curlIpresolveV4)
577
							curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
578
						if ($this->_curlSslVerifypeer)
579
							curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
580
						else
581
							curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
582
						curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
583
						curl_setopt($ch, CURLOPT_USERPWD, "{$this->_dnsUser}:{$this->_dnsPass}");
584
					}
585
					$server = str_replace("%IP%", $this->_dnsIP, $this->_dnsUpdateURL);
586
					if ($this->_dnsVerboseLog)
587
						log_error("Sending request to: ".$server);
588
					curl_setopt($ch, CURLOPT_URL, $server);
589
					break;
590
				case 'cloudflare':
591
					$needsIP = TRUE;
592
					$dnsServer ='www.cloudflare.com';
593
					$dnsHost = str_replace(' ','', $this->_dnsHost);
594
					$URL = "https://{$dnsServer}/api.html?a=DIUP&email={$this->_dnsUser}&tkn={$this->_dnsPass}&ip={$this->dnsIP}&hosts={$dnsHost}";
595
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
596
					curl_setopt($ch, CURLOPT_URL, $URL);
597
					break;
598
				default:
599
					break;
600
			}
601
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53') {
602
				$data = curl_exec($ch);
603
				$this->_checkStatus($ch, $data);
604
				@curl_close($ch);
605
			}
606
		}
607

    
608
		/*
609
		 * Private Function (added 12 July 2005) [beta]
610
		 *   Retrieve Update Status
611
		 */
612
		function _checkStatus($ch, $data) {
613
			if ($this->_dnsVerboseLog) {
614
				log_error("DynDNS ({$this->_dnsHost}): DynDns _checkStatus() starting.");
615
				log_error("DynDNS ({$this->_dnsHost}): Current Service: {$this->_dnsService}");
616
			}
617
			$successful_update = false;
618
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53' && @curl_error($ch)) {
619
				$status = "Curl error occurred: " . curl_error($ch);
620
				log_error($status);
621
				$this->status = $status;
622
				return;
623
			}
624
			switch ($this->_dnsService) {
625
				case 'dnsomatic':
626
					if (preg_match('/badauth/i', $data)) {
627
						$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.";
628
					} else if (preg_match('/notfqdn /i', $data)) {
629
						$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.";
630
					} else if (preg_match('/nohost/i', $data)) {
631
						$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.";
632
					} else if (preg_match('/numhost/i', $data)) {
633
						$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.";	
634
					} else if (preg_match('/abuse/i', $data)) {
635
						$status = "DNS-O-Matic ({$this->_dnsHost}): The hostname is blocked for update abuse.";
636
					} else if (preg_match('/good/i', $data)) {
637
						$status = "DNS-O-Matic ({$this->_dnsHost}): (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
638
						$successful_update = true;
639
					} else if (preg_match('/dnserr/i', $data)) {
640
						$status = "DNS-O-Matic ({$this->_dnsHost}): DNS error encountered. Stop updating for 30 minutes.";
641
					} else {
642
						$status = "DNS-O-Matic ({$this->_dnsHost}): (Unknown Response)";
643
						log_error("DNS-O-Matic ({$this->_dnsHost}): PAYLOAD: {$data}");
644
						$this->_debug($data);
645
					}
646
					break;
647
				case 'dyndns':
648
					if (preg_match('/notfqdn/i', $data)) {
649
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Not A FQDN!";
650
					} else if (preg_match('/nochg/i', $data)) {
651
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) No Change In IP Address";
652
						$successful_update = true;
653
					} else if (preg_match('/good/i', $data)) {
654
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
655
						$successful_update = true;
656
					} else if (preg_match('/noauth/i', $data)) {
657
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) User Authorization Failed";
658
					} else {
659
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
660
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
661
						$this->_debug($data);
662
					}
663
					break;
664
				case 'dyndns-static':
665
					if (preg_match('/notfqdn/i', $data)) {
666
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Not A FQDN!";
667
					} else if (preg_match('/nochg/i', $data)) {
668
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) No Change In IP Address";
669
						$successful_update = true;
670
					} else if (preg_match('/good/i', $data)) {
671
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully!";
672
						$successful_update = true;
673
					} else if (preg_match('/noauth/i', $data)) {
674
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) User Authorization Failed";
675
					} else {
676
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
677
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
678
						$this->_debug($data);
679
					}
680
					break;
681
				case 'dyndns-custom':
682
					if (preg_match('/notfqdn/i', $data)) {
683
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Not A FQDN!";
684
					} else if (preg_match('/nochg/i', $data)) {
685
						$status = "phpDynDNS: (Success) No Change In IP Address";
686
						$successful_update = true;
687
					} else if (preg_match('/good/i', $data)) {
688
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully!";
689
						$successful_update = true;
690
					} else if (preg_match('/noauth/i', $data)) {
691
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) User Authorization Failed";
692
					} else {
693
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
694
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
695
						$this->_debug($data);
696
					}
697
					break;
698
				case 'dhs':
699
					break;
700
				case 'noip':
701
				case 'noip-free':
702
					list($ip,$code) = explode(":",$data);
703
					switch ($code) {
704
						case 0:
705
							$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP address is current, no update performed.";
706
							$successful_update = true;
707
							break;
708
						case 1:
709
							$status = "phpDynDNS ({$this->_dnsHost}): (Success) DNS hostname update successful.";
710
							$successful_update = true;
711
							break;
712
						case 2:
713
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Hostname supplied does not exist.";
714
							break;
715
						case 3:
716
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Invalid Username.";
717
							break;
718
						case 4:
719
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Invalid Password.";
720
							break;
721
						case 5:
722
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) To many updates sent.";
723
							break;
724
						case 6:
725
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Account disabled due to violation of No-IP terms of service.";
726
							break;
727
						case 7:
728
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Invalid IP. IP Address submitted is improperly formatted or is a private IP address or is on a blacklist.";
729
							break;
730
						case 8:
731
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Disabled / Locked Hostname.";
732
							break;
733
						case 9:
734
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Host updated is configured as a web redirect and no update was performed.";
735
							break;
736
						case 10:
737
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Group supplied does not exist.";
738
							break;
739
						case 11:
740
							$status = "phpDynDNS ({$this->_dnsHost}): (Success) DNS group update is successful.";
741
							$successful_update = true;
742
							break;
743
						case 12:
744
							$status = "phpDynDNS ({$this->_dnsHost}): (Success) DNS group is current, no update performed.";
745
							$successful_update = true;
746
							break;
747
						case 13:
748
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Update client support not available for supplied hostname or group.";
749
							break;
750
						case 14:
751
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Hostname supplied does not have offline settings configured.";
752
							break;
753
						case 99:
754
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
755
							break;
756
						case 100:
757
							$status = "phpDynDNS ({$this->_dnsHost}): (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
758
							break;
759
						default:
760
							$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
761
							$this->_debug("Unknown Response: ".$data);
762
							break;
763
					}
764
					break;
765
				case 'easydns':
766
					if (preg_match('/NOACCESS/i', $data)) {
767
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Authentication Failed: Username and/or Password was Incorrect.";
768
					} else if (preg_match('/NOSERVICE/i', $data)) {
769
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) No Service: Dynamic DNS Service has been disabled for this domain.";
770
					} else if (preg_match('/ILLEGAL INPUT/i', $data)) {
771
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Illegal Input: Self-Explanatory";
772
					} else if (preg_match('/TOOSOON/i', $data)) {
773
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Too Soon: Not Enough Time Has Elapsed Since Last Update";
774
					} else if (preg_match('/NOERROR/i', $data)) {
775
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Updated Successfully!";
776
						$successful_update = true;
777
					} else {
778
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
779
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
780
						$this->_debug($data);
781
					}
782
					break;
783
				case 'hn':
784
					/* FIXME: add checks */
785
					break;
786
				case 'zoneedit':
787
					if (preg_match('/799/i', $data)) {
788
						$status = "phpDynDNS ({$this->_dnsHost}): (Error 799) Update Failed!";				
789
					} else if (preg_match('/700/i', $data)) {
790
						$status = "phpDynDNS ({$this->_dnsHost}): (Error 700) Update Failed!";
791
					} else if (preg_match('/200/i', $data)) {
792
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
793
						$successful_update = true;
794
					} else if (preg_match('/201/i', $data)) {
795
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
796
						$successful_update = true;						
797
					} else {
798
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
799
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
800
						$this->_debug($data);
801
					}
802
					break;
803
				case 'dyns':
804
					if (preg_match("/400/i", $data)) {
805
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Bad Request - The URL was malformed. Required parameters were not provided.";
806
					} else if (preg_match('/402/i', $data)) {
807
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Update Too Soon - You have tried updating to quickly since last change.";
808
					} else if (preg_match('/403/i', $data)) {
809
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Database Error - There was a server-sided database error.";
810
					} else if (preg_match('/405/i', $data)) {
811
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Hostname Error - The hostname (".$this->_dnsHost.") doesn't belong to you.";
812
					} else if (preg_match('/200/i', $data)) {
813
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
814
						$successful_update = true;
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 'ods':
822
					if (preg_match("/299/i", $data)) {
823
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
824
						$successful_update = true;
825
					} else {
826
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
827
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
828
						$this->_debug($data);
829
					}
830
					break;
831
				case 'freedns':
832
					if (preg_match("/has not changed./i", $data)) {
833
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) No Change In IP Address";
834
						$successful_update = true;
835
					} else if (preg_match("/Updated/i", $data)) {
836
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully!";
837
						$successful_update = true;
838
					} else {
839
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
840
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
841
						$this->_debug($data);
842
					} 
843
					break;
844
				case 'dnsexit':
845
					if (preg_match("/is the same/i", $data)) {
846
						$status = "phpDynDns ({$this->_dnsHost}): (Success) No Change In IP Address";
847
						$successful_update = true;
848
					} else if (preg_match("/Success/i", $data)) {
849
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully!";
850
						$successful_update = true;
851
					} else {
852
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
853
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
854
						$this->_debug($data);
855
					}
856
					break;
857
				case 'loopia':
858
					if (preg_match("/nochg/i", $data)) {
859
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) No Change In IP Address";
860
						$successful_update = true;
861
					} else if (preg_match("/good/i", $data)) {
862
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully!";
863
						$successful_update = true;
864
					} else if (preg_match('/badauth/i', $data)) {
865
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) User Authorization Failed";
866
					} else {
867
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
868
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
869
						$this->_debug($data);
870
					}
871
					break;
872
				case 'opendns':
873
					if (preg_match('/badauth/i', $data)) {
874
						$status = "phpDynDNS({$this->_dnsHost}): (Error) Not a valid username or password!";
875
					} else if (preg_match('/nohost/i', $data)) {
876
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Hostname you are trying to update does not exist.";
877
						$successful_update = true;
878
					} else if (preg_match('/good/i', $data)) {
879
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
880
						$successful_update = true;
881
					} else if (preg_match('/yours/i', $data)) {
882
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) hostname specified exists, but not under the username specified.";
883
					} else if (preg_match('/abuse/i', $data)) {
884
						$status = "phpDynDns ({$this->_dnsHost}): (Error) Updating too frequently, considered abuse.";
885
					} else {
886
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
887
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
888
						$this->_debug($data);
889
					}
890
					break;
891
				case 'staticcling':
892
					if (preg_match("/invalid ip/i", $data)) {
893
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Bad Request - The IP provided was invalid.";
894
					} else if (preg_match('/required info missing/i', $data)) {
895
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Bad Request - Required parameters were not provided.";
896
					} else if (preg_match('/invalid characters/i', $data)) {
897
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Bad Request - Illegal characters in either the username or the password.";
898
					} else if (preg_match('/bad password/i', $data)) {
899
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Invalid password.";
900
					} else if (preg_match('/account locked/i', $data)) {
901
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) This account has been administratively locked.";
902
					} else if (preg_match('/update too frequent/i', $data)) {
903
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Updating too frequently.";
904
					} else if (preg_match('/DB error/i', $data)) {
905
						$status = "phpDynDNS ({$this->_dnsHost}): (Error) Server side error.";
906
					} else if (preg_match('/success/i', $data)) {
907
						$status = "phpDynDNS ({$this->_dnsHost}): (Success) IP Address Updated Successfully!";
908
						$successful_update = true;
909
					} else {
910
						$status = "phpDynDNS ({$this->_dnsHost}): (Unknown Response)";
911
						log_error("phpDynDNS ({$this->_dnsHost}): PAYLOAD: {$data}");
912
						$this->_debug($data);
913
					}
914
					break;
915
				case 'namecheap':
916
					$tmp = str_replace("^M", "", $data);
917
					$ncresponse = @xml2array($tmp);
918
					if (preg_match("/internal server error/i", $data)) {
919
						$status = "phpDynDNS: (Error) Server side error.";
920
					} else if (preg_match("/request is badly formed/i", $data)) {
921
						$status = "phpDynDNS: (Error) Badly Formed Request (check your settings).";
922
					} else if ($ncresponse['interface-response']['ErrCount'] === "0") {
923
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
924
						$successful_update = true;
925
					} else if (is_numeric($ncresponse['interface-response']['ErrCount']) && ($ncresponse['interface-response']['ErrCount'] > 0)) {
926
						$status = "phpDynDNS: (Error) " . implode(", ", $ncresponse["interface-response"]["errors"]);
927
						$successful_update = true;
928
					} else {
929
						$status = "phpDynDNS: (Unknown Response)";
930
						log_error("phpDynDNS: PAYLOAD: {$data}");
931
						$this->_debug($data);
932
					}
933
					break;
934
					
935
				case 'he-net':
936
					if (preg_match("/badip/i", $data)) {
937
						$status = "phpDynDNS: (Error) Bad Request - The IP provided was invalid.";
938
					} else if (preg_match('/nohost/i', $data)) {
939
						$status = "phpDynDNS: (Error) Bad Request - A hostname was not provided.";
940
					} else if (preg_match('/badauth/i', $data)) {
941
						$status = "phpDynDNS: (Error) Invalid username or password.";
942
					} else if (preg_match('/good/i', $data)) {
943
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
944
						$successful_update = true;
945
					} else if (preg_match('/nochg/i', $data)) {
946
						$status = "phpDynDNS: (Success) No Change In IP Address.";
947
						$successful_update = true;
948
					} else {
949
						$status = "phpDynDNS: (Unknown Response)";
950
						log_error("phpDynDNS: PAYLOAD: {$data}");
951
						$this->_debug($data);
952
					}
953
					break;
954
				case 'he-net-tunnelbroker':
955
					/*
956
					-ERROR: Missing parameter(s).
957
					-ERROR: Invalid API key or password
958
					-ERROR: Tunnel not found
959
					-ERROR: Another tunnel exists for this IP.
960
					-ERROR: This tunnel is already associated with this IP address
961
					+OK: Tunnel endpoint updated to: x.x.x.x
962
					*/
963
					if (preg_match("/Missing parameter/i", $data)) {
964
						$status = "phpDynDNS: (Error) Bad Request - Missing/Invalid Parameters.";
965
					} else if (preg_match('/Tunnel not found/i', $data)) {
966
						$status = "phpDynDNS: (Error) Bad Request - Invalid Tunnel ID.";
967
					} else if (preg_match('/Invalid API key or password/i', $data)) {
968
						$status = "phpDynDNS: (Error) Invalid username or password.";
969
					} else if (preg_match('/OK:/i', $data)) {
970
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
971
						$successful_update = true;
972
					} else if (preg_match('/This tunnel is already associated with this IP address/i', $data)) {
973
						$status = "phpDynDNS: (Success) No Change In IP Address.";
974
						$successful_update = true;
975
					} else {
976
						$status = "phpDynDNS: (Unknown Response)";
977
						log_error("phpDynDNS: PAYLOAD: {$data}");
978
						$this->_debug($data);
979
					}
980
					break;
981
				case 'selfhost':
982
					if (preg_match('/notfqdn/i', $data)) {
983
						$status = "phpDynDNS: (Error) Not A FQDN!";
984
					} else if (preg_match('/nochg/i', $data)) {
985
						$status = "phpDynDNS: (Success) No Change In IP Address";
986
						$successful_update = true;
987
					} else if (preg_match('/good/i', $data)) {
988
						$status = "phpDynDNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
989
						$successful_update = true;
990
					} else if (preg_match('/noauth/i', $data)) {
991
						$status = "phpDynDNS: (Error) User Authorization Failed";
992
					} else {
993
						$status = "phpDynDNS: (Unknown Response)";
994
						log_error("phpDynDNS: PAYLOAD: {$data}");
995
						$this->_debug($data);
996
					}
997
					break;
998
				case 'route53':
999
					$successful_update = true;
1000
					break;
1001
				case 'custom':
1002
					$successful_update = false;
1003
					if ($this->_dnsResultMatch == "") {
1004
						$successful_update = true;
1005
					}else {
1006
						$this->_dnsResultMatch = str_replace("%IP%", $this->_dnsIP, $this->_dnsResultMatch);
1007
						$matches = preg_split("/(?<!\\\\)\\|/", $this->_dnsResultMatch);
1008
						foreach($matches as $match) {
1009
							$match= str_replace("\\|", "|", $match);
1010
							if(strcmp($match, trim($data, "\t\n\r")) == 0)
1011
								$successful_update = true;
1012
						}
1013
						unset ($matches);
1014
					}
1015
					if ($successful_update == true)
1016
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
1017
					else
1018
						$status = "phpDynDNS: (Error) Result did not match.";
1019
					break;
1020
				case 'cloudflare':
1021
					// recieve multipe results
1022
					$data = explode("\n",$data);
1023
					$lines = count($data)-1;
1024

    
1025
					// loop over the lines
1026
					for ($pos=0; ($successful_update || $pos == 0) && $pos < $lines; $pos++){
1027
						$resp = $data[$pos];
1028
						if (preg_match('/UAUTH/i', $resp)) {
1029
							$status = "DynDNS: The username specified is not authorized to update this hostname and domain.";
1030
						} else if (preg_match('/NOHOST/i', $resp)) {
1031
							$status = "DynDNS: No valid FQDN (fully qualified domain name) was specified";
1032
						} else if (preg_match('/INVLDHST/i', $resp)) {
1033
							$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.";
1034
						} else if (preg_match('/INVLDIP/i', $resp)) {
1035
							$status = "DynDNS: The IP address given is not valid.";
1036
						} else if (preg_match('/DUPHST/i', $resp)) {
1037
							$status = "DynDNS: Duplicate values exist for a record. Only single values for records are supported currently.";
1038
						} else if (preg_match('/NOUPDATE/i', $resp)) {
1039
							$status = "DynDNS: No changes made to the hostname (".strtok($resp,' ')."). Continual updates with no changes lead to blocked clients.";
1040
							$successful_update = true; //success if it is the same so that it saves
1041
						} else if (preg_match('/OK/i', $resp)) {
1042
							$status = "DynDNS: (Success) (".strtok($resp,' ').") IP Address for Changed Successfully!";
1043
							$successful_update = true;
1044
						} else {
1045
							$status = "DynDNS: (Unknown Response)";
1046
							log_error("DynDNS: PAYLOAD: {$resp}");
1047
							$this->_debug($resp);
1048
						}
1049
						log_error($status);
1050
					}
1051
					break;
1052
			}
1053
			
1054
			if($successful_update == true) {
1055
				/* Write WAN IP to cache file */
1056
				$wan_ip = $this->_checkIP();
1057
				conf_mount_rw();
1058
				if ($wan_ip > 0) {
1059
					$currentTime = time();
1060
					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));
1061
					log_error("phpDynDNS: updating cache file {$this->_cacheFile}: {$wan_ip}");
1062
					@file_put_contents($this->_cacheFile, "{$wan_ip}:{$currentTime}");
1063
				} else
1064
					@unlink($this->_cacheFile);
1065
				conf_mount_ro();
1066
			}
1067
			$this->status = $status;
1068
			log_error($status);
1069
		}
1070

    
1071
		/*
1072
		 * Private Function (added 12 July 05) [beta]
1073
		 *   Return Error, Set Last Error, and Die.
1074
		 */
1075
		function _error($errorNumber = '1') {
1076
			switch ($errorNumber) {
1077
				case 0:
1078
					break;
1079
				case 2:
1080
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
1081
					break;
1082
				case 3:
1083
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
1084
					break;
1085
				case 4:
1086
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
1087
					break;
1088
				case 5:
1089
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
1090
					break;
1091
				case 6:
1092
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
1093
					break;
1094
				case 7:
1095
					$error = 'phpDynDNS: (ERROR!) No Update URL Provided.';
1096
					break;
1097
				case 8:
1098
					$status = "Route 53: (Error) Invalid ZoneID";
1099
					break;
1100
				case 9:
1101
					$status = "Route 53: (Error) Invalid TTL";
1102
					break;  
1103
				case 10:
1104
					$error = "phpDynDNS ({$this->_dnsHost}): No change in my IP address and/or " . $this->_dnsMaxCacheAgeDays . " days has not passed. Not updating dynamic DNS entry.";
1105
					break;
1106
				default:
1107
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
1108
					/* FIXME: $data isn't in scope here */
1109
					/* $this->_debug($data); */
1110
					break;
1111
			}
1112
			$this->lastError = $error;
1113
			log_error($error);
1114
		}
1115

    
1116
		/*
1117
		 * Private Function (added 12 July 05) [beta]
1118
		 *   - Detect whether or not IP needs to be updated.
1119
		 *      | Written Specifically for pfSense (pfsense.com) may
1120
		 *      | work with other systems. pfSense base is FreeBSD.
1121
		 */
1122
		function _detectChange() {
1123
			global $debug;
1124

    
1125
			if ($debug)
1126
				log_error("DynDns ({$this->_dnsHost}): _detectChange() starting.");
1127
		
1128
			$currentTime = time();
1129

    
1130
			$wan_ip = $this->_checkIP();
1131
			if ($wan_ip == 0) {
1132
				log_error("DynDns ({$this->_dnsHost}): Current WAN IP could not be determined, skipping update process.");
1133
				return false;
1134
			}
1135
			$log_error = "DynDns ({$this->_dnsHost}): Current WAN IP: {$wan_ip} ";
1136

    
1137
			if (file_exists($this->_cacheFile)) {
1138
				$contents = file_get_contents($this->_cacheFile);
1139
				list($cacheIP,$cacheTime) = explode(':', $contents);
1140
				$this->_debug($cacheIP.'/'.$cacheTime);
1141
				$initial = false;
1142
				$log_error .= "Cached IP: {$cacheIP} ";
1143
			} else {
1144
				conf_mount_rw();
1145
				$cacheIP = '0.0.0.0';
1146
				@file_put_contents($this->_cacheFile, "0.0.0.0:{$currentTime}");
1147
				conf_mount_ro();
1148
				$cacheTime = $currentTime;
1149
				$initial = true;
1150
				$log_error .= "No Cached IP found.";
1151
			}
1152
			if ($this->_dnsVerboseLog)
1153
				log_error($log_error);
1154

    
1155
			// Convert seconds = days * hr/day * min/hr * sec/min
1156
			$maxCacheAgeSecs = $this->_dnsMaxCacheAgeDays * 24 * 60 * 60;
1157

    
1158
			$needs_updating = FALSE;
1159
			/* lets determine if the item needs updating */
1160
			if ($cacheIP != $wan_ip) {
1161
				$needs_updating = true;
1162
				$update_reason = "DynDns: cacheIP != wan_ip.  Updating. ";
1163
				$update_reason .= "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
1164
			}
1165
			if (($currentTime - $cacheTime) > $maxCacheAgeSecs) {
1166
				$needs_updating = true;
1167
				$this->_forceUpdateNeeded = true;
1168
				$update_reason = "DynDns: More than " . $this->_dnsMaxCacheAgeDays . " days.  Updating. ";
1169
				$update_reason .= "{$currentTime} - {$cacheTime} > {$maxCacheAgeSecs} ";
1170
			}
1171
			if ($initial == true) {
1172
				$needs_updating = true;
1173
				$update_reason .= "Initial update. ";
1174
			}
1175

    
1176
			/*   finally if we need updating then store the
1177
			 *   new cache value and return true
1178
			 */
1179
			if ($needs_updating == true) {
1180
				if ($this->_dnsVerboseLog)
1181
					log_error("DynDns ({$this->_dnsHost}): {$update_reason}");
1182
				return true;
1183
			}
1184

    
1185
			return false;			
1186
		}
1187

    
1188
		/*
1189
		 * Private Function (added 16 July 05) [beta]
1190
		 *   - Writes debug information to a file.
1191
		 *   - This function is only called when a unknown response
1192
		 *   - status is returned from a DynDNS service provider.
1193
		 */
1194
		function _debug($data) {
1195
			global $g;
1196

    
1197
			if (!$g['debug'])
1198
				return;
1199
			$string = date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data."\n";
1200
			conf_mount_rw();
1201
			$file = fopen($this->_debugFile, 'a');
1202
			fwrite($file, $string);
1203
			fclose($file);
1204
			conf_mount_ro();
1205
		}
1206
		function _checkIP() {
1207
			global $debug;
1208

    
1209
			if ($debug)
1210
				log_error("DynDns ({$this->_dnsHost}): _checkIP() starting.");
1211

    
1212
			$ip_address = find_interface_ip($this->_if);
1213
			if (!is_ipaddr($ip_address))
1214
				return 0;
1215
			if (is_private_ip($ip_address)) {
1216
				$hosttocheck = "checkip.dyndns.org";
1217
				$try = 0;
1218
				while ($try < 3) {
1219
					$checkip = gethostbyname($hosttocheck);
1220
					if (is_ipaddr($checkip))
1221
						break;
1222
					$try++;
1223
				}
1224
				if ($try >= 3) {
1225
					log_error("Dyndns debug information ({$this->_dnsHost}): Could not resolve {$hosttocheck} to ip using interface ip {$ip_address}.");
1226
					return 0;
1227
				}
1228
				$ip_ch = curl_init("http://{$checkip}");
1229
				curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
1230
				curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
1231
				curl_setopt($ip_ch, CURLOPT_INTERFACE, $ip_address);
1232
				curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
1233
				curl_setopt($ip_ch, CURLOPT_TIMEOUT, 120);
1234
				$ip_result_page = curl_exec($ip_ch);
1235
				curl_close($ip_ch);
1236
				$ip_result_decoded = urldecode($ip_result_page);
1237
				preg_match('/Current IP Address: (.*)<\/body>/', $ip_result_decoded, $matches);
1238
				$ip_address = trim($matches[1]);
1239
				if (is_ipaddr($ip_address)) {
1240
					if ($this->_dnsVerboseLog)
1241
						log_error("DynDns ({$this->_dnsHost}): {$ip_address} extracted from {$hosttocheck}");
1242
				} else {
1243
					log_error("DynDns ({$this->_dnsHost}): IP address could not be extracted from {$hosttocheck}");
1244
					return 0;
1245
				}
1246
			} else {
1247
				if ($this->_dnsVerboseLog)
1248
					log_error("DynDns ({$this->_dnsHost}): {$ip_address} extracted from local system.");
1249
			}
1250
			$this->_dnsIP = $ip_address;
1251

    
1252
			return $ip_address;
1253
		}
1254

    
1255
	}
1256

    
1257
?>
(17-17/66)