Created
November 30, 2016 04:58
-
-
Save tterrag1098/1dc991a7069d33364992934dd07ad55f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$dir = isset($_GET['dir']) ? $_GET['dir'] : '.'; | |
if ($dir[0] === '/' || preg_match("/\.\./", $dir)) { | |
header("Location: ". basename(__FILE__)); | |
} | |
?> | |
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="description" content="<?php echo "Index of $dir";?>"> | |
<meta name="author" content="tterrag"> | |
<style> | |
body { | |
font-family: Consolas, serif; | |
margin: 50px 10px; | |
background: black; | |
color: white; | |
} | |
a { | |
color: lime; | |
} | |
ul { | |
list-style: none; | |
padding: 0; | |
} | |
li { | |
margin: 2px; | |
padding: 2px; | |
} | |
.light { | |
background: #111; | |
} | |
</style> | |
</head> | |
<body> | |
<?php | |
$files = scandir($dir); | |
$files = array_filter($files, function($v) { | |
return $v !== '.' && $v !== basename(__FILE__); | |
}); | |
usort($files, function($a, $b) { | |
global $dir; | |
$a = "$dir/$a"; | |
$b = "$dir/$b"; | |
if (is_dir($a) && is_dir($b)) { | |
return strcmp($a, $b); | |
} else if (is_dir($a)) { | |
return -1; | |
} else if (is_dir($b)) { | |
return 1; | |
} | |
return filemtime($b) - filemtime($a); | |
}); | |
$root = basename(__FILE__); | |
$light = true; | |
echo '<pre><ul>'; | |
foreach ($files as $file) { | |
// Don't list .. on the root | |
if ($file === '..' && $dir === '.') continue; | |
// Don't add ./ to beginning of paths | |
$urldir = $dir == '.' ? null : $dir; | |
$urlfile = ''; | |
if ($file === '..') { | |
// If this is an "up one" link, remove the last folder from the current dir | |
$urldir = substr($urldir, 0, strrpos($urldir, '/')); | |
} else { | |
// Otherwise, we need to add a file to the current dir | |
$urlfile = $file; | |
if ($urldir) { | |
// Only use a slash if the dir is not the root | |
$urlfile = "/$urlfile"; | |
} | |
} | |
// Encode the path | |
$urlpath = urlencode("$urldir$urlfile"); | |
// Remove params if empty | |
$params = $urlpath ? "?dir=$urlpath" : ''; | |
echo "<li class=\"" . ($light ? 'light' : 'dark') . "\"><a href=\"$root$params\">$file</a>"; | |
// Calculate alignment of date | |
$spaces = 75 - strlen($file); | |
$whitespace1 = str_repeat(' ', $spaces); | |
echo $whitespace1; | |
// Print date | |
echo gmdate("Y-m-d H:i:s", filemtime("$dir/$file")); | |
$light = !$light; | |
} | |
echo '</pre></ul>'; | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment