Project

General

Profile

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

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

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

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

    
698
			$wan_ip = get_current_wan_address();
699
			$this->_dnsIP = $wan_ip;
700
			log_error("DynDns: Current WAN IP: {$wan_ip}");
701

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

    
723
			/*   use 2419200 for dyndns, dhs, easydns, noip, hn
724
			 *   zoneedit, dyns, ods
725
			 */
726
			$time = '2160000';
727

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

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

    
773
	}
774

    
775
?>
(6-6/27)