/* PHP Thumbnail AutoIndex * by Tomasz Sterna / smoku / * License: GPL */ define('VERSION', "1.3"); /*****************************************************************/ $DirectoriesFirst = true; // display directories first $OpenNewWin = false; // open files in new window $font = 2; $readmefile = 'README.html'; $headerfile = 'HEADER.html'; $cacheDir = '/var/tmp/thaindx.'; // empty to disable thumbnail cache // ---------------------------------------------------------------------------- function pngThumbnail($file) { global $font, $cacheDir; lastmodifiedCheck($file); if ($cacheDir !== '' ) { $thumbnailfile = $cacheDir . $_SERVER["HTTP_HOST"] . ereg_replace('/[^/]*$', '/', $_SERVER["REQUEST_URI"]) . $file . '.png'; if (is_readable($thumbnailfile) && filemtime($file) <= filemtime($thumbnailfile)) { readfile($thumbnailfile); exit(); } mkPath(dirname($thumbnailfile), 0700); @unlink($thumbnailfile); } if (function_exists('imagecreatetruecolor')) { $im = imagecreatetruecolor(112, 112); $white = imagecolorclosest ( $im, 255, 255, 255); $frame = imagecolorclosest ( $im, 208, 208, 208); $black = imagecolorclosest ( $im, 0, 0, 0); } else { $im = imagecreate(112, 112); $white = imagecolorallocate ($im, 255, 255, 255); $frame = imagecolorallocate ( $im, 208, 208, 208); $black = imagecolorallocate ( $im, 0, 0, 0); } imagefilledrectangle ( $im, 0, 0, 112, 112, $white); $size = @getimagesize($file); $name = ereg_replace('(.*)\.[^\.]*$', '\1', $file); switch($size[2]) { case '': $name = $file; if (is_dir($file)) { $si = imgFolder(); $size[0] = 94; $size[1] = 94; } else { $ri = imgTXT(mime_content_type($file)); $si = $ri['image']; $size[0] = $ri['width']; $size[1] = $ri['width']; } break; case 2: #$si = imagecreatefromjpeg(dirname(__FILE__)."/".$file); $si = imagecreatefromjpeg($file); #header("X-jpeg: $file"); break; case 3: $si = imagecreatefrompng($file); break; case 1: if (function_exists('imagecreatefromgif')){ $si = imagecreatefromgif($file); break; } default : $arr = split('/', $size['mime']); $ri = imgTXT(strtoupper($arr[1])); $si = $ri['image']; $size[0] = $ri['width']; $size[1] = $ri['width']; } $im_w = $size[0]; $im_h = $size[1]; if ( $im_w > $im_h ) { $dx = 9; $dw = 94; $dh = floor(94 * $im_h/$im_w); $dy = floor((96 - $dh)/2); } else { $dy = 1; $dh = 94; $dw = floor(94 * $im_w/$im_h); $dx = floor((112 - $dw)/2); } if (function_exists('imagecopyresampled')) { imagecopyresampled( $im, $si, $dx, $dy, 0, 0, $dw, $dh, $im_w, $im_h); } else { imagecopyresized( $im, $si, $dx, $dy, 0, 0, $dw, $dh, $im_w, $im_h); } imagerectangle ( $im, 8, 0, 103, 95, $frame); $px = (112 - imagefontwidth($font) * strlen($name)) / 2; if ($px < 0) $px = 0; imagestring($im, $font, $px, 98, $name, $black); imageinterlace($im, 1); imagepng($im); if ($cacheDir !== '' && $size[2]) imagepng($im, $thumbnailfile); imagedestroy($im); imagedestroy($si); exit(); } function imgTXT($text) { $font = 5; $txw = imagefontwidth(5) * strlen($text); $txh = imagefontheight(5); if (function_exists('imagecreatetruecolor')) { $img = imagecreatetruecolor($txw, $txw); $grey = imagecolorclosest ( $img, 246, 246, 246); $black = imagecolorclosest ( $img, 0, 0, 0); } else { $img = imagecreate($txw, $txw); $grey = imagecolorallocate ( $img, 246, 246, 246); $black = imagecolorallocate ( $img, 0, 0, 0); } imagefilledrectangle ( $img, 0, 0, $txw, $txw, $grey); $py = ($txw - $txh) / 2; imagestring($img, 5, 0, $py, $text, $black); $ret = array(); $ret['image'] = $img; $ret['width'] = $txw; return $ret; } function imgFolder() { $a = array( 18,24, 38,24, 43,29, 76,29, 76,70, 18,70 ); if (function_exists('imagecreatetruecolor')) { $img = imagecreatetruecolor(94, 94); $black = imagecolorclosest ( $img, 0, 0, 0); $yellow = imagecolorclosest ( $img, 250, 240, 0); $white = imagecolorclosest ( $img, 255, 255, 255); } else { $img = imagecreate(94, 94); $black = imagecolorallocate ( $img, 0, 0, 0); $yellow = imagecolorallocate ( $img, 250, 240, 0); $white = imagecolorallocate ( $img, 255, 255, 255); } imagefilledrectangle ( $img, 0, 0, 94, 94, $white); if (function_exists('imagesetthickness')) { imagesetthickness($img, 1); } imagefilledpolygon ($img, $a, 6, $yellow); imagepolygon ($img, $a, 6, $black); imageline ($img, 40, 32, 76, 32, $black); imageline ($img, 18, 34, 34, 34, $black); imageline ($img, 34, 34, 40, 32, $black); return $img; } function ThLink($file) { global $font, $OpenNewWin; $stdimg = 'border="0" width="112" height="112" hspace="4" vspace="4"'; $linkfile = str_replace('%', '%25', $file); $linkfile = str_replace('#', '%23', $linkfile); $linkfile = str_replace('?', '%3F', $linkfile); if (is_dir($file)) { return "\"$file/\""; } else { $size = filesize($file); $suffix = ''; if ($size > 4000) { $size = floor($size/1024); $suffix = 'k'; } if ($size > 2000) { $size = floor($size/1024); $suffix = 'M'; } $ret = ''; if (($imsize = @getimagesize($file)) && ($imsize[2] == 1 || $imsize[2] == 2 || $imsize[2] == 3)) { $name = ereg_replace('(.*)\.[^\.]*$', '\1', $file); if (imagefontwidth($font) * strlen($name) > 112) { $name .= "\n"; } else { $name = ''; } $ret .= "'; $ret .= "\"$file\""; } else { if (imagefontwidth($font) * strlen($file) > 112) { $name .= "$file\n"; } else { $name = ''; } $ret .= "'; $ret .= "\"$file\""; } return $ret; } } function cmpFiles($a, $b) { if (is_dir($a) && (!is_dir($b))) { return -1; } elseif ((!is_dir($a)) && is_dir($b)) { return 1; } else { return strnatcasecmp($a, $b); } } function lastmodifiedCheck($file) { if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) { $ifmodified = strtotime($_SERVER["HTTP_IF_MODIFIED_SINCE"]); if ($ifmodified !== -1 && $ifmodified >= filemtime($file)) { header("HTTP/1.1 304 Not Modified"); exit(); } } } function mkPath($dir, $mode) { if (is_dir($dir)) return true; $pDir = dirname($dir); if (!mkPath($pDir, $mode)) return false; return mkdir($dir, $mode); } // ---------------------------------------------------------------------------- if (isset($_SERVER['QUERY_STRING']) && ($_SERVER['QUERY_STRING'] !== '')) { $file = $_SERVER['QUERY_STRING']; $regs = array(); while (ereg('%([013-9a-fA-F][0-9a-fA-F]|2[0-46-9a-fA-F])', $file, $regs)) { $file = str_replace($regs[0], chr(hexdec($regs[0])), $file); } $file = str_replace('%25', '%', $file); $file = str_replace('/', '', $file); if (is_readable($file)) { $mtime = gmdate('D, d M Y H:i:s \G\M\T', filemtime($file)); header('Content-type: image/png'); header('Connection: close'); header("Date: $mtime"); header("Last-Modified: $mtime"); pngThumbnail($file); exit(); } } else { $path = split('/', $_SERVER['PHP_SELF']); $self = array_pop($path); $dir = join('/', $path); $currdir = array_pop($path); $parent = join('/', $path); $fulldir = $_SERVER['DOCUMENT_ROOT'].$dir; $mtime = gmdate('D, d M Y H:i:s \G\M\T', filemtime($fulldir)); header('Connection: close'); header("Date: $mtime"); header("Last-Modified: $mtime"); lastmodifiedCheck($fulldir); print ' Index of '.$dir.' '; if (is_readable($headerfile)) { include($headerfile); } else { print "

Index of $dir

\n"; } print '

Parent Directory
\n"; $files = array(); if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && $file != $self && $file[0] != '.' && $file != $readmefile && $file != $headerfile) { $files[] = $file; } } closedir($handle); } if ($DirectoriesFirst) { usort($files, 'cmpFiles'); } else { natcasesort($files); } foreach ($files as $file) { print ThLink($file); } if (function_exists('imagecopyresampled')) { $gdver = 'libGD2'; } else { $gdver = 'libGD1(aka "ugly thumbnails")'; } if (is_readable($readmefile)) { echo '
'; include($readmefile); } print '
Thumbnail AutoIndex/'.VERSION." PHP/$gdver at {$_SERVER['SERVER_NAME']}
"; } exit(); ?>