* Licensed under the GNU GPL, the version of your liking. * Do copy, share and modify! * * Additions in functionality by Andreas Aronsson . ***/ require_once path(__FILE__).'Thumbnail.class.php'; class DirectoryListing { var $EXCLUDE_FILES = array( ".", "..", ".htaccess", ".thumbnails", "index.php", "DirectoryListing.class.php", "Thumbnail.class.php", "visible.css", "hidden.css", "index.css", "icons", ".icons" // , // ".???" ); var $FILETYPE_ICON_PATH = 'http://courses.cs.tau.ac.il/pics'; var $_filetype_icon_path; var $_thumbnail_width; var $_thumbnail_height; var $_thumbnail_quality; var $_max_filename_length; var $_max_content_length; function DirectoryListing( $path, $columns = 4, $width = 75, $height = NULL, $max_filename_length = 80, $max_content_length = 60, $quality = 80 ) { echo "\n"; $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; // 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)) { $dirs[]=$file.'/'; } else { $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)) { if ($cnt++ == 0) { echo "\t\t\n"; } echo "\t\t\t\n"; if ($cnt == $columns) { echo "\t\t\n"; $cnt = 0; } } } if ($cnt != $columns) echo "\t\t\n"; echo "\t
".$this->href($file)."
\n"; @closedir($dir_handle); } function href($file) { $type = $this->type($file); $href = ''; if (is_dir($file)) { $href .= ''; } else { switch($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 .= ''; break; case 'txt': case 'html': case 'c': case 'cpp': case 'h': case 'rb': case 'py': case 'php': case 'pl': case 'asp': $fh = fopen($file, 'r'); if ($fh) $href .= substr(fread($fh, filesize($file)), 0, $this->max_content_length); break; default: $href .= ''; break; } } $href .= ''; $words = $this->words(substr($file, 0, $this->max_filename_length)); $underscore = ''; $cnt = 0; 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; if (is_dir($file)) $href .= $link_text."/"; else $href .= $link_text.$type.""; 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; } } ?>