<?
	function MakeList($dir) {
		global $found, $dirfound;
		global $basedir, $filezzz, $filenb, $filedir, $filezzzdir, $filename;
		global $savetext;
		
		chdir($basedir.$dir);
		$dirnb = 0;
		$zzzalert=false;
		echo "goto dir : ".$basedir.$dir."<br>";
		$handle=opendir($basedir.$dir);
		while ((($file = readdir($handle))!=false) and (!($zzzalert))) {
				if ($file != ".") {
					if ($file != "..") {
						$filenb++;
						$filename[$filenb] = $file;
						$filedir[$filenb] = $dir;
						if (stristr($file,"zzzdir")) {
							echo "*** zzz file found ***<br>";
							$zzzalert=true;
							$filezzz++;
							$filezzzdir[$filezzz] = $dir;
						}
#						echo "***".$dir."/".$file."***<br>";
						
						if (is_dir($file)) {
							$filenb--;
							$dirnb++;
							$dirarray[$dirnb] = $dir.'/'.$file;
						}
					}
				}
		}
		closedir($handle); 
		
		for ($i=1; $i <= $dirnb; $i++) {
			$ok=true;
			for ($j=1; $j<=$filezzz; $j++) {
				if (strpos($dirarray[$i], $filezzzdir[$j]) === false) {
				} else {
					echo "- not going into ".$dirarray[$i]."<br>";
					$ok=false;
				}
			}
			
			if ($ok) {
				MakeList($dirarray[$i]);
			}
		}
	}
	
	$filenb = 0;
	$filezzz = 0;
	echo "<b>Building paths and file list.</b><br><br>";
	echo "HTTP_HOST is <b>".$HTTP_HOST."</b><br>";
	$basedir = $PATH_TRANSLATED;
	echo "PATH_TRANSLATED is <b>".$PATH_TRANSLATED."</b><br>";
	$basedir = substr($basedir, 0, strrpos($basedir, "/")+1);
	echo "Physical host path is <b>".$basedir."</b><br>";
	$basedir = substr($PATH_TRANSLATED, strlen($DOCUMENT_ROOT), strlen($PATH_TRANSLATED)-strlen($DOCUMENT_ROOT));
	$basedir = substr($basedir, 0, strrpos($basedir, "/")+1);
	echo "Relative Local Path is <b>".$basedir."</b><br>";
	$basedir = $DOCUMENT_ROOT;
	echo "Building file list from <b>".$basedir."</b><br>";
	MakeList('');
/*
	for ($i=1; $i<=$filenb; $i++) {
		echo $i." ".$filename[$i]."<br>";
	}
	echo "<br><hr><br>";
*/
#	echo $filenb."<br>";
	$totalzzzfile=0;
	for ($j=1; $j<=$filenb; $j++) {
		for ($i=1; $i<=$filezzz; $i++) {
			if ($filedir[$j] == $filezzzdir[$i]) {
			if (stristr($filename[$j],"zzzdir")==false) {
				$totalzzzfile++;
				$filename[$j] = "";
				$filedir[$j] = "";
			}
			}
		}
	}
/*
	for ($i=1; $i<=$filenb; $i++) {
		echo $i." ".$filename[$i]."<br>";
	}
	echo "<br><hr><br>";
*/
#	echo "total: ".$totalzzzfile."<br>";
	$i=0;
	$j=0;
	do {
		$i++;
		$j++;
		if ($filename[$j] == "") {
#			echo "from ".$j."<br>";
			do {
				$j++;
#				echo $j."<br>";
			} while (($j<=$filenb) and ($filename[$j] == ""));
#			echo "to ".$j."<br>";
		}
		$filename[$i] = $filename[$j];
		$filedir[$i] = $filedir[$j];
	} while ($j<=$filenb);

	$filenb=$filenb-$totalzzzfile;
#	echo $filenb."<br>";

#	echo "<br><hr><br>";
	$savetext="";
	for ($i=1; $i<=$filenb; $i++) {
#		echo $i." ".$filename[$i]." ".$filedir[$i]."<br>";
		$savetext.=$filename[$i].";";
		$savetext.=$filedir[$i].chr(10);
	}

	echo "<b>Saving... ";
	$fp = fopen ($basedir."/zzzfilelist.csv", "w");
	fwrite($fp, $savetext);
	fclose($fp);
	echo "Results saved.</b><br><br><hr><br>";
	
	echo "<b>Duplicata</b><br><br>";
	for ($i=1; $i<=$filenb; $i++) {
		for ($j=$i+1; $j<=$filenb; $j++) {
			if (($i<>$j) and ($filename[$i]==$filename[$j]) && ($filename[$i] <> ".htaccess") && ($filename[$i] <> "index.html") && ($filename[$i] <> "index.php")) {
				echo $filename[$i]." duplicated (".$filedir[$i]." and ".$filedir[$j].")<br>";
			}
		}
	}
	echo "<b>End of duplicata list</b><br>";
?>