summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--conf/mime.conf15
-rw-r--r--fetch.php112
-rw-r--r--inc/common.php2
-rw-r--r--inc/utils.php36
4 files changed, 149 insertions, 16 deletions
diff --git a/conf/mime.conf b/conf/mime.conf
new file mode 100644
index 000000000..26657a895
--- /dev/null
+++ b/conf/mime.conf
@@ -0,0 +1,15 @@
+#Add extensions and mimetypes of files you want to allow to upload here
+
+jpg image/jpeg
+jpeg image/jpeg
+gif image/gif
+png image/png
+tgz application/octet-stream
+tar application/octet-stream
+gz application/octet-stream
+zip application/octet-stream
+pdf application/octet-stream #FIXME
+txt text/plain
+html text/html
+htm text/html
+
diff --git a/fetch.php b/fetch.php
index c417f354b..cd35b9be6 100644
--- a/fetch.php
+++ b/fetch.php
@@ -6,11 +6,12 @@
* @author Andreas Gohr <andi@splitbrain.org>
*/
-
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__)).'/');
require_once(DOKU_INC.'inc/init.php');
require_once(DOKU_INC.'inc/common.php');
+ require_once(DOKU_INC.'inc/utils.php');
require_once(DOKU_INC.'inc/auth.php');
+ $mimetypes = getMimeTypes();
//get input
$MEDIA = $_REQUEST['media'];
@@ -18,6 +19,12 @@
$WIDTH = $_REQUEST['w'];
$HEIGHT = $_REQUEST['h'];
$EXT = media_extension($MEDIA);
+ if($EXT !== false){
+ $MIME = $mimetypes[$EXT];
+ }else{
+ $EXT = 'unknown';
+ $MIME = 'application/octet-stream';
+ }
//media to local file
if(preg_match('#^(https?|ftp)://#i',$MEDIA)){
@@ -39,7 +46,8 @@
//check permissions (namespace only)
if(auth_quickaclcheck(getNS($MEDIA).':X') < AUTH_READ){
header("HTTP/1.0 401 Unauthorized");
- //fixme add some image for imagefiles else display login message
+ //fixme add some image for imagefiles
+ print 'Unauthorized';
exit;
}
$FILE = mediaFN($MEDIA);
@@ -48,26 +56,55 @@
//check file existance
if(!@file_exists($FILE)){
header("HTTP/1.0 404 Not Found");
- //FIXME add some default broken image or display message
+ //FIXME add some default broken image
+ print 'Not Found';
exit;
}
-
- //FIXME handle image resizing
+ //handle image resizing
+ if((substr($MIME,0,5) == 'image') && $WIDTH){
+ $FILE = get_resized($FILE,$EXT,$WIDTH,$HEIGHT);
+ }
- //FIXME add correct mimetype
- //FIXME send Size header
- //FIXME send Lastmod Handler
- //FIXME cache headers??
+ //FIXME set sane cachecontrol headers
//FIXME handle conditional and partial requests
//send file
- passthru($FILE) ;
+ header("Content-Type: $MIME");
+ header('Last-Modified: '.date('r',filemtime($FILE)));
+ header('Content-Length: '.filesize($FILE));
+
+
+ $fp = @fopen($FILE,"rb");
+ fpassthru($fp);
+ fclose($fp);
+
+/* ------------------------------------------------------------------------ */
+
+/**
+ * Resizes the given image to the given size
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function get_resized($file, $ext, $w, $h=0){
+ global $conf;
+ $md5 = md5($file);
+ $info = getimagesize($file);
+ if(!$h) $h = round(($w * $info[1]) / $info[0]);
-/* ----------- */
+ //cache
+ $local = $conf['mediadir'].'/_cache/'.$md5.'.'.$w.'x'.$h.'.'.$ext;
+ $mtime = @filemtime($local); // 0 if not exists
+
+ if( $mtime > filemtime($file) || resize_image($ext,$file,$info[0],$info[1],$local,$w,$h) ){
+ return $local;
+ }
+ //still here? resizing failed
+ return $file;
+}
/**
* Returns the wanted cachetime in seconds
@@ -119,7 +156,60 @@ function get_from_URL($url,$ext,$cache){
return false;
}
+/**
+ * resize images
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function resize_image($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
+ global $conf;
+
+ if($conf['gdlib'] < 1) return false; //no GDlib available or wanted
+
+ // create an image of the given filetype
+ if ($ext == 'jpg' || $ext == 'jpeg'){
+ if(!function_exists("imagecreatefromjpeg")) return false;
+ $image = @imagecreateFromjpeg($from);
+ }elseif($ext == 'png') {
+ if(!function_exists("imagecreatefrompng")) return false;
+ $image = @imagecreatefrompng($from);
+ }elseif($ext == 'gif') {
+ if(!function_exists("imagecreatefromgif")) return false;
+ $image = @imagecreatefromgif($from);
+ }
+ if(!$image) return false;
+
+ if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor")){
+ $newimg = @imagecreatetruecolor ($to_w, $to_h);
+ }
+ if(!$newimg) $newimg = @imagecreate($to_w, $to_h);
+ if(!$newimg) return false;
+
+ // create cachedir
+ io_makeFileDir($to);
+ //try resampling first
+ if(function_exists("imagecopyresampled")){
+ if(!@imagecopyresampled($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h)) {
+ imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
+ }
+ }else{
+ imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
+ }
+
+ if ($ext == 'jpg' || $ext == 'jpeg'){
+ if(!function_exists("imagejpeg")) return false;
+ return imagejpeg($newimg, $to, 70);
+ }elseif($ext == 'png') {
+ if(!function_exists("imagepng")) return false;
+ return imagepng($newimg, $to);
+ }elseif($ext == 'gif') {
+ if(!function_exists("imagegif")) return false;
+ return imagegif($newimg, $to);
+ }
+
+ return false;
+}
//Setup VIM: ex: et ts=2 enc=utf-8 :
diff --git a/inc/common.php b/inc/common.php
index 268072cbf..871196dd5 100644
--- a/inc/common.php
+++ b/inc/common.php
@@ -426,7 +426,7 @@ function mediaFN($id){
global $conf;
$id = cleanID($id);
$id = str_replace(':','/',$id);
- $fn = $conf['datadir'].'/'.utf8_encodeFN($id);
+ $fn = $conf['mediadir'].'/'.utf8_encodeFN($id);
return $fn;
}
diff --git a/inc/utils.php b/inc/utils.php
index 45b9b0d23..6610c217a 100644
--- a/inc/utils.php
+++ b/inc/utils.php
@@ -9,6 +9,36 @@
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
/**
+ * Returns the (known) extension of a given filename
+ *
+ * returns false if not a known extension
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function media_extension($file){
+ $exts = join('|',array_keys(getMimeTypes()));
+ if(preg_match('#\.('.$exts.')$#i',$file,$matches)){
+ return strtolower($matches[1]);
+ }
+
+ return false;
+}
+
+
+/**
+ * returns a hash of mimetypes
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function getMimeTypes() {
+ static $mime = NULL;
+ if ( !$mime ) {
+ $mime = confToHash(DOKU_INC . 'conf/mime.conf');
+ }
+ return $mime;
+}
+
+/**
* returns a hash of acronyms
*
* @author Harry Fuecks <hfuecks@gmail.com>
@@ -68,13 +98,11 @@ function getInterwiki() {
function confToHash($file) {
$conf = array();
$lines = @file( $file );
- if ( !$lines ) {
- return $conf;
- }
+ if ( !$lines ) return $conf;
foreach ( $lines as $line ) {
//ignore comments
- $line = preg_replace('/[^&]#.*$/','',$line);
+ $line = preg_replace('/[^&]?#.*$/','',$line);
$line = trim($line);
if(empty($line)) continue;
$line = preg_split('/\s+/',$line,2);