Project

General

Profile

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

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

    
120
			log_error("DynDns: updatedns() starting");
121

    
122
			$dyndnslck = lock("DDNS".$dnsID, LOCK_EX);
123

    
124
			if (!$dnsService) $this->_error(2);
125
			switch ($dnsService) {
126
			case 'freedns':
127
				if (!$dnsHost) $this->_error(5);
128
				break;
129
			case 'namecheap':
130
				if (!$dnsPass) $this->_error(4);
131
				if (!$dnsHost) $this->_error(5);
132
				break;
133
			case 'route53':
134
				if (!$dnsZoneID) $this->_error(8);
135
				if (!$dnsTTL) $this->_error(9);
136
				break;
137
			case 'custom':
138
				if (!$dnsUpdateURL) $this->_error(7);
139
				break;
140
			default:
141
				if (!$dnsUser) $this->_error(3);
142
				if (!$dnsPass) $this->_error(4);
143
				if (!$dnsHost) $this->_error(5);
144
			}
145
			
146
			$this->_dnsService = strtolower($dnsService);
147
			$this->_dnsUser = $dnsUser;
148
			$this->_dnsPass = $dnsPass;
149
			$this->_dnsHost = $dnsHost;
150
			$this->_dnsServer = $dnsServer;
151
			$this->_dnsPort = $dnsPort;
152
			$this->_dnsWildcard = $dnsWildcard;
153
			$this->_dnsMX = $dnsMX;
154
                        $this->_dnsZoneID = $dnsZoneID;
155
                        $this->_dnsTTL = $dnsTTL;
156
			$this->_if = get_real_interface($dnsIf);
157
			$this->_checkIP();
158
			$this->_dnsUpdateURL = $dnsUpdateURL;
159
			$this->_dnsResultMatch = $dnsResultMatch;
160
			$this->_dnsRequestIf = get_real_interface($dnsRequestIf);
161
			$this->_dnsRequestIfIP = get_interface_ip($dnsRequestIf);
162

    
163
			// Ensure that we where able to lookup the IP
164
			if(!is_ipaddr($this->_dnsIP)) {
165
				log_error("There was an error trying to determine the public IP for interface - {$dnsIf}({$this->_if}). Probably interface is not a WAN interface.");
166
				unlock($dyndnslck);
167
				return;
168
			}
169

    
170
			$this->_debugID = rand(1000000, 9999999);
171
			
172
			if ($forceUpdate == false && $this->_detectChange() == false) {
173
				$this->_error(10);
174
			} else {
175
				switch ($this->_dnsService) {
176
				case 'dnsomatic':
177
				case 'dyndns':
178
				case 'dyndns-static':
179
				case 'dyndns-custom':
180
				case 'dhs':
181
				case 'noip':
182
				case 'easydns':
183
				case 'hn':
184
				case 'zoneedit':
185
				case 'dyns':
186
				case 'ods':
187
				case 'freedns':
188
				case 'loopia':
189
				case 'staticcling':
190
				case 'dnsexit':
191
				case 'custom':
192
				case 'opendns':
193
				case 'namecheap':
194
				case 'he-net':
195
				case 'selfhost':
196
				case 'he-net-tunnelbroker':
197
				case 'route53':
198
					$this->_update();	
199
					break;
200
				default:
201
					$this->_error(6);
202
					break;
203
				}
204
			}
205

    
206
			unlock($dyndnslck);
207
		}
208
			
209
		/*
210
		 * Private Function (added 12 July 05) [beta]
211
		 *   Send Update To Selected Service.
212
		 */
213
		function _update() {
214
		
215
			log_error("DynDns: DynDns _update() starting.");
216
		
217
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53 ') {
218
				$ch = curl_init();
219
				curl_setopt($ch, CURLOPT_HEADER, 0);
220
				curl_setopt($ch, CURLOPT_USERAGENT, $this->_UserAgent);
221
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
222
				curl_setopt($ch, CURLOPT_INTERFACE, $this->_dnsRequestIfIP);
223
				curl_setopt($ch, CURLOPT_TIMEOUT, 120); // Completely empirical
224
			}
225

    
226
			switch ($this->_dnsService) {
227
				case 'dyndns':
228
				case 'dyndns-static':
229
				case 'dyndns-custom':
230
					$needsIP = FALSE;
231
					//log_error("DynDns: DynDns _update() starting. Dynamic");
232
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
233
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
234
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
235
					$server = "https://members.dyndns.org/nic/update";
236
					$port = "";
237
					if($this->_dnsServer)
238
						$server = $this->_dnsServer;
239
					if($this->_dnsPort)
240
						$port = ":" . $this->_dnsPort;
241
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
242
					break;
243
				case 'dhs':
244
					$needsIP = TRUE;
245
					$post_data['hostscmd'] = 'edit';
246
					$post_data['hostscmdstage'] = '2';
247
					$post_data['type'] = '4';
248
					$post_data['updatetype'] = 'Online';
249
					$post_data['mx'] = $this->_dnsMX;
250
					$post_data['mx2'] = '';
251
					$post_data['txt'] = '';
252
					$post_data['offline_url'] = '';
253
					$post_data['cloak'] = 'Y';
254
					$post_data['cloak_title'] = '';
255
					$post_data['ip'] = $this->_dnsIP;
256
					$post_data['domain'] = 'dyn.dhs.org';
257
					$post_data['hostname'] = $this->_dnsHost;
258
					$post_data['submit'] = 'Update';
259
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
260
					$server = "https://members.dhs.org/nic/hosts";
261
					$port = "";
262
					if($this->_dnsServer)
263
						$server = $this->_dnsServer;
264
					if($this->_dnsPort)
265
						$port = ":" . $this->_dnsPort;					
266
					curl_setopt($ch, CURLOPT_URL, '{$server}{$port}');
267
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
268
					curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
269
					break;
270
				case 'noip':
271
					$needsIP = TRUE;
272
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
273
					$server = "http://dynupdate.no-ip.com/ducupdate.php";
274
					$port = "";
275
					if($this->_dnsServer)
276
						$server = $this->_dnsServer;
277
					if($this->_dnsPort)
278
						$port = ":" . $this->_dnsPort;
279
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?username=' . urlencode($this->_dnsUser) . '&pass=' . urlencode($this->_dnsPass) . '&hostname=' . $this->_dnsHost.'&ip=' . $this->_dnsIP);
280
					break;
281
				case 'easydns':
282
					$needsIP = TRUE;
283
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
284
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
285
					$server = "http://members.easydns.com/dyn/dyndns.php";
286
					$port = "";
287
					if($this->_dnsServer)
288
						$server = $this->_dnsServer;
289
					if($this->_dnsPort)
290
						$port = ":" . $this->_dnsPort;
291
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard=' . $this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=' . $this->_dnsBackMX);
292
					break;
293
				case 'hn':
294
					$needsIP = TRUE;
295
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
296
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
297
					$server = "http://dup.hn.org/vanity/update";
298
					$port = "";
299
					if($this->_dnsServer)
300
						$server = $this->_dnsServer;
301
					if($this->_dnsPort)
302
						$port = ":" . $this->_dnsPort;
303
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?ver=1&IP=' . $this->_dnsIP);
304
					break;
305
				case 'zoneedit':
306
					$needsIP = FALSE;
307
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
308
					curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
309
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
310

    
311
					$server = "https://dynamic.zoneedit.com/auth/dynamic.html";
312
					$port = "";
313
					if($this->_dnsServer)
314
						$server = $this->_dnsServer;
315
					if($this->_dnsPort)
316
						$port = ":" . $this->_dnsPort;
317
					curl_setopt($ch, CURLOPT_URL, "{$server}{$port}?host=" .$this->_dnsHost);
318
					break;
319
				case 'dyns':
320
					$needsIP = FALSE;
321
					$server = "http://www.dyns.cx/postscript011.php";
322
					$port = "";
323
					if($this->_dnsServer)
324
						$server = $this->_dnsServer;
325
					if($this->_dnsPort)
326
						$port = ":" . $this->_dnsPort;					
327
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?username=' . urlencode($this->_dnsUser) . '&password=' . $this->_dnsPass . '&host=' . $this->_dnsHost);
328
					break;
329
				case 'ods':
330
					$needsIP = FALSE;
331
					$misc_errno = 0;
332
					$misc_error = "";
333
					$server = "ods.org";
334
					$port = "";
335
					if($this->_dnsServer)
336
						$server = $this->_dnsServer;
337
					if($this->_dnsPort)
338
						$port = ":" . $this->_dnsPort;						
339
					$this->con['socket'] = fsockopen("{$server}{$port}", "7070", $misc_errno, $misc_error, 30);
340
					/* Check that we have connected */
341
					if (!$this->con['socket']) {
342
						print "error! could not connect.";
343
						break;
344
					}
345
					/* Here is the loop. Read the incoming data (from the socket connection) */
346
					while (!feof($this->con['socket'])) {
347
						$this->con['buffer']['all'] = trim(fgets($this->con['socket'], 4096));
348
						$code = substr($this->con['buffer']['all'], 0, 3);
349
						sleep(1);
350
						switch($code) {
351
							case 100:
352
								fputs($this->con['socket'], "LOGIN ".$this->_dnsUser." ".$this->_dnsPass."\n");
353
								break;
354
							case 225:
355
								fputs($this->con['socket'], "DELRR ".$this->_dnsHost." A\n");
356
								break;
357
							case 901:
358
								fputs($this->con['socket'], "ADDRR ".$this->_dnsHost." A ".$this->_dnsIP."\n");
359
								break;
360
							case 795:
361
								fputs($this->con['socket'], "QUIT\n");
362
								break;
363
						}
364
					}
365
					$this->_checkStatus(0, $code);
366
					break;
367
				case 'freedns':
368
					$needIP = FALSE;
369
					curl_setopt($ch, CURLOPT_URL, 'http://freedns.afraid.org/dynamic/update.php?' . $this->_dnsPass);
370
					break;
371
				case 'dnsexit':
372
					$needsIP = TRUE;
373
					curl_setopt($ch, CURLOPT_URL, 'http://www.dnsexit.com/RemoteUpdate.sv?login='.$this->_dnsUser. '&password='.$this->_dnsPass.'&host='.$this->_dnsHost.'&myip='.$this->_dnsIP);
374
					break;
375
				case 'loopia':
376
					$needsIP = TRUE;
377
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
378
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
379
					curl_setopt($ch, CURLOPT_URL, 'https://dns.loopia.se/XDynDNSServer/XDynDNS.php?hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP);
380
					break;
381
				case 'opendns':
382
					$needsIP = FALSE;
383
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
384
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
385
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
386
					$server = "https://updates.opendns.com/nic/update?hostname=". $this->_dnsHost;
387
					$port = "";
388
					if($this->_dnsServer)
389
						$server = $this->_dnsServer;
390
					if($this->_dnsPort)
391
						$port = ":" . $this->_dnsPort;
392
					curl_setopt($ch, CURLOPT_URL, $server .$port);
393
					break;
394

    
395
				case 'staticcling':
396
					$needsIP = FALSE;
397
					curl_setopt($ch, CURLOPT_URL, 'http://www.staticcling.org/update.html?login='.$this->_dnsUser.'&pass='.$this->_dnsPass);
398
					break;	                    
399
				case 'dnsomatic':
400
					/* Example syntax 
401
						https://username:password@updates.dnsomatic.com/nic/update?hostname=yourhostname&myip=ipaddress&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG
402
					*/
403
					$needsIP = FALSE;
404
					log_error("DNS-O-Matic: DNS update() starting.");
405
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
406
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
407
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
408
					$server = "https://" . $this->_dnsUser . ":" . $this->_dnsPass . "@updates.dnsomatic.com/nic/update?hostname=";
409
					if($this->_dnsServer)
410
						$server = $this->_dnsServer;
411
					if($this->_dnsPort)
412
						$port = ":" . $this->_dnsPort;
413
					curl_setopt($ch, CURLOPT_URL, $server . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NOCHG');
414
					break;
415
				case 'namecheap':
416
					/* Example:
417
						https://dynamicdns.park-your-domain.com/update?host=[host_name]&domain=[domain.com]&password=[domain_password]&ip=[your_ip]
418
					*/
419
					$needsIP = FALSE;
420
					log_error("Namecheap: DNS update() starting.");
421
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
422
					$dparts = explode(".", $this->_dnsHost);
423
					$domain_part_count = ($dparts[count($dparts)-1] == "uk") ? 3 : 2;
424
					$domain_offset = count($dparts) - $domain_part_count;
425
					$hostname = implode(".", array_slice($dparts, 0, $domain_offset));
426
					$domain = implode(".", array_slice($dparts, $domain_offset));
427
					$server = "https://dynamicdns.park-your-domain.com/update?host={$hostname}&domain={$domain}&password={$this->_dnsPass}&ip={$this->_dnsIP}";
428
					curl_setopt($ch, CURLOPT_URL, $server);
429
					break;
430
				case 'he-net':
431
					$needsIP = FALSE;
432
					log_error("HE.net: DNS update() starting.");
433
					$server = "https://dyn.dns.he.net/nic/update?";
434
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
435
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsHost . ':' . $this->_dnsPass);
436
					curl_setopt($ch, CURLOPT_URL, $server . 'hostname=' . $this->_dnsHost);
437
					break;
438
				case 'he-net-tunnelbroker':
439
					$needsIP = FALSE;
440
					log_error("HE.net Tunnelbroker: DNS update() starting.");
441
					$server = "https://ipv4.tunnelbroker.net/ipv4_end.php?";
442
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
443
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser . ':' . $this->_dnsPass);
444
					curl_setopt($ch, CURLOPT_URL, $server . 'tid=' . $this->_dnsHost);
445
					break;
446
				case 'selfhost':
447
					$needsIP = FALSE;
448
					//log_error("SelfHost: DNS update() starting.");
449
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
450
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
451
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
452
					$server = "https://carol.selfhost.de/nic/update";
453
					$port = "";
454
					if($this->_dnsServer)
455
						$server = $this->_dnsServer;
456
					if($this->_dnsPort)
457
						$port = ":" . $this->_dnsPort;
458
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
459
					break;
460
                               case 'route53':
461
                                        log_error("Route53: DNS update() starting.");
462
                                       
463
                                        /* Setting Variables */
464
                                        $hostname = "{$this->_dnsHost}.";
465
                                        $ZoneID = $this->_dnsZoneID;
466
                                        $AccessKeyId=$this->_dnsUser;
467
                                        $SecretAccessKey=$this->_dnsPass;
468
                                        $NewIP=$this->_dnsIP;
469
                                        $NewTTL=$this->_dnsTTL;
470

    
471
                                        /* Include Route 53 Library Class */
472
                                        require_once('/etc/inc/r53.class');
473

    
474
                                        /* Set Amazon AWS Credentials for this record */
475
                                        $r53 = new Route53($AccessKeyId, $SecretAccessKey);
476

    
477
                                        /* Function to find old values of records in Route 53 */
478
                                        if(!function_exists('Searchrecords')) {
479
                                        function SearchRecords($records, $name) {
480
                                                $result = array();
481
                                                foreach($records as $record) {
482
                                                        if(strtolower($record['Name']) == strtolower($name)) {
483
                                                                $result [] = $record;
484
                                                        }
485
                                                }
486
                                                return ($result) ? $result : false;
487
                                        }}
488

    
489
                                        $records = $r53->listResourceRecordSets("/hostedzone/$ZoneID");
490

    
491
                                        /* Get IP for your hostname in Route 53 */
492
                                        if(false !== ($a_result = SearchRecords($records['ResourceRecordSets'], "$hostname"))) {
493
                                                $OldTTL=$a_result[0][TTL];
494
                                                $OldIP=$a_result[0][ResourceRecords][0];
495
                                        } else {
496
                                                $OldIP="";
497
                                        }
498

    
499
                                        /* Check if we need update DNS Record */
500
                                        if ($OldIP !== $NewIP) {
501
                                                if(!empty($OldIP)) {
502
                                                        /* Your Hostname already exist, deleting and creating it again */
503
                                                        $changes = array();
504
                                                        $changes[] = $r53->prepareChange(DELETE, $hostname, A, $OldTTL, $OldIP);
505
                                                        $changes[] = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
506
                                                        $result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
507
                                                        } else {
508
                                                        /* Your Hostname dosent exist yet, creating it */
509
                                                        $changes = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
510
                                                        $result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
511
                                                        }
512
                                                }
513
                                        $this->_checkStatus(0, $result);
514
                                                break;
515
				case 'custom':
516
					log_error("Custom DDNS: DNS update() starting.");
517
					if (strstr($this->dnsUpdateURL, "%IP%")) {$needsIP = TRUE;} else {$needsIP = FALSE;}
518
					if ($this->_dnsUser != '') {
519
						curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
520
						curl_setopt($ch, CURLOPT_USERPWD, "{$this->_dnsUser}:{$this->_dnsPass}");
521
					}
522
					$server = str_replace("%IP%", $this->_dnsIP, $this->_dnsUpdateURL);
523
					log_error("Sending request to: ".$server);
524
					curl_setopt($ch, CURLOPT_URL, $server);
525
					break;
526
				default:
527
					break;
528
			}
529
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53') {
530
				$data = curl_exec($ch);
531
				$this->_checkStatus($ch, $data);
532
				@curl_close($ch);
533
			}
534
		}
535

    
536
		/*
537
		 * Private Function (added 12 July 2005) [beta]
538
		 *   Retrieve Update Status
539
		 */
540
		function _checkStatus($ch, $data) {
541
			log_error("DynDns: DynDns _checkStatus() starting.");
542
			log_error("DynDns: Current Service: {$this->_dnsService}");
543
			$successful_update = false;
544
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53' && @curl_error($ch)) {
545
				$status = "Curl error occurred: " . curl_error($ch);
546
				log_error($status);
547
				$this->status = $status;
548
				return;
549
			}
550
			switch ($this->_dnsService) {
551
				case 'dnsomatic':
552
					if (preg_match('/badauth/i', $data)) {
553
						$status = "DNS-O-Matic: The DNS-O-Matic username or password specified are incorrect. No updates will be distributed to services until this is resolved.";
554
					} else if (preg_match('/notfqdn /i', $data)) {
555
						$status = "DNS-O-Matic: The hostname specified is not a fully-qualified domain name. If no hostnames included, notfqdn will be returned once.";
556
					} else if (preg_match('/nohost/i', $data)) {
557
						$status = "DNS-O-Matic: The hostname passed could not be matched to any services configured. The service field will be blank in the return code.";
558
					} else if (preg_match('/numhost/i', $data)) {
559
						$status = "DNS-O-Matic: You may update up to 20 hosts. numhost is returned if you try to update more than 20 or update a round-robin.";	
560
					} else if (preg_match('/abuse/i', $data)) {
561
						$status = "DNS-O-Matic: The hostname is blocked for update abuse.";
562
					} else if (preg_match('/good/i', $data)) {
563
						$status = "DNS-O-Matic: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
564
						$successful_update = true;
565
					} else if (preg_match('/dnserr/i', $data)) {
566
						$status = "DNS-O-Matic: DNS error encountered. Stop updating for 30 minutes.";
567
					} else {
568
						$status = "DNS-O-Matic: (Unknown Response)";
569
						log_error("DNS-O-Matic: PAYLOAD: {$data}");
570
						$this->_debug($data);
571
					}
572
					break;
573
				case 'dyndns':
574
					if (preg_match('/notfqdn/i', $data)) {
575
						$status = "phpDynDNS: (Error) Not A FQDN!";
576
					} else if (preg_match('/nochg/i', $data)) {
577
						$status = "phpDynDNS: (Success) No Change In IP Address";
578
						$successful_update = true;
579
					} else if (preg_match('/good/i', $data)) {
580
						$status = "phpDynDNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
581
						$successful_update = true;
582
					} else if (preg_match('/noauth/i', $data)) {
583
						$status = "phpDynDNS: (Error) User Authorization Failed";
584
					} else {
585
						$status = "phpDynDNS: (Unknown Response)";
586
						log_error("phpDynDNS: PAYLOAD: {$data}");
587
						$this->_debug($data);
588
					}
589
					break;
590
				case 'dyndns-static':
591
					if (preg_match('/notfqdn/i', $data)) {
592
						$status = "phpDynDNS: (Error) Not A FQDN!";
593
					} else if (preg_match('/nochg/i', $data)) {
594
						$status = "phpDynDNS: (Success) No Change In IP Address";
595
						$successful_update = true;
596
					} else if (preg_match('/good/i', $data)) {
597
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
598
						$successful_update = true;
599
					} else if (preg_match('/noauth/i', $data)) {
600
						$status = "phpDynDNS: (Error) User Authorization Failed";
601
					} else {
602
						$status = "phpDynDNS: (Unknown Response)";
603
						log_error("phpDynDNS: PAYLOAD: {$data}");
604
						$this->_debug($data);
605
					}
606
					break;
607
				case 'dyndns-custom':
608
					if (preg_match('/notfqdn/i', $data)) {
609
						$status = "phpDynDNS: (Error) Not A FQDN!";
610
					} else if (preg_match('/nochg/i', $data)) {
611
						$status = "phpDynDNS: (Success) No Change In IP Address";
612
						$successful_update = true;
613
					} else if (preg_match('/good/i', $data)) {
614
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
615
						$successful_update = true;
616
					} else if (preg_match('/noauth/i', $data)) {
617
						$status = "phpDynDNS: (Error) User Authorization Failed";
618
					} else {
619
						$status = "phpDynDNS: (Unknown Response)";
620
						log_error("phpDynDNS: PAYLOAD: {$data}");
621
						$this->_debug($data);
622
					}
623
					break;
624
				case 'dhs':
625
					break;
626
				case 'noip':
627
					list($ip,$code) = explode(":",$data);
628
					switch ($code) {
629
						case 0:
630
							$status = "phpDynDNS: (Success) IP address is current, no update performed.";
631
							$successful_update = true;
632
							break;
633
						case 1:
634
							$status = "phpDynDNS: (Success) DNS hostname update successful.";
635
							$successful_update = true;
636
							break;
637
						case 2:
638
							$status = "phpDynDNS: (Error) Hostname supplied does not exist.";
639
							break;
640
						case 3:
641
							$status = "phpDynDNS: (Error) Invalid Username.";
642
							break;
643
						case 4:
644
							$status = "phpDynDNS: (Error) Invalid Password.";
645
							break;
646
						case 5:
647
							$status = "phpDynDNS: (Error) To many updates sent.";
648
							break;
649
						case 6:
650
							$status = "phpDynDNS: (Error) Account disabled due to violation of No-IP terms of service.";
651
							break;
652
						case 7:
653
							$status = "phpDynDNS: (Error) Invalid IP. IP Address submitted is improperly formatted or is a private IP address or is on a blacklist.";
654
							break;
655
						case 8:
656
							$status = "phpDynDNS: (Error) Disabled / Locked Hostname.";
657
							break;
658
						case 9:
659
							$status = "phpDynDNS: (Error) Host updated is configured as a web redirect and no update was performed.";
660
							break;
661
						case 10:
662
							$status = "phpDynDNS: (Error) Group supplied does not exist.";
663
							break;
664
						case 11:
665
							$status = "phpDynDNS: (Success) DNS group update is successful.";
666
							$successful_update = true;
667
							break;
668
						case 12:
669
							$status = "phpDynDNS: (Success) DNS group is current, no update performed.";
670
							$successful_update = true;
671
							break;
672
						case 13:
673
							$status = "phpDynDNS: (Error) Update client support not available for supplied hostname or group.";
674
							break;
675
						case 14:
676
							$status = "phpDynDNS: (Error) Hostname supplied does not have offline settings configured.";
677
							break;
678
						case 99:
679
							$status = "phpDynDNS: (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
680
							break;
681
						case 100:
682
							$status = "phpDynDNS: (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
683
							break;
684
						default:
685
							$status = "phpDynDNS: (Unknown Response)";
686
							$this->_debug("Unknown Response: ".$data);
687
							break;
688
					}
689
					break;
690
				case 'easydns':
691
					if (preg_match('/NOACCESS/i', $data)) {
692
						$status = "phpDynDNS: (Error) Authentication Failed: Username and/or Password was Incorrect.";
693
					} else if (preg_match('/NOSERVICE/i', $data)) {
694
						$status = "phpDynDNS: (Error) No Service: Dynamic DNS Service has been disabled for this domain.";
695
					} else if (preg_match('/ILLEGAL INPUT/i', $data)) {
696
						$status = "phpDynDNS: (Error) Illegal Input: Self-Explantory";
697
					} else if (preg_match('/TOOSOON/i', $data)) {
698
						$status = "phpDynDNS: (Error) Too Soon: Not Enough Time Has Elapsed Since Last Update";
699
					} else if (preg_match('/NOERROR/i', $data)) {
700
						$status = "phpDynDNS: (Success) IP Updated Successfully!";
701
						$successful_update = true;
702
					} else {
703
						$status = "phpDynDNS: (Unknown Response)";
704
						log_error("phpDynDNS: PAYLOAD: {$data}");
705
						$this->_debug($data);
706
					}
707
					break;
708
				case 'hn':
709
					/* FIXME: add checks */
710
					break;
711
				case 'zoneedit':
712
					if (preg_match('/799/i', $data)) {
713
						$status = "phpDynDNS: (Error 799) Update Failed!";				
714
					} else if (preg_match('/700/i', $data)) {
715
						$status = "phpDynDNS: (Error 700) Update Failed!";
716
					} else if (preg_match('/200/i', $data)) {
717
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
718
						$successful_update = true;
719
					} else if (preg_match('/201/i', $data)) {
720
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
721
						$successful_update = true;						
722
					} else {
723
						$status = "phpDynDNS: (Unknown Response)";
724
						log_error("phpDynDNS: PAYLOAD: {$data}");
725
						$this->_debug($data);
726
					}
727
					break;
728
				case 'dyns':
729
					if (preg_match("/400/i", $data)) {
730
						$status = "phpDynDNS: (Error) Bad Request - The URL was malformed. Required parameters were not provided.";
731
					} else if (preg_match('/402/i', $data)) {
732
						$status = "phpDynDNS: (Error) Update Too Soon - You have tried updating to quickly since last change.";
733
					} else if (preg_match('/403/i', $data)) {
734
						$status = "phpDynDNS: (Error) Database Error - There was a server-sided database error.";
735
					} else if (preg_match('/405/i', $data)) {
736
						$status = "phpDynDNS: (Error) Hostname Error - The hostname (".$this->_dnsHost.") doesn't belong to you.";
737
					} else if (preg_match('/200/i', $data)) {
738
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
739
						$successful_update = true;
740
					} else {
741
						$status = "phpDynDNS: (Unknown Response)";
742
						log_error("phpDynDNS: PAYLOAD: {$data}");
743
						$this->_debug($data);
744
					}
745
					break;
746
				case 'ods':
747
					if (preg_match("/299/i", $data)) {
748
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
749
						$successful_update = true;
750
					} else {
751
						$status = "phpDynDNS: (Unknown Response)";
752
						log_error("phpDynDNS: PAYLOAD: {$data}");
753
						$this->_debug($data);
754
					}
755
					break;
756
				case 'freedns':
757
					if (preg_match("/has not changed./i", $data)) {
758
						$status = "phpDynDNS: (Success) No Change In IP Address";
759
						$successful_update = true;
760
					} else if (preg_match("/Updated/i", $data)) {
761
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
762
						$successful_update = true;
763
					} else {
764
						$status = "phpDynDNS: (Unknown Response)";
765
						log_error("phpDynDNS: PAYLOAD: {$data}");
766
						$this->_debug($data);
767
					} 
768
					break;
769
				case 'dnsexit':
770
					if (preg_match("/is the same/i", $data)) {
771
						$status = "phpDynDns: (Success) No Change In IP Address";
772
						$successful_update = true;
773
					} else if (preg_match("/Success/i", $data)) {
774
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
775
						$successful_update = true;
776
					} else {
777
						$status = "phpDynDNS: (Unknown Response)";
778
                                                log_error("phpDynDNS: PAYLOAD: {$data}");
779
                                                $this->_debug($data);
780
					}
781
					break;
782
				case 'loopia':
783
					if (preg_match("/nochg/i", $data)) {
784
						$status = "phpDynDNS: (Success) No Change In IP Address";
785
						$successful_update = true;
786
					} else if (preg_match("/good/i", $data)) {
787
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
788
						$successful_update = true;
789
					} else if (preg_match('/badauth/i', $data)) {
790
						$status = "phpDynDNS: (Error) User Authorization Failed";
791
					} else {
792
						$status = "phpDynDNS: (Unknown Response)";
793
						log_error("phpDynDNS: PAYLOAD: {$data}");
794
						$this->_debug($data);
795
					}
796
					break;
797
				case 'opendns':
798
					if (preg_match('/badauth/i', $data)) {
799
						$status = "phpDynDNS: (Error) Not a valid username or password!";
800
					} else if (preg_match('/nohost/i', $data)) {
801
						$status = "phpDynDNS: (Error) Hostname you are trying to update does not exist.";
802
						$successful_update = true;
803
					} else if (preg_match('/good/i', $data)) {
804
						$status = "phpDynDNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
805
						$successful_update = true;
806
					} else if (preg_match('/yours/i', $data)) {
807
						$status = "phpDynDNS: (Error) hostname specified exists, but not under the username specified.";
808
					} else if (preg_match('/abuse/i', $data)) {
809
						$status = "phpDynDns: (Error) Updating to frequently, considered abuse.";
810
					} else {
811
						$status = "phpDynDNS: (Unknown Response)";
812
						log_error("phpDynDNS: PAYLOAD: {$data}");
813
						$this->_debug($data);
814
					}
815
					break;
816
				case 'staticcling':
817
					if (preg_match("/invalid ip/i", $data)) {
818
					        $status = "phpDynDNS: (Error) Bad Request - The IP provided was invalid.";
819
					} else if (preg_match('/required info missing/i', $data)) {
820
					        $status = "phpDynDNS: (Error) Bad Request - Required parameters were not provided.";
821
					} else if (preg_match('/invalid characters/i', $data)) {
822
					        $status = "phpDynDNS: (Error) Bad Request - Illegal characters in either the username or the password.";
823
					} else if (preg_match('/bad password/i', $data)) {
824
					        $status = "phpDynDNS: (Error) Invalid password.";
825
					} else if (preg_match('/account locked/i', $data)) {
826
					        $status = "phpDynDNS: (Error) This account has been administratively locked.";
827
					} else if (preg_match('/update too frequent/i', $data)) {
828
					        $status = "phpDynDNS: (Error) Updating too frequently.";
829
					} else if (preg_match('/DB error/i', $data)) {
830
					        $status = "phpDynDNS: (Error) Server side error.";
831
					} else if (preg_match('/success/i', $data)) {
832
					        $status = "phpDynDNS: (Success) IP Address Updated Successfully!";
833
					        $successful_update = true;
834
					} else {
835
					        $status = "phpDynDNS: (Unknown Response)";
836
					        log_error("phpDynDNS: PAYLOAD: {$data}");
837
					        $this->_debug($data);
838
					}
839
					break;
840
				case 'namecheap':
841
					$tmp = str_replace("^M", "", $data);
842
					$ncresponse = @xml2array($tmp);
843
					if (preg_match("/internal server error/i", $data)) {
844
						$status = "phpDynDNS: (Error) Server side error.";
845
					} else if ($ncresponse['interface-response']['ErrCount'] === "0") {
846
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
847
						$successful_update = true;
848
					} else if (is_numeric($ncresponse['interface-response']['ErrCount']) && ($ncresponse['interface-response']['ErrCount'] > 0)) {
849
						$status = "phpDynDNS: (Error) " . implode(", ", $ncresponse["interface-response"]["errors"]);
850
						$successful_update = true;
851
					} else {
852
						$status = "phpDynDNS: (Unknown Response)";
853
						log_error("phpDynDNS: PAYLOAD: {$data}");
854
						$this->_debug($data);
855
					}
856
					break;
857
					
858
				case 'he-net':
859
					if (preg_match("/badip/i", $data)) {
860
					        $status = "phpDynDNS: (Error) Bad Request - The IP provided was invalid.";
861
					} else if (preg_match('/nohost/i', $data)) {
862
					        $status = "phpDynDNS: (Error) Bad Request - A hostname was not provided.";
863
					} else if (preg_match('/badauth/i', $data)) {
864
					        $status = "phpDynDNS: (Error) Invalid username or password.";
865
					} else if (preg_match('/good/i', $data)) {
866
					        $status = "phpDynDNS: (Success) IP Address Updated Successfully!";
867
					        $successful_update = true;
868
					} else if (preg_match('/nochg/i', $data)) {
869
							$status = "phpDynDNS: (Success) No Change In IP Address.";
870
							$successful_update = true;
871
					} else {
872
					        $status = "phpDynDNS: (Unknown Response)";
873
					        log_error("phpDynDNS: PAYLOAD: {$data}");
874
					        $this->_debug($data);
875
					}
876
					break;
877
				case 'he-net-tunnelbroker':
878
					/*
879
					-ERROR: Missing parameter(s).
880
					-ERROR: Invalid API key or password
881
					-ERROR: Tunnel not found
882
					-ERROR: Another tunnel exists for this IP.
883
					-ERROR: This tunnel is already associated with this IP address
884
					+OK: Tunnel endpoint updated to: x.x.x.x
885
					*/
886
					if (preg_match("/Missing parameter/i", $data)) {
887
					        $status = "phpDynDNS: (Error) Bad Request - Missing/Invalid Parameters.";
888
					} else if (preg_match('/Tunnel not found/i', $data)) {
889
					        $status = "phpDynDNS: (Error) Bad Request - Invalid Tunnel ID.";
890
					} else if (preg_match('/Invalid API key or password/i', $data)) {
891
					        $status = "phpDynDNS: (Error) Invalid username or password.";
892
					} else if (preg_match('/OK:/i', $data)) {
893
					        $status = "phpDynDNS: (Success) IP Address Updated Successfully!";
894
					        $successful_update = true;
895
					} else if (preg_match('/This tunnel is already associated with this IP address/i', $data)) {
896
							$status = "phpDynDNS: (Success) No Change In IP Address.";
897
							$successful_update = true;
898
					} else {
899
					        $status = "phpDynDNS: (Unknown Response)";
900
					        log_error("phpDynDNS: PAYLOAD: {$data}");
901
					        $this->_debug($data);
902
					}
903
					break;
904
				case 'selfhost':
905
					if (preg_match('/notfqdn/i', $data)) {
906
						$status = "phpDynDNS: (Error) Not A FQDN!";
907
					} else if (preg_match('/nochg/i', $data)) {
908
						$status = "phpDynDNS: (Success) No Change In IP Address";
909
						$successful_update = true;
910
					} else if (preg_match('/good/i', $data)) {
911
						$status = "phpDynDNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
912
						$successful_update = true;
913
					} else if (preg_match('/noauth/i', $data)) {
914
						$status = "phpDynDNS: (Error) User Authorization Failed";
915
					} else {
916
						$status = "phpDynDNS: (Unknown Response)";
917
						log_error("phpDynDNS: PAYLOAD: {$data}");
918
						$this->_debug($data);
919
					}
920
					break;
921
                                case 'route53':
922
                                        $successful_update = true;
923
                                        break;
924
				case 'custom':
925
					$successful_update = false;
926
					if ($this->_dnsResultMatch == "") {
927
						$successful_update = true;
928
					}else {
929
						$this->_dnsResultMatch = str_replace("%IP%", $this->_dnsIP, $this->_dnsResultMatch);
930
						$matches = preg_split("/(?<!\\\\)\\|/", $this->_dnsResultMatch);
931
						foreach($matches as $match) {
932
							$match= str_replace("\\|", "|", $match);
933
							if(strcmp($match, trim($data, "\t\n\r")) == 0)
934
								$successful_update = true;
935
						}
936
						unset ($matches);
937
					}
938
					if ($successful_update == true)
939
					        $status = "phpDynDNS: (Success) IP Address Updated Successfully!";
940
					else
941
					        $status = "phpDynDNS: (Error) Result did not match.";
942
					break;
943
			}
944
			
945
			if($successful_update == true) {
946
				/* Write WAN IP to cache file */
947
				$wan_ip = $this->_checkIP();
948
				conf_mount_rw();
949
				if ($wan_ip > 0) {
950
					$currentTime = time();				  
951
					log_error("phpDynDNS: updating cache file {$this->_cacheFile}: {$wan_ip}");
952
					@file_put_contents($this->_cacheFile, "{$wan_ip}:{$currentTime}");
953
				} else
954
					@unlink($this->_cacheFile);
955
				conf_mount_ro();
956
			}
957
			$this->status = $status;
958
			log_error($status);
959
		}
960

    
961
		/*
962
		 * Private Function (added 12 July 05) [beta]
963
		 *   Return Error, Set Last Error, and Die.
964
		 */
965
		function _error($errorNumber = '1') {
966
			switch ($errorNumber) {
967
				case 0:
968
					break;
969
				case 2:
970
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
971
					break;
972
				case 3:
973
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
974
					break;
975
				case 4:
976
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
977
					break;
978
				case 5:
979
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
980
					break;
981
				case 6:
982
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
983
					break;
984
				case 7:
985
					$error = 'phpDynDNS: (ERROR!) No Update URL Provided.';
986
					break;
987
                                case 8:
988
                                        $status = "Route 53: (Error) Invalid ZoneID";
989
					break;
990
                                case 9:
991
                                        $status = "Route 53: (Error) Invalid TTL";
992
                                        break;  
993
				case 10:
994
					$error = 'phpDynDNS: No change in my IP address and/or 25 days has not passed. Not updating dynamic DNS entry.';
995
					break;
996
				default:
997
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
998
					/* FIXME: $data isn't in scope here */
999
					/* $this->_debug($data); */
1000
					break;
1001
			}
1002
			$this->lastError = $error;
1003
			log_error($error);
1004
		}
1005

    
1006
		/*
1007
		 * Private Function (added 12 July 05) [beta]
1008
		 *   - Detect whether or not IP needs to be updated.
1009
		 *      | Written Specifically for pfSense (pfsense.com) may
1010
		 *      | work with other systems. pfSense base is FreeBSD.
1011
		 */
1012
		function _detectChange() {
1013
			global $debug;
1014

    
1015
			if ($debug)
1016
				log_error("DynDns: _detectChange() starting.");
1017
		
1018
			$currentTime = time();
1019

    
1020
			$wan_ip = $this->_checkIP();
1021
			if ($wan_ip == 0) {
1022
				log_error("DynDns: Current WAN IP could not be determined, skipping update process.");
1023
				return false;
1024
			}
1025
			$log_error = "DynDns: Current WAN IP: {$wan_ip} ";
1026

    
1027
			if (file_exists($this->_cacheFile)) {
1028
				$contents = file_get_contents($this->_cacheFile);
1029
				list($cacheIP,$cacheTime) = explode(':', $contents);
1030
				$this->_debug($cacheIP.'/'.$cacheTime);
1031
				$initial = false;
1032
				$log_error .= "Cached IP: {$cacheIP} ";
1033
			} else {
1034
				conf_mount_rw();
1035
				$cacheIP = '0.0.0.0';
1036
				@file_put_contents($this->_cacheFile, "0.0.0.0:{$currentTime}");
1037
				conf_mount_ro();
1038
				$cacheTime = $currentTime;
1039
				$initial = true;
1040
				$log_error .= "No Cached IP found.";
1041
			}
1042
			log_error($log_error);
1043

    
1044
			/*   use 2419200 for dyndns, dhs, easydns, noip, hn
1045
			 *   zoneedit, dyns, ods
1046
			 */
1047
			$time = '2160000';
1048

    
1049
			$needs_updating = FALSE;
1050
			/* lets determine if the item needs updating */
1051
			if ($cacheIP != $wan_ip) {
1052
				$needs_updating = true;
1053
				$update_reason = "DynDns: cacheIP != wan_ip.  Updating. ";
1054
				$update_reason .= "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
1055
			}
1056
			if (($currentTime - $cacheTime) > $time ) {
1057
				$needs_updating = true;
1058
				$update_reason = "DynDns: More than 25 days.  Updating. ";
1059
				$update_reason .= "{$currentTime} - {$cacheTime} > {$time} ";
1060
			}
1061
			if ($initial == true) {
1062
				$needs_updating = true;
1063
				$update_reason .= "Inital update. ";
1064
			}
1065

    
1066
			/*   finally if we need updating then store the
1067
			 *   new cache value and return true
1068
                         */
1069
			if ($needs_updating == true) {
1070
				log_error("DynDns debug information: {$update_reason}");
1071
				return true;
1072
			}
1073

    
1074
			return false;			
1075
		}
1076

    
1077
		/*
1078
		 * Private Funcation (added 16 July 05) [beta]
1079
		 *   - Writes debug information to a file.
1080
		 *   - This function is only called when a unknown response
1081
		 *   - status is returned from a DynDNS service provider.
1082
		 */
1083
		function _debug ($data) {
1084
			$string = '\n'.date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data.'\n';
1085
			conf_mount_rw();
1086
			$file = fopen($this->_debugFile, 'a');
1087
			fwrite($file, $string);
1088
			fclose($file);
1089
			conf_mount_ro();
1090
		}
1091
		function _checkIP() {
1092
			global $debug;
1093

    
1094
			if ($debug)
1095
				log_error("DynDns: _checkIP() starting.");
1096

    
1097
			$ip_address = find_interface_ip($this->_if);
1098
			if (!is_ipaddr($ip_address))
1099
				return 0;
1100
			if (is_private_ip($ip_address)) {
1101
				$hosttocheck = "checkip.dyndns.org";
1102
				$try = 0;
1103
				while ($try < 3) {
1104
					$checkip = gethostbyname($hosttocheck);
1105
					if (is_ipaddr($checkip))
1106
						break;
1107
					$try++;
1108
				}
1109
				if ($try >= 3) {
1110
					log_error("Dyndns debug information: Could not resolve {$hosttocheck} to ip using interface ip {$ip_address}.");
1111
					return 0;
1112
				}
1113
				$ip_ch = curl_init("http://{$checkip}");
1114
				curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
1115
				curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
1116
				curl_setopt($ip_ch, CURLOPT_INTERFACE, $ip_address);
1117
				curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
1118
				curl_setopt($ip_ch, CURLOPT_TIMEOUT, 120);
1119
				$ip_result_page = curl_exec($ip_ch);
1120
				curl_close($ip_ch);
1121
				$ip_result_decoded = urldecode($ip_result_page);
1122
				preg_match('/Current IP Address: (.*)<\/body>/', $ip_result_decoded, $matches);
1123
				$ip_address = trim($matches[1]);
1124
				if (is_ipaddr($ip_address))
1125
					log_error("DynDns debug information: {$ip_address} extracted from {$hosttocheck}");
1126
				else {
1127
					log_error("DynDns debug information: IP address could not be extracted from {$hosttocheck}");
1128
					return 0;
1129
				}
1130
			} else
1131
				log_error("DynDns debug information: {$ip_address} extracted from local system.");
1132

    
1133
			$this->_dnsIP = $ip_address;
1134

    
1135
			return $ip_address;
1136
		}
1137

    
1138
	}
1139

    
1140
?>
(18-18/65)