All pastes #113952 Raw Edit

Miscellany

public text v1 · immutable
·published 2006-08-03 20:56 UTC
rendered paste body
<?


$BASE_DIR = ".";
$BASE_URL = ".";

$BASE_EDITABLE = 0; // 0/1

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


$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',


	'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"
);

$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){
	$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;
}


function dirmessage($s){
	
}

function message($s){
	echo "<b>$s</b>";
}

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


// 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, "");
}

$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))."/";

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

if ($op){

	switch ($op){

		case 'del':

			$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':

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

			break;

		case 'mkdir':
			
			$file = basename($file);

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

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

			break;

		case 'mkfile':
			
			$mkfile = basename($file);

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

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

			break;

		case 'newfile':
			
			$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':

			$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)


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"
}


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

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

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

	echo "
		<tr>
	";

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

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

	echo "
			<td align='right' class='border'>$size</td>
			<td align='center' class='border'>$time</td>
			<td align='center' class='border'>$time1</td>
			<td align='center' class='border'>
				[<a href='javascript:show_delete_dialog(\"$f\")'>del</a>]
			</td>
			<td align='center' class='border'>
				[<a href='javascript:show_rename_dialog(\"$f\")'>ren</a>]
			</td>

		</tr>
	";
}


function printfilelist(){
	global $shdir, $msg;
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>JIZAS:craist</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="stylesheet3.css" rel="stylesheet" type="text/css">

<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>
</head>
<body>
<div id="container">
	<div id="smalltopleft"><h5>da site</h5></div>
	<div id="smalltop"><? readfile("menu2.d"); ?></div>

	<div id="cleanup">&nbsp;</div>
	<div id="stuff">
<?
if ($msg){
	message($msg);
}

dirmessage($shdir);
?>
<center>
<table border="1" width="95%">
<tr>
	<th class='border'>Name</th>
	<th class='border'>Size</th>
	<th class='border'>Date</th>
	<th class='border'>Time</th>
	<td  colspan='2' align='center' class='border'>[<a href="?">refresh</a>]</td>
</tr>
<tr>
	<td align='left' class='border'><a href='?op=cd&file=..'>..</a></td>
	<td align='right' class='border'>dir</td>
	<td align='center' class='border'>&nbsp;</td>
	<td align='center' class='border'>&nbsp;</td>
	<td align='center' class='border'>&nbsp;</td>
	<td align='center' class='border'>&nbsp;</td>
</tr>
<?

	$d = dir(".");

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

	$d->close();

?>
</table>
</center>
<table border="0">
<form>
<input type="hidden" name="op" value="mkdir">
<tr><td>Create new directory:</td><td><input type="text" name="file"></td><td><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"></td><td><input type="submit" value="&gt;&gt;"></td></tr>
</form>

</table>

</div>
<div id="cleanup">&nbsp;</div>
<div id="footer"><? readfile("footer.d"); ?></div>
</div>
</body>
</html>
<?
} //Print file list


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"); ?>

<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>

<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>

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

</form>

</center>
<?
}

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

?>