| 1 | <? |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | * Copyright (c) 2005, Tae-young Jung <master@mytears.org> |
|---|
| 5 | * All rights reserved. |
|---|
| 6 | * |
|---|
| 7 | * Redistribution and use in source and binary forms, with or without |
|---|
| 8 | * modification, are permitted provided that the following conditions |
|---|
| 9 | * are met: |
|---|
| 10 | * |
|---|
| 11 | * 1. Redistributions of source code must retain the above copyright |
|---|
| 12 | * notice, this list of conditions and the following disclaimer. |
|---|
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 14 | * notice, this list of conditions and the following disclaimer in the |
|---|
| 15 | * documentation and/or other materials provided with the distribution. |
|---|
| 16 | * |
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
|---|
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|---|
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
|---|
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|---|
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|---|
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|---|
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|---|
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|---|
| 27 | * SUCH DAMAGE. |
|---|
| 28 | * |
|---|
| 29 | */ |
|---|
| 30 | |
|---|
| 31 | require('Smarty/Smarty.class.php'); |
|---|
| 32 | require('lib_mime.php'); |
|---|
| 33 | |
|---|
| 34 | require('config.php'); |
|---|
| 35 | |
|---|
| 36 | /* compare function */ |
|---|
| 37 | function comp( $a, $b ){ |
|---|
| 38 | global $type, $descend; |
|---|
| 39 | |
|---|
| 40 | /* compared by name */ |
|---|
| 41 | if( $type == 'N' ) |
|---|
| 42 | return $descend * strcmp( $a[name], $b[name] ); |
|---|
| 43 | |
|---|
| 44 | /* compared by modification time */ |
|---|
| 45 | else if( $type == 'M' ) |
|---|
| 46 | return $descend * ($a[timestamp] - $b[timestamp]); |
|---|
| 47 | |
|---|
| 48 | /* compared by size */ |
|---|
| 49 | else |
|---|
| 50 | return $descend * ($a[r_size] - $b[r_size]); |
|---|
| 51 | |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | /* get current path */ |
|---|
| 55 | if( !$_GET[path] ) |
|---|
| 56 | $path = $open_basedir; |
|---|
| 57 | else |
|---|
| 58 | $path = $open_basedir."/".$_GET[path]; |
|---|
| 59 | |
|---|
| 60 | $basedir = realpath($open_basedir); |
|---|
| 61 | $path = realpath( $path ); |
|---|
| 62 | |
|---|
| 63 | /* make webpath */ |
|---|
| 64 | $webpath = ".".substr($path, strlen($basedir)); |
|---|
| 65 | $webpath_file = $open_basedir.substr($path, strlen($basedir)); |
|---|
| 66 | |
|---|
| 67 | /* make smarty object */ |
|---|
| 68 | $smarty = new Smarty; |
|---|
| 69 | |
|---|
| 70 | $smarty->left_delimiter = '<!--{'; |
|---|
| 71 | $smarty->right_delimiter = '}-->'; |
|---|
| 72 | |
|---|
| 73 | $smarty->assign( "skin_dir", "templates/".$skin ); |
|---|
| 74 | $smarty->assign( "encoding", $encoding ); |
|---|
| 75 | $smarty->assign( "location", $webpath ); |
|---|
| 76 | |
|---|
| 77 | /* sandbox: avoid show upper directory */ |
|---|
| 78 | if( strncmp( $basedir, $path, strlen($basedir) ) != 0 ){ |
|---|
| 79 | $msg = "not permitted to show upper directory.."; |
|---|
| 80 | $smarty->assign( "msg", $msg ); |
|---|
| 81 | |
|---|
| 82 | $smarty->display( $skin.'/error.tpl'); |
|---|
| 83 | } |
|---|
| 84 | else if( $dirhandle=@opendir($path) ){ |
|---|
| 85 | |
|---|
| 86 | if( isset($_GET[M]) ){ |
|---|
| 87 | $type = 'M'; |
|---|
| 88 | $arg = "&M"; |
|---|
| 89 | |
|---|
| 90 | if( !isset($_GET[D]) ) |
|---|
| 91 | $marg = "&D"; |
|---|
| 92 | } |
|---|
| 93 | else if( isset($_GET[S]) ){ |
|---|
| 94 | $type = 'S'; |
|---|
| 95 | $arg = "&S"; |
|---|
| 96 | |
|---|
| 97 | if( !isset($_GET[D]) ) |
|---|
| 98 | $sarg = "&D"; |
|---|
| 99 | } |
|---|
| 100 | else { |
|---|
| 101 | $type = 'N'; |
|---|
| 102 | $arg = "&N"; |
|---|
| 103 | |
|---|
| 104 | if( !isset($_GET[D]) ) |
|---|
| 105 | $narg = "&D"; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | if( isset($_GET[D]) ){ |
|---|
| 109 | $descend = -1; |
|---|
| 110 | $arg .= "&D"; |
|---|
| 111 | } |
|---|
| 112 | else |
|---|
| 113 | $descend = 1; |
|---|
| 114 | |
|---|
| 115 | $th = array( |
|---|
| 116 | "name" => "<a href='$PHPSELF?path=$webpath&N$narg' rel=\"nofollow\">Filename</a>", |
|---|
| 117 | "size" => "<a href='$PHPSELF?path=$webpath&S$sarg' rel=\"nofollow\">Size</a>", |
|---|
| 118 | "mtime" => "<a href='$PHPSELF?path=$webpath&M$marg' rel=\"nofollow\">Modification time</a>" |
|---|
| 119 | ); |
|---|
| 120 | |
|---|
| 121 | $dir = array(); |
|---|
| 122 | $file = array(); |
|---|
| 123 | |
|---|
| 124 | while( $name = readdir($dirhandle) ){ |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | if( $show_hidden ){ |
|---|
| 128 | if( strcmp( $name, "." ) == 0 ) |
|---|
| 129 | continue; |
|---|
| 130 | else if( strcmp( $name, ".." ) == 0 ) |
|---|
| 131 | continue; |
|---|
| 132 | } |
|---|
| 133 | else if( $name[0] == '.' ) |
|---|
| 134 | continue; |
|---|
| 135 | |
|---|
| 136 | /* get modification time */ |
|---|
| 137 | $times = filemtime( $path."/".$name ); |
|---|
| 138 | $mtime = date("Y-m-d g:i:s A", $times); |
|---|
| 139 | |
|---|
| 140 | /* is this directory? */ |
|---|
| 141 | if( is_dir($path."/".$name) ) { |
|---|
| 142 | |
|---|
| 143 | $name = "<a href='$PHPSELF?path=$webpath/$name$arg'>$name</a>"; |
|---|
| 144 | $dir[] = array( |
|---|
| 145 | name => $name, |
|---|
| 146 | img => 'folder.gif', |
|---|
| 147 | mtime => $mtime, |
|---|
| 148 | timestamp => $times |
|---|
| 149 | ); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | /* file */ |
|---|
| 153 | else { |
|---|
| 154 | $r_size = $size = filesize( $path."/".$name ); |
|---|
| 155 | $ext = substr( $name, strrpos($name, ".")+1 ); |
|---|
| 156 | |
|---|
| 157 | /* human readable size */ |
|---|
| 158 | if( $size < 1024 ) |
|---|
| 159 | $size = $size; |
|---|
| 160 | else if( $size < 1048576 ) |
|---|
| 161 | $size = floor($size/1024)."K"; |
|---|
| 162 | else if( $size < 130023424 ) |
|---|
| 163 | $size = floor($size/1048576)."M"; |
|---|
| 164 | else |
|---|
| 165 | $size = floor($size/1073741824)."G"; |
|---|
| 166 | |
|---|
| 167 | $name = "<a href='$webpath_file/$name'>$name</a>"; |
|---|
| 168 | $file[] = array( |
|---|
| 169 | name => $name, |
|---|
| 170 | img => mime_type($ext), |
|---|
| 171 | size => $size, |
|---|
| 172 | mtime => $mtime, |
|---|
| 173 | timestamp => $times, |
|---|
| 174 | r_size => $r_size |
|---|
| 175 | ); |
|---|
| 176 | |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | } |
|---|
| 180 | closedir(); |
|---|
| 181 | |
|---|
| 182 | /* sorting with filename */ |
|---|
| 183 | if( $type == 'N' ){ |
|---|
| 184 | usort( $dir, comp ); |
|---|
| 185 | usort( $file, comp ); |
|---|
| 186 | } |
|---|
| 187 | /* sorting with modification time */ |
|---|
| 188 | else if( $type == 'M' ){ |
|---|
| 189 | usort( $dir, comp ); |
|---|
| 190 | usort( $file, comp ); |
|---|
| 191 | } |
|---|
| 192 | /* sorting with size */ |
|---|
| 193 | else if( $type == 'S' ){ |
|---|
| 194 | usort( $file, comp ); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | /* add parent direcory link */ |
|---|
| 198 | if( strcmp($webpath,".") != 0 ){ |
|---|
| 199 | $name = "<a href='$PHP_SELF?path=$webpath/..$arg' rel=\"nofollow\">Parent Directory</a>"; |
|---|
| 200 | $mtime = date("Y-m-d g:i:s A", filemtime( $path."/.." )); |
|---|
| 201 | |
|---|
| 202 | $dir = array_merge( |
|---|
| 203 | array( |
|---|
| 204 | array( |
|---|
| 205 | name => $name, |
|---|
| 206 | img => 'back.gif', |
|---|
| 207 | mtime => $mtime |
|---|
| 208 | ) |
|---|
| 209 | ), |
|---|
| 210 | $dir |
|---|
| 211 | ); |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | $smarty->assign( 'th', $th ); |
|---|
| 215 | $smarty->assign( 'dir', $dir ); |
|---|
| 216 | $smarty->assign( 'file', $file ); |
|---|
| 217 | $smarty->display( $skin.'/index.tpl'); |
|---|
| 218 | |
|---|
| 219 | } |
|---|
| 220 | /* directory open failed... */ |
|---|
| 221 | else { |
|---|
| 222 | $msg = "invalid path or permission denided"; |
|---|
| 223 | $smarty->assign( "msg", $msg ); |
|---|
| 224 | |
|---|
| 225 | $smarty->display( $skin.'/error.tpl'); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | ?> |
|---|