File "index.php"

Full Path: /home/analogde/www/Prog/File explorer/010/index.php
File size: 12.48 KB
MIME-type: text/x-php
Charset: utf-8

<?php

session_start();

// Report all except warnings
error_reporting(E_ALL ^ E_WARNING);

// Create a new folder
if(isset($_REQUEST['create'])){
	$folder=$_REQUEST['create'];
	if(file_exists($_SESSION['path'].'/'.$folder)==FALSE){
		if(!mkdir($_SESSION['path'].'/'.$folder))
		{
			$errorMsg = "<script>alert('Lỗi khi tạo thư mục')</script>";
		}
	}
	else{
		$errorMsg = "<script>alert('Đã có thư mục trùng tên')</script>";
	}
	
	
}

// Delete files/folders
else if(isset($_REQUEST['delete'])){
	
	function recursiveDel($dir) {
		if ($dir[strlen($dir) - 1] != "/")
			$dir = $dir."/";
		$mydir = opendir($dir);
		while(($file = readdir($mydir)) !== false) {
			if($file != "." && $file != "..") {
				// Unix compat
				chmod($dir.$file, 0777);
				if(is_dir($dir.$file)) {
					chdir('.');
					recursiveDel($dir.$file.'/');
					rmdir($dir.$file);
				}
				else
					unlink($dir.$file);
			}
		}
		closedir($mydir);
		rmdir($dir);
	}
	
	$arr=split(",",$_REQUEST['delete']);
	while(list(,$file)=each($arr)){
		if(file_exists($file)){
			if(is_file($file)){
				unlink($file);
			}
			else if(is_dir($file)){
					recursiveDel($file);
			}
		}
		else{
			$errorMsg = "<script>alert('File hoặc thư mục không tồn tại')</script>";
		}
	}
	while(list(,$file)=each($arr)){
		if(file_exists($file))
			$errorMsg = "<script>alert('Lỗi khi xóa thư mục')</script>";
	}
}

// Navigate
else if(isset($_REQUEST['path'])){
	$_SESSION['path']=$_REQUEST['path'];
}

// Rename file/folder
else if(isset($_REQUEST['rename']) && isset($_REQUEST['newname'])){
	$oldName=$_REQUEST['rename'];
	$newName=$_REQUEST['newname'];
	if((file_exists($oldName)==TRUE) && (file_exists($_SESSION['path'].'/'.$newName)==FALSE)){
		if(is_file($oldName)){
			$ext = substr(strrchr($oldName, "."), 1 );
			$newName.=".".$ext;
		}
		if(!rename($oldName,$_SESSION['path'].'/'.$newName))
		{
			$errorMsg = "<script>alert('Lỗi khi đổi tên thư mục')</script>";
		}
	}
	else{
		$errorMsg = "<script>alert('Đã có file hoặc thư mục trùng tên')</script>";
	}
	
}

// Copy-Move files
else if(isset($_REQUEST['choosepath']) && $_REQUEST['choosepath']=='done'){
	if(isset($_SESSION['copy'])|| isset($_SESSION['move'])){
		if(isset($_SESSION['copy_move_path'])){
			$arr=array();
			if(isset($_SESSION['copy'])){
				$arr=split(",",$_SESSION['copy']);
			}
			else if(isset($_SESSION['move'])){
				$arr=split(",",$_SESSION['move']);
			}
			$newPath=$_SESSION['copy_move_path'];
			if(file_exists($newPath)){
				while(list(,$file)=each($arr)){
					if(file_exists($file) && is_file($file)){
						$fileName=$file;
						if(strrpos($file,"/")!=FALSE){
							$fileName = substr(strrchr($file, "/"), 1 );
						}
						if(!file_exists($newPath.'/'.$fileName)){
							copy($file,$newPath.'/'.$fileName);
							if(isset($_SESSION['copy'])){
								copy($file,$newPath.'/'.$fileName);
							}
							else if(isset($_SESSION['move'])){
								rename($file,$newPath.'/'.$fileName);
							}
							if(isset($_SESSION['move'])){
								unlink($file);
							}
						}
						else{
							$errorMsg = "<script>alert('FAILED!!!\nFile $newPath/$fileName exists')</script>";
						}
					}
				}
			}
			else {
				$errorMsg = "<script>alert('FAILED!!!\nDestination not exists')</script>";
			}
		}
	}
}

// No parameter
else{
	$_SESSION['path']='data';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Explorer</title>
<script type="text/javascript">
	
	// Get ID of a DOM HTML
	function $get(id)
	{
		return document.getElementById(id);
	}
	
	// Check folder name is valid ?
	function checkName(name){
		if(name.indexOf(":")>=0)
			return false;
		if(name.indexOf(".")>=0)
			return false;
		if(name.indexOf("..")>=0)
			return false;
		if(name.indexOf("/")>=0)
			return false;
		if(name.indexOf("\\")>=0)
			return false;
		return true;
	}
	
	// Hanlde when click UP button
	function goUp(){
		var upLink=$get("up");
		if(upLink){
			window.location.href = 'index.php?path=' + upLink.value;
		}
	}
	
	// Hanlde when click HOME button
	function goHome(){
		window.location.href = 'index.php';
	}
	
	// Hanlde when click CREATE button
	function create(){
		var folder=$get("txtFolderName");
		if(folder){
			if(folder.value==""){
				alert("Nhập tên thư mục đã...");
			}
			else{
				if(checkName(folder.value))
					window.location.href = 'index.php?create=' + folder.value;
				else
					alert("Tên thư mục không hợp lệ : . .. / \\");
					
			}
		}
	}
	
	// Hanlde when click DELETE button
	function del(){
		if(confirm("You really want to delete them ?")){
			var items=document.getElementsByName("fileName");
			var str="";
			for(var i=0;i<items.length;i++){
				if(items[i].checked){
					str+=items[i].value+",";
				}
			}
			str=str.substr(0,str.length-1);
			window.location.href = 'index.php?delete=' + str;
		}
	}
	
	// Hanlde when click RENAME button
	function rename(){
		var newName=prompt('Give me a new name,please!');
		if(newName==null || newName=="" || !checkName(newName)){
		
		}
		else{
			var items=document.getElementsByName("fileName");
			var str="";
			for(var i=0;i<items.length;i++){
				if(items[i].checked){
					str=items[i].value;
					break;
				}
			}
			window.location.href = 'index.php?rename=' + str + "&newname="+newName;
		}
	}
	
	// Hanlde when click COPY button
	function copy(){
		if(confirm("You really want to copy them ?")){
			var items=document.getElementsByName("fileName");
			var str="";
			for(var i=0;i<items.length;i++){
				if(items[i].checked){
					str+=items[i].value+",";
				}
			}
			str=str.substr(0,str.length-1);
			window.location.href = 'movecopyto.php?copy=' + str;
		}
	}
	
	// Hanlde when click MOVE button
	function move(){
		if(confirm("You really want to move them ?")){
			var items=document.getElementsByName("fileName");
			var str="";
			for(var i=0;i<items.length;i++){
				if(items[i].checked){
					str+=items[i].value+",";
				}
			}
			str=str.substr(0,str.length-1);
			window.location.href = 'movecopyto.php?move=' + str;
		}
	}
	
</script>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h1>Manhattan explorer</h1>
<?php
$outStr="";
$alt=true;
if(is_dir($_SESSION['path']))
{
	echo "Thư mục hiện tại: ".str_replace("data","Gốc",$_SESSION['path']);
	if (isset($errorMsg))
		echo $errorMsg;
	?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data" name="form1">
<div class="buttonbar">
	  <a href="#" onclick="goHome();"><img src="images/house.png" /> Gốc</a>&nbsp;&nbsp;
	  <a href="#" onclick="goUp();"><img src="images/up.png" /> Lên</a>&nbsp;&nbsp;
      <a href="#" onclick="rename();"><img src="images/ren.png" /> Đổi tên</a>&nbsp;&nbsp;
	  <a href="#" onclick="copy();"><img src="images/copy.png" /> Chép</a>&nbsp;&nbsp;
	  <a href="#" onclick="move();"><img src="images/move.png" /> Di chuyển</a>&nbsp;&nbsp;
	  <a href="#" onclick="del();"><img src="images/del.png" /> Xóa</a>&nbsp;&nbsp;
      
      |
	  <a href="#" onclick="create();"><img src="images/new.png" /> Tạo thư mục tên</a>&nbsp;
	  <input type="text" name="txtFolderName" id="txtFolderName" />
	
	  |
	  <a href="#" onclick="form1.action='upload.php';form1.submit()"><img src="images/upload.png" />Tải lên</a>
	  <input type="file" name="file" id="file" />
</div>
</form>
<table width="100%" cellpadding="0" cellspacing="0" class="list">	
	<tr class="listhead">
    	<td width="40%">Tên</td>
        <td width="15%">Kích cỡ</td>
        <td width="15%">Loại</td>
        <td width="15%">Sửa đổi cuối</td>
        <td width="15%">Phân quyền</td>
    </tr>
	<?php
	$newPath = $_SESSION['path'];
	// Filter malicious stuff
	$toRemove = array();
	$toRemove[] = "..";
	$toRemove[] = ":";
	$newPath = str_replace($toRemove,"",$newPath);
	// Not allow root, or anything outside the "data" directory
	if ($newPath[0] == "/" || $newPath[0] == "\\" || strpos($newPath, "data") != 0 || !is_dir($newPath))
		$newPath = "data";
	else
		$newPath = str_replace('//','/',str_replace('\\','/',$newPath));
	$dh  = opendir($newPath);
	while (false !== ($filename = readdir($dh))) {
		$files[] = $filename;
	}
	sort($files);
	while(list($i,$name)=each($files))
	{
		if($name!='.'){
			$tempPath = $newPath;
			if($name!='..'){
				$tempPath=$tempPath.'/'.$name;
				if (is_dir($tempPath))
					$clickJS="window.location.href='index.php?path=".urlencode($tempPath)."'";
				else
					$clickJS="window.location.href='$tempPath'";
				if (!$alt)
					$outStr.="<tr onclick=\"$clickJS\">";
				else
					$outStr.="<tr class=\"alt\" onclick=\"$clickJS\">";
				$alt = !$alt;
				// if current item is a folder
				if(is_dir($tempPath)){
					$outStr.="<td><input type='checkbox' value='$tempPath' name='fileName' onclick=\"javascript:if(!e){var e = window.event;if(!e){e = event;if(e.stopPropagation)e.stopPropagation();}e.cancelBubble = true;}\"/>$name</td>";
					$outStr.="<td>-</td>";
					$outStr.="<td>Thư mục</td>";
				}
				// if current item is a file
				else{
					$outStr.="<td><input type='checkbox' value='$tempPath' name='fileName' onclick=\"javascript:if(!e){var e = window.event;if(!e){e = event;if(e.stopPropagation)e.stopPropagation();}e.cancelBubble = true;}\"/>$name</td>";
					
					//get file size
					$size=filesize($tempPath);
					$strSize=$size." B";
					$a=array(' KB',' MB',' GB');
					$temp=$size*1.0/1024;
					$temp=round($temp,2);
					for($i=0;$i<3;$i++){
						if($temp>=1){
							$strSize=$temp.$a[$i];
							$temp=$temp*1.0/1024;
							$temp=round($temp,2);
						}
						else {
							break;
						}
					}
					$outStr.="<td>$strSize</td>";
					$outStr.="<td>Tập tin</td>";
				}
			}
			// if current item is .. --> parent of the current folder
			else{
				$idx=strrpos($tempPath,'/');
				if($idx==FALSE)
					continue;
				if($idx==strlen($tempPath)-1)
					continue;
				$tempPath=substr($tempPath,0,$idx);
				$encodedPath=urlencode($tempPath);
				if (!$alt)
					$outStr.="<tr onclick=\"window.location.href='index.php?path=$encodedPath'\">";
				else
					$outStr.="<tr class=\"alt\" onclick=\"window.location.href='index.php?path=$encodedPath'\">";
				$alt = !$alt;
				$outStr.="<td><input type=\"hidden\" id=\"up\" value=\"$encodedPath\" />$name</td>";
				$outStr.="<td>&nbsp;</td>";
				$outStr.="<td>Thư mục cha</td>";
			}
			
			// get Date Modified
			$modifiedDate=date ("d/m/Y", filemtime($tempPath));
			$outStr.="<td>$modifiedDate</td>";
			
			// get permission
			$perms = fileperms($tempPath);
			
			if (($perms & 0xC000) == 0xC000) {
				// Socket
				$info = 's';
			} elseif (($perms & 0xA000) == 0xA000) {
				// Symbolic Link
				$info = 'l';
			} elseif (($perms & 0x8000) == 0x8000) {
				// Regular
				$info = '-';
			} elseif (($perms & 0x6000) == 0x6000) {
				// Block special
				$info = 'b';
			} elseif (($perms & 0x4000) == 0x4000) {
				// Directory
				$info = 'd';
			} elseif (($perms & 0x2000) == 0x2000) {
				// Character special
				$info = 'c';
			} elseif (($perms & 0x1000) == 0x1000) {
				// FIFO pipe
				$info = 'p';
			} else {
				// Unknown
				$info = 'u';
			}
			// Owner
			$info .= (($perms & 0x0100) ? 'r' : '-');
			$info .= (($perms & 0x0080) ? 'w' : '-');
			$info .= (($perms & 0x0040) ?
					(($perms & 0x0800) ? 's' : 'x' ) :
					(($perms & 0x0800) ? 'S' : '-'));
			
			// Group
			$info .= (($perms & 0x0020) ? 'r' : '-');
			$info .= (($perms & 0x0010) ? 'w' : '-');
			$info .= (($perms & 0x0008) ?
					(($perms & 0x0400) ? 's' : 'x' ) :
					(($perms & 0x0400) ? 'S' : '-'));
			
			// World
			$info .= (($perms & 0x0004) ? 'r' : '-');
			$info .= (($perms & 0x0002) ? 'w' : '-');
			$info .= (($perms & 0x0001) ?
					(($perms & 0x0200) ? 't' : 'x' ) :
					(($perms & 0x0200) ? 'T' : '-'));
			
			$outStr.="<td class=\"fixedfont\">$info</td>";
			$outStr.="</tr>\r\n";
		}
	}
	echo $outStr;
}
else
{
	echo "Invalid directory. Possibly server error";
	$_SESSION['path'] = "data";
	//echo "<script>window.location.href='".$_SERVER["PHP_SELF"]."'</script>";
}
?>
</table>
</body>
</html>