Skip to content

Instantly share code, notes, and snippets.

@rudiedirkx
Last active September 18, 2016 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rudiedirkx/b907df6bd0f6091ef220d6cc1b4ff4c1 to your computer and use it in GitHub Desktop.
Save rudiedirkx/b907df6bd0f6091ef220d6cc1b4ff4c1 to your computer and use it in GitHub Desktop.
<?php
$utcStart = microtime(true);
// Last modified timestamp
defined('TIMESTAMP') or define('TIMESTAMP', 'D d-M-Y H:i');
// Permissions
defined('ENABLE_DOWNLOAD') or define('ENABLE_DOWNLOAD', false);
defined('ENABLE_SOURCE') or define('ENABLE_SOURCE', false);
defined('ENABLE_MULTIDIR') or define('ENABLE_MULTIDIR', false);
// static definitions
define('EOL', "\n");
define('BASEPAGE', basename($_SERVER['SCRIPT_NAME']));
$icons = array(
'folder' => 'folder',
'auth_dir' => 'folder.sec',
'php' => 'p',
'jpg' => 'image2',
'jpeg' => 'image2',
'zip' => 'compressed',
'rar' => 'compressed',
'gif' => 'image2',
'png' => 'image2',
'pdf' => 'pdf',
'gz' => 'compressed',
'txt' => 'text',
'htaccess' => 'text',
'ttf' => 'a',
'wmv' => 'movie',
'mpg' => 'movie',
'mpeg' => 'movie',
'gvp' => 'movie',
'3gp' => 'movie',
'wma' => 'sound2',
'mp3' => 'sound2',
'wvx' => 'movie',
'm' => 'script',
'c' => 'script',
'cpp' => 'script',
'cgi' => 'script',
'conf' => 'script',
'tpl' => 'layout',
'html' => 'layout',
'htm' => 'layout',
'shtml' => 'layout',
'dhtml' => 'layout',
'pdf' => 'layout',
'exe' => 'bomb',
'doc' => 'quill',
'xls' => '',
'swf' => 'right',
);
$showsource = array(
'script',
'layout',
'text',
'image2',
'p',
'layout',
);
// Dir to search
$dir = ( ENABLE_MULTIDIR && !empty($_GET['dir']) && file_exists($_GET['dir']) ) ? $_GET['dir'] : './';
define( 'DIR', rtrim($dir,'\\/').'/' );
$thisdir0 = rtrim(str_replace('\\','/',realpath(DIR)),'/\\').'/';
$thisdir = substr($thisdir0, strlen($_SERVER['DOCUMENT_ROOT']));
if ( !$thisdir ) $thisdir = basename(dirname($thisdir0)).'/';
define( 'THISDIR', $thisdir );
if ( ENABLE_DOWNLOAD && !empty($_GET['download']) ) {
// DIR depends on ENABLE_MULTIDIR
$file = realpath( DIR . basename($_GET['download']) );
// If the path doesn't exist, realpath() returns FALSE
if ( $file ) {
Header("Content-Disposition: attachment; filename=".basename($file)."");
readfile( $file );
}
}
if ( ENABLE_SOURCE && !empty($_GET['image']) ) {
$file = realpath( DIR . basename($_GET['image']) );
if ( $file ) {
if ( FALSE != ($is=@getimagesize($file)) ) {
Header("Content-type: ".$is['mime']."");
readfile($file);
// print_r( $is );
}
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Index of <?php echo THISDIR; ?></title>
<style>
* {
margin: 0;
padding: 0;
font-family: 'courier new', verdana, arial;
font-size: 10pt;
}
body, html {
height: 100%;
min-height: 100%;
}
img {
border: 0;
}
td.img img {
width: 20px;
height: 22px;
}
table#lsal {
border-collapse: collapse;
margin: 3px;
}
table#lsal td {
border: solid 1px #ccc;
padding: 2px 6px;
vertical-align: middle;
}
table#lsal td.img {
text-align: center;
padding-top: 0px;
padding-bottom: 0px;
}
table#lsal td.col_datetime {
text-align: right;
font-family: 'courier new';
}
table#lsal tr.title td {
background-color: #bbb;
font-weight: bold;
}
div#title h1 {
padding: 4px;
font-weight: bold;
font-size: 3em;
}
table#show_source td {
padding: 2px;
}
</style>
</head>
<body>
<?php
if ( ENABLE_SOURCE && !empty($_GET['source'])/* && '.' != substr(basename($_GET['source']),0,1)*/ ) {
// DIR depends on ENABLE_MULTIDIR
$file = realpath( DIR . basename($_GET['source']) );
// If the path doesn't exist, realpath() returns FALSE
if ( $file ) {
// echo "file exists - ";
$x = explode(".", basename($_GET['source']));
$ext = strtolower($x[count($x)-1]);
if ( isset($icons[$ext]) && in_array($icons[$ext], $showsource) ) {
echo '<script language="javascript">document.title="Source of '.THISDIR.basename($file).'";</script>'.EOL;
// echo "show file source";
if ( FALSE != ($is=@getimagesize($file)) )
{
echo '<img alt="'.basename($file).'" src="?dir='.urlencode(DIR).'&amp;image='.basename($file).'" />'.EOL;
// print_r( $is );
}
else
{
echo '<table id="show_source">'.EOL;
echo '<tr valign="top">'.EOL;
echo '<td colspan="2" align="left" style="padding-left:100px;border-bottom:solid 1px #000;">source of <b><a href="'.DIR.basename($file).'">'.basename($file).'</a></b> - '.make_number(filesize($file)).( ENABLE_DOWNLOAD ? ' - <a href="?dir='.urlencode(DIR).'&amp;download='.basename($file).'">download</a>' : "" ).'</td>'.EOL;
echo '</tr>'.EOL;
echo '<tr valign="top">'.EOL;
echo '<td align="right" style="border-right:solid 1px #000;">';
for ( $i=1; $i<=count(file($file))+1; $i++ )
{
echo $i . "<br/>".EOL;
}
echo '</td>';
echo '<td nowrap="1">';
highlight_file( $file );
echo '</td>'.EOL;
echo '</tr></table>'.EOL;
}
exit;
}
}
}
?>
<div id="title"><h1>Index&nbsp;of&nbsp;<?php echo str_replace(" ","&nbsp;",urldecode(THISDIR)); ?></h1></div>
<div>
<?php
/** FETCH DIRS & FILES **/
$dirs = $files = [];
$map = opendir(DIR);
while ( $file = readdir($map) ) {
if ( is_file(DIR.$file) ) {
$files[] = [
'name' => $file,
'mtime' => filemtime(DIR . $file),
'size' => filesize(DIR . $file),
'extension' => ($pos = strrpos($file, '.')) !== false ? substr($file, $pos+1) : false,
];
}
elseif (trim($file, '.')) {
$dirs[] = [
'name' => $file,
'mtime' => filemtime(DIR . $file),
'size' => 0,
'extension' => '',
];
}
}
closedir($map);
/** SORT & ORDER **/
$columns = ['N' => 'name', 'M' => 'mtime', 'S' => 'size', 'T' => 'extension'];
$orders = ['A' => 1, 'D' => -1];
$C = isset($_GET['C'], $columns[ $_GET['C'] ]) ? $_GET['C'] : 'N';
$O = isset($_GET['O'], $orders[ $_GET['O'] ]) ? $_GET['O'] : 'A';
$column = $columns[$C];
$order = $orders[$O];
$cmp = function($a, $b) use ($column, $order) {
return $order * (in_array($column, ['mtime', 'size']) ? $a[$column] - $b[$column] : strnatcasecmp($a[$column], $b[$column]));
};
usort($files, $cmp);
usort($dirs, $cmp);
$arrReverseOrd = Array( "A" => "D", "D" => "A" );
$arrNewOrds['N'] = $arrNewOrds['M'] = $arrNewOrds['S'] = $arrNewOrds['T'] = "A";
$arrNewOrds[$C] = $arrReverseOrd[$O];
// print_r( $arrNewOrds );
echo '<table id="lsal" cellspacing="0">'.EOL;
echo '<tr class="title">'.EOL;
echo '<td class="img"><a href="?dir='.urlencode(DIR).'&amp;C=T&amp;O='.$arrNewOrds['T'].'">T</a></td>'.EOL;
echo '<td><a href="?dir='.urlencode(DIR).'&amp;C=N&amp;O='.$arrNewOrds['N'].'">Name</a></td>'.EOL;
echo '<td align="right"><a href="?dir='.urlencode(DIR).'&amp;C=M&amp;O='.$arrNewOrds['M'].'">Last modified</a></td>'.EOL;
echo '<td><a href="?dir='.urlencode(DIR).'&amp;C=S&amp;O='.$arrNewOrds['S'].'">Size</a></td>'.EOL;
echo '</tr>'.EOL;
echo '<tr>'.EOL;
echo '<td class="img">'.( ENABLE_MULTIDIR ? '<a href="?dir='.urlencode(DIR.'../').'">' : "" ).'<img alt="back" src="/icons/back.gif" />'.( ENABLE_MULTIDIR ? '</a>' : "" ).'</td>'.EOL;
echo '<td><a href="../">.. Previous folder</a></td>'.EOL;
echo '<td align="right"></td>'.EOL;
echo '<td></td>'.EOL;
echo '</tr>'.EOL;
foreach ( $dirs AS $dir ) {
echo '<tr valign="bottom">';
echo '<td class="img">' . ( ENABLE_MULTIDIR ? '<a href="?dir=' . urlencode(DIR . $dir['name'] . '/') . '">' : '' ) . '<img alt="folder" src="/icons/' . $icons['folder'] . '.gif" />' . ( ENABLE_MULTIDIR ? '</a>' : '' ) . '</td>';
echo '<td><a href="' . DIR . html($dir['name']) . '/' . '">' . html($dir['name']) . '</a></td>';
echo '<td class="col_datetime">' . date(TIMESTAMP, $dir['mtime']) . '</td>';
echo '<td align="right">-&nbsp;</td>';
echo '</tr>';
}
foreach ( $files AS $file ) {
$icon = isset($icons[ $file['extension'] ]) ? $icons[ $file['extension'] ] : 'unknown';
$bSource = ENABLE_SOURCE && isset($icons[ $file['extension'] ]) && in_array($icons[ $file['extension'] ], $showsource);
$bDownload = !$bSource && ENABLE_DOWNLOAD;
$szAction = $bDownload ? 'download' : 'source';
echo '<tr>';
echo '<td class="img">' . ( $bSource || $bDownload ? '<a href="?dir=' . urlencode(DIR) . '&amp;' . $szAction . '=' . $file['name'] . '">' : '' ) . '<img title="' . strtoupper($file['extension']) . ( $bSource ? ' | view source' : '' ) . ( $bDownload ? ' | download' : '' ).'" alt="' . strtoupper($file['extension']) . '" src="/icons/' . $icon . '.gif" />' . ( $bSource || $bDownload ? '</a>' : '' ) . '</td>';
echo '<td><a href="' . DIR . html($file['name']) . '">' . $file['name'] . '</a></td>';
echo '<td class="col_datetime">' . date(TIMESTAMP, $file['mtime']) . '</td>';
echo '<td align="right">' . make_number($file['size']) . '</td>';
echo '</tr>';
}
echo '</table>'.EOL.EOL;
echo '</div></body>'.EOL.EOL;
echo '</html>';
function html( $text ) {
return htmlspecialchars((string)$text, ENT_QUOTES, 'UTF-8') ?: htmlspecialchars((string)$text, ENT_QUOTES, 'ISO-8859-1');
}
function myurlencode( $url ) {
$trans = array(
" " => "%20",
"&" => "%26",
);
return strtr($url, $trans);
}
function make_number( $int ) {
static $boundary = 3;
if ( $int <= $boundary*1024 ) {
return $int . ' B&nbsp;';
}
else if ( $int <= $boundary*1024*1024 ) {
$kb = $int/1024;
$kb = 512<$kb ? ceil($kb) : round($kb,1);
return $kb . ' KB';
}
else if ( $int <= $boundary*1024*1024*1024 ) {
$mb = $int/1024/1024;
$mb = 512<$mb ? ceil($mb) : round($mb,1);
return $mb . ' MB';
}
return $int . ' xB';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment