1<?php 2/** 3 * Functions used by lib/exe/fetch.php 4 * (not included by other parts of dokuwiki) 5 */ 6 7/** 8 * Set headers and send the file to the client 9 * 10 * The $cache parameter influences how long files may be kept in caches, the $public parameter 11 * influences if this caching may happen in public proxis or in the browser cache only FS#2734 12 * 13 * This function will abort the current script when a 304 is sent or file sending is handled 14 * through x-sendfile 15 * 16 * @author Andreas Gohr <andi@splitbrain.org> 17 * @author Ben Coburn <btcoburn@silicodon.net> 18 * @param string $file local file to send 19 * @param string $mime mime type of the file 20 * @param bool $dl set to true to force a browser download 21 * @param int $cache remaining cache time in seconds (-1 for $conf['cache'], 0 for no-cache) 22 * @param bool $public is this a public ressource or a private one? 23 */ 24function sendFile($file, $mime, $dl, $cache, $public = false) { 25 global $conf; 26 // send mime headers 27 header("Content-Type: $mime"); 28 29 // calculate cache times 30 if($cache == -1) { 31 $maxage = max($conf['cachetime'], 3600); // cachetime or one hour 32 $expires = time() + $maxage; 33 } else if($cache > 0) { 34 $maxage = $cache; // given time 35 $expires = time() + $maxage; 36 } else { // $cache == 0 37 $maxage = 0; 38 $expires = 0; // 1970-01-01 39 } 40 41 // smart http caching headers 42 if($maxage) { 43 if($public) { 44 // cache publically 45 header('Expires: '.gmdate("D, d M Y H:i:s", $expires).' GMT'); 46 header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.$maxage); 47 header('Pragma: public'); 48 } else { 49 // cache in browser 50 header('Expires: '.gmdate("D, d M Y H:i:s", $expires).' GMT'); 51 header('Cache-Control: private, no-transform, max-age='.$maxage); 52 header('Pragma: no-cache'); 53 } 54 } else { 55 // no cache at all 56 header('Expires: Thu, 01 Jan 1970 00:00:00 GMT'); 57 header('Cache-Control: no-cache, no-transform'); 58 header('Pragma: no-cache'); 59 } 60 61 //send important headers first, script stops here if '304 Not Modified' response 62 $fmtime = @filemtime($file); 63 http_conditionalRequest($fmtime); 64 65 //download or display? 66 if($dl) { 67 header('Content-Disposition: attachment; filename="'.utf8_basename($file).'";'); 68 } else { 69 header('Content-Disposition: inline; filename="'.utf8_basename($file).'";'); 70 } 71 72 //use x-sendfile header to pass the delivery to compatible webservers 73 if(http_sendfile($file)) exit; 74 75 // send file contents 76 $fp = @fopen($file, "rb"); 77 if($fp) { 78 http_rangeRequest($fp, filesize($file), $mime); 79 } else { 80 http_status(500); 81 print "Could not read $file - bad permissions?"; 82 } 83} 84 85/** 86 * Check for media for preconditions and return correct status code 87 * 88 * READ: MEDIA, MIME, EXT, CACHE 89 * WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE ) 90 * 91 * @author Gerry Weissbach <gerry.w@gammaproduction.de> 92 * @param string $media reference to the media id 93 * @param string $file reference to the file variable 94 * @param string $rev 95 * @param int $width 96 * @param int $height 97 * @return array(STATUS, STATUSMESSAGE) 98 */ 99function checkFileStatus(&$media, &$file, $rev = '', $width=0, $height=0) { 100 global $MIME, $EXT, $CACHE, $INPUT; 101 102 //media to local file 103 if(media_isexternal($media)) { 104 //check token for external image and additional for resized and cached images 105 if(media_get_token($media, $width, $height) !== $INPUT->str('tok')) { 106 return array(412, 'Precondition Failed'); 107 } 108 //handle external images 109 if(strncmp($MIME, 'image/', 6) == 0) $file = media_get_from_URL($media, $EXT, $CACHE); 110 if(!$file) { 111 //download failed - redirect to original URL 112 return array(302, $media); 113 } 114 } else { 115 $media = cleanID($media); 116 if(empty($media)) { 117 return array(400, 'Bad request'); 118 } 119 // check token for resized images 120 if (($width || $height) && media_get_token($media, $width, $height) !== $INPUT->str('tok')) { 121 return array(412, 'Precondition Failed'); 122 } 123 124 //check permissions (namespace only) 125 if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ) { 126 return array(403, 'Forbidden'); 127 } 128 $file = mediaFN($media, $rev); 129 } 130 131 //check file existance 132 if(!@file_exists($file)) { 133 return array(404, 'Not Found'); 134 } 135 136 return array(200, null); 137} 138 139/** 140 * Returns the wanted cachetime in seconds 141 * 142 * Resolves named constants 143 * 144 * @author Andreas Gohr <andi@splitbrain.org> 145 */ 146function calc_cache($cache) { 147 global $conf; 148 149 if(strtolower($cache) == 'nocache') return 0; //never cache 150 if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache 151 return -1; //cache endless 152} 153