<?
//(C) copyright Metalhead 2002
//This script is released under the terms of the GNU General Public License. A copy of the GPL is included with this script.
?>

<html>
<head>
<title>Sitemap</title>
<style type="text/css" media=screen>
body { line-height: 0 }  
<!--This is needed to make the segments of the directory tree fit seamless. Well, at least it works in SOME browsers.-->
</style>
</head>

<body>

<b>Sitemap</b><p>

<form action=<? echo $PHP_SELF; ?> method=post>
Display <input type=radio name=display value=PHP>PHPs&nbsp;<input type=radio name=display value=HTML>HTMLs&nbsp;<input type=radio name=display value=both checked=yes>both&nbsp;&nbsp;&nbsp;
<input type=submit value=Select>
</form>
<p>

<?

$stime = gettimeofday();

//This is just for the page generated in... at the bottom of the page. As executing the script may take some time with large pages, I thought it quite useful to add this.

$root = getcwd();

$pre = explode("/", $REQUEST_URI);
array_pop($pre);
$prefix = join("/", $pre);

//Determine whether the script runs in the root directory of the domain. If not, add director(y/ies) it's in as a prefix before the links

echo "<img src=server.gif> http://$SERVER_NAME$prefix/<br><img src=vertical.gif><br>";

function list_dir($cwd, $dir)
{
		  global $root, $display, $prefix;
		  unset($sdirs);
		  unset($sfiles);
		  $cwd1[0] = $cwd;
		  $cwd1[1] = $dir;
		  $chdir = join("/", $cwd1);
		  chdir($chdir);
		  
		  //delete all files from previous runs not to mess up the script with the old data, determine the path of the desired working directory and switch to it
		  
		  $handle = opendir('.');
		  while (false !== ($file = readdir($handle)))
		  {
		   		$PHP_SELF1 = basename($PHP_SELF);
				if(is_dir($file) && $file != "." && $file != "..")
		  		{
	   	  		 		 $sdirs[] = $file;
			    }
				elseif(is_file($file) && $file != "$PHP_SELF1" && (ereg("php$", $file) || ereg("html$", $file) || ereg("htm$", $file) || ereg("shtml$", $file)))
				{
	  			 		 $sfiles[] = $file;
				}
		  }
		  
		  //read the content of the directory, save directories, PHP and HTML files (except the script itself)
		  
		  if(is_array($sdirs))
		  {
							 sort($sdirs);
		  					 reset ($sdirs);
							 
							 $dir = getcwd();
		  					 $dir1 = str_replace($root, "", $dir);
		 					 $count = substr_count($dir1, "/") + substr_count($dir1, "\\");
							 
							 //determine the number of subdirectories we're in to be able to add the appropriate number of |s before the directory name
		  					 
		  					 for($y=0; $y<sizeof($sdirs); $y++)
		  					 {
		    				  	  for($z=1; $z<=$count; $z++)
		  						  { echo "<img src=vertical.gif>&nbsp;&nbsp;&nbsp;"; }
								  echo "<img src=verhor.gif>";
								  echo "<img src=folder.gif> $sdirs[$y]<br>";
								  list_dir($dir, $sdirs[$y]);
								  
								  //call the script itself in all directories found to recursively read all subdirectories
							 }
		  					 for($z=0; $z<=$count; $z++)
		  					 { echo "<img src=vertical.gif>&nbsp;&nbsp;&nbsp;"; }
							 echo "<br>";
		  }
		  
		  //if directories were found, display them in a tree
		 		  
		  chdir($chdir);
		  
		  //change back to the directory the files are in, we need to do this because in the loop above we've read all subdirectories and therefore changed the working directory
		  
		  if(is_array($sfiles))
		  {
		   					 sort($sfiles);
		 					 reset($sfiles);
							 
							 $dir = getcwd();
		  					 $dir = str_replace($root, "", $dir);
		  					 $count = substr_count($dir, "/") + substr_count($dir, "\\");
		  					 
							 //Same as above - determine number of subdirectories. We can't keep this variable as the loop above resets it for every subdirectory.
							 
							 if(!isset($display))
							 { $display = "both"; }
							 
							 if($display == "both")
							 {
							 for($y=0; $y<sizeof($sfiles); $y++)
		  					 {
		    				  	  if(ereg("php$", $sfiles[$y]))
								  {
								  for($z=1; $z<=$count; $z++)
		  					 	  { echo "<img src=vertical.gif>&nbsp;&nbsp;&nbsp;"; }
							 	  echo "<img src=verhor.gif>";  echo "<img src=php.gif> ";
								  echo "<a href=$prefix$dir/$sfiles[$y]>$sfiles[$y]</a><br>";
								  }
								  elseif(ereg("html$", $sfiles[$y]) || ereg("htm$", $sfiles[$y]) || ereg("shtml$", $sfiles[$y]))
								  {
								  for($z=1; $z<=$count; $z++)
		  					 	  { echo "<img src=vertical.gif>&nbsp;&nbsp;&nbsp;"; }
							 	  echo "<img src=verhor.gif>";  echo "<img src=html.gif> ";
								  echo "<a href=$prefix$dir/$sfiles[$y]>$sfiles[$y]</a><br>";
								  } 
		  					 }
							 }
							 elseif($display == "PHP")
							 {
							 for($y=0; $y<sizeof($sfiles); $y++)
		  					 {
		    				  	  if(ereg("php$", $sfiles[$y]))
								  {
								  for($z=1; $z<=$count; $z++)
		  					 	  { echo "<img src=vertical.gif>&nbsp;&nbsp;&nbsp;"; }
							 	  echo "<img src=verhor.gif>";  
								  echo "<img src=php.gif> ";
								  echo "<a href=$prefix$dir/$sfiles[$y]>$sfiles[$y]</a><br>";
								  }
							 }
							 }
							 elseif($display == "HTML")
							 {
							 for($y=0; $y<sizeof($sfiles); $y++)
		  					 {
		    				  	  if(ereg("html$", $sfiles[$y]) || ereg("htm$", $sfiles[$y]) || ereg("shtml$", $sfiles[$y]))
								  {
								  for($z=1; $z<=$count; $z++)
		  					 	  { echo "<img src=vertical.gif>&nbsp;&nbsp;&nbsp;"; }
							 	  echo "<img src=verhor.gif>";  
								  echo "<img src=html.gif> ";
								  echo "<a href=$prefix$dir/$sfiles[$y]>$sfiles[$y]</a><br>";
								  }
							 }
							 }
							 
							 //check which files to display and display them
							 
		  					 for($z=1; $z<=$count; $z++)
		  					 { echo "<img src=vertical.gif>&nbsp;&nbsp;&nbsp;"; }
							 echo "<br>"; 
		  }
		  
		  //now display the files in the directory with links to themselves
}

//We defined this function because we needed a script that calls itself recursively to read all subdirectories on the server. Now we just need to call it.

list_dir($root, "");

$ftime = gettimeofday();
$time = round(($ftime[sec] + $ftime[usec] / 1000000) - ($stime[sec] + $stime[usec] / 1000000), 5);
echo "<center>This page was generated in $time seconds.</center>";

?>

</body>
</html>
