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