<?php
$dirname = "";#preg_replace(array("/\/$/", "/\//"), array("", " / "), $_SERVER['REQUEST_URI']);
$linkeddirname = "";
foreach(split("/", $_SERVER['REQUEST_URI']) as $bit)
{
    if($bit != "/" && $bit != "")
    {
        $dirname .= "/" . $bit;
        $linkeddirname .= ' / <a href="' . $dirname . '/">' . $bit . '</a>';
    }
}
#$d = dir(".");
$d = dir("/home/download/public_html/" . $_SERVER['REQUEST_URI']);  // "." Could go here...

function niceSize($val)
{
    $symbols = array("B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB");
    for($i=0;$i<count($symbols)-1 && $val>=1024;$i++)
        $val /= 1024;
    return('<td class="size">' . round($val, 2) . '</td><td>' . $symbols[$i] . '</td>');
}

function foldersize($name)
{
    $dirs = 0;
    $thedir = dir($name);
    while (false !== ($entry = $thedir->read()))
        if(validfile($thedir->path . '/' . $entry) && is_dir($thedir->path . '/' . $entry))
            $dirs++;
    $files = folderfilecount($name);
    return('<td colspan="2"><a class="notalink" title="' . $files . ' File' . ( $files == 1 ? '' : 's' ) . ' in ' . $dirs . ' Folder' . ( $dirs == 1 ? '' : 's' ) . '">' . $files . ' File' . ( $files == 1 ? '' : 's' ) . '</a></td>');
}

function folderfilecount($thepath, $depth=0)
{
    $thedir = dir($thepath);
    $files = 0;
    if($depth > 10) return;
    while (false !== ($entry = $thedir->read()))
    {
        if(validfile($thedir->path . '/' . $entry))
        {
            if(is_dir($thedir->path . '/' . $entry))
                $files += folderfilecount($thedir->path . '/' . $entry, $depth+1);
            else
                $files++;
        }
    }
    return($files);
}

function validfile($name)
{
    return(preg_match("/\.pk3$/", $name) || ( is_dir($name) && substr($name,0,1) != "." && !preg_match("/\/\./", $name) ));
}

function filelist($d)
{
    $filelist = array();
    while (false !== ($entry = $d->read()))
        if(validfile($d->path.$entry) && $entry != "mappers")
            $filelist[] = $entry;
            
    sort($filelist);
    return ($filelist);
}

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>MG Download Center<?php print $dirname; ?></title>
        <link rel="stylesheet" type="text/css" href="/style.css" />
        <link title="MG Map Repo" type="application/rss+xml" rel="alternate" href="http://downloads.mercenariesguild.net/rss.xml"/>
    </head>
    <body>
        <h1><a href="/">Mercenaries Guild Download Center</a><?php print $linkeddirname; ?></h1>
        
        <table id="filelist">
        <?php
        $thefilelist = filelist($d);
        $i = 0;
        while($i < sizeof($thefilelist))
        {
            $entry = $thefilelist[$i];
            $link = '<td class="name"><a href="' . $dirname . '/' . $entry . '">' . $entry . '</a></td>';
            $size = (is_dir($d->path . $entry) ? foldersize($d->path . $entry) : niceSize(filesize($d->path . $entry)) );
            echo '<tr><td><img src="http://mercenariesguild.net/images/favicon.png" /></td>' . $link . $size . '</tr>' . "\n";
            $i++;
        }
        ?>
        </table>
        
        <hr />
        <!--small>All files are owned by there respective authors. <br />
        mercenariesguild.net holds no responsibility for these files. <br />
        If you have any questions or comments on this service, please post on the forums or email <img src="mgemail.png" /><br /></small-->
        <small>All files are owned by there respective authors. <br />
        mercenariesguild.net holds no responsibility for these files. or their content.<br />
        If you have questions, or new maps you want uploaded, please email downloads@mercenariesguild.net</small>
    </body>
</html>
