<?php
$seperator = "\t";
$indentation = "\t";
$max_symlink_level = 2;
// Print a line for a file
function outputLine( $path, $file, $fs, $lm, $user, $group, $permissions, $level, $symlink_level, $isDir = False ) {
global $seperator;
global $indentation;
//$spaces = str_repeat($indentation, $level);
// no space at beginning
$spaces = "";
// Concatenate together output line
$output_str = $spaces . $path."/".$file . $seperator;
// output a 'd' instead of filesize for directories
if ($isDir)
$output_str .= "d";
else
$output_str .= $fs;
$output_str .= $seperator . $lm;
if ( is_link("$path/$file") )
$output_str .= $seperator . "l";
else
$output_str .= $seperator . "f";
$output_str .= $seperator . $user;
$output_str .= $seperator . $group;
$output_str .= $seperator . sprintf('%o', $permissions);
$output_str .= $seperator . $symlink_level;
$output_str .= "\n";
echo $output_str;
}
function getDirectory( $path = '.', $level = 0, $symlink_level = 0 ){
global $max_symlink_level;
// Sanity check for directory depth
if ($level > 100)
return;
// echo "symlink level: ".$symlink_level."\n";
$ignore = array( 'cgi-bin', '.', '..', '/' );
// Directories to ignore when listing output. Many hosts
// will deny PHP access to the cgi-bin.
$dh = @opendir( $path );
// Open the directory to the handle $dh
while( false !== ( $file = readdir( $dh ) ) ){
// Loop through the directory
if( !in_array( $file, $ignore ) && strlen($file) != 0 ){
// Check that this file is not to be ignored
if( is_dir( "$path/$file" ) ){
// Its a directory, so we need to keep reading down...
$fs = filesize("$path/$file");
$lm = filemtime("$path/$file");
$user = fileowner("$path/$file");
$group = filegroup("$path/$file");
$permissions = fileperms("$path/$file");
outputLine($path, $file, $fs, $lm, $user, $group, $permissions, $level, $symlink_level, True);
if ( is_link("$path/$file") && $symlink_level < $max_symlink_level ) {
getDirectory( "$path/$file", ($level+1), ($symlink_level + 1) );
} else if (!is_link("$path/$file")) {
getDirectory( "$path/$file", ($level+1), $symlink_level );
}
// Re-call this same function but on a new directory.
// this is what makes function recursive.
} else {
$fs = filesize("$path/$file");
$lm = filemtime("$path/$file");
$user = fileowner("$path/$file");
$group = filegroup("$path/$file");
$permissions = fileperms("$path/$file");
outputLine($path, $file, $fs, $lm, $user, $group, $permissions, $level, $symlink_level);
// Just print out the filename
}
}
}
closedir( $dh );
// Close the directory handle
}
// Output as plaintext with utf8 encoding
// change charset to iso-8859-1 to change to ASCII
header('Content-Type: text/plain; charset=utf8');
$dir_arg = $_GET['dir'];
$print_path = $_GET['print_path'];
if ($print_path) {
echo "-listing-begin-\n";
echo getcwd() . "\n";
echo "-listing-end-\n";
return;
}
// Check if dir parameter is valid
if( is_dir($dir_arg) ) {
//echo $dir_arg."\n\n";
echo "-listing-begin-\n";
getDirectory( $dir_arg );
echo "-listing-end-\n";
}
else
echo "bad dir";
?>