directorybrowser/directory_browser.php - view - 1.6
<?php
/*
*
* @(#) $Id: directory_browser.php,v 1.6 2006/01/18 21:08:52 mlemos Exp $
*
*/
class directory_browser_class
{
var $initial_path="";
var $error="";
var $path_variable="path";
var $create_directory_variable="createdirectory";
var $path="";
var $entries=array();
var $invisible_hide_pattern="^\\..*\$";
var $directory_length_limit=20;
var $file_length_limit=0;
var $limit_cut_text="...";
var $highlight_color="#ffffff";
var $file_columns=array(
array(
"Type"=>"name"
),
array(
"Type"=>"size"
)
);
var $navigation_columns=array(
array(
"Type"=>"path"
)
);
var $context=array();
var $creating_directory=0;
var $new_directory="";
var $create_directory_error_message="";
var $created_directory_path="";
Function PathLink($path, $data, $title="")
{
$link="?".$this->path_variable."=".UrlEncode($path);
Reset($this->context);
$end=(GetType($key=Key($this->context))!="string");
for(;!$end;)
{
$link.="&".$key."=".UrlEncode($this->context[$key]);
Next($this->context);
$end=(GetType($key=Key($this->context))!="string");
}
$link="<a".(strlen($title) ? " title=\"".HtmlSpecialChars($title)."\"" : "")." href=\"".HtmlSpecialChars($link)."\">".HtmlSpecialChars($data)."</a>";
return($link);
}
Function Initialize()
{
if(strlen($this->initial_path)==0)
{
$this->error="it was not set a valid initial directory path";
return(0);
}
return(1);
}
Function Process()
{
if(IsSet($_REQUEST[$this->path_variable]))
{
$this->path=$_REQUEST[$this->path_variable];
$directory=@opendir($this->path);
if($directory)
{
$current_path=getcwd();
if(@chdir($this->path))
$this->path=getcwd();
else
{
$this->path="";
closedir($directory);
}
if(!@chdir($current_path))
{
$this->error="could not restore the current work directory";
return(0);
}
}
}
else
$this->path="";
if(strlen($this->path)==0)
{
$current_path=getcwd();
$directory=@opendir($this->initial_path);
if(!$directory
|| !@chdir($this->initial_path))
{
$this->error="could not access the initial path directory";
if($directory)
closedir($directory);
return(0);
}
$this->path=getcwd();
if(!@chdir($current_path))
{
closedir($directory);
$this->error="could not restore the current work directory";
return(0);
}
}
if(IsSet($_REQUEST[$this->create_directory_variable]))
{
$this->creating_directory=1;
$this->new_directory=$_REQUEST[$this->create_directory_variable];
if(strlen($this->new_directory))
{
$directory_path=$this->path."/".$this->new_directory;
if(is_dir($directory_path))
$this->create_directory_error_message="There is already a directory with the name ".$this->new_directory." .";
elseif(file_exists($directory_path))
$this->create_directory_error_message="There is already a file with the name ".$this->new_directory." .";
elseif(!is_writable($this->path))
$this->create_directory_error_message="There is no permission to create a directory in ".$this->path." .";
elseif(!@mkdir($directory_path))
{
$this->create_directory_error_message="Could not create the directory ".$this->new_directory." .";
if(IsSet($php_errormsg))
$this->create_directory_error_message.=" (".$php_errormsg.")";
}
else
$this->created_directory_path=$directory_path;
}
}
if($directory)
{
$this->entries=array();
while(strlen($entry=@readdir($directory)))
{
if(!strcmp($entry,".")
|| !strcmp($entry,".."))
continue;
$full_path=$this->path.(strcmp($this->path[strlen($this->path)-1],"/") ? "/" : "").$entry;
if(!($type=@filetype($full_path)))
continue;
if(GetType($size=@filesize($full_path))!="integer")
continue;
$this->entries[]=array(
"Name"=>$entry,
"Type"=>$type,
"Size"=>$size,
"FullPath"=>$full_path
);
}
closedir($directory);
}
return(1);
}
Function CustomizeFileColumn($entry, $column, &$column_data)
{
}
Function CustomizeNavigationColumn($column, &$column_data)
{
}
Function OutputColumnData($column_data)
{
?><?php
$column_tag=(IsSet($column_data["header"]) ? "th" : "td");
$style="";
if(IsSet($column_data["style"]))
$style.=$column_data["style"]."; ";
if(IsSet($column_data["nowrap"]))
$style.="white-space: nowrap; ";
if(IsSet($column_data["align"]))
$style.="text-align: ".$column_data["align"]."; ";
if(IsSet($column_data["width"]))
$style.="width: ".$column_data["width"]."; ";
echo "<".$column_tag;
if(strlen($style))
echo " style=\"".$style."\"";
echo ">";
if(IsSet($column_data["monospaced"]))
{
?><tt><?php
}
echo $column_data["data"];
if(IsSet($column_data["monospaced"]))
{
?></tt><?php
}
echo "</".$column_tag.">";
}
Function Output()
{
for($files=$directories=array(), $entry=0; $entry<count($this->entries); $entry++)
{
if(strlen($this->invisible_hide_pattern)
&& ereg($this->invisible_hide_pattern,$this->entries[$entry]["Name"]))
continue;
switch($this->entries[$entry]["Type"])
{
case "dir":
$directories[$entry]=strtolower($this->entries[$entry]["Name"]);
break;
default:
$files[$entry]=strtolower($this->entries[$entry]["Name"]);
break;
}
}
asort($files);
asort($directories);
?>
<table style="width: 100%; height: 100%">
<tr style="height: 1%">
<td colspan="2" style="white-space: nowrap">
<table style="width: 100%">
<tr>
<?php
for($column=0;$column<count($this->navigation_columns);$column++)
{
$column_data=array("data"=>"");
switch($this->navigation_columns[$column]["Type"])
{
case "path":
$column_data["width"]="99%";
$column_data["style"]="border-style: solid; border-width: 1px; border-bottom-color: #ffffff; border-right-color: #ffffff; border-top-color: #808080; border-left-color: #808080";
$column_data["nowrap"]=1;
$column_data["data"]="<b>Directory:</b> ";
for($last_position=$position=0;$position<strlen($this->path);)
{
if(GetType($position=strpos($this->path,"/",$position))!="integer")
$position=strlen($this->path);
$path=substr($this->path,0,$position+1);
$column_data["data"].=$this->PathLink($path, substr($this->path,$last_position,$position-$last_position+1), $path);
$position++;
$last_position=$position;
}
break;
case "createdirectory":
$column_data["nowrap"]=1;
if($this->creating_directory)
$column_data["data"]=" ";
else
{
$this->context["createdirectory"]="";
$column_data["data"]=$this->PathLink($this->path, "Create directory", "Create directory");
UnSet($this->context["createdirectory"]);
}
break;
case "custom":
$this->CustomizeNavigationColumn($column, $column_data);
break;
}
$this->OutputColumnData($column_data);
}
?>
</tr>
<?php
if($this->creating_directory)
{
?>
<tr>
<td style="text-align: center" colspan="<?php echo $column; ?>"><form method="GET" action="">
<fieldset>
<legend><b>Create a new directory</b></legend>
<?php
if(strlen($this->created_directory_path))
{
?>
<b>The directory <tt><?php echo HtmlSpecialChars($this->new_directory); ?></tt> was successfully created.</b>
<?php echo $this->PathLink($this->created_directory_path, "Change to the new directory", "Cancel creating a new directory"),"\n";
}
else
{
if(strlen($this->create_directory_error_message))
{
?><b><?php echo HtmlSpecialChars($this->create_directory_error_message); ?></b>
<hr />
<?php
}
?>
<b><label for="<?php echo $this->create_directory_variable; ?>">Directory:</label></b>
<input type="text" id="<?php echo $this->create_directory_variable; ?>" name="<?php echo $this->create_directory_variable; ?>" value="<?php echo HtmlSpecialChars($this->new_directory); ?>" />
<input type="submit" value="Create" />
<?php
echo $this->PathLink($this->path, "Cancel", "Cancel creating a new directory"),"\n";
?><input type="hidden" name="<?php echo $this->path_variable; ?>" value="<?php echo HtmlSpecialChars($this->path); ?>" />
<?php
Reset($this->context);
$end=(GetType($key=Key($this->context))!="string");
for(;!$end;)
{
?>
<input type="hidden" name="<?php echo $key; ?>" value="<?php echo HtmlSpecialChars($this->context[$key]); ?>" />
<?php
Next($this->context);
$end=(GetType($key=Key($this->context))!="string");
}
}
?>
</fieldset>
</form></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
<tr style="height: 1%">
<th style="width: 80%">Files</th>
<th style="width: 20%">Directories</th>
</tr>
<tr>
<td style="border-style: solid; border-width: 1px; border-bottom-color: #ffffff; border-right-color: #ffffff; border-top-color: #808080; border-left-color: #808080; vertical-align: top; width: 80%">
<table style="width: 100%" cellpadding="1" cellspacing="0">
<tr>
<?php
for($column=0;$column<count($this->file_columns);$column++)
{
$column_data=array("header"=>1);
switch($this->file_columns[$column]["Type"])
{
case "name":
$column_data["data"]="Name";
break;
case "size":
$column_data["data"]="Size";
$column_data["width"]="1%";
break;
case "custom":
$column_data["data"]=(IsSet($this->file_columns[$column]["Title"]) ? HtmlSpecialChars($this->file_columns[$column]["Title"]) : "");
$this->CustomizeFileColumn(-1, $column, $column_data);
break;
}
$this->OutputColumnData($column_data);
}
?>
</tr>
<tr>
<td colspan="<?php echo count($this->file_columns); ?>"><hr /></td>
</tr>
<?php
for($file=0, Reset($files); $file<count($files); Next($files), $file++)
{
$entry=Key($files);
?>
<tr id="file<?php echo $file; ?>" style="background-color: none" onmouseover="if(document.layers) { document.layers['file<?php echo $file; ?>'].bgColor='<?php echo $this->highlight_color; ?>' } else { if(document.all) { document.all['file<?php echo $file; ?>'].style.background='<?php echo $this->highlight_color; ?>' } else { if(this.style) { this.style.background='<?php echo $this->highlight_color; ?>' } } }" onmouseout="if(document.layers) { document.layers['file<?php echo $file; ?>'].bgColor='' } else { if(document.all) { document.all['file<?php echo $file; ?>'].style.background='' } else { if(this.style) { this.style.background='' } } }">
<?php
for($column=0;$column<count($this->file_columns);$column++)
{
$column_data=array();
switch($this->file_columns[$column]["Type"])
{
case "name":
$display_name=$entry_name=$this->entries[$entry]["Name"];
if($this->file_length_limit
&& strlen($display_name)>$this->file_length_limit)
$display_name=substr($display_name,0,$this->file_length_limit-strlen($this->limit_cut_text)).$this->limit_cut_text;
$column_data["data"]=HtmlSpecialChars($display_name);
$column_data["monospaced"]=1;
break;
case "size":
$column_data["data"]=number_format($this->entries[$entry]["Size"]);
$column_data["align"]="right";
$column_data["width"]="1%";
$column_data["monospaced"]=1;
break;
case "custom":
$column_data["data"]="";
$this->CustomizeFileColumn($entry, $column, $column_data);
break;
}
$this->OutputColumnData($column_data);
}
?>
</tr>
<?php
}
?>
</table>
</td>
<td style="border-style: solid; border-width: 1px; border-bottom-color: #ffffff; border-right-color: #ffffff; border-top-color: #808080; border-left-color: #808080; vertical-align: top; width: 20%">
<?php
if(strcmp(dirname($this->path),$this->path))
{
?>
<div style="text-align: center"><b><?php echo $this->PathLink(dirname($this->path), "Parent directory", dirname($this->path)); ?></b></div>
<hr>
<?php
}
?>
<?php
for($directory=0, Reset($directories); $directory<count($directories); Next($directories), $directory++)
{
$entry=Key($directories);
$display_name=$entry_name=$this->entries[$entry]["Name"];
if($this->directory_length_limit
&& strlen($display_name)>$this->directory_length_limit)
$display_name=substr($display_name,0,$this->directory_length_limit-strlen($this->limit_cut_text)).$this->limit_cut_text;
echo "<tt>".$this->PathLink($this->entries[$entry]["FullPath"], $display_name, $entry_name)."</tt><br />\n";
}
if($directory==0)
echo " ";
?>
</td>
</tr>
</table>
<?php
}
};
?>
|