<?php
/*******************************************************************
 * PHP Directory Explorer
 * Version 1.3
 * Website: www.mafiatic.com
 *******************************************************************/

##### SETTINGS ###################################
	$site_title  = 'Index of Your Site';		//Title of the page or your website
	$file_dir    = 'demofiles';					//Folder to start indexing from
	$script_file = 'index.php';					//Script file name, default is index.php
	$admin_email = 'webmaster@example.com';		//E-mail address, displayed if error occurs
	$icons_dir   = 'icons';						//Directory containing icon images
	$table_width = '920';						//Width of the table
	$single_img  = 'off';						//Use simgle image: _file.png for all files, on/off
	$anti_leech  = 'on';						//Enable download link protection, on/off
	$font_family = 'tahoma,arial,sans-serif';	//Font options
	$body_color  = '#FFFFFF';					//Page background color
	$text_color  = '#666666';					//Text color
	$link_color  = '#006699';					//Link color
	$dots_color  = '#888888';					//Dots/border color
	$show_icon   = 'yes';						//Show file icon column, yes/no
	$show_size   = 'yes';						//Show file size column, yes/no
	$show_type   = 'yes';						//Show file type column, yes/no
	$show_modf   = 'yes';						//Show file last modified column, yes/no
	$show_parent = 'yes';						//Show parent directory, yes/no
	$show_ext    = 'no';						//Show file extension, yes/no
	$login_user  = 'test';						//Login username
	$login_pass  = '';							//Login password, leave blank to open access
##### SETTINGS ###################################

if(!empty($login_pass)){
	session_start();
}
if($_GET['l']=='logout'){
	unset($_SESSION['username']);
	unset($_SESSION['password']);
	header('Location:'.$script_file.'');
	exit();
}
if(!empty($login_pass)){
	if(($_POST['username']==$login_user)&&($_POST['password']==$login_pass)){
		$_SESSION['username'] = $login_user;
		$_SESSION['password'] = md5($login_pass);
		header('Location:'.$script_file.'');
		exit();
	}
	elseif((($_POST['username'])||($_POST['password']))&&(($_POST['username']!==$login_user)||($_POST['password']!==$login_pass))){
		err("ERROR 401","The username and password you provided are incorrect.<br/>Please <a href='$script_file'>click here</a> to go back.");
	}
}
function h(){
	global $site_title, $font_family, $body_color, $text_color, $link_color, $dots_color;
	print'<html>
		<head>
		<meta http-equiv="pragma" content="no-cache">
		<title>'.$site_title.'</title>
		<style type="text/css">
		body,td,div,input{
			font-family:'.$font_family.';
			font-size:14px;
		}
		body,td,input{
			background-color:'.$body_color.';
			color:'.$text_color.';
		}
		a{
			color:'.$link_color.';
			text-decoration:none;
		}
		a:hover{
			color:'.$link_color.';
			text-decoration:underline;
		}
		hr.x{
			color:'.$body_color.';
			background-color:'.$body_color.';
			border:1px dotted '.$dots_color.';
			border-style:none none dotted;
		}
		td.y{
			vertical-align:bottom;
		}
		td.z{
			font-size:16px;
		}
		td.w{
			font-size:12px;
			text-align:right;
		}
		div.box{
			position:absolute;
			width:460px;
			height:200px;
			top:50%;
			left:50%;
			margin-left:-230px;
			margin-top:-100px;
		}
		div.box .x{
			font-size:14px;
			text-align:center;
			border-bottom:1px dotted '.$dots_color.';
			font-weight:bold;
			padding-bottom:10px;
		}
		div.box .y{
			font-size:13px;
			text-align:center;
			margin-top:10px;
		}
		form label{
			display:block;
			width:80px;
			padding-top:3px;
			margin-left:40px;
			float:left;
		}
		form input{
			display:block;
			margin-bottom:10px;
			padding:5px;
			border:solid 1px '.$dots_color.';
			width:250px;
			height:25px;
			font-size:12px;
		}
		.button{
			width:80px;
			font-size:12px;
			padding-top:3px;
		}
		</style>
	</head>
	<body>';
}
function f() {
	print'</body>
	</html>';
	exit();
}
function x($s){
	$p=strrpos($s,'.');
	return substr($s,$p+1,strlen($s));
}
function y($a,$c,$o='asc',$t=SORT_STRING){
	if(!isset($a[0][$c])){
		return $a;
	}
	for($i=0;$i<count($a);$i++){
		$w[$i]=&$a[$i][$c];
	}
	$o=($o=='asc')?SORT_ASC:SORT_DESC;
	array_multisort($w,$o,$t,$a);
	return $a;
}
function z($x){
	$y=strrpos($x,".");
	$z=substr($x,$y+1);
	return $z;
}
function err($x,$y){
	h();
	print'<div class="box">
		<div class="x">'.$x.'</div>
		<div class="y">'.$y.'</div>
	</div>';
	f();
}
$b=$_GET["d"];
$q=$_GET["f"];
if((!empty($login_pass))&&((!isset($_SESSION['username']))||(!isset($_SESSION['password'])))){
	if($q){
		err("ERROR 403","You are not authorized to access the requested file.<br>Please contact the webmaster at $admin_email.");
	}
	h();
	print'<div class="box">
		<div class="x">'.$site_title.'</div>
		<div class="y">'.$y.'</div>
		<form method="post" action="'.$script_file.'">
			<label>Username:</label><input type="text" name="username">
			<label>Password:</label><input type="password" name="password">
			<label></label><input type="submit" class="button" value="Login">
		</form>
	</div>';
	f();
}
elseif((empty($login_pass))||(isset($_SESSION['username']))&&(isset($_SESSION['password']))){
	if((substr($b,0,1)=='.')||(substr($q,0,1)=='.')||(substr($b,0,3)=='%2e')||(substr($q,0,3)=='%2e')){
		err("ERROR 400","Access denied due to bad request.<br>Please contact the webmaster at $admin_email.");
	}
	elseif(($b!='')&&($q!='')){
		$cwd=getcwd();
		$n=strtolower($_SERVER["HTTP_USER_AGENT"]);
		if($b=='d'){
			$g="$cwd/$file_dir/";
		}
		else{
			$g="$cwd/$file_dir/$b/";
		}
		$e=$g.$q;
		if(!file_exists($e)){
			err("ERROR 404","The file you have requested is not found.<br>Please contact the webmaster at $admin_email.");
		}
		elseif(!is_readable($e)){
			err("ERROR 500","Internal server error with read permission.<br>Please contact the webmaster at $admin_email.");
		}
		$m=fopen($g.$q,"r");
		$v=fread($m,filesize($g.$q));
		fclose($m);
		header('Content-Type: application/download');
		header('Content-Disposition:attachment;filename="'.$q.'"');
		print $v;
		exit();
	}
	else{
		$j=isset($_GET['s'])?$_GET['s']:"n";
		$k=isset($_GET['o'])?$_GET['o']:'asc';
		$l=$j==='s'||$j==='m'?SORT_NUMERIC:SORT_STRING;
		$d=isset($_GET['d'])?urldecode($_GET['d']):'';
		$d=$d!==''&&$d!=='/'?trim($d,'/').'/':'';
		$dir="$file_dir/";
		$p=$dir.$d;
		$h=@opendir($p);
		if(strstr($d,'../')){
			err("ERROR 403","Access to the requested directory is forbidden.<br>Please contact the webmaster at $admin_email.");
		}
		elseif(!$h){
			err("ERROR 404","The directory you have requested is not found.<br>Please contact the webmaster at $admin_email.");
		}
		$y=array();
		$x=array();
		while(FALSE!==($f=readdir($h))){
			if($f[0]!=='.'){
				if(is_dir($p.$f)){
					$x[]=array('n'=>$f,'m'=>filemtime($p.$f),'s'=>filesize($p.$f),'t'=>'Directory');
				}
				else{
					$y[]=array('n'=>$f,'m'=>filemtime($p.$f),'t'=>x($f),'s'=>filesize($p.$f));
				}
			}
		}
		if($j){
			$y=y($y,$j,$k,$l);
			$x=y($x,$j,$k,$l);
		}
		h();
		print'<div align="center">';
		print'<table cellspacing="2" cellpadding="1" width="'.$table_width.'">';
		print'<tr>';
		if(!empty($login_pass)){
			print'<td class="z" colspan="4">';
		}
		else{
			print'<td class="z" colspan="5">';
		}
		print($d!=='')?"<a href='$script_file' style='text-decoration:none'><b>$site_title</b></a> / ":"<b>$site_title</b>";
		if($d!==''){
			$t=explode('/',trim($d,'/'));
			for($i=0,$r=array(),$z='';($r[]=@$t[$i]),$z=@$t[$i];$i++){
				print(implode('/',$r)!==trim($d,'/'))?('<a href="'.$script_file.'?d='.implode('%2F',$r).'" style="text-decoration:none"><b>'.$z.'</b></a> / ' ):"<b>$z</b>";
			}
		}
		$f=trim($d,'/');
		print'</td>';
		if(!empty($login_pass)){
			print'<td class="w"><a href="'.$script_file.'?l=logout">Logout</a></td>';
		}
		print'</tr>';
		print'<tr><td colspan="5"><hr class="x"/></td></tr>';
		print'<tr>';
		if($show_icon=='yes'){
			print'<td class="y" width="3%"><img src="'.$icons_dir.'/_spacer.png"></td>';
		}
		print'<td class="y" width="42%"><a href="'.$script_file.'?s=n&amp;o='.($k=='asc'?'dsc':'asc').'&amp;d='.urlencode($f).'">Name</a></td>';
		if($show_size=='yes'){
			print'<td class="y" width="18%" align="right"><a href="'.$script_file.'?s=s&amp;o='.($k=='asc'?'dsc':'asc').'&amp;d='.urlencode($f).'">Size</a></td>';
		}
		if($show_type=='yes'){
			print'<td class="y" width="12%" align="right"><a href="'.$script_file.'?s=t&amp;o='.($k=='asc'?'dsc':'asc').'&amp;d='.urlencode($f).'">Type</a></td>';
		}
		if($show_modf=='yes'){
			print'<td class="y" width="25%" align="right"><a href="'.$script_file.'?s=m&amp;o='.($k=='asc'?'dsc':'asc').'&amp;d='.urlencode($f).'">Last Modified</a></td>';
		}
		print'</tr>';
		print'<tr><td colspan="5"><hr class="x"/></td></tr>';
		if($show_parent=='yes'){
			$pd = str_replace(array("\r\n","\r","\n"),' ',dirname($d).PHP_EOL);
			if(($pd=='. ')||(($pd!=="")&&($pd!==" "))){
				print'<tr>';
				if($show_icon=='yes'){
					print'<td><img src="'.$icons_dir.'/_parent.png"></td>';
				}
			}
			if($pd=='. '){
				print'<td><a href="'.$script_file.'">...Parent Directory</a></td>';
			}
			elseif(($pd!=="")&&($pd!==" ")){
				print'<td><a href="'.$script_file.'?d='.$pd.'">...Parent Directory</a></td>';
			}
			if(($pd=='. ')||(($pd!=="")&&($pd!==" "))){
				if($show_size=='yes'){
					print'<td></td>';
				}
				if($show_type=='yes'){
					print'<td></td>';
				}
				if($show_modf=='yes'){
					print'<td align="right">'.date('m/d/Y h:i A',time()).'</td>';
				}
				print'</tr>';
			}
		}
		for($i=0,$c='';($c=@$x[$i++]);){
			print'<tr>';
			if($show_icon=='yes'){
				print'<td><img src="'.$icons_dir.'/_folder.png"></td>';
			}
			print'<td><a href="'.$script_file.'?d='.urlencode($d.$c['n']).'&amp;s=n&amp;o=asc">'.$c['n'].'</a></td>';
			if($show_size=='yes'){
				print'<td></td>';
			}
			if($show_type=='yes'){
				print'<td></td>';
			}
			if($show_modf=='yes'){
				print'<td align="right">'.date('m/d/Y h:i A',$c['m']).'</td>';
			}
			print'</tr>';
		}
		for($i=0,$c='';($c=@$y[$i++]);){
			if($c['n']!==$script_file){
				print'<tr>';
				if($show_icon=='yes'){
					if($single_img=='on'){
						print'<td><img src="'.$icons_dir.'/_file.png"></td>';
					}
					else{
						print'<td><img src="'.$icons_dir.'/'.$c['t'].'.png"></td>';
					}
				}
				if($show_ext=='yes'){
					$fn=$c['n'];
				}
				else{
					$fn=str_replace('.'.$c['t'].'','',$c['n']);
				}
				if($anti_leech!=='on'){
					print'<td><a href="'.$dir.$d.$c['n'].'">'.$c['n'].'</td>';
				}
				elseif($d==''){
					print'<td><a href="'.$script_file.'?d=d&f='.$c['n'].'">'.$fn.'</a></td>';
				}
				else{
					print'<td><a href="'.$script_file.'?d='.substr($d,0,-1).'&f='.$c['n'].'">'.$fn.'</a></td>';
				}
				if($show_size=='yes'){
					print'<td align="right">'.number_format($c['s']/1048576,2).' MB</td>';
				}
				if($show_type=='yes'){
					print'<td align="right">'.strtoupper($c['t']).'</td>';
				}
				if($show_modf=='yes'){
					print'<td align="right">'.date('m/d/Y h:i A',$c['m']).'</td>';
				}
				print'</tr>';
			}
		}
		print'<tr><td colspan="5"><hr class="x"/></td></tr>';
		print'</table>';
		print'</div>';
		f();
	}
}
?>