Project

General

Profile

Download (19.8 KB) Statistics
| Branch: | Tag: | Revision:
1 d237be68 Scott Ullrich
<?php
2 1fa2f630 Erik Kristensen
	/*
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)
13
	 *    - DynS (dyns.org)
14
	 *    - ZoneEdit (zoneedit.com)
15
	 * +----------------------------------------------------+
16
	 *  Requirements:
17
	 *    - PHP version 4.0.2 or higher with CURL Library
18
	 * +----------------------------------------------------+
19
	 *  Public Functions
20
	 *    - updatedns()
21
	 *
22
	 *  Private Functions
23
	 *    - _update()
24
	 *    - _checkStatus()
25
	 *    - _error()
26
	 *    - _detectChange()
27
	 *    - _debug()
28
	 * +----------------------------------------------------+
29
	 *  DynDNS Dynamic - Last Tested: 12 July 2005
30
	 *  DynDNS Static  - Last Tested: NEVER
31
	 *  DynDNS Custom  - Last Tested: NEVER
32
	 *  No-IP          - Last Tested: 12 July 2005
33
	 *  HN.org         - Last Tested: 12 July 2005
34
	 *  EasyDNS        - Last Tested: NEVER
35
	 *  DHS            - Last Tested: 12 July 2005
36
	 *  ZoneEdit       - Last Tested: NEVER
37
	 *  Dyns           - Last Tested: NEVER
38 0da417be Erik Kristensen
	 *  ODS            - Last Tested: 02 August 2005
39 1fa2f630 Erik Kristensen
	 * +====================================================+
40
	 *
41 e24b26e9 Scott Ullrich
	 * @author 	E.Kristensen
42 1fa2f630 Erik Kristensen
	 * @link    	http://www.idylldesigns.com/projects/phpdns/
43 0da417be Erik Kristensen
	 * @version 	0.8
44 e24b26e9 Scott Ullrich
	 * @updated	13 October 05 at 21:02:42 GMT
45 1fa2f630 Erik Kristensen
	 *
46
	 */
47
48
	class updatedns {
49 53d366fd Scott Ullrich
		var $_cacheFile = '/cf/conf/dyndns.cache';
50 1fa2f630 Erik Kristensen
		var $_debugFile = '/var/etc/dyndns.debug';
51
		var $_UserAgent = 'User-Agent: phpDynDNS/0.7';
52
		var $_errorVerbosity = 0;
53
		var $_dnsService;
54
		var $_dnsUser;
55
		var $_dnsPass;
56
		var $_dnsHost;
57
		var $_dnsIP;
58
		var $_dnsWildcard;
59
		var $_dnsMX;
60
		var $_dnsBackMX;
61
		var $status;
62
		var $_debugID;
63
64
		/* 
65
		 * Public Constructor Function (added 12 July 05) [beta]
66
		 *   - Gets the dice rolling for the update. 
67
		 */
68
		function updatedns ($dnsService = '', $dnsHost = '', $dnsUser = '', $dnsPass = '', $dnsWildcard = 'OFF', $dnsMX = '', $dnsBackMX = '') {
69 0da417be Erik Kristensen
			global $config;
70
71 1331005d Scott Ullrich
			log_error("DynDns: updatedns() starting");
72
73 1fa2f630 Erik Kristensen
			if (!$dnsService) $this->_error(2);
74
			if (!$dnsUser) $this->_error(3);
75
			if (!$dnsPass) $this->_error(4);
76
			if (!$dnsHost) $this->_error(5);
77
78
			$this->_dnsService = strtolower($dnsService);
79
			$this->_dnsUser = $dnsUser;
80
			$this->_dnsPass = $dnsPass;
81
			$this->_dnsHost = $dnsHost;
82
			$this->_dnsIP = $dnsIP;
83
			$this->_debugID = rand(1000000, 9999999);
84
85
			if ($this->_detectChange() == FALSE) {
86
				$this->_error(10);
87
			} else {
88
				if ($this->_dnsService == 'dyndns' ||
89
					$this->_dnsService == 'dyndns-static' ||
90
					$this->_dnsService == 'dyndns-custom' ||
91
					$this->_dnsService == 'dhs' ||
92
					$this->_dnsService == 'noip' ||
93
					$this->_dnsService == 'easydns' ||
94
					$this->_dnsService == 'hn' ||
95
					$this->_dnsService == 'zoneedit' ||
96
					$this->_dnsService == 'dyns' ||
97
					$this->_dnsService == 'ods')
98
				{
99
					$this->_update();
100
				} else {
101
					$this->_error(6);
102
				}
103
			}
104
105
		}
106
107
		/*
108
		 * Private Function (added 12 July 05) [beta]
109
		 *   Send Update To Selected Service.
110
		 */
111
		function _update() {
112 1331005d Scott Ullrich
		
113
			log_error("DynDns: DynDns _update() starting.");
114
		
115 1fa2f630 Erik Kristensen
			if ($this->_dnsService != 'ods') {
116
				$ch = curl_init();
117
				curl_setopt($ch, CURLOPT_HEADER, 0);
118
				curl_setopt($ch, CURLOPT_USERAGENT, $this->_UserAgent);
119
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
120
			}
121
122
			switch ($this->_dnsService) {
123
				case 'dyndns':
124
					$needsIP = FALSE;
125
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
126
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
127
					curl_setopt($ch, CURLOPT_URL, 'https://members.dyndns.org/nic/update?hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP.'&wildcard='.$this->_dnsWildcard.'&mx='.$this->_dnsMX.'&backmx=NO');
128
					$data = curl_exec($ch);
129 0c7b834e Scott Ullrich
					if (@curl_error($ch)) log_error("Curl errror occured: " . curl_error($ch));
130 44318b56 Scott Ullrich
					curl_close($ch);
131 1fa2f630 Erik Kristensen
					$this->_checkStatus($data);
132
					break;
133
				case 'dyndns-static':
134
					$needsIP = FALSE;
135
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
136
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
137
					curl_setopt($ch, CURLOPT_URL, 'https://members.dyndns.org/nic/update?system=statdns&hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP.'&wildcard='.$this->_dnsWildcard.'&mx='.$this->_dnsMX.'&backmx=NO');
138
					$data = curl_exec($ch);
139 0c7b834e Scott Ullrich
					if (@curl_error($ch)) log_error("Curl errror occured: " . curl_error($ch));
140 1fa2f630 Erik Kristensen
					curl_close($ch);
141
					$this->_checkStatus($data);
142
					break;
143
				case 'dyndns-custom':
144
					$needsIP = FALSE;
145
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
146
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
147
					curl_setopt($ch, CURLOPT_URL, 'https://members.dyndns.org/nic/update?system=custom&hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP.'&wildcard='.$this->_dnsWildcard.'&mx='.$this->_dnsMX.'&backmx=NO');
148
					$data = curl_exec($ch);
149 0c7b834e Scott Ullrich
					if (@curl_error($ch)) log_error("Curl errror occured: " . curl_error($ch));
150 1fa2f630 Erik Kristensen
					curl_close($ch);
151
					$this->_checkStatus($data);
152
					break;
153
				case 'dhs':
154
					$needsIP = TRUE;
155 a255e1f7 Scott Ullrich
					$post_data['hostscmd'] = 'edit';
156
					$post_data['hostscmdstage'] = '2';
157
					$post_data['type'] = '4';
158
					$post_data['updatetype'] = 'Online';
159
					$post_data['mx'] = $this->_dnsMX;
160
					$post_data['mx2'] = '';
161
					$post_data['txt'] = '';
162
					$post_data['offline_url'] = '';
163
					$post_data['cloak'] = 'Y';
164
					$post_data['cloak_title'] = '';
165
					$post_data['ip'] = $this->_dnsIP;
166
					$post_data['domain'] = 'dyn.dhs.org';
167
					$post_data['hostname'] = $this->_dnsHost;
168
					$post_data['submit'] = 'Update';
169 1fa2f630 Erik Kristensen
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
170
					curl_setopt($ch, CURLOPT_URL, 'https://members.dhs.org/nic/hosts');
171
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
172
					curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
173
					$data = curl_exec($ch);
174 0c7b834e Scott Ullrich
					if (@curl_error($ch)) log_error("Curl errror occured: " . curl_error($ch));
175 1fa2f630 Erik Kristensen
					curl_close($ch);
176
					$this->_checkStatus($data);
177
					break;
178
				case 'noip':
179
					$needsIP = TRUE;
180
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
181
					curl_setopt($ch, CURLOPT_URL, 'http://dynupdate.no-ip.com/dns?username='.$this->_dnsUser.'&password='.$this->_dnsPass.'&hostname='.$this->_dnsHost.'&ip='.$this->_dnsIP);
182
					$data = curl_exec($ch);
183 0c7b834e Scott Ullrich
					if (@curl_error($ch)) log_error("Curl errror occured: " . curl_error($ch));
184 1fa2f630 Erik Kristensen
					curl_close($ch);
185
					$this->_checkStatus($data);
186
					break;
187
				case 'easydns':
188
					$needsIP = TRUE;
189
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
190
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
191
					curl_setopt($ch, CURLOPT_URL, 'http://members.easydns.com/dyn/dyndns.php?hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP.'&wildcard='.$this->_dnsWildcard.'&mx='.$this->_dnsMX.'&backmx='.$this->_dnsBackMX);
192
					$data = curl_exec($ch);
193 0c7b834e Scott Ullrich
					if (@curl_error($ch)) log_error("Curl errror occured: " . curl_error($ch));
194 1fa2f630 Erik Kristensen
					curl_close($ch);
195
					$this->_checkStatus($data);
196
					break;
197
				case 'hn':
198
					$needsIP = TRUE;
199
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
200
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
201
					curl_setopt($ch, CURLOPT_URL, 'http://dup.hn.org/vanity/update?ver=1&IP='.$this->_dnsIP);
202
					$data = curl_exec($ch);
203 0c7b834e Scott Ullrich
					if (@curl_error($ch)) log_error("Curl errror occured: " . curl_error($ch));
204 1fa2f630 Erik Kristensen
					curl_close($ch);
205
					$this->_checkStatus($data);
206
					break;
207
				case 'zoneedit':
208
					$needsIP = FALSE;
209
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
210
					curl_setopt($ch, CURLOPT_URL, 'https://dynamic.zoneedit.com/auth/dynamic.html?host='.$this->_dnsHost.'&dnsto='.$this->_dnsIP);
211
					$data = curl_exec($ch);
212 0c7b834e Scott Ullrich
					if (@curl_error($ch)) log_error("Curl errror occured: " . curl_error($ch));
213 1fa2f630 Erik Kristensen
					curl_close($ch);
214
					$this->_checkStatus($data);
215
					break;
216
				case 'dyns':
217
					$needsIP = FALSE;
218
					curl_setopt($ch, CURLOPT_URL, 'http://www.dyns.cx/postscript011.php?username='.$this->_dnsUser.'&password='.$this->_dnsPass.'&host='.$this->_dnsHost);
219
					$data = curl_exec($ch);
220 0c7b834e Scott Ullrich
					if (@curl_error($ch)) log_error("Curl errror occured: " . curl_error($ch));
221 1fa2f630 Erik Kristensen
					curl_close($ch);
222
					$this->_checkStatus($data);
223
					break;
224
				case 'ods':
225
					$needsIP = FALSE;
226
					$this->con['socket'] = fsockopen("ods.org", "7070", $misc_errno, $misc_error, 30);
227
					/* Check that we have connected */
228
					if (!$this->con['socket']) {
229
						print "error! could not connect.";
230
						break;
231
					}
232
					/* Here is the loop. Read the incoming data (from the socket connection) */
233
					while (!feof($this->con['socket'])) {
234
						$this->con['buffer']['all'] = trim(fgets($this->con['socket'], 4096));
235
						$code = substr($this->con['buffer']['all'], 0, 3);
236
						sleep(1);
237
						switch($code) {
238
							case 100:
239
								fputs($this->con['socket'], "LOGIN ".$this->_dnsUser." ".$this->_dnsPass."\n");
240
								break;
241
							case 225:
242
								fputs($this->con['socket'], "DELRR ".$this->_dnsHost." A\n");
243
								break;
244
							case 901:
245
								fputs($this->con['socket'], "ADDRR ".$this->_dnsHost." A ".$this->_dnsIP."\n");
246
								break;
247
							case 795:
248
								fputs($this->con['socket'], "QUIT\n");
249
								break;
250
						}
251
					}
252
					$this->_checkStatus($code);
253
					break;
254
				default:
255
					break;
256
			}
257
		}
258
259
		/*
260
		 * Private Function (added 12 July 2005) [beta]
261
		 *   Retrieve Update Status
262
		 */
263
		function _checkStatus($data) {
264 1331005d Scott Ullrich
			log_error("DynDns: DynDns _checkStatus() starting.");
265 d6fcb946 Scott Ullrich
			log_error("DynDns: Current Service: {$this->_dnsService}");
266 1fa2f630 Erik Kristensen
			switch ($this->_dnsService) {
267
				case 'dyndns':
268
					if (preg_match('/notfqdn/i', $data)) {
269
						$status = "phpDynDNS: (Error) Not A FQDN!";
270
					} else if (preg_match('/nochg/i', $data)) {
271
						$status = "phpDynDNS: (Success) No Change In IP Address";
272
					} else if (preg_match('/good/i', $data)) {
273
						$status = "phpDynDNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
274
					} else if (preg_match('/noauth/i', $data)) {
275
						$status = "phpDynDNS: (Error) User Authorization Failed";
276
					} else {
277
						$status = "phpDynDNS: (Unknown Response)";
278 492d334b Scott Ullrich
						log_error("phpDynDNS: PAYLOAD: {$data}");
279 1fa2f630 Erik Kristensen
						$this->_debug($data);
280
					}
281
					break;
282
				case 'dyndns-static':
283
					if (preg_match('/notfqdn/i', $data)) {
284
						$status = "phpDynDNS: (Error) Not A FQDN!";
285
					} else if (preg_match('/nochg/i', $data)) {
286
						$status = "phpDynDNS: (Success) No Change In IP Address";
287
					} else if (preg_match('/good/i', $data)) {
288
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
289
					} else if (preg_match('/noauth/i', $data)) {
290
						$status = "phpDynDNS: (Error) User Authorization Failed";
291
					} else {
292
						$status = "phpDynDNS: (Unknown Response)";
293 492d334b Scott Ullrich
						log_error("phpDynDNS: PAYLOAD: {$data}");
294 1fa2f630 Erik Kristensen
						$this->_debug($data);
295
					}
296
					break;
297
				case 'dyndns-custom':
298
					if (preg_match('/notfqdn/i', $data)) {
299
						$status = "phpDynDNS: (Error) Not A FQDN!";
300
					} else if (preg_match('/nochg/i', $data)) {
301
						$status = "phpDynDNS: (Success) No Change In IP Address";
302
					} else if (preg_match('/good/i', $data)) {
303
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
304
					} else if (preg_match('/noauth/i', $data)) {
305
						$status = "phpDynDNS: (Error) User Authorization Failed";
306
					} else {
307
						$status = "phpDynDNS: (Unknown Response)";
308 492d334b Scott Ullrich
						log_error("phpDynDNS: PAYLOAD: {$data}");
309 1fa2f630 Erik Kristensen
						$this->_debug($data);
310
					}
311
					break;
312
				case 'dhs':
313
					break;
314
				case 'noip':
315
					list($ip,$code) = split(":",$data);
316
					switch ($code) {
317
						case 0:
318
							$status = "phpDynDNS: (Success) IP address is current, no update performed.";
319
							break;
320
						case 1:
321
							$status = "phpDynDNS: (Success) DNS hostname update successful.";
322
							break;
323
						case 2:
324
							$status = "phpDynDNS: (Error) Hostname supplied does not exist.";
325
							break;
326
						case 3:
327
							$status = "phpDynDNS: (Error) Invalid Username.";
328
							break;
329
						case 4:
330
							$status = "phpDynDNS: (Error) Invalid Password.";
331
							break;
332
						case 5:
333
							$status = "phpDynDNS: (Error) To many updates sent.";
334
							break;
335
						case 6:
336
							$status = "phpDynDNS: (Error) Account disabled due to violation of No-IP terms of service.";
337
							break;
338
						case 7:
339
							$status = "phpDynDNS: (Error) Invalid IP. IP Address submitted is improperly formatted or is a private IP address or is on a blacklist.";
340
							break;
341
						case 8:
342
							$status = "phpDynDNS: (Error) Disabled / Locked Hostname.";
343
							break;
344
						case 9:
345
							$status = "phpDynDNS: (Error) Host updated is configured as a web redirect and no update was performed.";
346
							break;
347
						case 10:
348
							$status = "phpDynDNS: (Error) Group supplied does not exist.";
349
							break;
350
						case 11:
351
							$status = "phpDynDNS: (Success) DNS group update is successful.";
352
							break;
353
						case 12:
354
							$status = "phpDynDNS: (Success) DNS group is current, no update performed.";
355
							break;
356
						case 13:
357
							$status = "phpDynDNS: (Error) Update client support not available for supplied hostname or group.";
358
							break;
359
						case 14:
360
							$status = "phpDynDNS: (Error) Hostname supplied does not have offline settings configured.";
361
							break;
362
						case 99:
363
							$status = "phpDynDNS: (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
364
							break;
365
						case 100:
366
							$status = "phpDynDNS: (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
367
							break;
368
						default:
369
							$status = "phpDynDNS: (Unknown Response)";
370 458d80d7 Scott Ullrich
							$this->_debug("Unknown Response: ".$data);
371 1fa2f630 Erik Kristensen
							break;
372
					}
373
					break;
374
				case 'easydns':
375
					if (preg_match('/NOACCESS/i', $data)) {
376
						$status = "phpDynDNS: (Error) Authentication Failed: Username and/or Password was Incorrect.";
377
					} else if (preg_match('/NOSERVICE/i', $data)) {
378
						$status = "phpDynDNS: (Error) No Service: Dynamic DNS Service has been disabled for this domain.";
379
					} else if (preg_match('/ILLEGAL INPUT/i', $data)) {
380
						$status = "phpDynDNS: (Error) Illegal Input: Self-Explantory";
381
					} else if (preg_match('/TOOSOON/i', $data)) {
382
						$status = "phpDynDNS: (Error) Too Soon: Not Enough Time Has Elapsed Since Last Update";
383
					} else if (preg_match('/NOERROR/i', $data)) {
384
						$status = "phpDynDNS: (Success) IP Updated Successfully!";					
385
					} else {
386
						$status = "phpDynDNS: (Unknown Response)";
387 492d334b Scott Ullrich
						log_error("phpDynDNS: PAYLOAD: {$data}");
388 1fa2f630 Erik Kristensen
						$this->_debug($data);
389
					}
390
					break;
391
				case 'hn':
392
					break;
393
				case 'zoneedit':
394
					if (preg_match('/{700,799}/i', $data)) {
395
						$status = "phpDynDNS: (Error) Update Failed!";
396
					} else if (preg_match('/{200,201}/i', $data)) {
397
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
398
					} else {
399
						$status = "phpDynDNS: (Unknown Response)";
400 492d334b Scott Ullrich
						log_error("phpDynDNS: PAYLOAD: {$data}");
401 1fa2f630 Erik Kristensen
						$this->_debug($data);
402
					}
403
					break;
404
				case 'dyns':
405
					if (preg_match("/400/i", $data)) {
406
						$status = "phpDynDNS: (Error) Bad Request - The URL was malformed. Required parameters were not provided.";
407
					} else if (preg_match('/402/i', $data)) {
408
						$status = "phpDynDNS: (Error) Update Too Soon - You have tried updating to quickly since last change.";
409
					} else if (preg_match('/403/i', $data)) {
410
						$status = "phpDynDNS: (Error) Database Error - There was a server-sided database error.";
411
					} else if (preg_match('/405/i', $data)) {
412
						$status = "phpDynDNS: (Error) Hostname Error - The hostname (".$this->_dnsHost.") doesn't belong to you.";
413
					} else if (preg_match('/200/i', $data)) {
414
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
415
					} else {
416
						$status = "phpDynDNS: (Unknown Response)";
417 492d334b Scott Ullrich
						log_error("phpDynDNS: PAYLOAD: {$data}");
418 1fa2f630 Erik Kristensen
						$this->_debug($data);
419
					}
420
					break;
421
				case 'ods':
422
					if (preg_match("/299/i", $data)) {
423
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
424
					} else {
425
						$status = "phpDynDNS: (Unknown Response)";
426 492d334b Scott Ullrich
						log_error("phpDynDNS: PAYLOAD: {$data}");
427 1fa2f630 Erik Kristensen
						$this->_debug($data);
428
					}
429
					break;
430
			}
431
			$this->status = $status;
432
			log_error($status);
433
		}
434
435
		/*
436
		 * Private Function (added 12 July 05) [beta]
437
		 *   Return Error, Set Last Error, and Die.
438
		 */
439
		function _error($errorNumber = '1') {
440
			switch ($errorNumber) {
441
				case 0:
442
					break;
443
				case 2:
444
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
445
					break;
446
				case 3:
447
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
448
					break;
449
				case 4:
450
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
451
					break;
452
				case 5:
453
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
454
					break;
455
				case 6:
456
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
457
					break;
458
				case 10:
459
					$error = 'phpDynDNS: No Change In My IP Address and/or 28 Days Has Not Past. Not Updating Dynamic DNS Entry.';
460
					break;
461
				default:
462
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
463
					$this->_debug($data);
464
					break;
465
			}
466
			$this->lastError = $error;
467
			log_error($error);
468
		}
469
470
		/*
471
		 * Private Function (added 12 July 05) [beta]
472
		 *   - Detect whether or not IP needs to be updated.
473
		 *      | Written Specifically for pfSense (pfsense.com) may
474
		 *      | work with other systems. pfSense base is FreeBSD.
475
		 */
476
		function _detectChange() {
477 1331005d Scott Ullrich
			
478
			log_error("DynDns: _detectChange() starting.");
479
		
480 1fa2f630 Erik Kristensen
			$currentTime = time();
481
482
			$wan_ip = get_current_wan_address();
483
			$this->_dnsIP = $wan_ip;
484 fc3a37e9 Scott Ullrich
			log_error("DynDns: Current WAN IP: {$wan_ip}");
485 1fa2f630 Erik Kristensen
486
			if (file_exists($this->_cacheFile)) {
487 e24b26e9 Scott Ullrich
				if(file_exists($this->_cacheFile))
488
					$contents = file_get_contents($this->_cacheFile);
489
				else
490
					$contents = "";
491 1fa2f630 Erik Kristensen
				list($cacheIP,$cacheTime) = split(':', $contents);
492
				$this->_debug($cacheIP.'/'.$cacheTime);
493 003fb312 Scott Ullrich
				$initial = false;
494 d6fcb946 Scott Ullrich
				log_error("DynDns: Cached IP: {$cacheIP}");
495 1fa2f630 Erik Kristensen
			} else {
496 53d366fd Scott Ullrich
				conf_mount_rw();
497 1fa2f630 Erik Kristensen
				$file = fopen($this->_cacheFile, 'w');
498
				fwrite($file, '0.0.0.0:'.$currentTime);
499
				fclose($file);
500 53d366fd Scott Ullrich
				conf_mount_ro();
501 1fa2f630 Erik Kristensen
				$cacheIP = '0.0.0.0';
502
				$cacheTime = $currentTime;
503 003fb312 Scott Ullrich
				$initial = true;
504 fc3a37e9 Scott Ullrich
				log_error("DynDns: No Cached IP found.");
505 1fa2f630 Erik Kristensen
			}
506
507 e24b26e9 Scott Ullrich
			/*   use 2419200 for dyndns, dhs, easydns, noip, hn
508
			 *   zoneedit, dyns, ods
509
			 */
510
			$time = '2419200';
511 1fa2f630 Erik Kristensen
512 e24b26e9 Scott Ullrich
			$needs_updating = FALSE;
513
			/* lets deterimine if the item needs updating */
514 a255e1f7 Scott Ullrich
			if ($cacheIP != $wan_ip) {
515 e24b26e9 Scott Ullrich
				$needs_updating = TRUE;
516 fc3a37e9 Scott Ullrich
				log_error("DynDns: cacheIP != wan_ip.  Updating.");
517 a255e1f7 Scott Ullrich
			}
518
			$update_reason = "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
519
			if (($currentTime - $cacheTime) > $time ) {
520 e24b26e9 Scott Ullrich
				$needs_updating = TRUE;
521 fc3a37e9 Scott Ullrich
				log_error("DynDns: More than 28 days.  Updating.");
522 a255e1f7 Scott Ullrich
			}
523
			$update_reason .= "{$currentTime} - {$cacheTime} > {$time} ";
524
			if ($initial == TRUE) {
525 e24b26e9 Scott Ullrich
				$needs_updating = TRUE;
526 a255e1f7 Scott Ullrich
				$update_reason .= "Inital update. ";
527 fc3a37e9 Scott Ullrich
				log_error("DynDns: Initial run.   Updating.");
528 a255e1f7 Scott Ullrich
			}
529
			/*   finally if we need updating then store the
530
			 *   new cache value and return true
531
                         */
532 e24b26e9 Scott Ullrich
			if($needs_updating == TRUE) {
533 1fa2f630 Erik Kristensen
				/* Write WAN IP to cache file */
534 53d366fd Scott Ullrich
				conf_mount_rw();
535 1fa2f630 Erik Kristensen
				$file = fopen($this->_cacheFile, 'w');
536
				fwrite($file, $wan_ip.':'.$currentTime);
537 53d366fd Scott Ullrich
				fclose($file);
538
				conf_mount_ro();
539 1fa2f630 Erik Kristensen
				return TRUE;
540
			} else {
541 e24b26e9 Scott Ullrich
				return FALSE;			
542 1fa2f630 Erik Kristensen
			}
543 a255e1f7 Scott Ullrich
			
544
			log_error("DynDns debug information: {$update_reason}");
545
			
546 1fa2f630 Erik Kristensen
		}
547
548
		/*
549
		 * Private Funcation (added 16 July 05) [beta]
550
		 *   - Writes debug information to a file.
551
		 *   - This function is only called when a unknown response
552
		 *   - status is returned from a DynDNS service provider.
553
		 */
554
		function _debug ($data) {
555 918d2e7e Scott Ullrich
			$string = date('m-d-y h:i:s').' - ('.$this->_debugID.') - ['.$this->_dnsService.'] - '.$data.'\n';
556 53d366fd Scott Ullrich
			conf_mount_rw();
557 1fa2f630 Erik Kristensen
			$file = fopen($this->_debugFile, 'a');
558
			fwrite($file, $string);
559
			fclose($file);
560 53d366fd Scott Ullrich
			conf_mount_ro();
561 1fa2f630 Erik Kristensen
		}
562
563
	}
564
565 0c7b834e Scott Ullrich
?>