File: 0.02.55a/server/web/view.php (View as HTML)

  1: <?php
  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="";
 36: 	
 37: $items=array(); // items
 38: $ob=array(); // output buffer
 39: 
 40: function show_output()
 41: {
 42: global $ob,$view,$mode;
 43: foreach($ob as $oline)
 44: 	{
 45: 	echo $oline."<br>";
 46: 	}
 47: ob_end_flush();
 48: exit();
 49: }
 50: 
 51: $q="SELECT * FROM fnview WHERE viewid=".ss($_REQUEST['viewid']);
 52: $r=$NATS->DB->Query($q);
 53: if ($NATS->DB->Num_Rows($r)<=0)
 54: 	{
 55: 	$ob[]="Invalid View ".$_REQUEST['viewid'];
 56: 	show_output();
 57: 	}
 58: $view=$NATS->DB->Fetch_Array($r);
 59: if ($view['vpublic']!=1) // requires auth
 60: 	{
 61: 	if (!$session)
 62: 		{
 63: 		$ob[]="Sorry not a public view";
 64: 		show_output();
 65: 		}
 66: 	}
 67: 
 68: function ViewNode($nodeid,$detail=true)
 69: {
 70: global $NATS;
 71: $ret=array();
 72: $q="SELECT * FROM fnnode WHERE nodeid=\"".ss($nodeid)."\"";
 73: $r=$NATS->DB->Query($q);
 74: if ($NATS->DB->Num_Rows($r)<=0) return $ret;
 75: $row=$NATS->DB->Fetch_Array($r);
 76: if ($row['nodename']!="") $ret['name']=$row['nodename'];
 77: else $ret['name']=$nodeid;
 78: $ret['item']=$ret['name'];
 79: $ret['status']=$row['alertlevel'];
 80: $ret['link']="node.php?nodeid=".$nodeid;
 81: $NATS->DB->Free($r);
 82: 
 83: $ret['detail']=array();
 84: 
 85: if ($detail)
 86: {
 87: // get detail
 88: $q="SELECT testtype,testparam,testname,alertlevel FROM fnlocaltest WHERE nodeid=\"".ss($nodeid)."\" ORDER BY alertlevel DESC";
 89: $r=$NATS->DB->Query($q);
 90: $a=0;
 91: while ($row=$NATS->DB->Fetch_Array($r))
 92: 	{
 93: 	$an=$row['testtype'];
 94: 	if ($row['testparam']!="") $an.=" (".$row['testparam'].")";
 95: 	if ($row['testname']=="") $nn=$an; // textify!
 96: 	else $nn=$row['testname'];
 97: 	$ret['detail'][$a]['item']=$nn;
 98: 	$ret['detail'][$a]['status']=$row['alertlevel'];
 99: 	$ret['detail'][$a]['link']=$ret['link'];
100: 	$a++;
101: 	}
102: $NATS->DB->Free($r);
103: }
104: return $ret;
105: }
106: 	
107: $q="SELECT * FROM fnviewitem WHERE viewid=".ss($_REQUEST['viewid'])." ORDER BY iweight ASC";
108: $r=$NATS->DB->Query($q);
109: while ($row=$NATS->DB->Fetch_Array($r))
110: 	{
111: 	$id=$row['viewitemid'];
112: 	$items[$id]=$row;
113: 	// get name (varying), status and detail dependent...
114: 	switch ($row['itype'])
115: 		{
116: 		case "node": 
117: 			$items[$id]['data']=ViewNode($row['ioption']);
118: 			break;
119: 			
120: 		case "allnodes": case "alertnodes":
121: 			$items[$id]['detail']=array();
122: 			if ($row['itype']=="allnodes") $q="SELECT nodeid FROM fnnode WHERE nodeenabled=1 ORDER BY `weight` ASC";
123: 			else if ($row['itype']=="alertnodes") $q="SELECT nodeid FROM fnnode WHERE nodeenabled=1 AND alertlevel>0 ORDER BY `weight` ASC";
124: 			$ret=$NATS->DB->Query($q);
125: 			//echo "!".$q."<br>";
126: 			$a=0;
127: 			while ($noderow=$NATS->DB->Fetch_Array($ret))
128: 				{
129: 				if ($row['idetail']==1) $det=true;
130: 				else $det=false;
131: 				$items[$id]['detail'][$a]['data']=ViewNode($noderow['nodeid'],$det);
132: 				$items[$id]['detail'][$a]['icolour']=$row['icolour'];
133: 				$items[$id]['detail'][$a]['idetail']=$row['idetail'];
134: 				$items[$id]['detail'][$a]['igraphic']=$row['igraphic'];
135: 				$items[$id]['detail'][$a++]['itextstatus']=$row['itextstatus'];
136: 				}
137: 			break;
138: 		}
139: 		
140: 	}
141: 
142: // begin the buffering of output...
143: 
144: // title and header
145: if ($view['vstyle']=="plain")
146: 	{
147: 	$ob[]="<html><head>";
148: 	$ob[]="<style type=\"text/css\">";
149: 	$fp=@fopen("css/mini.css","r");
150: 	if ($fp)
151: 		{
152: 		while(!@feof($fp))
153: 			$ob[]=@fgets($fp,1024);
154: 		}
155: 	@fclose($fp);
156: 	$ob[]="</style>";
157: 	$ob[]="</head><body>";
158: 	}
159: else if ($view['vstyle']=="mobile")
160: 	{
161: 	$ob[]="<html><head>";
162: 	if ($view['vrefresh']>0) $ob[]="<meta http-equiv=\"refresh\" content=\"".$view['vrefresh']."\">";
163: 	$ob[]="<style type=\"text/css\">";
164: 	$fp=@fopen("css/mobile.css","r");
165: 	if ($fp)
166: 		{
167: 		while(!@feof($fp))
168: 			$ob[]=@fgets($fp,1024);
169: 		}
170: 	@fclose($fp);
171: 	$ob[]="</style>";
172: 	$ob[]="</head><body>";
173: 	}
174: else // standard and catch-all
175: 	{
176: 	$ob[]="<html><head>";
177: 	if ($view['vrefresh']>0) $ob[]="<meta http-equiv=\"refresh\" content=\"".$view['vrefresh']."\">";
178: 	$ob[]="<title>FreeNATS: ".$view['vtitle']."</title>";
179: 	$ob[]="<style type=\"text/css\">";
180: 	$fp=@fopen("css/main.css","r");
181: 	if ($fp>0)
182: 		{
183: 		while(!@feof($fp))
184: 			$ob[]=@fgets($fp,1024);
185: 		}
186: 	@fclose($fp);
187: 	$ob[]="</style>";
188: 	$ob[]="</head><body>";
189: 	$ob[]="<table width=100% class=\"maintitle\">";
190: 	$ob[]="<tr><td align=left valign=center>";
191: 	$ob[]="<b class=\"maintitle\">FreeNATS: ".$view['vtitle']."</b></td>";
192: 	$ob[]="</tr></table>";
193: 	}
194: 
195: // now the items
196: 
197: function small_node($item)
198: {
199: global $abs,$view;
200: $ob=array(); // our local copy
201: $uri=$abs.$item['data']['link'];
202: $link=$view['vclick'];
203: if ($link=="disabled") $l="<a href=#>";
204: else if ($link=="standard") $l="<a href=\"".$uri."\">";
205: else if ($link=="frametop") $l="<a href=\"".$uri."\" target=_top>";
206: else if ($link=="newwindow") $l="<a href=\"".$uri."\" target=top>";
207: else $l="<a href=#>";
208: 			
209: // alert lights only as no fancy full-on tables etc yet
210: if ($item['igraphic']>0) // actually therefore should only be ==1 with 2 being the "proper" one
211: 	{
212: 	$is="<img src=\"".$abs."images/lights/a".$item['data']['status'].".png\">&nbsp;";
213: 	$ob[]=$is;
214: 	}
215: 			
216: $ob[]=$l;
217: 			
218: if ($item['icolour']==1) $ob[]="<b class=\"al".$item['data']['status']."\">";
219: 			
220: $out=$item['data']['name'];
221: if ($item['itextstatus']==1) $out.=": ".oText($item['data']['status']);
222: $ob[]=$out."</a>";
223: 	
224: if ($item['icolour']==1) $ob[]="</b>";
225: 
226: // detail like tests etc...
227: if ($item['idetail']>0)
228: 	{
229: 	$a=0;
230: 	foreach($item['data']['detail'] as $dline)
231: 		{
232: 		$a++;
233: 		$ob[]="<br>&nbsp;-&nbsp;";
234: 		if ($item['icolour']==1) $ob[]="<b class=\"al".$dline['status']."\">";
235: 		$out=$dline['item'];
236: 		if ($item['itextstatus']==1) $out.=": ".oText($dline['status']);
237: 		$ob[]=$out;
238: 		if ($item['icolour']==1) $ob[]="</b>";
239: 		}
240: 	//if ($a>0) $ob[]="<br>";
241: 	}
242: 
243: return $ob;
244: }
245: 
246: foreach($items as $item)
247: 	{
248: 	switch ($item['itype'])
249: 		{
250: 		case "node":
251: 			$output=small_node($item);
252: 			foreach($output as $line) $ob[]=$line;
253: 			
254: 			break;
255: 			
256: 		case "allnodes": case "alertnodes":
257: 			foreach($item['detail'] as $node)
258: 				{
259: 				$output=small_node($node);
260: 				foreach($output as $line) $ob[]=$line;
261: 				$ob[]="<br>";
262: 				}
263: 			break;
264: 			
265: 		}
266: 		$ob[]="<br>";
267: 		
268: 	}
269: 	
270: // footer
271: $ob[]="</html>";
272: 	
273: // finally the output
274: switch ($mode)
275: 	{
276: 	case "debug":	
277: 	echo "<pre>";
278: 	var_dump($items);
279: 	echo "<br><br>";
280: 	foreach($ob as $l)
281: 		echo htmlspecialchars($l)."\n";
282: 	break;
283: 	
284: 	case "js":
285: 	//echo "<script type=\"text/javascript\">\n";
286: 	//echo "document.write(\"hello world\");\n";
287: 	foreach($ob as $l)
288: 		{
289: 		//if ($l[strlen($l)-1]=='\n') $l=substr($l,0,strlen($l)-1);
290: 		$l=trim($l);
291: 		echo "document.write(\"".addslashes($l)."\");\n";
292: 		}
293: 	//echo "</script>\n";
294: 	echo "\n";
295: 	break;
296: 	
297: 	default:
298: 	foreach($ob as $l)
299: 		{
300: 		echo $l;
301: 		echo "\n";
302: 		}
303: 	}
304: 
305: ?>