xref: /dokuwiki/lib/exe/fetch.php (revision 20e29859d7134c28e50a97c828f78aa7fe719352)
1<?php
2/**
3 * DokuWiki media passthrough file
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 */
8
9  if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../');
10  define('DOKU_DISABLE_GZIP_OUTPUT', 1);
11  require_once(DOKU_INC.'inc/init.php');
12  require_once(DOKU_INC.'inc/common.php');
13  require_once(DOKU_INC.'inc/pageutils.php');
14  require_once(DOKU_INC.'inc/confutils.php');
15  require_once(DOKU_INC.'inc/auth.php');
16  //close sesseion
17  session_write_close();
18  if(!defined('CHUNK_SIZE')) define('CHUNK_SIZE',16*1024);
19
20  $mimetypes = getMimeTypes();
21
22  //get input
23  $MEDIA  = stripctl(getID('media',false)); // no cleaning except control chars - maybe external
24  $CACHE  = calc_cache($_REQUEST['cache']);
25  $WIDTH  = (int) $_REQUEST['w'];
26  $HEIGHT = (int) $_REQUEST['h'];
27  list($EXT,$MIME) = mimetype($MEDIA);
28  if($EXT === false){
29    $EXT  = 'unknown';
30    $MIME = 'application/octet-stream';
31  }
32
33  //media to local file
34  if(preg_match('#^(https?)://#i',$MEDIA)){
35    //handle external images
36    if(strncmp($MIME,'image/',6) == 0) $FILE = get_from_URL($MEDIA,$EXT,$CACHE);
37    if(!$FILE){
38      //download failed - redirect to original URL
39      header('Location: '.$MEDIA);
40      exit;
41    }
42  }else{
43    $MEDIA = cleanID($MEDIA);
44    if(empty($MEDIA)){
45      header("HTTP/1.0 400 Bad Request");
46      print 'Bad request';
47      exit;
48    }
49
50    //check permissions (namespace only)
51    if(auth_quickaclcheck(getNS($MEDIA).':X') < AUTH_READ){
52      header("HTTP/1.0 401 Unauthorized");
53      //fixme add some image for imagefiles
54      print 'Unauthorized';
55      exit;
56    }
57    $FILE  = mediaFN($MEDIA);
58  }
59
60  //check file existance
61  if(!@file_exists($FILE)){
62    header("HTTP/1.0 404 Not Found");
63    //FIXME add some default broken image
64    print 'Not Found';
65    exit;
66  }
67
68  $ORIG = $FILE;
69
70  //handle image resizing/cropping
71  if((substr($MIME,0,5) == 'image') && $WIDTH){
72    if($HEIGHT){
73        $FILE = get_cropped($FILE,$EXT,$WIDTH,$HEIGHT);
74    }else{
75        $FILE = get_resized($FILE,$EXT,$WIDTH,$HEIGHT);
76    }
77  }
78
79  // finally send the file to the client
80  $data = array('file'   => $FILE,
81                'mime'   => $MIME,
82                'cache'  => $CACHE,
83                'orig'   => $ORIG,
84                'ext'    => $EXT,
85                'width'  => $WIDTH,
86                'height' => $HEIGHT);
87
88  $evt = new Doku_Event('MEDIA_SENDFILE', $data);
89  if ($evt->advise_before()) {
90    sendFile($data['file'],$data['mime'],$data['cache']);
91  }
92
93/* ------------------------------------------------------------------------ */
94
95/**
96 * Set headers and send the file to the client
97 *
98 * @author Andreas Gohr <andi@splitbrain.org>
99 * @author Ben Coburn <btcoburn@silicodon.net>
100 */
101function sendFile($file,$mime,$cache){
102  global $conf;
103  $fmtime = @filemtime($file);
104  // send headers
105  header("Content-Type: $mime");
106  // smart http caching headers
107  if ($cache==-1) {
108    // cache
109    // cachetime or one hour
110    header('Expires: '.gmdate("D, d M Y H:i:s", time()+max($conf['cachetime'], 3600)).' GMT');
111    header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($conf['cachetime'], 3600));
112    header('Pragma: public');
113  } else if ($cache>0) {
114    // recache
115    // remaining cachetime + 10 seconds so the newly recached media is used
116    header('Expires: '.gmdate("D, d M Y H:i:s", $fmtime+$conf['cachetime']+10).' GMT');
117    header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($fmtime-time()+$conf['cachetime']+10, 0));
118    header('Pragma: public');
119  } else if ($cache==0) {
120    // nocache
121    header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
122    header('Pragma: public');
123  }
124  //send important headers first, script stops here if '304 Not Modified' response
125  http_conditionalRequest($fmtime);
126
127
128  //application mime type is downloadable
129  if(substr($mime,0,11) == 'application'){
130    header('Content-Disposition: attachment; filename="'.basename($file).'";');
131  }
132
133  //use x-sendfile header to pass the delivery to compatible webservers
134  if($conf['xsendfile'] == 1){
135    header("X-LIGHTTPD-send-file: $file");
136    exit;
137  }elseif($conf['xsendfile'] == 2){
138    header("X-Sendfile: $file");
139    exit;
140  }elseif($conf['xsendfile'] == 3){
141    header("X-Accel-Redirect: $file");
142    exit;
143  }
144
145  //support download continueing
146  header('Accept-Ranges: bytes');
147  list($start,$len) = http_rangeRequest(filesize($file));
148
149  // send file contents
150  $fp = @fopen($file,"rb");
151  if($fp){
152    fseek($fp,$start); //seek to start of range
153
154    $chunk = ($len > CHUNK_SIZE) ? CHUNK_SIZE : $len;
155    while (!feof($fp) && $chunk > 0) {
156      @set_time_limit(30); // large files can take a lot of time
157      print fread($fp, $chunk);
158      flush();
159      $len -= $chunk;
160      $chunk = ($len > CHUNK_SIZE) ? CHUNK_SIZE : $len;
161    }
162    fclose($fp);
163  }else{
164    header("HTTP/1.0 500 Internal Server Error");
165    print "Could not read $file - bad permissions?";
166  }
167}
168
169/**
170 * Checks and sets headers to handle range requets
171 *
172 * @author  Andreas Gohr <andi@splitbrain.org>
173 * @returns array The start byte and the amount of bytes to send
174 */
175function http_rangeRequest($size){
176  if(!isset($_SERVER['HTTP_RANGE'])){
177    // no range requested - send the whole file
178    header("Content-Length: $size");
179    return array(0,$size);
180  }
181
182  $t = explode('=', $_SERVER['HTTP_RANGE']);
183  if (!$t[0]=='bytes') {
184    // we only understand byte ranges - send the whole file
185    header("Content-Length: $size");
186    return array(0,$size);
187  }
188
189  $r = explode('-', $t[1]);
190  $start = (int)$r[0];
191  $end = (int)$r[1];
192  if (!$end) $end = $size - 1;
193  if ($start > $end || $start > $size || $end > $size){
194    header('HTTP/1.1 416 Requested Range Not Satisfiable');
195    print 'Bad Range Request!';
196    exit;
197  }
198
199  $tot = $end - $start + 1;
200  header('HTTP/1.1 206 Partial Content');
201  header("Content-Range: bytes {$start}-{$end}/{$size}");
202  header("Content-Length: $tot");
203
204  return array($start,$tot);
205}
206
207/**
208 * Resizes the given image to the given size
209 *
210 * @author  Andreas Gohr <andi@splitbrain.org>
211 */
212function get_resized($file, $ext, $w, $h=0){
213  global $conf;
214
215  $info = @getimagesize($file); //get original size
216  if($info == false) return $file; // that's no image - it's a spaceship!
217
218  if(!$h) $h = round(($w * $info[1]) / $info[0]);
219
220  // we wont scale up to infinity
221  if($w > 2000 || $h > 2000) return $file;
222
223  //cache
224  $local = getCacheName($file,'.media.'.$w.'x'.$h.'.'.$ext);
225  $mtime = @filemtime($local); // 0 if not exists
226
227  if( $mtime > filemtime($file) ||
228      resize_imageIM($ext,$file,$info[0],$info[1],$local,$w,$h) ||
229      resize_imageGD($ext,$file,$info[0],$info[1],$local,$w,$h) ){
230    if($conf['fperm']) chmod($local, $conf['fperm']);
231    return $local;
232  }
233  //still here? resizing failed
234  return $file;
235}
236
237/**
238 * Crops the given image to the wanted ratio, then calls get_resized to scale it
239 * to the wanted size
240 *
241 * Crops are centered horizontally but prefer the upper third of an vertical
242 * image because most pics are more interesting in that area (rule of thirds)
243 *
244 * @author  Andreas Gohr <andi@splitbrain.org>
245 */
246function get_cropped($file, $ext, $w, $h=0){
247  global $conf;
248
249  if(!$h) $h = $w;
250  $info = @getimagesize($file); //get original size
251  if($info == false) return $file; // that's no image - it's a spaceship!
252
253  // calculate crop size
254  $fr = $info[0]/$info[1];
255  $tr = $w/$h;
256  if($tr >= 1){
257    if($tr > $fr){
258        $cw = $info[0];
259        $ch = (int) $info[0]/$tr;
260    }else{
261        $cw = (int) $info[1]*$tr;
262        $ch = $info[1];
263    }
264  }else{
265    if($tr < $fr){
266        $cw = (int) $info[1]*$tr;
267        $ch = $info[1];
268    }else{
269        $cw = $info[0];
270        $ch = (int) $info[0]/$tr;
271    }
272  }
273  // calculate crop offset
274  $cx = (int) ($info[0]-$cw)/2;
275  $cy = (int) ($info[1]-$ch)/3;
276
277  //cache
278  $local = getCacheName($file,'.media.'.$cw.'x'.$ch.'.crop.'.$ext);
279  $mtime = @filemtime($local); // 0 if not exists
280
281  if( $mtime > filemtime($file) ||
282      crop_imageIM($ext,$file,$info[0],$info[1],$local,$cw,$ch,$cx,$cy) ||
283      resize_imageGD($ext,$file,$cw,$ch,$local,$cw,$ch,$cx,$cy) ){
284    if($conf['fperm']) chmod($local, $conf['fperm']);
285    return get_resized($local,$ext, $w, $h);
286  }
287
288  //still here? cropping failed
289  return get_resized($file,$ext, $w, $h);
290}
291
292
293/**
294 * Returns the wanted cachetime in seconds
295 *
296 * Resolves named constants
297 *
298 * @author  Andreas Gohr <andi@splitbrain.org>
299 */
300function calc_cache($cache){
301  global $conf;
302
303  if(strtolower($cache) == 'nocache') return 0; //never cache
304  if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache
305  return -1; //cache endless
306}
307
308/**
309 * Download a remote file and return local filename
310 *
311 * returns false if download fails. Uses cached file if available and
312 * wanted
313 *
314 * @author  Andreas Gohr <andi@splitbrain.org>
315 * @author  Pavel Vitis <Pavel.Vitis@seznam.cz>
316 */
317function get_from_URL($url,$ext,$cache){
318  global $conf;
319
320  // if no cache or fetchsize just redirect
321  if ($cache==0)           return false;
322  if (!$conf['fetchsize']) return false;
323
324  $local = getCacheName(strtolower($url),".media.$ext");
325  $mtime = @filemtime($local); // 0 if not exists
326
327  //decide if download needed:
328  if( ($mtime == 0) ||                           // cache does not exist
329      ($cache != -1 && $mtime < time()-$cache)   // 'recache' and cache has expired
330    ){
331      if(image_download($url,$local)){
332        return $local;
333      }else{
334        return false;
335      }
336  }
337
338  //if cache exists use it else
339  if($mtime) return $local;
340
341  //else return false
342  return false;
343}
344
345/**
346 * Download image files
347 *
348 * @author Andreas Gohr <andi@splitbrain.org>
349 */
350function image_download($url,$file){
351  global $conf;
352  $http = new DokuHTTPClient();
353  $http->max_bodysize = $conf['fetchsize'];
354  $http->timeout = 25; //max. 25 sec
355  $http->header_regexp = '!\r\nContent-Type: image/(jpe?g|gif|png)!i';
356
357  $data = $http->get($url);
358  if(!$data) return false;
359
360  $fileexists = @file_exists($file);
361  $fp = @fopen($file,"w");
362  if(!$fp) return false;
363  fwrite($fp,$data);
364  fclose($fp);
365  if(!$fileexists and $conf['fperm']) chmod($file, $conf['fperm']);
366
367  // check if it is really an image
368  $info = @getimagesize($file);
369  if(!$info){
370    @unlink($file);
371    return false;
372  }
373
374  return true;
375}
376
377/**
378 * resize images using external ImageMagick convert program
379 *
380 * @author Pavel Vitis <Pavel.Vitis@seznam.cz>
381 * @author Andreas Gohr <andi@splitbrain.org>
382 */
383function resize_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
384  global $conf;
385
386  // check if convert is configured
387  if(!$conf['im_convert']) return false;
388
389  // prepare command
390  $cmd  = $conf['im_convert'];
391  $cmd .= ' -resize '.$to_w.'x'.$to_h.'!';
392  if ($ext == 'jpg' || $ext == 'jpeg') {
393      $cmd .= ' -quality '.$conf['jpg_quality'];
394  }
395  $cmd .= " $from $to";
396
397  @exec($cmd,$out,$retval);
398  if ($retval == 0) return true;
399  return false;
400}
401
402/**
403 * crop images using external ImageMagick convert program
404 *
405 * @author Andreas Gohr <andi@splitbrain.org>
406 */
407function crop_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x,$ofs_y){
408  global $conf;
409
410  // check if convert is configured
411  if(!$conf['im_convert']) return false;
412
413  // prepare command
414  $cmd  = $conf['im_convert'];
415  $cmd .= ' -crop '.$to_w.'x'.$to_h.'+'.$ofs_x.'+'.$ofs_y;
416  if ($ext == 'jpg' || $ext == 'jpeg') {
417      $cmd .= ' -quality '.$conf['jpg_quality'];
418  }
419  $cmd .= " $from $to";
420
421  @exec($cmd,$out,$retval);
422  if ($retval == 0) return true;
423  return false;
424}
425
426/**
427 * resize or crop images using PHP's libGD support
428 *
429 * @author Andreas Gohr <andi@splitbrain.org>
430 * @author Sebastian Wienecke <s_wienecke@web.de>
431 */
432function resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h,$ofs_x=0,$ofs_y=0){
433  global $conf;
434
435  if($conf['gdlib'] < 1) return false; //no GDlib available or wanted
436
437  // check available memory
438  if(!is_mem_available(($from_w * $from_h * 4) + ($to_w * $to_h * 4))){
439    return false;
440  }
441
442  // create an image of the given filetype
443  if ($ext == 'jpg' || $ext == 'jpeg'){
444    if(!function_exists("imagecreatefromjpeg")) return false;
445    $image = @imagecreatefromjpeg($from);
446  }elseif($ext == 'png') {
447    if(!function_exists("imagecreatefrompng")) return false;
448    $image = @imagecreatefrompng($from);
449
450  }elseif($ext == 'gif') {
451    if(!function_exists("imagecreatefromgif")) return false;
452    $image = @imagecreatefromgif($from);
453  }
454  if(!$image) return false;
455
456  if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor") && $ext != 'gif'){
457    $newimg = @imagecreatetruecolor ($to_w, $to_h);
458  }
459  if(!$newimg) $newimg = @imagecreate($to_w, $to_h);
460  if(!$newimg){
461    imagedestroy($image);
462    return false;
463  }
464
465  //keep png alpha channel if possible
466  if($ext == 'png' && $conf['gdlib']>1 && function_exists('imagesavealpha')){
467    imagealphablending($newimg, false);
468    imagesavealpha($newimg,true);
469  }
470
471  //keep gif transparent color if possible
472  if($ext == 'gif' && function_exists('imagefill') && function_exists('imagecolorallocate')) {
473    if(function_exists('imagecolorsforindex') && function_exists('imagecolortransparent')) {
474      $transcolorindex = @imagecolortransparent($image);
475      if($transcolorindex >= 0 ) { //transparent color exists
476        $transcolor = @imagecolorsforindex($image, $transcolorindex);
477        $transcolorindex = @imagecolorallocate($newimg, $transcolor['red'], $transcolor['green'], $transcolor['blue']);
478        @imagefill($newimg, 0, 0, $transcolorindex);
479        @imagecolortransparent($newimg, $transcolorindex);
480      }else{ //filling with white
481        $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255);
482        @imagefill($newimg, 0, 0, $whitecolorindex);
483      }
484    }else{ //filling with white
485      $whitecolorindex = @imagecolorallocate($newimg, 255, 255, 255);
486      @imagefill($newimg, 0, 0, $whitecolorindex);
487    }
488  }
489
490  //try resampling first
491  if(function_exists("imagecopyresampled")){
492    if(!@imagecopyresampled($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h)) {
493      imagecopyresized($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h);
494    }
495  }else{
496    imagecopyresized($newimg, $image, 0, 0, $ofs_x, $ofs_y, $to_w, $to_h, $from_w, $from_h);
497  }
498
499  $okay = false;
500  if ($ext == 'jpg' || $ext == 'jpeg'){
501    if(!function_exists('imagejpeg')){
502      $okay = false;
503    }else{
504      $okay = imagejpeg($newimg, $to, $conf['jpg_quality']);
505    }
506  }elseif($ext == 'png') {
507    if(!function_exists('imagepng')){
508      $okay = false;
509    }else{
510      $okay =  imagepng($newimg, $to);
511    }
512  }elseif($ext == 'gif') {
513    if(!function_exists('imagegif')){
514      $okay = false;
515    }else{
516      $okay = imagegif($newimg, $to);
517    }
518  }
519
520  // destroy GD image ressources
521  if($image) imagedestroy($image);
522  if($newimg) imagedestroy($newimg);
523
524  return $okay;
525}
526
527/**
528 * Checks if the given amount of memory is available
529 *
530 * If the memory_get_usage() function is not available the
531 * function just assumes $bytes of already allocated memory
532 *
533 * @param  int $mem  Size of memory you want to allocate in bytes
534 * @param  int $used already allocated memory (see above)
535 * @author Filip Oscadal <webmaster@illusionsoftworks.cz>
536 * @author Andreas Gohr <andi@splitbrain.org>
537 */
538function is_mem_available($mem,$bytes=1048576){
539  $limit = trim(ini_get('memory_limit'));
540  if(empty($limit)) return true; // no limit set!
541
542  // parse limit to bytes
543  $limit = php_to_byte($limit);
544
545  // get used memory if possible
546  if(function_exists('memory_get_usage')){
547    $used = memory_get_usage();
548  }
549
550  if($used+$mem > $limit){
551    return false;
552  }
553
554  return true;
555}
556
557//Setup VIM: ex: et ts=2 enc=utf-8 :
558?>
559