You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

209 lines
6.7 KiB

  1. <?php
  2. use dokuwiki\HTTP\Headers;
  3. use dokuwiki\Utf8\PhpString;
  4. /**
  5. * Functions used by lib/exe/fetch.php
  6. * (not included by other parts of dokuwiki)
  7. */
  8. /**
  9. * Set headers and send the file to the client
  10. *
  11. * The $cache parameter influences how long files may be kept in caches, the $public parameter
  12. * influences if this caching may happen in public proxis or in the browser cache only FS#2734
  13. *
  14. * This function will abort the current script when a 304 is sent or file sending is handled
  15. * through x-sendfile
  16. *
  17. * @param string $file local file to send
  18. * @param string $mime mime type of the file
  19. * @param bool $dl set to true to force a browser download
  20. * @param int $cache remaining cache time in seconds (-1 for $conf['cache'], 0 for no-cache)
  21. * @param bool $public is this a public ressource or a private one?
  22. * @param string $orig original file to send - the file name will be used for the Content-Disposition
  23. * @param array $csp The ContentSecurityPolicy to send
  24. * @author Andreas Gohr <andi@splitbrain.org>
  25. * @author Ben Coburn <btcoburn@silicodon.net>
  26. * @author Gerry Weissbach <dokuwiki@gammaproduction.de>
  27. *
  28. */
  29. function sendFile($file, $mime, $dl, $cache, $public = false, $orig = null, $csp = [])
  30. {
  31. global $conf;
  32. // send mime headers
  33. header("Content-Type: $mime");
  34. // send security policy if given
  35. if (!empty($csp)) Headers::contentSecurityPolicy($csp);
  36. // calculate cache times
  37. if ($cache == -1) {
  38. $maxage = max($conf['cachetime'], 3600); // cachetime or one hour
  39. $expires = time() + $maxage;
  40. } elseif ($cache > 0) {
  41. $maxage = $cache; // given time
  42. $expires = time() + $maxage;
  43. } else { // $cache == 0
  44. $maxage = 0;
  45. $expires = 0; // 1970-01-01
  46. }
  47. // smart http caching headers
  48. if ($maxage) {
  49. if ($public) {
  50. // cache publically
  51. header('Expires: ' . gmdate("D, d M Y H:i:s", $expires) . ' GMT');
  52. header('Cache-Control: public, proxy-revalidate, no-transform, max-age=' . $maxage);
  53. } else {
  54. // cache in browser
  55. header('Expires: ' . gmdate("D, d M Y H:i:s", $expires) . ' GMT');
  56. header('Cache-Control: private, no-transform, max-age=' . $maxage);
  57. }
  58. } else {
  59. // no cache at all
  60. header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
  61. header('Cache-Control: no-cache, no-transform');
  62. }
  63. //send important headers first, script stops here if '304 Not Modified' response
  64. $fmtime = @filemtime($file);
  65. http_conditionalRequest($fmtime);
  66. // Use the current $file if is $orig is not set.
  67. if ($orig == null) {
  68. $orig = $file;
  69. }
  70. //download or display?
  71. if ($dl) {
  72. header('Content-Disposition: attachment;' . rfc2231_encode(
  73. 'filename',
  74. PhpString::basename($orig)
  75. ) . ';');
  76. } else {
  77. header('Content-Disposition: inline;' . rfc2231_encode(
  78. 'filename',
  79. PhpString::basename($orig)
  80. ) . ';');
  81. }
  82. //use x-sendfile header to pass the delivery to compatible webservers
  83. http_sendfile($file);
  84. // send file contents
  85. $fp = @fopen($file, "rb");
  86. if ($fp) {
  87. http_rangeRequest($fp, filesize($file), $mime);
  88. } else {
  89. http_status(500);
  90. echo "Could not read $file - bad permissions?";
  91. }
  92. }
  93. /**
  94. * Try an rfc2231 compatible encoding. This ensures correct
  95. * interpretation of filenames outside of the ASCII set.
  96. * This seems to be needed for file names with e.g. umlauts that
  97. * would otherwise decode wrongly in IE.
  98. *
  99. * There is no additional checking, just the encoding and setting the key=value for usage in headers
  100. *
  101. * @author Gerry Weissbach <gerry.w@gammaproduction.de>
  102. * @param string $name name of the field to be set in the header() call
  103. * @param string $value value of the field to be set in the header() call
  104. * @param string $charset used charset for the encoding of value
  105. * @param string $lang language used.
  106. * @return string in the format " name=value" for values WITHOUT special characters
  107. * @return string in the format " name*=charset'lang'value" for values WITH special characters
  108. */
  109. function rfc2231_encode($name, $value, $charset = 'utf-8', $lang = 'en')
  110. {
  111. $internal = preg_replace_callback(
  112. '/[\x00-\x20*\'%()<>@,;:\\\\"\/[\]?=\x80-\xFF]/',
  113. static fn($match) => rawurlencode($match[0]),
  114. $value
  115. );
  116. if ($value != $internal) {
  117. return ' ' . $name . '*=' . $charset . "'" . $lang . "'" . $internal;
  118. } else {
  119. return ' ' . $name . '="' . $value . '"';
  120. }
  121. }
  122. /**
  123. * Check for media for preconditions and return correct status code
  124. *
  125. * READ: MEDIA, MIME, EXT, CACHE
  126. * WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE )
  127. *
  128. * @author Gerry Weissbach <gerry.w@gammaproduction.de>
  129. *
  130. * @param string $media reference to the media id
  131. * @param string $file reference to the file variable
  132. * @param string $rev
  133. * @param int $width
  134. * @param int $height
  135. * @return array as array(STATUS, STATUSMESSAGE)
  136. */
  137. function checkFileStatus(&$media, &$file, $rev = '', $width = 0, $height = 0)
  138. {
  139. global $MIME, $EXT, $CACHE, $INPUT;
  140. //media to local file
  141. if (media_isexternal($media)) {
  142. //check token for external image and additional for resized and cached images
  143. if (media_get_token($media, $width, $height) !== $INPUT->str('tok')) {
  144. return [412, 'Precondition Failed'];
  145. }
  146. //handle external images
  147. if (str_starts_with($MIME, 'image/')) $file = media_get_from_URL($media, $EXT, $CACHE);
  148. if (!$file) {
  149. //download failed - redirect to original URL
  150. return [302, $media];
  151. }
  152. } else {
  153. $media = cleanID($media);
  154. if (empty($media)) {
  155. return [400, 'Bad request'];
  156. }
  157. // check token for resized images
  158. if (($width || $height) && media_get_token($media, $width, $height) !== $INPUT->str('tok')) {
  159. return [412, 'Precondition Failed'];
  160. }
  161. //check permissions (namespace only)
  162. if (auth_quickaclcheck(getNS($media) . ':X') < AUTH_READ) {
  163. return [403, 'Forbidden'];
  164. }
  165. $file = mediaFN($media, $rev);
  166. }
  167. //check file existance
  168. if (!file_exists($file)) {
  169. return [404, 'Not Found'];
  170. }
  171. return [200, null];
  172. }
  173. /**
  174. * Returns the wanted cachetime in seconds
  175. *
  176. * Resolves named constants
  177. *
  178. * @author Andreas Gohr <andi@splitbrain.org>
  179. *
  180. * @param string $cache
  181. * @return int cachetime in seconds
  182. */
  183. function calc_cache($cache)
  184. {
  185. global $conf;
  186. if (strtolower($cache) == 'nocache') return 0; //never cache
  187. if (strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache
  188. return -1; //cache endless
  189. }