From 00f910ba2562d848e6c3ceb5b7ec3da100281335 Mon Sep 17 00:00:00 2001 From: Gabriel Dunne Date: Wed, 18 May 2011 13:17:21 -0700 Subject: [PATCH 1/1] First Checkin for Quilime branch --- File_Browser.php | 387 +++++++++++++++++++++++++++++++++++++++++++++++ index.php | 32 ++++ style.css | 48 ++++++ 3 files changed, 467 insertions(+) create mode 100644 File_Browser.php create mode 100644 index.php create mode 100644 style.css diff --git a/File_Browser.php b/File_Browser.php new file mode 100644 index 0000000..f9b7dac --- /dev/null +++ b/File_Browser.php @@ -0,0 +1,387 @@ + + * @copyright 2006 Gabriel Dunne + * @license http://www.opensource.org/licenses/bsd-license.php + * @link http://quilime.com/library/php/file-browser/ + */ + + +class File_Browser +{ + +var $p = ''; +var $root = 'files/'; +var $webroot = ''; +var $files = array(); +var $images = array(); +var $audio = array(); +var $parent_dir = false; +var $thumbs = true; +var $inline_audio = true; +var $readme_file = 'README'; +var $max_image_width = 200; + + +function File_Browser($params = array()) +{ + $this->p = $this->validate_path($_GET['p']); + $this->root = isset($params['root']) ? $params['root'] : $this->root; + $this->title = isset($params['title']) ? $params['title'] : 'File Browser'; + $this->webroot = isset($params['webroot']) ? $params['webroot'] : ''; + $this->readme_file = isset($params['readme']) ? $params['readme'] : $this->readme_file; + $this->thumbs_file = isset($params['thumbs_file']) ? $params['thumbs_file'] : '.thumbs'; + + + if( is_dir($this->root . $this->p) ) { + $allFiles = $this->fileArray($this->root . $this->p, array(), array()); + + $files = array(); + $folders = array(); + $images = array(); + $audio = array(); + + foreach($allFiles as $f) { + $f['name'] = $f['type'] == "Folder" ? $f['name'] . '/' : $f['name']; + $f['title'] = $f['name']; + $f['href'] = $f['type'] == "Folder" ? "?p=" . $this->p . $f['name'] : $this->root . $this->p . $f['name']; + $f['class'] = $f['type'] == "Folder" ? 'dir' : $f['name']; + + if($f['type'] == "Folder") + $folders[] = $f; + else + $files[] = $f; + + if ( $this->thumbs && + ( strtolower($f['type']) == 'jpg' || + strtolower($f['type']) == 'png' || + strtolower($f['type']) == 'jpeg' || + strtolower($f['type']) == 'gif') ) + { + $f['imagesize'] = getimagesize($this->root . $this->p . $f['name']); + + // create thumbnail with convert + $thumb_dims = array(200, 200); + + + + $tmp_image = $this->root . $this->p . $f['name']; + $thumb = '.t/' . $this->p . $f['name']; + + if(!file_exists($thumb)) { + + $this->mk_dir('.t/' . $this->p); + + //* resize centered on white + $exec = "/usr/bin/convert \"$tmp_image\" -resize $thumb_dims[0]x$thumb_dims[1]\> \ + -size $thumb_dims[0]x$thumb_dims[1] xc:white +swap -gravity center -composite \ + \"$thumb\""; + //*/ + + exec($exec, $retval); + + } + + $f['thumb'] = $thumb; + $images[] = $f; + } + + if ( $this->inline_audio && + ( strtolower($f['type']) == 'mp3' || + strtolower($f['type']) == 'wav') ) + { + $audio[] = $f; + } + + } + $this->files = $allFiles = array_merge($folders, $files); + $this->images = $images; + $this->audio = $audio; + } + + $this->breadcrumbs = '
  • ' . $this->title . '/
  • '; + $this->breadcrumbs .= $this->breadcrumbs( $this->p ); + $this->logo = isset($params['logo']) ? '' : false; + $this->parent_dir = $this->p ? pathinfo($this->p, PATHINFO_DIRNAME) . '/' : false; + + + switch($_GET['format']) + { + case 'xspf' : + exit; + break; + + default : + break; + } +} + + + +//recursively creates a folder. +function mk_dir($path, $rights = 0777) {//{{{ + //$folder_path = array(strstr($path, '.') ? dirname($path) : $path); + if (!@is_dir($path)) { + $folder_path = array($path); + } else { + return; + } + + while(!@is_dir(dirname(end($folder_path))) + && dirname(end($folder_path)) != '/' + && dirname(end($folder_path)) != '.' + && dirname(end($folder_path)) != '') + { + array_push($folder_path, dirname(end($folder_path))); + } + + while($parent_folder_path = array_pop($folder_path)) { + if(!@mkdir($parent_folder_path, $rights)) { + user_error("Can't create folder \"$parent_folder_path\".\n"); + } + } +}//}}} + + + + +/** + * Render HTML + */ +function html() +{ + if(is_file($this->root . $this->p . $this->readme_file)) { + echo '
    ';
    +        include($this->root . $this->p . $this->readme_file);
    +        echo '
    '; + } + + // + // IMAGES + // + if($this->images) { + echo '
      '; + foreach($this->images as $i) { + $style = ''; + $sm = false; + if( $i['imagesize'][0] < $this->max_image_width) { + $sm = true; + $img_w = $i['imagesize'][0]; + } + ?> +
    1. > + +
      + src="" class="im" /> +
      + +
      +
    2. + '; + echo '
    '; + } + + // + // LISTEN + // + if ($this->audio) { + ?> + + files) { + + echo '
      '; + foreach($this->files as $f) + { + ?> +
    1. + + + + + + listen + + +
    2. + '; + + echo $this->parent_dir ? '' : ''; + + } + else { + ?> +

      +

      + root . $path)) + return $path; + else + return ''; +} + + + +/** + * file/folder array + * + * @param String $p the path + * @param String[] $extensions extensions of files to show + * @param String[] $excludes files to exclude + * @param Bool $show_hidden show hidden files + * @param Array[] the array of files + * + */ +function fileArray($p, $extensions = Array(), $excludes = Array('.', '..'), $show_hidden = false) +{ + $result = Array(); + $parsedResult = Array(); + if ($handle = opendir($p)) { + while (false !== ($file = readdir($handle))) { + if (!in_array($file, $excludes)) + { + if(!$show_hidden) + if( $file{0} == "." || $file{0} == "~" || $file{0} == "#") continue; + // is folder + if(is_dir($p . $file)) { + $folderInfo = Array ( + 'name' => $file, + 'mtime' => $mtime = filemtime($p . $file), + 'type' => 'Folder', + 'size' => null ); + $result[] = $folderInfo; + } + // is file + else { + $fileInfo = Array( + 'name' => $file, + 'mtime' => $mtime = filemtime($p . $file), + 'type' => $type = pathinfo($file, PATHINFO_EXTENSION), + 'size' => filesize($p . $file)); + $result[] = $fileInfo; + } + } + } + } + return $this->sort_array_of_arrays($result, 'name', 'name'); +} + + +/** + * sort array of arrays + * requires array of associative arrays -- not checked within array + * + * @author : http://phosphorusandlime.blogspot.com/2005/12/php-sort-array-by-one-field-in-array.html + * + * @param Array[] $ARRAY (two dimensional array of arrays) + * @param String $sortby_index index/column to be sorted on + * @param String $key_index equivalent to primary key column in SQL + */ +function sort_array_of_arrays($ARRAY, $sortby_index, $key_index) +{ + $ORDERING = array(); + $SORTED = array(); + $_DATA = array(); + $_key = ''; + $_sort_col_val = ''; + $_i = 0; + + foreach ( $ARRAY as $_DATA ) { + $_key = $_DATA[$key_index]; + $ORDERING[$_key] = $_DATA[$sortby_index]; + } + asort($ORDERING); + + foreach ( $ORDERING as $_key => $_sort_col_val ) { + foreach ($ARRAY as $_i => $_DATA) { + if ( $_key == $_DATA[$key_index] ) { + $SORTED[] = $ARRAY[$_i]; + continue; + } + } + } + return $SORTED; +} + + +/** + * breadcrumbs + * @param String $path path + * @param String $sep separator + * @param String $path_var path variable for the url + * @return String + */ +function breadcrumbs($path, $sep = "/", $path_var = "p") +{ + $pathParts = explode("/", $path); + $pathCT = 0; + $br = ""; + foreach($pathParts as $pt) { + $br .= '
    3. '.$pt.''; + if($pathCT < sizeof($pathParts)-2) + $br .= $sep; + $br .= '
    4. '; + $pathCT++; + } + return $br; +} + +} + +?> \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..774652a --- /dev/null +++ b/index.php @@ -0,0 +1,32 @@ + 'media', 'webroot' => 'http://media.quilime.com/', ) ); +$filebrowser->parent_dir = true; + +?> + + + + +<?=$filebrowser->title;?>/<?=$filebrowser->p; ?> + + + + + + +
      + html(); ?> +
      + + + diff --git a/style.css b/style.css new file mode 100644 index 0000000..ba73709 --- /dev/null +++ b/style.css @@ -0,0 +1,48 @@ + +html { font-family:monospace; font-size:12px; line-height:18px; } +body { margin:40px 10px 80px 50px; color:#999; } + +a { color:#000; text-decoration:none; } +a:hover { color:#f04 !important; } +a:visited { color:#666; } + +h1 { font-weight:normal; margin-bottom:2em; } + +ul, ol, li { margin:0; padding:0; list-style-type:none; } + + +h3 { font-size:1em; margin:0 0 1em 0; border-bottom:1px solid #ddd; margin-right:50px; } + +#logo { position:absolute; top:20px; left:-36px; } +#nav { font-size:22px; padding-bottom:10px; } +#nav li { display:inline-block; } +#breadcrumbs { + list-style-type:none; +} +#content { margin-top:30px; } + +#media { padding-bottom:5em; } +#media li { width:200px; float:left; margin-right:20px; list-style-type:none; } +#media li img { width:100%; } +#media li a span { visibility:hidden; font-style:italic; } +#media li a:hover span { visibility:visible; } +#media li a { color:#666; } +#media li a img { border:1px solid #ddd; margin-top:30px; } + +.audio a { font-size: 0.75em; font-weight:bold; font-style:italic; margin-left:0.5em; color:#900; } + +#readme { color:#009; padding:2em 0; margin-bottom:2em; } +#readme h2 {margin-top:0; font-size:1.2em;} + +#list { } +#list li { color:#aaa; list-style-type:decimal; } +#list li a { } +#list li.Folder { font-weight:bold; } +.parent { font-weight:bold; display:inline-block; margin-top:4em; padding-bottom:1em; display:none; } +.parent a { color:#999; } +.parent a:hover {color:#444;} + + + + + -- 2.34.1