Project

General

Profile

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

    
71
	class updatedns {
72
		var $_cacheFile;
73
		var $_debugFile;
74
		var $_UserAgent = 'User-Agent: phpDynDNS/0.7';
75
		var $_errorVerbosity = 0;
76
		var $_dnsService;
77
		var $_dnsUser;
78
		var $_dnsPass;
79
		var $_dnsHost;
80
		var $_dnsIP;
81
		/* This is needed for support on addresses behind NAT. */
82
		var $_ifIP;
83
		var $_dnsWildcard;
84
		var $_dnsMX;
85
		var $_dnsBackMX;
86
		var $_dnsServer;
87
		var $_dnsPort;
88
		var $_dnsUpdateURL;
89
		var $_dnsZoneID;
90
		var $_dnsTTL;
91
		var $status;
92
		var $_debugID;
93
		var $_if;
94
		
95
		/* 
96
		 * Public Constructor Function (added 12 July 05) [beta]
97
		 *   - Gets the dice rolling for the update. 
98
		 */
99
		function updatedns ($dnsService = '', $dnsHost = '', $dnsUser = '', $dnsPass = '',
100
				    $dnsWildcard = 'OFF', $dnsMX = '', $dnsIf = '', $dnsBackMX = '',
101
				    $dnsServer = '', $dnsPort = '', $dnsUpdateURL = '', $forceUpdate = false,
102
				    $dnsZoneID ='', $dnsTTL='') {
103
			
104
			global $config, $g;
105
			
106
			$this->_cacheFile = "{$g['conf_path']}/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . ".cache";
107
			$this->_debugFile = "{$g['varetc_path']}/dyndns_{$dnsIf}{$dnsService}" . escapeshellarg($dnsHost) . ".debug";
108

    
109
			log_error("DynDns: updatedns() starting");
110

    
111
			$dyndnslck = lock($dnsHost, LOCK_EX);
112

    
113
			if (!$dnsService) $this->_error(2);
114
			switch ($dnsService) {
115
			case 'freedns':
116
				if (!$dnsHost) $this->_error(5);
117
				break;
118
			case 'namecheap':
119
				if (!$dnsPass) $this->_error(4);
120
				if (!$dnsHost) $this->_error(5);
121
				break;
122
                       case 'route53':
123
                                if (!$dnsZoneID) $this->_error(8);
124
                                if (!$dnsTTL) $this->_error(9);
125
                                break;
126

    
127
			default:
128
				if (!$dnsUser) $this->_error(3);
129
				if (!$dnsPass) $this->_error(4);
130
				if (!$dnsHost) $this->_error(5);
131
			}
132
			
133
			$this->_dnsService = strtolower($dnsService);
134
			$this->_dnsUser = $dnsUser;
135
			$this->_dnsPass = $dnsPass;
136
			$this->_dnsHost = $dnsHost;
137
			$this->_dnsServer = $dnsServer;
138
			$this->_dnsPort = $dnsPort;
139
			$this->_dnsWildcard = $dnsWildcard;
140
			$this->_dnsMX = $dnsMX;
141
                        $this->_dnsZoneID = $dnsZoneID;
142
                        $this->_dnsTTL = $dnsTTL;
143
			$this->_if = get_real_interface($dnsIf);
144
			$this->_ifIP = get_interface_ip($dnsIf);
145

    
146
			// Ensure that we where able to lookup the IP
147
			if(!is_ipaddr($this->_ifIP)) {
148
				log_error("There was an error trying to determine the IP for interface - {$dnsIf}({$this->_if}). Probably interface has no ip or is down. Dyndns update not possible for {$dnsService}.");
149
				unlock($dyndnslck);
150
				return;
151
			}
152

    
153
			$this->_debugID = rand(1000000, 9999999);
154
			
155
			if ($forceUpdate == false && $this->_detectChange() == false) {
156
				$this->_error(10);
157
			} else {
158
				switch ($this->_dnsService) {
159
				case 'dnsomatic':
160
				case 'dyndns':
161
				case 'dyndns-static':
162
				case 'dyndns-custom':
163
				case 'dhs':
164
				case 'noip':
165
				case 'easydns':
166
				case 'hn':
167
				case 'zoneedit':
168
				case 'dyns':
169
				case 'ods':
170
				case 'freedns':
171
				case 'loopia':
172
				case 'staticcling':
173
				case 'dnsexit':
174
				case 'opendns':
175
				case 'namecheap':
176
				case 'he-net':
177
				case 'selfhost':
178
				case 'route53':
179
					$this->_update();	
180
					break;
181
				case 'he-net-tunnelbroker':
182
					$this->_update();
183
					break;
184
				default:
185
					$this->_error(6);
186
					break;
187
				}
188
			}
189

    
190
			unlock($dyndnslck);
191
		}
192
			
193
		/*
194
		 * Private Function (added 12 July 05) [beta]
195
		 *   Send Update To Selected Service.
196
		 */
197
		function _update() {
198
		
199
			log_error("DynDns: DynDns _update() starting.");
200
		
201
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53 ') {
202
				$ch = curl_init();
203
				curl_setopt($ch, CURLOPT_HEADER, 0);
204
				curl_setopt($ch, CURLOPT_USERAGENT, $this->_UserAgent);
205
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
206
				curl_setopt($ch, CURLOPT_INTERFACE, $this->_ifIP);
207
				curl_setopt($ch, CURLOPT_TIMEOUT, 120); // Completely empirical
208
			}
209

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

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

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

    
455
                                        /* Include Route 53 Library Class */
456
                                        require_once('/etc/inc/r53.class');
457

    
458
                                        /* Set Amazon AWS Credentials for this record */
459
                                        $r53 = new Route53($AccessKeyId, $SecretAccessKey);
460

    
461
                                        /* Function to find old values of records in Route 53 */
462
                                        if(!function_exists('Searchrecords')) {
463
                                        function SearchRecords($records, $name) {
464
                                                $result = array();
465
                                                foreach($records as $record) {
466
                                                        if(strtolower($record['Name']) == strtolower($name)) {
467
                                                                $result [] = $record;
468
                                                        }
469
                                                }
470
                                                return ($result) ? $result : false;
471
                                        }}
472

    
473
                                        $records = $r53->listResourceRecordSets("/hostedzone/$ZoneID");
474

    
475
                                        /* Get IP for your hostname in Route 53 */
476
                                        if(false !== ($a_result = SearchRecords($records['ResourceRecordSets'], "$hostname"))) {
477
                                                $OldTTL=$a_result[0][TTL];
478
                                                $OldIP=$a_result[0][ResourceRecords][0];
479
                                        } else {
480
                                                $OldIP="";
481
                                        }
482

    
483
                                        /* Check if we need update DNS Record */
484
                                        if ($OldIP !== $NewIP) {
485
                                                if(!empty($OldIP)) {
486
                                                        /* Your Hostname already exist, deleting and creating it again */
487
                                                        $changes = array();
488
                                                        $changes[] = $r53->prepareChange(DELETE, $hostname, A, $OldTTL, $OldIP);
489
                                                        $changes[] = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
490
                                                        $result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
491
                                                        } else {
492
                                                        /* Your Hostname dosent exist yet, creating it */
493
                                                        $changes = $r53->prepareChange(CREATE, $hostname, A, $NewTTL, $NewIP);
494
                                                        $result = $r53->changeResourceRecordSets("/hostedzone/$ZoneID", $changes);
495
                                                        }
496
                                                }
497
                                        $this->_checkStatus(0, $result);
498
                                                break;
499
				default:
500
					break;
501
			}
502
			if ($this->_dnsService != 'ods' and $this->_dnsService != 'route53') {
503
				$data = curl_exec($ch);
504
				$this->_checkStatus($ch, $data);
505
				@curl_close($ch);
506
			}
507
		}
508

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

    
915
		/*
916
		 * Private Function (added 12 July 05) [beta]
917
		 *   Return Error, Set Last Error, and Die.
918
		 */
919
		function _error($errorNumber = '1') {
920
			switch ($errorNumber) {
921
				case 0:
922
					break;
923
				case 2:
924
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
925
					break;
926
				case 3:
927
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
928
					break;
929
				case 4:
930
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
931
					break;
932
				case 5:
933
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
934
					break;
935
				case 6:
936
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
937
					break;
938
				case 7:
939
					$error = 'phpDynDNS: (ERROR!) No Update URL Provided.';
940
					break;
941
                                case 8:
942
                                        $status = "Route 53: (Error) Invalid ZoneID";
943
					break;
944
                                case 9:
945
                                        $status = "Route 53: (Error) Invalid TTL";
946
                                        break;  
947
				case 10:
948
					$error = 'phpDynDNS: No change in my IP address and/or 25 days has not passed. Not updating dynamic DNS entry.';
949
					break;
950
				default:
951
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
952
					/* FIXME: $data isn't in scope here */
953
					/* $this->_debug($data); */
954
					break;
955
			}
956
			$this->lastError = $error;
957
			log_error($error);
958
		}
959

    
960
		/*
961
		 * Private Function (added 12 July 05) [beta]
962
		 *   - Detect whether or not IP needs to be updated.
963
		 *      | Written Specifically for pfSense (pfsense.com) may
964
		 *      | work with other systems. pfSense base is FreeBSD.
965
		 */
966
		function _detectChange() {
967
			global $debug;
968

    
969
			if ($debug)
970
				log_error("DynDns: _detectChange() starting.");
971
		
972
			$currentTime = time();
973

    
974
			$wan_ip = $this->_checkIP();
975
			if ($wan_ip == 0) {
976
				log_error("DynDns: Current WAN IP could not be determined, skipping update process.");
977
				return false;
978
			}
979
			$this->_dnsIP = $wan_ip;
980
			$log_error = "DynDns: Current WAN IP: {$wan_ip} ";
981

    
982
			if (file_exists($this->_cacheFile)) {
983
				$contents = file_get_contents($this->_cacheFile);
984
				list($cacheIP,$cacheTime) = explode(':', $contents);
985
				$this->_debug($cacheIP.'/'.$cacheTime);
986
				$initial = false;
987
				$log_error .= "Cached IP: {$cacheIP} ";
988
			} else {
989
				conf_mount_rw();
990
				$cacheIP = '0.0.0.0';
991
				@file_put_contents($this->_cacheFile, "0.0.0.0:{$currentTime}");
992
				conf_mount_ro();
993
				$cacheTime = $currentTime;
994
				$initial = true;
995
				$log_error .= "No Cached IP found.";
996
			}
997
			log_error($log_error);
998

    
999
			/*   use 2419200 for dyndns, dhs, easydns, noip, hn
1000
			 *   zoneedit, dyns, ods
1001
			 */
1002
			$time = '2160000';
1003

    
1004
			$needs_updating = FALSE;
1005
			/* lets determine if the item needs updating */
1006
			if ($cacheIP != $wan_ip) {
1007
				$needs_updating = true;
1008
				$update_reason = "DynDns: cacheIP != wan_ip.  Updating. ";
1009
				$update_reason .= "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
1010
			}
1011
			if (($currentTime - $cacheTime) > $time ) {
1012
				$needs_updating = true;
1013
				$update_reason = "DynDns: More than 25 days.  Updating. ";
1014
				$update_reason .= "{$currentTime} - {$cacheTime} > {$time} ";
1015
			}
1016
			if ($initial == true) {
1017
				$needs_updating = true;
1018
				$update_reason .= "Inital update. ";
1019
			}
1020

    
1021
			/*   finally if we need updating then store the
1022
			 *   new cache value and return true
1023
                         */
1024
			if ($needs_updating == true) {
1025
				log_error("DynDns debug information: {$update_reason}");
1026
				return true;
1027
			}
1028

    
1029
			return false;			
1030
		}
1031

    
1032
		/*
1033
		 * Private Funcation (added 16 July 05) [beta]
1034
		 *   - Writes debug information to a file.
1035
		 *   - This function is only called when a unknown response
1036
		 *   - status is returned from a DynDNS service provider.
1037
		 */
1038
		function _debug ($data) {
1039
			$string = '\n'.date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data.'\n';
1040
			conf_mount_rw();
1041
			$file = fopen($this->_debugFile, 'a');
1042
			fwrite($file, $string);
1043
			fclose($file);
1044
			conf_mount_ro();
1045
		}
1046
		function _checkIP() {
1047
			global $debug;
1048

    
1049
			if ($debug)
1050
				log_error("DynDns: _checkIP() starting.");
1051

    
1052
			$ip_address = find_interface_ip($this->_if);
1053
			if (!is_ipaddr($ip_address))
1054
				return 0;
1055
			$this->_ifIP = $ip_address;
1056
			if (is_private_ip($ip_address)) {
1057
				$hosttocheck = "checkip.dyndns.org";
1058
				$try = 0;
1059
				while ($try < 3) {
1060
					$checkip = gethostbyname($hosttocheck);
1061
					if (is_ipaddr($checkip))
1062
						break;
1063
					$try++;
1064
				}
1065
				if ($try >= 3) {
1066
					log_error("Dyndns debug information: Could not resolve {$hosttocheck} to ip using interface ip {$ip_address}.");
1067
					return 0;
1068
				}
1069
				$ip_ch = curl_init("http://{$checkip}");
1070
				curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
1071
				curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, FALSE);
1072
				curl_setopt($ip_ch, CURLOPT_INTERFACE, $ip_address);
1073
				curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
1074
				curl_setopt($ip_ch, CURLOPT_TIMEOUT, 120);
1075
				$ip_result_page = curl_exec($ip_ch);
1076
				curl_close($ip_ch);
1077
				$ip_result_decoded = urldecode($ip_result_page);
1078
				preg_match('/Current IP Address: (.*)<\/body>/', $ip_result_decoded, $matches);
1079
				$ip_address = trim($matches[1]);
1080
				if (is_ipaddr($ip_address))
1081
					log_error("DynDns debug information: {$ip_address} extracted from {$hosttocheck}");
1082
				else {
1083
					log_error("DynDns debug information: IP address could not be extracted from {$hosttocheck}");
1084
					return 0;
1085
				}
1086
			} else
1087
				log_error("DynDns debug information: {$ip_address} extracted from local system.");
1088

    
1089
			return $ip_address;
1090
		}
1091

    
1092
	}
1093

    
1094
?>
(18-18/65)