| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace Wikimedia\FileBackend; |
| 24 |
|
| 25 |
use HttpStatus; |
| 26 |
use Wikimedia\AtEase\AtEase; |
| 27 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class HTTPFileStreamer { |
| 35 |
|
| 36 |
protected $path; |
| 37 |
|
| 38 |
protected $obResetFunc; |
| 39 |
|
| 40 |
protected $streamMimeFunc; |
| 41 |
|
| 42 |
protected $headerFunc; |
| 43 |
|
| 44 |
|
| 45 |
public const STREAM_HEADLESS = 1; |
| 46 |
|
| 47 |
public const STREAM_ALLOW_OB = 2; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static function preprocessHeaders( $headers ) { |
| 57 |
$rawHeaders = []; |
| 58 |
$optHeaders = []; |
| 59 |
foreach ( $headers as $name => $header ) { |
| 60 |
$nameLower = strtolower( $name ); |
| 61 |
if ( in_array( $nameLower, [ 'range', 'if-modified-since' ], true ) ) { |
| 62 |
$optHeaders[$nameLower] = $header; |
| 63 |
} else { |
| 64 |
$rawHeaders[] = "$name: $header"; |
| 65 |
} |
| 66 |
} |
| 67 |
return [ $rawHeaders, $optHeaders ]; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function __construct( $path, array $params = [] ) { |
| 78 |
$this->path = $path; |
| 79 |
|
| 80 |
$this->obResetFunc = $params['obResetFunc'] ?? |
| 81 |
[ __CLASS__, 'resetOutputBuffers' ]; |
| 82 |
|
| 83 |
$this->streamMimeFunc = $params['streamMimeFunc'] ?? |
| 84 |
[ __CLASS__, 'contentTypeFromPath' ]; |
| 85 |
|
| 86 |
$this->headerFunc = $params['headerFunc'] ?? 'header'; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function stream( |
| 101 |
$headers = [], $sendErrors = true, $optHeaders = [], $flags = 0 |
| 102 |
) { |
| 103 |
$headless = ( $flags & self::STREAM_HEADLESS ); |
| 104 |
|
| 105 |
|
| 106 |
if ( $headers && headers_sent() ) { |
| 107 |
echo "Headers already sent, terminating.\n"; |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
$headerFunc = $headless |
| 112 |
? static function ( $header ) { |
| 113 |
|
| 114 |
} |
| 115 |
: [ $this, 'header' ]; |
| 116 |
|
| 117 |
AtEase::suppressWarnings(); |
| 118 |
$info = stat( $this->path ); |
| 119 |
AtEase::restoreWarnings(); |
| 120 |
|
| 121 |
if ( !is_array( $info ) ) { |
| 122 |
if ( $sendErrors ) { |
| 123 |
self::send404Message( $this->path, $flags ); |
| 124 |
} |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
$mtimeCT = new ConvertibleTimestamp( $info['mtime'] ); |
| 130 |
$headerFunc( 'Last-Modified: ' . $mtimeCT->getTimestamp( TS_RFC2822 ) ); |
| 131 |
|
| 132 |
if ( ( $flags & self::STREAM_ALLOW_OB ) == 0 ) { |
| 133 |
call_user_func( $this->obResetFunc ); |
| 134 |
} |
| 135 |
|
| 136 |
$type = call_user_func( $this->streamMimeFunc, $this->path ); |
| 137 |
if ( $type && $type != 'unknown/unknown' ) { |
| 138 |
$headerFunc( "Content-type: $type" ); |
| 139 |
} else { |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
$headerFunc( 'Content-type: application/x-wiki' ); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
if ( isset( $optHeaders['if-modified-since'] ) ) { |
| 149 |
$modsince = preg_replace( '/;.*$/', '', $optHeaders['if-modified-since'] ); |
| 150 |
if ( $mtimeCT->getTimestamp( TS_UNIX ) <= strtotime( $modsince ) ) { |
| 151 |
ini_set( 'zlib.output_compression', 0 ); |
| 152 |
$headerFunc( 304 ); |
| 153 |
return true; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
foreach ( $headers as $header ) { |
| 159 |
$headerFunc( $header ); |
| 160 |
} |
| 161 |
|
| 162 |
if ( isset( $optHeaders['range'] ) ) { |
| 163 |
$range = self::parseRange( $optHeaders['range'], $info['size'] ); |
| 164 |
if ( is_array( $range ) ) { |
| 165 |
$headerFunc( 206 ); |
| 166 |
$headerFunc( 'Content-Length: ' . $range[2] ); |
| 167 |
$headerFunc( "Content-Range: bytes {$range[0]}-{$range[1]}/{$info['size']}" ); |
| 168 |
} elseif ( $range === 'invalid' ) { |
| 169 |
if ( $sendErrors ) { |
| 170 |
$headerFunc( 416 ); |
| 171 |
$headerFunc( 'Cache-Control: no-cache' ); |
| 172 |
$headerFunc( 'Content-Type: text/html; charset=utf-8' ); |
| 173 |
$headerFunc( 'Content-Range: bytes */' . $info['size'] ); |
| 174 |
} |
| 175 |
return false; |
| 176 |
} else { |
| 177 |
$range = null; |
| 178 |
$headerFunc( 'Content-Length: ' . $info['size'] ); |
| 179 |
} |
| 180 |
} else { |
| 181 |
$range = null; |
| 182 |
$headerFunc( 'Content-Length: ' . $info['size'] ); |
| 183 |
} |
| 184 |
|
| 185 |
if ( is_array( $range ) ) { |
| 186 |
$handle = fopen( $this->path, 'rb' ); |
| 187 |
if ( $handle ) { |
| 188 |
$ok = true; |
| 189 |
fseek( $handle, $range[0] ); |
| 190 |
$remaining = $range[2]; |
| 191 |
while ( $remaining > 0 && $ok ) { |
| 192 |
$bytes = min( $remaining, 8 * 1024 ); |
| 193 |
$data = fread( $handle, $bytes ); |
| 194 |
$remaining -= $bytes; |
| 195 |
$ok = ( $data !== false ); |
| 196 |
print $data; |
| 197 |
} |
| 198 |
} else { |
| 199 |
return false; |
| 200 |
} |
| 201 |
} else { |
| 202 |
return readfile( $this->path ) !== false; |
| 203 |
} |
| 204 |
|
| 205 |
return true; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public static function send404Message( $fname, $flags = 0 ) { |
| 216 |
if ( ( $flags & self::STREAM_HEADLESS ) == 0 ) { |
| 217 |
HttpStatus::header( 404 ); |
| 218 |
header( 'Cache-Control: no-cache' ); |
| 219 |
header( 'Content-Type: text/html; charset=utf-8' ); |
| 220 |
} |
| 221 |
$encFile = htmlspecialchars( $fname ); |
| 222 |
$encScript = htmlspecialchars( $_SERVER['SCRIPT_NAME'] ); |
| 223 |
echo "<!DOCTYPE html><html><body> |
| 224 |
<h1>File not found</h1> |
| 225 |
<p>Although this PHP script ($encScript) exists, the file requested for output |
| 226 |
($encFile) does not.</p> |
| 227 |
</body></html> |
| 228 |
"; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public static function parseRange( $range, $size ) { |
| 240 |
$m = []; |
| 241 |
if ( preg_match( '#^bytes=(\d*)-(\d*)$#', $range, $m ) ) { |
| 242 |
[ , $start, $end ] = $m; |
| 243 |
if ( $start === '' && $end === '' ) { |
| 244 |
$absRange = [ 0, $size - 1 ]; |
| 245 |
} elseif ( $start === '' ) { |
| 246 |
$absRange = [ $size - (int)$end, $size - 1 ]; |
| 247 |
} elseif ( $end === '' ) { |
| 248 |
$absRange = [ (int)$start, $size - 1 ]; |
| 249 |
} else { |
| 250 |
$absRange = [ (int)$start, (int)$end ]; |
| 251 |
} |
| 252 |
if ( $absRange[0] >= 0 && $absRange[1] >= $absRange[0] ) { |
| 253 |
if ( $absRange[0] < $size ) { |
| 254 |
$absRange[1] = min( $absRange[1], $size - 1 ); |
| 255 |
$absRange[2] = $absRange[1] - $absRange[0] + 1; |
| 256 |
return $absRange; |
| 257 |
} elseif ( $absRange[0] == 0 && $size == 0 ) { |
| 258 |
return 'unrecognized'; |
| 259 |
} |
| 260 |
} |
| 261 |
return 'invalid'; |
| 262 |
} |
| 263 |
return 'unrecognized'; |
| 264 |
} |
| 265 |
|
| 266 |
protected static function resetOutputBuffers() { |
| 267 |
while ( ob_get_status() ) { |
| 268 |
if ( !ob_end_clean() ) { |
| 269 |
|
| 270 |
|
| 271 |
break; |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
protected static function contentTypeFromPath( $filename ) { |
| 283 |
$ext = strrchr( $filename, '.' ); |
| 284 |
$ext = $ext ? strtolower( substr( $ext, 1 ) ) : ''; |
| 285 |
|
| 286 |
switch ( $ext ) { |
| 287 |
case 'gif': |
| 288 |
return 'image/gif'; |
| 289 |
case 'png': |
| 290 |
return 'image/png'; |
| 291 |
case 'jpg': |
| 292 |
case 'jpeg': |
| 293 |
return 'image/jpeg'; |
| 294 |
|
| 295 |
case 'webp': |
| 296 |
return 'image/webp'; |
| 297 |
} |
| 298 |
|
| 299 |
return 'unknown/unknown'; |
| 300 |
} |
| 301 |
|
| 302 |
private function header( $header ) { |
| 303 |
if ( is_int( $header ) ) { |
| 304 |
$header = HttpStatus::getHeader( $header ); |
| 305 |
} |
| 306 |
|
| 307 |
( $this->headerFunc )( $header ); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
class_alias( HTTPFileStreamer::class, 'HTTPFileStreamer' ); |