Project

General

Profile

Download (13.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?
2
	/*
3
	 * PHP.updateDNS
4
	 *
5
	 * +====================================================+
6
	 *  Services Supported:
7
	 *    - DynDns (dyndns.org)
8
	 *    - DHSDns (dhs.org)
9
	 *    - No-IP (no-ip.com)
10
	 *    - EasyDNS (easydns.com)
11
	 * +----------------------------------------------------+
12
	 *  Requirements:
13
	 *    - PHP version 4.0.2 or higher with CURL Library
14
	 * +----------------------------------------------------+
15
	 *  Public Functions
16
	 *    - updatedns()
17
	 *
18
	 *  Private Functions
19
	 *    - _update()
20
	 *    - _checkStatus()
21
	 *    - _error()
22
	 *    - _detectChange()
23
	 *    - _debug()
24
	 * +----------------------------------------------------+
25
	 *  DynDNS   - Last Tested: 12 July 2005
26
	 *  No-IP    - Last Tested: 12 July 2005
27
	 *  HN.org   - Last Tested: 12 July 2005
28
	 *  EasyDNS  - Last Tested: NEVER
29
	 *  DHS      - Last Tested: 12 July 2005
30
	 *  ZoneEdit - Last Tested: NEVER
31
	 *  Dyns     - Last Tested: NEVER
32
	 * +====================================================+
33
	 *
34
	 * @author 		E.Kristensen
35
	 * @link    	http://www.idylldesigns.com/projects/phpdns/
36
	 * @version 	0.5
37
	 * @updated		16 July 05 at 17:59:12 GMT
38
	 *
39
	 */
40

    
41
	class updatedns {
42
		var $_cacheFile = '/var/etc/dyndns.cache';
43
		var $_debugFile = '/var/etc/dyndns.debug';
44
		var $_UserAgent = 'User-Agent: phpDynDNS/0.4';
45
		var $_errorVerbosity = 0;
46
		var $_dnsService;
47
		var $_dnsUser;
48
		var $_dnsPass;
49
		var $_dnsHost;
50
		var $_dnsIP;
51
		var $_dnsWildcard;
52
		var $_dnsMX;
53
		var $status;
54

    
55
		/* 
56
		 * Public Constructor Function (added 12 July 05) [beta]
57
		 *   - Gets the dice rolling for the update. 
58
		 */
59
		function updatedns ($dnsService = '', $dnsHost = '', $dnsUser = '', $dnsPass = '', $dnsWildcard = '', $dnsMX = '') {
60
			if (!$dnsService) $this->_error(2);
61
			if (!$dnsUser) $this->_error(3);
62
			if (!$dnsPass) $this->_error(4);
63
			if (!$dnsHost) $this->_error(5);
64

    
65
			$this->_dnsService = strtolower($dnsService);
66
			$this->_dnsUser = $dnsUser;
67
			$this->_dnsPass = $dnsPass;
68
			$this->_dnsHost = $dnsHost;
69
			$this->_dnsIP = $dnsIP;
70

    
71
			if ($this->_detectChange() == FALSE) {
72
				$this->_error(10);
73
			} else {
74
				if ($this->_dnsService == 'dyndns' ||
75
					$this->_dnsService == 'dhs' ||
76
					$this->_dnsService == 'noip' ||
77
					$this->_dnsService == 'easydns' ||
78
					$this->_dnsService == 'hn' ||
79
					$this->_dnsService == 'zoneedit' ||
80
					$this->_dnsService == 'dyns')
81
				{
82
					$this->_update();
83
				} else {
84
					$this->_error(6);
85
				}
86
			}
87

    
88
		}
89

    
90

    
91
		/*
92
		 * Private Function (added 12 July 05) [beta]
93
		 *   Send Update To Selected Service.
94
		 */
95
		function _update() {
96
			$ch = curl_init();
97
			curl_setopt($ch, CURLOPT_HEADER, 0);
98
			curl_setopt($ch, CURLOPT_USERAGENT, $this->_UserAgent);
99
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
100

    
101
			switch ($this->_dnsService) {
102
				case 'dyndns':
103
					$needsIP = FALSE;
104
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
105
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
106
					curl_setopt($ch, CURLOPT_URL, 'https://members.dyndns.org/nic/update?hostname='.$this->_dnsHost);
107
					$data = curl_exec($ch);
108
					curl_close($ch);
109
					$this->_checkStatus($data);
110
					break;
111
				case 'dhs':
112
					$needsIP = TRUE;
113
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
114
					curl_setopt($ch, CURLOPT_URL, 'https://members.dhs.org/nic/hosts');
115
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
116
					curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
117
					$data = curl_exec($ch);
118
					curl_close($ch);
119
					$this->_checkStatus($data);
120
					break;
121
				case 'noip':
122
					$needsIP = TRUE;
123
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
124
					curl_setopt($ch, CURLOPT_URL, 'http://dynupdate.no-ip.com/dns?username='.$this->_dnsUser.'&password='.$this->_dnsPass.'&hostname='.$this->_dnsHost.'&ip='.$this->_dnsIP);
125
					$data = curl_exec($ch);
126
					curl_close($ch);
127
					$this->_checkStatus($data);
128
					break;
129
				case 'easydns':
130
					$needsIP = TRUE;
131
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
132
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
133
					curl_setopt($ch, CURLOPT_URL, 'http://members.easydns.com/dyn/dyndns.php?hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP);
134
					$data = curl_exec($ch);
135
					curl_close($ch);
136
					$this->_checkStatus($data);
137
					break;
138
				case 'hn':
139
					$needsIP = TRUE;
140
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
141
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
142
					curl_setopt($ch, CURLOPT_URL, 'http://dup.hn.org/vanity/update?ver=1&IP='.$this->_dnsIP);
143
					$data = curl_exec($ch);
144
					curl_close($ch);
145
					$this->_checkStatus($data);
146
					break;
147
				case 'zoneedit':
148
					$needsIP = FALSE;
149
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
150
					curl_setopt($ch, CURLOPT_URL, 'https://dynamic.zoneedit.com/auth/dynamic.html?host='.$this->_dnsHost.'&dnsto='.$this->_dnsIP);
151
					$data = curl_exec($ch);
152
					curl_close($ch);
153
					$this->_checkStatus($data);
154
					break;
155
				case 'dyns';
156
					$needsIP = FALSE;
157
					curl_setopt($ch, CURLOPT_URL, 'http://www.dyns.cx/postscript011.php?username='.$this->_dnsUser.'&password='.$this->_dnsPass.'&host='.$this->_dnsHost);
158
					$data = curl_exec($ch);
159
					curl_close($ch);
160
					$this->_checkStatus($data);
161
					break;
162
				default:
163
					break;
164
			}
165
		}
166

    
167

    
168
		/*
169
		 * Private Function (added 12 July 2005) [beta]
170
		 *   Retrieve Update Status
171
		 */
172
		function _checkStatus($data) {
173
			switch ($this->_dnsService) {
174
				case 'dyndns':
175
					if (preg_match('/notfqdn/i', $data)) {
176
						$status = "phpDynDNS: (Error) Not A FQDN!";
177
					} else if (preg_match('/nochg/i', $data)) {
178
						$status = "phpDynDNS: (Success) No Change In IP Address";
179
					} else if (preg_match('/good/i', $data)) {
180
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
181
					} else if (preg_match('/noauth/i', $data)) {
182
						$status = "phpDynDNS: (Error) User Authorization Failed";
183
					} else {
184
						$status = "phpDynDNS: (Unknown Response)";
185
						$this->_debug($data);
186
					}
187
					break;
188
				case 'dhs':
189
					break;
190
				case 'noip':
191
					list($ip,$code) = split(":",$data);
192
					switch ($code) {
193
						case 0:
194
							$status = "phpDynDNS: (Success) IP address is current, no update performed.";
195
							break;
196
						case 1:
197
							$status = "phpDynDNS: (Success) DNS hostname update successful.";
198
							break;
199
						case 2:
200
							$status = "phpDynDNS: (Error) Hostname supplied does not exist.";
201
							break;
202
						case 3:
203
							$status = "phpDynDNS: (Error) Invalid Username.";
204
							break;
205
						case 4:
206
							$status = "phpDynDNS: (Error) Invalid Password.";
207
							break;
208
						case 5:
209
							$status = "phpDynDNS: (Error) To many updates sent.";
210
							break;
211
						case 6:
212
							$status = "phpDynDNS: (Error) Account disabled due to violation of No-IP terms of service.";
213
							break;
214
						case 7:
215
							$status = "phpDynDNS: (Error) Invalid IP. IP Address submitted is improperly formatted or is a private IP address or is on a blacklist.";
216
							break;
217
						case 8:
218
							$status = "phpDynDNS: (Error) Disabled / Locked Hostname.";
219
							break;
220
						case 9:
221
							$status = "phpDynDNS: (Error) Host updated is configured as a web redirect and no update was performed.";
222
							break;
223
						case 10:
224
							$status = "phpDynDNS: (Error) Group supplied does not exist.";
225
							break;
226
						case 11:
227
							$status = "phpDynDNS: (Success) DNS group update is successful.";
228
							break;
229
						case 12:
230
							$status = "phpDynDNS: (Success) DNS group is current, no update performed.";
231
							break;
232
						case 13:
233
							$status = "phpDynDNS: (Error) Update client support not available for supplied hostname or group.";
234
							break;
235
						case 14:
236
							$status = "phpDynDNS: (Error) Hostname supplied does not have offline settings configured.";
237
							break;
238
						case 99:
239
							$status = "phpDynDNS: (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
240
							break;
241
						case 100:
242
							$status = "phpDynDNS: (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
243
							break;
244
						default:
245
							$status = "phpDynDNS: (Unknown Response)";
246
							$this->_debug($data);
247
							break;
248
					}
249
					break;
250
				case 'easydns':
251
					if (preg_match('/NOACCESS/i', $data)) {
252
						$status = "phpDynDNS: (Error) Authentication Failed: Username and/or Password was Incorrect.";
253
					} else if (preg_match('/NOSERVICE/i', $data)) {
254
						$status = "phpDynDNS: (Error) No Service: Dynamic DNS Service has been disabled for this domain.";
255
					} else if (preg_match('/ILLEGAL INPUT/i', $data)) {
256
						$status = "phpDynDNS: (Error) Illegal Input: Self-Explantory";
257
					} else if (preg_match('/TOOSOON/i', $data)) {
258
						$status = "phpDynDNS: (Error) Too Soon: Not Enough Time Has Elapsed Since Last Update";
259
					} else if (preg_match('/NOERROR/i', $data)) {
260
						$status = "phpDynDNS: (Success) IP Updated Successfully!";					
261
					} else {
262
						$status = "phpDynDNS: (Unknown Response)";
263
						$this->_debug($data);
264
					}
265
					break;
266
				case 'hn':
267
					break;
268
				case 'zoneedit':
269
					if (preg_match('/{700,799}/i', $data)) {
270
						$status = "phpDynDNS: (Error) Update Failed!";
271
					} else if (preg_match('/{200,201}/i', $data)) {
272
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
273
					} else {
274
						$status = "phpDynDNS: (Unknown Response)";
275
						$this->_debug($data);
276
					}
277
					break;
278
				case 'dyns':
279
					if (preg_match("/400/i", $data)) {
280
						$status = "phpDynDNS: (Error) Bad Request - The URL was malformed. Required parameters were not provided.";
281
					} else if (preg_match('/402/i', $data)) {
282
						$status = "phpDynDNS: (Error) Update Too Soon - You have tried updating to quickly since last change.";
283
					} else if (preg_match('/403/i', $data)) {
284
						$status = "phpDynDNS: (Error) Database Error - There was a server-sided database error.";
285
					} else if (preg_match('/405/i', $data)) {
286
						$status = "phpDynDNS: (Error) Hostname Error - The hostname (".$this->_dnsHost.") doesn't belong to you.";
287
					} else if (preg_match('/200/i', $data)) {
288
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
289
					} else {
290
						$status = "phpDynDNS: (Unknown Response)";
291
						$this->_debug($data);
292
					}
293
					break;
294
			}
295
			$this->status = $status;
296
			log_error($status);
297
		}
298

    
299

    
300
		/*
301
		 * Private Function (added 12 July 05) [beta]
302
		 *   Return Error, Set Last Error, and Die.
303
		 */
304
		function _error($errorNumber = '1') {
305
			switch ($errorNumber) {
306
				case 0:
307
					break;
308
				case 2:
309
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
310
					break;
311
				case 3:
312
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
313
					break;
314
				case 4:
315
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
316
					break;
317
				case 5:
318
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
319
					break;
320
				case 6:
321
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
322
					break;
323
				case 10:
324
					$error = 'phpDynDNS: No Change In My IP Address and/or 28 Days Has Not Past. Not Updating Dynamic DNS Entry.';
325
					break;
326
				default:
327
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
328
					$this->_debug($data);
329
					break;
330
			}
331
			$this->lastError = $error;
332
			log_error($error);
333
		}
334

    
335

    
336
		/*
337
		 * Private Function (added 12 July 05) [beta]
338
		 *   - Detect whether or not IP needs to be updated.
339
		 *      | Written Specifically for pfSense (pfsense.com) may
340
		 *      | work with other systems. pfSense base is FreeBSD.
341
		 */
342
		function _detectChange() {
343
			$currentTime = time();
344

    
345
			$wan_if = get_real_wan_interface();
346
			$wan_ip = find_interface_ip($wan_if);
347
			$this->_dnsIP = $wan_ip;
348

    
349
			if (file_exists($this->_cacheFile)) {
350
				$file = fopen($this->_cacheFile, 'r');
351
				$contents = fread($file, filesize($this->_cacheFile));
352
				fclose($file);
353
				list($cacheIP,$cacheTime) = split(':', $contents);
354
			} else {
355
				$file = fopen($this->_cacheFile, 'w');
356
				fwrite($file, '0.0.0.0:'.$currentTime);
357
				fclose($file);
358
				$cacheIP = '0.0.0.0';
359
				$cacheTime = $currentTime;
360
			}
361

    
362
			switch ($this->_dnsService) {
363
				case 'dyndns':
364
					$time = '2419200';
365
					break;
366
				case 'dhs':
367
					$time = '2419200';
368
					break;
369
				case 'easydns':
370
					$time = '2419200';
371
					break;
372
				case 'noip':
373
					$time = '2419200';
374
					break;
375
				case 'hn':
376
					$time = '2419200';
377
					break;
378
				case 'zoneedit':
379
					$time = '2419200';
380
					break;
381
				case 'dyns':
382
					$time = '2419200';
383
					break;
384
			}
385

    
386
			/* If IP addresses are different or 28 days have passed update record */
387
			if ( ($cacheIP != $wan_ip) || ( ($currentTime - $cacheTime) > $time ) ) {
388
				/* Write WAN IP to cache file */
389
				$file = fopen($this->_cacheFile, 'w');
390
				fwrite($file, $wan_ip.':'.$currentTime);
391
				fclose($file);
392

    
393
				return TRUE;
394
			} else {
395

    
396
				return FALSE;
397
			}
398
		}
399

    
400

    
401
		/*
402
		 * Private Funcation (added 16 July 05) [beta]
403
		 *   - Writes debug information to a file.
404
		 *   - This function is only called when a unknown response
405
		 *   - status is returned from a DynDNS service provider.
406
		 */
407
		function _debug ($data) {
408
			$string = date('m-d-y h:i:s').' - ['.$this->_dnsService.'] - '.$data;
409
			$file = fopen($this->_debugFile, 'a');
410
			fwrite($file, $string);
411
			fclose($file);
412
		}
413

    
414
	}
415

    
416
?>
(4-4/22)