File: 1.19.1b/server/extras/tests/dynamic_dns_test.php (View as HTML)

  1: <?php // dynamic_dns_test.php v 0.01 14/11/2010
  2: /* -------------------------------------------------------------
  3: This file is part of FreeNATS
  4: 
  5: FreeNATS is (C) Copyright 2008-2009 PurplePixie Systems
  6: 
  7: FreeNATS is free software: you can redistribute it and/or modify
  8: it under the terms of the GNU General Public License as published by
  9: the Free Software Foundation, either version 3 of the License, or
 10: (at your option) any later version.
 11: 
 12: FreeNATS is distributed in the hope that it will be useful,
 13: but WITHOUT ANY WARRANTY; without even the implied warranty of
 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15: GNU General Public License for more details.
 16: 
 17: You should have received a copy of the GNU General Public License
 18: along with FreeNATS.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freenats
 21: -------------------------------------------------------------- */
 22: 
 23: /* Description:
 24: 
 25: This is a test add-on mobule for FreeNATS version 1
 26: 
 27: USAGE INSTRUCTIONS:
 28: 
 29: Place into the server/base/site/tests directory being sure to keep a .php
 30: extension on the end of the file. Enable the system variable site.include.testss
 31: (set to 1) to enable inclusion.
 32: 
 33: This test is configured through the standard test management interface
 34: 
 35: */
 36: 
 37: 
 38: function get_external_ip($url="") 
 39: 	{
 40: 	global $NATS;
 41: 	
 42: 	if ($url=="") $url="http://xml.purplepixie.org/apps/ipaddress/?format=plain";
 43: 		
 44: 	if (function_exists("curl_getinfo")) // use CURL if present
 45: 		{
 46: 		$ch=curl_init();
 47: 		curl_setopt($ch,CURLOPT_URL,$url);
 48: 		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
 49: 		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
 50: 		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
 51: 		curl_setopt($ch,CURLOPT_HEADER,0);
 52: 		if (!$output=curl_exec($ch))
 53: 			{
 54: 			$ctr=-1; // failed
 55: 			}
 56: 		else $ctr=round(curl_getinfo($ch,CURLINFO_SIZE_DOWNLOAD)/1024,2);
 57: 		curl_close($ch);
 58: 		
 59: 		if ($ctr==0) $ctr="0.0001";
 60: 		
 61: 		}
 62: 	else
 63: 		{	// no CURL - use fopen()	
 64: 		$fp=@fopen($url,"r");
 65: 		if ($fp<=0)
 66: 			{
 67: 			return -1;
 68: 			}
 69: 		$ctr=0;
 70: 		while ($output.=@fgets($fp,1024)) $ctr+=sizeof($body);
 71: 		@fclose($fp);
 72: 		}
 73: 		
 74: 	if ($ctr<0) return $ctr;
 75: 	else return $output;
 76: 	}
 77: 
 78: 
 79: global $NATS;
 80: 
 81: class Dynamic_DNS_Test extends FreeNATS_Local_Test
 82: {
 83: 
 84:  function DoTest($testname,$param,$hostname,$timeout,$params)
 85:  {
 86:  /* parameters: 
 87:  0: hostname
 88:  1: External IP Info Provider
 89:     0 = PurplePixie
 90:     1 = Other
 91:  2: Other IP Info Provider URL
 92:  */
 93:  
 94: if ($params[1]==0) $url="";
 95: else $url=$params[2];
 96: 
 97:  $ip=get_external_ip($url);
 98:  // echo "External: ".$ip."\n"; 
 99:  
100:  if ($ip<=0) return $ip;
101:  
102:  $dynamic_ip=gethostbyname($params[0]);
103:  // echo "Dynamic: ".$params[0]." = ".$dynamic_ip."\n"; 
104: 
105:  if ($dynamic_ip == $params[0]) return -2; // unmodified host; hostname lookup failed
106:  
107:  if ($dynamic_ip == $ip) return 1; // successful resolution and match
108:  else return 0; // External IP and Dynamic IP are not matching 
109:  }
110: 
111:  function Evaluate($result) 
112:  {
113:  if ($result>0) return 0; // FreeNATS passed (0) flag if > 0
114:  return 2; // FreeNATS failed (2) flag ( <= 0 )
115:  }
116: 
117:  function ProtectOutput(&$test)
118:  {
119:  $test['testparam3']=""; // blank password for output
120:  return true;
121:  }
122: 
123:  function DisplayForm(&$test)
124:  {
125:  $out="";
126:  $out.="<table width=100% border=0>";
127:  $out.="<tr><td align=right valign=top>Hostname:</td>";
128:  $out.="<td align=left>";
129:  $out.="<input type=text size=30 name=testparam value=\"".$test['testparam']."\">";
130:  $out.="<br><i>Dynamic DNS Hostname to Check (i.e. myhost.dyndns.org)</i>";
131:  $out.="</td></tr>";
132:  $out.="<tr><td align=right valign=top>IP:</td>";
133:  $out.="<td align=left>";
134:  $out.="<input type=radio name=testparam1 value=0";
135:  if ($test['testparam1']==0) $out.=" checked";
136:  $out.="> Use xml.purplepixie.org IP Address Service<br>";
137:  $out.="<input type=radio name=testparam1 value=1";
138:  if ($test['testparam1']==1) $out.=" checked"; 
139:  $out.="> URL: <input type=text name=testparam2 size=40 value=\"".$test['testparam2']."\"><br>";
140:  $out.="<i>This setting tells FreeNATS where to get your external IP from. You can either use ";
141:  $out.="the PurplePixie XML App gateway or put in your own URL. The URL must return a plain IP.</i>"; 
142:  $out.="</td></tr>";
143:  $out.="</table>";
144:  echo $out; // output the buffer
145:  }
146: }
147: 
148: // Now we have defined the class we must register it with FreeNATS
149: 
150: $params=array(); // blank parameters array as we have implemented DisplayForm above
151: 
152: $NATS->Tests->Register(
153:  "dynamicdns",           // the internal simple test name (must not conflict with anything else)
154:  "Dynamic_DNS_Test",      // the class name (above)
155:  $params,               // parameters (blank for now)
156:  "Dynamic DNS Test", // the display name of the test in the interface
157:  1,                     // the revision number of the test
158:  "Check Dyanamic DNS Host Against External IP");   // extended description for the test module used in overview
159: 
160: ?>
161: