xref: /dokuwiki/lib/exe/fetch.php (revision 4e4067763a8794182ca4fb8bc788537afa0c7f04)
1f62ea8a1Sandi<?php
2f62ea8a1Sandi/**
3f62ea8a1Sandi * DokuWiki media passthrough file
4f62ea8a1Sandi *
5f62ea8a1Sandi * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6f62ea8a1Sandi * @author     Andreas Gohr <andi@splitbrain.org>
7f62ea8a1Sandi */
8f62ea8a1Sandi
9b1a72e3dSmatthiasgrimm  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
10f62ea8a1Sandi  require_once(DOKU_INC.'inc/init.php');
11f62ea8a1Sandi  require_once(DOKU_INC.'inc/common.php');
12f62ea8a1Sandi  require_once(DOKU_INC.'inc/pageutils.php');
13f62ea8a1Sandi  require_once(DOKU_INC.'inc/confutils.php');
14f62ea8a1Sandi  require_once(DOKU_INC.'inc/auth.php');
158746e727Sandi  //close sesseion
168746e727Sandi  session_write_close();
178746e727Sandi
18f62ea8a1Sandi  $mimetypes = getMimeTypes();
19f62ea8a1Sandi
20f62ea8a1Sandi  //get input
2142905504SAndreas Gohr  $MEDIA  = getID('media',false); // no cleaning - maybe external
22f62ea8a1Sandi  $CACHE  = calc_cache($_REQUEST['cache']);
23f62ea8a1Sandi  $WIDTH  = $_REQUEST['w'];
24f62ea8a1Sandi  $HEIGHT = $_REQUEST['h'];
25f62ea8a1Sandi  list($EXT,$MIME) = mimetype($MEDIA);
26f62ea8a1Sandi  if($EXT === false){
27f62ea8a1Sandi    $EXT  = 'unknown';
28f62ea8a1Sandi    $MIME = 'application/octet-stream';
29f62ea8a1Sandi  }
30f62ea8a1Sandi
31f62ea8a1Sandi  //media to local file
32f62ea8a1Sandi  if(preg_match('#^(https?|ftp)://#i',$MEDIA)){
33f62ea8a1Sandi    //handle external media
34f62ea8a1Sandi    $FILE = get_from_URL($MEDIA,$EXT,$CACHE);
35f62ea8a1Sandi    if(!$FILE){
36f62ea8a1Sandi      //download failed - redirect to original URL
37f62ea8a1Sandi      header('Location: '.$MEDIA);
38f62ea8a1Sandi      exit;
39f62ea8a1Sandi    }
40f62ea8a1Sandi  }else{
41f62ea8a1Sandi    $MEDIA = cleanID($MEDIA);
42f62ea8a1Sandi    if(empty($MEDIA)){
43f62ea8a1Sandi      header("HTTP/1.0 400 Bad Request");
44f62ea8a1Sandi      print 'Bad request';
45f62ea8a1Sandi      exit;
46f62ea8a1Sandi    }
47f62ea8a1Sandi
48f62ea8a1Sandi    //check permissions (namespace only)
49f62ea8a1Sandi    if(auth_quickaclcheck(getNS($MEDIA).':X') < AUTH_READ){
50f62ea8a1Sandi      header("HTTP/1.0 401 Unauthorized");
51f62ea8a1Sandi      //fixme add some image for imagefiles
52f62ea8a1Sandi      print 'Unauthorized';
53f62ea8a1Sandi      exit;
54f62ea8a1Sandi    }
55f62ea8a1Sandi    $FILE  = mediaFN($MEDIA);
56f62ea8a1Sandi  }
57f62ea8a1Sandi
58f62ea8a1Sandi  //check file existance
59f62ea8a1Sandi  if(!@file_exists($FILE)){
60f62ea8a1Sandi    header("HTTP/1.0 404 Not Found");
61f62ea8a1Sandi    //FIXME add some default broken image
62f62ea8a1Sandi    print 'Not Found';
63f62ea8a1Sandi    exit;
64f62ea8a1Sandi  }
65f62ea8a1Sandi
66f62ea8a1Sandi  //handle image resizing
67f62ea8a1Sandi  if((substr($MIME,0,5) == 'image') && $WIDTH){
68f62ea8a1Sandi    $FILE = get_resized($FILE,$EXT,$WIDTH,$HEIGHT);
69f62ea8a1Sandi  }
70f62ea8a1Sandi
71f62ea8a1Sandi
72f62ea8a1Sandi  //FIXME set sane cachecontrol headers
73f62ea8a1Sandi  //FIXME handle conditional and partial requests
74f62ea8a1Sandi
75f62ea8a1Sandi  //send file
76f62ea8a1Sandi  header("Content-Type: $MIME");
77f62ea8a1Sandi  header('Last-Modified: '.date('r',filemtime($FILE)));
78f62ea8a1Sandi  header('Content-Length: '.filesize($FILE));
794d5714c4SAndreas Gohr  header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0');
80f62ea8a1Sandi
81f62ea8a1Sandi  //application mime type is downloadable
82f62ea8a1Sandi  if(substr($MIME,0,11) == 'application'){
83f62ea8a1Sandi    header('Content-Disposition: attachment; filename="'.basename($FILE).'"');
84f62ea8a1Sandi  }
85f62ea8a1Sandi
86f62ea8a1Sandi  $fp = @fopen($FILE,"rb");
87f62ea8a1Sandi  if($fp){
88615a21edSBrian Cowan    while (!feof($fp)) {
89615a21edSBrian Cowan      @set_time_limit(); // large files can take a lot of time
90615a21edSBrian Cowan      print fread($fp, 16*1024);
91615a21edSBrian Cowan      flush();
92615a21edSBrian Cowan    }
93615a21edSBrian Cowan    fclose($fp);
94f62ea8a1Sandi  }else{
95f62ea8a1Sandi    header("HTTP/1.0 500 Internal Server Error");
96f62ea8a1Sandi    print "Could not read $FILE - bad permissions?";
97f62ea8a1Sandi  }
98f62ea8a1Sandi
99f62ea8a1Sandi/* ------------------------------------------------------------------------ */
100f62ea8a1Sandi
101f62ea8a1Sandi/**
102f62ea8a1Sandi * Resizes the given image to the given size
103f62ea8a1Sandi *
104f62ea8a1Sandi * @author  Andreas Gohr <andi@splitbrain.org>
105f62ea8a1Sandi */
106f62ea8a1Sandifunction get_resized($file, $ext, $w, $h=0){
107f62ea8a1Sandi  global $conf;
108f62ea8a1Sandi
109f62ea8a1Sandi  $info  = getimagesize($file);
110f62ea8a1Sandi  if(!$h) $h = round(($w * $info[1]) / $info[0]);
111f62ea8a1Sandi
112f62ea8a1Sandi
113f62ea8a1Sandi  //cache
11498407a7aSandi  $local = getCacheName($file,'.media.'.$w.'x'.$h.'.'.$ext);
115f62ea8a1Sandi  $mtime = @filemtime($local); // 0 if not exists
116f62ea8a1Sandi
11768375754SPavel Vitis  if( $mtime > filemtime($file) ||
11868375754SPavel Vitis      resize_imageIM($ext,$file,$info[0],$info[1],$local,$w,$h) ||
11968375754SPavel Vitis      resize_imageGD($ext,$file,$info[0],$info[1],$local,$w,$h) ){
120f62ea8a1Sandi    return $local;
121f62ea8a1Sandi  }
122f62ea8a1Sandi  //still here? resizing failed
123f62ea8a1Sandi  return $file;
124f62ea8a1Sandi}
125f62ea8a1Sandi
126f62ea8a1Sandi/**
127f62ea8a1Sandi * Returns the wanted cachetime in seconds
128f62ea8a1Sandi *
129f62ea8a1Sandi * Resolves named constants
130f62ea8a1Sandi *
131f62ea8a1Sandi * @author  Andreas Gohr <andi@splitbrain.org>
132f62ea8a1Sandi */
133f62ea8a1Sandifunction calc_cache($cache){
134f62ea8a1Sandi  global $conf;
135f62ea8a1Sandi
136f62ea8a1Sandi  if(strtolower($cache) == 'nocache') return 0; //never cache
137f62ea8a1Sandi  if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache
138f62ea8a1Sandi  return -1; //cache endless
139f62ea8a1Sandi}
140f62ea8a1Sandi
141f62ea8a1Sandi/**
142f62ea8a1Sandi * Download a remote file and return local filename
143f62ea8a1Sandi *
144f62ea8a1Sandi * returns false if download fails. Uses cached file if available and
145f62ea8a1Sandi * wanted
146f62ea8a1Sandi *
147f62ea8a1Sandi * @author  Andreas Gohr <andi@splitbrain.org>
14868375754SPavel Vitis * @author  Pavel Vitis <Pavel.Vitis@seznam.cz>
149f62ea8a1Sandi */
150f62ea8a1Sandifunction get_from_URL($url,$ext,$cache){
151f62ea8a1Sandi  global $conf;
152f62ea8a1Sandi
15368375754SPavel Vitis  $local = getCacheName(strtolower($url),".media.$ext");
154f62ea8a1Sandi  $mtime = @filemtime($local); // 0 if not exists
155f62ea8a1Sandi
156f62ea8a1Sandi  //decide if download needed:
15768375754SPavel Vitis  if( $cache == 0 ||                             // never cache
15868375754SPavel Vitis      ($mtime != 0 && $cache != -1) ||           // exists but no endless cache
15968375754SPavel Vitis      ($mtime == 0) ||                           // not exists
16068375754SPavel Vitis      ($cache != -1 && $mtime < time()-$cache)   // expired
16168375754SPavel Vitis    ){
162f62ea8a1Sandi      if(io_download($url,$local)){
163f62ea8a1Sandi        return $local;
164f62ea8a1Sandi      }else{
165f62ea8a1Sandi        return false;
166f62ea8a1Sandi      }
167f62ea8a1Sandi  }
168f62ea8a1Sandi
169f62ea8a1Sandi  //if cache exists use it else
170f62ea8a1Sandi  if($mtime) return $local;
171f62ea8a1Sandi
172f62ea8a1Sandi  //else return false
173f62ea8a1Sandi  return false;
174f62ea8a1Sandi}
175f62ea8a1Sandi
176f62ea8a1Sandi/**
17768375754SPavel Vitis * resize images using external ImageMagick convert program
17868375754SPavel Vitis *
17968375754SPavel Vitis * @author Pavel Vitis <Pavel.Vitis@seznam.cz>
18068375754SPavel Vitis * @author Andreas Gohr <andi@splitbrain.org>
18168375754SPavel Vitis */
18268375754SPavel Vitisfunction resize_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
18368375754SPavel Vitis  global $conf;
18468375754SPavel Vitis
1857bc7a78eSAndreas Gohr  // check if convert is configured
1867bc7a78eSAndreas Gohr  if(!$conf['im_convert']) return false;
18768375754SPavel Vitis
18868375754SPavel Vitis  // prepare command
18968375754SPavel Vitis  $cmd  = $conf['im_convert'];
19068375754SPavel Vitis  $cmd .= ' -resize '.$to_w.'x'.$to_h.'!';
19168375754SPavel Vitis  $cmd .= " $from $to";
19268375754SPavel Vitis
19368375754SPavel Vitis  @exec($cmd,$out,$retval);
19468375754SPavel Vitis  if ($retval == 0) return true;
19568375754SPavel Vitis
19668375754SPavel Vitis  return false;
19768375754SPavel Vitis}
19868375754SPavel Vitis
19968375754SPavel Vitis/**
20068375754SPavel Vitis * resize images using PHP's libGD support
201f62ea8a1Sandi *
202f62ea8a1Sandi * @author Andreas Gohr <andi@splitbrain.org>
203f62ea8a1Sandi */
20468375754SPavel Vitisfunction resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
205f62ea8a1Sandi  global $conf;
206f62ea8a1Sandi
207f62ea8a1Sandi  if($conf['gdlib'] < 1) return false; //no GDlib available or wanted
208f62ea8a1Sandi
209*4e406776SAndreas Gohr  // check available memory
210*4e406776SAndreas Gohr  if(!is_mem_available(($from_w * $from_h * 4) + ($to_w * $to_h * 4))){
211*4e406776SAndreas Gohr    return false;
212*4e406776SAndreas Gohr  }
213*4e406776SAndreas Gohr
214f62ea8a1Sandi  // create an image of the given filetype
215f62ea8a1Sandi  if ($ext == 'jpg' || $ext == 'jpeg'){
216f62ea8a1Sandi    if(!function_exists("imagecreatefromjpeg")) return false;
217f62ea8a1Sandi    $image = @imagecreatefromjpeg($from);
218f62ea8a1Sandi  }elseif($ext == 'png') {
219f62ea8a1Sandi    if(!function_exists("imagecreatefrompng")) return false;
220f62ea8a1Sandi    $image = @imagecreatefrompng($from);
221f62ea8a1Sandi
222f62ea8a1Sandi  }elseif($ext == 'gif') {
223f62ea8a1Sandi    if(!function_exists("imagecreatefromgif")) return false;
224f62ea8a1Sandi    $image = @imagecreatefromgif($from);
225f62ea8a1Sandi  }
226f62ea8a1Sandi  if(!$image) return false;
227f62ea8a1Sandi
228f62ea8a1Sandi  if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor")){
229f62ea8a1Sandi    $newimg = @imagecreatetruecolor ($to_w, $to_h);
230f62ea8a1Sandi  }
231f62ea8a1Sandi  if(!$newimg) $newimg = @imagecreate($to_w, $to_h);
232dd7bbbf4SAndreas Gohr  if(!$newimg){
233dd7bbbf4SAndreas Gohr    imagedestroy($image);
234dd7bbbf4SAndreas Gohr    return false;
235dd7bbbf4SAndreas Gohr  }
236f62ea8a1Sandi
237f62ea8a1Sandi  //keep png alpha channel if possible
238f62ea8a1Sandi  if($ext == 'png' && $conf['gdlib']>1 && function_exists('imagesavealpha')){
239f62ea8a1Sandi    imagealphablending($newimg, false);
240f62ea8a1Sandi    imagesavealpha($newimg,true);
241f62ea8a1Sandi  }
242f62ea8a1Sandi
243f62ea8a1Sandi  //try resampling first
244f62ea8a1Sandi  if(function_exists("imagecopyresampled")){
245f62ea8a1Sandi    if(!@imagecopyresampled($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h)) {
246f62ea8a1Sandi      imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
247f62ea8a1Sandi    }
248f62ea8a1Sandi  }else{
249f62ea8a1Sandi    imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
250f62ea8a1Sandi  }
251f62ea8a1Sandi
252dd7bbbf4SAndreas Gohr  $okay = false;
253f62ea8a1Sandi  if ($ext == 'jpg' || $ext == 'jpeg'){
254dd7bbbf4SAndreas Gohr    if(!function_exists('imagejpeg')){
255dd7bbbf4SAndreas Gohr      $okay = false;
256dd7bbbf4SAndreas Gohr    }else{
257dd7bbbf4SAndreas Gohr      $okay = imagejpeg($newimg, $to, 70);
258dd7bbbf4SAndreas Gohr    }
259f62ea8a1Sandi  }elseif($ext == 'png') {
260dd7bbbf4SAndreas Gohr    if(!function_exists('imagepng')){
261dd7bbbf4SAndreas Gohr      $okay = false;
262dd7bbbf4SAndreas Gohr    }else{
263dd7bbbf4SAndreas Gohr      $okay =  imagepng($newimg, $to);
264dd7bbbf4SAndreas Gohr    }
265f62ea8a1Sandi  }elseif($ext == 'gif') {
266dd7bbbf4SAndreas Gohr    if(!function_exists('imagegif')){
267dd7bbbf4SAndreas Gohr      $okay = false;
268dd7bbbf4SAndreas Gohr    }else{
269dd7bbbf4SAndreas Gohr      $okay = imagegif($newimg, $to);
270dd7bbbf4SAndreas Gohr    }
271f62ea8a1Sandi  }
272f62ea8a1Sandi
273dd7bbbf4SAndreas Gohr  // destroy GD image ressources
274dd7bbbf4SAndreas Gohr  if($image) imagedestroy($image);
275dd7bbbf4SAndreas Gohr  if($newimg) imagedestroy($newimg);
276dd7bbbf4SAndreas Gohr
277dd7bbbf4SAndreas Gohr  return $okay;
278f62ea8a1Sandi}
279f62ea8a1Sandi
280*4e406776SAndreas Gohr/**
281*4e406776SAndreas Gohr * Checks if the given amount of memory is available
282*4e406776SAndreas Gohr *
283*4e406776SAndreas Gohr * If the memory_get_usage() function is not available the
284*4e406776SAndreas Gohr * function just assumes $used bytes of already allocated memory
285*4e406776SAndreas Gohr *
286*4e406776SAndreas Gohr * @param  int $mem  Size of memory you want to allocate in bytes
287*4e406776SAndreas Gohr * @param  int $used already allocated memory (see above)
288*4e406776SAndreas Gohr * @author Filip Oscadal <webmaster@illusionsoftworks.cz>
289*4e406776SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
290*4e406776SAndreas Gohr */
291*4e406776SAndreas Gohrfunction is_mem_available($mem,$bytes=1048576){
292*4e406776SAndreas Gohr  $limit = trim(ini_get('memory_limit'));
293*4e406776SAndreas Gohr  if(empty($limit)) return true; // no limit set!
294*4e406776SAndreas Gohr
295*4e406776SAndreas Gohr  // parse limit to bytes
296*4e406776SAndreas Gohr  $unit = strtolower(substr($limit,-1));
297*4e406776SAndreas Gohr  switch($unit){
298*4e406776SAndreas Gohr    case 'g':
299*4e406776SAndreas Gohr      $limit = substr($limit,0,-1);
300*4e406776SAndreas Gohr      $limit *= 1024*1024*1024;
301*4e406776SAndreas Gohr      break;
302*4e406776SAndreas Gohr    case 'm':
303*4e406776SAndreas Gohr      $limit = substr($limit,0,-1);
304*4e406776SAndreas Gohr      $limit *= 1024*1024;
305*4e406776SAndreas Gohr      break;
306*4e406776SAndreas Gohr    case 'k':
307*4e406776SAndreas Gohr      $limit = substr($limit,0,-1);
308*4e406776SAndreas Gohr      $limit *= 1024;
309*4e406776SAndreas Gohr      break;
310*4e406776SAndreas Gohr  }
311*4e406776SAndreas Gohr
312*4e406776SAndreas Gohr  // get used memory if possible
313*4e406776SAndreas Gohr  if(function_exists('memory_get_usage')){
314*4e406776SAndreas Gohr    $used = memory_get_usage();
315*4e406776SAndreas Gohr  }
316*4e406776SAndreas Gohr
317*4e406776SAndreas Gohr
318*4e406776SAndreas Gohr  if($used+$mem > $limit){
319*4e406776SAndreas Gohr    return false;
320*4e406776SAndreas Gohr  }
321*4e406776SAndreas Gohr
322*4e406776SAndreas Gohr  return true;
323*4e406776SAndreas Gohr}
324f62ea8a1Sandi
325f62ea8a1Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
326f62ea8a1Sandi?>
327