summaryrefslogtreecommitdiffstats
path: root/etc/inc/dyndns.class
blob: eb81ec46942fa9802be6ec859280f70afb8c20b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
<?php
	/*
	 * PHP.updateDNS (pfSense version)
	 *
	 * +====================================================+
	 *  Services Supported:
	 *    - DynDns (dyndns.org) [dynamic, static, custom]
	 *    - DHSDns (dhs.org)
	 *    - No-IP (no-ip.com)
	 *    - EasyDNS (easydns.com)
	 *    - DHS (www.dhs.org)
	 *    - HN (hn.org) -- incomplete checking!
	 *    - DynS (dyns.org)
	 *    - ZoneEdit (zoneedit.com)
	 *    - FreeDNS (freedns.afraid.org)
	 *    - Loopia (loopia.se)
	 *    - StaticCling (staticcling.org)
	 * +----------------------------------------------------+
	 *  Requirements:
	 *    - PHP version 4.0.2 or higher with CURL Library
	 * +----------------------------------------------------+
	 *  Public Functions
	 *    - updatedns()
	 *
	 *  Private Functions
	 *    - _update()
	 *    - _checkStatus()
	 *    - _error()
	 *    - _detectChange()
	 *    - _debug()
	 * +----------------------------------------------------+
	 *  DynDNS Dynamic - Last Tested: 12 July 2005
	 *  DynDNS Static  - Last Tested: NEVER
	 *  DynDNS Custom  - Last Tested: NEVER
	 *  No-IP          - Last Tested: 12 July 2005
	 *  HN.org         - Last Tested: 12 July 2005
	 *  EasyDNS        - Last Tested: NEVER
	 *  DHS            - Last Tested: 12 July 2005
	 *  ZoneEdit       - Last Tested: NEVER
	 *  Dyns           - Last Tested: NEVER
	 *  ODS            - Last Tested: 02 August 2005
	 *  FreeDNS        - Last Tested: NEVER
	 *  Loopia         - Last Tested: NEVER
	 *  StaticCling    - Last Tested: 27 April 2006
	 * +====================================================+
	 *
	 * @author 	E.Kristensen
	 * @link    	http://www.idylldesigns.com/projects/phpdns/
	 * @version 	0.8
	 * @updated	13 October 05 at 21:02:42 GMT
	 *
	 */

	class updatedns {
		var $_cacheFile = '/cf/conf/dyndns.cache';
		var $_debugFile = '/var/etc/dyndns.debug';
		var $_UserAgent = 'User-Agent: phpDynDNS/0.7';
		var $_errorVerbosity = 0;
		var $_dnsService;
		var $_dnsUser;
		var $_dnsPass;
		var $_dnsHost;
		var $_dnsIP;
		var $_dnsWildcard;
		var $_dnsMX;
		var $_dnsBackMX;
		var $_dnsWanip;
		var $_dnsServer;
		var $_dnsPort;
		var $_dnsUpdateURL;
		var $status;
		var $_debugID;
		
		/* 
		 * Public Constructor Function (added 12 July 05) [beta]
		 *   - Gets the dice rolling for the update. 
		 */
		function updatedns ($dnsService = '', $dnsHost = '', $dnsUser = '', $dnsPass = '',
				    $dnsWildcard = 'OFF', $dnsMX = '', $dnsBackMX = '', $dnsWanip = '',
				    $dnsServer = '', $dnsPort = '', $dnsUpdateURL = '') {
			
			global $config;
			
			log_error("DynDns: updatedns() starting");
			
			if (!$dnsService) $this->_error(2);
			if (!($dnsService == 'freedns')) {

				/* all services except freedns use these */

				if (!$dnsUser) $this->_error(3);
				if (!$dnsPass) $this->_error(4);
				if (!$dnsHost) $this->_error(5);
			} else {

				/* freedns needs this */

				if (!$dnsHost) $this->_error(5);
			}
			
			$this->_dnsService = strtolower($dnsService);
			$this->_dnsUser = $dnsUser;
			$this->_dnsPass = $dnsPass;
			$this->_dnsHost = $dnsHost;
			$this->_dnsWanip = $dnsWanip;
			$this->_dnsServer = $dnsServer;
			$this->_dnsPort = $dnsPort;
			$this->_dnsWildcard = $dnsWildcard;
			$this->_dnsMX = $dnsMX;
			
			if(!$wan_ip) 
				$wan_ip = get_current_wan_address();
				
			$this->_dnsIP = $wan_ip;
			$this->_debugID = rand(1000000, 9999999);
			
			if ($this->_detectChange() == FALSE) {
				$this->_error(10);
			} else {
				if ($this->_dnsService == 'dyndns' ||
					$this->_dnsService == 'dyndns-static' ||
					$this->_dnsService == 'dyndns-custom' ||
					$this->_dnsService == 'dhs' ||
					$this->_dnsService == 'noip' ||
					$this->_dnsService == 'easydns' ||
					$this->_dnsService == 'hn' ||
					$this->_dnsService == 'zoneedit' ||
					$this->_dnsService == 'dyns' ||
					$this->_dnsService == 'ods' ||
					$this->_dnsService == 'freedns' ||
					$this->_dnsService == 'loopia' ||
					$this->_dnsService == 'staticcling')
				{
					$this->_update();
				} else {
					$this->_error(6);
				}
			}					
		}
			
		/*
		 * Private Function (added 12 July 05) [beta]
		 *   Send Update To Selected Service.
		 */
		function _update() {
		
			log_error("DynDns: DynDns _update() starting.");
		
			if ($this->_dnsService != 'ods') {
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_HEADER, 0);
				curl_setopt($ch, CURLOPT_USERAGENT, $this->_UserAgent);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
			}

			switch ($this->_dnsService) {
				case 'dyndns':
					$needsIP = FALSE;
					log_error("DynDns: DynDns _update() starting. Dynamic");
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
					$server = "https://members.dyndns.org/nic/update";
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
						$port = ":" . $this->_dnsPort;
					curl_setopt($ch, CURLOPT_URL, $server .$port . '?system=dyndns&hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP.'&wildcard='.$this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=NO');
					$data = curl_exec($ch);
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
					curl_close($ch);
					$this->_checkStatus($data);
					break;
				case 'dyndns-static':
					$needsIP = FALSE;
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
					$server = "https://members.dyndns.org/nic/update";
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
						$port = ":" . $this->_dnsPort;
					curl_setopt($ch, CURLOPT_URL, $server.$port.'?system=statdns&hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP.'&wildcard='.$this->_dnsWildcard.'&mx='.$this->_dnsMX.'&backmx=NO');
					$data = curl_exec($ch);
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
					curl_close($ch);
					$this->_checkStatus($data);
					break;
				case 'dyndns-custom':
					$needsIP = FALSE;
					if (isset($this->_dnsWildcard) && $this->_dnsWildcard != "OFF") $this->_dnsWildcard = "ON";
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
					$server = "https://members.dyndns.org/nic/update";
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
						$port = ":" . $this->_dnsPort;					
					curl_setopt($ch, CURLOPT_URL, $server.$port.'?system=custom&hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP.'&wildcard='.$this->_dnsWildcard.'&mx='.$this->_dnsMX.'&backmx=NO');
					$data = curl_exec($ch);
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
					curl_close($ch);
					$this->_checkStatus($data);
					break;
				case 'dhs':
					$needsIP = TRUE;
					$post_data['hostscmd'] = 'edit';
					$post_data['hostscmdstage'] = '2';
					$post_data['type'] = '4';
					$post_data['updatetype'] = 'Online';
					$post_data['mx'] = $this->_dnsMX;
					$post_data['mx2'] = '';
					$post_data['txt'] = '';
					$post_data['offline_url'] = '';
					$post_data['cloak'] = 'Y';
					$post_data['cloak_title'] = '';
					$post_data['ip'] = $this->_dnsIP;
					$post_data['domain'] = 'dyn.dhs.org';
					$post_data['hostname'] = $this->_dnsHost;
					$post_data['submit'] = 'Update';
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					$server = "https://members.dhs.org/nic/hosts";
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
						$port = ":" . $this->_dnsPort;					
					curl_setopt($ch, CURLOPT_URL, '{$server}{$port}');
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
					curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
					$data = curl_exec($ch);
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
					curl_close($ch);
					$this->_checkStatus($data);
					break;
				case 'noip':
					$needsIP = TRUE;
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					$server = "http://dynupdate.no-ip.com/ducupdate.php";
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
						$port = ":" . $this->_dnsPort;
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?username=' . $this->_dnsUser . '&pass=' . $this->_dnsPass . '&hostname=' . $this->_dnsHost.'&ip=' . $this->_dnsIP);
					$data = curl_exec($ch);
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
					curl_close($ch);
					$this->_checkStatus($data);
					break;
				case 'easydns':
					$needsIP = TRUE;
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
					$server = "http://members.easydns.com/dyn/dyndns.php";
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
						$port = ":" . $this->_dnsPort;
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?hostname=' . $this->_dnsHost . '&myip=' . $this->_dnsIP . '&wildcard=' . $this->_dnsWildcard . '&mx=' . $this->_dnsMX . '&backmx=' . $this->_dnsBackMX);
					$data = curl_exec($ch);
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
					curl_close($ch);
					$this->_checkStatus($data);
					break;
				case 'hn':
					$needsIP = TRUE;
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
					$server = "http://dup.hn.org/vanity/update";
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
						$port = ":" . $this->_dnsPort;
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?ver=1&IP=' . $this->_dnsIP);
					$data = curl_exec($ch);
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
					curl_close($ch);
					$this->_checkStatus($data);
					break;
				case 'zoneedit':
					$needsIP = FALSE;
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);

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

					$data = curl_exec($ch);
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
					curl_close($ch);
					$this->_checkStatus($data);
					break;
				case 'dyns':
					$needsIP = FALSE;
					$server = "http://www.dyns.cx/postscript011.php";
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
						$port = ":" . $this->_dnsPort;					
					curl_setopt($ch, CURLOPT_URL, $server . $port . '?username=' . $this->_dnsUser . '&password=' . $this->_dnsPass . '&host=' . $this->_dnsHost);
					$data = curl_exec($ch);
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
					curl_close($ch);
					$this->_checkStatus($data);
					break;
				case 'ods':
					$needsIP = FALSE;
					$misc_errno = 0;
					$misc_error = "";
					$server = "ods.org";
					$port = "";
					if($this->_dnsServer)
						$server = $this->_dnsServer;
					if($this->_dnsPort)
						$port = ":" . $this->_dnsPort;						
					$this->con['socket'] = fsockopen("{$server}{$port}", "7070", $misc_errno, $misc_error, 30);
					/* Check that we have connected */
					if (!$this->con['socket']) {
						print "error! could not connect.";
						break;
					}
					/* Here is the loop. Read the incoming data (from the socket connection) */
					while (!feof($this->con['socket'])) {
						$this->con['buffer']['all'] = trim(fgets($this->con['socket'], 4096));
						$code = substr($this->con['buffer']['all'], 0, 3);
						sleep(1);
						switch($code) {
							case 100:
								fputs($this->con['socket'], "LOGIN ".$this->_dnsUser." ".$this->_dnsPass."\n");
								break;
							case 225:
								fputs($this->con['socket'], "DELRR ".$this->_dnsHost." A\n");
								break;
							case 901:
								fputs($this->con['socket'], "ADDRR ".$this->_dnsHost." A ".$this->_dnsIP."\n");
								break;
							case 795:
								fputs($this->con['socket'], "QUIT\n");
								break;
						}
					}
					$this->_checkStatus($code);
					break;
				case 'freedns':
					$needIP = FALSE;
					curl_setopt($ch, CURLOPT_URL, 'http://freedns.afraid.org/dynamic/update.php?'.$this->_dnsHost);
					$data = curl_exec($ch);
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
					curl_close($ch);
					$this->_checkStatus($data);
					break;
				case 'loopia':
					$needsIP = TRUE;
					curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
					curl_setopt($ch, CURLOPT_USERPWD, $this->_dnsUser.':'.$this->_dnsPass);
					curl_setopt($ch, CURLOPT_URL, 'https://dns.loopia.se/XDynDNSServer/XDynDNS.php?hostname='.$this->_dnsHost.'&myip='.$this->_dnsIP);
					$data = curl_exec($ch);
					if (@curl_error($ch)) log_error("Curl error occurred: " . curl_error($ch));
					curl_close($ch);
					$this->_checkStatus($data);
					break;
                                case 'staticcling':
                                        $needsIP = FALSE;
                                        curl_setopt($ch, CURLOPT_URL, 'http://www.staticcling.org/update.html?login='.$this->_dnsUser.'&pass='.$this->_dnsPass);
                                        $data = curl_exec($ch);
                                        if (@curl_error($ch)) log_error("Curl error occured: " . curl_error($ch));
                                        curl_close($ch);
                                        $this->_checkStatus($data);
                                        break;
				default:
					break;
			}
		}

		/*
		 * Private Function (added 12 July 2005) [beta]
		 *   Retrieve Update Status
		 */
		function _checkStatus($data) {
			log_error("DynDns: DynDns _checkStatus() starting.");
			log_error("DynDns: Current Service: {$this->_dnsService}");
			$successful_update = false;
			switch ($this->_dnsService) {
				case 'dyndns':
					if (preg_match('/notfqdn/i', $data)) {
						$status = "phpDynDNS: (Error) Not A FQDN!";
					} else if (preg_match('/nochg/i', $data)) {
						$status = "phpDynDNS: (Success) No Change In IP Address";
						$successful_update = true;
					} else if (preg_match('/good/i', $data)) {
						$status = "phpDynDNS: (Success) IP Address Changed Successfully! (".$this->_dnsIP.")";
						$successful_update = true;
					} else if (preg_match('/noauth/i', $data)) {
						$status = "phpDynDNS: (Error) User Authorization Failed";
					} else {
						$status = "phpDynDNS: (Unknown Response)";
						log_error("phpDynDNS: PAYLOAD: {$data}");
						$this->_debug($data);
					}
					break;
				case 'dyndns-static':
					if (preg_match('/notfqdn/i', $data)) {
						$status = "phpDynDNS: (Error) Not A FQDN!";
					} else if (preg_match('/nochg/i', $data)) {
						$status = "phpDynDNS: (Success) No Change In IP Address";
						$successful_update = true;
					} else if (preg_match('/good/i', $data)) {
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
						$successful_update = true;
					} else if (preg_match('/noauth/i', $data)) {
						$status = "phpDynDNS: (Error) User Authorization Failed";
					} else {
						$status = "phpDynDNS: (Unknown Response)";
						log_error("phpDynDNS: PAYLOAD: {$data}");
						$this->_debug($data);
					}
					break;
				case 'dyndns-custom':
					if (preg_match('/notfqdn/i', $data)) {
						$status = "phpDynDNS: (Error) Not A FQDN!";
					} else if (preg_match('/nochg/i', $data)) {
						$status = "phpDynDNS: (Success) No Change In IP Address";
						$successful_update = true;
					} else if (preg_match('/good/i', $data)) {
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
						$successful_update = true;
					} else if (preg_match('/noauth/i', $data)) {
						$status = "phpDynDNS: (Error) User Authorization Failed";
					} else {
						$status = "phpDynDNS: (Unknown Response)";
						log_error("phpDynDNS: PAYLOAD: {$data}");
						$this->_debug($data);
					}
					break;
				case 'dhs':
					break;
				case 'noip':
					list($ip,$code) = split(":",$data);
					switch ($code) {
						case 0:
							$status = "phpDynDNS: (Success) IP address is current, no update performed.";
							$successful_update = true;
							break;
						case 1:
							$status = "phpDynDNS: (Success) DNS hostname update successful.";
							$successful_update = true;
							break;
						case 2:
							$status = "phpDynDNS: (Error) Hostname supplied does not exist.";
							break;
						case 3:
							$status = "phpDynDNS: (Error) Invalid Username.";
							break;
						case 4:
							$status = "phpDynDNS: (Error) Invalid Password.";
							break;
						case 5:
							$status = "phpDynDNS: (Error) To many updates sent.";
							break;
						case 6:
							$status = "phpDynDNS: (Error) Account disabled due to violation of No-IP terms of service.";
							break;
						case 7:
							$status = "phpDynDNS: (Error) Invalid IP. IP Address submitted is improperly formatted or is a private IP address or is on a blacklist.";
							break;
						case 8:
							$status = "phpDynDNS: (Error) Disabled / Locked Hostname.";
							break;
						case 9:
							$status = "phpDynDNS: (Error) Host updated is configured as a web redirect and no update was performed.";
							break;
						case 10:
							$status = "phpDynDNS: (Error) Group supplied does not exist.";
							break;
						case 11:
							$status = "phpDynDNS: (Success) DNS group update is successful.";
							$successful_update = true;
							break;
						case 12:
							$status = "phpDynDNS: (Success) DNS group is current, no update performed.";
							$successful_update = true;
							break;
						case 13:
							$status = "phpDynDNS: (Error) Update client support not available for supplied hostname or group.";
							break;
						case 14:
							$status = "phpDynDNS: (Error) Hostname supplied does not have offline settings configured.";
							break;
						case 99:
							$status = "phpDynDNS: (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
							break;
						case 100:
							$status = "phpDynDNS: (Error) Client disabled. Client should exit and not perform any more updates without user intervention.";
							break;
						default:
							$status = "phpDynDNS: (Unknown Response)";
							$this->_debug("Unknown Response: ".$data);
							break;
					}
					break;
				case 'easydns':
					if (preg_match('/NOACCESS/i', $data)) {
						$status = "phpDynDNS: (Error) Authentication Failed: Username and/or Password was Incorrect.";
					} else if (preg_match('/NOSERVICE/i', $data)) {
						$status = "phpDynDNS: (Error) No Service: Dynamic DNS Service has been disabled for this domain.";
					} else if (preg_match('/ILLEGAL INPUT/i', $data)) {
						$status = "phpDynDNS: (Error) Illegal Input: Self-Explantory";
					} else if (preg_match('/TOOSOON/i', $data)) {
						$status = "phpDynDNS: (Error) Too Soon: Not Enough Time Has Elapsed Since Last Update";
					} else if (preg_match('/NOERROR/i', $data)) {
						$status = "phpDynDNS: (Success) IP Updated Successfully!";
						$successful_update = true;
					} else {
						$status = "phpDynDNS: (Unknown Response)";
						log_error("phpDynDNS: PAYLOAD: {$data}");
						$this->_debug($data);
					}
					break;
				case 'hn':
					/* FIXME: add checks */
					break;
				case 'zoneedit':
					if (preg_match('/799/i', $data)) {
						$status = "phpDynDNS: (Error 799) Update Failed!";				
					} else if (preg_match('/700/i', $data)) {
						$status = "phpDynDNS: (Error 700) Update Failed!";
					} else if (preg_match('/200/i', $data)) {
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
						$successful_update = true;
					} else if (preg_match('/201/i', $data)) {
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
						$successful_update = true;						
					} else {
						$status = "phpDynDNS: (Unknown Response)";
						log_error("phpDynDNS: PAYLOAD: {$data}");
						$this->_debug($data);
					}
					break;
				case 'dyns':
					if (preg_match("/400/i", $data)) {
						$status = "phpDynDNS: (Error) Bad Request - The URL was malformed. Required parameters were not provided.";
					} else if (preg_match('/402/i', $data)) {
						$status = "phpDynDNS: (Error) Update Too Soon - You have tried updating to quickly since last change.";
					} else if (preg_match('/403/i', $data)) {
						$status = "phpDynDNS: (Error) Database Error - There was a server-sided database error.";
					} else if (preg_match('/405/i', $data)) {
						$status = "phpDynDNS: (Error) Hostname Error - The hostname (".$this->_dnsHost.") doesn't belong to you.";
					} else if (preg_match('/200/i', $data)) {
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
						$successful_update = true;
					} else {
						$status = "phpDynDNS: (Unknown Response)";
						log_error("phpDynDNS: PAYLOAD: {$data}");
						$this->_debug($data);
					}
					break;
				case 'ods':
					if (preg_match("/299/i", $data)) {
						$status = "phpDynDNS: (Success) IP Address Updated Successfully!";
						$successful_update = true;
					} else {
						$status = "phpDynDNS: (Unknown Response)";
						log_error("phpDynDNS: PAYLOAD: {$data}");
						$this->_debug($data);
					}
					break;
				case 'freedns':
					if (preg_match("/has not changed./i", $data)) {
						$status = "phpDynDNS: (Success) No Change In IP Address";
						$successful_update = true;
					} else if (preg_match("/Updated/i", $data)) {
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
						$successful_update = true;
					} else {
						$status = "phpDynDNS: (Unknown Response)";
						log_error("phpDynDNS: PAYLOAD: {$data}");
						$this->_debug($data);
					} 
					break;
				case 'loopia':
					if (preg_match("/nochg/i", $data)) {
						$status = "phpDynDNS: (Success) No Change In IP Address";
						$successful_update = true;
					} else if (preg_match("/good/i", $data)) {
						$status = "phpDynDNS: (Success) IP Address Changed Successfully!";
						$successful_update = true;
					} else if (preg_match('/badauth/i', $data)) {
						$status = "phpDynDNS: (Error) User Authorization Failed";
					} else {
						$status = "phpDynDNS: (Unknown Response)";
						log_error("phpDynDNS: PAYLOAD: {$data}");
						$this->_debug($data);
					}
					break;
                                case 'staticcling':
                                        if (preg_match("/invalid ip/i", $data)) {
                                                $status = "phpDynDNS: (Error) Bad Request - The IP provided was invalid.";
                                        } else if (preg_match('/required info missing/i', $data)) {
                                                $status = "phpDynDNS: (Error) Bad Request - Required parameters were not provided.";
                                        } else if (preg_match('/invalid characters/i', $data)) {
                                                $status = "phpDynDNS: (Error) Bad Request - Illegal characters in either the username or the password.";
                                        } else if (preg_match('/bad password/i', $data)) {
                                                $status = "phpDynDNS: (Error) Invalid password.";
                                        } else if (preg_match('/account locked/i', $data)) {
                                                $status = "phpDynDNS: (Error) This account has been administratively locked.";
                                        } else if (preg_match('/update too frequent/i', $data)) {
                                                $status = "phpDynDNS: (Error) Updating too frequently.";
                                        } else if (preg_match('/DB error/i', $data)) {
                                                $status = "phpDynDNS: (Error) Server side error.";
                                        } else if (preg_match('/success/i', $data)) {
                                                $status = "phpDynDNS: (Success) IP Address Updated Successfully!";
                                                $successful_update = true;
                                        } else {
                                                $status = "phpDynDNS: (Unknown Response)";
                                                log_error("phpDynDNS: PAYLOAD: {$data}");
                                                $this->_debug($data);
                                        }
                                        break;
			}
			
			if($successful_update == true) {
				/* Write WAN IP to cache file */
				$wan_ip = get_current_wan_address();
				$currentTime = time();				  
				log_error("phpDynDNS: updating cache file {$this->_cacheFile}: {$wan_ip}");
				conf_mount_rw();
				$file = fopen($this->_cacheFile, 'w');
				fwrite($file, $wan_ip.':'.$currentTime);
				fclose($file);
				conf_mount_ro();
			}
			$this->status = $status;
			log_error($status);
		}

		/*
		 * Private Function (added 12 July 05) [beta]
		 *   Return Error, Set Last Error, and Die.
		 */
		function _error($errorNumber = '1') {
			switch ($errorNumber) {
				case 0:
					break;
				case 2:
					$error = 'phpDynDNS: (ERROR!) No Dynamic DNS Service provider was selected.';
					break;
				case 3:
					$error = 'phpDynDNS: (ERROR!) No Username Provided.';
					break;
				case 4:
					$error = 'phpDynDNS: (ERROR!) No Password Provided.';
					break;
				case 5:
					$error = 'phpDynDNS: (ERROR!) No Hostname Provided.';
					break;
				case 6:
					$error = 'phpDynDNS: (ERROR!) The Dynamic DNS Service provided is not yet supported.';
					break;
				case 7:
					$error = 'phpDynDNS: (ERROR!) No Update URL Provided.';
					break;
				case 10:
					$error = 'phpDynDNS: No Change In My IP Address and/or 25 Days Has Not Past. Not Updating Dynamic DNS Entry.';
					break;
				default:
					$error = "phpDynDNS: (ERROR!) Unknown Response.";
					/* FIXME: $data isn't in scope here */
					/* $this->_debug($data); */
					break;
			}
			$this->lastError = $error;
			log_error($error);
		}

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

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

			if (file_exists($this->_cacheFile)) {
				if(file_exists($this->_cacheFile))
					$contents = file_get_contents($this->_cacheFile);
				else
					$contents = "";
				list($cacheIP,$cacheTime) = split(':', $contents);
				$this->_debug($cacheIP.'/'.$cacheTime);
				$initial = false;
				log_error("DynDns: Cached IP: {$cacheIP}");
			} else {
				conf_mount_rw();
				$file = fopen($this->_cacheFile, 'w');
				fwrite($file, '0.0.0.0:'.$currentTime);
				fclose($file);
				conf_mount_ro();
				$cacheIP = '0.0.0.0';
				$cacheTime = $currentTime;
				$initial = true;
				log_error("DynDns: No Cached IP found.");
			}

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

			$needs_updating = FALSE;
			/* lets deterimine if the item needs updating */
			if ($cacheIP != $wan_ip) {
				$needs_updating = TRUE;
				log_error("DynDns: cacheIP != wan_ip.  Updating.");
			}
			$update_reason = "Cached IP: {$cacheIP} WAN IP: {$wan_ip} ";
			if (($currentTime - $cacheTime) > $time ) {
				$needs_updating = TRUE;
				log_error("DynDns: More than 25 days.  Updating.");
			}
			$update_reason .= "{$currentTime} - {$cacheTime} > {$time} ";
			if ($initial == TRUE) {
				$needs_updating = TRUE;
				$update_reason .= "Inital update. ";
				log_error("DynDns: Initial run.   Updating.");
			}
			/*   finally if we need updating then store the
			 *   new cache value and return true
                         */
			if($needs_updating == TRUE) {
				return TRUE;
			} else {
				return FALSE;			
			}
			
			log_error("DynDns debug information: {$update_reason}");
			
		}

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

	}

?>
OpenPOWER on IntegriCloud