xref: /dokuwiki/lib/exe/fetch.php (revision d88b5f049b763613b6c2953b8f21105639a53afc)
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',realpath(dirname(__FILE__).'/../../').'/');
10  require_once(DOKU_INC.'inc/init.php');
11  require_once(DOKU_INC.'inc/common.php');
12  require_once(DOKU_INC.'inc/pageutils.php');
13  require_once(DOKU_INC.'inc/confutils.php');
14  require_once(DOKU_INC.'inc/auth.php');
15  //close sesseion
16  session_write_close();
17  if(!defined('CHUNK_SIZE')) define('CHUNK_SIZE',16*1024);
18
19  $mimetypes = getMimeTypes();
20
21  //get input
22  $MEDIA  = getID('media',false); // no cleaning - maybe external
23  $CACHE  = calc_cache($_REQUEST['cache']);
24  $WIDTH  = $_REQUEST['w'];
25  $HEIGHT = $_REQUEST['h'];
26  list($EXT,$MIME) = mimetype($MEDIA);
27  if($EXT === false){
28    $EXT  = 'unknown';
29    $MIME = 'application/octet-stream';
30  }
31
32  //media to local file
33  if(preg_match('#^(https?|ftp)://#i',$MEDIA)){
34    //handle external media
35    $FILE = get_from_URL($MEDIA,$EXT,$CACHE);
36    if(!$FILE){
37      //download failed - redirect to original URL
38      header('Location: '.$MEDIA);
39      exit;
40    }
41  }else{
42    $MEDIA = cleanID($MEDIA);
43    if(empty($MEDIA)){
44      header("HTTP/1.0 400 Bad Request");
45      print 'Bad request';
46      exit;
47    }
48
49    //check permissions (namespace only)
50    if(auth_quickaclcheck(getNS($MEDIA).':X') < AUTH_READ){
51      header("HTTP/1.0 401 Unauthorized");
52      //fixme add some image for imagefiles
53      print 'Unauthorized';
54      exit;
55    }
56    $FILE  = mediaFN($MEDIA);
57  }
58
59  //check file existance
60  if(!@file_exists($FILE)){
61    header("HTTP/1.0 404 Not Found");
62    //FIXME add some default broken image
63    print 'Not Found';
64    exit;
65  }
66
67  //handle image resizing
68  if((substr($MIME,0,5) == 'image') && $WIDTH){
69    $FILE = get_resized($FILE,$EXT,$WIDTH,$HEIGHT);
70  }
71
72  // finally send the file to the client
73  sendFile($FILE,$MIME,$CACHE);
74
75/* ------------------------------------------------------------------------ */
76
77/**
78 * Set headers and send the file to the client
79 *
80 * @author Andreas Gohr <andi@splitbrain.org>
81 * @author Ben Coburn <btcoburn@silicodon.net>
82 */
83function sendFile($file,$mime,$cache){
84  global $conf;
85  $fmtime = filemtime($file);
86  // send headers
87  header("Content-Type: $mime");
88  // smart http caching headers
89  if ($cache==-1) {
90    // cache
91    // cachetime or one hour
92    header('Expires: '.gmdate("D, d M Y H:i:s", time()+max($conf['cachetime'], 3600)).' GMT');
93    header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($conf['cachetime'], 3600));
94    header('Pragma: public');
95  } else if ($cache>0) {
96    // recache
97    // remaining cachetime + 10 seconds so the newly recached media is used
98    header('Expires: '.gmdate("D, d M Y H:i:s", $fmtime+$conf['cachetime']+10).' GMT');
99    header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($fmtime-time()+$conf['cachetime']+10, 0));
100    header('Pragma: public');
101  } else if ($cache==0) {
102    // nocache
103    header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
104    header('Pragma: public');
105  }
106  header('Accept-Ranges: bytes');
107  //send important headers first, script stops here if '304 Not Modified' response
108  http_conditionalRequest($fmtime);
109  list($start,$len) = http_rangeRequest(filesize($file));
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  // send file contents
117  $fp = @fopen($file,"rb");
118  if($fp){
119    fseek($fp,$start); //seek to start of range
120
121    $chunk = ($len > CHUNK_SIZE) ? CHUNK_SIZE : $len;
122    while (!feof($fp) && $chunk > 0) {
123      @set_time_limit(); // large files can take a lot of time
124      print fread($fp, $chunk);
125      flush();
126      $len -= $chunk;
127      $chunk = ($len > CHUNK_SIZE) ? CHUNK_SIZE : $len;
128    }
129    fclose($fp);
130  }else{
131    header("HTTP/1.0 500 Internal Server Error");
132    print "Could not read $file - bad permissions?";
133  }
134}
135
136/**
137 * Checks and sets headers to handle range requets
138 *
139 * @author  Andreas Gohr <andi@splitbrain.org>
140 * @returns array The start byte and the amount of bytes to send
141 */
142function http_rangeRequest($size){
143  if(!isset($_SERVER['HTTP_RANGE'])){
144    // no range requested - send the whole file
145    header("Content-Length: $size");
146    return array(0,$size);
147  }
148
149  $t = explode('=', $_SERVER['HTTP_RANGE']);
150  if (!$t[0]=='bytes') {
151    // we only understand byte ranges - send the whole file
152    header("Content-Length: $size");
153    return array(0,$size);
154  }
155
156  $r = explode('-', $t[1]);
157  $start = (int)$r[0];
158  $end = (int)$r[1];
159  if (!$end) $end = $size - 1;
160  if ($start > $end || $start > $size || $end > $size){
161    header('HTTP/1.1 416 Requested Range Not Satisfiable');
162    print 'Bad Range Request!';
163    exit;
164  }
165
166  $tot = $end - $start + 1;
167  header('HTTP/1.1 206 Partial Content');
168  header("Content-Range: bytes {$start}-{$end}/{$size}");
169  header("Content-Length: $tot");
170
171  return array($start,$tot);
172}
173
174/**
175 * Resizes the given image to the given size
176 *
177 * @author  Andreas Gohr <andi@splitbrain.org>
178 */
179function get_resized($file, $ext, $w, $h=0){
180  global $conf;
181
182  $info  = getimagesize($file);
183  if(!$h) $h = round(($w * $info[1]) / $info[0]);
184
185
186  //cache
187  $local = getCacheName($file,'.media.'.$w.'x'.$h.'.'.$ext);
188  $mtime = @filemtime($local); // 0 if not exists
189
190  if( $mtime > filemtime($file) ||
191      resize_imageIM($ext,$file,$info[0],$info[1],$local,$w,$h) ||
192      resize_imageGD($ext,$file,$info[0],$info[1],$local,$w,$h) ){
193    return $local;
194  }
195  //still here? resizing failed
196  return $file;
197}
198
199/**
200 * Returns the wanted cachetime in seconds
201 *
202 * Resolves named constants
203 *
204 * @author  Andreas Gohr <andi@splitbrain.org>
205 */
206function calc_cache($cache){
207  global $conf;
208
209  if(strtolower($cache) == 'nocache') return 0; //never cache
210  if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache
211  return -1; //cache endless
212}
213
214/**
215 * Download a remote file and return local filename
216 *
217 * returns false if download fails. Uses cached file if available and
218 * wanted
219 *
220 * @author  Andreas Gohr <andi@splitbrain.org>
221 * @author  Pavel Vitis <Pavel.Vitis@seznam.cz>
222 */
223function get_from_URL($url,$ext,$cache){
224  global $conf;
225
226  // if 'nocache' just redirect
227  if ($cache==0) { return false; }
228
229  $local = getCacheName(strtolower($url),".media.$ext");
230  $mtime = @filemtime($local); // 0 if not exists
231
232  //decide if download needed:
233  if( ($mtime == 0) ||                           // cache does not exist
234      ($cache != -1 && $mtime < time()-$cache)   // 'recache' and cache has expired
235    ){
236      if(io_download($url,$local)){
237        return $local;
238      }else{
239        return false;
240      }
241  }
242
243  //if cache exists use it else
244  if($mtime) return $local;
245
246  //else return false
247  return false;
248}
249
250/**
251 * resize images using external ImageMagick convert program
252 *
253 * @author Pavel Vitis <Pavel.Vitis@seznam.cz>
254 * @author Andreas Gohr <andi@splitbrain.org>
255 */
256function resize_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
257  global $conf;
258
259  // check if convert is configured
260  if(!$conf['im_convert']) return false;
261
262  // prepare command
263  $cmd  = $conf['im_convert'];
264  $cmd .= ' -resize '.$to_w.'x'.$to_h.'!';
265  $cmd .= " $from $to";
266
267  @exec($cmd,$out,$retval);
268  if ($retval == 0) return true;
269
270  return false;
271}
272
273/**
274 * resize images using PHP's libGD support
275 *
276 * @author Andreas Gohr <andi@splitbrain.org>
277 */
278function resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
279  global $conf;
280
281  if($conf['gdlib'] < 1) return false; //no GDlib available or wanted
282
283  // check available memory
284  if(!is_mem_available(($from_w * $from_h * 4) + ($to_w * $to_h * 4))){
285    return false;
286  }
287
288  // create an image of the given filetype
289  if ($ext == 'jpg' || $ext == 'jpeg'){
290    if(!function_exists("imagecreatefromjpeg")) return false;
291    $image = @imagecreatefromjpeg($from);
292  }elseif($ext == 'png') {
293    if(!function_exists("imagecreatefrompng")) return false;
294    $image = @imagecreatefrompng($from);
295
296  }elseif($ext == 'gif') {
297    if(!function_exists("imagecreatefromgif")) return false;
298    $image = @imagecreatefromgif($from);
299  }
300  if(!$image) return false;
301
302  if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor")){
303    $newimg = @imagecreatetruecolor ($to_w, $to_h);
304  }
305  if(!$newimg) $newimg = @imagecreate($to_w, $to_h);
306  if(!$newimg){
307    imagedestroy($image);
308    return false;
309  }
310
311  //keep png alpha channel if possible
312  if($ext == 'png' && $conf['gdlib']>1 && function_exists('imagesavealpha')){
313    imagealphablending($newimg, false);
314    imagesavealpha($newimg,true);
315  }
316
317  //try resampling first
318  if(function_exists("imagecopyresampled")){
319    if(!@imagecopyresampled($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h)) {
320      imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
321    }
322  }else{
323    imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
324  }
325
326  $okay = false;
327  if ($ext == 'jpg' || $ext == 'jpeg'){
328    if(!function_exists('imagejpeg')){
329      $okay = false;
330    }else{
331      $okay = imagejpeg($newimg, $to, 70);
332    }
333  }elseif($ext == 'png') {
334    if(!function_exists('imagepng')){
335      $okay = false;
336    }else{
337      $okay =  imagepng($newimg, $to);
338    }
339  }elseif($ext == 'gif') {
340    if(!function_exists('imagegif')){
341      $okay = false;
342    }else{
343      $okay = imagegif($newimg, $to);
344    }
345  }
346
347  // destroy GD image ressources
348  if($image) imagedestroy($image);
349  if($newimg) imagedestroy($newimg);
350
351  return $okay;
352}
353
354/**
355 * Checks if the given amount of memory is available
356 *
357 * If the memory_get_usage() function is not available the
358 * function just assumes $used bytes of already allocated memory
359 *
360 * @param  int $mem  Size of memory you want to allocate in bytes
361 * @param  int $used already allocated memory (see above)
362 * @author Filip Oscadal <webmaster@illusionsoftworks.cz>
363 * @author Andreas Gohr <andi@splitbrain.org>
364 */
365function is_mem_available($mem,$bytes=1048576){
366  $limit = trim(ini_get('memory_limit'));
367  if(empty($limit)) return true; // no limit set!
368
369  // parse limit to bytes
370  $unit = strtolower(substr($limit,-1));
371  switch($unit){
372    case 'g':
373      $limit = substr($limit,0,-1);
374      $limit *= 1024*1024*1024;
375      break;
376    case 'm':
377      $limit = substr($limit,0,-1);
378      $limit *= 1024*1024;
379      break;
380    case 'k':
381      $limit = substr($limit,0,-1);
382      $limit *= 1024;
383      break;
384  }
385
386  // get used memory if possible
387  if(function_exists('memory_get_usage')){
388    $used = memory_get_usage();
389  }
390
391
392  if($used+$mem > $limit){
393    return false;
394  }
395
396  return true;
397}
398
399//Setup VIM: ex: et ts=2 enc=utf-8 :
400?>
401