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

  1: <?php // advanced_page_test.php v 0.03 17/08/2009
  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: The advanced page check is configured through the standard test management
 34: interface
 35: 
 36: */
 37: 
 38: 
 39: function extended_page_checker($url,$text,$notext,$user="",$pass="",$timeout=-1) // $text and $notext are arrays in this instance
 40: 	{
 41: 	global $NATS;
 42: 	
 43: 	$timer=new TFNTimer(); // initialise the timer
 44: 	url_lookup($url); // pre-resolve the DNS into cache
 45: 
 46: 	$output=""; // output buffer
 47: 
 48: 	if ($user!="") // use HTTP-AUTH
 49: 		{
 50: 		$pos=strpos($url,"://");
 51: 		if ($pos===false) return -1; // not a valid URL
 52: 		$protocol=substr($url,0,$pos+3); // protocol section
 53: 		$uri=substr($url,$pos+3); // uri section
 54: 		$url=$protocol.$user.":".$pass."@".$uri; // make http://user:pass@uri
 55: 		}
 56: 		
 57: 	if ($timeout<=0) // use NATS or env
 58: 		{
 59: 		if (isset($NATS))
 60: 			{
 61: 			$nto=$NATS->Cfg->Get("test.http.timeout",-1);
 62: 			if ($nto>0) $timeout=$nto; // use NATS timeout
 63: 			}
 64: 		}
 65: 	if ($timeout>0) // use the set timeout
 66: 		$oldtimeout=ini_set("default_socket_timeout",$timeout);
 67: 		
 68: 	$timer->Start();
 69: 		
 70: 	if (function_exists("curl_getinfo")) // use CURL if present
 71: 		{
 72: 		$ch=curl_init();
 73: 		curl_setopt($ch,CURLOPT_URL,$url);
 74: 		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
 75: 		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
 76: 		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
 77: 		curl_setopt($ch,CURLOPT_HEADER,1);
 78: 		if ($timeout>0) curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
 79: 		if ($timeout>0) curl_setopt($ch,CURLOPT_TIMEOUT,$timeout);
 80: 		if (!$output=curl_exec($ch))
 81: 			{
 82: 			$ctr=-1; // failed
 83: 			}
 84: 		else $ctr=round(curl_getinfo($ch,CURLINFO_SIZE_DOWNLOAD)/1024,2);
 85: 		curl_close($ch);
 86: 		
 87: 		if ($ctr==0) $ctr="0.0001";
 88: 		
 89: 		}
 90: 	else
 91: 		{	// no CURL - use fopen()	
 92: 		$fp=@fopen($url,"r");
 93: 		if ($fp<=0)
 94: 			{
 95: 			if ($timeout>0) ini_set("default_socket_timeout",$oldtimeout);
 96: 			return -1;
 97: 			}
 98: 		$ctr=0;
 99: 		while ($output.=@fgets($fp,1024)) $ctr+=sizeof($body);
100: 		@fclose($fp);
101: 		}
102: 		
103: 	if ($ctr<0) return $ctr; // negative number (-1) failed to open
104: 	
105: 	$elapsed=$timer->Stop();
106: 	
107: 	if ($timeout>0) ini_set("default_socket_timeout",$oldtimeout);
108: 	
109: 	// now to check the actual text
110: 	if (is_array($text))
111: 		{
112: 		foreach($text as $findthis)
113: 			{
114: 			if ($findthis!="")
115: 				if (strpos($output,$findthis)===false) return -2; // text to be found not found
116: 			}
117: 		}
118: 	if (is_array($notext))
119: 		{
120: 		foreach($notext as $donotfindthis)
121: 			{
122: 			if ($donotfindthis!="")
123: 				if (strpos($output,$donotfindthis)!==false) return -3; // text not to find found
124: 			}
125: 		}
126: 	
127: 	return $elapsed;
128: 	}
129: 
130: 
131: global $NATS;
132: 
133: class Advanced_Pagecheck_Test extends FreeNATS_Local_Test
134: {
135: 
136:  function DoTest($testname,$param,$hostname,$timeout,$params)
137:  {
138:  /* parameters:   -- sadly very messy but to do otherwise would break plug+play
139:  0: url
140:  1: text
141:  4: notext
142:  2: user
143:  3: pass
144: 
145:  5: text
146:  6: text
147:  7: notext
148:  8: notext
149:  9: NULL
150:  */
151:  $text=array( $params[1], $params[5], $params[6] );
152:  $notext=array( $params[4], $params[7], $params[8] ); 
153: 
154:  $result=extended_page_checker($params[0],$text,$notext,$params[2],$params[3],$timeout);
155:  return $result;
156:  }
157: 
158:  function Evaluate($result) 
159:  {
160:  if ($result>0) return 0; // FreeNATS passed (0) flag if > 0
161:  return 2; // FreeNATS failed (2) flag ( <= 0 )
162:  }
163: 
164:  function ProtectOutput(&$test)
165:  {
166:  $test['testparam3']=""; // blank password for output
167:  return true;
168:  }
169: 
170:  function DisplayForm(&$test)
171:  {
172:  $out="";
173:  $out.="<table width=100% border=0>";
174:  $out.="<tr><td align=right valign=top>URL :</td>";
175:  $out.="<td align=left>";
176:  $out.="<input type=text size=30 name=testparam value=\"".$test['testparam']."\">";
177:  $out.="<br><i>Fully-qualified URL including http://</i>";
178:  $out.="</td></tr>";
179:  $out.="<tr><td align=right valign=top>Strings :</td>";
180:  $out.="<td align=left>";
181:  $out.="<input type=text size=30 name=testparam1 value=\"".$test['testparam1']."\"><br />";
182:  $out.="<input type=text size=30 name=testparam5 value=\"".$test['testparam5']."\"><br />";
183:  $out.="<input type=text size=30 name=testparam6 value=\"".$test['testparam6']."\">";
184:  $out.="<br><i>String(s) to search for - all defined must<br />be found for the test to pass<br />";
185:  $out.="Blank strings are ignored.</i>";
186:  $out.="</td></tr>";
187:  $out.="<tr><td align=right valign=top>No Strings :</td>";
188:  $out.="<td align=left>";
189:  $out.="<input type=text size=30 name=testparam4 value=\"".$test['testparam4']."\"><br />";
190:  $out.="<input type=text size=30 name=testparam7 value=\"".$test['testparam7']."\"><br />";
191:  $out.="<input type=text size=30 name=testparam8 value=\"".$test['testparam8']."\">"; 
192:  $out.="<br><i>String(s) to NOT find - fails if any are present<br>Leave blank to not use this portion of the test</i>";
193:  $out.="</td></tr>";
194: 
195:  $out.="<tr><td align=right valign=top>Username :</td>";
196:  $out.="<td align=left>";
197:  $out.="<input type=text size=30 name=testparam2 value=\"".$test['testparam2']."\">";
198:  $out.="<br><i>Specify to use HTTP-AUTH on the URL</i>";
199:  $out.="</td></tr>"; 
200: 
201:  $out.="<tr><td align=right valign=top>Password :</td>";
202:  $out.="<td align=left>";
203:  $out.="<input type=text size=30 name=testparam3 value=\"\">"; // dont display it
204:  $out.="<input type=hidden name=keepparam3 value=1>"; // don't update testparam3 (if blank)
205:  $out.="<br><i>Enter a new password to set or... ";
206:  $out.="<input type=checkbox name=clearparam3 value=1> "; // clears testparam3 if set
207:  $out.="clear it</i>";
208:  $out.="</td></tr>";
209:  echo $out; // output the buffer
210:  }
211: }
212: 
213: // Now we have defined the class we must register it with FreeNATS
214: 
215: $params=array(); // blank parameters array as we have implemented DisplayForm above
216: 
217: $NATS->Tests->Register(
218:  "advpagecheck",           // the internal simple test name (must not conflict with anything else)
219:  "Advanced_Pagecheck_Test",      // the class name (above)
220:  $params,               // parameters (blank for now)
221:  "Web Content Test", // the display name of the test in the interface
222:  3,                     // the revision number of the test
223:  "Advanced Page Checker");   // extended description for the test module used in overview
224: 
225: ?>
226: