File: 1.00.6a/server/web/api.php (View as Code)

1: 2: /* ------------------------------------------------------------- 3: This file is part of FreeNATS 4: 5: FreeNATS is (C) Copyright 2008 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: ob_start(); 24: require("include.php"); 25: $NATS->Start(); 26: $session=true; 27: if (!$NATS_Session->Check($NATS->DB)) 28: { 29: $session=false; 30: } 31: 32: $abs=GetAbsolute(); 33: 34: if (isset($_REQUEST['mode'])) $mode=$_REQUEST['mode']; 35: else $mode="xml"; 36: 37: // api.public - is available without session auth 38: // api.key - usage key used if public and no session (if set) 39: 40: if ($NATS->Cfg->Get("api.public",0)!=1) // NOT public 41: { 42: if (!$session) 43: { 44: echo "Error: Public API Access Disabled"; 45: exit(); 46: } 47: } 48: else // IS PUBLIC 49: { 50: $key=$NATS->Cfg->Get("api.key",""); 51: if ($key!="") // require a key 52: { 53: if ( (!isset($_REQUEST['apikey'])) || ($_REQUEST['apikey'] != $key) ) 54: { 55: // No key or doesn't match 56: echo "Error: Public API Key Mismatch"; 57: exit(); 58: } 59: } 60: } 61: 62: // Got this far so it must be a winner (either public or no key or correct key) 63: 64: 65: 66: function lo($text) // line out 67: { 68: echo $text."\n"; 69: } 70: 71: // Header 72: 73: switch($mode) 74: { 75: case "xml": 76: header("Content-type: text/xml"); 77: lo(""); 78: lo(""); 79: break; 80: 81: case "js": 82: if (isset($_REQUEST['dataid'])) $dataid=$_REQUEST['dataid']; 83: else 84: { 85: $allow="abcdef0123456789"; 86: $allow_len=strlen($allow); 87: mt_srand(microtime()*1000000); 88: $id_len=10; 89: $dataid="fnd_"; 90: for ($a=0; $a<$id_len; $a++) 91: { 92: $dataid.=$allow[mt_rand(0,$allow_len-1)]; 93: } 94: } 95: lo("var ".$dataid."=new Array();"); 96: break; 97: 98: 99: } 100: ob_end_flush(); 101: 102: // Queries 103: $query_count=count($_REQUEST['query']); 104: for ($a=0; $a<$query_count; $a++) 105: { 106: switch($_REQUEST['query'][$a]) 107: { 108: case "node": 109: $nodedata=$NATS->GetNode($_REQUEST['param'][$a]); 110: if ($nodedata) // got a valid response 111: { 112: if ($mode=="js") 113: { 114: lo($dataid."[".$a."]=new Array();"); 115: } 116: else if ($mode=="xml") lo(""); 117: $ctr=0; 118: foreach($nodedata as $key => $val) 119: { 120: if (!is_numeric($key)) // pesky double-arrays avoided 121: { 122: if ($mode=="xml") lo(" <".$key.">".$val.""); 123: else if ($mode=="js") 124: { 125: lo($dataid."[".$a."][".$ctr."]=new Array;"); 126: lo($dataid."[".$a."][".$ctr."][0]='".$key."';"); 127: lo($dataid."[".$a."][".$ctr."][1]='".$val."';"); 128: } 129: $ctr++; 130: } 131: } 132: if ($mode=="xml") lo(""); 133: } 134: break; 135: 136: case "group": 137: $groupdata=$NATS->GetGroup($_REQUEST['param'][$a]); 138: if ($groupdata) // got a valid response 139: { 140: if ($mode=="js") 141: { 142: lo($dataid."[".$a."]=new Array();"); 143: } 144: else if ($mode=="xml") lo(""); 145: $ctr=0; 146: foreach($groupdata as $key => $val) 147: { 148: if (!is_numeric($key)) // pesky double-arrays avoided 149: { 150: if ($mode=="xml") lo(" <".$key.">".$val.""); 151: else if ($mode=="js") 152: { 153: lo($dataid."[".$a."][".$ctr."]=new Array;"); 154: lo($dataid."[".$a."][".$ctr."][0]='".$key."';"); 155: lo($dataid."[".$a."][".$ctr."][1]='".$val."';"); 156: } 157: $ctr++; 158: } 159: } 160: if ($mode=="xml") lo(""); 161: } 162: break; 163: 164: 165: case "test": 166: $testdata=$NATS->GetTest($_REQUEST['param'][$a],true); 167: if ($testdata) // got a valid response 168: { 169: 170: if ( (isset($_REQUEST['param1'][$a])) && (isset($_REQUEST['param2'][$a])) ) 171: { // get data 172: $testdata['period.startx']=0; 173: $testdata['period.finishx']=0; 174: $testdata['period.tested']=0; 175: $testdata['period.passed']=0; 176: $testdata['period.warning']=0; 177: $testdata['period.failed']=0; 178: $testdata['period.untested']=0; 179: $testdata['period.average']=0; 180: 181: if (($testdata['testrecord']==1)||($testdata['testtype']=="ICMP")) 182: { 183: $sx=smartx($_REQUEST['param1'][$a]); 184: $fx=smartx($_REQUEST['param2'][$a]); 185: $testdata['period.startx']=$sx; 186: $testdata['period.finishx']=$fx; 187: 188: $q="SELECT alertlevel,COUNT(recordid) AS counter FROM fnrecord WHERE testid=\"".ss($testdata['testid'])."\" AND "; 189: $q.="recordx>=".ss($sx)." AND recordx<=".ss($fx)." GROUP BY alertlevel"; 190: //echo $q; 191: $r=$NATS->DB->Query($q); 192: while ($row=$NATS->DB->Fetch_Array($r)) 193: { 194: switch ($row['alertlevel']) 195: { 196: case -1: $testdata['period.untested']+=$row['counter']; 197: break; 198: case 0: $testdata['period.passed']+=$row['counter']; 199: break; 200: case 1: $testdata['period.warning']+=$row['counter']; 201: break; 202: case 2: $testdata['period.failed']+=$row['counter']; 203: break; 204: } 205: $testdata['period.tested']+=$row['counter']; 206: } 207: $NATS->DB->Free($r); 208: 209: $q="SELECT AVG(testvalue) FROM fnrecord WHERE testid=\"".ss($testdata['testid'])."\" AND "; 210: $q.="recordx>=".ss($sx)." AND recordx<=".ss($fx); //." AND alertlevel IN (0,1)"; // warnings and passes only 211: 212: $r=$NATS->DB->Query($q); 213: 214: if ($row=$NATS->DB->Fetch_Array($r)) 215: { 216: $testdata['period.average']=round($row['AVG(testvalue)'],4); 217: } 218: 219: $NATS->DB->Free($r); 220: 221: } 222: } 223: 224: 225: // header 226: 227: if ($mode=="js") 228: { 229: lo($dataid."[".$a."]=new Array();"); 230: lo($dataid."[".$a."][0]=new Array();"); // Keys 231: lo($dataid."[".$a."][1]=new Array();"); // Values 232: } 233: else if ($mode=="xml") lo(""); 234: $ctr=0; 235: foreach($testdata as $key => $val) 236: { 237: if (!is_numeric($key)) // pesky double-arrays avoided 238: { 239: if ($mode=="xml") lo(" <".$key.">".$val.""); 240: else if ($mode=="js") 241: { 242: lo($dataid."[".$a."][0][".$ctr."]='".$key."';"); 243: lo($dataid."[".$a."][1][".$ctr."]='".$val."';"); 244: } 245: $ctr++; 246: } 247: } 248: if ($mode=="xml") lo(""); 249: } 250: break; 251: 252: case "alerts": 253: $alerts=$NATS->GetAlerts(); 254: 255: $count=count($alerts); 256: if ($alerts===false) $count=0; // as showing a 1 in count otherwise 257: if ($mode=="xml") lo(""); 258: else if ($mode=="js") lo($dataid."[".$a."]=new Array();"); 259: 260: if ($alerts) // some were returned 261: { 262: // nodeid alertlevel 263: for ($alctr=0; $alctr<$count; $alctr++) 264: { 265: if ($mode=="xml") lo(" ".$alerts[$alctr]['nodeid'].""); 266: else if ($mode=="js") lo($dataid."[".$a."][".$alctr."]='".$alerts[$alctr]['nodeid']."';"); 267: } 268: } 269: 270: if ($mode=="xml") lo(""); 271: 272: break; 273: 274: 275: case "testdata": 276: // param = testid 277: // param1 = startx 278: // param2 = finishx 279: 280: $q="SELECT recordx,testvalue,alertlevel FROM fnrecord WHERE testid=\"".ss($_REQUEST['param'][$a])."\" AND "; 281: $sx=smartx($_REQUEST['param1'][$a]); 282: $fx=smartx($_REQUEST['param2'][$a]); 283: $q.="recordx>=".ss($sx)." AND recordx<=".ss($fx)." ORDER BY recordx ASC"; 284: 285: $r=$NATS->DB->Query($q); 286: $count=$NATS->DB->Num_Rows($r); 287: 288: if ($mode=="xml") lo(""); 289: else if ($mode=="js") 290: { 291: lo($dataid."[".$a."]=new Array();"); 292: } 293: 294: $ctr=0; 295: while ($row=$NATS->DB->Fetch_Array($r)) 296: { 297: if ($mode=="xml") 298: { 299: lo(" ".$row['testvalue'].""); 300: } 301: else 302: { 303: lo($dataid."[".$a."][".$ctr."]=new Array();"); 304: lo($dataid."[".$a."][".$ctr."][0]=".$row['recordx'].";"); 305: lo($dataid."[".$a."][".$ctr."][1]=".$row['testvalue'].";"); 306: lo($dataid."[".$a."][".$ctr."][2]=".$row['alertlevel'].";"); 307: } 308: $ctr++; 309: } 310: $NATS->DB->Free($r); 311: 312: if ($mode=="xml") lo(""); 313: 314: break; 315: 316: } 317: } 318: 319: // Footer and Finish 320: 321: if ($mode=="xml") lo(""); 322: else if ($mode=="js") 323: { 324: if(isset($_REQUEST['callback'])) 325: { 326: lo($_REQUEST['callback']."(".$dataid.");"); 327: } 328: } 329: 330: ?>