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

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