<?
class filebrowser{
// constructor - clears the stat cache because of the functions used in displaying files and lists
function filebrowser(){
clearstatcache();
}
// takes a long path and resolves it down. Same as realpath, except it works even if the path
// doesn't exist on this system
function str2path($path_str){
$pwd=realpath($path_str);
$EX_FLAG=TRUE;
if(empty($pwd)){
$EX_FLAG=FALSE;
$pwd='';
$strArr=preg_split("/(\/)/",$path_str,-1,PREG_SPLIT_NO_EMPTY);
$pwdArr=array();
$j=0;
for($i=0;$i<count($strArr);$i++){
if($strArr[$i]!=".."){
if($strArr[$i]!="."){
$pwdArr[$j]=$strArr[$i];
$j++;
}
}else{
array_pop($pwdArr);
$j--;
}
}
$pStr=implode("/",$pwdArr);
$pwd=(strlen($pStr)>0) ? ("/".$pStr) : "/";
}
return $pwd;
}
// displays the directory listing, one item per line. will display according to the CSS style class name
// passed as a second argument.
function DisplayList($strpath,$css=''){
global $PHP_SELF;
$path=$this->str2path($strpath);
if(is_file($path)){
ereg("(.*)/[^/]*",$path,$match);
$path=$match[1];
}
$retVal="<h2>$path</h2>\n";
exec('ls -a '.$path,$result);
$retVal.="<p";
if($css) $retVal.=" class=\"$css\"";
$retVal.=">";
for($i=1;$i<count($result);$i++){
$tmp=$path."/".$result[$i];
if(is_dir($tmp)){
$tmp.="/";
$filestring="<a href=$PHP_SELF?path=$tmp>$result[$i]/</a><br>\n";
}else{
$filestring="$result[$i] (<a href=$PHP_SELF?view=1&path=$tmp>View</a>)<br>\n";
}
$retVal.=$filestring;
}
return $retVal."</p>";
}
// displays the contents of a file according to the CSS class name passed as a second argument.
function DisplayFile($strpath,$css=''){
$path=$this->str2path($strpath);
// if it's not a directory, lets see it (if we can)!
// WARNING!!! You won't want to display binary files in this way
$fileresult=file($path);
if(count($fileresult)>1){
$retVal="<b>Contents of: $path</b><hr>\n<pre";
if($css) $retVal.=" class=\"$css\"";
$retVal.=">";
for($i=0;$i<count($fileresult);$i++){
$retVal.=htmlspecialchars($fileresult[$i]);
}
$retVal.="</pre>\n";
}
return $retVal;
}
}
// end of class
// example
$system=new filebrowser();
if(!$path)
$path = exec('pwd');
if($view)
echo $system->DisplayFile($path);
echo $system->DisplayList($path);
?>
|