rendered paste body<?php
/* PHP Thumbnail AutoIndex
* by Tomasz Sterna / smoku / <tomek@xiaoka.com>
* License: GPL
*/
define (VERSION, '2.0');
/*****************************************************************/
$DirectoriesFirst = true; // display directories first
$OpenNewWin = false; // open files in new window
$subDirs = true; // subdirs support
$thWidth = 94; // thumbnail width
$thHeight = 94; // thumbnail height
$font = 2;
$readmefile = 'README.html';
$headerfile = 'HEADER.html';
$cacheDir = '/var/www/xiaoka/staff.xiaoka.com/smoku/.cache/thaindx.'; // empty to disable thumbnail cache
// ----------------------------------------------------------------------------
function pngThumbnail($file) {
global $font, $cacheDir, $thWidth, $thHeight;
lastmodifiedCheck($file);
if ($cacheDir !== '' ) {
$thumbnailfile = $cacheDir . $_SERVER['HTTP_HOST'] . ereg_replace('/[^/]*$', '/', $_SERVER['REQUEST_URI']) . "{$file}-{$thWidth}x{$thHeight}.png";
if (is_readable($thumbnailfile) && filemtime($file) <= filemtime($thumbnailfile)) {
readfile($thumbnailfile);
exit();
}
mkPath(dirname($thumbnailfile), 0700);
@unlink($thumbnailfile);
}
if (function_exists('imagecreatetruecolor')) {
$im = imagecreatetruecolor($thWidth+18, $thHeight+18);
$white = imagecolorclosest ( $im, 255, 255, 255);
$frame = imagecolorclosest ( $im, 208, 208, 208);
$black = imagecolorclosest ( $im, 0, 0, 0);
} else {
$im = imagecreate($thWidth+18, $thHeight+18);
$white = imagecolorallocate ($im, 255, 255, 255);
$frame = imagecolorallocate ( $im, 208, 208, 208);
$black = imagecolorallocate ( $im, 0, 0, 0);
}
imagefilledrectangle ( $im, 0, 0, $thWidth+18, $thHeight+18, $white);
$pathelems = split('/', $file);
$filename = array_pop($pathelems);
$size = @getimagesize($file);
$name = ereg_replace('(.*)\.[^\.]*$', '\1', $filename);
switch($size[2]) {
case '':
$name = $filename;
if (is_dir($file)) {
$si = imgFolder();
$size[0] = 94;
$size[1] = 94;
} else {
if (class_exists('finfo')) {
$info = new finfo( FILEINFO_MIME );
$ri = imgTXT($info->file(realpath($file)));
}
elseif (function_exists('mime_content_type')) {
$ri = imgTXT(mime_content_type($file));
}
else
$ri = imgTXT('no MIME support');
$si = $ri['image'];
$size[0] = $ri['width'];
$size[1] = $ri['height'];
}
break;
case 2:
$si = imagecreatefromjpeg($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['height'];
}
$im_w = $size[0];
$im_h = $size[1];
$sx = $thWidth / $im_w;
$sy = $thHeight / $im_h;
if ($sy < $sx ) {
$scale = $sy;
} else {
$scale = $sx;
}
if ($scale > 1) $scale = 1;
$dw = floor($im_w * $scale);
$dh = floor($im_h * $scale);
$dx = floor(($thWidth+18 - $dw)/2);
$dy = floor(($thHeight+2 - $dh)/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, $thWidth+9, $thHeight+1, $frame);
$px = ($thWidth+18 - imagefontwidth($font) * strlen($name)) / 2;
if ($px < 0) $px = 0;
imagestring($im, $font, $px, $thHeight+4, $name, $black);
imageinterlace($im, 1);
imagepng($im);
if ($cacheDir !== '' && $size[2]) imagepng($im, $thumbnailfile);
imagedestroy($im);
imagedestroy($si);
exit();
}
function imgTXT($text) {
global $thWidth, $thHeight;
$font = 5;
$txw = imagefontwidth(5) * strlen($text);
$txh = imagefontheight(5);
$imw = $txw > $thWidth ? $txw : $thWidth;
$imh = $thHeight * ($imw / $thWidth);
if (function_exists('imagecreatetruecolor')) {
$img = imagecreatetruecolor($imw, $imh);
$grey = imagecolorclosest ( $img, 246, 246, 246);
$black = imagecolorclosest ( $img, 0, 0, 0);
} else {
$img = imagecreate($imw, $imh);
$grey = imagecolorallocate ( $img, 246, 246, 246);
$black = imagecolorallocate ( $img, 0, 0, 0);
}
imagefilledrectangle ($img, 0, 0, $imw, $imh, $grey);
$dx = floor(($imw - $txw) / 2);
$dy = floor(($imh - $txh) / 2);
imagestring($img, 5, $dx, $dy, $text, $black);
$ret = array();
$ret['image'] = $img;
$ret['width'] = $imw;
$ret['height'] = $imh;
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, $dir) {
global $font, $OpenNewWin, $subDirs, $thWidth, $thHeight;
$stdimg = 'border="0" width="' . ($thWidth + 18) . '" height="' . ($thHeight + 18) . '" hspace="4" vspace="4"';
$linkfile = str_replace('%', '%25', ($subDirs?"$dir$file":"$file"));
$linkfile = str_replace('#', '%23', $linkfile);
$linkfile = str_replace('?', '%3F', $linkfile);
$linkfile = str_replace(' ', '%20', $linkfile);
if (is_dir("$dir$file")) {
if ($subDirs) {
return "<a href=\"?$linkfile/\"><img src=\"?$linkfile\" alt=\"$file/\" $stdimg></a>";
} else {
return "<a href=\"$linkfile/\"><img src=\"?$linkfile\" alt=\"$file/\" $stdimg></a>";
}
} else {
$size = @filesize("$dir$file");
$suffix = '';
if ($size > 4000) {
$size = floor($size/1024);
$suffix = 'k';
}
if ($size > 2000) {
$size = floor($size/1024);
$suffix = 'M';
}
$ret = '';
$pathelems = split('/', $file);
$filename = array_pop($pathelems);
if (($imsize = @getimagesize("$dir$file")) && ($imsize[2] == 1 || $imsize[2] == 2 || $imsize[2] == 3)) {
$name = ereg_replace('(.*)\.[^\.]*$', '\1', $filename);
if (imagefontwidth($font) * strlen($name) > 112) {
$name .= "\n";
} else {
$name = '';
}
$ret .= "<a href=\"$linkfile\"".($OpenNewWin?' target="_blank"':'').'>';
$ret .= "<img src=\"?$linkfile\" alt=\"$filename\" title=\"";
$ret .= "$name";
$ret .= "Dimensions: {$imsize[0]} x {$imsize[1]}\n";
$ret .= "Type: {$imsize['mime']}\n";
$ret .= "Size: $size {$suffix}B\" $stdimg></a>";
} else {
if (imagefontwidth($font) * strlen($file) > 112) {
$name .= "$filename\n";
} else {
$name = '';
}
$ret .= "<a href=\"$linkfile\"".($OpenNewWin?' target="_blank"':'').'>';
$ret .= "<img src=\"?$linkfile\" alt=\"$filename\" title=\"";
$ret .= "$name";
if (class_exists('finfo')) {
$info = new finfo( FILEINFO_MIME );
$ret .= "Type: ".$info->file(realpath($file))."\n";
}
elseif (function_exists('mime_content_type')) {
$ret .= "Type: ".@mime_content_type($file)."\n";
}
$ret .= "Size: $size {$suffix}B\" $stdimg></a>";
}
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_NONE_MATCH'])) {
$etag = (int) $_SERVER['HTTP_IF_NONE_MATCH'];
if ($etag != 0 && $etag == filemtime($file)) {
header('Etag: '.filemtime($file));
header('HTTP/1.1 304 Not Modified');
exit();
}
}
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);
}
// ----------------------------------------------------------------------------
$subdir = '';
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);
$file = ereg_replace('^/', '', $file);
if ($subDirs && is_dir($file) && $file[strlen($file) - 1] == '/') {
$subdir = $file;
} else {
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("Last-Modified: $mtime");
header('Etag: '.filemtime($file)."/{$thWidth}x{$thHeight}");
header('Expires:');
header('Cache-Control:');
header('Pragma:');
pngThumbnail($file);
exit();
}
}
}
if (!isset($_SERVER['QUERY_STRING']) || $_SERVER['QUERY_STRING'] == '' || $subdir != '') {
$path = split('/', $_SERVER['PHP_SELF']);
$self = array_pop($path);
$dir = join('/', $path);
$currdir = array_pop($path);
$parent = join('/', $path);
$realpath = split('/', $_SERVER['SCRIPT_FILENAME']);
$realself = array_pop($realpath);
$realdir = join('/', $realpath);
if ($subdir != '') {
$bdir = $dir;
$dir = $dir.'/'.$subdir;
$realdir = $realdir.'/'.$subdir;
$subpath = split('/', $subdir);
$tmp = array_pop($subpath); // empty, after slash
$tmp = array_pop($subpath); // current dir
$subparent = join('/', $subpath);
if ($subparent != '') {
$parent = "?$subparent";
} else {
$parent = $bdir;
}
}
if ($dir == '')
$dir = '/';
$mtime = gmdate('D, d M Y H:i:s \G\M\T', filemtime($realdir));
header('Connection: close');
header("Last-Modified: $mtime");
header('Etag: '.filemtime($realdir));
header('Expires:');
header('Cache-Control:');
header('Pragma:');
lastmodifiedCheck($realdir);
print <<< EOD
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<title>Index of $dir</title>
</head>
<body bgcolor="#FFFFFF">
EOD;
if (is_readable("$realdir/$headerfile")) {
readfile("$realdir/$headerfile");
} else {
print "<h1>Index of $dir</h1>\n";
}
print <<< EOD
<pre><hr />
<a href="$parent/">Parent Directory</a></pre>
EOD;
$files = array();
if ($handle = opendir($realdir)) {
while (false !== ($file = readdir($handle))) {
if (is_readable("$realdir/$file") && $file[0] != '.' && $file != $self && $file != $readmefile && $file != $headerfile) {
$files[] = "$realdir/$file";
}
}
closedir($handle);
}
if ($DirectoriesFirst) {
usort($files, 'cmpFiles');
} else {
natcasesort($files);
}
foreach ($files as $file) {
$tmp = split('/', $file);
$file = array_pop($tmp);
print ThLink($file, $subdir);
}
if (function_exists('imagecopyresampled')) {
$gdver = 'libGD2';
} else {
$gdver = 'libGD1(aka "ugly thumbnails")';
}
if (is_readable("$realdir/$readmefile")) {
echo '<br clear="all">';
readfile("$realdir/$readmefile");
}
print '<hr />
<address><a href="http://staff.xiaoka.com/smoku/stuff/ThAutoIndex/">Thumbnail AutoIndex</a>/'.VERSION." PHP/$gdver at {$_SERVER['SERVER_NAME']}</address>
</body></html>";
}
exit();
?>