PHPBuilder - really very small file manager



RSS Twitter
Snippets File Management

really very small file manager

by: NIkolay Mihaylov
|
March 11, 2002

Version: 0.9.7

Type: Full Script

Category: File Management

License: BSD License

Description: The very small file manager. I made it because there was not really small (one file) solution for this, written in PHP. The real idea for usage is to build extensible pseudo hosting solutions running on one and same user_id. The script is usable for putting in password protected folder in order to edit your own website, without FTP access (is really cool) The design is simple because I expect those file to be included in a template. The "includer" must provide the authentication (and to set working directory and URL) The script works with PHP4/Linux and uses cookies and javascript on the client side. I also expect some problems with Netscape Navigator, but nothing found yet.



<?
/*

Nikolay Mihaylov <nmmm@nmmm.nu> <info@photonhost.com>

http://nmmm.nu/

This software is under BSD licence.

----
little modified by Arthit Suriyawongkul art@siit.net - 2002.03.09

*/

//chroot(realpath("../New Folder/")); // Dont work :(

// this zero is for configuration switching... :)

$BASE_DIR = "../New Folder/"; // Will be canonized later.
$BASE_URL = "http://file.photonhost.kom/New%20Folder"; //do not include trailing slash.

$BASE_EDITABLE = 1; // 0/1

$DATE_FORMAT         = "Y-m-d";
$TIME_FORMAT         = "H:i"; //"H:i:s";
$UNKNOWN_FILE_FORMAT = "&nbsp;"; //"Unknown";

/*
TO DO:
---------------------------------------
Canonize CD path. (non security issue)
currently from time to time is:
/xxx/yyy/.. (instead of /xxx/)
the problem exists if you try to delete yyy directory.
---------------------------------------
User quota. (urgent because of e-nick.org and wap-bg.com)
---------------------------------------
Cut/Copy/Paste.
---------------------------------------
Determine if user has rights to upload/edit
e.g. quota support by crontab...
This is implemented now.
---------------------------------------
chmod and show file attr.
also must be flag if "advanced controls" are used.
---------------------------------------
*/

/*
0.9.7	Determine if user has rights to upload/edit/mkdir/mkfile
0.9.6	Submited some changes by art_krub@yahoo.com, the edit by me.
	Thanks.
0.9.5	Changed query string to use op=xxx, fixed filetypes
0.9.4	Added location after CD.
0.9.2	Fixed <> in the edit textarea
0.9.0	Build
*/

//file types descriptions:

$describe_ext = array(
	'htm'  => 'htm',	'html'  => 'htm',
	'txt'  => 'txt',	'text'  => 'txt',	'csv'  => 'txt',
	'css'  => 'css',
	'js'   => 'js',
	'php'  => 'php',	'php3'  => 'php',	'php4' => 'php',
	'phtm' => 'phtm',	'phtml' => 'phtm',
	'chtm' => 'chtm',	'chtml' => 'chtm',
	'shtm' => 'shtm',	'shtml' => 'shtm',
	'asp'  => 'asp',	'aspx'  => 'asp',
	'xml'  => 'xml',	'xsl'   => 'xml',	'xslt' => 'xml',	'dtd' => 'xml',
	'wml'  => 'wml',	'wmls'  => 'wml',	'hdml' => 'wml',

	'c'    => 'c',		'cpp'   => 'c',		'cxx'  => 'c',		'cc'  => 'c',
	'pl'   => 'c',		'bas'   => 'c',		'tcl'  => 'c',		'pas' => 'c',
	'java' => 'c',		'inc'   => 'c',		'bat'  => 'c',		'sh'  => 'c',

	'htaccess' => 'htaccess',
	'htpasswd' => 'htaccess',

	'jpg'  => 'pic',	'jpeg' => 'pic',	'jif'  => 'pic',	'png'  => 'pic',	'gif'  => 'pic',

	'swf'  => 'swf',
	'class'=> 'class',	'jar'  => 'class',

	'zip'  => 'zip',	'arj'  => 'zip',	'tar'  => 'zip',	'gz'  => 'zip',		'bz2' => 'zip',
	'uu'   => 'zip',	'uue'  => 'zip',


	// changed "ms" to "doc" (document), for more general description
	// added StarOffice/OpenOffice.org file types (sxw, sxc, sxi, sxd)
	'doc'  => 'doc',	'xls'  => 'doc',	'ppt'  => 'doc',	'pdf' => 'doc',
	'sxw'  => 'doc',    'sxc'  => 'doc',    'sxi'  => 'doc',    'sxd' => 'doc',


	'mpg'  => 'vid',	'mpeg' => 'vid',	'ram'  => 'vid',	'rm'  => 'vid',		'avi'  => 'vid',


	// changed "mp3" to "aud" (audio), for more general description
	'mp3'  => 'aud',    'mid'  => 'aud',	'midi' => 'aud',	'wav' => 'aud',

	'bak'  => 'bak'
);

$describe_type = array(
	// add 0 as "Unknown" file type
	0	=> $UNKNOWN_FILE_FORMAT,

	'htm'	=> "HTML file",
	'txt'	=> "Text file",
	'css'	=> "Style Sheet",
	'js'	=> "Java Script	source code",
	'php'	=> "PHP	source code",
	'phtm'	=> "PHP	or ePerl source	code",
	'chtm'	=> "CGI_Core source code",
	'shtm'	=> "Server parced HTML file",
	'asp'	=> "Active server pages",
	'xml'	=> "XML	/ XSLT / DTD file",
	'wml'	=> "WML	/ HDML file for	mobile internet",
	'c'	=> "Program or script source code",
	'htaccess' => "Apache security configuration file",
	'pic'	=> "Picture",
	'swf'	=> "Macromedia Flash object",
	'class'	=> "Java class file",	// change from "Java applet file" to "Java class file"
	'zip'	=> "Archive",
	'doc'	=> "Office document or similar file",
	'vid'	=> "Video file",
	'aud'	=> "Audio file",	// change from "Music file" to "Audio file", more generic.
	'bak'	=> "Backup copy"
);

// e-edit

$describe_op = array(
	'htm'	=> "e",
	'txt'	=> "e",
	'css'	=> "e",
	'js'	=> "e",
	'php'	=> "e",
	'phtm'	=> "e",
	'chtm'  => "e",
	'shtm'	=> "e",
	'asp'	=> "e",
	'xml'	=> "e",
	'wml'	=> "e",
	'c'	=> "e",
	'htaccess' => "e",
);

// **************************************************************************************
// **************************************************************************************
// **************************************************************************************
// **************************************************************************************
// **************************************************************************************

function HTMLdecode($s){
	//return htmlentities($s);
	//There was too much problems with the cyrillic...
	//	nmmm
	$o = "";
	for ($i=0; $i < strlen($s); $i++){
		switch ($s[$i]){
			case "<": $o = $o."&lt;"; break;
			case ">": $o = $o."&gt;"; break;
			case "&": $o = $o."&amp;"; break;
			case "\"": $o = $o."&quot;"; break;
			default: $o = $o.$s[$i];
		}
	}
	return $o;
}

//this is for head message
function dirmessage($s){
	echo "<b>$s</b>";
}

//this is for error messages (over the header message)
function message($s){
	echo "<b>$s</b>";
}

function goheader($res, $msg){
	header("location: ?".($res ? "" : "msg=".urlencode($msg) ) );
}

// **************************************************************************************
//=== handing cd request ===
// **************************************************************************************

// does $op already declared?, if not declare it
if (!isset($op))
	$op = "";

if ($op=='cd'){
	// does $dir1 already declared?, if not declare it
	if(!isset($dir1))
		$dir1 = "";

	SetCookie("dir1", "$dir1/$file");

	goheader(1, "");
}

// **************************************************************************************
//=== Checking directory ===
// **************************************************************************************

$BASE_DIR=realpath($BASE_DIR);


// do $dir and $dir1 already declared?, if not declare them
if (!isset($dir))
	$dir = "";

if (!isset($dir1))
	$dir1 = "";


$dir=realpath("$BASE_DIR/$dir1");

if ( strpos("x".$dir, $BASE_DIR) != 1 ){
	$dir = $BASE_DIR;
	SetCookie("dir1", "");
}

@chdir($dir);

//used to show to the users (chroot-like)....
$shdir=substr($dir, strlen($BASE_DIR))."/";

// **************************************************************************************
//=== Fixing URL ===
// **************************************************************************************

$url=$BASE_URL.substr($dir, strlen($BASE_DIR))."/";

if ($op){

/*
	del	op	file
	ren	op	file	newfile
	mkdir	op	file
	mkfile	op	file
	newfile	op	file	file_name	//this is file upload
	newedit	op	file	(content)	//this is after edit
	edit	op	file
*/

	switch ($op){

		case 'del':

			// **************************************************************************************
			//=== handing delete ===
			// **************************************************************************************

			$file=basename($file);

			if (is_dir($file))
				$res=@rmdir ($file);
			else
				$res=@unlink($file);

			goheader($res, "Delete failed. If this is a directory, be sure is empty.");

			break;

		case 'ren':
			// **************************************************************************************
			//=== handing rename ===
			// **************************************************************************************

			$file    = basename($file);
			$newfile = basename($newfile );
			$res     = @rename($file, $newfile);
			goheader($res, "Rename failed.");

			break;

		case 'mkdir':
			// **************************************************************************************
			//=== handing mkdir ===
			// **************************************************************************************

			$file = basename($file);

			$res = 0;
			if ($BASE_EDITABLE){
				$res  = @mkdir($file, 0755);
			}

			goheader($res, "Create directory failed.");

			break;

		case 'mkfile':
			// **************************************************************************************
			//=== handing mkfile ===
			// **************************************************************************************

			$mkfile = basename($file);

			$fd = 0;
			if ($BASE_EDITABLE){
				$fd = @fopen($file, "w+");
				@fclose($fd);
			}

			goheader($fd, "Create file failed.");

			break;

		case 'newfile':
			// **************************************************************************************
			//=== handing file upload ===
			// **************************************************************************************

			$file_name = basename($file_name);

			$res = 0;
			if ($BASE_EDITABLE){
				$res = move_uploaded_file($file, $file_name);
			}

			goheader($res, "File upload failed.");

			break;

		case 'newedit':

			// **************************************************************************************
			//=== handing newedit - the return after edit ==
			// **************************************************************************************

			$file = basename($file);

			$res = 0;
			if ($BASE_EDITABLE){
				$fd=fopen($file, "w+");
				fwrite($fd, $content);
				fclose($fd);
			}

			goheader($fd, "Edit upload failed.");

			break;
	} // switch
} // if ($op)

// **************************************************************************************
//=== handing file type - returns description of the file.
// **************************************************************************************

function describe($f){
	global $describe_ext;

	$m    = explode(".", $f);
	$size = (sizeof($m)-1);
	$ext  = strtolower($m[$size]);

	if ( filetype($f) != 'file' && $size == 0 )
		return 0;

	// check if $ext is a "known" file type
	if (isset($describe_ext[$ext]))
		return $describe_ext[$ext];   // if yes, return its describe_ext

	return 0;   // if not, return as "unknown"
}

// **************************************************************************************
// === print a row of the table ===
// **************************************************************************************

function printfile($f){
	global $describe_type, $describe_op, $url, $DATE_FORMAT, $TIME_FORMAT;

	if($f == "." || $f == "..")
		return;

	$type  = filetype($f);
	$size  = $type != 'file' ? "&nbsp;" : round(filesize($f) / 1024)." KB";
	$descr = $type != 'file' ? "&nbsp;" : $describe_type[describe($f)];

	if ($type == 'dir')
		$links = "[<a href='?op=cd&file=".urlencode($f)."'>cd</a>]";
	else if ($describe_op[describe($f)] == 'e')
		$links = "[<a href='?op=edit&file=".urlencode($f)."'>edit</a>]";
	else
		$links = "&nbsp;";


	echo "
		<tr>
	";

	if ($type == 'dir'){
		echo "	<td><a href='?op=cd&file=".urlencode($f)."'>$f</a></td>";
	}else{
		echo "	<td><a href='$url$f' target='preview'>$f</a></td>";
	}

	$ftime = filemtime($f);
	$time  = date( $DATE_FORMAT, $ftime );
	$time1 = date( $TIME_FORMAT, $ftime );

	echo "
			<td align='right'>$size</td>
			<td align='center'>$time</td>
			<td align='center'>$time1</td>
			<td align='center'>$type</td>
			<td>$descr&nbsp;</td>
			<td align='center'>
				[<a href='javascript:show_delete_dialog(\"$f\")'>del</a>]
			</td>
			<td align='center'>
				[<a href='javascript:show_rename_dialog(\"$f\")'>ren</a>]
			</td>
			<td align='center'>
				$links
			</td>
		</tr>
	";
}

// **************************************************************************************
// === print the table with files ===
// **************************************************************************************

function printfilelist(){
	global $shdir, $msg;
?>
<script><!--

function show_rename_dialog(f){
	a = prompt("Please enter new name:", f);

	if (!a)		return;
	if (a == f)	return;

	location = "?op=ren&newfile=" + escape(a) + "&file=" + escape(f);
}

function show_delete_dialog(f){
	if (confirm("Are you sure you want to delete file " + f))
		location = "?op=del&file=" + escape(f);
}

//--></script>

<center>

<?
if ($msg){
	message($msg);
	echo "<hr>";
}

dirmessage($shdir);
?>

<hr>

<table border="1">
<tr>
	<th>Name</th>
	<th>Size</th>
	<th>Date</th>
	<th>Time</th>
	<th>Type</th>
	<th>Description</th>
	<td  colspan='3' align='center'>[<a href="?">refresh</a>]</td>
</tr>
<tr>
	<td><a href='?op=cd&file=..'>..</a></td>
	<td align='right'>&nbsp;</td>
	<td align='center'>&nbsp;</td>
	<td align='center'>&nbsp;</td>
	<td align='center'>dir</td><td>One level up</td>
	<td align='center'>&nbsp;</td>
	<td align='center'>&nbsp;</td>
	<td align='center'>[<a href='?op=cd&file=..'>cd</a>]</td>
</tr>
<?

	$d = dir(".");

	while ( $entry = $d->read() ) {
    		printfile($entry);
	}

	$d->close();

?>
</table>

<hr>

<table border="0">
<form>
<input type="hidden" name="op" value="mkdir">
<tr><td>Create new directory:</td><td><input type="text" name="file"> <input type="submit" value="&gt;&gt;"></td></tr>
</form>

<form>
<input type="hidden" name="op" value="mkfile">
<tr><td>Create new text file:</td><td><input type="text" name="file"> <input type="submit" value="&gt;&gt;"></td></tr>
</form>

<form enctype="multipart/form-data" method="post">
<input type="hidden" name="op" value="newfile">
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
<tr><td>Upload a file:</td><td><input name="file" type="file"> <input type="submit" value="&gt;&gt;"></td></tr>
</form>

</table>

</center>

<?
} //Print file list

// **************************************************************************************
// === edit file ===
// **************************************************************************************

function editfile($f){
	global $shdir;

	$f = basename($f);
?>

<center>

<form method="post" action='?'>
<input type="hidden" name="file" value="<? echo $f; ?>">
<input type="hidden" name="op" value="newedit">

<? dirmessage("$shdir$f"); ?>
<hr>
<table><tr>
	<td><input type="checkbox" onclick="form.content.wrap=checked ? 'soft' : 'off'"> Wrap Lines</td>
	<td>&nbsp;&nbsp;&nbsp;</td>
	<td><input type="button" value="Previev" onclick="x=window.open('','preview'); x.document.write(form.content.value)"></td>
</tr></table>
<hr>

<textarea cols="60" rows="20" wrap="off" name="content"><?

$fds = filesize($f) < 10000 ? filesize($f) : 10000;

if ($fd=@fopen($f, "r")){
	$contents = @fread ($fd, $fds);
	@fclose ($fd);
	echo HTMLdecode($contents);
	$contents=""; //Hope release the memory
}else
	echo "error reading the file $f";

?></textarea>
<hr>
<input type="submit" value="Save changes"><input type="button" value="Return back" onclick="location='?'">

</form>

</center>
<?
}

// **************************************************************************************
// **************************************************************************************
// **************************************************************************************
// **************************************************************************************
// **************************************************************************************

if ($op == 'edit')
	editfile($file);
else
	printfilelist();

?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
NIkolay Mihaylov

Comment:



Comment:

(Maximum characters: 1200). You have characters left.