Project

General

Profile

Download (28.7 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
	 * +----------------------------------------------------+
19
	 *  Requirements:
20
	 *    - PHP version 4.0.2 or higher with CURL Library
21
	 * +----------------------------------------------------+
22
	 *  Public Functions
23
	 *    - updatedns()
24
	 *
25
	 *  Private Functions
26
	 *    - _update()
27
	 *    - _checkStatus()
28
	 *    - _error()
29
	 *    - _detectChange()
30
	 *    - _debug()
31
	 * +----------------------------------------------------+
32
	 *  DynDNS Dynamic - Last Tested: 12 July 2005
33
	 *  DynDNS Static  - Last Tested: NEVER
34
	 *  DynDNS Custom  - Last Tested: NEVER
35
	 *  No-IP          - Last Tested: 12 July 2005
36
	 *  HN.org         - Last Tested: 12 July 2005
37
	 *  EasyDNS        - Last Tested: NEVER
38
	 *  DHS            - Last Tested: 12 July 2005
39
	 *  ZoneEdit       - Last Tested: NEVER
40
	 *  Dyns           - Last Tested: NEVER
41
	 *  ODS            - Last Tested: 02 August 2005
42
	 *  FreeDNS        - Last Tested: NEVER
43
	 *  Loopia         - Last Tested: NEVER
44
	 *  StaticCling    - Last Tested: 27 April 2006
45
	 * +====================================================+
46
	 *
47
	 * @author 	E.Kristensen
48
	 * @link    	http://www.idylldesigns.com/projects/phpdns/
49
	 * @version 	0.8
50
	 * @updated	13 October 05 at 21:02:42 GMT
51
	 *
52
	 */
53

    
54
	class updatedns {
55
		var $_cacheFile = '/cf/conf/dyndns.cache';
56
		var $_debugFile = '/var/etc/dyndns.debug';
57
		var $_UserAgent = 'User-Agent: phpDynDNS/0.7';
58
		var $_errorVerbosity = 0;
59
		var $_dnsService;
60
		var $_dnsUser;
61
		var $_dnsPass;
62
		var $_dnsHost;
63
		var $_dnsIP;
64
		var $_dnsWildcard;
65
		var $_dnsMX;
66
		var $_dnsBackMX;
67
		var $_dnsWanip;
68
		var $_dnsServer;
69
		var $_dnsPort;
70
		var $_dnsUpdateURL;
71
		var $status;
72
		var $_debugID;
73
		
74
		/* 
75
		 * Public Constructor Function (added 12 July 05) [beta]
76
		 *   - Gets the dice rolling for the update. 
77
		 */
78
		function updatedns ($dnsService = '', $dnsHost = '', $dnsUser = '', $dnsPass = '',
79
				    $dnsWildcard = 'OFF', $dnsMX = '', $dnsBackMX = '', $dnsWanip = '',
80
				    $dnsServer = '', $dnsPort = '', $dnsUpdateURL = '') {
81
			
82
			global $config;
83
			
84
			log_error("DynDns: updatedns() starting");
85
			
86
			if (!$dnsService) $this->_error(2);
87
			if (!($dnsService == 'freedns')) {
88

    
89
				/* all services except freedns use these */
90

    
91
				if (!$dnsUser) $this->_error(3);
92
				if (!$dnsPass) $this->_error(4);
93
				if (!$dnsHost) $this->_error(5);
94
			} else {
95

    
96
				/* freedns needs this */
97

    
98
				if (!$dnsUpdateURL) $this->_error(7);
99
			}
100
			
101
			$this->_dnsService = strtolower($dnsService);
102
			$this->_dnsUser = $dnsUser;
103
			$this->_dnsPass = $dnsPass;
104
			$this->_dnsHost = $dnsHost;
105
			$this->_dnsWanip = $dnsWanip;
106
			$this->_dnsServer = $dnsServer;
107
			$this->_dnsPort = $dnsPort;
108
			$this->_dnsWildcard = $dnsWildcard;
109
			$this->_dnsMX = $dnsMX;
110
			$this->_dnsUpdateURL = $dnsUpdateURL;
111
			
112
			if(!$wan_ip) 
113
				$wan_ip = get_current_wan_address();
114
				
115
			$this->_dnsIP = $wan_ip;
116
			$this->_debugID = rand(1000000, 9999999);
117
			
118
			if ($this->_detectChange() == FALSE) {
119
				$this->_error(10);
120
			} else {
121
				if ($this->_dnsService == 'dyndns' ||
122
					$this->_dnsService == 'dyndns-static' ||
123
					$this->_dnsService == 'dyndns-custom' ||
124
					$this->_dnsService == 'dhs' ||
125
					$this->_dnsService == 'noip' ||
126
					$this->_dnsService == 'easydns' ||
127
					$this->_dnsService == 'hn' ||
128
					$this->_dnsService == 'zoneedit' ||
129
					$this->_dnsService == 'dyns' ||
130
					$this->_dnsService == 'ods' ||
131
					$this->_dnsService == 'freedns' ||
132
					$this->_dnsService == 'loopia' ||
133
					$this->_dnsService == 'staticcling')
134
				{
135
					$this->_update();
136
				} else {
137
					$this->_error(6);
138
				}
139
			}					
140
		}
141
			
142
		/*
143
		 * Private Function (added 12 July 05) [beta]
144
		 *   Send Update To Selected Service.
145
		 */
146
		function _update() {
147
		
148
			log_error("DynDns: DynDns _update() starting.");
149
		
150
			if ($this->_dnsService != 'ods') {
151
				$ch = curl_init();
152
				curl_setopt($ch, CURLOPT_HEADER, 0);
153
				curl_setopt($ch, CURLOPT_USERAGENT, $this->_UserAgent);
154
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
155
			}
156

    
157
			switch ($this->_dnsService) {
158
				case 'dyndns':
159
					$needsIP = FALSE;
160
					log_error("DynDns: DynDns _update() starting. Dynamic");
161
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
162
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
163
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
164
					$server = "https://members.dyndns.org/nic/update";
165
					$port = "";
166
					if($this->_dnsServer)
167
						$server = $this->_dnsServer;
168
					if($this->_dnsPort)
169
						$port = ":" . $this->_dnsPort;
170
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP.'&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
171
					$data = curl_exec($ch);
172
					if (@curl_error($ch)) log_error("Curl errror occured: " . curl_error($ch));
173
					curl_close($ch);
174
					$this->_checkStatus($data);
175
					break;
176
				case 'dyndns-static':
177
					$needsIP = FALSE;
178
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
179
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
180
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
181
					$server = "https://members.dyndns.org/nic/update";
182
					$port = "";
183
					if($this->_dnsServer)
184
						$server = $this->_dnsServer;
185
					if($this->_dnsPort)
186
						$port = ":" . $this->_dnsPort;
187
					curl_setopt($ch, CURLOPT_URL, $server.$port.'?system=statdns&hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP.'&wildcard='.$this->_dnsWildcard.'&mx='.$this->_dnsMX.'&backmx=NO');
188
					$data = curl_exec($ch);
189
					if (@curl_error($ch)) log_error("Curl errror occured: " . curl_error($ch));
190
					curl_close($ch);
191
					$this->_checkStatus($data);
192
					break;
193
				case 'dyndns-custom':
194
					$needsIP = FALSE;
195
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
196
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
197
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
198
					$server = "https://members.dyndns.org/nic/update";
199
					$port = "";
200
					if($this->_dnsServer)
201
						$server = $this->_dnsServer;
202
					if($this->_dnsPort)
203
						$port = ":" . $this->_dnsPort;					
204
					curl_setopt($ch, CURLOPT_URL, $server.$port.'?system=custom&hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP.'&wildcard='.$this->_dnsWildcard.'&mx='.$this->_dnsMX.'&backmx=NO');
205
					$data = curl_exec($ch);
206
					if (@curl_error($ch)) log_error("Curl errror occured: " . curl_error($ch));
207
					curl_close($ch);
208
					$this->_checkStatus($data);
209
					break;
210
				case 'dhs':
211
					$needsIP = TRUE;
212
					$post_data['hostscmd'] = 'edit';
213
					$post_data['hostscmdstage'] = '2';
214
					$post_data['type'] = '4';
215
					$post_data['updatetype'] = 'Online';
216
					$post_data['mx'] = $this->_dnsMX;
217
					$post_data['mx2'] = '';
218
					$post_data['txt'] = '';
219
					$post_data['offline_url'] = '';
220
					$post_data['cloak'] = 'Y';
221
					$post_data['cloak_title'] = '';
222
					$post_data['ip'] = $this->_dnsIP;
223
					$post_data['domain'] = 'dyn.dhs.org';
224
					$post_data['hostname'] = $this->_dnsHost;
225
					$post_data['submit'] = 'Update';
226
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
227
					$server = "https://members.dhs.org/nic/hosts";
228
					$port = "";
229
					if($this->_dnsServer)
230
						$server = $this->_dnsServer;
231
					if($this->_dnsPort)
232
						$port = ":" . $this->_dnsPort;					
233
					curl_setopt($ch, CURLOPT_URL, '{$server}{$port}');
234
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
235
					curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
236
					$data = curl_exec($ch);
237
					if (@curl_error($ch)) log_error("Curl errror occured: " . curl_error($ch));
238
					curl_close($ch);
239
					$this->_checkStatus($data);
240
					break;
241
				case 'noip':
242
					$needsIP = TRUE;
243
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
244
					$server = "http://dynupdate.no-ip.com/dns";
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 . '?username=' . $this->_dnsUser . '&password=' . $this->_dnsPass . '&hostname=' . $this->_dnsHost.'&ip=' . $this->_dnsIP);
251
					$data = curl_exec($ch);
252
					if (@curl_error($ch)) log_error("Curl errror occured: " . curl_error($ch));
253
					curl_close($ch);
254
					$this->_checkStatus($data);
255
					break;
256
				case 'easydns':
257
					$needsIP = TRUE;
258
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
259
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
260
					$server = "http://members.easydns.com/dyn/dyndns.php";
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 . '?hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard=' . $this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=' . $this->_dnsBackMX);
267
					$data = curl_exec($ch);
268
					if (@curl_error($ch)) log_error("Curl errror occured: " . curl_error($ch));
269
					curl_close($ch);
270
					$this->_checkStatus($data);
271
					break;
272
				case 'hn':
273
					$needsIP = TRUE;
274
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
275
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
276
					$server = "http://dup.hn.org/vanity/update";
277
					$port = "";
278
					if($this->_dnsServer)
279
						$server = $this->_dnsServer;
280
					if($this->_dnsPort)
281
						$port = ":" . $this->_dnsPort;
282
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?ver=1&IP=' . $this->_dnsIP);
283
					$data = curl_exec($ch);
284
					if (@curl_error($ch)) log_error("Curl errror occured: " . curl_error($ch));
285
					curl_close($ch);
286
					$this->_checkStatus($data);
287
					break;
288
				case 'zoneedit':
289
					$needsIP = FALSE;
290
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
291
					curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
292
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
293

    
294
					$server = "https://dynamic.zoneedit.com/auth/dynamic.html";
295
					$port = "";
296
					if($this->_dnsServer)
297
						$server = $this->_dnsServer;
298
					if($this->_dnsPort)
299
						$port = ":" . $this->_dnsPort;
300
					curl_setopt($ch, CURLOPT_URL, "{$server}{$port}?host=" .$this->_dnsHost);
301

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

    
390
		/*
391
		 * Private Function (added 12 July 2005) [beta]
392
		 *   Retrieve Update Status
393
		 */
394
		function _checkStatus($data) {
395
			log_error("DynDns: DynDns _checkStatus() starting.");
396
			log_error("DynDns: Current Service: {$this->_dnsService}");
397
			$successful_update = false;
398
			switch ($this->_dnsService) {
399
				case 'dyndns':
400
					if (preg_match('/notfqdn/i', $data)) {
401
						$status = "phpDynDNS: (Error) Not A FQDN!";
402
					} else if (preg_match('/nochg/i', $data)) {
403
						$status = "phpDynDNS: (Success) No Change In IP Address";
404
						$successful_update = true;
405
					} else if (preg_match('/good/i', $data)) {
406
						$status = "phpDynDNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
407
						$successful_update = true;
408
					} else if (preg_match('/noauth/i', $data)) {
409
						$status = "phpDynDNS: (Error) User Authorization Failed";
410
					} else {
411
						$status = "phpDynDNS: (Unknown Response)";
412
						log_error("phpDynDNS: PAYLOAD: {$data}");
413
						$this->_debug($data);
414
					}
415
					break;
416
				case 'dyndns-static':
417
					if (preg_match('/notfqdn/i', $data)) {
418
						$status = "phpDynDNS: (Error) Not A FQDN!";
419
					} else if (preg_match('/nochg/i', $data)) {
420
						$status = "phpDynDNS: (Success) No Change In IP Address";
421
						$successful_update = true;
422
					} else if (preg_match('/good/i', $data)) {
423
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
424
						$successful_update = true;
425
					} else if (preg_match('/noauth/i', $data)) {
426
						$status = "phpDynDNS: (Error) User Authorization Failed";
427
					} else {
428
						$status = "phpDynDNS: (Unknown Response)";
429
						log_error("phpDynDNS: PAYLOAD: {$data}");
430
						$this->_debug($data);
431
					}
432
					break;
433
				case 'dyndns-custom':
434
					if (preg_match('/notfqdn/i', $data)) {
435
						$status = "phpDynDNS: (Error) Not A FQDN!";
436
					} else if (preg_match('/nochg/i', $data)) {
437
						$status = "phpDynDNS: (Success) No Change In IP Address";
438
						$successful_update = true;
439
					} else if (preg_match('/good/i', $data)) {
440
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
441
						$successful_update = true;
442
					} else if (preg_match('/noauth/i', $data)) {
443
						$status = "phpDynDNS: (Error) User Authorization Failed";
444
					} else {
445
						$status = "phpDynDNS: (Unknown Response)";
446
						log_error("phpDynDNS: PAYLOAD: {$data}");
447
						$this->_debug($data);
448
					}
449
					break;
450
				case 'dhs':
451
					break;
452
				case 'noip':
453
					list($ip,$code) = split(":",$data);
454
					switch ($code) {
455
						case 0:
456
							$status = "phpDynDNS: (Success) IP address is current, no update performed.";
457
							$successful_update = true;
458
							break;
459
						case 1:
460
							$status = "phpDynDNS: (Success) DNS hostname update successful.";
461
							$successful_update = true;
462
							break;
463
						case 2:
464
							$status = "phpDynDNS: (Error) Hostname supplied does not exist.";
465
							break;
466
						case 3:
467
							$status = "phpDynDNS: (Error) Invalid Username.";
468
							break;
469
						case 4:
470
							$status = "phpDynDNS: (Error) Invalid Password.";
471
							break;
472
						case 5:
473
							$status = "phpDynDNS: (Error) To many updates sent.";
474
							break;
475
						case 6:
476
							$status = "phpDynDNS: (Error) Account disabled due to violation of No-IP terms of service.";
477
							break;
478
						case 7:
479
							$status = "phpDynDNS: (Error) Invalid IP. IP Address submitted is improperly formatted or is a private IP address or is on a blacklist.";
480
							break;
481
						case 8:
482
							$status = "phpDynDNS: (Error) Disabled / Locked Hostname.";
483
							break;
484
						case 9:
485
							$status = "phpDynDNS: (Error) Host updated is configured as a web redirect and no update was performed.";
486
							break;
487
						case 10:
488
							$status = "phpDynDNS: (Error) Group supplied does not exist.";
489
							break;
490
						case 11:
491
							$status = "phpDynDNS: (Success) DNS group update is successful.";
492
							$successful_update = true;
493
							break;
494
						case 12:
495
							$status = "phpDynDNS: (Success) DNS group is current, no update performed.";
496
							$successful_update = true;
497
							break;
498
						case 13:
499
							$status = "phpDynDNS: (Error) Update client support not available for supplied hostname or group.";
500
							break;
501
						case 14:
502
							$status = "phpDynDNS: (Error) Hostname supplied does not have offline settings configured.";
503
							break;
504
						case 99:
505
							$status = "phpDynDNS: (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
506
							break;
507
						case 100:
508
							$status = "phpDynDNS: (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
509
							break;
510
						default:
511
							$status = "phpDynDNS: (Unknown Response)";
512
							$this->_debug("Unknown Response: ".$data);
513
							break;
514
					}
515
					break;
516
				case 'easydns':
517
					if (preg_match('/NOACCESS/i', $data)) {
518
						$status = "phpDynDNS: (Error) Authentication Failed: Username and/or Password was Incorrect.";
519
					} else if (preg_match('/NOSERVICE/i', $data)) {
520
						$status = "phpDynDNS: (Error) No Service: Dynamic DNS Service has been disabled for this domain.";
521
					} else if (preg_match('/ILLEGAL INPUT/i', $data)) {
522
						$status = "phpDynDNS: (Error) Illegal Input: Self-Explantory";
523
					} else if (preg_match('/TOOSOON/i', $data)) {
524
						$status = "phpDynDNS: (Error) Too Soon: Not Enough Time Has Elapsed Since Last Update";
525
					} else if (preg_match('/NOERROR/i', $data)) {
526
						$status = "phpDynDNS: (Success) IP Updated Successfully!";
527
						$successful_update = true;
528
					} else {
529
						$status = "phpDynDNS: (Unknown Response)";
530
						log_error("phpDynDNS: PAYLOAD: {$data}");
531
						$this->_debug($data);
532
					}
533
					break;
534
				case 'hn':
535
					/* FIXME: add checks */
536
					break;
537
				case 'zoneedit':
538
					if (preg_match('/799/i', $data)) {
539
						$status = "phpDynDNS: (Error 799) Update Failed!";				
540
					} else if (preg_match('/700/i', $data)) {
541
						$status = "phpDynDNS: (Error 700) Update Failed!";
542
					} else if (preg_match('/200/i', $data)) {
543
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
544
						$successful_update = true;
545
					} else if (preg_match('/201/i', $data)) {
546
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
547
						$successful_update = true;						
548
					} else {
549
						$status = "phpDynDNS: (Unknown Response)";
550
						log_error("phpDynDNS: PAYLOAD: {$data}");
551
						$this->_debug($data);
552
					}
553
					break;
554
				case 'dyns':
555
					if (preg_match("/400/i", $data)) {
556
						$status = "phpDynDNS: (Error) Bad Request - The URL was malformed. Required parameters were not provided.";
557
					} else if (preg_match('/402/i', $data)) {
558
						$status = "phpDynDNS: (Error) Update Too Soon - You have tried updating to quickly since last change.";
559
					} else if (preg_match('/403/i', $data)) {
560
						$status = "phpDynDNS: (Error) Database Error - There was a server-sided database error.";
561
					} else if (preg_match('/405/i', $data)) {
562
						$status = "phpDynDNS: (Error) Hostname Error - The hostname (".$this->_dnsHost.") doesn't belong to you.";
563
					} else if (preg_match('/200/i', $data)) {
564
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
565
						$successful_update = true;
566
					} else {
567
						$status = "phpDynDNS: (Unknown Response)";
568
						log_error("phpDynDNS: PAYLOAD: {$data}");
569
						$this->_debug($data);
570
					}
571
					break;
572
				case 'ods':
573
					if (preg_match("/299/i", $data)) {
574
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
575
						$successful_update = true;
576
					} else {
577
						$status = "phpDynDNS: (Unknown Response)";
578
						log_error("phpDynDNS: PAYLOAD: {$data}");
579
						$this->_debug($data);
580
					}
581
					break;
582
				case 'freedns':
583
					if (preg_match("/has not changed./i", $data)) {
584
						$status = "phpDynDNS: (Success) No Change In IP Address";
585
						$successful_update = true;
586
					} else if (preg_match("/Updated/i", $data)) {
587
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
588
						$successful_update = true;
589
					} else {
590
						$status = "phpDynDNS: (Unknown Response)";
591
						log_error("phpDynDNS: PAYLOAD: {$data}");
592
						$this->_debug($data);
593
					} 
594
					break;
595
				case 'loopia':
596
					if (preg_match("/nochg/i", $data)) {
597
						$status = "phpDynDNS: (Success) No Change In IP Address";
598
						$successful_update = true;
599
					} else if (preg_match("/good/i", $data)) {
600
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
601
						$successful_update = true;
602
					} else if (preg_match('/badauth/i', $data)) {
603
						$status = "phpDynDNS: (Error) User Authorization Failed";
604
					} else {
605
						$status = "phpDynDNS: (Unknown Response)";
606
						log_error("phpDynDNS: PAYLOAD: {$data}");
607
						$this->_debug($data);
608
					}
609
					break;
610
                                case 'staticcling':
611
                                        if (preg_match("/invalid ip/i", $data)) {
612
                                                $status = "phpDynDNS: (Error) Bad Request - The IP provided was invalid.";
613
                                        } else if (preg_match('/required info missing/i', $data)) {
614
                                                $status = "phpDynDNS: (Error) Bad Request - Required parameters were not provided.";
615
                                        } else if (preg_match('/invalid characters/i', $data)) {
616
                                                $status = "phpDynDNS: (Error) Bad Request - Illegal characters in either the username or the password.";
617
                                        } else if (preg_match('/bad password/i', $data)) {
618
                                                $status = "phpDynDNS: (Error) Invalid password.";
619
                                        } else if (preg_match('/account locked/i', $data)) {
620
                                                $status = "phpDynDNS: (Error) This account has been administratively locked.";
621
                                        } else if (preg_match('/update too frequent/i', $data)) {
622
                                                $status = "phpDynDNS: (Error) Updating too frequently.";
623
                                        } else if (preg_match('/DB error/i', $data)) {
624
                                                $status = "phpDynDNS: (Error) Server side error.";
625
                                        } else if (preg_match('/success/i', $data)) {
626
                                                $status = "phpDynDNS: (Success) IP Address Updated Successfully!";
627
                                                $successful_update = true;
628
                                        } else {
629
                                                $status = "phpDynDNS: (Unknown Response)";
630
                                                log_error("phpDynDNS: PAYLOAD: {$data}");
631
                                                $this->_debug($data);
632
                                        }
633
                                        break;
634
			}
635
			
636
			if($successful_update == true) {
637
				/* Write WAN IP to cache file */
638
				$wan_ip = get_current_wan_address();
639
				$currentTime = time();				  
640
				log_error("phpDynDNS: updating cache file {$this->_cacheFile}: {$wan_ip}");
641
				conf_mount_rw();
642
				$file = fopen($this->_cacheFile, 'w');
643
				fwrite($file, $wan_ip.':'.$currentTime);
644
				fclose($file);
645
				conf_mount_ro();
646
			}
647
			$this->status = $status;
648
			log_error($status);
649
		}
650

    
651
		/*
652
		 * Private Function (added 12 July 05) [beta]
653
		 *   Return Error, Set Last Error, and Die.
654
		 */
655
		function _error($errorNumber = '1') {
656
			switch ($errorNumber) {
657
				case 0:
658
					break;
659
				case 2:
660
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
661
					break;
662
				case 3:
663
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
664
					break;
665
				case 4:
666
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
667
					break;
668
				case 5:
669
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
670
					break;
671
				case 6:
672
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
673
					break;
674
				case 7:
675
					$error = 'phpDynDNS: (ERROR!) No Update URL Provided.';
676
					break;
677
				case 10:
678
					$error = 'phpDynDNS: No Change In My IP Address and/or 25 Days Has Not Past. Not Updating Dynamic DNS Entry.';
679
					break;
680
				default:
681
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
682
					/* FIXME: $data isn't in scope here */
683
					/* $this->_debug($data); */
684
					break;
685
			}
686
			$this->lastError = $error;
687
			log_error($error);
688
		}
689

    
690
		/*
691
		 * Private Function (added 12 July 05) [beta]
692
		 *   - Detect whether or not IP needs to be updated.
693
		 *      | Written Specifically for pfSense (pfsense.com) may
694
		 *      | work with other systems. pfSense base is FreeBSD.
695
		 */
696
		function _detectChange() {
697
			
698
			log_error("DynDns: _detectChange() starting.");
699
		
700
			$currentTime = time();
701

    
702
			$wan_ip = get_current_wan_address();
703
			$this->_dnsIP = $wan_ip;
704
			log_error("DynDns: Current WAN IP: {$wan_ip}");
705

    
706
			if (file_exists($this->_cacheFile)) {
707
				if(file_exists($this->_cacheFile))
708
					$contents = file_get_contents($this->_cacheFile);
709
				else
710
					$contents = "";
711
				list($cacheIP,$cacheTime) = split(':', $contents);
712
				$this->_debug($cacheIP.'/'.$cacheTime);
713
				$initial = false;
714
				log_error("DynDns: Cached IP: {$cacheIP}");
715
			} else {
716
				conf_mount_rw();
717
				$file = fopen($this->_cacheFile, 'w');
718
				fwrite($file, '0.0.0.0:'.$currentTime);
719
				fclose($file);
720
				conf_mount_ro();
721
				$cacheIP = '0.0.0.0';
722
				$cacheTime = $currentTime;
723
				$initial = true;
724
				log_error("DynDns: No Cached IP found.");
725
			}
726

    
727
			/*   use 2419200 for dyndns, dhs, easydns, noip, hn
728
			 *   zoneedit, dyns, ods
729
			 */
730
			$time = '2160000';
731

    
732
			$needs_updating = FALSE;
733
			/* lets deterimine if the item needs updating */
734
			if ($cacheIP != $wan_ip) {
735
				$needs_updating = TRUE;
736
				log_error("DynDns: cacheIP != wan_ip.  Updating.");
737
			}
738
			$update_reason = "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
739
			if (($currentTime - $cacheTime) > $time ) {
740
				$needs_updating = TRUE;
741
				log_error("DynDns: More than 25 days.  Updating.");
742
			}
743
			$update_reason .= "{$currentTime} - {$cacheTime} > {$time} ";
744
			if ($initial == TRUE) {
745
				$needs_updating = TRUE;
746
				$update_reason .= "Inital update. ";
747
				log_error("DynDns: Initial run.   Updating.");
748
			}
749
			/*   finally if we need updating then store the
750
			 *   new cache value and return true
751
                         */
752
			if($needs_updating == TRUE) {
753
				return TRUE;
754
			} else {
755
				return FALSE;			
756
			}
757
			
758
			log_error("DynDns debug information: {$update_reason}");
759
			
760
		}
761

    
762
		/*
763
		 * Private Funcation (added 16 July 05) [beta]
764
		 *   - Writes debug information to a file.
765
		 *   - This function is only called when a unknown response
766
		 *   - status is returned from a DynDNS service provider.
767
		 */
768
		function _debug ($data) {
769
			$string = date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data.'\n';
770
			conf_mount_rw();
771
			$file = fopen($this->_debugFile, 'a');
772
			fwrite($file, $string);
773
			fclose($file);
774
			conf_mount_ro();
775
		}
776

    
777
	}
778

    
779
?>
(6-6/27)