<?php

$title = "Directory of '$dir'";
include("pre.php3");

function str_repeat2($input, $mult) { 
$ret = ""; 
while ($mult > 0) { 
$ret .= $input; 
$mult --; 
} 
return $ret; 
} 

if ($pos = strrpos($dir, '/')) {
  $parent = substr($dir, 0, $pos);
	echo "Go to <A HREF=\"dir.php3?dir=" . $parent . "\">parent directory ('" . $parent . "')</A>";
}

$dir  .= '/';
$path = $DOCUMENT_ROOT;
if ($pos = strrpos($PHP_SELF, '/'))
	$path .= substr($PHP_SELF, 0, $pos);
$path .= '/' . $dir;

$max_title_length = 0;
$max_file_length = 0;

$dh = opendir($path);
while ($file = readdir($dh)) { 
  if ($file != "." && $file != "..") { 
  
  	if ($dotpos = strrpos($file, '.')) {
	  	$name = substr($file, 0, $dotpos);
	  	$ext  = strtolower(substr($file, $dotpos+1));
	  }
	  else {
	    $name = $file;
	    $ext  = "";
	  }
  	
  	if ($hyppos = strpos($name, '-')) {
  		$title   = substr($name, 0, $hyppos);
  		$variant = substr($name, $hyppos+1);
  	}
  	else {
  	  $title   = $name;
  	  $variant = "";
  	}

	  $title = ereg_replace("([^^ A-Z])([A-Z])", "\\1 \\2", $title);
	  $title = ereg_replace("([^^ A-Z])([A-Z])", "\\1 \\2", $title);
	  $title = eregi_replace("([A-Z])([0-9])", "\\1 \\2", $title);

		$length = strlen($title);
		if ($length > $max_title_length)
		  $max_title_length = $length;
		$length = strlen($file);
		if ($length > $max_file_length)
		  $max_file_length = $length;

		$filepath = $path . $file;
		if (is_dir($filepath))
		  $size = "";
		else
			$size = filesize($filepath);
		$date = filemtime($filepath);

		$titles[$title][$ext][] = array("file"    => $file, 
							 			                "variant" => $variant, 
														        "size"    => $size,
														        "date"    => $date);
  
  } 
}
closedir($dh);

$max_title_length += 3;
$max_file_length += 3;

echo "<PRE>";

echo "Title";
echo str_repeat2(' ', $max_title_length - strlen("Title"));
echo "File";
echo str_repeat2(' ', $max_file_length - strlen("File"));
echo "Size";
echo str_repeat2(' ', 8 - strlen("File"));
echo "Date\n\n";

ksort ($titles);
reset ($titles);

while (list($title, $exts) = each($titles)) {
	echo $title;
	$first = 1;
	
	ksort ($exts);
	reset ($exts);
	
	while (list($ext, $extinfo) = each($exts)) {
	
		reset ($extinfo);
		
		while (list($dummy, $fileinfo) = each($extinfo)) {
		  $indent = $max_title_length;
		  if ($first) {
		  	$indent -= strlen($title);
		  	$first = 0;
		  }
			echo str_repeat2(' ', $indent);
			$file = $fileinfo["file"];
			$size = $fileinfo["size"];
			if ($size == "") {
				echo "<A HREF=\"dir.php3?dir=" . $dir . $file . "\">";
			  $file .= "/";
			}
			else
				echo "<A HREF=\"" . $dir . $file . "\">";
			echo $file . "</A>";
			echo str_repeat2(' ', $max_file_length - strlen($file));
			echo $size;
			echo str_repeat2(' ', 8 - strlen($size));
			echo date("d M Y H:i", $fileinfo["date"]) . "\n";
	  }
	}	
}
echo "</PRE>";

include("post.php3");
?>

