xref: /dokuwiki/lib/exe/fetch.php (revision 847b82981c3abfa3a352c66c3922c3c2cb89f023)
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
9b1a72e3dSmatthiasgrimm  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(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');
13f62ea8a1Sandi  require_once(DOKU_INC.'inc/pageutils.php');
14f62ea8a1Sandi  require_once(DOKU_INC.'inc/confutils.php');
15f62ea8a1Sandi  require_once(DOKU_INC.'inc/auth.php');
168746e727Sandi  //close sesseion
178746e727Sandi  session_write_close();
18e935fb4aSAndreas Gohr  if(!defined('CHUNK_SIZE')) define('CHUNK_SIZE',16*1024);
198746e727Sandi
20f62ea8a1Sandi  $mimetypes = getMimeTypes();
21f62ea8a1Sandi
22f62ea8a1Sandi  //get input
2342905504SAndreas Gohr  $MEDIA  = getID('media',false); // no cleaning - maybe external
24f62ea8a1Sandi  $CACHE  = calc_cache($_REQUEST['cache']);
25f62ea8a1Sandi  $WIDTH  = $_REQUEST['w'];
26f62ea8a1Sandi  $HEIGHT = $_REQUEST['h'];
27f62ea8a1Sandi  list($EXT,$MIME) = mimetype($MEDIA);
28f62ea8a1Sandi  if($EXT === false){
29f62ea8a1Sandi    $EXT  = 'unknown';
30f62ea8a1Sandi    $MIME = 'application/octet-stream';
31f62ea8a1Sandi  }
32f62ea8a1Sandi
33f62ea8a1Sandi  //media to local file
34f62ea8a1Sandi  if(preg_match('#^(https?|ftp)://#i',$MEDIA)){
35f62ea8a1Sandi    //handle external media
36f62ea8a1Sandi    $FILE = get_from_URL($MEDIA,$EXT,$CACHE);
37f62ea8a1Sandi    if(!$FILE){
38f62ea8a1Sandi      //download failed - redirect to original URL
39f62ea8a1Sandi      header('Location: '.$MEDIA);
40f62ea8a1Sandi      exit;
41f62ea8a1Sandi    }
42f62ea8a1Sandi  }else{
43f62ea8a1Sandi    $MEDIA = cleanID($MEDIA);
44f62ea8a1Sandi    if(empty($MEDIA)){
45f62ea8a1Sandi      header("HTTP/1.0 400 Bad Request");
46f62ea8a1Sandi      print 'Bad request';
47f62ea8a1Sandi      exit;
48f62ea8a1Sandi    }
49f62ea8a1Sandi
50f62ea8a1Sandi    //check permissions (namespace only)
51f62ea8a1Sandi    if(auth_quickaclcheck(getNS($MEDIA).':X') < AUTH_READ){
52f62ea8a1Sandi      header("HTTP/1.0 401 Unauthorized");
53f62ea8a1Sandi      //fixme add some image for imagefiles
54f62ea8a1Sandi      print 'Unauthorized';
55f62ea8a1Sandi      exit;
56f62ea8a1Sandi    }
57f62ea8a1Sandi    $FILE  = mediaFN($MEDIA);
58f62ea8a1Sandi  }
59f62ea8a1Sandi
60f62ea8a1Sandi  //check file existance
61f62ea8a1Sandi  if(!@file_exists($FILE)){
62f62ea8a1Sandi    header("HTTP/1.0 404 Not Found");
63f62ea8a1Sandi    //FIXME add some default broken image
64f62ea8a1Sandi    print 'Not Found';
65f62ea8a1Sandi    exit;
66f62ea8a1Sandi  }
67f62ea8a1Sandi
68f62ea8a1Sandi  //handle image resizing
69f62ea8a1Sandi  if((substr($MIME,0,5) == 'image') && $WIDTH){
70f62ea8a1Sandi    $FILE = get_resized($FILE,$EXT,$WIDTH,$HEIGHT);
71f62ea8a1Sandi  }
72f62ea8a1Sandi
73e935fb4aSAndreas Gohr  // finally send the file to the client
7483730152SBen Coburn  sendFile($FILE,$MIME,$CACHE);
75f62ea8a1Sandi
76e935fb4aSAndreas Gohr/* ------------------------------------------------------------------------ */
77f62ea8a1Sandi
78e935fb4aSAndreas Gohr/**
79e935fb4aSAndreas Gohr * Set headers and send the file to the client
80e935fb4aSAndreas Gohr *
81e935fb4aSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
8283730152SBen Coburn * @author Ben Coburn <btcoburn@silicodon.net>
83e935fb4aSAndreas Gohr */
8483730152SBen Coburnfunction sendFile($file,$mime,$cache){
8583730152SBen Coburn  global $conf;
8683730152SBen Coburn  $fmtime = filemtime($file);
87e935fb4aSAndreas Gohr  // send headers
88e935fb4aSAndreas Gohr  header("Content-Type: $mime");
8983730152SBen Coburn  // smart http caching headers
9083730152SBen Coburn  if ($cache==-1) {
9183730152SBen Coburn    // cache
9283730152SBen Coburn    // cachetime or one hour
9383730152SBen Coburn    header('Expires: '.gmdate("D, d M Y H:i:s", time()+max($conf['cachetime'], 3600)).' GMT');
9483730152SBen Coburn    header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($conf['cachetime'], 3600));
95e935fb4aSAndreas Gohr    header('Pragma: public');
9683730152SBen Coburn  } else if ($cache>0) {
9783730152SBen Coburn    // recache
9883730152SBen Coburn    // remaining cachetime + 10 seconds so the newly recached media is used
9983730152SBen Coburn    header('Expires: '.gmdate("D, d M Y H:i:s", $fmtime+$conf['cachetime']+10).' GMT');
10083730152SBen Coburn    header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.max($fmtime-time()+$conf['cachetime']+10, 0));
10183730152SBen Coburn    header('Pragma: public');
10283730152SBen Coburn  } else if ($cache==0) {
10383730152SBen Coburn    // nocache
10483730152SBen Coburn    header('Cache-Control: must-revalidate, no-transform, post-check=0, pre-check=0');
10583730152SBen Coburn    header('Pragma: public');
10683730152SBen Coburn  }
107e935fb4aSAndreas Gohr  header('Accept-Ranges: bytes');
108ff4f5ee7SBen Coburn  //send important headers first, script stops here if '304 Not Modified' response
10983730152SBen Coburn  http_conditionalRequest($fmtime);
110ff4f5ee7SBen Coburn  list($start,$len) = http_rangeRequest(filesize($file));
111f62ea8a1Sandi
112f62ea8a1Sandi  //application mime type is downloadable
113e935fb4aSAndreas Gohr  if(substr($mime,0,11) == 'application'){
114e935fb4aSAndreas Gohr    header('Content-Disposition: attachment; filename="'.basename($file).'";');
115f62ea8a1Sandi  }
116f62ea8a1Sandi
117e935fb4aSAndreas Gohr  // send file contents
118e935fb4aSAndreas Gohr  $fp = @fopen($file,"rb");
119f62ea8a1Sandi  if($fp){
120e935fb4aSAndreas Gohr    fseek($fp,$start); //seek to start of range
121e935fb4aSAndreas Gohr
122e935fb4aSAndreas Gohr    $chunk = ($len > CHUNK_SIZE) ? CHUNK_SIZE : $len;
123e935fb4aSAndreas Gohr    while (!feof($fp) && $chunk > 0) {
124615a21edSBrian Cowan      @set_time_limit(); // large files can take a lot of time
125e935fb4aSAndreas Gohr      print fread($fp, $chunk);
126615a21edSBrian Cowan      flush();
127e935fb4aSAndreas Gohr      $len -= $chunk;
128e935fb4aSAndreas Gohr      $chunk = ($len > CHUNK_SIZE) ? CHUNK_SIZE : $len;
129615a21edSBrian Cowan    }
130615a21edSBrian Cowan    fclose($fp);
131f62ea8a1Sandi  }else{
132f62ea8a1Sandi    header("HTTP/1.0 500 Internal Server Error");
133e935fb4aSAndreas Gohr    print "Could not read $file - bad permissions?";
134e935fb4aSAndreas Gohr  }
135f62ea8a1Sandi}
136f62ea8a1Sandi
137e935fb4aSAndreas Gohr/**
138e935fb4aSAndreas Gohr * Checks and sets headers to handle range requets
139e935fb4aSAndreas Gohr *
140e935fb4aSAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
141e935fb4aSAndreas Gohr * @returns array The start byte and the amount of bytes to send
142e935fb4aSAndreas Gohr */
143e935fb4aSAndreas Gohrfunction http_rangeRequest($size){
144e935fb4aSAndreas Gohr  if(!isset($_SERVER['HTTP_RANGE'])){
145e935fb4aSAndreas Gohr    // no range requested - send the whole file
146e935fb4aSAndreas Gohr    header("Content-Length: $size");
147e935fb4aSAndreas Gohr    return array(0,$size);
148e935fb4aSAndreas Gohr  }
149e935fb4aSAndreas Gohr
150e935fb4aSAndreas Gohr  $t = explode('=', $_SERVER['HTTP_RANGE']);
151e935fb4aSAndreas Gohr  if (!$t[0]=='bytes') {
152e935fb4aSAndreas Gohr    // we only understand byte ranges - send the whole file
153e935fb4aSAndreas Gohr    header("Content-Length: $size");
154e935fb4aSAndreas Gohr    return array(0,$size);
155e935fb4aSAndreas Gohr  }
156e935fb4aSAndreas Gohr
157e935fb4aSAndreas Gohr  $r = explode('-', $t[1]);
158e935fb4aSAndreas Gohr  $start = (int)$r[0];
159e935fb4aSAndreas Gohr  $end = (int)$r[1];
160e935fb4aSAndreas Gohr  if (!$end) $end = $size - 1;
161e935fb4aSAndreas Gohr  if ($start > $end || $start > $size || $end > $size){
162e935fb4aSAndreas Gohr    header('HTTP/1.1 416 Requested Range Not Satisfiable');
163e935fb4aSAndreas Gohr    print 'Bad Range Request!';
164e935fb4aSAndreas Gohr    exit;
165e935fb4aSAndreas Gohr  }
166e935fb4aSAndreas Gohr
167e935fb4aSAndreas Gohr  $tot = $end - $start + 1;
168e935fb4aSAndreas Gohr  header('HTTP/1.1 206 Partial Content');
169e935fb4aSAndreas Gohr  header("Content-Range: bytes {$start}-{$end}/{$size}");
170e935fb4aSAndreas Gohr  header("Content-Length: $tot");
171e935fb4aSAndreas Gohr
172e935fb4aSAndreas Gohr  return array($start,$tot);
173e935fb4aSAndreas Gohr}
174e935fb4aSAndreas Gohr
175e935fb4aSAndreas Gohr/**
176f62ea8a1Sandi * Resizes the given image to the given size
177f62ea8a1Sandi *
178f62ea8a1Sandi * @author  Andreas Gohr <andi@splitbrain.org>
179f62ea8a1Sandi */
180f62ea8a1Sandifunction get_resized($file, $ext, $w, $h=0){
181f62ea8a1Sandi  global $conf;
182f62ea8a1Sandi
183f62ea8a1Sandi  $info  = getimagesize($file);
184f62ea8a1Sandi  if(!$h) $h = round(($w * $info[1]) / $info[0]);
185f62ea8a1Sandi
186f62ea8a1Sandi
187f62ea8a1Sandi  //cache
18898407a7aSandi  $local = getCacheName($file,'.media.'.$w.'x'.$h.'.'.$ext);
189f62ea8a1Sandi  $mtime = @filemtime($local); // 0 if not exists
190f62ea8a1Sandi
19168375754SPavel Vitis  if( $mtime > filemtime($file) ||
19268375754SPavel Vitis      resize_imageIM($ext,$file,$info[0],$info[1],$local,$w,$h) ||
19368375754SPavel Vitis      resize_imageGD($ext,$file,$info[0],$info[1],$local,$w,$h) ){
194f62ea8a1Sandi    return $local;
195f62ea8a1Sandi  }
196f62ea8a1Sandi  //still here? resizing failed
197f62ea8a1Sandi  return $file;
198f62ea8a1Sandi}
199f62ea8a1Sandi
200f62ea8a1Sandi/**
201f62ea8a1Sandi * Returns the wanted cachetime in seconds
202f62ea8a1Sandi *
203f62ea8a1Sandi * Resolves named constants
204f62ea8a1Sandi *
205f62ea8a1Sandi * @author  Andreas Gohr <andi@splitbrain.org>
206f62ea8a1Sandi */
207f62ea8a1Sandifunction calc_cache($cache){
208f62ea8a1Sandi  global $conf;
209f62ea8a1Sandi
210f62ea8a1Sandi  if(strtolower($cache) == 'nocache') return 0; //never cache
211f62ea8a1Sandi  if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache
212f62ea8a1Sandi  return -1; //cache endless
213f62ea8a1Sandi}
214f62ea8a1Sandi
215f62ea8a1Sandi/**
216f62ea8a1Sandi * Download a remote file and return local filename
217f62ea8a1Sandi *
218f62ea8a1Sandi * returns false if download fails. Uses cached file if available and
219f62ea8a1Sandi * wanted
220f62ea8a1Sandi *
221f62ea8a1Sandi * @author  Andreas Gohr <andi@splitbrain.org>
22268375754SPavel Vitis * @author  Pavel Vitis <Pavel.Vitis@seznam.cz>
223f62ea8a1Sandi */
224f62ea8a1Sandifunction get_from_URL($url,$ext,$cache){
225f62ea8a1Sandi  global $conf;
226f62ea8a1Sandi
227*847b8298SAndreas Gohr  // if no cache or fetchsize just redirect
228*847b8298SAndreas Gohr  if ($cache==0)           return false;
229*847b8298SAndreas Gohr  if (!$conf['fetchsize']) return false;
2304f3c4962SBen Coburn
23168375754SPavel Vitis  $local = getCacheName(strtolower($url),".media.$ext");
232f62ea8a1Sandi  $mtime = @filemtime($local); // 0 if not exists
233f62ea8a1Sandi
234f62ea8a1Sandi  //decide if download needed:
2354f3c4962SBen Coburn  if( ($mtime == 0) ||                           // cache does not exist
2364f3c4962SBen Coburn      ($cache != -1 && $mtime < time()-$cache)   // 'recache' and cache has expired
23768375754SPavel Vitis    ){
238*847b8298SAndreas Gohr      if(io_download($url,$local,false,'',$conf['fetchsize'])){
239f62ea8a1Sandi        return $local;
240f62ea8a1Sandi      }else{
241f62ea8a1Sandi        return false;
242f62ea8a1Sandi      }
243f62ea8a1Sandi  }
244f62ea8a1Sandi
245f62ea8a1Sandi  //if cache exists use it else
246f62ea8a1Sandi  if($mtime) return $local;
247f62ea8a1Sandi
248f62ea8a1Sandi  //else return false
249f62ea8a1Sandi  return false;
250f62ea8a1Sandi}
251f62ea8a1Sandi
252f62ea8a1Sandi/**
25368375754SPavel Vitis * resize images using external ImageMagick convert program
25468375754SPavel Vitis *
25568375754SPavel Vitis * @author Pavel Vitis <Pavel.Vitis@seznam.cz>
25668375754SPavel Vitis * @author Andreas Gohr <andi@splitbrain.org>
25768375754SPavel Vitis */
25868375754SPavel Vitisfunction resize_imageIM($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
25968375754SPavel Vitis  global $conf;
26068375754SPavel Vitis
2617bc7a78eSAndreas Gohr  // check if convert is configured
2627bc7a78eSAndreas Gohr  if(!$conf['im_convert']) return false;
26368375754SPavel Vitis
26468375754SPavel Vitis  // prepare command
26568375754SPavel Vitis  $cmd  = $conf['im_convert'];
26668375754SPavel Vitis  $cmd .= ' -resize '.$to_w.'x'.$to_h.'!';
2672b03e74dSBen Coburn  if ($ext == 'jpg' || $ext == 'jpeg') {
2682b03e74dSBen Coburn      $cmd .= ' -quality '.$conf['jpg_quality'];
2692b03e74dSBen Coburn  }
27068375754SPavel Vitis  $cmd .= " $from $to";
27168375754SPavel Vitis
27268375754SPavel Vitis  @exec($cmd,$out,$retval);
27368375754SPavel Vitis  if ($retval == 0) return true;
27468375754SPavel Vitis
27568375754SPavel Vitis  return false;
27668375754SPavel Vitis}
27768375754SPavel Vitis
27868375754SPavel Vitis/**
27968375754SPavel Vitis * resize images using PHP's libGD support
280f62ea8a1Sandi *
281f62ea8a1Sandi * @author Andreas Gohr <andi@splitbrain.org>
282f62ea8a1Sandi */
28368375754SPavel Vitisfunction resize_imageGD($ext,$from,$from_w,$from_h,$to,$to_w,$to_h){
284f62ea8a1Sandi  global $conf;
285f62ea8a1Sandi
286f62ea8a1Sandi  if($conf['gdlib'] < 1) return false; //no GDlib available or wanted
287f62ea8a1Sandi
2884e406776SAndreas Gohr  // check available memory
2894e406776SAndreas Gohr  if(!is_mem_available(($from_w * $from_h * 4) + ($to_w * $to_h * 4))){
2904e406776SAndreas Gohr    return false;
2914e406776SAndreas Gohr  }
2924e406776SAndreas Gohr
293f62ea8a1Sandi  // create an image of the given filetype
294f62ea8a1Sandi  if ($ext == 'jpg' || $ext == 'jpeg'){
295f62ea8a1Sandi    if(!function_exists("imagecreatefromjpeg")) return false;
296f62ea8a1Sandi    $image = @imagecreatefromjpeg($from);
297f62ea8a1Sandi  }elseif($ext == 'png') {
298f62ea8a1Sandi    if(!function_exists("imagecreatefrompng")) return false;
299f62ea8a1Sandi    $image = @imagecreatefrompng($from);
300f62ea8a1Sandi
301f62ea8a1Sandi  }elseif($ext == 'gif') {
302f62ea8a1Sandi    if(!function_exists("imagecreatefromgif")) return false;
303f62ea8a1Sandi    $image = @imagecreatefromgif($from);
304f62ea8a1Sandi  }
305f62ea8a1Sandi  if(!$image) return false;
306f62ea8a1Sandi
307f62ea8a1Sandi  if(($conf['gdlib']>1) && function_exists("imagecreatetruecolor")){
308f62ea8a1Sandi    $newimg = @imagecreatetruecolor ($to_w, $to_h);
309f62ea8a1Sandi  }
310f62ea8a1Sandi  if(!$newimg) $newimg = @imagecreate($to_w, $to_h);
311dd7bbbf4SAndreas Gohr  if(!$newimg){
312dd7bbbf4SAndreas Gohr    imagedestroy($image);
313dd7bbbf4SAndreas Gohr    return false;
314dd7bbbf4SAndreas Gohr  }
315f62ea8a1Sandi
316f62ea8a1Sandi  //keep png alpha channel if possible
317f62ea8a1Sandi  if($ext == 'png' && $conf['gdlib']>1 && function_exists('imagesavealpha')){
318f62ea8a1Sandi    imagealphablending($newimg, false);
319f62ea8a1Sandi    imagesavealpha($newimg,true);
320f62ea8a1Sandi  }
321f62ea8a1Sandi
322f62ea8a1Sandi  //try resampling first
323f62ea8a1Sandi  if(function_exists("imagecopyresampled")){
324f62ea8a1Sandi    if(!@imagecopyresampled($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h)) {
325f62ea8a1Sandi      imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
326f62ea8a1Sandi    }
327f62ea8a1Sandi  }else{
328f62ea8a1Sandi    imagecopyresized($newimg, $image, 0, 0, 0, 0, $to_w, $to_h, $from_w, $from_h);
329f62ea8a1Sandi  }
330f62ea8a1Sandi
331dd7bbbf4SAndreas Gohr  $okay = false;
332f62ea8a1Sandi  if ($ext == 'jpg' || $ext == 'jpeg'){
333dd7bbbf4SAndreas Gohr    if(!function_exists('imagejpeg')){
334dd7bbbf4SAndreas Gohr      $okay = false;
335dd7bbbf4SAndreas Gohr    }else{
3362b03e74dSBen Coburn      $okay = imagejpeg($newimg, $to, $conf['jpg_quality']);
337dd7bbbf4SAndreas Gohr    }
338f62ea8a1Sandi  }elseif($ext == 'png') {
339dd7bbbf4SAndreas Gohr    if(!function_exists('imagepng')){
340dd7bbbf4SAndreas Gohr      $okay = false;
341dd7bbbf4SAndreas Gohr    }else{
342dd7bbbf4SAndreas Gohr      $okay =  imagepng($newimg, $to);
343dd7bbbf4SAndreas Gohr    }
344f62ea8a1Sandi  }elseif($ext == 'gif') {
345dd7bbbf4SAndreas Gohr    if(!function_exists('imagegif')){
346dd7bbbf4SAndreas Gohr      $okay = false;
347dd7bbbf4SAndreas Gohr    }else{
348dd7bbbf4SAndreas Gohr      $okay = imagegif($newimg, $to);
349dd7bbbf4SAndreas Gohr    }
350f62ea8a1Sandi  }
351f62ea8a1Sandi
352dd7bbbf4SAndreas Gohr  // destroy GD image ressources
353dd7bbbf4SAndreas Gohr  if($image) imagedestroy($image);
354dd7bbbf4SAndreas Gohr  if($newimg) imagedestroy($newimg);
355dd7bbbf4SAndreas Gohr
356dd7bbbf4SAndreas Gohr  return $okay;
357f62ea8a1Sandi}
358f62ea8a1Sandi
3594e406776SAndreas Gohr/**
3604e406776SAndreas Gohr * Checks if the given amount of memory is available
3614e406776SAndreas Gohr *
3624e406776SAndreas Gohr * If the memory_get_usage() function is not available the
3634e406776SAndreas Gohr * function just assumes $used bytes of already allocated memory
3644e406776SAndreas Gohr *
3654e406776SAndreas Gohr * @param  int $mem  Size of memory you want to allocate in bytes
3664e406776SAndreas Gohr * @param  int $used already allocated memory (see above)
3674e406776SAndreas Gohr * @author Filip Oscadal <webmaster@illusionsoftworks.cz>
3684e406776SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
3694e406776SAndreas Gohr */
3704e406776SAndreas Gohrfunction is_mem_available($mem,$bytes=1048576){
3714e406776SAndreas Gohr  $limit = trim(ini_get('memory_limit'));
3724e406776SAndreas Gohr  if(empty($limit)) return true; // no limit set!
3734e406776SAndreas Gohr
3744e406776SAndreas Gohr  // parse limit to bytes
3754e406776SAndreas Gohr  $unit = strtolower(substr($limit,-1));
3764e406776SAndreas Gohr  switch($unit){
3774e406776SAndreas Gohr    case 'g':
3784e406776SAndreas Gohr      $limit = substr($limit,0,-1);
3794e406776SAndreas Gohr      $limit *= 1024*1024*1024;
3804e406776SAndreas Gohr      break;
3814e406776SAndreas Gohr    case 'm':
3824e406776SAndreas Gohr      $limit = substr($limit,0,-1);
3834e406776SAndreas Gohr      $limit *= 1024*1024;
3844e406776SAndreas Gohr      break;
3854e406776SAndreas Gohr    case 'k':
3864e406776SAndreas Gohr      $limit = substr($limit,0,-1);
3874e406776SAndreas Gohr      $limit *= 1024;
3884e406776SAndreas Gohr      break;
3894e406776SAndreas Gohr  }
3904e406776SAndreas Gohr
3914e406776SAndreas Gohr  // get used memory if possible
3924e406776SAndreas Gohr  if(function_exists('memory_get_usage')){
3934e406776SAndreas Gohr    $used = memory_get_usage();
3944e406776SAndreas Gohr  }
3954e406776SAndreas Gohr
3964e406776SAndreas Gohr
3974e406776SAndreas Gohr  if($used+$mem > $limit){
3984e406776SAndreas Gohr    return false;
3994e406776SAndreas Gohr  }
4004e406776SAndreas Gohr
4014e406776SAndreas Gohr  return true;
4024e406776SAndreas Gohr}
403f62ea8a1Sandi
404f62ea8a1Sandi//Setup VIM: ex: et ts=2 enc=utf-8 :
405f62ea8a1Sandi?>
406