File: 0.04.17a/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: //lo($dataid."[".$a."][0]=new Array();"); // Keys 116: //lo($dataid."[".$a."][1]=new Array();"); // Values 117: } 118: else if ($mode=="xml") lo(""); 119: $ctr=0; 120: foreach($nodedata as $key => $val) 121: { 122: if (!is_numeric($key)) // pesky double-arrays avoided 123: { 124: if ($mode=="xml") lo(" <".$key.">".$val.""); 125: else if ($mode=="js") 126: { 127: lo($dataid."[".$a."][".$ctr."]=new Array;"); 128: lo($dataid."[".$a."][".$ctr."][0]='".$key."';"); 129: lo($dataid."[".$a."][".$ctr."][1]='".$val."';"); 130: } 131: $ctr++; 132: } 133: } 134: if ($mode=="xml") lo(""); 135: } 136: break; 137: 138: 139: case "test": 140: $testdata=$NATS->GetTest($_REQUEST['param'][$a]); 141: if ($testdata) // got a valid response 142: { 143: if ($mode=="js") 144: { 145: lo($dataid."[".$a."]=new Array();"); 146: lo($dataid."[".$a."][0]=new Array();"); // Keys 147: lo($dataid."[".$a."][1]=new Array();"); // Values 148: } 149: else if ($mode=="xml") lo(""); 150: $ctr=0; 151: foreach($testdata as $key => $val) 152: { 153: if (!is_numeric($key)) // pesky double-arrays avoided 154: { 155: if ($mode=="xml") lo(" <".$key.">".$val.""); 156: else if ($mode=="js") 157: { 158: lo($dataid."[".$a."][0][".$ctr."]='".$key."';"); 159: lo($dataid."[".$a."][1][".$ctr."]='".$val."';"); 160: } 161: $ctr++; 162: } 163: } 164: if ($mode=="xml") lo(""); 165: } 166: break; 167: 168: case "alerts": 169: $alerts=$NATS->GetAlerts(); 170: 171: $count=count($alerts); 172: if ($alerts===false) $count=0; // as showing a 1 in count otherwise 173: if ($mode=="xml") lo(""); 174: else if ($mode=="js") lo($dataid."[".$a."]=new Array();"); 175: 176: if ($alerts) // some were returned 177: { 178: // nodeid alertlevel 179: for ($alctr=0; $alctr<$count; $alctr++) 180: { 181: if ($mode=="xml") lo(" ".$alerts[$alctr]['nodeid'].""); 182: else if ($mode=="js") lo($dataid."[".$a."][".$alctr."]='".$alerts[$alctr]['nodeid']."';"); 183: } 184: } 185: 186: if ($mode=="xml") lo(""); 187: 188: break; 189: 190: } 191: } 192: 193: // Footer and Finish 194: 195: if ($mode=="xml") lo(""); 196: else if ($mode=="js") 197: { 198: if(isset($_REQUEST['callback'])) 199: { 200: lo($_REQUEST['callback']."(".$dataid.");"); 201: } 202: } 203: 204: ?>