<?php
/*
 * Notes :
 * 
 * Ce fichier nécessite une version de PHP >= 4.3.0
 * 
 * Les icones utilisées proviennent du pack "famfamfam Silk Icons"
 * <http://www.famfamfam.com/lab/icons/silk/>
 *
 */



error_reporting(0);



/*
 * Configuration
 */

// always - never - local
define('PHP_INFOS''local');





/*
 * Images
 */

if(isset($_GET['img']))
{
    
$imgExtension        'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHhSURBVDjLpZI9SJVxFMZ/r2YFflw/kcQsiJt5b1ije0tDtbQ3GtFQYwVNFbQ1ujRFa1MUJKQ4VhYqd7K4gopK3UIly+57nnMaXjHjqotnOfDnnOd/nt85SURwkDi02+ODqbsldxUlD0mvHw09ubSXQF1t8512nGJ/Uz/5lnxi0tB+E9QI3D//+EfVqhtppGxUNzCzmf0Ekojg4fS9cBeSoyzHQNuZxNyYXp5ZM5Mk1ZkZT688b6thIBenG/N4OB5B4InciYBCVyGnEBHO+/LH3SFKQuF4OEs/51ndXMXC8Ajqknrcg1O5PGa2h4CJUqVES0OO7sYevv2qoFBmJ/4gF4boaOrg6rPLYWaYiVfDo0my8w5uj12PQleB0vcp5I6HsHAUoqUhR29zH+5B4IxNTvDmxljy3x2YCYUwZVlbzXJh9UKeQY6t2m0Lt94Oh5loPdqK3EkjzZi4MM/Y9Db3MTv/mYWVxaqkw9IOATNR7B5ABHPrZQrtg9sb8XDKa1+QOwsri4zeHD9SAzE1wxBTXz9xtvMc5ZU5lirLSKIz18nJnhOZjb22YKkhd4odg5icpcoyL669TAAujlyIvmPHSWXY1ti1AmZ8mJ3ElP1ips1/YM3H300g+W+51nc95YPEX8fEbdA2ReVYAAAAAElFTkSuQmCC';
    
$imgFolder            'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAA3NCSVQICAjb4U/gAAABhlBMVEX//v7//v3///7//fr//fj+/v3//fb+/fT+/Pf//PX+/Pb+/PP+/PL+/PH+/PD+++/+++7++u/9+vL9+vH79+r79+n79uj89tj89Nf889D88sj78sz78sr58N3u7u7u7ev777j67bL67Kv46sHt6uP26cns6d356aP56aD56Jv45pT45pP45ZD45I324av344r344T14J734oT34YD13pD24Hv03af13pP233X025303JL23nX23nHz2pX23Gvn2a7122fz2I3122T12mLz14Xv1JPy1YD12Vz02Fvy1H7v04T011Py03j011b01k7v0n/x0nHz1Ejv0Hnuz3Xx0Gvz00buzofz00Pxz2juz3Hy0TrmznzmzoHy0Djqy2vtymnxzS3xzi/kyG3jyG7wyyXkwJjpwHLiw2Liw2HhwmDdvlXevVPduVThsX7btDrbsj/gq3DbsDzbrT7brDvaqzjapjrbpTraojnboTrbmzrbmjrbl0Tbljrakz3ajzzZjTfZijLZiTJdVmhqAAAAgnRSTlP///////////////////////////////////////8A////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////9XzUpQAAAAlwSFlzAAALEgAACxIB0t1+/AAAAB90RVh0U29mdHdhcmUATWFjcm9tZWRpYSBGaXJld29ya3MgOLVo0ngAAACqSURBVBiVY5BDAwxECGRlpgNBtpoKCMjLM8jnsYKASFJycnJ0tD1QRT6HromhHj8YMOcABYqEzc3d4uO9vIKCIkULgQIlYq5haao8YMBUDBQoZWIBAnFtAwsHD4kyoEA5l5SCkqa+qZ27X7hkBVCgUkhRXcvI2sk3MCpRugooUCOooWNs4+wdGpuQIlMDFKiWNbO0dXTx9AwICVGuBQqkFtQ1wEB9LhGeAwDSdzMEmZfC0wAAAABJRU5ErkJggg==';
    
$imgParentFolder    'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAKFSURBVDjLjZPPb4xhEMc/z/vublvt292m1TZoo4RI4yAhgvhx4OBHSFwkHCQuuEq4SBxcHP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/llhevplg9zTtR8RC0AmSlGtrGCMxVqF9x5j/gRlRQLZbIbt3fvW4lwmpS0IhCA4FwgEjDForVFK/Ta9oYDa8jdmpt83Hndu86DaEQkgHgkBrXXT5QaA4FuiqI3itl4IPzHWk7G5NQUBQgISUEoBYIxpVlArle9+fCbntFY6qM2Z4BOWazFzS13UPrwjlUqzuFhtXF9NZZ0Cn7hLc59mrly+/uPQ+OO3T+6PP8W7OpH1fJ6cpLU1hUsSphcqRLlNFHK6GXD84nuvlCoDS1FrgZn28+T5zom933jzeoKpyZeY9oPceOJpz1e4erbtLw/WTTBZWVpaVNmcYuvWDk6eOsPAwCCLseHOpCOfNg0vgACg1rXxSL1enzDGZAC9QSOD9345nU4PrgfsWKvzRp9/jwcWfgF7VEKXfHY5kwAAAABJRU5ErkJggg==';
    
$imgDefault            'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAC4SURBVCjPdZFbDsIgEEWnrsMm7oGGfZrohxvU+Iq1TyjU60Bf1pac4Yc5YS4ZAtGWBMk/drQBOVwJlZrWYkLhsB8UV9K0BUrPGy9cWbng2CtEEUmLGppPjRwpbixUKHBiZRS0p+ZGhvs4irNEvWD8heHpbsyDXznPhYFOyTjJc13olIqzZCHBouE0FRMUjA+s1gTjaRgVFpqRwC8mfoXPPEVPS7LbRaJL2y7bOifRCTEli3U7BMWgLzKlW/CuebZPAAAAAElFTkSuQmCC';
    
    switch(
strtolower($_GET['img']))
    {
        case 
'extension':
            
header('Content-type: image/png');
            echo 
base64_decode($imgExtension);
            exit;
        
        case 
'folder':
            
header('Content-type: image/png');
            echo 
base64_decode($imgFolder);
            exit;
        
        case 
'parentfolder':
            
header('Content-type: image/png');
            echo 
base64_decode($imgParentFolder);
            exit;
        
        case 
'jpg':
        case 
'jpeg':
        case 
'gif':
        case 
'png':
            
header('Content-type: image/png');
            echo 
base64_decode($imgImage);
            exit;
        
        case 
'txt':
            
header('Content-type: image/png');
            echo 
base64_decode($imgText);
            exit;
        
        case 
'php':
        case 
'phps':
            
header('Content-type: image/png');
            echo 
base64_decode($imgPHP);
            exit;
        
        case 
'bz':
        case 
'bz2':
        case 
'gz':
        case 
'gz2':
        case 
'rar':
        case 
'zip':
            
header('Content-type: image/png');
            echo 
base64_decode($imgCompressed);
            exit;
        
        default:
            
header('Content-type: image/png');
            echo 
base64_decode($imgDefault);
            exit;
    }
}




/*
 * PhpInfo
 */

if( isset($_GET['phpinfo']) && can_show_php_infos() )
{
    
phpinfo();
    exit;
}





/*
 * Préparation
 */

$order_by 'basename';




// Evite d'avoir "index.php" dans l'url
if(preg_match('`'.preg_quote(basename($_SERVER['SCRIPT_NAME']), '`').'$`'$_SERVER['REQUEST_URI']))
{
    
header('Status: 301 Moved Permanently'true301);
    
header('Location: ./');
    exit;
}



// Paths
$current_file_path realpath(__FILE__);
//$current_dir_path = realpath($_SERVER['DOCUMENT_ROOT'] . $_SERVER['REQUEST_URI']);
$current_dir_path  $_SERVER['DOCUMENT_ROOT'] . urldecode($_SERVER['REQUEST_URI']);
if (!
file_exists($current_dir_path)) {
//    $current_dir_path = dirname($current_file_path);
    
header('Status: 301 Moved Permanently'true301);
    
header('Location: ../');
    exit;
}

$dirs_path  glob($current_dir_path.'/*'GLOB_ONLYDIR);

$files_path glob($current_dir_path.'/*');
$files_path array_diff($files_path$dirs_path);



// Infos
$dirs_infos = array();
foreach(
$dirs_path as $path)
{
    
$infos pathinfo($path);
    
$infos['path'] = realpath($path);
    
    
$infos['utf8name'] = to_utf8($infos['basename']);
    
    
$dirs_infos$infos[$order_by] ] = $infos;
}
uksort($dirs_infos'strnatcasecmp');

$files_infos = array();
foreach(
$files_path as $path)
{
    
$path realpath($path);
    if(
$path!=$current_file_path)
    {
        
$infos pathinfo($path);
        
$infos['path'] = $path;
        
$infos['size'] = filesize($path);
        
        
$infos['utf8name'] = to_utf8($infos['basename']);
        
        
$files_infos$infos[$order_by] ] = $infos;
    }
}
uksort($files_infos'strnatcasecmp');







/*
 * Génération d'affichage
 */

// Titre
$path preg_replace('`^'preg_quote(realpath($_SERVER['DOCUMENT_ROOT'])) .'`''/'$current_dir_path);
$path str_replace('\\''/'$path);
$path ltrim($path'/');
$title 'Index of /'$path;



// Infos PHP
if(can_show_php_infos())
{
    
$php_infos  '<div class="php_infos">';
    
$php_infos .= '<table class="php_infos"><tr>';

    
$php_infos .= '<th>PHP Version :</th>';
    
$php_infos .= '<td>'PHP_VERSION .'</td>';

    
$php_infos .= '</tr><tr>';

    
$php_infos .= '<th>Loaded Extensions :</th>';
    
$php_infos .= '<td><ul>';
    
$extensions get_loaded_extensions();
    
natcasesort($extensions);
    foreach(
$extensions as $extension)
    {
        
$php_infos .= '<li><span>'$extension .'</span></li>';
    }
    
$php_infos .= '</ul></td>';

    if(
can_show_php_infos())
    {
        
$php_infos .= '</tr><tr>';
        
$php_infos .= '<th>Tools :</th>';
        
$php_infos .= '<td><a class="php_infos_link" href="'$_SERVER['REQUEST_URI'] .'?phpinfo=1">phpinfo()</a></td>';
    }
    
    
$php_infos .= '</tr></table>';
    
$php_infos .= '</div>';
}



// Liste des éléments du dossier
$dir_contents '<table class="dir_contents"><thead><tr>'.
        
'<th> </th>'.
        
'<th>Name</th>'.
        
'<th>Type</th>'.
        
'<th>Size</th>'.
        
'</tr></thead><tbody>';
if(
$_SERVER['REQUEST_URI']!='/')
{
    
$dir_contents .= '<tr>';
    
$dir_contents .= '<td><img src="'$_SERVER['REQUEST_URI'] .'?img=parentfolder" alt=""></td>';
    
$dir_contents .= '<td><a href="../">Parent Directory</a></td>';
    
$dir_contents .= '<td>Directory</td>';
    
$dir_contents .= '<td></td>';
    
$dir_contents .= '</tr>';
}
foreach(
$dirs_infos as $dir)
{
    
$dir_contents .= '<tr>';
    
$dir_contents .= '<td><img src="'$_SERVER['REQUEST_URI'] .'?img=folder" alt=""></td>';
    
$dir_contents .= '<td><a href="'$dir['utf8name'] .'">'$dir['utf8name'] .'</a></td>';
    
$dir_contents .= '<td>Directory</td>';
    
$dir_contents .= '<td></td>';
    
$dir_contents .= '</tr>';
}
foreach(
$files_infos as $file)
{
    
$dir_contents .= '<tr>';
    
$dir_contents .= '<td><img src="'$_SERVER['REQUEST_URI'] .'?img='$file['extension'] .'" alt=""></td>';
    
$dir_contents .= '<td><a href="'$file['utf8name'] .'">'$file['utf8name'] .'</a></td>';
    
$dir_contents .= '<td>'strtoupper($file['extension']) .' File</td>';
    
$dir_contents .= '<td align="right">'human_size($file['size'], 1) .'</td>';
    
$dir_contents .= '</tr>';
}
$dir_contents .= '</tbody></table>';










/*
 * Fonctions utiles
 */

function human_size($bytes$precision 2)
{
    
$units = array('B''k''M''G''T');

    
//$bytes = max($bytes, 0);
    
$bytes max($bytes1024);
    
$pow floor(($bytes log($bytes) : 0) / log(1024));
    
$pow min($powcount($units) - 1);

    
$bytes /= pow(1024$pow);

    return 
round($bytes$precision) . $units[$pow];



function 
to_utf8($str)
{
    if(
$str !== mb_convert_encodingmb_convert_encoding($str'UTF-32''UTF-8'), 'UTF-8''UTF-32') )
        
$str mb_convert_encoding($str'UTF-8');
    return 
$str;
}


function 
can_show_php_infos()
{
    switch(
PHP_INFOS)
    {
        case 
'always':    return true;
        case 
'local':    return ($_SERVER['REMOTE_ADDR']=='127.0.0.1');
        default:        return 
false;
    }
}



?>
<html>
<head>
    <title><?php echo $title ?></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="google-site-verification" content="K-VI7cnIx4hkDvHxQ1GOvK-RceYINrWe0UDAog087RU" />
    <style type="text/css">
    html,
    body {
    margin:0; padding:0;
    }
    html {
    background-color:#b2b2b2;
    font-family:sans-serif;
    font-size:13px;
    }
    h1 {
    font-size:2em;
    }
    table {
    font-size:13px;
    }
    a {
    color:#024378;
    }
    a:visited {
    color:#130074;
    }
    div.main {
    margin:15px 10%;
    border:1px solid gray;
    background-color:#fefefe;
    }
    div.content {
    margin:15px 40px;
    }
    div.php_infos {
    margin:2em 0; padding:1em 0;
    border-top:1px solid #cccccc;
    border-bottom:1px solid #cccccc;
    }
    div.php_infos table {
    font-size:small;
    line-height:1.5em;
    }
    div.php_infos table th {
    text-align:right;
    vertical-align:top;
    white-space:nowrap;
    padding-right:1em;
    }
    div.php_infos ul {
    margin:0; padding:0;
    list-style-type:none;
    }
    div.php_infos ul li {
    margin:0; padding:0;
    display:inline-block;
    width:24%; float:left;
    background:transparent url('./?img=extension') left no-repeat;
    }
    div.php_infos ul li span {
    margin-left:20px;
    }
    div.php_infos a.php_infos_link {
    font-weight:bold;
    text-decoration:none;
    }
    table.dir_contents {
    font-family:monospace;
    }
    table.dir_contents th {
    margin:0;
    text-align:left;
    border-bottom:1px solid #aaaaaa;
    }
    table.dir_contents th,
    table.dir_contents td {
    padding:4px 2px;
    }
    table.dir_contents td a {
    margin-right:5em;
    }
    </style>
    </head>
<body>
<div class="main">
<div class="content">

<h1><?php echo $title ?></h1>

<?php echo $php_infos ?> 
<?php echo $dir_contents ?> 

</div>
</div>
</body>
</html>