* Licensed under either the GNU GPL (any version of your liking) or the CreativeCommons * BY-SA 3.0 license. * * Do copy, share and modify! * * Additions in functionality by Andreas Aronsson . ***/ error_reporting(E_ALL); require_once path(__FILE__).'Thumbnail.class.php'; class DirectoryListing { var $EXCLUDE_FILES = array( ".", "..", ".htaccess", ".thumbnails", "index.php", "dir-generator.php", "file.php", "googlehostedservice.html", "mail", "DirectoryListing.class.php", "Thumbnail.class.php", "visible.css", "hidden.css", "index.css", "icons", ".icons" // , // ".???" ); var $FILETYPE_ICON_PATH = '.icons'; var $_filetype_icon_path; var $_thumbnail_width; var $_thumbnail_height; var $_thumbnail_quality; var $_max_filename_length; var $_max_content_length; function DirectoryListing( $path, $url = '', $width = 75, $height = NULL, $max_filename_length = 8, $max_content_length = 60, $quality = 75 ) { $this->filetype_icon_path = $this->FILETYPE_ICON_PATH.$this->pd(); $this->thumbnail_width = $width; $this->thumbnail_height = $height; $this->max_filename_length = $max_filename_length; $this->max_content_length = $max_content_length; $this->thumbnail_quality = $quality; $dir_handle = @opendir($path) or die("Unable to open $path"); $cnt = 0; $dirs = array(); $files = array(); // Split the file and directory cases. // Add trailing slash if directory. while (false !== ($file=readdir($dir_handle))) { if (substr($file,0,1) != ".") { if (is_dir($path.'/'.$file)) { array_push($dirs, $file); } else { array_push($files, $file); } } } @closedir($dir_handle); // Sorting alphabetically. if ($files) { natcasesort($files); } if ($dirs) { natcasesort($dirs); } $files=array_merge($dirs, $files); foreach ($files as $file) { if (! in_array($file, $this->EXCLUDE_FILES)) { echo "
" .$this->href($file, $url) ."
\n"; } } @closedir($dir_handle); } function href($file, $url) { $type = $this->type($file); $href = '
'; if (is_dir($file)) { $href .= ''.$file.''; } else { switch(strtolower($type)) { case 'jpg': case 'png': case 'gif': case 'jpeg': case 'jpe': $tn = new Thumbnail($file, $this->thumbnail_width, $this->thumbnail_height, $this->thumbnail_quality); if ($tn) $href .= ''.$file.''; break; case 'txt': case 'html': case 'c': case 'cpp': case 'h': case 'rb': case 'py': case 'php': case 'pl': case 'asp': case 'phps': case 'log': case 'sh': case 'list': case 'diff': case 'patch': case 'tex': case 'po': case 'conf': $fh = fopen($file, 'r'); if (filesize($file)!= 0) { // If file is NOT empty. if ($fh) $href .= ''.htmlspecialchars(substr(fread($fh, filesize($file)), 0, $this->max_content_length)).''; } else { // If file IS empty. if ($this->max_content_length >= 12) $href .= ''; // Changed from "[empty file]" to maintain language agnosticism. } break; case 'pdf': $output = ".thumbnails/".basename(str_replace(".pdf", ".jpg", $file)); if (!file_exists($output)) { exec("gs -q -dNOPAUSE -dBATCH -sDEVICE=jpeg -sOutputFile=\"$output\" \"$file\""); exec("convert -resize 50x75 \"$output\" \"$output\""); } $href .= ''.$file.''; break; default: $href .= '?'; break; } } $href .= '
'; $words = $this->words(substr($file, 0, $this->max_filename_length)); $underscore = ''; $cnt = 0; $link_text = ''; foreach ($words as $word) { if ($cnt++ > 0) $link_text .= $underscore; $link_text .= $word; } if ($type) $type = ''; if (strlen($file) > $this->max_filename_length) $type = '…'.$type; else if ($type) $type = '.'.$type; $href .= ''; $href .= $link_text.$type.""; if (is_dir($file)) $href .= "/"; return $href; } function words($file) { $words = explode('_', trim($file)); $last_word = array_pop($words); $last_word = explode('.', $last_word); $last_word = $last_word[0]; $words[] = $last_word; return $words; } function type($file) { $chunks = explode('.', trim($file)); if (count($chunks) > 1) return array_pop($chunks); else return false; } function pd() { $OS = strtolower(substr(PHP_OS, 0, 3)); $OS == 'win' ? $PD = '\\' : $PD = '/'; return $PD; } } ?>