xref: /dokuwiki/lib/exe/fetch.php (revision 032933052326cab7864caafa674a9253814e1a17)
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');
12f62ea8a1Sandi  require_once(DOKU_INC.'inc/common.php');
1313c08e2fSMichael Klier  require_once(DOKU_INC.'inc/media.php');
14f62ea8a1Sandi  require_once(DOKU_INC.'inc/pageutils.php');
15758447cfSAndreas Gohr  require_once(DOKU_INC.'inc/httputils.php');
16f62ea8a1Sandi  require_once(DOKU_INC.'inc/confutils.php');
17f62ea8a1Sandi  require_once(DOKU_INC.'inc/auth.php');
186106ad89SChris Smith
198746e727Sandi  //close sesseion
208746e727Sandi  session_write_close();
218746e727Sandi
22f62ea8a1Sandi  $mimetypes = getMimeTypes();
23f62ea8a1Sandi
24f62ea8a1Sandi  //get input
2502b0b681SAndreas Gohr  $MEDIA  = stripctl(getID('media',false)); // no cleaning except control chars - maybe external
26f62ea8a1Sandi  $CACHE  = calc_cache($_REQUEST['cache']);
278fcc3410SAndreas Gohr  $WIDTH  = (int) $_REQUEST['w'];
288fcc3410SAndreas Gohr  $HEIGHT = (int) $_REQUEST['h'];
2927bf7924STom N Harris  list($EXT,$MIME,$DL) = mimetype($MEDIA,false);
30f62ea8a1Sandi  if($EXT === false){
31f62ea8a1Sandi    $EXT  = 'unknown';
32f62ea8a1Sandi    $MIME = 'application/octet-stream';
33ecebf3a8SAndreas Gohr    $DL   = true;
34f62ea8a1Sandi  }
35f62ea8a1Sandi
36*03293305SAndreas Gohr  // check for permissions, preconditions and cache external files
37*03293305SAndreas Gohr  list($STATUS, $STATUSMESSAGE) = checkFileStatus($MEDIA, $FILE);
38*03293305SAndreas Gohr
39*03293305SAndreas Gohr  // prepare data for plugin events
40cd98d9c3SGerry Weißbach  $data = array('media'           => $MEDIA,
41cd98d9c3SGerry Weißbach                'file'            => $FILE,
42cd98d9c3SGerry Weißbach                'orig'            => $FILE,
43cd98d9c3SGerry Weißbach                'mime'            => $MIME,
44cd98d9c3SGerry Weißbach                'download'        => $DL,
45cd98d9c3SGerry Weißbach                'cache'           => $CACHE,
46cd98d9c3SGerry Weißbach                'ext'             => $EXT,
47cd98d9c3SGerry Weißbach                'width'           => $WIDTH,
48cd98d9c3SGerry Weißbach                'height'          => $HEIGHT,
49cd98d9c3SGerry Weißbach                'status'          => $STATUS,
50cd98d9c3SGerry Weißbach                'statusmessage'   => $STATUSMESSAGE,
51cd98d9c3SGerry Weißbach  );
52f62ea8a1Sandi
53*03293305SAndreas Gohr  // handle the file status
54*03293305SAndreas Gohr  $evt = new Doku_Event('FETCH_MEDIA_STATUS', $data);
55cd98d9c3SGerry Weißbach  if ( $evt->advise_before() ) {
56*03293305SAndreas Gohr    // redirects
57*03293305SAndreas Gohr    if($data['status'] > 300 && $data['status'] <= 304){
58*03293305SAndreas Gohr      send_redirect($data['statusmessage']);
59*03293305SAndreas Gohr    }
60*03293305SAndreas Gohr    // send any non 200 status
61*03293305SAndreas Gohr    if($data['status'] != 200){
62cd98d9c3SGerry Weißbach      header('HTTP/1.0 ' . $data['status'] . ' ' . $data['statusmessage']);
63*03293305SAndreas Gohr    }
64*03293305SAndreas Gohr    // die on errors
65*03293305SAndreas Gohr    if($data['status'] > 203){
66cd98d9c3SGerry Weißbach      print $data['statusmessage'];
67f62ea8a1Sandi      exit;
68f62ea8a1Sandi    }
69f62ea8a1Sandi  }
70*03293305SAndreas Gohr  $evt->advise_after();
71*03293305SAndreas Gohr  unset($evt);
72f62ea8a1Sandi
7320bc86cfSAndreas Gohr  //handle image resizing/cropping
74f62ea8a1Sandi  if((substr($MIME,0,5) == 'image') && $WIDTH){
75d52a56e1SAndreas Gohr    if($HEIGHT){
76cd98d9c3SGerry Weißbach        $data['file'] = $FILE = media_crop_image($data['file'],$EXT,$WIDTH,$HEIGHT);
7720bc86cfSAndreas Gohr    }else{
78cd98d9c3SGerry Weißbach        $data['file'] = $FILE  = media_resize_image($data['file'],$EXT,$WIDTH,$HEIGHT);
79f62ea8a1Sandi    }
8020bc86cfSAndreas Gohr  }
81f62ea8a1Sandi
82e935fb4aSAndreas Gohr  // finally send the file to the client
83b80bedd6SAndreas Gohr  $evt = new Doku_Event('MEDIA_SENDFILE', $data);
84b80bedd6SAndreas Gohr  if ($evt->advise_before()) {
85ecebf3a8SAndreas Gohr    sendFile($data['file'],$data['mime'],$data['download'],$data['cache']);
86b80bedd6SAndreas Gohr  }
87cd98d9c3SGerry Weißbach  // Do something after the download finished.
88cd98d9c3SGerry Weißbach  $evt->advise_after();
89f62ea8a1Sandi
90e935fb4aSAndreas Gohr/* ------------------------------------------------------------------------ */
91f62ea8a1Sandi
92e935fb4aSAndreas Gohr/**
93e935fb4aSAndreas Gohr * Set headers and send the file to the client
94e935fb4aSAndreas Gohr *
95e935fb4aSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
9683730152SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
97e935fb4aSAndreas Gohr */
98ecebf3a8SAndreas Gohrfunction sendFile($file,$mime,$dl,$cache){
9983730152SBen Coburn  global $conf;
10009a5b61fSgweissbach  $fmtime = @filemtime($file);
101e935fb4aSAndreas Gohr  // send headers
102e935fb4aSAndreas Gohr  header("Content-Type: $mime");
10383730152SBen Coburn  // smart http caching headers
10483730152SBen Coburn  if ($cache==-1) {
10583730152SBen Coburn    // cache
10683730152SBen Coburn    // cachetime or one hour
10783730152SBen Coburn    header('Expires: '.gmdate("D, d M Y H:i:s", time()+max($conf['cachetime'], 3600)).' GMT');
10883730152SBen Coburn    header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($conf['cachetime'], 3600));
109e935fb4aSAndreas Gohr    header('Pragma: public');
11083730152SBen Coburn  } else if ($cache>0) {
11183730152SBen Coburn    // recache
11283730152SBen Coburn    // remaining cachetime + 10 seconds so the newly recached media is used
11383730152SBen Coburn    header('Expires: '.gmdate("D, d M Y H:i:s", $fmtime+$conf['cachetime']+10).' GMT');
11483730152SBen Coburn    header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($fmtime-time()+$conf['cachetime']+10, 0));
11583730152SBen Coburn    header('Pragma: public');
11683730152SBen Coburn  } else if ($cache==0) {
11783730152SBen Coburn    // nocache
11883730152SBen Coburn    header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
11983730152SBen Coburn    header('Pragma: public');
12083730152SBen Coburn  }
121ff4f5ee7SBen Coburn  //send important headers first, script stops here if '304 Not Modified' response
12283730152SBen Coburn  http_conditionalRequest($fmtime);
1239a87c72aSAndreas Gohr
124f62ea8a1Sandi
125ecebf3a8SAndreas Gohr  //download or display?
126ecebf3a8SAndreas Gohr  if($dl){
127e935fb4aSAndreas Gohr    header('Content-Disposition: attachment; filename="'.basename($file).'";');
128ecebf3a8SAndreas Gohr  }else{
129ecebf3a8SAndreas Gohr    header('Content-Disposition: inline; filename="'.basename($file).'";');
130f62ea8a1Sandi  }
131f62ea8a1Sandi
1329a87c72aSAndreas Gohr  //use x-sendfile header to pass the delivery to compatible webservers
1336106ad89SChris Smith  if (http_sendfile($file)) exit;
1349a87c72aSAndreas Gohr
135e935fb4aSAndreas Gohr  // send file contents
136e935fb4aSAndreas Gohr  $fp = @fopen($file,"rb");
137f62ea8a1Sandi  if($fp){
138758447cfSAndreas Gohr    http_rangeRequest($fp,filesize($file),$mime);
139f62ea8a1Sandi  }else{
140f62ea8a1Sandi    header("HTTP/1.0 500 Internal Server Error");
141e935fb4aSAndreas Gohr    print "Could not read $file - bad permissions?";
142e935fb4aSAndreas Gohr  }
143f62ea8a1Sandi}
144f62ea8a1Sandi
145*03293305SAndreas Gohr/**
146*03293305SAndreas Gohr * Check for media for preconditions and return correct status code
147cd98d9c3SGerry Weißbach *
148cd98d9c3SGerry Weißbach * READ: MEDIA, MIME, EXT, CACHE
149cd98d9c3SGerry Weißbach * WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE )
150cd98d9c3SGerry Weißbach *
151cd98d9c3SGerry Weißbach * @author Gerry Weissbach <gerry.w@gammaproduction.de>
152cd98d9c3SGerry Weißbach * @param $media reference to the media id
153cd98d9c3SGerry Weißbach * @param $file reference to the file variable
154cd98d9c3SGerry Weißbach * @returns array(STATUS, STATUSMESSAGE)
155cd98d9c3SGerry Weißbach */
156*03293305SAndreas Gohrfunction checkFileStatus(&$media, &$file) {
157cd98d9c3SGerry Weißbach  global $MIME, $EXT, $CACHE;
158cd98d9c3SGerry Weißbach
159cd98d9c3SGerry Weißbach  //media to local file
160cd98d9c3SGerry Weißbach  if(preg_match('#^(https?)://#i',$media)){
161cd98d9c3SGerry Weißbach    //check hash
162cd98d9c3SGerry Weißbach    if(substr(md5(auth_cookiesalt().$media),0,6) != $_REQUEST['hash']){
163cd98d9c3SGerry Weißbach      return array( 412, 'Precondition Failed');
164cd98d9c3SGerry Weißbach    }
165cd98d9c3SGerry Weißbach    //handle external images
166cd98d9c3SGerry Weißbach    if(strncmp($MIME,'image/',6) == 0) $file = media_get_from_URL($media,$EXT,$CACHE);
167cd98d9c3SGerry Weißbach    if(!$file){
168cd98d9c3SGerry Weißbach      //download failed - redirect to original URL
169*03293305SAndreas Gohr      return array( 302, $media );
170cd98d9c3SGerry Weißbach    }
171cd98d9c3SGerry Weißbach  }else{
172cd98d9c3SGerry Weißbach    $media = cleanID($media);
173cd98d9c3SGerry Weißbach    if(empty($media)){
174cd98d9c3SGerry Weißbach      return array( 400, 'Bad request' );
175cd98d9c3SGerry Weißbach    }
176cd98d9c3SGerry Weißbach
177cd98d9c3SGerry Weißbach    //check permissions (namespace only)
178cd98d9c3SGerry Weißbach    if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ){
179cd98d9c3SGerry Weißbach      return array( 401, 'Unauthorized' );
180cd98d9c3SGerry Weißbach    }
181cd98d9c3SGerry Weißbach    $file  = mediaFN($media);
182cd98d9c3SGerry Weißbach  }
183cd98d9c3SGerry Weißbach
184cd98d9c3SGerry Weißbach  //check file existance
185cd98d9c3SGerry Weißbach  if(!@file_exists($file)){
186cd98d9c3SGerry Weißbach      return array( 404, 'Not Found' );
187cd98d9c3SGerry Weißbach  }
188cd98d9c3SGerry Weißbach
189*03293305SAndreas Gohr  return array(200, null);
190cd98d9c3SGerry Weißbach}
191cd98d9c3SGerry Weißbach
192e935fb4aSAndreas Gohr/**
193f62ea8a1Sandi * Returns the wanted cachetime in seconds
194f62ea8a1Sandi *
195f62ea8a1Sandi * Resolves named constants
196f62ea8a1Sandi *
197f62ea8a1Sandi * @author  Andreas Gohr <andi@splitbrain.org>
198f62ea8a1Sandi */
199f62ea8a1Sandifunction calc_cache($cache){
200f62ea8a1Sandi  global $conf;
201f62ea8a1Sandi
202f62ea8a1Sandi  if(strtolower($cache) == 'nocache') return 0; //never cache
203f62ea8a1Sandi  if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache
204f62ea8a1Sandi  return -1; //cache endless
205f62ea8a1Sandi}
206f62ea8a1Sandi
207f62ea8a1Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
208