diff options
Diffstat (limited to 'forum/utils')
-rw-r--r-- | forum/utils/file.php | 31 | ||||
-rw-r--r-- | forum/utils/filehandler.php | 2 | ||||
-rw-r--r-- | forum/utils/files.php | 5 | ||||
-rw-r--r-- | forum/utils/imagecache.php | 92 | ||||
-rw-r--r-- | forum/utils/parser.php | 93 |
5 files changed, 163 insertions, 60 deletions
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 @@ +<?php + +include_once("config.php"); +include_once($UTIL_DIR . "/mimetypes.php"); +include_once($UTIL_DIR . "/files.php"); + +function getFile($fid) +{ + global $DATA_DIR, $PERMSTORE, $MIME_TYPES; + $files = new Files($DATA_DIR . "/files.xml"); + $file = $files->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(); ?> -<form enctype="multipart/form-data" action="?mode=files&task=upload" method="post"> +<form enctype="multipart/form-data" action="?mode=filehandler&task=upload" method="post"> <p> File: <input name="userfile" type="file"> </p> 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 "<div class=\"file\">\n"; - echo " <a class=\"delete\" href=\"?mode=files&task=delete&fid=" . $this->fid . "\">Delete</a>\n"; + echo " <a class=\"delete\" href=\"?mode=filehandler&task=delete&fid=" . $this->fid . "\">Delete</a>\n"; echo " <div class=\"filename\">Filename: <a href=\"" . $this->link() . "\">" . $this->name . "</a>(" . $this->mimetype. ")</div>\n"; echo " <div class=\"filesize\">Size: " . ceil(filesize($PERMSTORE . "/" . $this->fid) / 1024) . "kb</div>\n"; echo "</div>\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 @@ +<?php + +include_once("config.php"); +include_once($UTIL_DIR . "/ping.php"); + + +function rescale($image) { + + $maxwidth = 300; + $maxheight = 240; + + $width = imagesx($image); + $height = imagesy($image); + + $scale = 1; + if($width > $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 <a/> links) + $output = preg_replace("/http:\/\/(.*\.jpg|\.gif|\.png|\.jpeg)/", $imgstartmarker."$1".$imgendmarker, $output); // Replace URLs with <a></a> tags $output = preg_replace("/http:\/\/(.*?)([\n ])/s", "<a href=\"http://$1\">$1</a>$2", $output); - // Finish inserting images - $output = preg_replace("/IMAGE(.*?)EGAMI/s", "<img alt=\"$1\" src=\"imagecache.php?filename=$1\"/>", $output); + // Convert marked images to img tags and links + $output = preg_replace("/".$imgstartmarker."(.*?)".$imgendmarker."/s", "<a href=\"http://$1\"><img alt=\"$1\" src=\"?mode=imagecache&uri=http://$1\"/></a>", $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"), "<div class=\"quote\">", $header); $header = str_replace(array("]"), " </div>", $header); - $urls .= $pre . $header . $body . "</div>"; - $output = $post; + $urls .= $pre . $header . $body . "</div>"; + $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 . "</div>"; $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( - '<strong>$1</strong>', - '<em>$1</em>', - '<u>$1</u>', - '<img src="$1" />', - '<a href="$1">$1</a>', - '<a href="$1">$2</a>' - ); - - $output = preg_replace ($search, $replace, $output); - /* - // <b></b> - $b = array("[b]", "[B]"); - $b = str_replace($b, "<strong>", $output); - $output = $b; - - $b = array("[/b]", "[/B]"); - $b = str_replace($b, "</strong>", $output); - $output = $b; - - // <i></i> - $i = array("[i]", "[I]"); - $i = str_replace($i, "<em>", $output); - $output = $i; - - $i = array("[/i]", "[/i]"); - $i = str_replace($i, "</em>", $output); - $output = $i; - */ + $urls .= $output; + $output = $urls; + + $search = array('/\[b\](.*?)\[\/b\]/is', + '/\[i\](.*?)\[\/i\]/is', + '/\[u\](.*?)\[\/u\]/is'); + + $replace = array('<strong>$1</strong>', + '<em>$1</em>', + '<u>$1</u>'); + $output = preg_replace ($search, $replace, $output); + // Replace newlines with <br/> tags $nls = array("\n"); $nls = str_replace($nls, "<br/>\n", $output); |