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