<?php
/* 
 * Alexou.net
 * This file is public domain, do whatever you want with it.
 */
 
/* config */
$root '.';
$show_readme 'README.txt';
$hide_real_path true;

/* work around if the script is used as index */
if (!$_SERVER['QUERY_STRING']) {
    
$here dirname($_SERVER['PHP_SELF']) . '/';
    if (
strncmp($_SERVER['REQUEST_URI'], $herestrlen($here)) === 0) {
        if (
$req strstr(substr($_SERVER['REQUEST_URI'], strlen($here)).'?''?'true)) {
            
header("Location: $here?$req");
        }
    }
}

/* misc to make that thing portable */
$assets = array(
            
'..file' => 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQBAMAAADt3eJSAAAAKlBMVEUAAP/39/fv7+/m5ube3t7X19fMzMzExMTAwMCZmZmEhIR4eHhzc3P///8ee1ssAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAK8AAACvABQqw0mAAAAAd0SU1FB9QFEwIPAqUlKhUAAAB5SURBVHjaY2BQMjY2NjNgYGCQvXv38uVmIEM39O7luxYKQAZQ5O6tACAjLe2yS0wCWOTuXZkEsEhaogxURBDMSM9NK9QBi1xMLAIz0gvLi2yADEVBQSVjMKO8vLzYB8hQUjZxcfEBmhy5+syZM6ccGBgYTdPLy0IUAHN3Lc5dv3wxAAAAAElFTkSuQmCC',
            
'..folder' => 'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAAY1BMVEUAAABhXiaKgTGZlTyXmZazpUK/sErHsk7NsU+ztbLVuV3av2rex4TizY/k0Jji0J7T1dLl1ann2bPb3drq377n5cju5crl5+Tq69Tt6drx7d7y8ejx8/Dz9fL39u38/vtMRyAp0OkZAAAAAXRSTlMAQObYZgAAAAlwSFlzAAAK8AAACvABQqw0mAAAAAd0SU1FB9QFEwIOLRfvJg0AAACISURBVHjaZc7hFoIgDAVgLClFkkTagJXy/k/ZZtThnO6/fecOptR/Fg6+mnnj2PuzmYkI7GTMoDsUyClBjCF4P5cR1bJv0hEEV/q1Qs4JAG5fIOK1CMFWIAGAGKYffAq+Aj/A8OBfjIDbDgDw3g0Cc+CuO+66aAF71edTV2oYcOyb8KUK1yao3oQtEREVqaRLAAAAAElFTkSuQmCC',
         );

if (
substr($_SERVER['QUERY_STRING'], 02) === '..' && isset($assets[$_SERVER['QUERY_STRING']])) {
    
header('Pragma: public');
    
header('Cache-Control: max-age=7200');
    
header('Expires: ' gmdate('D, d M Y H:i:s'time() + 7200) . ' GMT');
    
header('Content-Type: image/png');
    echo 
base64_decode($assets[$_SERVER['QUERY_STRING']]);
    exit;
}

/* program */
$root realpath($root).'/';
$mask realpath($root.'/'.$_SERVER['QUERY_STRING']);

if (!
$mask || substr($mask.'/'0strlen($root)) !== $root) die('Not found');

ini_set('open_basedir'$root);

if (
is_file($mask)) {
    
// header('Content-Type: text/plain');
    // readfile($mask);
    
echo '<html><head><title>'.($hide_real_path substr($maskstrlen($root)-1) : $mask).'</title></head><body>';
    
highlight_file($mask);
    echo 
'</body></html>';
    exit;
} else {
    @
chdir ($mask .= '/') or die ('Not found');
}
?>
<!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" xml:lang="en" lang="en">
    <head>
        <style type="text/css" media="all">
            body { background-color: #fefefe; }
            table { width: 100%; background-color: #fff;font-size:14px;border:1px solid #F0E0FF;}
            td {border-bottom: thin solid #d0d0d0; padding: 2px;}
            tr:first-child {font-size:12px;font-weight:bold;background:#DDD;}
            /*tr:nth-child(even)*/ .odd {background:#EEE;}
            a:link {color: #0066CC;text-decoration: none}
            a:visited {color: /*#214682*/#0066CC;text-decoration: none}
            a:hover {color: #F3960B;text-decoration: none;}
            .header td {border-bottom:1px solid #907F90;}
            .readme { color:#929291 !important;}
            img {border:0px;}
        </style>
        <meta http-equiv="Content-Type" content="text/html;charset=windows-1252" />
        <title>Browsing: <?php echo $hide_real_path substr($maskstrlen($root)-1) : $mask ?></title>    
    </head>
<body>
    <h2>Index of <?php echo $hide_real_path substr($maskstrlen($root)-1) : $mask ?></h2>
    <table border="0" cellspacing="0" cellpadding="0">
    <tr class="header">
    <td width="300px">Name</td> 
    <td width="80px">Source</td>
    <td width="60px">Size</td>
    <td width="145px">CTime</td>
    <td width="160px">Last modified</td>
    <td>Comment</td>    
    </tr>
    <?php
    $folders 
$files = array();
    
    if (
$mask !== $root) {
        
$folders['..'] = array('..''mime' => '''size' => 0'file' => '..''path' => substr(realpath('..'), strlen($root)));
    }
    
    foreach(
glob('{*,.?*}'GLOB_BRACE) as $file) {
        if (
is_dir($file))
            
$folders[$file] = array('mime' => '''filemtime' => date('Y-M-d H:i:s'filemtime($file)), 'filectime' => date('Y-M-d H:i:s',filectime($file)), 'file' => $file'path' => substr(realpath($file), strlen($root)));
        else
            
$files[$file] = array('mime' => mime_content_type($file), 'path' => substr(realpath($file), strlen($root)), 'size' => filesize($file), 'file' => $file'filemtime' => date('Y-M-d H:i:s'filemtime($file)), 'filectime' => date('Y-M-d H:i:s',filectime($file)));
    }

    
ksort($folders);
    
    
$cl false;
    
    foreach(
$folders as $folder=>$folder_details) {
        if (
$folder_details['path'] !== false && glob($root.$folder_details['path'].'/index.*')) {
            
$link $folder_details['path'];
        } else {
            
$link '?'.$folder_details['path'];
        }
        
        
$style = ($cl = !$cl) ? ' class="odd"' ''/* Require for older browsers... */
        
        
echo <<<EOF
            <tr{$style}>
            <td><a href="
$link"><img src="?..folder" alt="folder icon" /> {$folder}/</a></td> 
            <td><a href="?
{$folder_details['path']}">Browse</a></td>
            <td>-</td>
            <td>
{$folder_details['filectime']}&nbsp;</td>
            <td>
{$folder_details['filemtime']}&nbsp;</td>
            <td>
{$folder_details['mime']}&nbsp;</td>
            </tr>
EOF;
    }

    
ksort($files);
    
    foreach(
$files as $file) {
        if (
preg_match('#.php[s45]?$#'$file['file'])) {
            
$link "<a href=\"?{$file['path']}\">View</a>";
        } else {
            
$link '&nbsp;';
        }
        
        
$astyle $show_readme && $file['file'] == $show_readme 'class="readme" ' '';
        
$style = ($cl = !$cl) ? ' class="odd"' ''/* Require for older browsers... */
        
        
echo <<<EOF
            <tr{$style}>
            <td><a 
{$astyle}href="{$file['path']}"><img src="?..file" alt="file icon" /> {$file['file']}</a></td> 
            <td>
{$link}</td>
            <td>
{$file['size']}</td>
            <td>
{$file['filectime']}&nbsp;</td>    
            <td>
{$file['filemtime']}&nbsp;</td>    
            <td>
{$file['mime']}&nbsp;</td>
            </tr>
EOF;
    }
    
?>
    </table>
    <pre><?php if ($show_readme && file_exists($show_readme)) { echo htmlentities(file_get_contents($show_readme)); } ?></pre>
</body>
</html>