xref: /dokuwiki/inc/fetch.functions.php (revision 32c584aadfbf4d297dbea400e69a0255614ff542)
17fb7960fSChristopher Smith<?php
27fb7960fSChristopher Smith/**
37fb7960fSChristopher Smith * Functions used by lib/exe/fetch.php
47fb7960fSChristopher Smith * (not included by other parts of dokuwiki)
57fb7960fSChristopher Smith */
67fb7960fSChristopher Smith
77fb7960fSChristopher Smith/**
87fb7960fSChristopher Smith * Set headers and send the file to the client
97fb7960fSChristopher Smith *
107fb7960fSChristopher Smith * The $cache parameter influences how long files may be kept in caches, the $public parameter
117fb7960fSChristopher Smith * influences if this caching may happen in public proxis or in the browser cache only FS#2734
127fb7960fSChristopher Smith *
137fb7960fSChristopher Smith * This function will abort the current script when a 304 is sent or file sending is handled
147fb7960fSChristopher Smith * through x-sendfile
157fb7960fSChristopher Smith *
167fb7960fSChristopher Smith * @author Andreas Gohr <andi@splitbrain.org>
177fb7960fSChristopher Smith * @author Ben Coburn <btcoburn@silicodon.net>
187fb7960fSChristopher Smith * @param string $file   local file to send
19*32c584aaSGerry Weißbach * @param string $orig   original file to send - the file name will be used for the Content-Disposition
207fb7960fSChristopher Smith * @param string $mime   mime type of the file
217fb7960fSChristopher Smith * @param bool   $dl     set to true to force a browser download
227fb7960fSChristopher Smith * @param int    $cache  remaining cache time in seconds (-1 for $conf['cache'], 0 for no-cache)
237fb7960fSChristopher Smith * @param bool   $public is this a public ressource or a private one?
247fb7960fSChristopher Smith */
25*32c584aaSGerry Weißbachfunction sendFile($file, $orig, $mime, $dl, $cache, $public = false) {
267fb7960fSChristopher Smith    global $conf;
277fb7960fSChristopher Smith    // send mime headers
287fb7960fSChristopher Smith    header("Content-Type: $mime");
297fb7960fSChristopher Smith
307fb7960fSChristopher Smith    // calculate cache times
317fb7960fSChristopher Smith    if($cache == -1) {
327fb7960fSChristopher Smith        $maxage  = max($conf['cachetime'], 3600); // cachetime or one hour
337fb7960fSChristopher Smith        $expires = time() + $maxage;
347fb7960fSChristopher Smith    } else if($cache > 0) {
357fb7960fSChristopher Smith        $maxage  = $cache; // given time
367fb7960fSChristopher Smith        $expires = time() + $maxage;
377fb7960fSChristopher Smith    } else { // $cache == 0
387fb7960fSChristopher Smith        $maxage  = 0;
397fb7960fSChristopher Smith        $expires = 0; // 1970-01-01
407fb7960fSChristopher Smith    }
417fb7960fSChristopher Smith
427fb7960fSChristopher Smith    // smart http caching headers
437fb7960fSChristopher Smith    if($maxage) {
447fb7960fSChristopher Smith        if($public) {
457fb7960fSChristopher Smith            // cache publically
467fb7960fSChristopher Smith            header('Expires: '.gmdate("D, d M Y H:i:s", $expires).' GMT');
477fb7960fSChristopher Smith            header('Cache-Control: public, proxy-revalidate, no-transform, max-age='.$maxage);
487fb7960fSChristopher Smith            header('Pragma: public');
497fb7960fSChristopher Smith        } else {
507fb7960fSChristopher Smith            // cache in browser
517fb7960fSChristopher Smith            header('Expires: '.gmdate("D, d M Y H:i:s", $expires).' GMT');
527fb7960fSChristopher Smith            header('Cache-Control: private, no-transform, max-age='.$maxage);
537fb7960fSChristopher Smith            header('Pragma: no-cache');
547fb7960fSChristopher Smith        }
557fb7960fSChristopher Smith    } else {
567fb7960fSChristopher Smith        // no cache at all
577fb7960fSChristopher Smith        header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
587fb7960fSChristopher Smith        header('Cache-Control: no-cache, no-transform');
597fb7960fSChristopher Smith        header('Pragma: no-cache');
607fb7960fSChristopher Smith    }
617fb7960fSChristopher Smith
627fb7960fSChristopher Smith    //send important headers first, script stops here if '304 Not Modified' response
637fb7960fSChristopher Smith    $fmtime = @filemtime($file);
647fb7960fSChristopher Smith    http_conditionalRequest($fmtime);
657fb7960fSChristopher Smith
667fb7960fSChristopher Smith    //download or display?
677fb7960fSChristopher Smith    if($dl) {
68*32c584aaSGerry Weißbach        header('Content-Disposition: attachment; filename="'.utf8_basename($orig).'";');
697fb7960fSChristopher Smith    } else {
70*32c584aaSGerry Weißbach        header('Content-Disposition: inline; filename="'.utf8_basename($orig).'";');
717fb7960fSChristopher Smith    }
727fb7960fSChristopher Smith
737fb7960fSChristopher Smith    //use x-sendfile header to pass the delivery to compatible webservers
747fb7960fSChristopher Smith    if(http_sendfile($file)) exit;
757fb7960fSChristopher Smith
767fb7960fSChristopher Smith    // send file contents
777fb7960fSChristopher Smith    $fp = @fopen($file, "rb");
787fb7960fSChristopher Smith    if($fp) {
797fb7960fSChristopher Smith        http_rangeRequest($fp, filesize($file), $mime);
807fb7960fSChristopher Smith    } else {
817fb7960fSChristopher Smith        http_status(500);
827fb7960fSChristopher Smith        print "Could not read $file - bad permissions?";
837fb7960fSChristopher Smith    }
847fb7960fSChristopher Smith}
857fb7960fSChristopher Smith
867fb7960fSChristopher Smith/**
877fb7960fSChristopher Smith * Check for media for preconditions and return correct status code
887fb7960fSChristopher Smith *
897fb7960fSChristopher Smith * READ: MEDIA, MIME, EXT, CACHE
907fb7960fSChristopher Smith * WRITE: MEDIA, FILE, array( STATUS, STATUSMESSAGE )
917fb7960fSChristopher Smith *
927fb7960fSChristopher Smith * @author Gerry Weissbach <gerry.w@gammaproduction.de>
93f481fb8cSKlap-in * @param string $media  reference to the media id
94f481fb8cSKlap-in * @param string $file   reference to the file variable
95f481fb8cSKlap-in * @param string $rev
96f481fb8cSKlap-in * @param int    $width
97f481fb8cSKlap-in * @param int    $height
98f481fb8cSKlap-in * @return array(STATUS, STATUSMESSAGE)
997fb7960fSChristopher Smith */
1007fb7960fSChristopher Smithfunction checkFileStatus(&$media, &$file, $rev = '', $width=0, $height=0) {
1017fb7960fSChristopher Smith    global $MIME, $EXT, $CACHE, $INPUT;
1027fb7960fSChristopher Smith
1037fb7960fSChristopher Smith    //media to local file
1043e7e6067SKlap-in    if(media_isexternal($media)) {
105cc036f74SKlap-in        //check token for external image and additional for resized and cached images
106cc036f74SKlap-in        if(media_get_token($media, $width, $height) !== $INPUT->str('tok')) {
1077fb7960fSChristopher Smith            return array(412, 'Precondition Failed');
1087fb7960fSChristopher Smith        }
1097fb7960fSChristopher Smith        //handle external images
1107fb7960fSChristopher Smith        if(strncmp($MIME, 'image/', 6) == 0) $file = media_get_from_URL($media, $EXT, $CACHE);
1117fb7960fSChristopher Smith        if(!$file) {
1127fb7960fSChristopher Smith            //download failed - redirect to original URL
1137fb7960fSChristopher Smith            return array(302, $media);
1147fb7960fSChristopher Smith        }
1157fb7960fSChristopher Smith    } else {
1167fb7960fSChristopher Smith        $media = cleanID($media);
1177fb7960fSChristopher Smith        if(empty($media)) {
1187fb7960fSChristopher Smith            return array(400, 'Bad request');
1197fb7960fSChristopher Smith        }
1207fb7960fSChristopher Smith        // check token for resized images
1217fb7960fSChristopher Smith        if (($width || $height) && media_get_token($media, $width, $height) !== $INPUT->str('tok')) {
1227fb7960fSChristopher Smith            return array(412, 'Precondition Failed');
1237fb7960fSChristopher Smith        }
1247fb7960fSChristopher Smith
1257fb7960fSChristopher Smith        //check permissions (namespace only)
1267fb7960fSChristopher Smith        if(auth_quickaclcheck(getNS($media).':X') < AUTH_READ) {
1277fb7960fSChristopher Smith            return array(403, 'Forbidden');
1287fb7960fSChristopher Smith        }
1297fb7960fSChristopher Smith        $file = mediaFN($media, $rev);
1307fb7960fSChristopher Smith    }
1317fb7960fSChristopher Smith
1327fb7960fSChristopher Smith    //check file existance
1337fb7960fSChristopher Smith    if(!@file_exists($file)) {
1347fb7960fSChristopher Smith        return array(404, 'Not Found');
1357fb7960fSChristopher Smith    }
1367fb7960fSChristopher Smith
1377fb7960fSChristopher Smith    return array(200, null);
1387fb7960fSChristopher Smith}
1397fb7960fSChristopher Smith
1407fb7960fSChristopher Smith/**
1417fb7960fSChristopher Smith * Returns the wanted cachetime in seconds
1427fb7960fSChristopher Smith *
1437fb7960fSChristopher Smith * Resolves named constants
1447fb7960fSChristopher Smith *
1457fb7960fSChristopher Smith * @author  Andreas Gohr <andi@splitbrain.org>
1467fb7960fSChristopher Smith */
1477fb7960fSChristopher Smithfunction calc_cache($cache) {
1487fb7960fSChristopher Smith    global $conf;
1497fb7960fSChristopher Smith
1507fb7960fSChristopher Smith    if(strtolower($cache) == 'nocache') return 0; //never cache
1517fb7960fSChristopher Smith    if(strtolower($cache) == 'recache') return $conf['cachetime']; //use standard cache
1527fb7960fSChristopher Smith    return -1; //cache endless
1537fb7960fSChristopher Smith}
154