File:
0.02.49a/server/base/freenats.inc.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: class TFreeNATS
24: {
25: var $init=false;
26: var $DB;
27: var $Cfg;
28: var $Version="0.02.49";
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:
101: $doalert=true;
102:
103: // clear mdata right at the start to get around duplicate emails whilst processing
104: $q="UPDATE fnalertaction SET mdata=\"\" WHERE aaid=".$row['aaid'];
105: $this->DB->Query($q);
106:
107: if ($this->DB->Affected_Rows()<=0) // already flushed or failed to flush
108: {
109: $doalert=false;
110: $this->Event("Alert Action Already Flushed - Skipping",8,"Flush","Action");
111: }
112:
113: // alert counter
114: $td=date("Ymd");
115: if ($td!=$row['ctrdate']) // new day or no flush record
116: {
117: $q="UPDATE fnalertaction SET ctrdate=\"".$td."\",ctrtoday=1 WHERE aaid=".$row['aaid'];
118: $this->DB->Query($q);
119: }
120: else
121: {
122:
123: if ( ($row['ctrlimit']==0) || ($row['ctrlimit']>$row['ctrtoday']) ) // no limit or below
124: {
125: $q="UPDATE fnalertaction SET ctrtoday=ctrtoday+1 WHERE aaid=".$row['aaid'];
126: $this->DB->Query($q);
127: }
128: else // at or over limit
129: {
130: $this->Event("Alert Action Limit Reached - Skipping",2,"Flush","Action");
131: $doalert=false;
132: }
133:
134: }
135:
136:
137: if ($row['atype']=="email")
138: {
139: if ($row['esubject']==0) $sub="";
140: else if ($row['esubject']==1) $sub="FreeNATS Alert";
141: else $sub="** FreeNATS Alert **";
142: $body="";
143: if ($row['etype']==0) $body=$row['mdata'];
144: else $body="FreeNATS Alert,\r\n".$row['mdata']."\r\n--FreeNATS @ ".nicedt(time());
145: //$tolist=preg_split("[\n\r]",$row['etolist']);
146: $tolist=array();
147: $f=0;
148: $tolist[0]="";
149: for ($a=0; $a150: {
151: $chr=$row['etolist'][$a];
152: //echo $chr;
153: if (strpos($allowed,$chr)===false) // special char
154: {
155: $f++;
156: $tolist[$f]="";
157: }
158: else
159: {
160: $tolist[$f].=$chr;
161: }
162: }
163:
164: foreach($tolist as $toaddr)
165: {
166: $toaddr=nices($toaddr);
167: if ($toaddr!="")
168: {
169: //echo $row['efrom'].":".$toaddr.":".$sub.":".$body."\n\n";
170: //$body="fish";
171: //mail($toaddr,$sub,$body,"From: ".$row['efrom']."\r\n","-f".$row['efrom']);
172: $header="From: ".$row['efrom']."\r\n";
173: //$header="";
174: //$exhead="-f".$row['efrom'];
175: //db("Sending Email: ".$toaddr);
176: if ($doalert)
177: {
178: mail($toaddr,$sub,$body,$header);
179: $this->Event("Sent alert email to ".$toaddr,4,"Flush","Email");
180: }
181: }
182: }
183:
184:
185:
186: }
187: else if ($row['atype']=="url")
188: {
189: // url send
190: if ($row['etype']==0) $body=$row['mdata'];
191: else $body="FreeNATS Alert,\r\n".$row['mdata']."\r\n--FreeNATS @ ".nicedt(time());
192:
193: $body=urlencode($body);
194: $tolist=array();
195: $f=0;
196: $tolist[0]="";
197: for ($a=0; $a198: {
199: $chr=$row['etolist'][$a];
200: //echo $chr;
201: if (strpos($allowed,$chr)===false) // special char
202: {
203: $f++;
204: $tolist[$f]="";
205: }
206: else
207: {
208: $tolist[$f].=$chr;
209: }
210: }
211:
212: foreach($tolist as $tourl)
213: {
214: if ($doalert)
215: {
216: $url=$tourl.$body;
217: $fp=@fopen($url,"r");
218: if ($fp>0) fclose($fp);
219: else $this->Event("URL Alert Failed ".$url,1,"Flush","URL");
220: $this->Event("URL Alert ".$url,4,"Flush","URL");
221: }
222: }
223:
224:
225: }
226:
227: }
228: }
229:
230: function GetAlerts()
231: {
232: $q="SELECT nodeid,alertlevel FROM fnalert WHERE closedx=0";
233: $r=$this->DB->Query($q);
234: $c=0;
235: $al=array();
236: while ($row=$this->DB->Fetch_Array($r))
237: {
238: $al[$c]['nodeid']=$row['nodeid'];
239: $al[$c]['alertlevel']=$row['alertlevel'];
240: $c++;
241: }
242: if ($c>0) return $al;
243: else return false;
244: }
245:
246: function SetAlerts($nodeid,$alertlevel,$alerts="")
247: {
248: // get current alert level
249: $q="SELECT alertlevel,nodealert FROM fnnode WHERE nodeid=\"".ss($nodeid)."\"";
250: $r=$this->DB->Query($q);
251: $row=$this->DB->Fetch_Array($r);
252: $this->DB->Free($r);
253: $cal=$row['alertlevel'];
254:
255: if ($alertlevel>$cal)
256: {
257: // trigger alert process
258: }
259:
260: if ($alertlevel!=$cal)
261: {
262: // update table
263: $q="UPDATE fnnode SET alertlevel=".ss($alertlevel)." WHERE nodeid=\"".ss($nodeid)."\"";
264: $this->DB->Query($q);
265: }
266:
267: // do not continue if node alert isn't set
268: if ($row['nodealert']!=1) return 0;
269:
270: // ALERTS
271: // is there an existing alert for this node
272: $q="SELECT alertid,alertlevel FROM fnalert WHERE nodeid=\"".ss($nodeid)."\" AND closedx=0";
273: $r=$this->DB->Query($q);
274: if ($row=$this->DB->Fetch_Array($r))
275: { // yes there is
276: // if new alert level is 0 let's close it
277: if ($alertlevel==0)
278: {
279: $q="UPDATE fnalert SET closedx=".time()." WHERE alertid=".$row['alertid'];
280: $this->DB->Query($q);
281: if (is_array($alerts)) $alerts[]="Alert Closed";
282: else
283: {
284: $alerts=array();
285: $alerts[]="Alert Closed";
286: }
287: }
288: else
289: {
290: $alertid=$row['alertid'];
291: // otherwise update the alert to the new value (was: regardless, now just if not a 0)
292: $q="UPDATE fnalert SET alertlevel=".ss($alertlevel)." WHERE alertid=".$alertid;
293: $this->DB->Query($q);
294: }
295: }
296: else
297: { // no there's not
298: if ($alertlevel>0) // only if an actual alert
299: {
300: $q="INSERT INTO fnalert(nodeid,alertlevel,openedx) VALUES(";
301: $q.="\"".ss($nodeid)."\",".ss($alertlevel).",".time().")";
302: $this->DB->Query($q);
303: $alertid=$this->DB->Insert_Id();
304: }
305: }
306: // ALERT LOG with $alertid
307: $t=time();
308: $at="";
309: if (is_array($alerts))
310: {
311: foreach($alerts as $alert)
312: {
313: if ($at!="") $at.=", ";
314: $at.=$alert;
315: //echo $at."\n";
316: $iq="INSERT INTO fnalertlog(alertid,postedx,logentry) VALUES(";
317: $iq.=$alertid.",".$t.",\"".ss($alert)."\")";
318: //echo $iq;
319: $this->DB->Query($iq);
320: }
321: }
322:
323: $this->AlertAction($nodeid,$alertlevel,$alertlevel-$cal,$at);
324:
325:
326:
327: }
328:
329: function NodeAlertLevel($nodeid)
330: {
331: $q="SELECT alertlevel FROM fnnode WHERE nodeid=\"".ss($nodeid)."\"";
332: $r=$this->DB->Query($q);
333: if ($row=$this->DB->Fetch_Array($r)) return $row['alertlevel'];
334: else return -1;
335: }
336:
337: function GroupAlertLevel($groupid)
338: {
339: $lvl=-1;
340: $q="SELECT nodeid FROM fngrouplink WHERE groupid=\"".ss($groupid)."\"";
341: $r=$this->DB->Query($q);
342: while ($row=$this->DB->Fetch_Array($r))
343: {
344: $nl=$this->NodeAlertLevel($row['nodeid']);
345: if ($nl>$lvl) $lvl=$nl;
346: }
347: $this->DB->Free($r);
348: return $lvl;
349: }
350:
351: function PhoneHome($mode=0,$type="ping") // 0 - php, 1 - html, 2 - data
352: {
353: if ($mode<2)
354: {
355: $qs="?type=".$type."&data=version=".$this->Version;
356: if (isset($_SERVER['REMOTE_ADDR']))
357: $qs.=",ip=".$_SERVER['REMOTE_ADDR'];
358: $ploc="http://www.purplepixie.org/freenats/report/";
359: if ($mode==1) $ploc.="ping.html";
360: else $ploc.="ping.php";
361:
362: $ploc.=$qs;
363:
364: $lp=@fopen($ploc,"r");
365: if ($lp>0) @fclose($lp);
366: }
367: else
368: {
369: // data post -- !!
370: }
371: }
372:
373: }
374: ?>