1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-02 20:57:26 +02:00

Issue #499 'PageTotal' data was not saving correctly.

This commit is contained in:
Cameron
2015-09-24 15:29:51 -07:00
parent cf093cc34e
commit 4660c4384c
4 changed files with 437 additions and 193 deletions

View File

@@ -2,21 +2,15 @@
/* /*
* e107 website system * e107 website system
* *
* Copyright (C) 2008-2009 e107 Inc (e107.org) * Copyright (C) 2008-2015 e107 Inc (e107.org)
* Released under the terms and conditions of the * Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
*
*
*
* $Source: /cvs_backup/e107_0.8/e107_plugins/log/consolidate.php,v $
* $Revision$
* $Date$
* $Author$
*/ */
/* first thing to do is check if the log file is out of date ... */ /* first thing to do is check if the log file is out of date ... */
// $pathtologs = e_PLUGIN."log/logs/";
if (!defined('e107_INIT')){ exit; } if (!defined('e107_INIT')){ exit; }
@@ -31,15 +25,392 @@ $pfile = "logp_".$date.".php"; // Today's log file
$ifileprev = "logi_".$yesterday.".php"; $ifileprev = "logi_".$yesterday.".php";
$ifile = "logi_".$date.".php"; $ifile = "logi_".$date.".php";
// Begin v2.x cleanup.
class logConsolidate
{
function ___construct()
{
}
function createLog($pathtologs, $statTotal='', $statUnique='')
{
global $pfile, $ifile;
if(!is_writable($pathtologs))
{
echo "Log directory is not writable - please CHMOD ".e_LOG." to 777";
echo '<br />Path to logs: '.$pathtologs;
return FALSE;
}
$varStart = chr(36);
$quote = chr(34);
$data = chr(60)."?php\n". chr(47)."* e107 website system: Log file: ".date("z:Y", time())." *". chr(47)."\n\n".
$varStart."refererData = ".$quote.$quote.";\n".
$varStart."ipAddresses = ".$quote.$quote.";\n".
$varStart."hosts = ".$quote.$quote.";\n".
$varStart."siteTotal = ".$quote."0".$quote.";\n".
$varStart."siteUnique = ".$quote."0".$quote.";\n".
$varStart."screenInfo = array();\n".
$varStart."browserInfo = array();\n".
$varStart."osInfo = array();\n".
$varStart."pageInfo = array(\n";
$data .= "\n);\n\n?". chr(62);
if(!touch($pathtologs.$pfile)) {
return FALSE;
}
if(!touch($pathtologs.$ifile)) {
return FALSE;
}
if(!is_writable($pathtologs.$pfile)) {
$old = umask(0);
chmod($pathtologs.$pfile, 0777);
umask($old);
// return FALSE;
}
if(!is_writable($pathtologs.$ifile)) {
$old = umask(0);
chmod($pathtologs.$ifile, 0777);
umask($old);
// return FALSE;
}
if ($handle = fopen($pathtologs.$pfile, 'w'))
{
fwrite($handle, $data);
}
fclose($handle);
$data = "<?php
/* e107 website system: Log info file: ".date("z:Y", time())." */
";
$data .= '$domainInfo'." = array();\n\n";
$data .= '$screenInfo'." = array();\n\n";
$data .= '$browserInfo'." = array();\n\n";
$data .= '$osInfo'." = array();\n\n";
$data .= '$refInfo'." = array();\n\n";
$data .= '$searchInfo'." = array();\n\n";
$data .= '$visitInfo'." = array();\n\n";
$data .= "?>";
if ($handle = fopen($pathtologs.$ifile, 'w'))
{
fwrite($handle, $data);
}
fclose($handle);
return;
}
/**
* Called if both today's and yesterday's log files missing, to see
* if there are any older files we could process. Return FALSE if nothing
* Otherwise return a string of relevant information
* @param $pathtologs
* @return bool|string
*/
function check_for_old_files($pathtologs)
{
$no_files = TRUE;
if ($dir_handle = opendir($pathtologs))
{
while (false !== ($file = readdir($dir_handle)))
{
// Do match on #^logp_(\d{1,3})\.php$#i
if (preg_match('#^logp_(\d{1,3}\.\d{4})\.php$#i',$file,$match) == 1)
{ // got a matching file
$yesterday = $match[1]; // Day of year - zero is 1st Jan
$pfileprev = "logp_".$yesterday.".php"; // Yesterday's log file
$ifileprev = "logi_".$yesterday.".php";
list($day,$year) = explode('.',$yesterday);
$tstamp = mktime(0,0,0,1,1,$year) + ($day*86400);
$date2 = date("Y-m-j", $tstamp); // Yesterday's date for the database summary
$temp = array($pfileprev,$ifileprev,$date2,$tstamp);
return implode('|',$temp);
}
}
}
return FALSE;
}
// for future use.
function collate($pfile)
{
if(is_readable(e_LOG.$pfile))
{
require(e_LOG.$pfile); // contains $pageInfo;
}
else
{
return false;
}
}
/**
* @param $url
* @param bool $logQry
* @param string $err_code
* @return bool|mixed|string
*/
function getPageKey($url,$logQry=false,$err_code='')
{
$pageDisallow = "cache|file|eself|admin";
$tagRemove = "(\\\)|(\s)|(\')|(\")|(eself)|(&nbsp;)|(\.php)|(\.html)";
$tagRemove2 = "(\\\)|(\s)|(\')|(\")|(eself)|(&nbsp;)";
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 == "")
{
return "index";
}
if(preg_match("/".$pageDisallow."/i", $pageName))
{
return false;
}
if ($logQry)
{
$pageName .= '+'.$match[3]; // All queries match
}
$pageName = $err_code.$pageName; // Add the error code at the beginning, so its treated uniquely
return $pageName;
}
/**
* Fix corrupted page data.
* Re-calculate all page totals from all existing database information and save to DB as 'pageTotal'. .
*/
function collatePageTotalDB()
{
$sql = e107::getDb();
$qry = "SELECT * FROM `#logstats` WHERE `log_id` REGEXP '^[0-9]' AND `log_data` LIKE '%http%'";
$data = $sql->retrieve($qry,true);
$pageTotal = array();
foreach($data as $values)
{
$tmp = explode(chr(1),$values['log_data']);
unset($tmp[0],$tmp[1]);
$thisTotal = array();
foreach($tmp as $val)
{
if(!empty($val))
{
list($url,$ttl,$unq) = explode("|",$val);
$key = $this->getPageKey($url);
$thisTotal[$key]['url'] = $url;
$thisTotal[$key]['ttlv'] += $ttl;
$thisTotal[$key]['unqv'] += $unq;
$pageTotal[$key]['url'] = $url;
$pageTotal[$key]['ttlv'] += $ttl;
$pageTotal[$key]['unqv'] += $unq;
}
}
// echo "<h3>".$values['log_id']."</h3>";
// print_a($thisTotal);
}
if(empty($pageTotal))
{
return false;
}
$id = $sql->retrieve('logstats','log_uniqueid', "log_id='pageTotal'");
$insertData = array(
'log_uniqueid' => intval($id),
'log_id'=> 'pageTotal',
'log_data'=> serialize($pageTotal)
);
// echo "<h2>Total</h2>";
// print_a($pageTotal);
return $sql->replace('logstats', $insertData);
}
/**
* collate page total information using today's data and totals stored in DB.
* @param $pageInfo - from today's file.
*/
function collatePageTotal($pageInfo=array())
{
$sql = e107::getDb();
if($sql->select("logstats", "*", "log_id='pageTotal' "))
{
$tmp = $sql->fetch();
$pageTotal = unserialize($tmp['log_data']);
$uniqueID = $tmp['log_uniqueid'];
unset($tmp);
}
else
{
$pageTotal = array();
$uniqueID = 0;
}
// echo "<h3>DB Totals</h3>";
// print_a($pageTotal);
// echo "<h3>From File</h3>";
// print_a($pageInfo);
foreach($pageInfo as $key => $info)
{
$pageTotal[$key]['url'] = $info['url'];
$pageTotal[$key]['ttlv'] += $info['ttl'];
$pageTotal[$key]['unqv'] += $info['unq'];
}
// echo "<h3>Consilidated</h3>";
// print_a($pageTotal);
if(empty($pageTotal))
{
return false;
}
$insertData = array(
'log_uniqueid' => intval($uniqueID),
'log_id' => 'pageTotal',
'log_data' => serialize($pageTotal)
);
return $sql->replace('logstats', $insertData);
}
/**
* Collate individual page information into an array
* @param $pageInfo
*/
function collatePageInfo($pageInfo)
{
global $date2;
$sql = e107::getDb();
$tp = e107::getParser();
$data = "";
$dailytotal = 0;
$uniquetotal = 0;
foreach($pageInfo as $key => $value)
{
$data .= $value['url']."|".$value['ttl']."|".$value['unq'].chr(1);
$dailytotal += $value['ttl'];
$uniquetotal += $value['unq'];
}
$data = $dailytotal.chr(1).$uniquetotal.chr(1) . $data;
$sql->insert("logstats", "0, '$date2', '".$tp -> toDB($data, true)."'");
}
/**
* Reset (empty) yesterday's log files.
* @return bool
*/
function resetLogFiles()
{
global $pathtologs, $pfileprev, $ifileprev;
if(empty($pfileprev) || empty($ifileprev))
{
return false;
}
/* ok, we're finished with the log file now, we can empty it ... */
if(!unlink($pathtologs.$pfileprev))
{
$data = chr(60)."?php\n". chr(47)."* e107 website system: Log file: ".date("z:Y", time())." *". chr(47)."\n\n\n\n".chr(47)."* THE INFORMATION IN THIS LOG FILE HAS BEEN CONSOLIDATED INTO THE DATABASE - YOU CAN SAFELY DELETE IT. *". chr(47)."\n\n\n?". chr(62);
if($handle = fopen($pathtologs.$pfileprev, 'w'))
{
fwrite($handle, $data);
}
fclose($handle);
}
if(!unlink($pathtologs.$ifileprev))
{
$data = chr(60)."?php\n". chr(47)."* e107 website system: Log file: ".date("z:Y", time())." *". chr(47)."\n\n\n\n".chr(47)."* THE INFORMATION IN THIS LOG INFO FILE HAS BEEN CONSOLIDATED INTO THE DATABASE - YOU CAN SAFELY DELETE IT. *". chr(47)."\n\n\n?". chr(62);
if($handle = fopen($pathtologs.$ifileprev, 'w'))
{
fwrite($handle, $data);
}
fclose($handle);
}
}
}
$lgc = new logConsolidate();
if(file_exists($pathtologs.$pfile)) /* log file is up to date, no consolidation required */ if(file_exists($pathtologs.$pfile)) /* log file is up to date, no consolidation required */
{ {
return; return;
} }
else if(!file_exists($pathtologs.$pfileprev)) // See if any older log files else if(!file_exists($pathtologs.$pfileprev)) // See if any older log files
{ {
if (($retvalue = check_for_old_files($pathtologs)) === FALSE) /* no logfile found at all - create - this will only ever happen once ... */ if (($retvalue = $lgc->check_for_old_files($pathtologs)) === FALSE) /* no logfile found at all - create - this will only ever happen once ... */
{ {
createLog($pathtologs); $lgc->createLog($pathtologs);
return FALSE; return FALSE;
} }
@@ -228,178 +599,20 @@ else
} }
/* collate page total information */
if($sql->select("logstats", "*", "log_id='pageTotal' "))
{
$tmp = $sql->fetch();
$pageTotal = unserialize($tmp['log_data']);
unset($tmp);
}
else
{
$pageTotal = array();
}
foreach($pageInfo as $key => $info)
{
$pageTotal[$key]['url'] = $info['url'];
$pageTotal[$key]['ttlv'] += $info['ttl'];
$pageTotal[$key]['unqv'] += $info['unq'];
}
$pagetotal = serialize($pageTotal);
$insertPageTotal = array('log_data'=> $pageTotal, 'WHERE' => "log_id='pageTotal'");
$sql->replace('logstats', $insertPageTotal);
/*
if(!$sql->update("logstats", "log_data='{$pagetotal}' WHERE log_id='pageTotal' "))
{
$sql->insert("logstats", "0, 'pageTotal', '{$pagetotal}' ");
}*/
/* now we need to collate the individual page information into an array ... */
$data = "";
$dailytotal = 0;
$uniquetotal = 0;
foreach($pageInfo as $key => $value)
{
$data .= $value['url']."|".$value['ttl']."|".$value['unq'].chr(1);
$dailytotal += $value['ttl'];
$uniquetotal += $value['unq'];
}
$data = $dailytotal.chr(1).$uniquetotal.chr(1) . $data; $lgc->collatePageTotal($pageInfo);
$sql->insert("logstats", "0, '$date2', '".$tp -> toDB($data, true)."'");
$lgc->collatePageInfo($pageInfo);
$lgc->resetLogFiles();
/* ok, we're finished with the log file now, we can empty it ... */
if(!unlink($pathtologs.$pfileprev))
{
$data = chr(60)."?php\n". chr(47)."* e107 website system: Log file: ".date("z:Y", time())." *". chr(47)."\n\n\n\n".chr(47)."* THE INFORMATION IN THIS LOG FILE HAS BEEN CONSOLIDATED INTO THE DATABASE - YOU CAN SAFELY DELETE IT. *". chr(47)."\n\n\n?". chr(62);
if ($handle = fopen($pathtologs.$pfileprev, 'w')) {
fwrite($handle, $data);
}
fclose($handle);
}
if(!unlink($pathtologs.$ifileprev))
{
$data = chr(60)."?php\n". chr(47)."* e107 website system: Log file: ".date("z:Y", time())." *". chr(47)."\n\n\n\n".chr(47)."* THE INFORMATION IN THIS LOG INFO FILE HAS BEEN CONSOLIDATED INTO THE DATABASE - YOU CAN SAFELY DELETE IT. *". chr(47)."\n\n\n?". chr(62);
if ($handle = fopen($pathtologs.$ifileprev, 'w')) {
fwrite($handle, $data);
}
fclose($handle);
}
/* and finally, we need to create new logfiles for today ... */ /* and finally, we need to create new logfiles for today ... */
createLog($pathtologs); $lgc->createLog($pathtologs,$statTotal,$statUnique);
/* done! */
function createLog($pathtologs)
{
global $statTotal, $statUnique, $pfile, $ifile;
if(!is_writable($pathtologs))
{
echo "Log directory is not writable - please CHMOD ".e_LOG." to 777";
echo '<br />Path to logs: '.$pathtologs;
return FALSE;
}
$varStart = chr(36);
$quote = chr(34);
$data = chr(60)."?php\n". chr(47)."* e107 website system: Log file: ".date("z:Y", time())." *". chr(47)."\n\n".
$varStart."refererData = ".$quote.$quote.";\n".
$varStart."ipAddresses = ".$quote.$quote.";\n".
$varStart."hosts = ".$quote.$quote.";\n".
$varStart."siteTotal = ".$quote."0".$quote.";\n".
$varStart."siteUnique = ".$quote."0".$quote.";\n".
$varStart."screenInfo = array();\n".
$varStart."browserInfo = array();\n".
$varStart."osInfo = array();\n".
$varStart."pageInfo = array(\n";
$data .= "\n);\n\n?". chr(62);
if(!touch($pathtologs.$pfile)) {
return FALSE;
}
if(!touch($pathtologs.$ifile)) {
return FALSE;
}
if(!is_writable($pathtologs.$pfile)) {
$old = umask(0);
chmod($pathtologs.$pfile, 0777);
umask($old);
// return FALSE;
}
if(!is_writable($pathtologs.$ifile)) {
$old = umask(0);
chmod($pathtologs.$ifile, 0777);
umask($old);
// return FALSE;
}
if ($handle = fopen($pathtologs.$pfile, 'w'))
{
fwrite($handle, $data);
}
fclose($handle);
$data = "<?php
/* e107 website system: Log info file: ".date("z:Y", time())." */
";
$data .= '$domainInfo'." = array();\n\n";
$data .= '$screenInfo'." = array();\n\n";
$data .= '$browserInfo'." = array();\n\n";
$data .= '$osInfo'." = array();\n\n";
$data .= '$refInfo'." = array();\n\n";
$data .= '$searchInfo'." = array();\n\n";
$data .= '$visitInfo'." = array();\n\n";
$data .= "?>";
if ($handle = fopen($pathtologs.$ifile, 'w'))
{
fwrite($handle, $data);
}
fclose($handle);
return;
}
// Called if both today's and yesterday's log files missing, to see
// if there are any older files we could process. Return FALSE if nothing
// Otherwise return a string of relevant information
function check_for_old_files($pathtologs)
{
$no_files = TRUE;
if ($dir_handle = opendir($pathtologs))
{
while (false !== ($file = readdir($dir_handle)))
{
// Do match on #^logp_(\d{1,3})\.php$#i
if (preg_match('#^logp_(\d{1,3}\.\d{4})\.php$#i',$file,$match) == 1)
{ // got a matching file
$yesterday = $match[1]; // Day of year - zero is 1st Jan
$pfileprev = "logp_".$yesterday.".php"; // Yesterday's log file
$ifileprev = "logi_".$yesterday.".php";
list($day,$year) = explode('.',$yesterday);
$tstamp = mktime(0,0,0,1,1,$year) + ($day*86400);
$date2 = date("Y-m-j", $tstamp); // Yesterday's date for the database summary
$temp = array($pfileprev,$ifileprev,$date2,$tstamp);
return implode('|',$temp);
}
}
}
return FALSE;
}
?> ?>

View File

@@ -12,12 +12,14 @@ class log_shortcodes extends e_shortcode
private $dbPageInfo; private $dbPageInfo;
function __construct($order) function __construct()
{ {
$sql = e107::getDB(); $sql = e107::getDB();
$logfile = e_LOG.'logp_'.date('z.Y', time()).'.php'; /* get today's logfile ... */ $logfile = e_LOG.'logp_'.date('z.Y', time()).'.php'; /* get today's logfile ... */
if(is_readable($logfile)) $pageInfo = array();
if(is_readable($logfile)) // populate $pageInfo
{ {
require($logfile); require($logfile);
} }
@@ -27,6 +29,7 @@ class log_shortcodes extends e_shortcode
if(is_readable($logfile)) if(is_readable($logfile))
{ {
require($logfile); require($logfile);
// e107::getMessage()->addDebug("Loading Log File: ".$logfile);
} }
@@ -34,6 +37,7 @@ class log_shortcodes extends e_shortcode
{ {
$row = $sql->fetch(); $row = $sql->fetch();
$this->dbPageInfo = unserialize($row['log_data']); $this->dbPageInfo = unserialize($row['log_data']);
// e107::getMessage()->addDebug("Loading Logstats from DB: ".print_a($this->dbPageInfo,true));
} }
else else
{ {

View File

@@ -186,25 +186,52 @@ if($ref && !strstr($ref, $_SERVER['HTTP_HOST']))
} }
$pageDisallow = "cache|file|eself|admin";
$tagRemove = "(\\\)|(\s)|(\')|(\")|(eself)|(&nbsp;)|(\.php)|(\.html)";
$tagRemove2 = "(\\\)|(\s)|(\')|(\")|(eself)|(&nbsp;)";
preg_match("#/(.*?)(\?|$)(.*)#si", $self, $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 == "") $pageName = "index";
if(preg_match("/".$pageDisallow."/i", $pageName)) return;
if ($logQry)
function logGetPageKey($url,$logQry=false,$err_code='')
{ {
$pageName .= '+'.$match[3]; // All queries match $pageDisallow = "cache|file|eself|admin";
$tagRemove = "(\\\)|(\s)|(\')|(\")|(eself)|(&nbsp;)|(\.php)|(\.html)";
$tagRemove2 = "(\\\)|(\s)|(\')|(\")|(eself)|(&nbsp;)";
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 == "")
{
return "index";
}
if(preg_match("/".$pageDisallow."/i", $pageName))
{
return false;
}
if ($logQry)
{
$pageName .= '+'.$match[3]; // All queries match
}
$pageName = $err_code.$pageName; // Add the error code at the beginning, so its treated uniquely
return $pageName;
} }
$pageName = $err_code.$pageName; // Add the error code at the beginning, so its treated uniquely
if(!$pageName = logGetPageKey($self,$logQry,$err_code))
{
return;
}
//$logfp = fopen(e_LOG.'rcvstring.txt', 'a+'); fwrite($logfp, $pageName."\n"); fclose($logfp); //$logfp = fopen(e_LOG.'rcvstring.txt', 'a+'); fwrite($logfp, $pageName."\n"); fclose($logfp);
$p_handle = fopen($logPfile, 'r+'); $p_handle = fopen($logPfile, 'r+');

View File

@@ -1964,7 +1964,7 @@ class siteStats
* *
* @return string text to be displayed * @return string text to be displayed
*/ */
function bar($percen, $val,$name) function bar($percen, $val,$name='')
{ {
if(deftrue('BOOTSTRAP')) if(deftrue('BOOTSTRAP'))
{ {