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

1: 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.=""; 174: $out.=""; 175: $out.=""; 179: $out.=""; 180: $out.=""; 187: $out.=""; 188: $out.=""; 194: 195: $out.=""; 196: $out.=""; 200: 201: $out.=""; 202: $out.=""; 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:
URL :"; 176: $out.=""; 177: $out.="
Fully-qualified URL including http://";
178: $out.="
Strings :"; 181: $out.="
";
182: $out.="
";
183: $out.=""; 184: $out.="
String(s) to search for - all defined must
be found for the test to pass
";
185: $out.="Blank strings are ignored."; 186: $out.="
No Strings :"; 189: $out.="
";
190: $out.="
";
191: $out.=""; 192: $out.="
String(s) to NOT find - fails if any are present
Leave blank to not use this portion of the test
";
193: $out.="
Username :"; 197: $out.=""; 198: $out.="
Specify to use HTTP-AUTH on the URL";
199: $out.="
Password :"; 203: $out.=""; // dont display it 204: $out.=""; // don't update testparam3 (if blank) 205: $out.="
Enter a new password to set or... ";
206: $out.=" "; // clears testparam3 if set 207: $out.="clear it"; 208: $out.="