xref: /dokuwiki/inc/httputils.php (revision 758447cfa419f1a11bc022ef8168447992364e52)
1*758447cfSAndreas Gohr<?php
2*758447cfSAndreas Gohr/**
3*758447cfSAndreas Gohr * Utilities for handling HTTP related tasks
4*758447cfSAndreas Gohr *
5*758447cfSAndreas Gohr * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6*758447cfSAndreas Gohr * @author     Andreas Gohr <andi@splitbrain.org>
7*758447cfSAndreas Gohr */
8*758447cfSAndreas Gohr
9*758447cfSAndreas Gohrdefine('HTTP_MULTIPART_BOUNDARY','D0KuW1K1B0uNDARY');
10*758447cfSAndreas Gohrdefine('HTTP_HEADER_LF',"\r\n");
11*758447cfSAndreas Gohrdefine('HTTP_CHUNK_SIZE',16*1024);
12*758447cfSAndreas Gohr
13*758447cfSAndreas Gohr/**
14*758447cfSAndreas Gohr * Checks and sets HTTP headers for conditional HTTP requests
15*758447cfSAndreas Gohr *
16*758447cfSAndreas Gohr * @author   Simon Willison <swillison@gmail.com>
17*758447cfSAndreas Gohr * @link     http://simon.incutio.com/archive/2003/04/23/conditionalGet
18*758447cfSAndreas Gohr * @param    timestamp $timestamp lastmodified time of the cache file
19*758447cfSAndreas Gohr * @returns  void or exits with previously header() commands executed
20*758447cfSAndreas Gohr */
21*758447cfSAndreas Gohrfunction http_conditionalRequest($timestamp){
22*758447cfSAndreas Gohr  // A PHP implementation of conditional get, see
23*758447cfSAndreas Gohr  //   http://fishbowl.pastiche.org/archives/001132.html
24*758447cfSAndreas Gohr  $last_modified = substr(gmdate('r', $timestamp), 0, -5).'GMT';
25*758447cfSAndreas Gohr  $etag = '"'.md5($last_modified).'"';
26*758447cfSAndreas Gohr  // Send the headers
27*758447cfSAndreas Gohr  header("Last-Modified: $last_modified");
28*758447cfSAndreas Gohr  header("ETag: $etag");
29*758447cfSAndreas Gohr  // See if the client has provided the required headers
30*758447cfSAndreas Gohr  if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){
31*758447cfSAndreas Gohr    $if_modified_since = stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']);
32*758447cfSAndreas Gohr  }else{
33*758447cfSAndreas Gohr    $if_modified_since = false;
34*758447cfSAndreas Gohr  }
35*758447cfSAndreas Gohr
36*758447cfSAndreas Gohr  if (isset($_SERVER['HTTP_IF_NONE_MATCH'])){
37*758447cfSAndreas Gohr    $if_none_match = stripslashes($_SERVER['HTTP_IF_NONE_MATCH']);
38*758447cfSAndreas Gohr  }else{
39*758447cfSAndreas Gohr    $if_none_match = false;
40*758447cfSAndreas Gohr  }
41*758447cfSAndreas Gohr
42*758447cfSAndreas Gohr  if (!$if_modified_since && !$if_none_match){
43*758447cfSAndreas Gohr    return;
44*758447cfSAndreas Gohr  }
45*758447cfSAndreas Gohr
46*758447cfSAndreas Gohr  // At least one of the headers is there - check them
47*758447cfSAndreas Gohr  if ($if_none_match && $if_none_match != $etag) {
48*758447cfSAndreas Gohr    return; // etag is there but doesn't match
49*758447cfSAndreas Gohr  }
50*758447cfSAndreas Gohr
51*758447cfSAndreas Gohr  if ($if_modified_since && $if_modified_since != $last_modified) {
52*758447cfSAndreas Gohr    return; // if-modified-since is there but doesn't match
53*758447cfSAndreas Gohr  }
54*758447cfSAndreas Gohr
55*758447cfSAndreas Gohr  // Nothing has changed since their last request - serve a 304 and exit
56*758447cfSAndreas Gohr  header('HTTP/1.0 304 Not Modified');
57*758447cfSAndreas Gohr
58*758447cfSAndreas Gohr  // don't produce output, even if compression is on
59*758447cfSAndreas Gohr  ob_end_clean();
60*758447cfSAndreas Gohr  exit;
61*758447cfSAndreas Gohr}
62*758447cfSAndreas Gohr
63*758447cfSAndreas Gohr/**
64*758447cfSAndreas Gohr * Let the webserver send the given file vi x-sendfile method
65*758447cfSAndreas Gohr *
66*758447cfSAndreas Gohr * @author Chris Smith <chris.eureka@jalakai.co.uk>
67*758447cfSAndreas Gohr * @returns  void or exits with previously header() commands executed
68*758447cfSAndreas Gohr */
69*758447cfSAndreas Gohrfunction http_sendfile($file) {
70*758447cfSAndreas Gohr  global $conf;
71*758447cfSAndreas Gohr
72*758447cfSAndreas Gohr  //use x-sendfile header to pass the delivery to compatible webservers
73*758447cfSAndreas Gohr  if($conf['xsendfile'] == 1){
74*758447cfSAndreas Gohr    header("X-LIGHTTPD-send-file: $file");
75*758447cfSAndreas Gohr    ob_end_clean();
76*758447cfSAndreas Gohr    exit;
77*758447cfSAndreas Gohr  }elseif($conf['xsendfile'] == 2){
78*758447cfSAndreas Gohr    header("X-Sendfile: $file");
79*758447cfSAndreas Gohr    ob_end_clean();
80*758447cfSAndreas Gohr    exit;
81*758447cfSAndreas Gohr  }elseif($conf['xsendfile'] == 3){
82*758447cfSAndreas Gohr    header("X-Accel-Redirect: $file");
83*758447cfSAndreas Gohr    ob_end_clean();
84*758447cfSAndreas Gohr    exit;
85*758447cfSAndreas Gohr  }
86*758447cfSAndreas Gohr
87*758447cfSAndreas Gohr  return false;
88*758447cfSAndreas Gohr}
89*758447cfSAndreas Gohr
90*758447cfSAndreas Gohr/**
91*758447cfSAndreas Gohr * Send file contents supporting rangeRequests
92*758447cfSAndreas Gohr *
93*758447cfSAndreas Gohr * This function exits the running script
94*758447cfSAndreas Gohr *
95*758447cfSAndreas Gohr * @param ressource $fh - file handle for an already open file
96*758447cfSAndreas Gohr * @param int $size     - size of the whole file
97*758447cfSAndreas Gohr * @param int $mime     - MIME type of the file
98*758447cfSAndreas Gohr *
99*758447cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
100*758447cfSAndreas Gohr */
101*758447cfSAndreas Gohrfunction http_rangeRequest($fh,$size,$mime){
102*758447cfSAndreas Gohr    $ranges  = array();
103*758447cfSAndreas Gohr    $isrange = false;
104*758447cfSAndreas Gohr
105*758447cfSAndreas Gohr    header('Accept-Ranges: bytes');
106*758447cfSAndreas Gohr
107*758447cfSAndreas Gohr    if(!isset($_SERVER['HTTP_RANGE'])){
108*758447cfSAndreas Gohr        // no range requested - send the whole file
109*758447cfSAndreas Gohr        $ranges[] = array(0,$size,$size);
110*758447cfSAndreas Gohr    }else{
111*758447cfSAndreas Gohr        $t = explode('=', $_SERVER['HTTP_RANGE']);
112*758447cfSAndreas Gohr        if (!$t[0]=='bytes') {
113*758447cfSAndreas Gohr            // we only understand byte ranges - send the whole file
114*758447cfSAndreas Gohr            $ranges[] = array(0,$size,$size);
115*758447cfSAndreas Gohr        }else{
116*758447cfSAndreas Gohr            $isrange = true;
117*758447cfSAndreas Gohr            // handle multiple ranges
118*758447cfSAndreas Gohr            $r = explode(',',$t[1]);
119*758447cfSAndreas Gohr            foreach($r as $x){
120*758447cfSAndreas Gohr                $p = explode('-', $x);
121*758447cfSAndreas Gohr                $start = (int)$p[0];
122*758447cfSAndreas Gohr                $end   = (int)$p[1];
123*758447cfSAndreas Gohr                if (!$end) $end = $size - 1;
124*758447cfSAndreas Gohr                if ($start > $end || $start > $size || $end > $size){
125*758447cfSAndreas Gohr                    header('HTTP/1.1 416 Requested Range Not Satisfiable');
126*758447cfSAndreas Gohr                    print 'Bad Range Request!';
127*758447cfSAndreas Gohr                    exit;
128*758447cfSAndreas Gohr                }
129*758447cfSAndreas Gohr                $len = $end - $start + 1;
130*758447cfSAndreas Gohr                $ranges[] = array($start,$end,$len);
131*758447cfSAndreas Gohr            }
132*758447cfSAndreas Gohr        }
133*758447cfSAndreas Gohr    }
134*758447cfSAndreas Gohr    $parts = count($ranges);
135*758447cfSAndreas Gohr
136*758447cfSAndreas Gohr    // now send the type and length headers
137*758447cfSAndreas Gohr    if(!$isrange){
138*758447cfSAndreas Gohr        header("Content-Type: $mime",true);
139*758447cfSAndreas Gohr    }else{
140*758447cfSAndreas Gohr        header('HTTP/1.1 206 Partial Content');
141*758447cfSAndreas Gohr        if($parts == 1){
142*758447cfSAndreas Gohr            header("Content-Type: $mime",true);
143*758447cfSAndreas Gohr        }else{
144*758447cfSAndreas Gohr            header('Content-Type: multipart/byteranges; boundary='.HTTP_MULTIPART_BOUNDARY,true);
145*758447cfSAndreas Gohr        }
146*758447cfSAndreas Gohr    }
147*758447cfSAndreas Gohr
148*758447cfSAndreas Gohr    // send all ranges
149*758447cfSAndreas Gohr    for($i=0; $i<$parts; $i++){
150*758447cfSAndreas Gohr        list($start,$end,$len) = $ranges[$i];
151*758447cfSAndreas Gohr
152*758447cfSAndreas Gohr        // multipart or normal headers
153*758447cfSAndreas Gohr        if($parts > 1){
154*758447cfSAndreas Gohr            echo HTTP_HEADER_LF.'--'.HTTP_MULTIPART_BOUNDARY.HTTP_HEADER_LF;
155*758447cfSAndreas Gohr            echo "Content-Type: $mime".HTTP_HEADER_LF;
156*758447cfSAndreas Gohr            echo "Content-Range: bytes $start-$end/$size".HTTP_HEADER_LF;
157*758447cfSAndreas Gohr        }else{
158*758447cfSAndreas Gohr            header("Content-Length: $len");
159*758447cfSAndreas Gohr            if($isrange){
160*758447cfSAndreas Gohr                header("Content-Range: bytes $start-$end/$size");
161*758447cfSAndreas Gohr            }
162*758447cfSAndreas Gohr        }
163*758447cfSAndreas Gohr
164*758447cfSAndreas Gohr        // send file content
165*758447cfSAndreas Gohr        fseek($fh,$start); //seek to start of range
166*758447cfSAndreas Gohr        $chunk = ($len > HTTP_CHUNK_SIZE) ? HTTP_CHUNK_SIZE : $len;
167*758447cfSAndreas Gohr        while (!feof($fh) && $chunk > 0) {
168*758447cfSAndreas Gohr          @set_time_limit(30); // large files can take a lot of time
169*758447cfSAndreas Gohr          print fread($fh, $chunk);
170*758447cfSAndreas Gohr          flush();
171*758447cfSAndreas Gohr          $len -= $chunk;
172*758447cfSAndreas Gohr          $chunk = ($len > HTTP_CHUNK_SIZE) ? HTTP_CHUNK_SIZE : $len;
173*758447cfSAndreas Gohr        }
174*758447cfSAndreas Gohr    }
175*758447cfSAndreas Gohr    if($parts > 1){
176*758447cfSAndreas Gohr        echo HTTP_HEADER_LF.'--'.HTTP_MULTIPART_BOUNDARY.'--'.HTTP_HEADER_LF;
177*758447cfSAndreas Gohr    }
178*758447cfSAndreas Gohr
179*758447cfSAndreas Gohr    // everything should be done here, exit
180*758447cfSAndreas Gohr    exit;
181*758447cfSAndreas Gohr}
182*758447cfSAndreas Gohr
183*758447cfSAndreas Gohr/**
184*758447cfSAndreas Gohr * Check for a gzipped version and create if necessary
185*758447cfSAndreas Gohr *
186*758447cfSAndreas Gohr * return true if there exists a gzip version of the uncompressed file
187*758447cfSAndreas Gohr * (samepath/samefilename.sameext.gz) created after the uncompressed file
188*758447cfSAndreas Gohr *
189*758447cfSAndreas Gohr * @author Chris Smith <chris.eureka@jalakai.co.uk>
190*758447cfSAndreas Gohr */
191*758447cfSAndreas Gohrfunction http_gzip_valid($uncompressed_file) {
192*758447cfSAndreas Gohr    $gzip = $uncompressed_file.'.gz';
193*758447cfSAndreas Gohr    if (filemtime($gzip) < filemtime($uncompressed_file)) {    // filemtime returns false (0) if file doesn't exist
194*758447cfSAndreas Gohr        return copy($uncompressed_file, 'compress.zlib://'.$gzip);
195*758447cfSAndreas Gohr    }
196*758447cfSAndreas Gohr
197*758447cfSAndreas Gohr    return true;
198*758447cfSAndreas Gohr}
199