diff --git a/e107_plugins/log/consolidate.php b/e107_plugins/log/consolidate.php index cfbf8e2ab..858a549b5 100644 --- a/e107_plugins/log/consolidate.php +++ b/e107_plugins/log/consolidate.php @@ -151,7 +151,7 @@ class logConsolidate // for future use. - function collate($pfile) + private function collate($pfile) { if(is_readable(e_LOG.$pfile)) { @@ -162,8 +162,12 @@ class logConsolidate return false; } + return null; } + + + /** * @param $url * @param bool $logQry @@ -179,7 +183,6 @@ class logConsolidate preg_match("#/(.*?)(\?|$)(.*)#si", $url, $match); $match[1] = isset($match[1]) ? $match[1] : ''; $pageName = substr($match[1], (strrpos($match[1], "/")+1)); - $PN = $pageName; $pageName = preg_replace("/".$tagRemove."/si", "", $pageName); @@ -205,7 +208,118 @@ class logConsolidate } + /** + * Process Raw Backup Log File. e. e_LOG."log/2015-09-24_SiteStats.log + * This method can be used in the case of a database corruption to restore stats to the database. + * @param string $file + * @example processRawBackupLog('2015-09-24_SiteStats.log', false); + */ + function processRawBackupLog($file, $savetoDB=false) + { + $path = e_LOG."log/".$file; + if(!is_readable($path)) + { + return false; + } + + $handle = fopen($path, "r"); + + $pageTotal = array(); + + if ($handle) + { + while (($buffer = fgets($handle, 4096)) !== false) + { + if($vars = $this->splitRawBackupLine($buffer)) + { + $key = $this->getPageKey($vars['eself']); + + $pageTotal[$key]['url'] = $vars['eself']; + $pageTotal[$key]['ttl'] += 1; + + if(!isset($pageTotal[$key]['unq'])) + { + $pageTotal[$key]['unq'] = 0; + } + + if(isset($vars['unique'])) + { + if($vars['unique'] == 1) + { + $pageTotal[$key]['unq'] += 1; + } + } + else + { + $pageTotal[$key]['unq'] += 1; + } + } + } + + if (!feof($handle)) + { + echo "Error: unexpected fgets() fail\n"; + } + + fclose($handle); + + echo "

".$file."

"; + print_a($pageTotal); + } + + if($savetoDB === false) + { + echo "Saving mode is off"; + return true; + } + + + if(!empty($pageTotal)) + { + list($date,$name) = explode("_", $file, 2); + + $unix = strtotime($date); + + $datestamp = date("Y-m-j", $unix); + + if(!empty($datestamp)) + { + $sql = e107::getDb(); + + if($sql->select('logstats','log_id',"log_id='".$datestamp."' ")) + { + $sql->update('logstats', "log_id='".$datestamp."-bak' WHERE log_id='".$datestamp."' "); + } + + if($this->collatePageInfo($pageTotal, $datestamp)) + { + echo "
Data saved to database with id: ".$datestamp; + } + else + { + echo "
Couldn't save data to database with id: ".$datestamp; + } + } + + } + } + + + + + private function splitRawBackupLine($line) + { + list($datestamp,$bla,$data) = explode("\t",$line, 3); + + if(!empty($data)) + { + parse_str($data,$vars); + return $vars; + } + + return false; + } /** * Fix corrupted page data. @@ -273,6 +387,7 @@ class logConsolidate /** * collate page total information using today's data and totals stored in DB. * @param $pageInfo - from today's file. + * @return bool */ function collatePageTotal($pageInfo=array()) { @@ -325,14 +440,14 @@ class logConsolidate /** - * Collate individual page information into an array - * @param $pageInfo + * Collate individual page information into an array and save to database. + * @param array $pageInfo + * @param string $date - the value saved to log_id ie. Y-m-j , 2015-02-1, 2015-02-30 + * @return bool */ - function collatePageInfo($pageInfo) + function collatePageInfo($pageInfo, $date) { - global $date2; - $sql = e107::getDb(); $tp = e107::getParser(); @@ -348,7 +463,7 @@ class logConsolidate } $data = $dailytotal.chr(1).$uniquetotal.chr(1) . $data; - $sql->insert("logstats", "0, '$date2', '".$tp -> toDB($data, true)."'"); + return $sql->insert("logstats", "0, '$date', '".$tp -> toDB($data, true)."'"); } @@ -605,7 +720,7 @@ else $lgc->collatePageTotal($pageInfo); -$lgc->collatePageInfo($pageInfo); +$lgc->collatePageInfo($pageInfo, $date2); $lgc->resetLogFiles(); diff --git a/e107_plugins/log/log.php b/e107_plugins/log/log.php index 24d9e8462..7630368ae 100644 --- a/e107_plugins/log/log.php +++ b/e107_plugins/log/log.php @@ -29,45 +29,34 @@ require_once("../../class2.php"); // More secure to include it. header('Cache-Control: no-cache, must-revalidate'); // See if this discourages browser caching header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past +define('LOG_DEBUG', false); + +// @example url: e107_plugins/log/log.php?lv=cmVmZXJlcj1odHRwJTNBLy9sb2NhbGhvc3QlM0E4MDgwL2UxMDdfMi4wL3N0YXRzJmNvbG91cj0yNCZlc2VsZj1odHRwJTNBLy9sb2NhbGhvc3QlM0E4MDgwL2UxMDdfMi4wL2UxMDdfcGx1Z2lucy9sb2cvc3RhdHMucGhwJTNGMiZyZXM9MTkyMHgxMjAw + +if(LOG_DEBUG == true) +{ + echo "Debug is Active"; +} + if (!vartrue($pref['statActivate'])) { + if(LOG_DEBUG == true) + { + echo "Stats log is inactive"; + } + exit(); } -/** - * Set up path to log files. - * The log file directory contains a flag file which defines whether logging is enabled. - */ - /* -function setLogPath() -{ - $siteRoot = realpath(dirname(__FILE__).'./../../').'/'; - @include_once($siteRoot.'e107_config.php'); - if (!isset($mySQLdefaultdb)) return FALSE; - if (!isset($mySQLprefix)) return FALSE; - $hash = substr(md5($mySQLdefaultdb.".".$mySQLprefix),0,10); - $logDir = $siteRoot.$SYSTEM_DIRECTORY.$hash.'/logs/'; - $logEnable = 0; - @include_once($logDir.'LogFlag.php'); // See if logging enabled - define('e_LOG', $logDir); - return $logEnable; -} -*/ - -// if (!setLogPath()) exit(); // Could be logging disabled, missing files, all sorts of things. Just do nothing. //print_r(base64_decode($_GET['lv'])); define('log_INIT', TRUE); - - // Array of page names which should have individual query values recorded. // The top level array index is the page name. // If the top level value is an array, it must be an array of query string beginnings to match. $pageUnique = array('page' => 1, 'content' => array('content')); - - //$logVals = urldecode(base64_decode($_SERVER['QUERY_STRING'])); //$logVals = urldecode(base64_decode($_GET['lv'])); @@ -110,10 +99,6 @@ e107::getEvent()->trigger('user_log_stats',$vals); // ------------------------------------ --------------------- - - - - // We MUST have a timezone set in PHP >= 5.3. This should work for PHP >= 5.1: // @todo may be able to remove this check once minimum PHP version finalised if (function_exists('date_default_timezone_get')) @@ -122,7 +107,6 @@ if (function_exists('date_default_timezone_get')) } - //$logfp = fopen(e_LOG.'rcvstring.txt', 'a+'); fwrite($logfp, $logVals."\n"); fclose($logfp); //$logfp = fopen(e_LOG.'rcvstring.txt', 'a+'); fwrite($logfp, print_r($vals, TRUE)."\n"); fclose($logfp); @@ -174,7 +158,7 @@ if(strstr($ref, 'admin')) $screenstats = $res.'@'.$colour; $agent = $_SERVER['HTTP_USER_AGENT']; -$ip = getip(); +$ip = e107::getIPHandler()->ipDecode(USERIP); $oldref = $ref; // backup for search string being stripped off for referer if($ref && !strstr($ref, $_SERVER['HTTP_HOST'])) @@ -186,19 +170,18 @@ if($ref && !strstr($ref, $_SERVER['HTTP_HOST'])) } - +$pageDisallow = "cache|file|eself|admin"; +$tagRemove = "(\\\)|(\s)|(\')|(\")|(eself)|( )|(\.php)|(\.html)"; +$tagRemove2 = "(\\\)|(\s)|(\')|(\")|(eself)|( )"; function logGetPageKey($url,$logQry=false,$err_code='') { - $pageDisallow = "cache|file|eself|admin"; - $tagRemove = "(\\\)|(\s)|(\')|(\")|(eself)|( )|(\.php)|(\.html)"; - $tagRemove2 = "(\\\)|(\s)|(\')|(\")|(eself)|( )"; + global $pageDisallow, $tagRemove; preg_match("#/(.*?)(\?|$)(.*)#si", $url, $match); $match[1] = isset($match[1]) ? $match[1] : ''; $pageName = substr($match[1], (strrpos($match[1], "/")+1)); - $PN = $pageName; $pageName = preg_replace("/".$tagRemove."/si", "", $pageName); if($pageName == "") @@ -227,6 +210,10 @@ function logGetPageKey($url,$logQry=false,$err_code='') if(!$pageName = logGetPageKey($self,$logQry,$err_code)) { + if(LOG_DEBUG == true) + { + echo 'pageName was empty'; + } return; } @@ -268,6 +255,8 @@ else $flag = TRUE; } + + if(!strstr($ipAddresses, $ip)) { /* unique visit */ if(!$flag) @@ -280,6 +269,10 @@ if(!strstr($ipAddresses, $ip)) } + + + + $siteTotal ++; $info_data = var_export($pageInfo, true); //$date_stamp = date("z:Y", time()); // Same as '$date' variable @@ -304,66 +297,10 @@ if ($p_handle) fclose($p_handle); } - -// Get current IP address - return as a hex-encoded string -/* -function getip() +if(LOG_DEBUG == true) { - $ip = $_SERVER['REMOTE_ADDR']; - if (getenv('HTTP_X_FORWARDED_FOR')) - { - if (preg_match("#^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})#", getenv('HTTP_X_FORWARDED_FOR'), $ip3)) - { - $ip2 = array('#^0\..*#', - '#^127\..*#', // Local loopbacks - '#^192\.168\..*#', // RFC1918 - Private Network - '#^172\.(?:1[6789]|2\d|3[01])\..*#', // RFC1918 - Private network - '#^10\..*#', // RFC1918 - Private Network - '#^169\.254\..*#', // RFC3330 - Link-local, auto-DHCP - '#^2(?:2[456789]|[345][0-9])\..*#' // Single check for Class D and Class E - ); - $ip = preg_replace($ip2, $ip, $ip3[1]); - } - } - if ($ip == "") - { - $ip = "x.x.x.x"; - } - if (strpos($ip, ':') === FALSE) - { // Its an IPV4 address - return it as 32-character packed hex string - $ipa = explode(".", $ip); - return str_repeat('0000',5).'ffff'.sprintf('%02x%02x%02x%02x', $ipa[0], $ipa[1], $ipa[2], $ipa[3]); - } - else - { // Its IPV6 - if (strpos($ip,'.') !== FALSE) - { // IPV4 'tail' to deal with - $temp = strrpos($ip,':') +1; - $ipa = explode('.',substr($ip,$temp)); - $ip = substr($ip,0, $temp).sprintf('%02x%02x:%02x%02x', $ipa[0], $ipa[1], $ipa[2], $ipa[3]); - } - // Now 'normalise' the address - $temp = explode(':',$ip); - $s = 8 - count($temp); // One element will of course be the blank - foreach ($temp as $f) - { - if ($f == '') - { - $ret .= '0000'; // Always put in one set of zeros for the blank - if ($s > 0) - { - $ret .= str_repeat('0000',$s); - $s = 0; - } - } - else - { - $ret .= sprintf('%04x',hexdec($f)); - } - } - return $ret; - } + echo '
Script Completed'; } -*/ + ?> \ No newline at end of file