bit dojo labs : file explorer

Inspecting: home > php > file-explorer > file-explorer.php (download)
<?php
//
// Bit Dojo File Explorer
// Copyright (C) 2009 Alan Huan-Chun Peng Hsu 
// http://bitdojo.net
// 
// This script uses SyntaxHighlighter
// http://alexgorbatchev.com/wiki/SyntaxHighlighter
//

//== Configuration ==============================================================
	
	$site_name = "bit dojo labs : file explorer";	
	$ignores = array(".", "..");
	$script_root = "http://".$_SERVER['SERVER_NAME'];
	$browse_cmd = "/browse/";
	$view_cmd = "/view/";
	$google_analytics_id = "UA-564953-13"; // Set to null if you don't use GA
	
//== End of Configuration =======================================================
	
	$version = "v2.02.20091013 beta";

	$browse_cmd = $script_root.$browse_cmd;
	$view_cmd = $script_root.$view_cmd;
	$mydir = getcwd();
	$qtype = $_REQUEST["type"];
	$open = $_REQUEST["open"];
	$download = $_REQUEST["download"];
	if(!$open) $open = ".";
?>
<!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><?php echo $site_name?></title>

<style type="text/css">
	html { height: 100%; width: 100%; }
	body { height: 98%; margin: 0px; padding: 15px 15px 0 20px; font-size: 14px; color: #555; background: #fff; font-family: Calibri, Helvetica, Arial, sans-serif; }
	h1, h2, h3 { margin: 3px 0px; }
	a { color: #555; text-decoration: none; }
	a:hover { color: #000; cursor: pointer; text-decoration: underline; }
	div { padding: 0; margin: 0; }
	table { width: 60%; border: none; border-spacing: 0px; border-collapse: collapse; }
	tr:hover td, tr.highlight td { background: #efe; }
	td { padding: 5px 10px; margin: 0px 0px; }
	.filename { width: 40%; }
	.fileinfo { width: 20%; font-size: 12px; margin: 0; text-align: right; }
	.footer { font-size: 12px; width: 90%; padding: 0; margin: 3px 0; background: transparent; }
	#code-wrapper { border : 3px #eee solid; max-height: 85%; width: 98%; margin: 8px 0; padding: 0; overflow: auto; }
	#code-text { margin: 0; padding: 0; overflow: scroll; }
</style>

<script type="text/javascript" src="/js/lib/syntax/scripts/shCore.js"></script>
<script type="text/javascript" src="/js/lib/syntax/scripts/shCustomCompressedBrushes.js"></script>

<link href="/js/lib/syntax/styles/shCore.css" rel="stylesheet" type="text/css" />
<link href="/js/lib/syntax/styles/shThemeDefault.css" rel="stylesheet" type="text/css" />

<style type="text/css">
	/* style overrides */
	.syntaxhighlighter { margin: 0 0 !important; height: 100% important!; width: auto !important; min-width: 99.9%; float: left !important; }
</style>

<script type="text/javascript">
	SyntaxHighlighter.config.clipboardSwf = "/js/lib/syntax/scripts/clipboard.swf";
	SyntaxHighlighter.all();
</script>

</head>

<body id="body">

<?php
	// Security Check
	$ohno = strstr($open, "..") || (!strstr(realpath($open), $mydir));
	if($ohno) exit("It is pitch black. You are likely to be eaten by a grue.");
?>

<h2 id="title1"><?php echo $site_name?></h2>

<?php
if(is_dir($open)){ // if this is a dir

	$numfiles = 0; $numdirs = 0;
	chdir($open);

	if ($handle = opendir(".")) {

		$cwd = getcwd();
		$cwd = str_replace($mydir, "", $cwd);
		$cwd = trim($cwd, "/");
		$breadcrumb = BuildBreadcrumb($cwd);
		
	    echo "Browsing: $breadcrumb<br />";

		$files = array(); $dirs = array();
	    while (false !== ($file = readdir($handle))) {
		
			if (!in_array($file, $ignores)){ 
				if(!is_dir("$file"))
					$files[] = $file;
				else
					$dirs[] = $file;
			}
	    }
		
		sort($files); sort($dirs);
		$files = array_merge($dirs, $files);
	    closedir($handle);
		
		if($files){
			echo "<table id='FileTable'>";
			
			foreach ($files as $file) {

				$filepath = "$open/$file";
				$filepath = str_replace("./", "", $filepath);
				echo "<tr>";
				
				if(is_dir("$file")){
					echo "<td class='filename'>[ <a href='$browse_cmd$filepath'>$file</a> ]</td>";
					echo "<td class='fileinfo'></td>";
					echo "<td class='fileinfo'> folder </td>";
					$numdirs+=1;
				} else {
					$size = filesize("$file");
					$size = number_format($size);
					echo "<td class='filename'><a target='_blank' href='/$filepath'>$file</a></td> ";
					echo "<td class='fileinfo'> <a href='$view_cmd$filepath' title='Open with code viewer'>inspect</a></td>";
					echo "<td class='fileinfo'> $size bytes </td>" ;
					$numfiles+=1;
				}	
				echo "</tr>";
			}
			echo "</table>";
		}
	}
	echo "<b>$numfiles file".(($numfiles==1)?"":"s");
	echo " and $numdirs folder".(($numdirs==1)?"":"s")."</b>";
	
} else { // else this is a file

	if($download) { // send the file as binary stream
	
		header('Content-Description: File Transfer');
		header('Content-Type: application/octet-stream');
		header('Content-Disposition: attachment; filename='.basename($open));
		header('Content-Transfer-Encoding: binary');
		header('Expires: 0');
		header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
		header('Pragma: public');
		header('Content-Length: ' . filesize($open));
		ob_clean();
		flush();
		readfile($open);
		exit;
	
	} else { // display the file using code viewer

		$type = ($qtype) ? $qtype : FindFileType($open);
		$breadcrumb = BuildBreadcrumb($open);
		$downloadLink = "$view_cmd$open&amp;download=1";
		$preClass = "brush: $type";
		$preClass = $preClass."; wrap-lines: false";
		//if($type == "php") $preClass = $preClass."; html-script: true";
		
		echo "Inspecting: $breadcrumb ";
		echo "(<a href='$downloadLink' title='Download this file to your computer'>download</a>)";
		
		echo "<div id='code-wrapper'><pre id='code-pre' class='$preClass'>";
		echo htmlentities(file_get_contents($open));
		echo "</pre></div>";
		
		echo "<div class='footer'>";
		echo "Syntax: <b>$type</b> ( ";
		echo "<a href='$view_cmd$open&amp;type=cpp'>cpp</a> | ";
		echo "<a href='$view_cmd$open&amp;type=csharp'>csharp</a> | ";
		echo "<a href='$view_cmd$open&amp;type=css'>css</a> | ";
		echo "<a href='$view_cmd$open&amp;type=html'>html</a> | ";	
		echo "<a href='$view_cmd$open&amp;type=java'>java</a> | ";
		echo "<a href='$view_cmd$open&amp;type=javascript'>javascript</a> | ";
		echo "<a href='$view_cmd$open&amp;type=perl'>perl</a> | ";
		echo "<a href='$view_cmd$open&amp;type=php'>php</a> | ";
		echo "<a href='$view_cmd$open&amp;type=ruby'>ruby</a> | ";
		echo "<a href='$view_cmd$open&amp;type=sql'>sql</a> | ";
		echo "<a href='$view_cmd$open&amp;type=vb'>vb</a> | ";
		echo "<a href='$view_cmd$open&amp;type=xml'>xml</a> | ";
		echo "<a href='$view_cmd$open&amp;type=plain'>plain</a> ) <br/>";
		echo "</div>";		
	}
}

?> 
<div class="footer">Bit Dojo File Explorer <?php echo $version; ?></div>

<?php if($google_analytics_id): ?>
<script type="text/javascript">var gaJsHost=(("https:"==document.location.protocol)?"https://ssl.":"http://www.");document.write(unescape("%3Cscript src='"+gaJsHost+"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));</script>
<script type="text/javascript">try{var pageTracker=_gat._getTracker("<?php echo $google_analytics_id; ?>");pageTracker._trackPageview();}catch(err){}</script>
<?php endif; ?>

</body></html>

<?php

/// Functions ///

function BuildBreadcrumb($path){
	
		global $browse_cmd, $script_root;
		$breadcrumb = "";

		if($path == ""){
			$breadcrumb = "home";
		} else {
			$a = explode("/", $path);
			foreach($a as $v){
				$breadcrumb = $breadcrumb.($breadcrumb==""?"":"/").$v;
				$paths[] = $breadcrumb;
			}
			$breadcrumb = "<a href='$script_root'>home</a>";
			for($i=0;$i<count($a);$i++){
				if($i!=count($a)-1)
					$breadcrumb = $breadcrumb." &gt; "."<a href='$browse_cmd$paths[$i]'>$a[$i]</a>";
				else
					$breadcrumb = $breadcrumb." &gt; ".$a[$i];
			}
		}
		return $breadcrumb;
}

function FindFileType($path){

	// $types["ext"] = "type";
	$types["c"] = "cpp"; 
	$types["cpp"] = "cpp"; 
	$types["cs"] = "csharp"; 
	$types["css"] = "css"; 
	$types["html"] = "html";
	$types["htm"] = "html";
	$types["java"] = "java"; 
	$types["js"] = "javascript"; 
	$types["pl"] = "perl"; 
	$types["rb"] = "ruby";	
	$types["php"] = "php"; 
	$types["txt"] = "plain"; 
	$types["sql"] = "sql";
	$types["vb"] = "vb";
	$types["xml"] = "xml";
	
	$path = strtolower($path) ;
	$ext = split("[/\\.]", $path) ;
	$n = count($ext)-1;
	$ext = $ext[$n];
	
	$ext = $types[$ext];	
	if(!$ext)$ext = "plain";
	
	return $ext;
}
?>