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