xref: /dokuwiki/lib/exe/fetch.php (revision e3776c06c37cc197709dac60892604dfea894ac2)
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
9d0a27cb0SAndreas Gohr  if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../');
103138b5c7SAndreas Gohr  define('DOKU_DISABLE_GZIP_OUTPUT', 1);
11f62ea8a1Sandi  require_once(DOKU_INC.'inc/init.php');
126106ad89SChris Smith
1324b3cb1aSAndreas Gohr  //close session
148746e727Sandi  session_write_close();
158746e727Sandi
16f62ea8a1Sandi  $mimetypes = getMimeTypes();
17f62ea8a1Sandi
18f62ea8a1Sandi  //get input
1902b0b681SAndreas Gohr  $MEDIA  = stripctl(getID('media',false)); // no cleaning except control chars - maybe external
20f62ea8a1Sandi  $CACHE  = calc_cache($_REQUEST['cache']);
218fcc3410SAndreas Gohr  $WIDTH  = (int) $_REQUEST['w'];
228fcc3410SAndreas Gohr  $HEIGHT = (int) $_REQUEST['h'];
2327bf7924STom N Harris  list($EXT,$MIME,$DL) = mimetype($MEDIA,false);
24f62ea8a1Sandi  if($EXT === false){
25f62ea8a1Sandi    $EXT  = 'unknown';
26f62ea8a1Sandi    $MIME = 'application/octet-stream';
27ecebf3a8SAndreas Gohr    $DL   = true;
28f62ea8a1Sandi  }
29f62ea8a1Sandi
3003293305SAndreas Gohr  // check for permissions, preconditions and cache external files
3103293305SAndreas Gohr  list($STATUS, $STATUSMESSAGE) = checkFileStatus($MEDIA, $FILE);
3203293305SAndreas Gohr
3303293305SAndreas Gohr  // prepare data for plugin events
34cd98d9c3SGerry Weißbach  $data = array('media'           => $MEDIA,
35cd98d9c3SGerry Weißbach                'file'            => $FILE,
36cd98d9c3SGerry Weißbach                'orig'            => $FILE,
37cd98d9c3SGerry Weißbach                'mime'            => $MIME,
38cd98d9c3SGerry Weißbach                'download'        => $DL,
39cd98d9c3SGerry Weißbach                'cache'           => $CACHE,
40cd98d9c3SGerry Weißbach                'ext'             => $EXT,
41cd98d9c3SGerry Weißbach                'width'           => $WIDTH,
42cd98d9c3SGerry Weißbach                'height'          => $HEIGHT,
43cd98d9c3SGerry Weißbach                'status'          => $STATUS,
44cd98d9c3SGerry Weißbach                'statusmessage'   => $STATUSMESSAGE,
45cd98d9c3SGerry Weißbach  );
46f62ea8a1Sandi
4703293305SAndreas Gohr  // handle the file status
4803293305SAndreas Gohr  $evt = new Doku_Event('FETCH_MEDIA_STATUS', $data);
49cd98d9c3SGerry Weißbach  if ( $evt->advise_before() ) {
5003293305SAndreas Gohr    // redirects
5103293305SAndreas Gohr    if($data['status'] > 300 && $data['status'] <= 304){
5203293305SAndreas Gohr      send_redirect($data['statusmessage']);
5303293305SAndreas Gohr    }
5403293305SAndreas Gohr    // send any non 200 status
5503293305SAndreas Gohr    if($data['status'] != 200){
56cd98d9c3SGerry Weißbach      header('HTTP/1.0 ' . $data['status'] . ' ' . $data['statusmessage']);
5703293305SAndreas Gohr    }
5803293305SAndreas Gohr    // die on errors
5903293305SAndreas Gohr    if($data['status'] > 203){
60cd98d9c3SGerry Weißbach      print $data['statusmessage'];
61f62ea8a1Sandi      exit;
62f62ea8a1Sandi    }
63f62ea8a1Sandi  }
6403293305SAndreas Gohr  $evt->advise_after();
6503293305SAndreas Gohr  unset($evt);
66f62ea8a1Sandi
6720bc86cfSAndreas Gohr  //handle image resizing/cropping
68f62ea8a1Sandi  if((substr($MIME,0,5) == 'image') && $WIDTH){
69d52a56e1SAndreas Gohr    if($HEIGHT){
70cd98d9c3SGerry Weißbach        $data['file'] = $FILE = media_crop_image($data['file'],$EXT,$WIDTH,$HEIGHT);
7120bc86cfSAndreas Gohr    }else{
72cd98d9c3SGerry Weißbach        $data['file'] = $FILE  = media_resize_image($data['file'],$EXT,$WIDTH,$HEIGHT);
73f62ea8a1Sandi    }
7420bc86cfSAndreas Gohr  }
75f62ea8a1Sandi
76e935fb4aSAndreas Gohr  // finally send the file to the client
77b80bedd6SAndreas Gohr  $evt = new Doku_Event('MEDIA_SENDFILE', $data);
78b80bedd6SAndreas Gohr  if ($evt->advise_before()) {
79ecebf3a8SAndreas Gohr    sendFile($data['file'],$data['mime'],$data['download'],$data['cache']);
80b80bedd6SAndreas Gohr  }
81cd98d9c3SGerry Weißbach  // Do something after the download finished.
82cd98d9c3SGerry Weißbach  $evt->advise_after();
83f62ea8a1Sandi
84e935fb4aSAndreas Gohr/* ------------------------------------------------------------------------ */
85f62ea8a1Sandi
86e935fb4aSAndreas Gohr/**
87e935fb4aSAndreas Gohr * Set headers and send the file to the client
88e935fb4aSAndreas Gohr *
89e935fb4aSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
9083730152SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
91e935fb4aSAndreas Gohr */
92ecebf3a8SAndreas Gohrfunction sendFile($file,$mime,$dl,$cache){
9383730152SBen Coburn  global $conf;
9409a5b61fSgweissbach  $fmtime = @filemtime($file);
95e935fb4aSAndreas Gohr  // send headers
96e935fb4aSAndreas Gohr  header("Content-Type: $mime");
9783730152SBen Coburn  // smart http caching headers
9883730152SBen Coburn  if ($cache==-1) {
9983730152SBen Coburn    // cache
10083730152SBen Coburn    // cachetime or one hour
10183730152SBen Coburn    header('Expires: '.gmdate("D, d M Y H:i:s", time()+max($conf['cachetime'], 3600)).' GMT');
10283730152SBen Coburn    header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($conf['cachetime'], 3600));
103e935fb4aSAndreas Gohr    header('Pragma: public');
10483730152SBen Coburn  } else if ($cache>0) {
10583730152SBen Coburn    // recache
10683730152SBen Coburn    // remaining cachetime + 10 seconds so the newly recached media is used
10783730152SBen Coburn    header('Expires: '.gmdate("D, d M Y H:i:s", $fmtime+$conf['cachetime']+10).' GMT');
10883730152SBen Coburn    header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($fmtime-time()+$conf['cachetime']+10, 0));
10983730152SBen Coburn    header('Pragma: public');
11083730152SBen Coburn  } else if ($cache==0) {
11183730152SBen Coburn    // nocache
11283730152SBen Coburn    header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
11383730152SBen Coburn    header('Pragma: public');
11483730152SBen Coburn  }
115ff4f5ee7SBen Coburn  //send important headers first, script stops here if '304 Not Modified' response
11683730152SBen Coburn  http_conditionalRequest($fmtime);
1179a87c72aSAndreas Gohr
118f62ea8a1Sandi
119ecebf3a8SAndreas Gohr  //download or display?
120ecebf3a8SAndreas Gohr  if($dl){
121e935fb4aSAndreas Gohr    header('Content-Disposition: attachment; filename="'.basename($file).'";');
122ecebf3a8SAndreas Gohr  }else{
123ecebf3a8SAndreas Gohr    header('Content-Disposition: inline; filename="'.basename($file).'";');
124f62ea8a1Sandi  }
125f62ea8a1Sandi
1269a87c72aSAndreas Gohr  //use x-sendfile header to pass the delivery to compatible webservers
1276106ad89SChris Smith  if (http_sendfile($file)) exit;
1289a87c72aSAndreas Gohr
129e935fb4aSAndreas Gohr  // send file contents
130e935fb4aSAndreas Gohr  $fp = @fopen($file,"rb");
131f62ea8a1Sandi  if($fp){
132758447cfSAndreas Gohr    http_rangeRequest($fp,filesize($file),$mime);
133f62ea8a1Sandi  }else{
134f62ea8a1Sandi    header("HTTP/1.0 500 Internal Server Error");
135e935fb4aSAndreas Gohr    print "Could not read $file - bad permissions?";
136e935fb4aSAndreas Gohr  }
137f62ea8a1Sandi}
138f62ea8a1Sandi
13903293305SAndreas Gohr/**
14003293305SAndreas Gohr * Check for media for preconditions and return correct status code
141cd98d9c3SGerry Weißbach *
142cd98d9c3SGerry Weißbach * READ: MEDIA, MIME, EXT, CACHE
143cd98d9c3SGerry Weißbach * WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE )
144cd98d9c3SGerry Weißbach *
145cd98d9c3SGerry Weißbach * @author Gerry Weissbach <gerry.w@gammaproduction.de>
146cd98d9c3SGerry Weißbach * @param $media reference to the media id
147cd98d9c3SGerry Weißbach * @param $file reference to the file variable
148cd98d9c3SGerry Weißbach * @returns array(STATUS, STATUSMESSAGE)
149cd98d9c3SGerry Weißbach */
15003293305SAndreas Gohrfunction checkFileStatus(&$media, &$file) {
151cd98d9c3SGerry Weißbach  global $MIME, $EXT, $CACHE;
152cd98d9c3SGerry Weißbach
153cd98d9c3SGerry Weißbach  //media to local file
154cd98d9c3SGerry Weißbach  if(preg_match('#^(https?)://#i',$media)){
155cd98d9c3SGerry Weißbach    //check hash
156cd98d9c3SGerry Weißbach    if(substr(md5(auth_cookiesalt().$media),0,6) != $_REQUEST['hash']){
157cd98d9c3SGerry Weißbach      return array( 412, 'Precondition Failed');
158cd98d9c3SGerry Weißbach    }
159cd98d9c3SGerry Weißbach    //handle external images
160cd98d9c3SGerry Weißbach    if(strncmp($MIME,'image/',6) == 0) $file = media_get_from_URL($media,$EXT,$CACHE);
161cd98d9c3SGerry Weißbach    if(!$file){
162cd98d9c3SGerry Weißbach      //download failed - redirect to original URL
16303293305SAndreas Gohr      return array( 302, $media );
164cd98d9c3SGerry Weißbach    }
165cd98d9c3SGerry Weißbach  }else{
166cd98d9c3SGerry Weißbach    $media = cleanID($media);
167cd98d9c3SGerry Weißbach    if(empty($media)){
168cd98d9c3SGerry Weißbach      return array( 400, 'Bad request' );
169cd98d9c3SGerry Weißbach    }
170cd98d9c3SGerry Weißbach
171cd98d9c3SGerry Weißbach    //check permissions (namespace only)
172cd98d9c3SGerry Weißbach    if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ){
173c6f610efSAndreas Gohr      return array( 403, 'Forbidden' );
174cd98d9c3SGerry Weißbach    }
175cd98d9c3SGerry Weißbach    $file  = mediaFN($media);
176cd98d9c3SGerry Weißbach  }
177cd98d9c3SGerry Weißbach
178cd98d9c3SGerry Weißbach  //check file existance
179cd98d9c3SGerry Weißbach  if(!@file_exists($file)){
180cd98d9c3SGerry Weißbach      return array( 404, 'Not Found' );
181cd98d9c3SGerry Weißbach  }
182cd98d9c3SGerry Weißbach
18303293305SAndreas Gohr  return array(200, null);
184cd98d9c3SGerry Weißbach}
185cd98d9c3SGerry Weißbach
186e935fb4aSAndreas Gohr/**
187f62ea8a1Sandi * Returns the wanted cachetime in seconds
188f62ea8a1Sandi *
189f62ea8a1Sandi * Resolves named constants
190f62ea8a1Sandi *
191f62ea8a1Sandi * @author  Andreas Gohr <andi@splitbrain.org>
192f62ea8a1Sandi */
193f62ea8a1Sandifunction calc_cache($cache){
194f62ea8a1Sandi  global $conf;
195f62ea8a1Sandi
196f62ea8a1Sandi  if(strtolower($cache) == 'nocache') return 0; //never cache
197f62ea8a1Sandi  if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache
198f62ea8a1Sandi  return -1; //cache endless
199f62ea8a1Sandi}
200f62ea8a1Sandi
201*e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 :
202