| px | top | add code | search | signup | login | help |
<?php
/*
Copyright (c) 1999 Patrick C. Audley
E-mail: paudley.px@blackcat.ca
All rights reserved.
Version: v1.0
This code provided "As Is" with no warrantees express or implied.
The author and contributors are not liable for anything good or bad
that results from your use of this code.
You are free to distribute, cannibalize, cook, consume or otherwise
use this in any way. I don't require acknowledgement, but ask (not require)
that any enhancements you make get passed back to me.
Have fun! - Patrick.
P.S. An example function at the bottom is included.
*/
function dir_bits_to_string( $bits ) {
$ret = "";
if( $bits < 8 ) {
// Too few bits to convert to bytes...
$ret = sprintf("%d bits",$bits);
} else {
$bits = $bits/8;
if( $bits < 1024 ) {
// Too few bits to convert to kbytes...
$ret = sprintf("%.2f Bytes",$bits);
} else {
$bits = $bits/1024;
if( $bits < 1024 ) {
// Too few bits to convert to Mbytes...
$ret = sprintf("%.2f kBytes",$bits);
} else {
$bits = $bits/1024;
if( $bits < 1024 ) {
// Too few bits to convert to Gbytes...
$ret = sprintf("%.2f MBytes",$bits);
} else {
$bits = $bits/1024;
if( $bits < 1024 ) {
// Too few bits to convert to Tbytes...
$ret = sprintf("%.2f GBytes",$bits);
} else {
$bits = $bits/1024;
if( $bits < 1024 ) {
// Too few bits to convert to Pbytes...
$ret = sprintf("%.2f TBytes",$bits);
} else {
$ret = "Too many bits...";
}
}
}
}
}
}
$ret = str_replace(".00","",$ret);
return $ret;
}
function display_dir( $dir ) {
global $dir_secure, $dir_nobackups, $DOCUMENT_ROOT;
$orig_dir = $dir;
$dir = $DOCUMENT_ROOT.$dir;
$d = dir($dir);
if( !isset($dir_secure) ) { $dir_secure = true; }
if( !isset($dir_nobackups) ) { $dir_nobackups = true; }
$descs = array();
if( is_file($dir."/.descriptions") ) {
$filea = file( $dir."/.descriptions");
for($i=0;$i<count($filea);$i++) {
$s = split(":",$filea[$i]);
$descs[$s[1]] = $s[2];
}
}
$cvs = array();
$cvs_repo = "";
if( is_dir($dir."/CVS") && is_file($dir."/CVS/Entries") && is_file($dir."/CVS/Repository") ) {
// This dir is CVS controlled...
// Pull the info out for the Entries file.
$filea = file( $dir."/CVS/Entries");
for($i=0;$i<count($filea);$i++) {
if( $filea[$i][0] == "/" ) { // Only process lines that are file info lines.
$s = split("/",$filea[$i]);
$cvs[$s[1]] = $s[2];
}
}
// Snarf the Repository for CVSWEB
$filea = file( $dir."/CVS/Repository");
$cvs_repo = $filea[0];
}
?>
<table>
<tr>
<th>Type</th>
<th>Name</th>
<th colspan=2>Size</th>
<th>Actions</th>
<th>Description</th>
</tr>
<?php
$entries = array();
while($entry=$d->read()) {
$entries[] = $entry;
}
sort($entries);
reset($entries);
while($entry=next($entries)) {
$view = true;
$icon = "";
$filename = $entry;
$action = "";
$desc = $descs[$entry];
$color = "ddddff";
$fullname = $dir."/".$entry;
$stat = stat( $fullname );
// Set default icons
if( $cvs[ $entry ] ) {
$action .= "<a href=\"/cgi-bin/cvsweb/$cvs_repo/$entry\">(cvs)</a>";
}
if( is_dir( $fullname ) ) {
$icon = "/nicons/generic/dir.gif";
$action .= "<a href=\"/showdir.php?dir=$orig_dir/$entry\">(browse)</a>";
}
if( is_file( $fullname ) ) {
ereg("\.(.[A-z0-9]+)$",$entry,$regs);
$ext = $regs[1];
switch( $ext ) {
case "php":
case "spim":
case "php3":
$icon = "/nicons/generic/php.gif";
$action .= "<a href=\"/source.php3?page_url=$orig_dir/$entry\">(view source)</a>";
break;
case "htaccess":
$icon = "/nicons/generic/generic.sec.gif"; break;
case "link":
$icon = "/nicons/generic/worldsm.gif";
$a = file($fullname);
#$desc;
break;
}
}
switch( $entry ) {
case ".descriptions":
case "CVS":
case ".":
// Suppress display.
$view = false;
break;
case "..":
// Handle parent directory differently..
$entry = "[Parent Dir]";
break;
}
if( $entry[0] == "." ) {
if( $dir_secure == true ) {
$view = false;
} else {
$color = "ffdddd";
}
}
if( $entry[ strlen($entry)-1 ] == "~" ) {
if( $dir_nobackups == true ) {
$view = false;
} else {
$color = "ddffdd";
}
}
if( $icon != "" ) { $icon = "<a href=\"$filename\"><img border=0 src=\"$icon\"></a>"; }
$entry = "<a href=\"$filename\">$entry</a>";
if( $view ) {
$sizes = split(" ",dir_bits_to_string($stat[7]*8));
$size = "<td><span style=\"text-align:right;\">".$sizes[0]."</span></td>";
$size.= "<td><span style=\"text-align:left;\">".$sizes[1]."</span></td>";
echo "<tr><td>$icon</td><td bgcolor=$color>$entry</td>$size<td><center><sup>$action</sup></center></td><td bgcolor=eeeeee>$desc</td></tr>\n";
}
}
$d->close();
echo "</table>";
}
function view_dir_button() {
global $PHP_SELF;
echo "<a href=\"/showdir.php?dir=".dirname($PHP_SELF)."\"><img src=/nicons/generic/dir.gif border=0></a>";
}
?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.