<?php
    $version 
"0.0.2";
    
$file "/var/log/xferlog";
    
$site_name "kaplosj";

    
/**
     * todo:
     *  * caching (dont regenerate stats all the time :)
     *  * parse anonymous downloads correctly (according to user)
     **/

    
function remove_empty ($arr) {

        
/**********************************************
         * function that removes empty array-elements.
         **********************************************/

        
$arr_ret = array();

        for (
$i 0$i count($arr); $i++) {
            if (
$arr[$i] != ""array_push($arr_ret$arr[$i]);
        }

        return (
$arr_ret);
    }

?>
<html>
<head>
    <style type='text/css'>
    body {
        font-family:        verdana;
        font-size:        12px;
    }
    td {
        padding:        4px;
        font-size:        12px;
        vertical-align:        top;
        border-width:        1px;
        border-style:        solid;
        border-color:        #000000;
        width:            50%;
    }
    td.header {
        background:        #647A92;
        color:            #eeeeee;
    }
    td.content {
        background:        #B0BDCB;
    }

    </style>
<body>
<center>
<h2><?php print($site_name); ?></h2>
<?php
    
// 6 = host
    // 7 = size
    // 8 = filename
    // 11 = o / i (out / in)
    // 13 = username

    
$data file($file);

    
// unsets arrays / stats.
    
unset ($sum_down);
    unset (
$sum_down_user);
    unset (
$sum_down_file);
    unset (
$sum_up);
    unset (
$sum_up_user);
    unset (
$sum_up_file);


    
// parse data
    
for ($i 0$i count($data); $i++) {
        
$line remove_empty(explode(" "$data[$i]));
        if (
$line[11] == "o") {
            
// houston, we have a download!
            
$sum_down += $line[7];
            
$sum_down_user[$line[13]] += $line[7];
            
$sum_down_file[$line[8]] += $line[7];
        } elseif (
$line[11] == "i") {
            
// .. or a upload.
            
$sum_up += $line[7];
            
$sum_up_user[$line[13]] += $line[7];
            
$sum_up_file[$line[8]] += $line[7];
        }

        if (
$i == 0) {
            
$first_date $line[4] . "-" $line[1] . "-" $line[2] . " (" $line[3] . ")";
        }

        if (
$i == (count($data) - 1)) {
            
$last_date $line[4] . "-" $line[1] . "-" $line[2] . " (" $line[3] . ")";
        }
    }
?>
<?php 
print ($first_date " <b> - </b> " $last_date); ?>
<br /><br /></center>
<table width='100%'>
<tr>
<td class='header'>top uploaders (hooray!)</td><td class='header'>top downloaders (sucky, sucky)</td></tr>
<tr><td class='content'>
<table width='100%'>
<?php
    arsort
($sum_up_user);
    while (list(
$user$bytes) = each($sum_up_user)) {
        print (
"<tr><td>" $user "</td><td>" round($bytes / (1024 1024), 2). " MB</td></tr>\n");
    }
?>
</table>
</td><td class='content'>
<table width='100%'>
<?php
    arsort
($sum_down_user);

    while (list(
$user$bytes) = each($sum_down_user)) {
        print (
"<tr><td>" $user "</td><td>" round($bytes / (1024 1024), 2). " MB</td></tr>\n");
    }
?>
</table>
</td></tr>
<tr><td class='header'>
total upload: <?php print (round($sum_up / (1024 1024)) . " MB"); ?>
</td><td class='header'>
total download: <?php print (round($sum_down / (1024 1024)) . " MB"); ?>
</tr>
</table>
<br>
<table width='100%'>
<tr><td class='header'>top uploaded files (by size)</td><td class='header'>top downloaded files (hi, i'm popular)</td></tr>
<tr><td class='content'>
<table width='100%'>
<?php
    $i 
0;

    
arsort($sum_up_file);
    while ((list(
$user$bytes) = each($sum_up_file)) && ($i 10)) {
        print (
"<tr><td>" basename($user) . "</td><td>" round($bytes / (1024)). " KB</td></tr>\n");
        
$i++;
    }
?>
</table>
</td><td class='content'>
<table width='100%'>
<?php
    $i 
0;

    
arsort($sum_down_file);
    while ((list(
$user$bytes) = each($sum_down_file)) && ($i 10)) {
        print (
"<tr><td>" basename($user) . "</td><td>" round($bytes / (1024)). " KB</td></tr>\n");
        
$i++;
    }
?>
</table>
</td></tr>
</table>
<br>
generated by <a href='http://software.e-mats.org/'>xferlog.info</a> (php) v<?php print($version); ?>
</body>
</html>