From 28856d6f9b7d726b1681df9384f3ef000dbf6f4c Mon Sep 17 00:00:00 2001
From: deva <deva>
Date: Sun, 12 Oct 2008 16:02:03 +0000
Subject: Made file preview (original image or png with filename as text).
 Added error image as png with error text.

---
 forum/htdocs/config.php.defaults |  2 +-
 forum/htdocs/index.php           |  3 ++-
 forum/utils/file.php             | 26 ++++++++++++++++++++++++++
 forum/utils/imagecache.php       | 25 ++++++++++++-------------
 forum/utils/parser.php           |  2 +-
 5 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/forum/htdocs/config.php.defaults b/forum/htdocs/config.php.defaults
index 51f7f4e..7f0d643 100644
--- a/forum/htdocs/config.php.defaults
+++ b/forum/htdocs/config.php.defaults
@@ -6,5 +6,5 @@ $ADMIN_TIMEOUT = 100000;
 $PERMSTORE = $DATA_DIR . "/files";
 $IMAGECACHE = $DATA_DIR . "/imagecache";
 $LOG_FILE = $DATA_DIR . "/forum.log";
-$FILE_MAX_SIZE = 5*1024*1024; // 5kb
+$FILE_MAX_SIZE = 5*1024; // 5kb
 ?>
diff --git a/forum/htdocs/index.php b/forum/htdocs/index.php
index 99169ab..ab63d9b 100644
--- a/forum/htdocs/index.php
+++ b/forum/htdocs/index.php
@@ -17,7 +17,8 @@ if($current_user) {
 
 	case "file":
 		include_once($UTIL_DIR. "/file.php");
-		getFile($fid);
+		if($preview) getFilePreview($fid);
+		else getFile($fid);
 		return;
 	}
 }
diff --git a/forum/utils/file.php b/forum/utils/file.php
index 50993b5..25cb4c2 100644
--- a/forum/utils/file.php
+++ b/forum/utils/file.php
@@ -28,4 +28,30 @@ function getFile($fid)
 	readfile($filename); 
 }
 
+function getFilePreview($fid)
+{
+	global $DATA_DIR, $PERMSTORE, $MIME_TYPES;
+	$files = new Files($DATA_DIR . "/files.xml");
+	$file = $files->getFile($fid);
+	
+	$filename = $PERMSTORE . "/" . $file->fid;
+	
+	if(strstr($file->mimetype, "image")) {
+		header('Content-Description: File Transfer');
+		header('Content-Type: ' . $file->mimetype);
+		header('Content-Length: ' . filesize($filename));
+		header('Content-Disposition: inline; filename=' . basename($file->name));
+		readfile($filename); 
+	} else {
+		header("Content-type: image/png");
+		$im = @imagecreate(8 + strlen($file->name) * 5, 20)
+			or die("Cannot Initialize new GD image stream");
+		$background_color = imagecolorallocate($im, 0, 0, 0);
+		$text_color = imagecolorallocate($im, 233, 14, 91);
+		imagestring($im, 1, 5, 5,  $file->name, $text_color);
+		imagepng($im);
+		imagedestroy($im);
+	}
+}
+
 ?>
\ No newline at end of file
diff --git a/forum/utils/imagecache.php b/forum/utils/imagecache.php
index ac3ebc3..0c18e1a 100644
--- a/forum/utils/imagecache.php
+++ b/forum/utils/imagecache.php
@@ -22,17 +22,16 @@ function rescale($image) {
 	return $image_p;
 }
 
-function errorImage($filename)
+function errorImage($message)
 {
-	/*
-	 header('Content-Description: File Transfer');
-	 header('Content-Type: image/jpeg');
-	 header('Content-Length: ' . filesize($fullfilename));
-	 header('Content-Disposition: inline; filename=' . basename($filename));
-	 readfile($fullfilename); 
-	*/
-	echo "Error fetching image: " . $filename;
-	die(404);
+	header("Content-type: image/png");
+	$im = @imagecreate(8 + strlen($message) * 5, 20)
+		or die("Cannot Initialize new GD image stream");
+	$background_color = imagecolorallocate($im, 0, 0, 0);
+	$text_color = imagecolorallocate($im, 233, 14, 91);
+	imagestring($im, 1, 5, 5,  $message, $text_color);
+	imagepng($im);
+	imagedestroy($im);
 }
 
 function getCachedImage($filename)
@@ -63,21 +62,21 @@ function getCachedImage($filename)
 			case ".jpeg":
 			case ".jpg":
 				$image = imagecreatefromjpeg(urldecode($filename));
-				if(!$image) errorImage($filename);
+				if(!$image) errorImage("Could not read: ". $filename);
 				$image = rescale($image);
 				imagejpeg($image, $fullfilename, 90);
 				break;
 
 			case ".gif":
 				$image = imagecreatefromgif(urldecode($filename));
-				if(!$image) errorImage($filename);
+				if(!$image) errorImage("Could not read: ". $filename);
 				$image = rescale($image);
 				imagegif($image, $fullfilename);
 				break;
 
 			case ".png":
 				$image = imagecreatefrompng(urldecode($filename));
-				if(!$image) errorImage($filename);
+				if(!$image) errorImage("Could not read: ". $filename);
 				$image = rescale($image);
 				imagepng($image, $fullfilename);
 				break;
diff --git a/forum/utils/parser.php b/forum/utils/parser.php
index 6d47162..5864753 100644
--- a/forum/utils/parser.php
+++ b/forum/utils/parser.php
@@ -48,7 +48,7 @@ function parse($input, $indent = "")
   $output = preg_replace("/".$imgstartmarker."(.*?)".$imgendmarker."/s", "<a href=\"http://$1\"><img alt=\"$1\" src=\"?mode=imagecache&amp;uri=http://$1\"/></a>", $output);
 
 	// Replace URLs with <a></a> tags
-  $output = preg_replace("/\{\{([0-9]*?)\}\}/s", "<a href=\"?mode=file&amp;fid=$1\">File: $1</a>", $output);
+  $output = preg_replace("/\{\{([0-9]*?)\}\}/s", "<a href=\"?mode=file&amp;fid=$1\"><img src=\"?mode=file&amp;preview=1&amp;fid=$1\"/></a>", $output);
 
 	// Replace [quote title=...]...[/quote]
   $urls = "";
-- 
cgit v1.2.3