xref: /dokuwiki/lib/exe/fetch.php (revision 9af6e647b56056230f069dbb99935ae7402210fd)
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
18  $mimetypes = getMimeTypes();
19
20	//get input
21	$MEDIA  = $_REQUEST['media'];
22	$CACHE  = calc_cache($_REQUEST['cache']);
23	$WIDTH  = $_REQUEST['w'];
24	$HEIGHT = $_REQUEST['h'];
25  list($EXT,$MIME) = mimetype($MEDIA);
26  if($EXT === false){
27    $EXT  = 'unknown';
28    $MIME = 'application/octet-stream';
29  }
30
31	//media to local file
32	if(preg_match('#^(https?|ftp)://#i',$MEDIA)){
33    //handle external media
34  	$FILE = get_from_URL($MEDIA,$EXT,$CACHE);
35    if(!$FILE){
36      //download failed - redirect to original URL
37      header('Location: '.$MEDIA);
38      exit;
39    }
40  }else{
41    $MEDIA = cleanID($MEDIA);
42    if(empty($MEDIA)){
43      header("HTTP/1.0 400 Bad Request");
44      print 'Bad request';
45      exit;
46    }
47
48    //check permissions (namespace only)
49    if(auth_quickaclcheck(getNS($MEDIA).':X') < AUTH_READ){
50      header("HTTP/1.0 401 Unauthorized");
51      //fixme add some image for imagefiles
52      print 'Unauthorized';
53      exit;
54    }
55    $FILE  = mediaFN($MEDIA);
56  }
57
58  //check file existance
59  if(!@file_exists($FILE)){
60    header("HTTP/1.0 404 Not Found");
61    //FIXME add some default broken image
62    print 'Not Found';
63    exit;
64  }
65
66  //handle image resizing
67  if((substr($MIME,0,5) == 'image') && $WIDTH){
68    $FILE = get_resized($FILE,$EXT,$WIDTH,$HEIGHT);
69  }
70
71
72  //FIXME set sane cachecontrol headers
73  //FIXME handle conditional and partial requests
74
75  //send file
76  header("Content-Type: $MIME");
77  header('Last-Modified: '.date('r',filemtime($FILE)));
78  header('Content-Length: '.filesize($FILE));
79  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
80
81  //application mime type is downloadable
82  if(substr($MIME,0,11) == 'application'){
83    header('Content-Disposition: attachment; filename="'.basename($FILE).'"');
84  }
85
86  $fp = @fopen($FILE,"rb");
87  if($fp){
88    fpassthru($fp); //does a close itself
89  }else{
90    header("HTTP/1.0 500 Internal Server Error");
91    print "Could not read $FILE - bad permissions?";
92  }
93
94/* ------------------------------------------------------------------------ */
95
96/**
97 * Resizes the given image to the given size
98 *
99 * @author  Andreas Gohr <andi@splitbrain.org>
100 */
101function get_resized($file, $ext, $w, $h=0){
102  global $conf;
103
104  $md5   = md5($file);
105  $info  = getimagesize($file);
106  if(!$h) $h = round(($w * $info[1]) / $info[0]);
107
108
109  //cache
110  $local = $conf['mediadir'].'/_cache/'.$md5.'.'.$w.'x'.$h.'.'.$ext;
111  $mtime = @filemtime($local); // 0 if not exists
112
113  if( $mtime > filemtime($file) || resize_image($ext,$file,$info[0],$info[1],$local,$w,$h) ){
114    return $local;
115  }
116  //still here? resizing failed
117  return $file;
118}
119
120/**
121 * Returns the wanted cachetime in seconds
122 *
123 * Resolves named constants
124 *
125 * @author  Andreas Gohr <andi@splitbrain.org>
126 */
127function calc_cache($cache){
128  global $conf;
129
130  if(strtolower($cache) == 'nocache') return 0; //never cache
131  if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache
132  return -1; //cache endless
133}
134
135/**
136 * Download a remote file and return local filename
137 *
138 * returns false if download fails. Uses cached file if available and
139 * wanted
140 *
141 * @author  Andreas Gohr <andi@splitbrain.org>
142 */
143function get_from_URL($url,$ext,$cache){
144  global $conf;
145
146  $url = strtolower($url);
147  $md5 = md5($url);
148
149  $local = $conf['mediadir']."/_cache/$md5.$ext";
150  $mtime = @filemtime($local); // 0 if not exists
151
152  //decide if download needed:
153
154  //  never cache     exists but no endless cache     not exists or expired
155  if( $cache == 0 || ($mtime != 0 && $cache != -1) || $mtime < time()-$cache ){
156    if(io_download($url,$local)){
157      return $local;
158    }else{
159      return false;
160    }
161  }
162
163  //if cache exists use it else
164  if($mtime) return $local;
165
166  //else return false
167  return false;
168}
169
170/**
171 * resize images
172 *
173 * @author Andreas Gohr <andi@splitbrain.org>
174 */
175function resize_image($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
176  global $conf;
177
178  if($conf['gdlib'] < 1) return false; //no GDlib available or wanted
179
180  // create an image of the given filetype
181  if ($ext == 'jpg' || $ext == 'jpeg'){
182    if(!function_exists("imagecreatefromjpeg")) return false;
183    $image = @imagecreatefromjpeg($from);
184  }elseif($ext == 'png') {
185    if(!function_exists("imagecreatefrompng")) return false;
186    $image = @imagecreatefrompng($from);
187
188  }elseif($ext == 'gif') {
189    if(!function_exists("imagecreatefromgif")) return false;
190    $image = @imagecreatefromgif($from);
191  }
192  if(!$image) return false;
193
194  if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor")){
195    $newimg = @imagecreatetruecolor ($to_w, $to_h);
196  }
197  if(!$newimg) $newimg = @imagecreate($to_w, $to_h);
198  if(!$newimg) return false;
199
200  //keep png alpha channel if possible
201  if($ext == 'png' && $conf['gdlib']>1 && function_exists('imagesavealpha')){
202    imagealphablending($newimg, false);
203    imagesavealpha($newimg,true);
204  }
205
206  // create cachedir
207  io_makeFileDir($to);
208
209  //try resampling first
210  if(function_exists("imagecopyresampled")){
211    if(!@imagecopyresampled($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h)) {
212      imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
213    }
214  }else{
215    imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
216  }
217
218  if ($ext == 'jpg' || $ext == 'jpeg'){
219    if(!function_exists("imagejpeg")) return false;
220    return imagejpeg($newimg, $to, 70);
221  }elseif($ext == 'png') {
222    if(!function_exists("imagepng")) return false;
223    return imagepng($newimg, $to);
224  }elseif($ext == 'gif') {
225    if(!function_exists("imagegif")) return false;
226    return imagegif($newimg, $to);
227  }
228
229  return false;
230}
231
232
233//Setup VIM: ex: et ts=2 enc=utf-8 :
234?>
235