[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/inc/ -> fetch.functions.php (source)

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