From 07882614bfd402132d2f8df23cc23c2c013b5f14 Mon Sep 17 00:00:00 2001 From: deva Date: Sat, 11 Oct 2008 10:55:28 +0000 Subject: Did a lot of work on the imagecache, and the filehandler. Now both are functional, and used indirectly throught index.php, thus requiering login for access. --- forum/htdocs/imagecache.php | 62 ------------------------------ forum/htdocs/index.php | 26 +++++++++---- forum/utils/file.php | 31 +++++++++++++++ forum/utils/filehandler.php | 2 +- forum/utils/files.php | 5 +-- forum/utils/imagecache.php | 92 ++++++++++++++++++++++++++++++++++++++++++++ forum/utils/parser.php | 93 ++++++++++++++++++--------------------------- 7 files changed, 181 insertions(+), 130 deletions(-) delete mode 100644 forum/htdocs/imagecache.php create mode 100644 forum/utils/file.php create mode 100644 forum/utils/imagecache.php (limited to 'forum') diff --git a/forum/htdocs/imagecache.php b/forum/htdocs/imagecache.php deleted file mode 100644 index f23a51d..0000000 --- a/forum/htdocs/imagecache.php +++ /dev/null @@ -1,62 +0,0 @@ - $maxwidth) $scale = $width / $maxwidth; - if($height / $scale > $maxheight) $scale = $height / $maxheight; - - $image_p = imagecreatetruecolor($width / $scale, $height / $scale); - imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width / $scale, $height / $scale, $width, $height); - - return $image_p; -} - - -$fullfilename = $IMAGECACHE . "/" . urlencode($filename); - -if(!file_exists($fullfilename)) { - - $url = parse_url($filename); - $filetype = strrchr($url["path"], '.'); - - if( true || ping($url["hostname"], 1000) != -1) { - - if(strcasecmp($filetype, ".jpeg") == 0 || strcasecmp($filetype, ".jpg") == 0) { - $image = imagecreatefromjpeg(urldecode($filename)); - if(!$image) die(404); - $image = rescale($image); - imagejpeg($image, $fullfilename, 90); - } else if(strcasecmp($filetype, ".gif") == 0) { - $image = imagecreatefromgif(urldecode($filename)); - if(!$image) die(404); - $image = rescale($image); - imagegif($image, $fullfilename); - } else if(strcasecmp($filetype, ".png") == 0) { - $image = imagecreatefrompng(urldecode($filename)); - if(!$image) die(404); - $image = rescale($image); - imagepng($image, $fullfilename); - } else { - echo "

Unknown image format " . $filetype . "

"; - } - - } -} - -header('Content-Description: File Transfer'); -header('Content-Type: image/jpeg'); -header('Content-Length: ' . filesize($fullfilename)); -header('Content-Disposition: inline; filename=' . basename($filename)); -readfile($fullfilename); - -?> \ No newline at end of file diff --git a/forum/htdocs/index.php b/forum/htdocs/index.php index a452f20..f4d2ed7 100644 --- a/forum/htdocs/index.php +++ b/forum/htdocs/index.php @@ -1,17 +1,27 @@ @@ -66,7 +76,7 @@ if($current_user) { include_once($UTIL_DIR. "/edit.php"); break; - case "files": + case "filehandler": include_once($UTIL_DIR. "/filehandler.php"); break; diff --git a/forum/utils/file.php b/forum/utils/file.php new file mode 100644 index 0000000..50993b5 --- /dev/null +++ b/forum/utils/file.php @@ -0,0 +1,31 @@ +getFile($fid); + + $filename = $PERMSTORE . "/" . $file->fid; + + $download = false; + foreach($MIME_TYPES as $m) { + if($m->name == $file->mimetype) $download = !$m->show; + } + + //header ("Cache-Control: must-revalidate, post-check=0, pre-check=0"); + header('Content-Description: File Transfer'); + header('Content-Type: ' . $file->mimetype); + header('Content-Length: ' . filesize($filename)); + + if($download) header('Content-Disposition: attachment; filename=' . basename($file->name)); + else header('Content-Disposition: inline; filename=' . basename($file->name)); + + readfile($filename); +} + +?> \ No newline at end of file diff --git a/forum/utils/filehandler.php b/forum/utils/filehandler.php index 231ddd6..9cacb1c 100644 --- a/forum/utils/filehandler.php +++ b/forum/utils/filehandler.php @@ -19,7 +19,7 @@ $files->show(); ?> -
+

File:

diff --git a/forum/utils/files.php b/forum/utils/files.php index d714ba2..6cae023 100644 --- a/forum/utils/files.php +++ b/forum/utils/files.php @@ -10,15 +10,14 @@ class File { public function link() { - global $PERMSTORE; - return "file.php?fid=" . $this->fid; + return "?mode=file&fid=" . $this->fid; } public function show() { global $PERMSTORE; echo "
\n"; - echo " fid . "\">Delete\n"; + echo " fid . "\">Delete\n"; echo "
Filename: link() . "\">" . $this->name . "(" . $this->mimetype. ")
\n"; echo "
Size: " . ceil(filesize($PERMSTORE . "/" . $this->fid) / 1024) . "kb
\n"; echo "
\n"; diff --git a/forum/utils/imagecache.php b/forum/utils/imagecache.php new file mode 100644 index 0000000..aae5e66 --- /dev/null +++ b/forum/utils/imagecache.php @@ -0,0 +1,92 @@ + $maxwidth) $scale = $width / $maxwidth; + if($height / $scale > $maxheight) $scale = $height / $maxheight; + + $image_p = imagecreatetruecolor($width / $scale, $height / $scale); + imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width / $scale, $height / $scale, $width, $height); + + return $image_p; +} + +function errorImage($filename) +{ + /* + 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); +} + +function getCachedImage($filename) +{ + global $IMAGECACHE; + $fullfilename = $IMAGECACHE . "/" . urlencode($filename); + + if(!file_exists($fullfilename)) { + + $url = parse_url($filename); + $filetype = strrchr($url["path"], '.'); + + if( true || ping($url["hostname"], 1000) != -1) { + + error_reporting(E_ERROR | E_PARSE); + + switch(tolower($filetype)) { + case ".jpeg": + case ".jpg": + $image = imagecreatefromjpeg(urldecode($filename)); + if(!$image) errorImage($filename); + $image = rescale($image); + imagejpeg($image, $fullfilename, 90); + break; + + case ".gif": + $image = imagecreatefromgif(urldecode($filename)); + if(!$image) errorImage($filename); + $image = rescale($image); + imagegif($image, $fullfilename); + break; + + case ".png": + $image = imagecreatefrompng(urldecode($filename)); + if(!$image) errorImage($filename); + $image = rescale($image); + imagepng($image, $fullfilename); + break; + + default: + if(!$image) errorImage("Unknown image type " . $filetype); + break; + } + + error_reporting(E_ALL ^ E_NOTICE); + + } + } + + header('Content-Description: File Transfer'); + header('Content-Type: image/jpeg'); + header('Content-Length: ' . filesize($fullfilename)); + header('Content-Disposition: inline; filename=' . basename($filename)); + readfile($fullfilename); +} + +?> \ No newline at end of file diff --git a/forum/utils/parser.php b/forum/utils/parser.php index e857c7e..b5a80b7 100644 --- a/forum/utils/parser.php +++ b/forum/utils/parser.php @@ -7,6 +7,7 @@ include_once($UTIL_DIR . "/smileys.php"); // strtr // ereg_replace // str_replace +// error_reporting // function parse($input, $indent = "") @@ -28,23 +29,32 @@ function parse($input, $indent = "") $output = $smile; } - // Insert images - $output = preg_replace("/http:\/\/(.*\.jpg|\.gif|\.png|\.jpeg)/s", "IMAGE$1EGAMI", $output); + // Find a unique symbols for image start and end markers + $imgstartsymbol = "A"; + $imgstartmarker = $imgstartsymbol; + while(strpos($output, $imgstartmarker)) $imgstartmarker .= $imgstartsymbol; + + $imgendsymbol = "B"; + $imgendmarker = $imgendsymbol; + while(strpos($output, $imgendmarker)) $imgendmarker .= $imgendsymbol; + + // Find and mark image URLs (so that they don't get converted into normal links) + $output = preg_replace("/http:\/\/(.*\.jpg|\.gif|\.png|\.jpeg)/", $imgstartmarker."$1".$imgendmarker, $output); // Replace URLs with tags $output = preg_replace("/http:\/\/(.*?)([\n ])/s", "$1$2", $output); - // Finish inserting images - $output = preg_replace("/IMAGE(.*?)EGAMI/s", "\"$1\"", $output); + // Convert marked images to img tags and links + $output = preg_replace("/".$imgstartmarker."(.*?)".$imgendmarker."/s", "\"$1\"", $output); // Replace [quote title=...]...[/quote] - $urls = ""; + $urls = ""; while(($start = strpos($output, "[quote"))) { - $pre = substr($output, 0, $start); - $url = substr($output, $start); - $end = strpos($url, "[/quote]") + strlen("[/quote]"); - $url = substr($url, 0, $end - strlen("[/quote]")); - $post = substr($output, $start + $end + strlen("[/quote]") ); + $pre = substr($output, 0, $start); + $url = substr($output, $start); + $end = strpos($url, "[/quote]") + strlen("[/quote]"); + $url = substr($url, 0, $end - strlen("[/quote]")); + $post = substr($output, $start + $end + strlen("[/quote]") ); $header = substr($url, 0, strpos($url, "]") + 1); $body = substr($url, strpos($url, "]") + 1); @@ -54,17 +64,17 @@ function parse($input, $indent = "") $header = str_replace(array("[quote"), "
", $header); $header = str_replace(array("]"), "
", $header); - $urls .= $pre . $header . $body . ""; - $output = $post; + $urls .= $pre . $header . $body . ""; + $output = $post; } - $urls .= $output; - $output = $urls; + $urls .= $output; + $output = $urls; // // Hack to make quotes two levels deep. // // Replace [quote title=...]...[/quote] - $urls = ""; + $urls = ""; while(($start = strpos($output, "[quote"))) { $pre = substr($output, 0, $start); $url = substr($output, $start); @@ -83,48 +93,19 @@ function parse($input, $indent = "") $urls .= $pre . $header . $body . ""; $output = $post; } - $urls .= $output; - $output = $urls; - - $search = array( - '/\[b\](.*?)\[\/b\]/is', - '/\[i\](.*?)\[\/i\]/is', - '/\[u\](.*?)\[\/u\]/is', - '/\[img\](.*?)\[\/img\]/is', - '/\[url\](.*?)\[\/url\]/is', - '/\[url\=(.*?)\](.*?)\[\/url\]/is' - ); - - $replace = array( - '$1', - '$1', - '$1', - '', - '$1', - '$2' - ); - - $output = preg_replace ($search, $replace, $output); - /* - // - $b = array("[b]", "[B]"); - $b = str_replace($b, "", $output); - $output = $b; - - $b = array("[/b]", "[/B]"); - $b = str_replace($b, "", $output); - $output = $b; - - // - $i = array("[i]", "[I]"); - $i = str_replace($i, "", $output); - $output = $i; - - $i = array("[/i]", "[/i]"); - $i = str_replace($i, "", $output); - $output = $i; - */ + $urls .= $output; + $output = $urls; + + $search = array('/\[b\](.*?)\[\/b\]/is', + '/\[i\](.*?)\[\/i\]/is', + '/\[u\](.*?)\[\/u\]/is'); + + $replace = array('$1', + '$1', + '$1'); + $output = preg_replace ($search, $replace, $output); + // Replace newlines with
tags $nls = array("\n"); $nls = str_replace($nls, "
\n", $output); -- cgit v1.2.3