migrated traffic-cron-functions to new PDO database class, refs #1287

Signed-off-by: Michael Kaufmann (d00p) <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann (d00p)
2013-11-18 09:23:09 +01:00
parent bb1561b628
commit e2ca3873a8

View File

@@ -17,24 +17,22 @@
* *
*/ */
function awstatsDoSingleDomain($domain, $outputdir) function awstatsDoSingleDomain($domain, $outputdir) {
{
global $cronlog, $settings, $theme; global $cronlog, $settings, $theme;
$returnval = 0; $returnval = 0;
$domainconfig = makeCorrectFile($settings['system']['awstats_conf'].'/awstats.' . $domain . '.conf'); $domainconfig = makeCorrectFile($settings['system']['awstats_conf'].'/awstats.' . $domain . '.conf');
if(file_exists($domainconfig))
{ if (file_exists($domainconfig)) {
$outputdir = makeCorrectDir($outputdir . '/' . $domain); $outputdir = makeCorrectDir($outputdir . '/' . $domain);
if(!is_dir($outputdir)) if (!is_dir($outputdir)) {
{
safe_exec('mkdir -p ' . escapeshellarg($outputdir)); safe_exec('mkdir -p ' . escapeshellarg($outputdir));
} }
/** //check for correct path of awstats_buildstaticpages.pl
* check for correct path of awstats_buildstaticpages.pl
*/
$awbsp = makeCorrectFile($settings['system']['awstats_path'].'/awstats_buildstaticpages.pl'); $awbsp = makeCorrectFile($settings['system']['awstats_path'].'/awstats_buildstaticpages.pl');
$awprog = makeCorrectFile($settings['system']['awstats_awstatspath'].'/awstats.pl'); $awprog = makeCorrectFile($settings['system']['awstats_awstatspath'].'/awstats.pl');
@@ -47,46 +45,43 @@ function awstatsDoSingleDomain($domain, $outputdir)
$cronlog->logAction(CRON_ACTION, LOG_INFO, "Running awstats_buildstaticpages.pl for domain '".$domain."' (Output: '".$outputdir."')"); $cronlog->logAction(CRON_ACTION, LOG_INFO, "Running awstats_buildstaticpages.pl for domain '".$domain."' (Output: '".$outputdir."')");
safe_exec($awbsp.' -awstatsprog='.escapeshellarg($awprog).' -update -month=' . date('n') . ' -year=' . date('Y') . ' -config=' . $domain . ' -dir='.escapeshellarg($outputdir)); safe_exec($awbsp.' -awstatsprog='.escapeshellarg($awprog).' -update -month=' . date('n') . ' -year=' . date('Y') . ' -config=' . $domain . ' -dir='.escapeshellarg($outputdir));
/** // index file is saved like 'awstats.[domain].html',
* index file is saved like 'awstats.[domain].html', // so link a index.html to it
* so link a index.html to it
*/
$original_index = makeCorrectFile($outputdir.'/awstats.'.$domain.'.html'); $original_index = makeCorrectFile($outputdir.'/awstats.'.$domain.'.html');
$new_index = makeCorrectFile($outputdir.'/index.html'); $new_index = makeCorrectFile($outputdir.'/index.html');
if(!file_exists($new_index)) { if (!file_exists($new_index)) {
safe_exec('ln -s '.escapeshellarg($original_index).' '.escapeshellarg($new_index)); safe_exec('ln -s '.escapeshellarg($original_index).' '.escapeshellarg($new_index));
} }
/** //statistics file looks like: 'awstats[month][year].[domain].txt'
* statistics file looks like: 'awstats[month][year].[domain].txt'
*/
$file = makeCorrectFile($outputdir.'/awstats'.date('mY', time()).'.'.$domain.'.txt'); $file = makeCorrectFile($outputdir.'/awstats'.date('mY', time()).'.'.$domain.'.txt');
$cronlog->logAction(CRON_ACTION, LOG_INFO, "Gathering traffic information from '".$file."'"); $cronlog->logAction(CRON_ACTION, LOG_INFO, "Gathering traffic information from '".$file."'");
if (file_exists($file)) { if (file_exists($file)) {
$content = @file_get_contents($file); $content = @file_get_contents($file);
if ($content !== false) { if ($content !== false) {
$content_array = explode("\n", $content); $content_array = explode("\n", $content);
$count_bdw = false; $count_bdw = false;
foreach($content_array as $line) foreach ($content_array as $line) {
{ // skip empty lines and comments
if(trim($line) == '' // skip empty lines if (trim($line) == ''
|| substr(trim($line), 0, 1) == '#' // skip comments || substr(trim($line), 0, 1) == '#'
) { ) {
continue; continue;
} }
$parts = explode(' ', $line); $parts = explode(' ', $line);
if(isset($parts[0]) if (isset($parts[0])
&& strtoupper($parts[0]) == 'BEGIN_DOMAIN' && strtoupper($parts[0]) == 'BEGIN_DOMAIN'
) { ) {
$count_bdw = true; $count_bdw = true;
} }
if ($count_bdw) { if ($count_bdw) {
if(isset($parts[0]) if (isset($parts[0])
&& strtoupper($parts[0]) == 'END_DOMAIN' && strtoupper($parts[0]) == 'END_DOMAIN'
) { ) {
$count_bdw = false; $count_bdw = false;
@@ -102,19 +97,19 @@ function awstatsDoSingleDomain($domain, $outputdir)
return $returnval; return $returnval;
} }
function callAwstatsGetTraffic($customerid, $outputdir, $usersdomainlist) function callAwstatsGetTraffic($customerid, $outputdir, $usersdomainlist) {
{
global $settings, $db, $cronlog, $theme; global $cronlog;
$returnval = 0; $returnval = 0;
foreach($usersdomainlist as $domainid => $singledomain) foreach ($usersdomainlist as $domainid => $singledomain) {
{
// as we check for the config-model awstats will only parse // as we check for the config-model awstats will only parse
// 'real' domains and no subdomains which are aliases in the // 'real' domains and no subdomains which are aliases in the
// model-config-file. // model-config-file.
$returnval += awstatsDoSingleDomain($singledomain, $outputdir); $returnval += awstatsDoSingleDomain($singledomain, $outputdir);
} }
/** /**
* as of #124, awstats traffic is saved in bytes instead * as of #124, awstats traffic is saved in bytes instead
* of kilobytes (like webalizer does) * of kilobytes (like webalizer does)
@@ -127,14 +122,21 @@ function callAwstatsGetTraffic($customerid, $outputdir, $usersdomainlist)
* a sane value for our panel_traffic and to remain the whole stats * a sane value for our panel_traffic and to remain the whole stats
* (awstats overwrites the customers .html stats-files) * (awstats overwrites the customers .html stats-files)
*/ */
if ($customerid !== false) {
if($customerid !== false)
{ $result_stmt = Database::prepare("
$result = $db->query_first("SELECT SUM(`http`) as `trafficmonth` FROM `" . TABLE_PANEL_TRAFFIC . "` SELECT SUM(`http`) as `trafficmonth` FROM `" . TABLE_PANEL_TRAFFIC . "`
WHERE `customerid` = '".(int)$customerid."' WHERE `customerid` = :customerid
AND `year`='".date('Y', time())."' AND `year` = :year AND `month` = :month
AND `month`='".date('m', time())."'"); ");
if(is_array($result) $result_data = array(
'customerid' => $customerid,
'year' => date('Y', time()),
'month' => date('m', time())
);
$result = Database::pexecute_first($result_stmt, $result_data);
if (is_array($result)
&& isset($result['trafficmonth']) && isset($result['trafficmonth'])
) { ) {
$returnval = ($returnval - floatval($result['trafficmonth'])); $returnval = ($returnval - floatval($result['trafficmonth']));
@@ -153,60 +155,51 @@ function callAwstatsGetTraffic($customerid, $outputdir, $usersdomainlist)
* @return int Used traffic * @return int Used traffic
* @author Florian Lippert <flo@syscp.org> * @author Florian Lippert <flo@syscp.org>
*/ */
function callWebalizerGetTraffic($logfile, $outputdir, $caption, $usersdomainlist) {
global $settings, $cronlog;
function callWebalizerGetTraffic($logfile, $outputdir, $caption, $usersdomainlist)
{
global $settings, $cronlog, $theme;
$returnval = 0; $returnval = 0;
if(file_exists($settings['system']['logfiles_directory'] . $logfile . '-access.log')) $logfile = makeCorrectFile($settings['system']['logfiles_directory'] . $logfile . '-access.log');
{ if (file_exists($logfile)) {
$domainargs = ''; $domainargs = '';
foreach($usersdomainlist as $domainid => $domain) foreach ($usersdomainlist as $domainid => $domain) {
{
// hide referer // hide referer
$domainargs.= ' -r ' . escapeshellarg($domain); $domainargs.= ' -r ' . escapeshellarg($domain);
} }
$outputdir = makeCorrectDir($outputdir); $outputdir = makeCorrectDir($outputdir);
if (!file_exists($outputdir)) {
if(!file_exists($outputdir))
{
safe_exec('mkdir -p ' . escapeshellarg($outputdir)); safe_exec('mkdir -p ' . escapeshellarg($outputdir));
} }
if(file_exists($outputdir . 'webalizer.hist.1')) if (file_exists($outputdir . 'webalizer.hist.1')) {
{ @unlink($outputdir . 'webalizer.hist.1');
unlink($outputdir . 'webalizer.hist.1');
} }
if(file_exists($outputdir . 'webalizer.hist') if (file_exists($outputdir . 'webalizer.hist')
&& !file_exists($outputdir . 'webalizer.hist.1')) && !file_exists($outputdir . 'webalizer.hist.1')
{ ) {
safe_exec('cp ' . escapeshellarg($outputdir . 'webalizer.hist') . ' ' . escapeshellarg($outputdir . 'webalizer.hist.1')); safe_exec('cp ' . escapeshellarg($outputdir . 'webalizer.hist') . ' ' . escapeshellarg($outputdir . 'webalizer.hist.1'));
} }
$verbosity = ''; $verbosity = '';
if ($settings['system']['webalizer_quiet'] == '1') {
if($settings['system']['webalizer_quiet'] == '1')
{
$verbosity = '-q'; $verbosity = '-q';
} } elseif($settings['system']['webalizer_quiet'] == '2') {
elseif($settings['system']['webalizer_quiet'] == '2')
{
$verbosity = '-Q'; $verbosity = '-Q';
} }
$we = '/usr/bin/webalizer'; $we = '/usr/bin/webalizer';
// FreeBSD uses other paths, #140 // FreeBSD uses other paths, #140
if(!file_exists($we)) if (!file_exists($we)) {
{
$we = '/usr/local/bin/webalizer'; $we = '/usr/local/bin/webalizer';
} }
$cronlog->logAction(CRON_ACTION, LOG_INFO, "Running webalizer for domain '".$caption."'"); $cronlog->logAction(CRON_ACTION, LOG_INFO, "Running webalizer for domain '".$caption."'");
safe_exec($we . ' ' . $verbosity . ' -p -o ' . escapeshellarg($outputdir) . ' -n ' . escapeshellarg($caption) . $domainargs . ' ' . escapeshellarg($settings['system']['logfiles_directory'] . $logfile . '-access.log')); safe_exec($we . ' ' . $verbosity . ' -p -o ' . escapeshellarg($outputdir) . ' -n ' . escapeshellarg($caption) . $domainargs . ' ' . escapeshellarg($logfile));
/** /**
* Format of webalizer.hist-files: * Format of webalizer.hist-files:
@@ -214,27 +207,25 @@ function callWebalizerGetTraffic($logfile, $outputdir, $caption, $usersdomainlis
* Year: $webalizer_hist_row['1'] * Year: $webalizer_hist_row['1']
* KB: $webalizer_hist_row['5'] * KB: $webalizer_hist_row['5']
*/ */
$httptraffic = array(); $httptraffic = array();
$webalizer_hist = @file_get_contents($outputdir . 'webalizer.hist'); $webalizer_hist = @file_get_contents($outputdir . 'webalizer.hist');
$cronlog->logAction(CRON_ACTION, LOG_INFO, "Gathering traffic information from '".$webalizer_hist."'"); $cronlog->logAction(CRON_ACTION, LOG_INFO, "Gathering traffic information from '".$webalizer_hist."'");
$webalizer_hist_rows = explode("\n", $webalizer_hist); $webalizer_hist_rows = explode("\n", $webalizer_hist);
foreach($webalizer_hist_rows as $webalizer_hist_row) foreach ($webalizer_hist_rows as $webalizer_hist_row) {
{ if ($webalizer_hist_row != '') {
if($webalizer_hist_row != '')
{
$webalizer_hist_row = explode(' ', $webalizer_hist_row); $webalizer_hist_row = explode(' ', $webalizer_hist_row);
if(isset($webalizer_hist_row['0']) if (isset($webalizer_hist_row['0'])
&& isset($webalizer_hist_row['1']) && isset($webalizer_hist_row['1'])
&& isset($webalizer_hist_row['5'])) && isset($webalizer_hist_row['5'])
{ ) {
$month = intval($webalizer_hist_row['0']); $month = intval($webalizer_hist_row['0']);
$year = intval($webalizer_hist_row['1']); $year = intval($webalizer_hist_row['1']);
$traffic = floatval($webalizer_hist_row['5']); $traffic = floatval($webalizer_hist_row['5']);
if(!isset($httptraffic[$year])) if (!isset($httptraffic[$year])) {
{
$httptraffic[$year] = array(); $httptraffic[$year] = array();
} }
@@ -247,23 +238,22 @@ function callWebalizerGetTraffic($logfile, $outputdir, $caption, $usersdomainlis
$httptrafficlast = array(); $httptrafficlast = array();
$webalizer_lasthist = @file_get_contents($outputdir . 'webalizer.hist.1'); $webalizer_lasthist = @file_get_contents($outputdir . 'webalizer.hist.1');
$cronlog->logAction(CRON_ACTION, LOG_INFO, "Gathering traffic information from '".$webalizer_lasthist."'"); $cronlog->logAction(CRON_ACTION, LOG_INFO, "Gathering traffic information from '".$webalizer_lasthist."'");
$webalizer_lasthist_rows = explode("\n", $webalizer_lasthist); $webalizer_lasthist_rows = explode("\n", $webalizer_lasthist);
foreach($webalizer_lasthist_rows as $webalizer_lasthist_row) foreach ($webalizer_lasthist_rows as $webalizer_lasthist_row) {
{ if ($webalizer_lasthist_row != '') {
if($webalizer_lasthist_row != '')
{
$webalizer_lasthist_row = explode(' ', $webalizer_lasthist_row); $webalizer_lasthist_row = explode(' ', $webalizer_lasthist_row);
if(isset($webalizer_lasthist_row['0']) if (isset($webalizer_lasthist_row['0'])
&& isset($webalizer_lasthist_row['1']) && isset($webalizer_lasthist_row['1'])
&& isset($webalizer_lasthist_row['5'])) && isset($webalizer_lasthist_row['5'])
{ ) {
$month = intval($webalizer_lasthist_row['0']); $month = intval($webalizer_lasthist_row['0']);
$year = intval($webalizer_lasthist_row['1']); $year = intval($webalizer_lasthist_row['1']);
$traffic = floatval($webalizer_lasthist_row['5']); $traffic = floatval($webalizer_lasthist_row['5']);
if(!isset($httptrafficlast[$year])) if (!isset($httptrafficlast[$year])) {
{
$httptrafficlast[$year] = array(); $httptrafficlast[$year] = array();
} }
@@ -273,16 +263,11 @@ function callWebalizerGetTraffic($logfile, $outputdir, $caption, $usersdomainlis
} }
reset($httptrafficlast); reset($httptrafficlast);
foreach($httptraffic as $year => $months) foreach ($httptraffic as $year => $months) {
{ foreach ($months as $month => $traffic) {
foreach($months as $month => $traffic) if (!isset($httptrafficlast[$year][$month])) {
{
if(!isset($httptrafficlast[$year][$month]))
{
$returnval+= $traffic; $returnval+= $traffic;
} } elseif($httptrafficlast[$year][$month] < $httptraffic[$year][$month]) {
elseif($httptrafficlast[$year][$month] < $httptraffic[$year][$month])
{
$returnval+= ($httptraffic[$year][$month] - $httptrafficlast[$year][$month]); $returnval+= ($httptraffic[$year][$month] - $httptrafficlast[$year][$month]);
} }
} }