<?

/**
* AWStats Totals is a simple php script to view the browser stats
* for multiple sites per month with sort options.
*
* @author      Jeroen de Jong <jeroen@telartis.nl>
* @copyright   2004 Telartis
* @version     1.5
*
* @link        http://www.telartis.nl/xcms/awstats
*
* Changelog:
* 1.0 initial version
* 1.1 use awstats language files to set your language
* 1.2 register_globals setting can be off
* 1.3 display yearly totals
* 1.4 use english messages when no language files found
* 1.5 error_reporting setting can be E_ALL
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/



/*******************************************************
* SETUP SECTION
*******************************************************/


/**
* Set this value to the directory where AWStats
* saves its database and working files into.
*/
$DirData "/var/lib/awstats";

/**
* The URL of the AWStats script.
*/
$AWStatsURL "/awstats/awstats.pl";

/**
* Set your language.
* Possible value:
*  Albanian=al, Bosnian=ba, Bulgarian=bg, Catalan=ca,
*  Chinese (Taiwan)=tw, Chinese (Simpliefied)=cn, Czech=cz, Danish=dk,
*  Dutch=nl, English=en, Estonian=et, Euskara=eu, Finnish=fi,
*  French=fr, Galician=gl, German=de, Greek=gr, Hebrew=he, Hungarian=hu,
*  Icelandic=is, Indonesian=id, Italian=it, Japanese=jp, Korean=kr,
*  Latvian=lv, Norwegian (Nynorsk)=nn, Norwegian (Bokmal)=nb, Polish=pl,
*  Portuguese=pt, Portuguese (Brazilian)=br, Romanian=ro, Russian=ru,
*  Serbian=sr, Slovak=sk,  Spanish=es, Swedish=se, Turkish=tr, Ukrainian=ua,
*  Welsh=wlk.
*/
$Lang "en";

/**
* Set the location of language files.
*/
$DirLang "/usr/local/awstats/wwwroot/cgi-bin/lang";

/**
* Config names to filter. Shows all if empty array.
*/
$FilterConfigs = array();

/*
To read website configs from database, do something like:
$sql = "SELECT config FROM users WHERE (user=...)";
$rs = mysql_query($sql); if ($rs) while ($row = mysql_fetch_array($rs)) $FilterConfigs[] = $row["config"];
*/


/*******************************************************
* PROGRAM SECTION
*******************************************************/

if (isset($_GET["year"])) $year $_GET["year"]; else $year date("Y");
if (isset(
$_GET["month"])) $month $_GET["month"]; else $month date("n");
if (isset(
$_GET["sort"])) $sort $_GET["sort"]; else $sort "bandwidth";

function 
get_config($file) {
    
$r '';
    if (
preg_match('/awstats\d{6}\.(.+)\.txt/'$file$match)) $r $match[1];
    return 
$r;
}

function 
read_history($file) {
    global 
$stats$total_stats;

    
$f fopen($file"r");
    
$s fread($ffilesize($file));
    
fclose($f);

    if (
preg_match('/\nBEGIN_BROWSER \d+\n(.*)\nEND_BROWSER\n/s'$s$match)) {
        foreach (
explode("\n"$match[1]) as $row) {
            list(
$fullbrowser$hits) = split(' '$row);
        
$browser 'Other';
        foreach (
$total_stats as $key => $value) {
        if (
strpos($fullbrowser$key) === 0)
            
$browser $key;
        }
        if (
$stats[$browser])    
            
$stats[$browser] += $hits;
        else
        
$stats[$browser] = $hits;
        
$total_stats[$browser] += $hits;
        
$stats['Totals'] += $hits;
        
$total_stats['Totals'] += $hits;
        }
    }
}

$dirfiles = array();
$dir = @opendir($DirData);
if (!
$dir) die("Could not open directory $DirData");
while (
$file readdir($dir)) $dirfiles[] = $file;

$files = array();
$configs = array();
$months = array();
if (
$month == 'all'$pat '\d{2}'; else $pat substr("0".$month, -2);
$pat '/awstats('.$pat.')'.$year.'\.(.+)\.txt/';
foreach (
$dirfiles as $file) if (preg_match($pat$file$match)) {
    
$config $match[2];
    if (!
$FilterConfigs || in_array($config$FilterConfigs)) {
        
$configs[] = $config;
    
$months[] = $match[1];
        
$files[] = $file;
    }
}

$total_stats['config'] = '';
$total_stats['msie'] = 0;
$total_stats['firefox'] = 0;
$total_stats['netscape'] = 0;
$total_stats['mozilla'] = 0;
$total_stats['camino'] = 0;
$total_stats['safari'] = 0;
$total_stats['opera'] = 0;
$total_stats['lynx'] = 0;
$total_stats['Other'] = 0;
$total_stats['Unknown'] = 0;
$total_stats['Totals'] = 0;
if (
$files) {
    
array_multisort($configs$files);
    
$all_stats = array();
    
$stats['config'] = $configs[0];
    
$stats['Totals'] = 0;
    for (
$i 0$cnt count($files); $i $cnt$i++) {
    if (
$i && $configs[$i] != $stats['config']) {
        
$all_stats[] = $stats;
        
$stats = array();
            
$stats['config'] = $configs[$i];
            
$stats['Totals'] = 0;
    }
        
read_history($DirData.'/'.$files[$i]);
    }
    
$all_stats[] = $stats;
}

function 
multisort(&$array$key) {
   
$cmp create_function('$a, $b',
       
'if ($a["'.$key.'"] == $b["'.$key.'"]) return 0;'.
       
'return ($a["'.$key.'"] > $b["'.$key.'"]) ? -1 : 1;');
   
usort($array$cmp);
}

foreach (
$total_stats as $key => $value) {
    if (
$key != 'Totals') {
        
$total_stats[$key] = (int) ($total_stats[$key] * 100 $total_stats['Totals']);
    }
}
foreach (
$all_stats as $stats) {
    foreach (
$total_stats as $key => $value) {
        if (
$key != 'Totals' && $key != 'config') {
        if (
$stats['Totals'])
            
$stats[$key] = (int) ($stats[$key] * 100 $stats['Totals']);
        else
            
$stats[$key] = 0;
    }
    }
    
$new_stats[] = $stats;
}
$all_stats $new_stats;

if (
$sort == "config"sort($all_stats); else multisort($all_stats$sort);

function 
read_language_data($file) {
    
$r = array();
    
$lines file($file);
    foreach (
$lines as $line)
        if (
preg_match('/^message(\d+)=(.*)$/'$line$match))
            
$r[$match[1]] = $match[2];
    return 
$r;
}

$message read_language_data("$DirLang/awstats-$Lang.txt");

if (!
$message) {
    
$message[7]   = 'Statistics for';
    
$message[10]  = 'Number of visits';
    
$message[11]  = 'Unique visitors';
    
$message[56]  = 'Pages';
    
$message[57]  = 'Hits';
    
$message[60]  = 'Jan';
    
$message[61]  = 'Feb';
    
$message[62]  = 'Mar';
    
$message[63]  = 'Apr';
    
$message[64]  = 'May';
    
$message[65]  = 'Jun';
    
$message[66]  = 'Jul';
    
$message[67]  = 'Aug';
    
$message[68]  = 'Sep';
    
$message[69]  = 'Oct';
    
$message[70]  = 'Nov';
    
$message[71]  = 'Dec';
    
$message[75]  = 'Bandwidth';
    
$message[102] = 'Total';
    
$message[115] = 'OK';
    
$message[133] = 'Reported period';
}

/*******************************************************
* HTML SECTION
*******************************************************/


?>
<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN>
<html>
<head>
<title>AWStats Totals</title>
<style type="text/css">
body { font: 12px arial,verdana,helvetica,sans-serif; background-color: white }
td   { font: 12px arial,verdana,helvetica,sans-serif; text-align: center; color: black }
.l { text-align: left }
.b { background-color: #ccccdd; padding: 2px; margin: 0 }
.d { background-color: white }
.f { font: 14px verdana,arial,helvetica }
a  { text-decoration: none }
a:hover { text-decoration: underline }
a.h  { color: black }
</style>
</head>
<body>

<form action="<?=$SCRIPT_NAME?>">
<table class=b border=0 cellpadding=2 cellspacing=0 width="100%">
<tr><td class=l>
<table class=d border=0 cellpadding=8 cellspacing=0 width="100%">
<tr>
<th><?=$message[133]?>:</th>
<td class=l>
<?
echo "<select class=f name=month>\n";
for (
$i 1$i <= 12$i++) { 
    echo 
"<option value=$i"; if ($month == $i) echo " selected"; echo ">".$message[$i 59]."\n";
}
echo 
"<option value=all"; if ($month == 'all') echo " selected"; echo ">-\n";
echo 
"</select>\n";

echo 
"<select class=f name=year>\n";
for (
$curyear date("Y"), $i $curyear 4$i <= $curyear$i++) {
    echo 
"<option value=$i"; if ($year == $i) echo " selected"; echo ">$i\n";
}
echo 
"</select>";
?>
<input type=submit class=f value="<?=$message[115]?>">
</td></tr>
</table>
</td></tr>
</table>
</form>

<table align=center>
<tr>
<? $url "$SCRIPT_NAME?month=$month&year=$year&sort="?>
<td bgcolor=#ECECEC class=l nowrap>&nbsp;<a href="<?=$url?>config" class=h><?=$message[7]?></a></td>
<? foreach ($total_stats as $key => $value
    if (
$key != 'config') { ?>
<td width=80 bgcolor=#ECECEC><a href="<?=$url.$key?>" class=h><?=$key?></a></td>
<? ?>
</tr>
<?

foreach ($all_stats as $stats) {
    echo 
"<tr><td class=l><a href=$AWStatsURL?config=".$stats["config"].">".$stats["config"]."</a>";
    foreach (
$total_stats as $key => $value)
    if (
$key == 'Totals')
        echo 
"<td>".$stats[$key]."</td>";
        else if (
$key != 'config')
        echo 
"<td>".$stats[$key]."%</td>";
    echo 
"</tr>\n";
}

echo 
"<tr><td class=l bgcolor=#ECECEC>&nbsp;Total</td>";
foreach (
$total_stats as $key => $value)
    if (
$key == 'Totals')
        echo 
"<td bgcolor=#ECECEC>".$value."</td>";
    else if (
$key != 'config')
        echo 
"<td bgcolor=#ECECEC>".$value."%</td>";
echo 
"</tr>\n";
?>
</table>

<br><br><center><b>AWStats Browsers 1.0</b> - <a href="http://www.curioso.org/awstats">Created by Curioso</a></center><br><br>

</body>
</html>