<?php
class file {
var $stat = array();
var $pathinfo = array();
var $name;
var $path;
var $dirname;
var $basename;
var $extension;
var $size;
var $size_h;
var $read;
var $write;
var $execute;
var $mime;
////
// function file
// (Constructor) Reads the rights and sets the default value for a file
////
function file ($file) {
$this->stat = stat($file);
$this->pathinfo = pathinfo($file);
$this->dirname = $this->pathinfo["dirname"];
$this->basename = $this->pathinfo["basename"];
$this->extension = strtolower($this->pathinfo["extension"]);
$this->size = $this->stat[7];
$this->uid = $this->stat[4];
$this->gid = $this->stat[5];
$this->date = $this->stat[9];
if (!ereg("/$", $this->dirname )) $this->dirname .= "/";
// Create a human readable file size
if ($this->size < 1024) $this->size_h = $this->size . " bytes";
if ($this->size > 1024) $this->size_h = round($this->size / 1024, 2) . " KB";
if ($this->size > 1048576) $this->size_h = round($this->size / 1048576, 2) . " MB";
if ($this->size > 1073741824) $this->size_h = round($this->size / 1073741824, 2) . " GB";
$this->read = false;
$this->write = false;
$this->execute = false;
if (is_readable($file)) $this->read = true;
if (is_writable($file)) $this->write = true;
if (is_executable($file)) $this->execute = true;
clearstatcache();
// aliasses
$this->name = $this->basename;
$this->path = $this->dirname;
if (strlen($this->extension) > 10) $this->extension = "";
}
function get_mime() {
$verify = TRUE;
$fn = $this->dirname . $this->name;
if (empty($fn)) {
return "";
}
if ($verify) {
if ($fp = @fopen($fn, "rb")) {
$fcont = fread($fp, 32);
fclose($fp);
} else {
return "";
}
}
$ext = $this->extension;
switch ($ext) {
case "aif":
case "aifc":
case "aiff":
return "audio/aiff";
case "asp":
return "text/asp";
case "avi":
return "video/avi" .
(!$verify || (substr($fcont, 0, 4) == "RIFF") ? "" : "*");
case "mpg":
case "mpeg":
return "video/mpeg";
case "wmv":
return "video/windows-media";
case "bmp":
return "image/bmp" .
(!$verify || (substr($fcont, 0, 2) == "BM") ? "" : "*");
case "css":
return "text/css";
case "doc":
return "application/msword" .
(!$verify ||
(substr($fcont, 0, 8) == "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1") ?
"" : "*");
case "exe":
case "dll":
case "scr":
return "application/x-msdownload" .
(!$verify || (substr($fcont, 0, 2) == "MZ") ? "" : "*");
case "hlp":
return "application/windows-help" .
(!$verify ||
(substr($fcont, 0, 4) == "\x3F\x5F\x03\x00") ? "" : "*");
case "htm":
case "html":
return "text/html" .
(!$verify ||
(substr($fcont, 0, 6) == "<html>") ||
(substr($fcont, 0, 14) == "<!DOCTYPE HTML") ?
"" : "*");
case "gif":
return "image/gif" .
(!$verify || (substr($fcont, 0, 3) == "GIF") ? "" : "*");
case "gz":
case "tgz":
return "application/x-gzip" .
(!$verify || (substr($fcont, 0, 3) == "\x1F\x8B\x08") ? "" : "*");
case "jfif":
case "jpe":
case "jpeg":
case "jpg":
return "image/jpeg" .
(!$verify ||
(substr($fcont, 0, 10) == "\xFF\xD8\xFF\xE0\x00\x10JFIF") ?
"" : "*");
case "mov":
return "video/quicktime";
case "pdf":
return "application/pdf" .
(!$verify || (substr($fcont, 0, 4) == "%PDF") ? "" : "*");
case "php":
case "php3":
case "php4":
case "phtml":
return "application/x-httpd-php" .
(!$verify || (substr($fcont, 0, 5) == "<?") ? "" : "*");
case "pl":
return "text/perl" .
(!$verify ||
(substr($fcont, 0, 21) == "#!/usr/local/bin/perl") ? "" : "*");
case "png":
return "image/x-png" .
(!$verify || (substr($fcont, 0, 4) == "\x89PNG") ? "" : "*");
case "psd":
return "image/psd" .
(!$verify || (substr($fcont, 0, 4) == "8BPS") ? "" : "*");
case "tiff":
case "tif":
return "image/tiff" .
(!$verify ||
(substr($fcont, 0, 8) == "\x4D\x4D\x00\x2A\x00\x00\x00\x08") ||
(substr($fcont, 0, 4) == "\x49\x49\x2A\x00") ?
"" : "*");
case "ttf":
return "application/x-ttf";
case "txt":
case "ini":
case "log":
case "sql":
case "cfg":
case "conf":
case "tpl":
return "text/plain";
case "swf":
return "application/x-shockwave-flash";
case "wav":
return "audio/x-wav" .
(!$verify || (substr($fcont, 0, 4) == "RIFF") ? "" : "*");
case "wma":
return "audio/x-ms-wma";
case "mp3":
return "audio/x-mp3";
case "xml":
return "text/xml" .
(!$verify || (substr($fcont, 0, 5) == "<?xml") ? "" : "*");
case "zip":
return "application/x-zip-compressed" .
(!$verify || (substr($fcont, 0, 2) == "PK") ? "" : "*");
}
return "";
}
}
class dir {
var $stat = array();
var $pathinfo = array();
var $name;
var $path;
var $dirname;
var $dirname;
var $basename;
var $size;
var $read;
var $write;
var $execute;
////
// function dir
// (Constructor) Reads the rights and sets the default value for a directory
////
function dir ($dir) {
$this->stat = stat($dir);
$this->pathinfo = pathinfo($dir);
$this->size = $this->stat[7];
$this->uid = $this->stat[4];
$this->gid = $this->stat[5];
$this->date = $this->stat[9];
$this->dirname = $this->pathinfo["dirname"];
$this->basename = $this->pathinfo["basename"];
$this->read = false;
$this->write = false;
$this->execute = false;
if (is_readable($dir)) $this->read = true;
if (is_writable($dir)) $this->write = true;
if (is_executable($dir)) $this->execute = true;
clearstatcache();
// aliasses
$this->name = $this->basename;
$this->path = $this->dirname;
}
}
class fileman {
var $files = array();
var $directories = array();
var $base_dir;
var $sub_dir;
var $dir_obj;
var $error_strings = array();
var $complete_path;
var $readable;
var $sort_order;
////
// function fileman
// (Constructor) Reads the rights, sets the default value for a directory and starts the dir read loop.
////
function fileman ($base_dir, $sub_dir) {
// Add final slashes do the dirs in ARGS and create a full string.
if (!ereg("/$", $base_dir)) $base_dir .= "/";
if (!ereg("/$", $sub_dir )) $sub_dir .= "/";
$this->sub_dir = $sub_dir;
$this->base_dir = $base_dir;
$this->complete_path = $base_dir . $sub_dir;
// Checks if given dir is a valid one and is readable.
if (!is_dir($this->complete_path)) $this->error("Not a valid directory.");
if (strstr($this->sub_dir, "../")) $this->error("Path contains a '../' string, this is not permited due to security issues.");
if (!is_readable($this->complete_path)) {
$this->error("$sub_dir is not a readable directory.");
$this->readable = false;
} else {
$this->readable = true;
}
$this->check_errors();
$this->read_directory();
return true;
}
////
// function read_directory
// Reads the current directory and calls the file and dir classes.
////
function read_directory () {
$dir_counter = 0;
$file_counter = 0;
$this->dir_obj = opendir($this->complete_path);
while (FALSE !== ($object = readdir($this->dir_obj))) {
if ($object == ".") continue;
if ($object == "..") continue;
if (is_file($this->complete_path . $object)) {
$file_counter++;
$this->files[$file_counter] = new file ($this->complete_path . $object);
}
if (is_dir($this->complete_path . $object)) {
$dir_counter++;
$this->directories[$dir_counter] = new dir ($this->complete_path . $object);
}
}
closedir($this->dir_obj);
}
////
// function sort
// Sorts the file and directory listing to to the string specified (name, extension, size, date)
////
function sort ($sort, $sort_direction="up") {
$this->sort_order = $sort;
if ($sort_direction == "") $sort_direction = "down";
if ($sort_direction == "down") {
usort($this->files, array ($this, "compare"));
usort($this->directories, array ($this, "compare"));
} else {
usort($this->files, array ($this, "compare_up"));
usort($this->directories, array ($this, "compare_up"));
}
}
// File sorting part, used by sort function.
function compare ($a, $b) {
if ($this->sort_order == "name") return strcasecmp($a->name, $b->name);
if ($this->sort_order == "extension") return strcasecmp($a->extension, $b->extension);
if ($this->sort_order == "date") return strcasecmp($a->date, $b->date);
if ($this->sort_order == "size") return ($a->size > $b->size) ? 1 : -1;
return strcasecmp($a->name, $b->name);
}
function compare_up ($b, $a) {
if ($this->sort_order == "name") return strcasecmp($a->name, $b->name);
if ($this->sort_order == "extension") return strcasecmp($a->extension, $b->extension);
if ($this->sort_order == "date") return strcasecmp($a->date, $b->date);
if ($this->sort_order == "size") return ($a->size > $b->size) ? 1 : -1;
return strcasecmp($a->name, $b->name);
}
////
// function error
// Adds error to the error string.
////
function error ($string) {
$this->error_strings[] = $string;
}
////
// function check_error
// Checks if any errors were found. and takes action.
////
function check_errors () {
if (count($this->error_strings) > 0) {
foreach ($this->error_strings as $error) { print $error . "<BR>"; }
print "Errors found: " . count($this->error_strings) . "<P>";
exit;
} else {
return true;
}
}
}
// END OF MAIN CLASS
?>