<?php

/* dirlister.php
 * A simple script which generates a HTML listing of files in the current directory
 * allowing you to navigate around the files, viewing, and listing in subdirectories
 *
 * Paul Gregg <pgregg@pgregg.com>
 * November 2007
 *
 * Open Source Code:   If you use this code on your site for public
 * access (i.e. on the Internet) then you must attribute the author and
 * source web site: http://www.pgregg.com/projects/php/code/dirlister.php
 *
 */

set_time_limit(120);

Function 
GetIcon ($file) {
  if (
is_dir($file)) {
    
$ext 'dir';
  } else {
    
$filename basename($file);
    
$ext '';
    if (
strrpos($filename'.') !== false)
      
$ext substr($filenamestrrpos($filename'.')+1);
  }

  
$icon '';
  switch(
strtolower($ext)) {
    case 
'dir':
      
$icon 'folder.gif';
      break;
    case 
'html':
    case 
'htm':
    case 
'php':
      
$icon 'layout.gif';
      break;
    case 
'pdf':
      
$icon 'pdf.gif';
      break;
    case 
'gif':
    case 
'jpg':
    case 
'jpeg':
    case 
'png':
    case 
'ico':
      
$icon 'image2.gif';
      break;

    case 
'zip':
    case 
'gz':
    case 
'z':
      
$icon 'compressed.gif';
      break;

    case 
'chm':
      
$icon 'generic.gif';
      break;

    default:
      
$icon 'unknown.gif';
      break;
  }

  return 
$icon;
}


Function 
GetSize ($s$verbose=true) {
  
settype($s'double');
  if (
$s 1024) {
    return 
$s .($verbose ' bytes' ' B');
  }
  
$s $s 1024;
  if (
$s 1024) {
    return 
sprintf('%.2f %s'$s, ($verbose ' Kbytes' ' KB'));
  }
  
$s $s 1024;
  if (
$s 1024) {
    return 
sprintf('%.2f %s'$s, ($verbose ' Mbytes' ' MB'));
  }
  
$s $s 1024;
  if (
$s 1024) {
    return 
sprintf('%.2f %s'$s, ($verbose ' Gbytes' ' GB'));
  }
  
$s $s 1024;
  return 
sprintf('%.2f %s'$s, ($verbose ' Tbytes' ' TB'));
}




#exit;

$dir '.';
if (isset(
$_GET['dir']))
    
$dir $_SERVER['DOCUMENT_ROOT'] . $_GET['dir'];
else
    
$dir $_SERVER['DOCUMENT_ROOT'] . '/projects/php/code/';

if (
get_magic_quotes_gpc() == 1)
  
$dir stripslashes($dir);

$dir realpath($dir);
$dir preg_replace("|/usr/local|"''$dir); # pgregg hack for my vhost

if (strpos($dir, ($_SERVER['DOCUMENT_ROOT'] . '/projects/php/code')) !== ) {
  print 
"Sorry, we do not permit access outside the web root.\n";
  exit;
}

if (
$_GET['view'] == '1') {
        
#print "<pre>\n";
        
if (function_exists('mime_content_type')) {
          
$mimetype mime_content_type($dir);
          
header("Content-Type: $mimetype");
          
$filename basename($dir);
          
header("Content-Disposition: inline; filename=\"$filename\"");
        }
        
readfile($dir);
        
#print "</pre>";
        
exit;
}


include 
$_SERVER['DOCUMENT_ROOT'] . '/projects/php/preg_find/preg_find.php';

$PREG_flags PREG_FIND_RETURNASSOC PREG_FIND_DIRMATCH;
if (isset(
$_GET['r'])) $PREG_flags $PREG_flags PREG_FIND_RECURSIVE;
if (isset(
$_GET['d'])) $PREG_flags $PREG_flags PREG_FIND_DIRONLY;

$files preg_find('/./'$dir$PREG_flags);
ksort($files);

$show_file_href TRUE;
$sfont '';

$printdir str_replace($_SERVER['DOCUMENT_ROOT'].'/'''$dir);
print 
"<h4>Directory Listing: $printdir</h4>";

print 
"<table border=0 cellspacing=0 cellpassing=6><tr><td width=\"5%\">&nbsp;</td><td width=\"35%\" valign=left>$sfont Filename</td><td align=left width=\"15%\">$sfont Size</td><td width=\"30%\" align=left>$sfont Last Modified</td></tr>\n";

#$olddir = '';
for($i=0;$i<2;$i++) { // Run through this twice - when:
  // $i == 0  display directories
  // $i == 1  display files
  
foreach ($files as $fileentry => $filestats) {
    
#$file = basename($fileentry);
    
$file str_replace($_SERVER['DOCUMENT_ROOT'].'/'.$printdir.'/'''$fileentry);
    
$filedir dirname($fileentry);
    
#if ($file == "dirlist.php") continue;
    
if ($file == '.htaccess') continue;
    if (
$file == 'favicon.ico') continue;
    if (
$file == 'torrentmod') continue;
    if (
strpos($fileentry$_SERVER['DOCUMENT_ROOT'] . '/index.php') === 0) continue;

    
$s $filestats['stat']['size'];
    
$t $filestats['stat']['ctime'];
    if (
$filestats['filetype'] != 'dir') {
      if (
$i==0) continue;
      
$size GetSize($s);
    } else {
      if (
$i==1) continue;
      
$size "Directory";
    }
    
$when Date("D, d F, Y, H:i:s"$t);
    
$directlink false;
    if (
function_exists('mime_content_type')) {
      
$mimetype mime_content_type($dir.'/'.$file);
      if (
$mimetype == 'text/html'$directlink true;
      if (
preg_match("|\.php$|"$file)) $directlink true;
    }
    if (
$show_file_href) {
      
#$href = "<a href=\"$dir/$file\">$file</a>";
      
$printdir2 str_replace($_SERVER['DOCUMENT_ROOT'], ''$dir);
      if (
$directlink) {
        
$href sprintf("<a href='%s'>%s</a>",
          
str_replace('%2F''/'rawurlencode($printdir2.'/'.$file)),
          
$file );
      } else {
        
$href sprintf("<a href='%s?%sdir=/%s/%s%s%s'>%s</a>",
          
$_SERVER['PHP_SELF'],
          (
is_file("$dir/$file") ? "view=1&" "" ),
          
urlencode($printdir),
          
urlencode($file),
          (isset(
$_GET['r']) ? '&r' ''),
          (isset(
$_GET['d']) ? '&d' ''),
          
$file);
      }
    } else {
      
$href $file;
    }
    
#if ($filedir != $olddir && isset($_GET['r'])) {
    #  $printdir2 = str_replace($_SERVER['DOCUMENT_ROOT'].'/', '', $filedir);
    #  printf("<tr><td colspan=4><hr>$sfont<b>%s</b></td></tr>\n", $printdir2);
    #  $olddir = $filedir;
    #}
    
$fileicon GetIcon($fileentry);
    
printf ("<tr><td>%s</td><td>$sfont %s</td><td align=left>$sfont %s </td><td>$sfont %s</td></tr>\n",
        (
$fileicon == '' '&nbsp;' '<img src="/icons/'.$fileicon.'" border=0>'),
        
$href$size ' '$when);
  }
}
print 
"</table>\n";

print 
'<hr><small><i>File listing generated by <a href="http://www.pgregg.com/projects/php/code/dirlister.phps">dirlister.php</a> from '.$_SERVER['HTTP_HOST'].'</i></small>';