PageRenderTime 116ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/FileList/filelist.php

http://rtoss.googlecode.com/
PHP | 499 lines | 371 code | 39 blank | 89 comment | 54 complexity | 949788fe7a4a79948c18e3e265348b9d MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, GPL-3.0, LGPL-3.0, GPL-2.0
  1. <?
  2. /*
  3. Directory Listing Script - Version 2
  4. ====================================
  5. Script Author: Ash Young <ash@evoluted.net>. www.evoluted.net
  6. Layout: Manny <manny@tenka.co.uk>. www.tenka.co.uk
  7. REQUIREMENTS
  8. ============
  9. This script requires PHP and GD2 if you wish to use the
  10. thumbnail functionality.
  11. INSTRUCTIONS
  12. ============
  13. 1) Unzip all files
  14. 2) Edit this file, making sure everything is setup as required.
  15. 3) Upload to server
  16. 4) ??????
  17. 5) Profit!
  18. CONFIGURATION
  19. =============
  20. Edit the variables in this section to make the script work as
  21. you require.
  22. Start Directory - To list the files contained within the current
  23. directory enter '.', otherwise enter the path to the directory
  24. you wish to list. The path must be relative to the current
  25. directory.
  26. */
  27. $startdir = '.';
  28. /*
  29. Show Thumbnails? - Set to true if you wish to use the
  30. scripts auto-thumbnail generation capabilities.
  31. This requires that GD2 is installed.
  32. */
  33. $showthumbnails = false;
  34. /*
  35. Show Directories - Do you want to make subdirectories available?
  36. If not set this to false
  37. */
  38. $showdirs = true;
  39. /*
  40. Force downloads - Do you want to force people to download the files
  41. rather than viewing them in their browser?
  42. */
  43. $forcedownloads = false;
  44. /*
  45. Hide Files - If you wish to hide certain files or directories
  46. then enter their details here. The values entered are matched
  47. against the file/directory names. If any part of the name
  48. matches what is entered below then it is now shown.
  49. */
  50. $hide = array(
  51. 'dlf',
  52. 'index.php',
  53. 'Thumbs',
  54. '.htaccess',
  55. '.htpasswd'
  56. );
  57. /*
  58. Show index files - if an index file is found in a directory
  59. to you want to display that rather than the listing output
  60. from this script?
  61. */
  62. $displayindex = true;
  63. /*
  64. Allow uploads? - If enabled users will be able to upload
  65. files to any viewable directory. You should really only enable
  66. this if the area this script is in is already password protected.
  67. */
  68. $allowuploads = true;
  69. /* Show delete button? */
  70. $showdelete=true;
  71. /* Upload/Delete password */
  72. $uppass = "uppass";
  73. /* Use PATH_INFO for the dirs? */
  74. $usepathinfo=true;
  75. /*
  76. Overwrite files - If a user uploads a file with the same
  77. name as an existing file do you want the existing file
  78. to be overwritten?
  79. */
  80. $overwrite = false;
  81. /*
  82. Index files - The follow array contains all the index files
  83. that will be used if $displayindex (above) is set to true.
  84. Feel free to add, delete or alter these
  85. */
  86. $indexfiles = array (
  87. 'index.html',
  88. 'index.htm',
  89. 'index.php',
  90. 'default.htm',
  91. 'default.html'
  92. );
  93. /*
  94. File Icons - If you want to add your own special file icons use
  95. this section below. Each entry relates to the extension of the
  96. given file, in the form <extension> => <filename>.
  97. These files must be located within the dlf directory.
  98. */
  99. $filetypes = array (
  100. 'png' => 'jpg.gif',
  101. 'jpeg' => 'jpg.gif',
  102. 'bmp' => 'jpg.gif',
  103. 'jpg' => 'jpg.gif',
  104. 'gif' => 'gif.gif',
  105. 'zip' => 'archive.png',
  106. 'rar' => 'archive.png',
  107. 'exe' => 'exe.gif',
  108. 'setup' => 'setup.gif',
  109. 'txt' => 'text.png',
  110. 'htm' => 'html.gif',
  111. 'html' => 'html.gif',
  112. 'fla' => 'fla.gif',
  113. 'swf' => 'swf.gif',
  114. 'xls' => 'xls.gif',
  115. 'doc' => 'doc.gif',
  116. 'sig' => 'sig.gif',
  117. 'fh10' => 'fh10.gif',
  118. 'pdf' => 'pdf.gif',
  119. 'psd' => 'psd.gif',
  120. 'rm' => 'real.gif',
  121. 'mpg' => 'video.gif',
  122. 'mpeg' => 'video.gif',
  123. 'mov' => 'video2.gif',
  124. 'avi' => 'video.gif',
  125. 'eps' => 'eps.gif',
  126. 'gz' => 'archive.png',
  127. 'asc' => 'sig.gif',
  128. );
  129. /*
  130. That's it! You are now ready to upload this script to the server.
  131. Only edit what is below this line if you are sure that you know what you
  132. are doing!
  133. */
  134. error_reporting(0);
  135. // cgi.fix_pathinfo Workaround
  136. if(isset($_SERVER['ORIG_PATH_INFO'])) $_SERVER['PATH_INFO'] = $_SERVER['ORIG_PATH_INFO'];
  137. // IIS Workaround
  138. if(strpos($_SERVER['PATH_INFO'],$_SERVER['SCRIPT_NAME'])!==false) $_SERVER['PATH_INFO'] = substr($_SERVER['PATH_INFO'],strlen($_SERVER['SCRIPT_NAME']));
  139. function properSize($size,$maxcnt=0) {
  140. $suffix=''; $suxAry=array("KB","MB","GB","TB");
  141. $ccnt=$maxcnt?$maxcnt:count($suxAry);
  142. for($cnt=0;$cnt<$ccnt;$cnt++)
  143. if($size > 1024) {$size/=1024; $suffix=$suxAry[$cnt];}
  144. return $suffix?sprintf("%.1f",$size).$suffix:$size.'B';
  145. }
  146. $myname=preg_replace('/.*\/+/', '', $_SERVER['PHP_SELF']);
  147. $mypath=preg_replace("/$myname$/", '', $_SERVER['PHP_SELF']);
  148. $myfilespath='http://'.$_SERVER['HTTP_HOST'].$mypath.'dlf';
  149. if(!function_exists('imagecreatetruecolor')) $showthumbnails = false;
  150. $leadon = $startdir;
  151. if($leadon=='.') $leadon = '';
  152. if((substr($leadon, -1, 1)!='/') && $leadon!='') $leadon = $leadon . '/';
  153. $startdir = $leadon;
  154. $cdir=isset($_GET['dir'])?$_GET['dir']:substr($_SERVER['PATH_INFO'],1);
  155. if($cdir) {
  156. //check this is okay.
  157. if(substr($cdir, -1, 1)!='/') {
  158. $cdir = $cdir . '/';
  159. }
  160. $dirok = true;
  161. $dirnames = split('/', $cdir);
  162. for($di=0; $di<sizeof($dirnames); $di++) {
  163. if($di<(sizeof($dirnames)-2)) {
  164. $dotdotdir = $dotdotdir . $dirnames[$di] . '/';
  165. }
  166. if($dirnames[$di] == '..') {
  167. $dirok = false;
  168. }
  169. }
  170. if(substr($cdir, 0, 1)=='/') {
  171. $dirok = false;
  172. }
  173. if($dirok) {
  174. $leadon = $leadon . $cdir;
  175. }
  176. }
  177. if($_GET['download'] && $forcedownloads) {
  178. $file = str_replace('/', '', $_GET['download']);
  179. $file = str_replace('..', '', $file);
  180. if(file_exists($leadon . $file)) {
  181. header("Content-type: application/x-download");
  182. header("Content-Length: ".filesize($leadon . $file));
  183. header('Content-Disposition: attachment; filename="'.$file.'"');
  184. readfile($leadon . $file);
  185. die();
  186. }
  187. }
  188. if($allowuploads && $_FILES['file'] && $_POST['pass']==$uppass) {
  189. $upload = true;
  190. if(!$overwrite) {
  191. if(file_exists($leadon.$_FILES['file']['name'])) {
  192. $upload = false;
  193. }
  194. }
  195. if($upload) {
  196. move_uploaded_file($_FILES['file']['tmp_name'], $leadon . $_FILES['file']['name']);
  197. @chmod(0666,$leadon . $_FILES['file']['name']);
  198. }
  199. }
  200. if($_POST['action']=="delete" && file_exists($leadon.$_POST['file']) && $_POST['pass']==$uppass) {
  201. @unlink($leadon.$_POST['file']);
  202. }
  203. $opendir = $leadon;
  204. if(!$leadon) $opendir = '.';
  205. if(!file_exists($opendir)) {
  206. $opendir = '.';
  207. $leadon = $startdir;
  208. }
  209. clearstatcache();
  210. if ($handle = opendir($opendir)) {
  211. while (false !== ($file = readdir($handle))) {
  212. //first see if this file is required in the listing
  213. if ($file == "." || $file == "..") continue;
  214. $discard = false;
  215. for($hi=0;$hi<sizeof($hide);$hi++) {
  216. if(strpos($file, $hide[$hi])!==false) {
  217. $discard = true;
  218. }
  219. }
  220. if($discard) continue;
  221. if (@filetype($leadon.$file) == "dir") {
  222. if(!$showdirs) continue;
  223. $n++;
  224. if($_GET['sort']=="date") {
  225. $key = @filemtime($leadon.$file) . ".$n";
  226. }
  227. else {
  228. $key = $n;
  229. }
  230. $dirs[$key] = $file . "/";
  231. }
  232. else {
  233. $n++;
  234. if($_GET['sort']=="date") {
  235. $key = @filemtime($leadon.$file) . ".$n";
  236. }
  237. elseif($_GET['sort']=="size") {
  238. $key = @filesize($leadon.$file) . ".$n";
  239. }
  240. else {
  241. $key = $n;
  242. }
  243. $files[$key] = $file;
  244. if($displayindex) {
  245. if(in_array(strtolower($file), $indexfiles)) {
  246. header("Location: $mypath$leadon$file");
  247. die();
  248. }
  249. }
  250. }
  251. }
  252. closedir($handle);
  253. }
  254. //sort our files
  255. if($_GET['sort']=="date") {
  256. @ksort($dirs, SORT_NUMERIC);
  257. @ksort($files, SORT_NUMERIC);
  258. }
  259. elseif($_GET['sort']=="size") {
  260. @natcasesort($dirs);
  261. @ksort($files, SORT_NUMERIC);
  262. }
  263. else {
  264. @natcasesort($dirs);
  265. @natcasesort($files);
  266. }
  267. //order correctly
  268. if($_GET['order']=="desc" && $_GET['sort']!="size") {$dirs = @array_reverse($dirs);}
  269. if($_GET['order']=="desc") {$files = @array_reverse($files);}
  270. $dirs = @array_values($dirs); $files = @array_values($files);
  271. $pathtitle=str_replace('//','/',dirname($_SERVER['PHP_SELF']).'/'.$leadon);
  272. $pathtitle=str_replace('\\','',$pathtitle);
  273. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  274. <html xmlns="http://www.w3.org/1999/xhtml">
  275. <head>
  276. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  277. <title>Directory Listing of <?=$pathtitle;?></title>
  278. <link rel="stylesheet" type="text/css" href="<?=$myfilespath;?>/styles.css" />
  279. <?
  280. if($showthumbnails) {
  281. ?>
  282. <script language="javascript" type="text/javascript">
  283. <!--
  284. function o(n, i) {
  285. document.images['thumb'+n].src = '<?=$myfilespath;?>/i.php?f='+i;
  286. }
  287. function f(n) {
  288. document.images['thumb'+n].src = '<?=$myfilespath;?>/trans.gif';
  289. }
  290. //-->
  291. </script>
  292. <?
  293. }
  294. ?>
  295. <!--[if IE]>
  296. <style type="text/css">
  297. #listing a {
  298. float:left;
  299. }
  300. </style>
  301. <![endif]-->
  302. </head>
  303. <body>
  304. <div id="container">
  305. <h1>Directory Listing of <?=$pathtitle;?></h1>
  306. <div id="breadcrumbs"> <a href="<?=$_SERVER['PHP_SELF'];?>">home</a>
  307. <?
  308. $breadcrumbs = split('/', $leadon);
  309. if(($bsize = sizeof($breadcrumbs))>0) {
  310. $sofar = '';
  311. for($bi=0;$bi<($bsize-1);$bi++) {
  312. $sofar = $sofar . $breadcrumbs[$bi] . '/';
  313. echo ' &gt; <a href="'.$_SERVER['PHP_SELF'].'/'.urlencode($sofar).'">'.$breadcrumbs[$bi].'</a>';
  314. }
  315. }
  316. $baseurl = $usepathinfo?$_SERVER['PHP_SELF'] . '/'.$cdir . '?':$_SERVER['PHP_SELF'] . '?dir='.$cdir . '&';
  317. $fileurl = 'sort=name&amp;order=asc';
  318. $sizeurl = 'sort=size&amp;order=asc';
  319. $dateurl = 'sort=date&amp;order=asc';
  320. switch ($_GET['sort']) {
  321. case 'name':
  322. if($_GET['order']=='asc') $fileurl = 'sort=name&amp;order=desc';
  323. break;
  324. case 'size':
  325. if($_GET['order']=='asc') $sizeurl = 'sort=size&amp;order=desc';
  326. break;
  327. case 'date':
  328. if($_GET['order']=='asc') $dateurl = 'sort=date&amp;order=desc';
  329. break;
  330. default:
  331. $fileurl = 'sort=name&amp;order=desc';
  332. break;
  333. }
  334. ?>
  335. </div>
  336. <div id="listingcontainer">
  337. <div id="listingheader">
  338. <div id="headerfile"><a href="<?=$baseurl . $fileurl;?>">File</a></div>
  339. <div id="headersize"><a href="<?=$baseurl . $sizeurl;?>">Size</a></div>
  340. <div id="headermodified"><a href="<?=$baseurl . $dateurl;?>">Last Modified</a></div>
  341. </div>
  342. <div id="listing">
  343. <?
  344. $class = 'b';
  345. if($dirok) {
  346. ?>
  347. <div><a href="<?=$usepathinfo?$_SERVER['PHP_SELF'].'/'.urlencode($dotdotdir):$_SERVER['PHP_SELF'] . '?dir='.urlencode($dotdotdir);?>" class="<?=$class;?>"><img src="<?=$myfilespath;?>/dirup.png" alt="Folder" /><strong>..</strong> <em>-</em> <?=date ("M d Y h:i:s A", filemtime($dotdotdir));?></a></div>
  348. <?
  349. if($class=='b') $class='w';
  350. else $class = 'b';
  351. }
  352. $arsize = sizeof($dirs);
  353. for($i=0;$i<$arsize;$i++) {
  354. ?>
  355. <div><a href="<?=$usepathinfo?$_SERVER['PHP_SELF'].'/'.urlencode($leadon.$dirs[$i]):$_SERVER['PHP_SELF'] . '?dir='.urlencode($leadon.$dirs[$i]);?>" class="<?=$class;?>"><img src="<?=$myfilespath;?>/folder.png" alt="<?=$dirs[$i];?>" /><strong><?=$dirs[$i];?></strong> <em>-</em> <?=date ("M d Y h:i:s A", filemtime($leadon.$dirs[$i]));?></a></div>
  356. <?
  357. if($class=='b') $class='w';
  358. else $class = 'b';
  359. }
  360. $arsize = sizeof($files);
  361. for($i=0;$i<$arsize;$i++) {
  362. $icon = 'unknown.png';
  363. $ext = strtolower(substr($files[$i], strrpos($files[$i], '.')+1));
  364. $supportedimages = array('gif', 'png', 'jpeg', 'jpg');
  365. $thumb = '';
  366. if($showthumbnails && in_array($ext, $supportedimages)) {
  367. $thumb = '<span><img src="'.$myfilespath.'/trans.gif" alt="'.$files[$i].'" name="thumb'.$i.'" /></span>';
  368. $thumb2 = ' onmouseover="o('.$i.', \''.urlencode($leadon . $files[$i]).'\');" onmouseout="f('.$i.');"';
  369. }
  370. if($filetypes[$ext]) {
  371. $icon = $filetypes[$ext];
  372. }
  373. $filename = $files[$i];
  374. if(strlen($filename)>42) {
  375. $filename = substr($files[$i], 0, 39) . '...';
  376. }
  377. $fileurl = $mypath.$leadon.$files[$i];
  378. if($forcedownloads) {
  379. $fileurl = $_SESSION['PHP_SELF'] . '?dir=' . urlencode($leadon) . '&amp;download=' . urlencode($files[$i]);
  380. }
  381. ?>
  382. <div><?php if($showdelete) { ?><a class="<?=$class;?>" style="float:left;text-align:left;width:0.5em;padding:2px;clear:none;" href="<?=$_SERVER['PHP_SELF'];?>?file=<?=urlencode($leadon.$files[$i]);?>&amp;action=del">D</a><?php } ?><a href="<?=$fileurl;?>" class="<?=$class;?>"<?=$thumb2;?>><img src="<?=$myfilespath;?>/<?=$icon;?>" alt="<?=$files[$i];?>" /><strong><?=$filename;?></strong> <em><?=properSize(filesize($leadon.$files[$i]),2);?></em> <?=date ("M d Y h:i:s A", filemtime($leadon.$files[$i]));?><?=$thumb;?></a></div>
  383. <?
  384. if($class=='b') $class='w';
  385. else $class = 'b';
  386. }
  387. ?></div>
  388. <?
  389. if($allowuploads) {
  390. $phpallowuploads = (bool) ini_get('file_uploads');
  391. $phpmaxsize = ini_get('upload_max_filesize');
  392. $phpmaxsize = trim($phpmaxsize);
  393. $last = strtolower($phpmaxsize{strlen($phpmaxsize)-1});
  394. switch($last) {
  395. case 'g':
  396. $phpmaxsize *= 1024;
  397. case 'm':
  398. $phpmaxsize *= 1024;
  399. }
  400. ?>
  401. <div id="upload">
  402. <div id="uploadtitle"><strong>File Upload</strong> (Max Filesize: <?=$phpmaxsize;?>KB)</div>
  403. <div id="uploadcontent">
  404. <?
  405. if($phpallowuploads) {
  406. ?>
  407. <form method="post" action="<?=$_SERVER['PHP_SELF'];?>?dir=<?=urlencode($leadon);?>" enctype="multipart/form-data">
  408. <input type="file" name="file" /> Password: <input type="password" name="pass" value="" size="8" /> <input type="submit" value="Upload" />
  409. </form>
  410. <?
  411. }
  412. else {
  413. ?>
  414. File uploads are disabled in your php.ini file. Please enable them.
  415. <?
  416. }
  417. ?>
  418. </div>
  419. </div>
  420. <?
  421. }
  422. ?>
  423. <?
  424. if($_GET['action']=="del" && file_exists($leadon.$_GET['file'])) {
  425. ?>
  426. <div id="upload">
  427. <div id="uploadtitle"><strong>File Delete Confirmation</strong></div>
  428. <div id="uploadcontent">
  429. Input password and press Delete button if you are sure to delete file <strong>&quot;<?=$_GET['file'];?>&quot;</strong>.<br />
  430. <form method="post" action="<?=$_SERVER['PHP_SELF'];?>?dir=<?=urlencode($leadon);?>" enctype="multipart/form-data">
  431. <input type="hidden" name="file" value="<?=$_GET['file'];?>"/><input type="hidden" name="action" value="delete"/>Password: <input type="password" name="pass" value="" size="8" /> <input type="submit" value="Delete" />
  432. </form>
  433. </div>
  434. </div>
  435. <?
  436. }
  437. ?>
  438. </div>
  439. </div>
  440. <div id="copy">Directory Listing Script &copy;2005 <a href="http://evoluted.net/">Ash Young</a>. Design by <a href="http://www.tenka.co.uk/">Manny</a>.</div>
  441. </body>
  442. </html>