m_dbName = $GLOBALS['wlini']['dbName']; $this->m_onlineTime = $GLOBALS['wlini']['visitTime']; $this->botKeywords = $GLOBALS['wlini']['botKeywords']; $this->included = false; $this->m_usoIP = $_SERVER['REMOTE_ADDR']; if(isset($_SERVER['REMOTE_HOST'])){ $this->m_usoHost = stripslashes($_SERVER['REMOTE_HOST']); } if(isset($_SERVER['HTTP_USER_AGENT'])){ $this->m_usoUseragent = stripslashes($_SERVER['HTTP_USER_AGENT']); } if($setAccessNow){ $this->setAccess(); } } // end constructor // ----------------------------- Get methods ------------------------------------ // /** * Returns the own IP address * * @access public * @return REMOTE_ADDR */ function getMyIP(){ return $this->m_usoIP; } // end method /** * Returns the own Hostname * * @access public * @return REMOTE_HOST */ function getMyHost(){ return $this->m_usoHost; } // end method /** * Return the state of bot counts * * @access public * @return true if bots are in statistics */ function getCountBots(){ return $this->countBots; } // end method /** * Returns the oldest day found in the database, therefore begin of data collection * * @access public * @return first day data collection, 0 if nothing found */ function getFirstDay(){ $ret = 0; $sql = "SELECT DATE_FORMAT(min(usoDay),'%d-%m-%Y') AS firstday FROM tabuseronline"; $res = $this->_sendSql($sql); if ($row = mysql_fetch_array($res,MYSQL_ASSOC)){ $ret = $row['firstday']; } return $ret; } // end method /** * Returns the all time amount of different IP Adresses * * @access public * @return count IPs total */ function getIPCountTotal($pUpdateCycle = 0, $pCss = 0){ $ret = ""; if($this->countBots){ $sql = "SELECT usoIP FROM tabuseronline GROUP BY usoIP"; } else{ $sql = "SELECT usoIP FROM tabuseronline LEFT JOIN tabbotlist ON tabuseronline.usoUseragent = tabbotlist.botUseragent "; $sql .= "WHERE (tabbotlist.botUseragent IS NULL OR tabbotlist.botState = 'inactive') GROUP BY usoIP"; } $res = $this->_sendSql($sql); $ret = mysql_num_rows($res); if($pUpdateCycle){ $ret = $this->_getLiveDiv("getIPCountTotal", $pUpdateCycle, $ret, $pCss); } return $ret; } // end method /** * Returns the amount of different IP Adresses of a specified day * * @access public * @param mixed $day [YYYYMMDD] * @return count IPs */ function getIPCountDay($day, $pUpdateCycle = 0, $pCss = 0){ $ret = ""; if($this->countBots){ $sql = "SELECT usoIP FROM tabuseronline WHERE "; $sql .= "DATE_FORMAT(usoDay, '%Y%m%d') = '".$day."' GROUP BY usoIP"; } else{ $sql = "SELECT usoIP FROM tabuseronline LEFT JOIN tabbotlist ON tabuseronline.usoUseragent = tabbotlist.botUseragent "; $sql .= "WHERE (tabbotlist.botUseragent IS NULL OR tabbotlist.botState = 'inactive') AND "; $sql .= "DATE_FORMAT(usoDay, '%Y%m%d') = '".$day."' GROUP BY usoIP"; } $res = $this->_sendSql($sql); $ret = mysql_num_rows($res); if($pUpdateCycle){ $ret = $this->_getLiveDiv("getIPCountDay", $pUpdateCycle, $ret, $pCss, $day); } return $ret; } // end method /** * Returns the amount of different IP Adresses of a specified month * * @access public * @param mixed $month [YYYYMM] * @return count IPs */ function getIPCountMonth($month, $pUpdateCycle = 0, $pCss = 0){ $ret = ""; if($this->countBots){ $sql = "SELECT usoIP FROM tabuseronline WHERE "; $sql .= "DATE_FORMAT(usoDay, '%Y%m') = '".$month."' GROUP BY usoIP"; } else{ $sql = "SELECT usoIP FROM tabuseronline LEFT JOIN tabbotlist ON tabuseronline.usoUseragent = tabbotlist.botUseragent "; $sql .= "WHERE (tabbotlist.botUseragent IS NULL OR tabbotlist.botState = 'inactive') AND "; $sql .= "DATE_FORMAT(usoDay, '%Y%m') = '".$month."' GROUP BY usoIP"; } $res = $this->_sendSql($sql); $ret = mysql_num_rows($res); if($pUpdateCycle){ $ret = $this->_getLiveDiv("getIPCountMonth", $pUpdateCycle, $ret, $pCss, $month); } return $ret; } // end method /** * Returns the amount of different IP Adresses of a specified year * * @access public * @param mixed $year [YYYY] * @return count IPs */ function getIPCountYear($year, $pUpdateCycle = 0, $pCss = 0){ $ret = ""; if($this->countBots){ $sql = "SELECT usoIP FROM tabuseronline WHERE "; $sql .= "DATE_FORMAT(usoDay, '%Y') = '".$year."' GROUP BY usoIP"; } else{ $sql = "SELECT usoIP FROM tabuseronline LEFT JOIN tabbotlist ON tabuseronline.usoUseragent = tabbotlist.botUseragent "; $sql .= "WHERE (tabbotlist.botUseragent IS NULL OR tabbotlist.botState = 'inactive') AND "; $sql .= "DATE_FORMAT(usoDay, '%Y') = '".$year."' GROUP BY usoIP"; } $res = $this->_sendSql($sql); $ret = mysql_num_rows($res); if($pUpdateCycle){ $ret = $this->_getLiveDiv("getIPCountYear", $pUpdateCycle, $ret, $pCss, $year); } return $ret; } // end method /** * Returns the highest amount of different IP Adresses on a single day * * @access public * @return count IPs record */ function getIPCountRecord(){ $ret = array("",""); if($this->countBots){ $sql = "SELECT DATE_FORMAT(usoDay, '%d-%m-%Y') AS day, count(*) AS max "; $sql .= "FROM tabuseronline GROUP BY usoDay ORDER BY max DESC, usoDay DESC limit 1"; } else{ $sql = "SELECT DATE_FORMAT(usoDay, '%d-%m-%Y') AS day, count(*) AS max "; $sql .= "FROM tabuseronline LEFT JOIN tabbotlist ON tabuseronline.usoUseragent = tabbotlist.botUseragent "; $sql .= "WHERE (tabbotlist.botUseragent IS NULL OR tabbotlist.botState = 'inactive') "; $sql .= "GROUP BY usoDay ORDER BY max DESC, usoDay DESC limit 1"; } $res = $this->_sendSql($sql); if($row = mysql_fetch_array($res,MYSQL_ASSOC)){ $ret['day'] = $row['day']; $ret['max'] = $row['max']; } return $ret; } // end method /** * Returns the amount of visitors during the last x minutes (specified online time) * * @access public * @param $pUpdateCycle (seconds of reload cycle, 0 if static) * @return count IPs online */ function getOnlineCount($pUpdateCycle = 0, $pCss = 0){ $ret = 0; if($this->countBots){ $sql = "SELECT COUNT(usoIP) AS nowonline FROM tabuseronline WHERE "; $sql .= "DATE_SUB(now(), INTERVAL ".$this->m_onlineTime." MINUTE) < usoLastAccess"; } else{ $sql = "SELECT COUNT(usoIP) AS nowonline FROM tabuseronline LEFT JOIN tabbotlist "; $sql .= "ON tabuseronline.usoUseragent = tabbotlist.botUseragent "; $sql .= "WHERE (tabbotlist.botUseragent IS NULL OR tabbotlist.botState = 'inactive') AND "; $sql .= "DATE_SUB(now(), INTERVAL ".$this->m_onlineTime." MINUTE) < usoLastAccess"; } $res = $this->_sendSql($sql); if ($row = mysql_fetch_array($res,MYSQL_ASSOC)){ $ret = $row['nowonline']; } if($pUpdateCycle){ $ret = $this->_getLiveDiv("getOnlineCount", $pUpdateCycle, $ret, $pCss); } return $ret; } // end method /** * Returns the amount of active bots in the botlist * * @access public * @return count bots */ function getActiveBotCount(){ $ret = 0; $sql = "SELECT COUNT(botId) AS botscount FROM tabbotlist WHERE botState = 'active'"; $res = $this->_sendSql($sql); if ($row = mysql_fetch_array($res,MYSQL_ASSOC)){ $ret = $row['botscount']; } return $ret; } // end method /** * Returns an assoc. array of access values of the chosen day * * @access public * @param mixed $date [YYYYMMDD] * @return array (ip,host,useragent,visits,hits,botid,lastAccess[DD-MM-YYYY hh:mm:ss],lastAccessUnix) */ function getIPList($date){ $ret = array(); $onlineStart = time() - ($this->m_onlineTime * 60); $sql = "SELECT *, DATE_FORMAT(usoLastAccess, '%d-%m-%Y %T') AS lastAccess, "; $sql .= "UNIX_TIMESTAMP(usoLastAccess) AS lastAccessUnix "; $sql .= "FROM tabuseronline LEFT JOIN tabbotlist ON tabuseronline.usoUseragent = tabbotlist.botUseragent "; $sql .= "WHERE DATE_FORMAT(usoDay, '%Y%m%d') = ".$date; $sql .= " ORDER BY usoId"; $res = $this->_sendSql($sql); if(mysql_errno() == $this->tableDoesNotExistError){ if($this->_addAllTables()){ echo "-> added tables! please refresh!
"; } } else{ while ($row = mysql_fetch_array($res,MYSQL_ASSOC)){ $current = array(); $current['ip'] = $row['usoIP']; $current['host'] = $row['usoHost']; $current['useragent'] = $row['usoUseragent']; $current['visits'] = $row['usoVisits']; $current['hits'] = $row['usoHits']; if(($row['botId']) && ($row['botState'] == "active")){ $current['botId'] = $row['botId']; } else{ $current['botId'] = 0; } $current['lastAccess'] = $row['lastAccess']; if($row['lastAccessUnix'] > $onlineStart){ $current['online'] = true; } else{ $current['online'] = false; } array_push($ret,$current); } } return $ret; } // end method /** * Returns an assoc. array of bot values * * @access public * @return array (botid,useragent,enterdate,moddate,state,lastAccess[DD-MM-YYYY hh:mm:ss]) */ function getBotList(){ $ret = array(); $sql = "SELECT * FROM tabbotlist ORDER BY botState,botEnterdate DESC"; $res = $this->_sendSql($sql); if(mysql_errno() == $this->tableDoesNotExistError){ if($this->_addAllTables()){ echo "-> added tables! please refresh!
"; } } else{ while ($row = mysql_fetch_array($res,MYSQL_ASSOC)){ $current = array(); $current['botid'] = $row['botId']; $current['useragent'] = $row['botUseragent']; $current['enterdate'] = $row['botEnterdate']; $current['moddate'] = $row['botModdate']; $current['state'] = $row['botState']; array_push($ret,$current); } } return $ret; } // end method /** * Returns an assoc. array of values of useragents which might be a bot * * @access public * @return array (useragent,visits,hist,lastAccess[DD-MM-YYYY hh:mm:ss]) */ function getPossibleBotList(){ $ret = array(); $sql = "SELECT tabuseronline.usoUseragent, sum(usoVisits) AS visits, sum(usoHits) AS hits, "; $sql .= "DATE_FORMAT(usoLastAccess, '%d-%m-%Y %T') AS lastAccess "; $sql .= "FROM tabuseronline LEFT JOIN tabbotlist ON tabuseronline.usoUseragent = tabbotlist.botUseragent "; $sql .= "WHERE tabbotlist.botUseragent IS NULL AND ("; foreach($this->botKeywords as $value){ $sql .= " lower(tabuseronline.usoUseragent) LIKE '%".$value."%' OR"; } //$sql = ereg_replace("OR$", "", $sql); $sql .= " length(tabuseronline.usoUseragent) < 25) "; $sql .= "GROUP BY tabuseronline.usoUseragent"; $sql .= " ORDER BY usoLastAccess DESC"; $res = $this->_sendSql($sql); if(mysql_errno() == $this->tableDoesNotExistError){ if($this->_addAllTables()){ echo "-> added tables! please refresh!
"; } } else{ while ($row = mysql_fetch_array($res,MYSQL_ASSOC)){ $current = array(); $current['useragent'] = $row['usoUseragent']; $current['visits'] = $row['visits']; $current['hits'] = $row['hits']; $current['lastAccess'] = $row['lastAccess']; array_push($ret,$current); } } return $ret; } // end method /** * Creates a image chart of the specified month. * Call this method in src part of an img tag. e.g. * * @access public * @param mixed $pFilepath Path an Filename of the image (directory must have rights to write) * @param mixed $pTitle Title of chart * @param mixed $pShow 'visits', 'hits' or 'ip' * @param mixed $pBarColor default 'lightskyblue' * @param mixed $pPicLength default 400px * @param mixed $pPicHeight default 250px * @param mixed $month [YYYYMM] default current month * @return picsource */ function getChartMonth($pFilepath, $pTitle, $pShow, $month, $pBarColor = "lightskyblue", $pPicLength = 410, $pPicHeight = 250, $pShadow = true){ if(!$this->included){ include ("graphsrc/jpgraph.php"); include ("graphsrc/jpgraph_line.php"); include ("graphsrc/jpgraph_bar.php"); $this->included = true; } $curDayOfMonth = date("j"); $lastDayOfCurrentMonth = 1; $unixTimestamp = 0; $dataList = array(); $avgList = array(); if($pShow == "visits"){ $sql = "SELECT SUM(usoVisits) as total, "; } elseif($pShow == "hits"){ $sql = "SELECT SUM(usoHits) as total, "; } else{ $sql = "SELECT COUNT(usoIP) as total, "; } $sql .= "DATE_FORMAT(usoDay, '%d') AS day, UNIX_TIMESTAMP(usoDay) AS unixts FROM tabuseronline "; if(!$this->countBots){ $sql .= "LEFT JOIN tabbotlist ON tabuseronline.usoUseragent = tabbotlist.botUseragent "; $sql .= "WHERE (tabbotlist.botUseragent IS NULL OR tabbotlist.botState = 'inactive') AND "; } else{ $sql .= "WHERE "; } $datepattern = "%Y%m"; if(strlen($month) < 6){ $datepattern = "%Y%c"; } $sql .= "DATE_FORMAT(usoDay, '".$datepattern."') = ".$month." GROUP BY usoDay ORDER BY usoDay"; $res = $this->_sendSql($sql); while ($row = mysql_fetch_array($res,MYSQL_ASSOC)){ $lastDayOfCurrentMonth = $row['day']; $dataList[$lastDayOfCurrentMonth - 1] = $row['total']; if(!$unixTimestamp){ $unixTimestamp = $row['unixts']; } } // fill up missing days for($i = 0; $i < date("t", $unixTimestamp); $i++){ if(!isset($dataList[$i])){ $dataList[$i] = 0; } } ksort($dataList); reset($dataList); // Calculate the average $avg = (array_sum($dataList) / $lastDayOfCurrentMonth); foreach($dataList as $elem){ array_push($avgList,$avg); } // Create the graph $graph = new Graph($pPicLength, $pPicHeight, "auto"); $graph->SetScale("textlin"); $graph->img->SetMargin(40,20,20,30); if($pShadow){ $graph->SetShadow(); } // Create the linear error plot $lPlot = new LinePlot($avgList); $lPlot->SetColor("red"); $lPlot->SetWeight(2); $lPlot->SetBarCenter(); // Create the bar plot $bPlot = new BarPlot($dataList); $bPlot->SetFillColor($pBarColor); $bPlot->SetWidth(0.8); if($pPicHeight >= 250){ $bPlot->value->Show(); $bPlot->value->SetFormat('%d'); $graph->yaxis->scale->SetGrace(5); } // Add the plots to the graph $graph->Add($bPlot); $graph->Add($lPlot); $graph->title->Set($pTitle); if($pPicLength < 410){ $graph->title->SetFont(FF_FONT1); } else{ $graph->title->SetFont(FF_FONT1 ,FS_BOLD); } $graph->xaxis->SetFont(FF_FONT0); $graph->xaxis->SetLabelAngle(90); if($pPicHeight < 250){ $graph->yaxis->SetFont(FF_FONT0); } //$graph->xaxis->title->Set($xTitle); //$graph->yaxis->title->Set($yTitle); $graph->SetMarginColor('#DDDDDD'); $graph->ygrid->SetFill(true,'#EFEFEF@0.5','#CCDDFF@0.5'); // Display the graph $graph->Stroke($pFilepath); } // end method /** * Returns the sourcecode for a livemonitor div * * @access private * @param mixed $pFunctionName Name of the calling function * @param mixed $pUpdateCycle Cycle of reload in seconds * @param mixed $pValue Value of current standing * @param mixed $pCss Styling of the div Element * @param mixed $pMethodParams commaseperated all values to handover to the method in right order * @return jscode and div */ function _getLiveDiv($pFunctionName, $pUpdateCycle, $pValue, $pCss, $pMethodParams = 0){ $ret = "\n"; if($pUpdateCycle < 10){ $pUpdateCycle = 10; } if(!$this->jsfilesincluded){ $ret .= $this->_includeJsFiles(); } $url = $GLOBALS['wlini']['wlrootpath']."westlog/inc/livemonitor.php?method="; $url .= $pFunctionName; if($pMethodParams){ $url .= "¶ms=".$pMethodParams; } $ret .= "\n"; $ret .= "
\n"; return $ret; } // end method // ----------------------------- Set methods ------------------------------------ // /** * Sets the count of bots true. Bot accesses will also be shown in the statistics * * @access public */ function setCountBots(){ $this->countBots = true; } // end method /** * Sets the access values on database for calling remote. * set param true if you want to add the table (if not exists). * * @access public * @param boolean $addTable * @return true if log was affective */ function setAccess($addTable = true){ $ret = true; $sql = "UPDATE tabuseronline SET "; $sql .= "usoVisits = if(usoLastAccess < DATE_SUB(now(), INTERVAL ".$this->m_onlineTime." MINUTE), (usoVisits + 1), usoVisits),"; $sql .= "usoHits = (usoHits + 1),"; $sql .= "usoUseragent = '".$this->m_usoUseragent."',"; $sql .= "usoLastAccess = now() "; $sql .= "WHERE DATE_FORMAT(usoDay, '%Y%m%d') = ".date("Ymd"); $sql .= " AND usoIP = '".$this->m_usoIP."'"; $this->_sendSql($sql); $affRows = mysql_affected_rows(); if(mysql_errno() == $this->tableDoesNotExistError){ $ret = false; if($addTable){ if($this->_addAllTables()){ if($this->_addAccess()){ $ret = true; } } } } elseif($affRows == 0){ // if new remote on current day, create a new record with initial values if(!$this->_addAccess()){ $ret = false; } } return $ret; } // end method /** * Sets the userAgent to the botlist. * set param true if you want to add the table (if not exists). * * @access public * @param boolean $addTable * @return true if set was affective */ function setBot($userAgent, $addTable = true){ $ret = true; $sql = "UPDATE tabbotlist SET "; $sql .= "botState = 'active',"; $sql .= "botModdate = now() "; $sql .= "WHERE botUseragent = '".$userAgent."'"; $this->_sendSql($sql); $affRows = mysql_affected_rows(); if(mysql_errno() == $this->tableDoesNotExistError){ $ret = false; if($addTable){ if($this->_addAllTables()){ if($this->_addToBotList($userAgent)){ $ret = true; } } } } elseif($affRows == 0){ if(!$this->_addToBotList($userAgent)){ $ret = false; } } return $ret; } // end method // ----------------------------- Add methods ------------------------------------ // /** * Puts a new user access to the list (first access of the day). * * @access private * @return boolean true if INSERT was successful */ function _addAccess(){ $ret = true; $sql = "INSERT INTO tabuseronline (usoDay, usoIP, usoHost, usoUseragent, usoLastAccess) VALUES ("; $sql .= "now(),'".$this->m_usoIP."','".$this->m_usoHost."','".$this->m_usoUseragent."',now())"; $this->_sendSql($sql); if(mysql_affected_rows() < 1){ $ret = false; } return $ret; } // end method /** * Puts a new bot to the botlist. * * @access private * @param mixed $userAgent * @return boolean true if INSERT was successful */ function _addToBotList($userAgent){ $ret = true; $sql = "INSERT INTO tabbotlist (botUseragent, botEnterdate) VALUES ('".$userAgent."',now())"; $this->_sendSql($sql); if(mysql_affected_rows() < 1){ $ret = false; } return $ret; } // end method function _addAllTables(){ $ret = false; if(($this->_addTabuseronline()) && ($this->_addTabbotlist())){ $ret = true; } return $ret; } // end method /** * Creates the table tabuseronline if it does not exist * * @access private * @return true if system was able to create */ function _addTabuseronline(){ $ret = true; $sql = "CREATE TABLE tabuseronline ("; $sql .= "usoId INT(11) NOT NULL auto_increment,"; $sql .= "usoDay DATE NOT NULL,"; $sql .= "usoIP VARCHAR(20) NOT NULL,"; $sql .= "usoHost VARCHAR(100) default NULL,"; $sql .= "usoUseragent VARCHAR(200) default NULL,"; $sql .= "usoVisits INT(11) NOT NULL default '1',"; $sql .= "usoHits INT(11) NOT NULL default '1',"; $sql .= "usoLastAccess DATETIME NOT NULL, "; $sql .= "PRIMARY KEY (usoId),"; $sql .= "UNIQUE KEY uso_unique (usoDay,usoIP)"; $sql .= ") TYPE=MyISAM"; $res = $this->_sendSql($sql); if(!$res){ $ret = false; } return $ret; } // end method /** * Creates the table tabbotlist if it does not exist * * @access private * @return true if system was able to create */ function _addTabbotlist(){ $ret = true; $sql = "CREATE TABLE tabbotlist ("; $sql .= "botId INT(11) NOT NULL auto_increment,"; $sql .= "botUseragent VARCHAR(200) default NULL,"; $sql .= "botState ENUM('active','inactive') default 'active',"; $sql .= "botEnterdate DATETIME NOT NULL,"; $sql .= "botModdate DATETIME default NULL,"; $sql .= "PRIMARY KEY (botId),"; $sql .= "UNIQUE KEY bot_unique (botUseragent)"; $sql .= ") TYPE=MyISAM"; $res = $this->_sendSql($sql); if(!$res){ $ret = false; } return $ret; } // end method // ----------------------------- Remove methods ------------------------------------ // /** * Sets the bot entry to inactive * * @access public * @param boolean $addTable * @return true if log was affective */ function removeBot($botID){ $ret = true; $sql = "UPDATE tabbotlist SET botState = 'inactive',"; $sql .= "botModdate = now() WHERE botId = ".$botID; $this->_sendSql($sql); if(mysql_affected_rows() < 1){ $ret = false; } return $ret; } // end method // ----------------------------- Misc methods ------------------------------------ // /** * Includes all required js files for ajax live monitoring * * @access private * @return code */ function _includeJsFiles() { $ret = ""; $this->jsfilesincluded = true; $ret .= "\n"; $ret .= "\n"; return $ret; } // end method /** * Deletes older specified files * * @access public */ function delOldFiles($directory, $prefix) { if($dir = opendir($directory)) { while($file = readdir($dir)) { if(eregi("^".$prefix,$file)){ unlink($directory.$file); } } } closedir($dir); } // end method /** * Passes the sql query to the mysql database * * @access private * @return result from database */ function _sendSql($sql){ $res = false; $selected = MYSQL_SELECT_DB($this->m_dbName); if($selected){ $res = mysql_query($sql); if (!$res){ //echo mysql_error(); } } return $res; } // end method } // end class ?>db_host = $host; $this->db_name = $dbName; $this->db_user = $user; $this->db_pass = $pass; $this->dbConnect(); } // end constructor /** * builds up a non-persistent connection to database * * @access public */ function dbConnect(){ $this->db_linkId = mysql_connect($this->db_host,$this->db_user,$this->db_pass); } // end method /** * Disconnects the database * * @access public */ function dbDisconnect(){ if(mysql_close($this->db_linkId)){ $this->db_linkId = 0; } } // end method /** * Checks if there is a valid link ID * * @access public * @return boolean true if database is connected */ function isConnected(){ if($this->db_linkId){ return $this->db_linkId; } else{ return false; } } // end method /** * Passes the sql query to the mysql database * * @access public * @return result from database */ function sendSql($sql, $printError = false){ $res = false; $selected = MYSQL_SELECT_DB($this->db_name); if($selected){ $res = mysql_query($sql); if((!$res) && ($printError)){ echo mysql_error(); } } return $res; } // end method } // end class ?>