File: 0.02.58a/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['nodeid']=$nodeid;
 80: $ret['status']=$row['alertlevel'];
 81: $ret['link']="node.php?nodeid=".$nodeid;
 82: $NATS->DB->Free($r);
 83: 
 84: $ret['detail']=array();
 85: 
 86: if ($detail)
 87: {
 88: // get detail
 89: $q="SELECT testtype,testparam,testname,alertlevel FROM fnlocaltest WHERE nodeid=\"".ss($nodeid)."\" ORDER BY alertlevel DESC";
 90: $r=$NATS->DB->Query($q);
 91: $a=0;
 92: while ($row=$NATS->DB->Fetch_Array($r))
 93: 	{
 94: 	$an=$row['testtype'];
 95: 	if ($row['testparam']!="") $an.=" (".$row['testparam'].")";
 96: 	if ($row['testname']=="") $nn=$an; // textify!
 97: 	else $nn=$row['testname'];
 98: 	$ret['detail'][$a]['item']=$nn;
 99: 	$ret['detail'][$a]['status']=$row['alertlevel'];
100: 	$ret['detail'][$a]['link']=$ret['link'];
101: 	$a++;
102: 	}
103: $NATS->DB->Free($r);
104: }
105: return $ret;
106: }
107: 	
108: $q="SELECT * FROM fnviewitem WHERE viewid=".ss($_REQUEST['viewid'])." ORDER BY iweight ASC";
109: $r=$NATS->DB->Query($q);
110: while ($row=$NATS->DB->Fetch_Array($r))
111: 	{
112: 	$id=$row['viewitemid'];
113: 	$items[$id]=$row;
114: 	// get name (varying), status and detail dependent...
115: 	switch ($row['itype'])
116: 		{
117: 		case "node": 
118: 			$items[$id]['data']=ViewNode($row['ioption']);
119: 			break;
120: 			
121: 		case "allnodes": case "alertnodes":
122: 			$items[$id]['detail']=array();
123: 			if ($row['itype']=="allnodes") $q="SELECT nodeid FROM fnnode WHERE nodeenabled=1 ORDER BY `weight` ASC";
124: 			else if ($row['itype']=="alertnodes") $q="SELECT nodeid FROM fnnode WHERE nodeenabled=1 AND alertlevel>0 ORDER BY `weight` ASC";
125: 			$ret=$NATS->DB->Query($q);
126: 			//echo "!".$q."<br>";
127: 			$a=0;
128: 			while ($noderow=$NATS->DB->Fetch_Array($ret))
129: 				{
130: 				if ($row['idetail']==1) $det=true;
131: 				else $det=false;
132: 				$items[$id]['detail'][$a]['data']=ViewNode($noderow['nodeid'],$det);
133: 				$items[$id]['detail'][$a]['icolour']=$row['icolour'];
134: 				$items[$id]['detail'][$a]['idetail']=$row['idetail'];
135: 				$items[$id]['detail'][$a]['igraphic']=$row['igraphic'];
136: 				$items[$id]['detail'][$a++]['itextstatus']=$row['itextstatus'];
137: 				}
138: 			break;
139: 		}
140: 		
141: 	}
142: 
143: // begin the buffering of output...
144: 
145: // title and header
146: if ($view['vstyle']=="plain")
147: 	{
148: 	$ob[]="<html><head>";
149: 	$ob[]="<style type=\"text/css\">";
150: 	$fp=@fopen("css/mini.css","r");
151: 	if ($fp)
152: 		{
153: 		while(!@feof($fp))
154: 			$ob[]=@fgets($fp,1024);
155: 		}
156: 	@fclose($fp);
157: 	$ob[]="</style>";
158: 	$ob[]="</head><body>";
159: 	}
160: else if ($view['vstyle']=="mobile")
161: 	{
162: 	$ob[]="<html><head>";
163: 	if ($view['vrefresh']>0) $ob[]="<meta http-equiv=\"refresh\" content=\"".$view['vrefresh']."\">";
164: 	$ob[]="<style type=\"text/css\">";
165: 	$fp=@fopen("css/mobile.css","r");
166: 	if ($fp)
167: 		{
168: 		while(!@feof($fp))
169: 			$ob[]=@fgets($fp,1024);
170: 		}
171: 	@fclose($fp);
172: 	$ob[]="</style>";
173: 	$ob[]="</head><body>";
174: 	}
175: else // standard and catch-all
176: 	{
177: 	$ob[]="<html><head>";
178: 	if ($view['vrefresh']>0) $ob[]="<meta http-equiv=\"refresh\" content=\"".$view['vrefresh']."\">";
179: 	$ob[]="<title>FreeNATS: ".$view['vtitle']."</title>";
180: 	$ob[]="<style type=\"text/css\">";
181: 	$fp=@fopen("css/main.css","r");
182: 	if ($fp>0)
183: 		{
184: 		while(!@feof($fp))
185: 			$ob[]=@fgets($fp,1024);
186: 		}
187: 	@fclose($fp);
188: 	$ob[]="</style>";
189: 	$ob[]="</head><body>";
190: 	$ob[]="<table width=100% class=\"maintitle\">";
191: 	$ob[]="<tr><td align=left valign=center>";
192: 	$ob[]="<b class=\"maintitle\">FreeNATS: ".$view['vtitle']."</b></td>";
193: 	$ob[]="</tr></table>";
194: 	$ob[]="<br>";
195: 	}
196: 
197: // now the items
198: 
199: function small_node($item)
200: {
201: global $abs,$view;
202: $ob=array(); // our local copy
203: $uri=$abs.$item['data']['link'];
204: if ($view['vlinkv']!=0)
205: 	{
206: 	$uri=$abs."view.php?viewid=".$view['vlinkv'];
207: 	}
208: $link=$view['vclick'];
209: if ($link=="disabled") $l="<a href=#>";
210: else if ($link=="standard") $l="<a href=\"".$uri."\">";
211: else if ($link=="frametop") $l="<a href=\"".$uri."\" target=_top>";
212: else if ($link=="newwindow") $l="<a href=\"".$uri."\" target=top>";
213: else $l="<a href=#>";
214: 			
215: // alert lights only as no fancy full-on tables etc yet
216: if ($item['igraphic']>0) // actually therefore should only be ==1 with 2 being the "proper" one
217: 	{
218: 	$is="<img src=\"".$abs."images/lights/a".$item['data']['status'].".png\">&nbsp;";
219: 	$ob[]=$is;
220: 	}
221: 			
222: $ob[]=$l;
223: 			
224: if ($item['icolour']==1) $ob[]="<b class=\"al".$item['data']['status']."\">";
225: 			
226: $out=$item['data']['name'];
227: if ($item['itextstatus']==1) $out.=": ".oText($item['data']['status']);
228: $ob[]=$out."</a>";
229: 	
230: if ($item['icolour']==1) $ob[]="</b>";
231: 
232: // detail like tests etc...
233: if ($item['idetail']>0)
234: 	{
235: 	$a=0;
236: 	foreach($item['data']['detail'] as $dline)
237: 		{
238: 		$a++;
239: 		$ob[]="<br>&nbsp;-&nbsp;";
240: 		if ($item['icolour']==1) $ob[]="<b class=\"al".$dline['status']."\">";
241: 		$out=$dline['item'];
242: 		if ($item['itextstatus']==1) $out.=": ".oText($dline['status']);
243: 		$ob[]=$out;
244: 		if ($item['icolour']==1) $ob[]="</b>";
245: 		}
246: 	//if ($a>0) $ob[]="<br>";
247: 	}
248: 
249: return $ob;
250: }
251: 
252: function large_node($item)
253: {
254: global $abs,$view;
255: $ob=array(); // our local copy
256: $uri=$abs.$item['data']['link'];
257: if ($view['vlinkv']!=0)
258: 	{
259: 	$uri=$abs."view.php?viewid=".$view['vlinkv'];
260: 	}
261: $link=$view['vclick'];
262: if ($link=="disabled") $l="<a href=#>";
263: else if ($link=="standard") $l="<a href=\"".$uri."\">";
264: else if ($link=="frametop") $l="<a href=\"".$uri."\" target=_top>";
265: else if ($link=="newwindow") $l="<a href=\"".$uri."\" target=top>";
266: else $l="<a href=#>";
267: 
268: if ($item['icolour']==1) $col=true;
269: else $col=false;
270: 
271: if ($col)
272: 	{
273: 	switch($item['data']['status'])
274: 		{
275: 		case -1: $c="#a0a0a0"; break;
276: 		case 0: $c="green"; break;
277: 		case 1: $c="orange"; break;
278: 		case 2: $c="red"; break;
279: 		default: $c="black"; break;
280: 		}
281: 	}
282: else $c="#a0a0a0";			
283: $ss="width: 250; border: dotted 1px ".$c.";";
284: 
285: $ob[]="<table style=\"".$ss."\">";
286: 
287: if ($item['igraphic']==1)
288: 	{
289: 	$is="<img src=\"".$abs."images/lights/a".$item['data']['status'].".png\">&nbsp;";
290: 	}			
291: else if ($item['igraphic']>0)
292: 	{
293: 	$is="<img src=\"".$abs."icons/".NodeIcon($item['data']['nodeid'])."\">&nbsp;";
294: 	}
295: else $is="&nbsp;";
296: 
297: $ob[]="<tr><td align=left valign=center>";
298: $ob[]=$l;
299: if ($item['icolour']==1) $ob[]="<b class=\"al".$item['data']['status']."\">";
300: 			
301: $out=$item['data']['name'];
302: if ($item['itextstatus']==1) $out.=": ".oText($item['data']['status']);
303: $ob[]=$out."</a>";
304: 	
305: if ($item['icolour']==1) $ob[]="</b>";
306: $ob[]="</td><td align=right valign=center>";
307: $ob[]=$is;
308: $ob[]="</td></tr>";
309: // detail like tests etc...
310: if ($item['idetail']>0)
311: 	{
312: 	$ob[]="<tr><td colspan=2 align=left valign=top>";
313: 	$a=0;
314: 	foreach($item['data']['detail'] as $dline)
315: 		{
316: 		$a++;
317: 		$ob[]="&nbsp;-&nbsp;";
318: 		if ($item['icolour']==1) $ob[]="<b class=\"al".$dline['status']."\">";
319: 		$out=$dline['item'];
320: 		if ($item['itextstatus']==1) $out.=": ".oText($dline['status']);
321: 		$ob[]=$out;
322: 		if ($item['icolour']==1) $ob[]="</b>";
323: 		$ob[]="<br>";
324: 		}
325: 	$ob[]="</td></tr>";
326: 	//if ($a>0) $ob[]="<br>";
327: 	}
328: 
329: 	
330: $ob[]="</table>";
331: return $ob;
332: }
333: 
334: if ($view['vcolumns']>1)
335: 	{
336: 	$usecols=true;
337: 	$colcount=$view['vcolumns'];
338: 	}
339: else $usecols=false;
340: 
341: foreach($items as $item)
342: 	{
343: 	switch ($item['itype'])
344: 		{
345: 		case "node":
346: 			$output=small_node($item);
347: 			foreach($output as $line) $ob[]=$line;
348: 			
349: 			break;
350: 			
351: 		case "allnodes": case "alertnodes":
352: 			$c=0;
353: 			if ($usecols) $ob[]="<table border=0>";
354: 			foreach($item['detail'] as $node)
355: 				{
356: 				if ($usecols)
357: 					{
358: 					if ($c==0) $ob[]="<tr>";
359: 					$ob[]="<td align=left valign=top>";
360: 					}
361: 				if ($item['isize']==1) $output=large_node($node);
362: 				else $output=small_node($node);
363: 				foreach($output as $line) $ob[]=$line;
364: 				
365: 				if ($usecols)
366: 					{
367: 					$ob[]="</td>";
368: 					$c++;
369: 					if ($c>=$colcount)
370: 						{
371: 						$ob[]="</tr>";
372: 						$ob[]="<tr><td colspan=".$colcount.">&nbsp;</td></tr>";
373: 						$c=0;
374: 						}
375: 					}
376: 				else $ob[]="<br>";
377: 				}
378: 			if (($usecols) && ($c<3)) $ob[]="</tr>";
379: 			if ($usecols) echo "</table>";
380: 			break;
381: 			
382: 		}
383: 		$ob[]="<br>";
384: 		
385: 	}
386: 	
387: // footer
388: if ($view['vstyle']=="mobile")
389: 	{
390: 	$ob[]="</html>";
391: 	}
392: else if ($view['vstyle']=="plain")
393: 	{
394: 	$ob[]="</html>";
395: 	}
396: else // standard and catch-all
397: 	{
398: 	$ob[]="<table class=\"nfooter\" width=100%>";
399: 	$ob[]="<tr><td align=left>Powered by <a href=http://www.purplepixie.org/freenats/>FreeNATS</a>";
400: 	$ob[]="</td><td align=right>";
401: 	$ob[]="<a href=".$abs.">Login to System</a>";
402: 	$ob[]="</td></tr>";
403: 	$ob[]="</table>";
404: 	}
405: 	
406: 	
407: 	
408: // finally the output
409: switch ($mode)
410: 	{
411: 	case "debug":	
412: 	echo "<pre>";
413: 	var_dump($items);
414: 	echo "<br><br>";
415: 	foreach($ob as $l)
416: 		echo htmlspecialchars($l)."\n";
417: 	break;
418: 	
419: 	case "js":
420: 	//echo "<script type=\"text/javascript\">\n";
421: 	//echo "document.write(\"hello world\");\n";
422: 	foreach($ob as $l)
423: 		{
424: 		//if ($l[strlen($l)-1]=='\n') $l=substr($l,0,strlen($l)-1);
425: 		$l=trim($l);
426: 		echo "document.write(\"".addslashes($l)."\");\n";
427: 		}
428: 	//echo "</script>\n";
429: 	echo "\n";
430: 	break;
431: 	
432: 	default:
433: 	foreach($ob as $l)
434: 		{
435: 		echo $l;
436: 		echo "\n";
437: 		}
438: 	}
439: 
440: ?>