xref: /dokuwiki/inc/fetch.functions.php (revision dccd6b2bba7367e4d1d2d7aa84c9f9d15584b593)
17fb7960fSChristopher Smith<?php
224870174SAndreas Gohruse dokuwiki\HTTP\Headers;
324870174SAndreas Gohruse dokuwiki\Utf8\PhpString;
47fb7960fSChristopher Smith/**
57fb7960fSChristopher Smith * Functions used by lib/exe/fetch.php
67fb7960fSChristopher Smith * (not included by other parts of dokuwiki)
77fb7960fSChristopher Smith */
87fb7960fSChristopher Smith
97fb7960fSChristopher Smith/**
107fb7960fSChristopher Smith * Set headers and send the file to the client
117fb7960fSChristopher Smith *
127fb7960fSChristopher Smith * The $cache parameter influences how long files may be kept in caches, the $public parameter
137fb7960fSChristopher Smith * influences if this caching may happen in public proxis or in the browser cache only FS#2734
147fb7960fSChristopher Smith *
157fb7960fSChristopher Smith * This function will abort the current script when a 304 is sent or file sending is handled
167fb7960fSChristopher Smith * through x-sendfile
177fb7960fSChristopher Smith *
187fb7960fSChristopher Smith * @param string $file local file to send
197fb7960fSChristopher Smith * @param string $mime mime type of the file
207fb7960fSChristopher Smith * @param bool $dl set to true to force a browser download
217fb7960fSChristopher Smith * @param int $cache remaining cache time in seconds (-1 for $conf['cache'], 0 for no-cache)
227fb7960fSChristopher Smith * @param bool $public is this a public ressource or a private one?
232fd6745dSGerry Weißbach * @param string $orig original file to send - the file name will be used for the Content-Disposition
246cda96e3SAndreas Gohr * @param array $csp The ContentSecurityPolicy to send
256cda96e3SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
266cda96e3SAndreas Gohr * @author Ben Coburn <btcoburn@silicodon.net>
276cda96e3SAndreas Gohr * @author Gerry Weissbach <dokuwiki@gammaproduction.de>
286cda96e3SAndreas Gohr *
297fb7960fSChristopher Smith */
30d868eb89SAndreas Gohrfunction sendFile($file, $mime, $dl, $cache, $public = false, $orig = null, $csp = [])
31d868eb89SAndreas Gohr{
327fb7960fSChristopher Smith    global $conf;
337fb7960fSChristopher Smith    // send mime headers
347fb7960fSChristopher Smith    header("Content-Type: $mime");
357fb7960fSChristopher Smith
366cda96e3SAndreas Gohr    // send security policy if given
3724870174SAndreas Gohr    if (!empty($csp)) Headers::contentSecurityPolicy($csp);
386cda96e3SAndreas Gohr
397fb7960fSChristopher Smith    // calculate cache times
407fb7960fSChristopher Smith    if ($cache == -1) {
417fb7960fSChristopher Smith        $maxage  = max($conf['cachetime'], 3600); // cachetime or one hour
427fb7960fSChristopher Smith        $expires = time() + $maxage;
437fb7960fSChristopher Smith    } elseif ($cache > 0) {
447fb7960fSChristopher Smith        $maxage  = $cache; // given time
457fb7960fSChristopher Smith        $expires = time() + $maxage;
467fb7960fSChristopher Smith    } else { // $cache == 0
477fb7960fSChristopher Smith        $maxage  = 0;
487fb7960fSChristopher Smith        $expires = 0; // 1970-01-01
497fb7960fSChristopher Smith    }
507fb7960fSChristopher Smith
517fb7960fSChristopher Smith    // smart http caching headers
527fb7960fSChristopher Smith    if($maxage) {
537fb7960fSChristopher Smith        if($public) {
547fb7960fSChristopher Smith            // cache publically
557fb7960fSChristopher Smith            header('Expires: '.gmdate("D, d M Y H:i:s", $expires).' GMT');
567fb7960fSChristopher Smith            header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.$maxage);
577fb7960fSChristopher Smith        } else {
587fb7960fSChristopher Smith            // cache in browser
597fb7960fSChristopher Smith            header('Expires: '.gmdate("D, d M Y H:i:s", $expires).' GMT');
607fb7960fSChristopher Smith            header('Cache-Control: private, no-transform, max-age='.$maxage);
617fb7960fSChristopher Smith        }
627fb7960fSChristopher Smith    } else {
637fb7960fSChristopher Smith        // no cache at all
647fb7960fSChristopher Smith        header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
657fb7960fSChristopher Smith        header('Cache-Control: no-cache, no-transform');
667fb7960fSChristopher Smith    }
677fb7960fSChristopher Smith
687fb7960fSChristopher Smith    //send important headers first, script stops here if '304 Not Modified' response
697fb7960fSChristopher Smith    $fmtime = @filemtime($file);
707fb7960fSChristopher Smith    http_conditionalRequest($fmtime);
717fb7960fSChristopher Smith
722fd6745dSGerry Weißbach    // Use the current $file if is $orig is not set.
732fd6745dSGerry Weißbach    if ( $orig == null ) {
742fd6745dSGerry Weißbach        $orig = $file;
752fd6745dSGerry Weißbach    }
762fd6745dSGerry Weißbach
777fb7960fSChristopher Smith    //download or display?
787fb7960fSChristopher Smith    if ($dl) {
796ce3e5f8SAndreas Gohr        header('Content-Disposition: attachment;' . rfc2231_encode(
80*dccd6b2bSAndreas Gohr            'filename',
81*dccd6b2bSAndreas Gohr            PhpString::basename($orig)
82*dccd6b2bSAndreas Gohr        ) . ';');
837fb7960fSChristopher Smith    } else {
846ce3e5f8SAndreas Gohr        header('Content-Disposition: inline;' . rfc2231_encode(
85*dccd6b2bSAndreas Gohr            'filename',
86*dccd6b2bSAndreas Gohr            PhpString::basename($orig)
87*dccd6b2bSAndreas Gohr        ) . ';');
887fb7960fSChristopher Smith    }
897fb7960fSChristopher Smith
907fb7960fSChristopher Smith    //use x-sendfile header to pass the delivery to compatible webservers
9140e0b444SDominik Eckelmann    http_sendfile($file);
927fb7960fSChristopher Smith
937fb7960fSChristopher Smith    // send file contents
947fb7960fSChristopher Smith    $fp = @fopen($file, "rb");
957fb7960fSChristopher Smith    if($fp) {
967fb7960fSChristopher Smith        http_rangeRequest($fp, filesize($file), $mime);
977fb7960fSChristopher Smith    } else {
987fb7960fSChristopher Smith        http_status(500);
997fb7960fSChristopher Smith        print "Could not read $file - bad permissions?";
1007fb7960fSChristopher Smith    }
1017fb7960fSChristopher Smith}
1027fb7960fSChristopher Smith
1037fb7960fSChristopher Smith/**
10404585e6cSGerry Weißbach * Try an rfc2231 compatible encoding. This ensures correct
10504585e6cSGerry Weißbach * interpretation of filenames outside of the ASCII set.
10604585e6cSGerry Weißbach * This seems to be needed for file names with e.g. umlauts that
10704585e6cSGerry Weißbach * would otherwise decode wrongly in IE.
10804585e6cSGerry Weißbach *
10904585e6cSGerry Weißbach * There is no additional checking, just the encoding and setting the key=value for usage in headers
11004585e6cSGerry Weißbach *
11104585e6cSGerry Weißbach * @author Gerry Weissbach <gerry.w@gammaproduction.de>
11204585e6cSGerry Weißbach * @param string $name      name of the field to be set in the header() call
11304585e6cSGerry Weißbach * @param string $value     value of the field to be set in the header() call
11404585e6cSGerry Weißbach * @param string $charset   used charset for the encoding of value
11504585e6cSGerry Weißbach * @param string $lang      language used.
11604585e6cSGerry Weißbach * @return string           in the format " name=value" for values WITHOUT special characters
11704585e6cSGerry Weißbach * @return string           in the format " name*=charset'lang'value" for values WITH special characters
11804585e6cSGerry Weißbach */
119d868eb89SAndreas Gohrfunction rfc2231_encode($name, $value, $charset = 'utf-8', $lang = 'en')
120d868eb89SAndreas Gohr{
12164159a61SAndreas Gohr    $internal = preg_replace_callback(
12264159a61SAndreas Gohr        '/[\x00-\x20*\'%()<>@,;:\\\\"\/[\]?=\x80-\xFF]/',
12324870174SAndreas Gohr        static fn($match) => rawurlencode($match[0]),
12464159a61SAndreas Gohr        $value
12564159a61SAndreas Gohr    );
12604585e6cSGerry Weißbach    if ( $value != $internal ) {
12704585e6cSGerry Weißbach        return ' '.$name.'*='.$charset."'".$lang."'".$internal;
12804585e6cSGerry Weißbach    } else {
12904585e6cSGerry Weißbach        return ' '.$name.'="'.$value.'"';
13004585e6cSGerry Weißbach    }
13104585e6cSGerry Weißbach}
13204585e6cSGerry Weißbach
13304585e6cSGerry Weißbach/**
1347fb7960fSChristopher Smith * Check for media for preconditions and return correct status code
1357fb7960fSChristopher Smith *
1367fb7960fSChristopher Smith * READ: MEDIA, MIME, EXT, CACHE
1377fb7960fSChristopher Smith * WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE )
1387fb7960fSChristopher Smith *
1397fb7960fSChristopher Smith * @author Gerry Weissbach <gerry.w@gammaproduction.de>
14042ea7f44SGerrit Uitslag *
141f481fb8cSKlap-in * @param string $media  reference to the media id
142f481fb8cSKlap-in * @param string $file   reference to the file variable
143f481fb8cSKlap-in * @param string $rev
144f481fb8cSKlap-in * @param int    $width
145f481fb8cSKlap-in * @param int    $height
14642ea7f44SGerrit Uitslag * @return array as array(STATUS, STATUSMESSAGE)
1477fb7960fSChristopher Smith */
148d868eb89SAndreas Gohrfunction checkFileStatus(&$media, &$file, $rev = '', $width = 0, $height = 0)
149d868eb89SAndreas Gohr{
1507fb7960fSChristopher Smith    global $MIME, $EXT, $CACHE, $INPUT;
1517fb7960fSChristopher Smith
1527fb7960fSChristopher Smith    //media to local file
1533e7e6067SKlap-in    if(media_isexternal($media)) {
154cc036f74SKlap-in        //check token for external image and additional for resized and cached images
155cc036f74SKlap-in        if(media_get_token($media, $width, $height) !== $INPUT->str('tok')) {
15624870174SAndreas Gohr            return [412, 'Precondition Failed'];
1577fb7960fSChristopher Smith        }
1587fb7960fSChristopher Smith        //handle external images
1597fb7960fSChristopher Smith        if(strncmp($MIME, 'image/', 6) == 0) $file = media_get_from_URL($media, $EXT, $CACHE);
1607fb7960fSChristopher Smith        if(!$file) {
1617fb7960fSChristopher Smith            //download failed - redirect to original URL
16224870174SAndreas Gohr            return [302, $media];
1637fb7960fSChristopher Smith        }
1647fb7960fSChristopher Smith    } else {
1657fb7960fSChristopher Smith        $media = cleanID($media);
1667fb7960fSChristopher Smith        if(empty($media)) {
16724870174SAndreas Gohr            return [400, 'Bad request'];
1687fb7960fSChristopher Smith        }
1697fb7960fSChristopher Smith        // check token for resized images
1707fb7960fSChristopher Smith        if (($width || $height) && media_get_token($media, $width, $height) !== $INPUT->str('tok')) {
17124870174SAndreas Gohr            return [412, 'Precondition Failed'];
1727fb7960fSChristopher Smith        }
1737fb7960fSChristopher Smith
1747fb7960fSChristopher Smith        //check permissions (namespace only)
1757fb7960fSChristopher Smith        if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ) {
17624870174SAndreas Gohr            return [403, 'Forbidden'];
1777fb7960fSChristopher Smith        }
1787fb7960fSChristopher Smith        $file = mediaFN($media, $rev);
1797fb7960fSChristopher Smith    }
1807fb7960fSChristopher Smith
1817fb7960fSChristopher Smith    //check file existance
18279e79377SAndreas Gohr    if(!file_exists($file)) {
18324870174SAndreas Gohr        return [404, 'Not Found'];
1847fb7960fSChristopher Smith    }
1857fb7960fSChristopher Smith
18624870174SAndreas Gohr    return [200, null];
1877fb7960fSChristopher Smith}
1887fb7960fSChristopher Smith
1897fb7960fSChristopher Smith/**
1907fb7960fSChristopher Smith * Returns the wanted cachetime in seconds
1917fb7960fSChristopher Smith *
1927fb7960fSChristopher Smith * Resolves named constants
1937fb7960fSChristopher Smith *
1947fb7960fSChristopher Smith * @author  Andreas Gohr <andi@splitbrain.org>
19542ea7f44SGerrit Uitslag *
19642ea7f44SGerrit Uitslag * @param string $cache
19742ea7f44SGerrit Uitslag * @return int cachetime in seconds
1987fb7960fSChristopher Smith */
199d868eb89SAndreas Gohrfunction calc_cache($cache)
200d868eb89SAndreas Gohr{
2017fb7960fSChristopher Smith    global $conf;
2027fb7960fSChristopher Smith
2037fb7960fSChristopher Smith    if(strtolower($cache) == 'nocache') return 0; //never cache
2047fb7960fSChristopher Smith    if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache
2057fb7960fSChristopher Smith    return -1; //cache endless
2067fb7960fSChristopher Smith}
207