<?php

/*
    Copyright 2009 Michael Sumner
    Email: msumner@dnmedia.com
    Web: http://www.DNMedia.com
    
    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 3 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, see <http://www.gnu.org/licenses/>.
*/ 

define("CPANELUSER""********");
define("CPANELPASS""********");
define("DOMAIN""yoursite.com");
define("WWWTXT""www"); // Replace "www" with "" if you redirect to non-www version.
define("MAXSITES"5000);

?>

<!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>AWStats - View All</title>
<style type="text/css">
table.stats {
    border-width: 1px 2px 1px 2px;
    border-spacing: 0px;
    border-style: solid solid solid solid;
    border-color: black black black black;
    border-collapse: separate;
    background-color: white;
}
table.stats th {
    border-width: 1px 2px 1px 2px;
    padding: 5px 5px 5px 5px;
    border-style: solid solid solid solid;
    border-color: black black black black;
    -moz-border-radius: 0px 0px 0px 0px;
}
table.stats td {
    border-width: 1px 0px 1px 0px;
    padding: 5px 5px 5px 5px;
    border-style: solid solid solid solid;
    border-color: black black black black;
    -moz-border-radius: 0px 0px 0px 0px;
}
</style>
</head>

<?

if(!isset($_POST['submit']))
{
    
$mon date("M");
    
$year date("Y");
?>

<form method="POST" action="<?=$_SERVER['PHP_SELF']?>">
    <select name="mon">
        <option val="Jan"<?=($mon == "Jan") ? " selected" ""?>>Jan</option>
        <option val="Feb"<?=($mon == "Feb") ? " selected" ""?>>Feb</option>
        <option val="Mar"<?=($mon == "Mar") ? " selected" ""?>>Mar</option>
        <option val="Apr"<?=($mon == "Apr") ? " selected" ""?>>Apr</option>
        <option val="May"<?=($mon == "May") ? " selected" ""?>>May</option>
        <option val="Jun"<?=($mon == "Jun") ? " selected" ""?>>Jun</option>
        <option val="Jul"<?=($mon == "Jul") ? " selected" ""?>>Jul</option>
        <option val="Aug"<?=($mon == "Aug") ? " selected" ""?>>Aug</option>
        <option val="Sep"<?=($mon == "Sep") ? " selected" ""?>>Sep</option>
        <option val="Oct"<?=($mon == "Oct") ? " selected" ""?>>Oct</option>
        <option val="Nov"<?=($mon == "Nov") ? " selected" ""?>>Nov</option>
        <option val="Dec"<?=($mon == "Dec") ? " selected" ""?>>Dec</option>
    </select>
    <select name="year">
        <option val="2005">2005</option>
        <option val="2006">2006</option>
        <option val="2007">2007</option>
        <option val="2008">2008</option>
        <option val="2009"<?=($year == "2009") ? " selected" ""?>>2009</option>
        <option val="2010"<?=($year == "2010") ? " selected" ""?>>2010</option>
    </select>
    <input type="submit" name="submit" value="Go" />
</form>

<?
}
else
{
    
$sites = array();
    
$stats = array();
    
$runningStats = array("uniques" => 0"visits" => 0"pages" => 0"hits" => 0);
    
$mon $_POST['mon'];
    
$year $_POST['year'];

    
$sites getSites();
    
    
?>

<table class="stats">
    <tr>
        <td bgcolor="#ECECEC" width="200"><strong>Domain</strong></td>
        <td bgcolor="#FF9933" width="100"><strong>Uniques</strong></td>
        <td bgcolor="#F3F300" width="100"><strong>Visits</strong></td>
        <td bgcolor="#4477DD" width="100"><strong>Pages</strong></td>
        <td bgcolor="#66F0FF" width="100"><strong>Hits</strong></td>
    </tr>

    <?
    
    
foreach($sites as $site)
    {
        
$pieces explode(","$site);
        
$url $pieces[0];
        
$domain $pieces[1];
        
        
$stats getStats($domain$mon$year);
        
displayStats($url$stats);
        
        
$runningStats['uniques'] += $stats['uniques'];
        
$runningStats['visits'] += $stats['visits'];
        
$runningStats['pages'] += $stats['pages'];
        
$runningStats['hits'] += $stats['hits'];
    }
    
    
?>
    
    <tr>
        <td><strong>Totals (<? echo count($sites); ?> Domains)</strong></td>
        <td><?=$runningStats['uniques']?></td>
        <td><?=$runningStats['visits']?></td>
        <td><?=$runningStats['pages']?></td>
        <td><?=$runningStats['hits']?></td>
    </tr>
</table>

    <?
}

function 
getSites()
{
    
$list = array();
    
$domStart '<td class="truncate">';
    
$start '<td class="truncate" truncate="25" align="center">';
    
$finish '</td>';
    
    
$ch curl_init();
    
    
curl_setopt($chCURLOPT_URL'http://' WWWTXT DOMAIN ':2082/frontend/x3/addon/index.html?itemsperpage=' MAXSITES);
    
curl_setopt($chCURLOPT_RETURNTRANSFER1);
    
curl_setopt($chCURLOPT_HTTPAUTHCURLAUTH_BASIC);
    
curl_setopt($chCURLOPT_USERPWDCPANELUSER ':' CPANELPASS);
    
    
$html curl_exec($ch);
    
curl_close($ch);
    
    while(
$html strstr($html$domStart))
    {
        
$end strpos($html$finish);
        
$domain substr($htmlstrlen($domStart), $end strlen($domStart));
        
        
$html strstr($html$start);
        
$end strpos($html$finish);
        
$subDom substr($htmlstrlen($start), $end strlen($start));
        
$html substr($htmlstrlen($start));
        
$list[] = $domain "," $subDom;
    }
    
    return 
$list;
}

function 
getStats($site$mon$year)
{
    
$stats = array();
    
$months = array("Jan" => "01""Feb" => "02""Mar" => "03""Apr" => "04""May" => "05""Jun" => "06",
                    
"Jul" => "07""Aug" => "08""Sep" => "09""Oct" => "10""Nov" => "11""Dec" => "12");
                    
    
$ch curl_init();
    
$url 'http://' WWWTXT DOMAIN ':2082/awstats.pl?month=' $months[$mon] . '&year=' $year '&config='
                
$site '.' DOMAIN '&ssl=&lang=en&framename=mainright';
    
    
curl_setopt($chCURLOPT_URL$url);
    
curl_setopt($chCURLOPT_RETURNTRANSFER1);
    
curl_setopt($chCURLOPT_HTTPAUTHCURLAUTH_BASIC);
    
curl_setopt($chCURLOPT_USERPWDCPANELUSER ':' CPANELPASS);
    
    
$html curl_exec($ch);
    
curl_close($ch);
    
    
$currMon date("M");
    
$monSearch = ($currMon == $mon) ? '<font class="currentday">' $mon " " $year '</font>' 'td>' $mon " " $year '</td>';
    
    
$html strstr($html$monSearch);
    
$html strstr($html'<td>');
    
$end strpos($html'</td>');
    
$stats['uniques'] = substr($html4$end 4);
    
$html substr($html4);
    
    
$html strstr($html'<td>');
    
$end strpos($html'</td>');
    
$stats['visits'] = substr($html4$end 4);
    
$html substr($html4);
    
    
$html strstr($html'<td>');
    
$end strpos($html'</td>');
    
$stats['pages'] = substr($html4$end 4);
    
$html substr($html4);
    
    
$html strstr($html'<td>');
    
$end strpos($html'</td>');
    
$stats['hits'] = substr($html4$end 4);
    
$html substr($html4);
    
    return 
$stats;
}

function 
displayStats($domain$stats)
{
?>
    <tr>
        <td><?=$domain?></td>
        <td><?=$stats['uniques']?></td>
        <td><?=$stats['visits']?></td>
        <td><?=$stats['pages']?></td>
        <td><?=$stats['hits']?></td>
    </tr>
<?
}

?>