File: 0.02.35a/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.35"; 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: $alertid=$row['alertid']; 243: // otherwise update the alert to the new value regardless 244: $q="UPDATE fnalert SET alertlevel=".ss($alertlevel)." WHERE alertid=".$alertid; 245: $this->DB->Query($q); 246: } 247: else 248: { // no there's not 249: if ($alertlevel>0) // only if an actual alert 250: { 251: $q="INSERT INTO fnalert(nodeid,alertlevel,openedx) VALUES("; 252: $q.="\"".ss($nodeid)."\",".ss($alertlevel).",".time().")"; 253: $this->DB->Query($q); 254: $alertid=$this->DB->Insert_Id(); 255: } 256: } 257: // ALERT LOG with $alertid 258: $t=time(); 259: $at=""; 260: if (is_array($alerts)) 261: { 262: foreach($alerts as $alert) 263: { 264: if ($at!="") $at.=", "; 265: $at.=$alert; 266: //echo $at."\n"; 267: $iq="INSERT INTO fnalertlog(alertid,postedx,logentry) VALUES("; 268: $iq.=$alertid.",".$t.",\"".ss($alert)."\")"; 269: //echo $iq; 270: $this->DB->Query($iq); 271: } 272: } 273: 274: $this->AlertAction($nodeid,$alertlevel,$alertlevel-$cal,$at); 275: 276: 277: 278: } 279: 280: function NodeAlertLevel($nodeid) 281: { 282: $q="SELECT alertlevel FROM fnnode WHERE nodeid=\"".ss($nodeid)."\""; 283: $r=$this->DB->Query($q); 284: if ($row=$this->DB->Fetch_Array($r)) return $row['alertlevel']; 285: else return -1; 286: } 287: 288: function GroupAlertLevel($groupid) 289: { 290: $lvl=-1; 291: $q="SELECT nodeid FROM fngrouplink WHERE groupid=\"".ss($groupid)."\""; 292: $r=$this->DB->Query($q); 293: while ($row=$this->DB->Fetch_Array($r)) 294: { 295: $nl=$this->NodeAlertLevel($row['nodeid']); 296: if ($nl>$lvl) $lvl=$nl; 297: } 298: $this->DB->Free($r); 299: return $lvl; 300: } 301: 302: function PhoneHome($mode=0,$type="ping") // 0 - php, 1 - html, 2 - data 303: { 304: if ($mode<2) 305: { 306: $qs="?type=".$type."&data=version=".$this->Version; 307: if (isset($_SERVER['REMOTE_ADDR'])) 308: $qs.=",ip=".$_SERVER['REMOTE_ADDR']; 309: $ploc="http://www.purplepixie.org/freenats/report/"; 310: if ($mode==1) $ploc.="ping.html"; 311: else $ploc.="ping.php"; 312: 313: $ploc.=$qs; 314: 315: $lp=@fopen($ploc,"r"); 316: if ($lp>0) @fclose($lp); 317: } 318: else 319: { 320: // data post -- !! 321: } 322: } 323: 324: } 325: ?>