File: 0.02.46a/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.46"; 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: } 188: } 189: 190: function GetAlerts() 191: { 192: $q="SELECT nodeid,alertlevel FROM fnalert WHERE closedx=0"; 193: $r=$this->DB->Query($q); 194: $c=0; 195: $al=array(); 196: while ($row=$this->DB->Fetch_Array($r)) 197: { 198: $al[$c]['nodeid']=$row['nodeid']; 199: $al[$c]['alertlevel']=$row['alertlevel']; 200: $c++; 201: } 202: if ($c>0) return $al; 203: else return false; 204: } 205: 206: function SetAlerts($nodeid,$alertlevel,$alerts="") 207: { 208: // get current alert level 209: $q="SELECT alertlevel,nodealert FROM fnnode WHERE nodeid=\"".ss($nodeid)."\""; 210: $r=$this->DB->Query($q); 211: $row=$this->DB->Fetch_Array($r); 212: $this->DB->Free($r); 213: $cal=$row['alertlevel']; 214: 215: if ($alertlevel>$cal) 216: { 217: // trigger alert process 218: } 219: 220: if ($alertlevel!=$cal) 221: { 222: // update table 223: $q="UPDATE fnnode SET alertlevel=".ss($alertlevel)." WHERE nodeid=\"".ss($nodeid)."\""; 224: $this->DB->Query($q); 225: } 226: 227: // do not continue if node alert isn't set 228: if ($row['nodealert']!=1) return 0; 229: 230: // ALERTS 231: // is there an existing alert for this node 232: $q="SELECT alertid,alertlevel FROM fnalert WHERE nodeid=\"".ss($nodeid)."\" AND closedx=0"; 233: $r=$this->DB->Query($q); 234: if ($row=$this->DB->Fetch_Array($r)) 235: { // yes there is 236: // if new alert level is 0 let's close it 237: if ($alertlevel==0) 238: { 239: $q="UPDATE fnalert SET closedx=".time()." WHERE alertid=".$row['alertid']; 240: $this->DB->Query($q); 241: if (is_array($alerts)) $alerts[]="Alert Closed"; 242: else 243: { 244: $alerts=array(); 245: $alerts[]="Alert Closed"; 246: } 247: } 248: else 249: { 250: $alertid=$row['alertid']; 251: // otherwise update the alert to the new value (was: regardless, now just if not a 0) 252: $q="UPDATE fnalert SET alertlevel=".ss($alertlevel)." WHERE alertid=".$alertid; 253: $this->DB->Query($q); 254: } 255: } 256: else 257: { // no there's not 258: if ($alertlevel>0) // only if an actual alert 259: { 260: $q="INSERT INTO fnalert(nodeid,alertlevel,openedx) VALUES("; 261: $q.="\"".ss($nodeid)."\",".ss($alertlevel).",".time().")"; 262: $this->DB->Query($q); 263: $alertid=$this->DB->Insert_Id(); 264: } 265: } 266: // ALERT LOG with $alertid 267: $t=time(); 268: $at=""; 269: if (is_array($alerts)) 270: { 271: foreach($alerts as $alert) 272: { 273: if ($at!="") $at.=", "; 274: $at.=$alert; 275: //echo $at."\n"; 276: $iq="INSERT INTO fnalertlog(alertid,postedx,logentry) VALUES("; 277: $iq.=$alertid.",".$t.",\"".ss($alert)."\")"; 278: //echo $iq; 279: $this->DB->Query($iq); 280: } 281: } 282: 283: $this->AlertAction($nodeid,$alertlevel,$alertlevel-$cal,$at); 284: 285: 286: 287: } 288: 289: function NodeAlertLevel($nodeid) 290: { 291: $q="SELECT alertlevel FROM fnnode WHERE nodeid=\"".ss($nodeid)."\""; 292: $r=$this->DB->Query($q); 293: if ($row=$this->DB->Fetch_Array($r)) return $row['alertlevel']; 294: else return -1; 295: } 296: 297: function GroupAlertLevel($groupid) 298: { 299: $lvl=-1; 300: $q="SELECT nodeid FROM fngrouplink WHERE groupid=\"".ss($groupid)."\""; 301: $r=$this->DB->Query($q); 302: while ($row=$this->DB->Fetch_Array($r)) 303: { 304: $nl=$this->NodeAlertLevel($row['nodeid']); 305: if ($nl>$lvl) $lvl=$nl; 306: } 307: $this->DB->Free($r); 308: return $lvl; 309: } 310: 311: function PhoneHome($mode=0,$type="ping") // 0 - php, 1 - html, 2 - data 312: { 313: if ($mode<2) 314: { 315: $qs="?type=".$type."&data=version=".$this->Version; 316: if (isset($_SERVER['REMOTE_ADDR'])) 317: $qs.=",ip=".$_SERVER['REMOTE_ADDR']; 318: $ploc="http://www.purplepixie.org/freenats/report/"; 319: if ($mode==1) $ploc.="ping.html"; 320: else $ploc.="ping.php"; 321: 322: $ploc.=$qs; 323: 324: $lp=@fopen($ploc,"r"); 325: if ($lp>0) @fclose($lp); 326: } 327: else 328: { 329: // data post -- !! 330: } 331: } 332: 333: } 334: ?>