From 39bc773476e0ec48110d375aa412bf1d9b16d7ab Mon Sep 17 00:00:00 2001
From: tbrehm <t.brehm@ispconfig.org>
Date: Wed, 7 Sep 2011 14:17:57 +0000
Subject: [PATCH] Fixed: FS#1690 - Awstats shows only the last month.

---
 server/conf/awstats_index.php.master | 69 ++++++++++++++++++++++++++++
 server/cron_daily.php                | 36 ++++++++++++++-
 2 files changed, 103 insertions(+), 2 deletions(-)
 create mode 100644 server/conf/awstats_index.php.master

diff --git a/server/conf/awstats_index.php.master b/server/conf/awstats_index.php.master
new file mode 100644
index 0000000000..09fcde0536
--- /dev/null
+++ b/server/conf/awstats_index.php.master
@@ -0,0 +1,69 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
+<title>AWAstats Statistics</title>
+<script>
+function load_content(url)
+{
+    var iframe = document.getElementById("content");
+    iframe.src = url;
+}
+</script>
+</head>
+<body>
+<?php
+$aw['aw_jump_text'] = 'Jump to previous stats: ';
+$aw['aw_renamed_index'] = 'awsindex.html';
+
+if ($handle = opendir('.'))
+{
+        while(false !== ($file = readdir($handle)))
+        {
+                if (substr($file,0,1) != "." && is_dir($file))
+                {
+                        $awprev[] = $file;
+                }
+        }
+
+        $month = date("m")+1-1;
+
+        if (date("d") == 1)
+        {
+                        $month = date("m")-1;
+        }
+
+        $year = date("Y");
+
+        if (date("m") == 1)
+        {
+                        $year = date("Y")-1;
+                        $month = "12";
+        }
+
+        $awprev[] = $year."-".$month;
+        closedir($handle);
+}
+
+echo '<div style="width: 97%; margin-left: 4px; height: 20px; background-color: #FFFFFF; position: fixed; padding: 7px; border: 2px solid #cccccc;><div align="left"><font color="#000000" size="2" face="Verdana, Arial, Helvetica,  sans-serif">' .$aw["aw_jump_text"]. '</font </div>';
+
+echo "<select name='awdate' onchange=\"load_content(this.value)\">";
+rsort($awprev);
+
+foreach ($awprev as $key => $value)
+{
+        if($key == 0)
+        {
+                echo "<option selected=\"selected\" value=\"".$aw['aw_renamed_index']."\"> $value</option>";
+        }
+        else
+        {
+                echo "<option value='$value/".$aw['aw_renamed_index']."'> $value</option>";
+        }
+}
+
+echo '</select></div><iframe src="'.$aw['aw_renamed_index'].'" frameborder="0" scrolling="Yes" width="100%" height="100%" style="margin-top:25px" id="content"></iframe>';
+
+?>
+</body>
+</html>
\ No newline at end of file
diff --git a/server/cron_daily.php b/server/cron_daily.php
index e21520c5c0..571a02bdf3 100644
--- a/server/cron_daily.php
+++ b/server/cron_daily.php
@@ -200,12 +200,44 @@ HostAliases="www.'.$domain.' localhost 127.0.0.1"'.$aliasdomain;
 	if(is_link('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log')) unlink('/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
 	symlink($logfile,'/var/log/ispconfig/httpd/'.$domain.'/yesterday-access.log');
 	
+	$awmonth = date("m");
+	if (date("d") == 1)	{
+		$awmonth = date("m")-1;
+	}
+
+	$awyear = date("Y");
+	if (date("m") == 1) {
+		$awyear = date("Y")-1;
+		$awmonth = "12";
+	}
+	
 	// awstats_buildstaticpages.pl -update -config=mydomain.com -lang=en -dir=/var/www/domain.com/web/stats -awstatsprog=/path/to/awstats.pl
-	$command = "$awstats_buildstaticpages_pl -update -config='$domain' -lang=".$conf['language']." -dir='$statsdir' -awstatsprog='$awstats_pl'";
+	// $command = "$awstats_buildstaticpages_pl -update -config='$domain' -lang=".$conf['language']." -dir='$statsdir' -awstatsprog='$awstats_pl'";
+	
+	$command = "$awstats_buildstaticpages_pl -month='$awmonth' -year='$awyear' -update -config='$domain' -lang=".$conf['language']." -dir='$statsdir' -awstatsprog='$awstats_pl'";
+
+	if (date("d") == 2) {
+		$awmonth = date("m")-1;
+		if (date("m") == 1) {
+			$awyear = date("Y")-1;
+			$awmonth = "12";
+		}
+
+		$statsdirold = $statsdir."/".$awyear."-".$awmonth."/";
+		mkdir($statsdirold);
+		$files = scandir($statsdir);
+		foreach ($files as $file) {
+			if (substr($file,0,1) != "." && !is_dir($file) && substr($file,0,1) != "w" && substr($file,0,1) != "i") copy("$statsdir"."/"."$file","$statsdirold"."$file");
+		}
+	}
+	
 	
 	if($awstats_pl != '' && $awstats_buildstaticpages_pl != '' && fileowner($awstats_pl) == 0 && fileowner($awstats_buildstaticpages_pl) == 0) {
 		exec($command);
-		rename($rec['document_root'].'/web/stats/awstats.'.$domain.'.html',$rec['document_root'].'/web/stats/index.html');
+		if(is_file($rec['document_root'].'/web/stats/index.html')) unlink($rec['document_root'].'/web/stats/index.html');
+		rename($rec['document_root'].'/web/stats/awstats.'.$domain.'.html',$rec['document_root'].'/web/stats/awsindex.html');
+		if(!is_file($rec['document_root']."/web/stats/index.php")) copy("/usr/local/ispconfig/server/conf/awstats_index.php.master",$rec['document_root']."/web/stats/index.php");
+		
 		$app->log('Created awstats statistics with command: '.$command,LOGLEVEL_DEBUG);
 	} else {
 		$app->log("No awstats statistics created. Either $awstats_pl or $awstats_buildstaticpages_pl is not owned by root user.",LOGLEVEL_WARN);
-- 
GitLab