| 497 | | function spitfile_with_mtime_check ($filepath, $mimetype) { |
|---|
| 498 | | |
|---|
| 499 | | if (is_file($filepath)) { |
|---|
| 500 | | $tstamp = filemtime($filepath); |
|---|
| 501 | | $filesize = filesize($filepath); |
|---|
| 502 | | $lm = gmdate("D, d M Y H:i:s", $tstamp) . " GMT"; |
|---|
| 503 | | $etag = md5($filepath . "_" . $tstamp . "_" . $mimetype . "_" . $filesize); |
|---|
| 504 | | header('ETag: "' . $etag . '"'); |
|---|
| | 497 | function spitfile_with_mtime_check ($filepath, $mimetype, $handler = "local") { |
|---|
| | 498 | |
|---|
| | 499 | if (is_file($filepath) || $handler != "local") { |
|---|
| | 500 | if ($handler == "local") { |
|---|
| | 501 | $tstamp = filemtime($filepath); |
|---|
| | 502 | $filesize = filesize($filepath); |
|---|
| | 503 | $lm = gmdate("D, d M Y H:i:s", $tstamp) . " GMT"; |
|---|
| | 504 | $etag = md5($filepath . "_" . $tstamp . "_" . $mimetype . "_" . $filesize); |
|---|
| | 505 | header('ETag: "' . $etag . '"'); |
|---|
| | 506 | |
|---|
| | 507 | $timenow = time(); |
|---|
| 506 | | $timenow = time(); |
|---|
| 507 | | |
|---|
| 508 | | // Send last-modified header to enable if-modified-since requests |
|---|
| 509 | | if ($tstamp < $timenow) { |
|---|
| 510 | | header("Last-Modified: " . $lm); |
|---|
| 511 | | if ($tstamp < ($timenow - 3600)) { |
|---|
| 512 | | header('Expires: ' . gmdate("D, d M Y H:i:s", ($timenow+3600)) . " GMT"); |
|---|
| 513 | | } |
|---|
| 514 | | } |
|---|
| 515 | | |
|---|
| 516 | | // Send 304s where possible, rather than spitting out the file each time |
|---|
| 517 | | if (array_key_exists('HTTP_IF_NONE_MATCH',$_SERVER)) { |
|---|
| 518 | | $if_none_match = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_NONE_MATCH']); |
|---|
| 519 | | if ($if_none_match == $etag) { |
|---|
| 520 | | header("{$_SERVER['SERVER_PROTOCOL']} 304 Not Modified"); |
|---|
| 521 | | exit; |
|---|
| 522 | | } |
|---|
| 523 | | } |
|---|
| 524 | | if (array_key_exists('HTTP_IF_MODIFIED_SINCE',$_SERVER)) { |
|---|
| 525 | | $if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']); |
|---|
| 526 | | if ($if_modified_since == $lm) { |
|---|
| 527 | | header("{$_SERVER['SERVER_PROTOCOL']} 304 Not Modified"); |
|---|
| 528 | | exit; |
|---|
| 529 | | } |
|---|
| 530 | | } |
|---|
| 531 | | |
|---|
| 532 | | if ($mimetype) { |
|---|
| 533 | | header("Content-Type: $mimetype"); |
|---|
| 534 | | } |
|---|
| 535 | | readfile($filepath); |
|---|
| | 509 | // Send last-modified header to enable if-modified-since requests |
|---|
| | 510 | if ($tstamp < $timenow) { |
|---|
| | 511 | header("Last-Modified: " . $lm); |
|---|
| | 512 | if ($tstamp < ($timenow - 3600)) { |
|---|
| | 513 | header('Expires: ' . gmdate("D, d M Y H:i:s", ($timenow+3600)) . " GMT"); |
|---|
| | 514 | } |
|---|
| | 515 | } |
|---|
| | 516 | |
|---|
| | 517 | // Send 304s where possible, rather than spitting out the file each time |
|---|
| | 518 | if (array_key_exists('HTTP_IF_NONE_MATCH',$_SERVER)) { |
|---|
| | 519 | $if_none_match = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_NONE_MATCH']); |
|---|
| | 520 | if ($if_none_match == $etag) { |
|---|
| | 521 | header("{$_SERVER['SERVER_PROTOCOL']} 304 Not Modified"); |
|---|
| | 522 | exit; |
|---|
| | 523 | } |
|---|
| | 524 | } |
|---|
| | 525 | if (array_key_exists('HTTP_IF_MODIFIED_SINCE',$_SERVER)) { |
|---|
| | 526 | $if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']); |
|---|
| | 527 | if ($if_modified_since == $lm) { |
|---|
| | 528 | header("{$_SERVER['SERVER_PROTOCOL']} 304 Not Modified"); |
|---|
| | 529 | exit; |
|---|
| | 530 | } |
|---|
| | 531 | } |
|---|
| | 532 | |
|---|
| | 533 | if ($mimetype) { |
|---|
| | 534 | header("Content-Type: $mimetype"); |
|---|
| | 535 | } |
|---|
| | 536 | readfile($filepath); |
|---|
| | 537 | } else { |
|---|
| | 538 | $CFG->files->handler[$handler]($filepath); |
|---|
| | 539 | } |
|---|