File: 0.02.19a/server/base/freenats.inc.php (View as HTML)

  1: <?php // freenats.inc.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 Foobar.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freenats
 21: -------------------------------------------------------------- */
 22: 
 23: class TFreeNATS
 24: {
 25: var $init=false;
 26: var $DB;
 27: var $Cfg;
 28: var $Version="0.02.19";
 29: var $Release="a";
 30: 	
 31: function Start()
 32: 	{
 33: 	$this->DB=new TNATS_DB();
 34: 	$this->Cfg=new TNATS_Cfg();
 35: 	$this->DB->Connect();
 36: 	$this->Cfg->Load($this->DB);
 37: 	$this->init=true;
 38: 	}
 39: 	
 40: function Stop()
 41: 	{
 42: 	$this->DB->Disconnect();
 43: 	$this->init=false;
 44: 	}	
 45: 	
 46: function Event($logevent,$loglevel=1,$modid="NONE",$catid="NONE")
 47: 	{
 48: 	global $NATS_Session;
 49: 	if ((isset($NATS_Session))&&($NATS_Session->auth)) $username=$NATS_Session->username;
 50: 	else $username="";
 51: 	$l=$this->Cfg->Get("log.level");
 52: 	//echo "** $l **\n";
 53: 	if ( $l=="" ) $l=10; // debug logging if no variable
 54: 	if ( $l < $loglevel ) return false;
 55: 	if (strlen($logevent)>249) $logevent=substr($logevent,0,245)."...";
 56: 	$q="INSERT INTO fnlog(postedx,modid,catid,loglevel,logevent,username) VALUES(".time().",";
 57: 	$q.="\"".ss($modid)."\",\"".ss($catid)."\",".ss($loglevel).",\"".ss($logevent)."\",\"".ss($username)."\")";
 58: 	//echo $q;
 59: 	$this->DB->Query($q);
 60: 	}
 61: 
 62: function AlertAction($nodeid,$alertlevel,$change,$alerttext)
 63: 	{
 64: 	echo "Called for node: ".$nodeid."\n";
 65: 	if ($change==0) return false;
 66: 	//echo $nodeid.":".$alertlevel.":".$change.":".$alerttext."\n";
 67: 	// get all the alertactions for this node id
 68: 	$q="SELECT aaid FROM fnnalink WHERE nodeid=\"".ss($nodeid)."\"";
 69: 	$r=$this->DB->Query($q);
 70: 	while ($arow=$this->DB->Fetch_Array($r))
 71: 		{
 72: 		// get details for this alert action
 73: 		$aq="SELECT * FROM fnalertaction WHERE aaid=".$arow['aaid']." LIMIT 0,1";
 74: 		//echo $aq."\n";
 75: 		$ar=$this->DB->Query($aq);
 76: 		$aa=$this->DB->Fetch_Array($ar);
 77: 		$this->DB->Free($ar);
 78: 		//echo $aa['atype']."FISH\n";
 79: 		
 80: 		// UGGGGGGGG continue!!
 81: 		if ( ($aa['atype']=="") || ($aa['atype']=="Disabled") ) continue;
 82: 		if ( ($aa['awarnings']==0) && ($alertlevel<2) ) continue;
 83: 		if ( ($aa['adecrease']==0) && ($change<1) ) continue;
 84: 		
 85: 		// made it this far
 86: 		$ndata=$aa['mdata']."\n".$nodeid.": ".$alerttext;
 87: 		$uq="UPDATE fnalertaction SET mdata=\"".ss($ndata)."\" WHERE aaid=".$arow['aaid'];
 88: 		//echo $uq."\n";
 89: 		$this->DB->Query($uq);
 90: 		}
 91: 	}
 92: 	
 93: function ActionFlush()
 94: 	{
 95: 	global $allowed; // allowed chars from screen in YA BODGE
 96: 	$q="SELECT * FROM fnalertaction WHERE mdata!=\"\"";
 97: 	$r=$this->DB->Query($q);
 98: 	while ($row=$this->DB->Fetch_Array($r))
 99: 		{
100: 		if ($row['atype']=="email")
101: 			{
102: 			if ($row['esubject']==0) $sub="";
103: 			else if ($row['esubject']==1) $sub="FreeNATS Alert";
104: 			else $sub="** FreeNATS Alert **";
105: 			$body="";
106: 			if ($row['etype']==0) $body=$row['mdata'];
107: 			else $body="FreeNATS Alert,\r\n".$row['mdata']."\r\n--FreeNATS @ ".nicedt(time());
108: 			//$tolist=preg_split("[\n\r]",$row['etolist']);
109: 			$tolist=array();
110: 			$f=0;
111: 			$tolist[0]="";
112: 			for ($a=0; $a<strlen($row['etolist']); $a++)
113: 				{
114: 				$chr=$row['etolist'][$a];
115: 				//echo $chr;
116: 				if (strpos($allowed,$chr)===false) // special char
117: 					{
118: 					$f++;
119: 					$tolist[$f]="";
120: 					}
121: 				else
122: 					{
123: 					$tolist[$f].=$chr;
124: 					}
125: 				}
126: 			
127: 			foreach($tolist as $toaddr)
128: 				{
129: 				$toaddr=nices($toaddr);
130: 				if ($toaddr!="")
131: 					{
132: 					//echo $row['efrom'].":".$toaddr.":".$sub.":".$body."\n\n";
133: 					//$body="fish";
134: 					//mail($toaddr,$sub,$body,"From: ".$row['efrom']."\r\n","-f".$row['efrom']);
135: 					$header="From: ".$row['efrom']."\r\n";
136: 					//$header="";
137: 					//$exhead="-f".$row['efrom'];
138: 					//db("Sending Email: ".$toaddr);
139: 					mail($toaddr,$sub,$body,$header);
140: 					$this->Event("Sent alert email to ".$toaddr,4,"Flush","Email");
141: 					}
142: 				}
143: 			
144: 			// clear mdata as email has been sent
145: 			$q="UPDATE fnalertaction SET mdata=\"\" WHERE aaid=".$row['aaid'];
146: 			$this->DB->Query($q);
147: 				
148: 			}
149: 		}
150: 	}	
151: 
152: function GetAlerts()
153: 	{
154: 	$q="SELECT nodeid,alertlevel FROM fnalert WHERE closedx=0";
155: 	$r=$this->DB->Query($q);
156: 	$c=0;
157: 	$al=array();
158: 	while ($row=$this->DB->Fetch_Array($r))
159: 		{
160: 		$al[$c]['nodeid']=$row['nodeid'];
161: 		$al[$c]['alertlevel']=$row['alertlevel'];
162: 		$c++;
163: 		}
164: 	if ($c>0) return $al;
165: 	else return false;
166: 	}
167: 	
168: function SetAlerts($nodeid,$alertlevel,$alerts="")
169: 	{
170: 	// get current alert level
171: 	$q="SELECT alertlevel FROM fnnode WHERE nodeid=\"".ss($nodeid)."\"";
172: 	$r=$this->DB->Query($q);
173: 	$row=$this->DB->Fetch_Array($r);
174: 	$this->DB->Free($r);
175: 	$cal=$row['alertlevel'];
176: 	
177: 	if ($alertlevel>$cal)
178: 		{
179: 		// trigger alert process
180: 		}
181: 		
182: 	if ($alertlevel!=$cal)
183: 		{
184: 		// update table
185: 		$q="UPDATE fnnode SET alertlevel=".ss($alertlevel)." WHERE nodeid=\"".ss($nodeid)."\"";
186: 		$this->DB->Query($q);
187: 		}
188: 		
189: 		
190: 	// ALERTS
191: 	// is there an existing alert for this node
192: 	$q="SELECT alertid,alertlevel FROM fnalert WHERE nodeid=\"".ss($nodeid)."\" AND closedx=0";
193: 	$r=$this->DB->Query($q);
194: 	if ($row=$this->DB->Fetch_Array($r))
195: 		{ // yes there is
196: 		// if new alert level is 0 let's close it
197: 		if ($alertlevel==0)
198: 			{
199: 			$q="UPDATE fnalert SET closedx=".time()." WHERE alertid=".$row['alertid'];
200: 			$this->DB->Query($q);
201: 			if (is_array($alerts)) $alerts[]="Alert Closed";
202: 			else
203: 				{
204: 				$alerts=array();
205: 				$alerts[]="Alert Closed";
206: 				}
207: 			}
208: 		$alertid=$row['alertid'];
209: 		// otherwise update the alert to the new value regardless
210: 		$q="UPDATE fnalert SET alertlevel=".ss($alertlevel)." WHERE alertid=".$alertid;
211: 		$this->DB->Query($q);
212: 		}
213: 	else
214: 		{ // no there's not
215: 		if ($alertlevel>0) // only if an actual alert
216: 			{
217: 			$q="INSERT INTO fnalert(nodeid,alertlevel,openedx) VALUES(";
218: 			$q.="\"".ss($nodeid)."\",".ss($alertlevel).",".time().")";
219: 			$this->DB->Query($q);
220: 			$alertid=$this->DB->Insert_Id();
221: 			}
222: 		}
223: 	// ALERT LOG with $alertid
224: 	$t=time();
225: 	$at="";
226: 	if (is_array($alerts))
227: 		{
228: 		foreach($alerts as $alert)
229: 			{
230: 			if ($at!="") $at.=", ";
231: 			$at.=$alert;
232: 			//echo $at."\n";
233: 			$iq="INSERT INTO fnalertlog(alertid,postedx,logentry) VALUES(";
234: 			$iq.=$alertid.",".$t.",\"".ss($alert)."\")";
235: 			//echo $iq;
236: 			$this->DB->Query($iq);
237: 			}
238: 		}
239: 		
240: 	$this->AlertAction($nodeid,$alertlevel,$alertlevel-$cal,$at);
241: 	
242: 		
243: 		
244: 	}
245: 
246: function NodeAlertLevel($nodeid)
247: 	{
248: 	$q="SELECT alertlevel FROM fnnode WHERE nodeid=\"".ss($nodeid)."\"";
249: 	$r=$this->DB->Query($q);
250: 	if ($row=$this->DB->Fetch_Array($r)) return $row['alertlevel'];
251: 	else return -1;
252: 	}	
253: 
254: function GroupAlertLevel($groupid)
255: 	{
256: 	$lvl=-1;
257: 	$q="SELECT nodeid FROM fngrouplink WHERE groupid=\"".ss($groupid)."\"";
258: 	$r=$this->DB->Query($q);
259: 	while ($row=$this->DB->Fetch_Array($r))
260: 		{
261: 		$nl=$this->NodeAlertLevel($row['nodeid']);
262: 		if ($nl>$lvl) $lvl=$nl;
263: 		}
264: 	$this->DB->Free($r);
265: 	return $lvl;
266: 	}
267: 	
268: }
269: ?>