-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPassThrough.php
90 lines (88 loc) · 3.33 KB
/
PassThrough.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace Luracast\Restler;
/**
* Static Class to pass through content outside of web root
*
* @category Framework
* @package Restler
* @author R.Arul Kumaran <[email protected]>
* @copyright 2010 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/
*
*/
class PassThrough
{
public static $mimeTypes = array(
'js' => 'text/javascript',
'css' => 'text/css',
'png' => 'image/png',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'html' => 'text/html',
);
/**
* Serve a file outside web root
*
* Respond with a file stored outside web accessible path
*
* @param string $filename full path for the file to be served
* @param bool $forceDownload should the we download instead of viewing
* @param int $expires cache expiry in number of seconds
* @param bool $isPublic cache control, is it public or private
*
* @throws RestException
*
*/
public static function file($filename, $forceDownload = false, $expires = 0, $isPublic = true)
{
if (!is_file($filename))
throw new RestException(404);
if (!is_readable($filename))
throw new RestException(403);
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
if (!$mime = Util::nestedValue(static::$mimeTypes, $extension)) {
if (!function_exists('finfo_open')) {
throw new RestException(
500,
'Unable to find media type of ' .
basename($filename) .
' either enable fileinfo php extension or update ' .
'PassThrough::$mimeTypes to include mime type for ' . $extension .
' extension'
);
}
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $filename);
}
if (!is_array(Defaults::$headerCacheControl))
Defaults::$headerCacheControl = array(Defaults::$headerCacheControl);
$cacheControl = Defaults::$headerCacheControl[0];
if ($expires > 0) {
$cacheControl = $isPublic ? 'public' : 'private';
$cacheControl .= end(Defaults::$headerCacheControl);
$cacheControl = str_replace('{expires}', $expires, $cacheControl);
$expires = gmdate('D, d M Y H:i:s \G\M\T', time() + $expires);
}
header('Cache-Control: ' . $cacheControl);
header('Expires: ' . $expires);
$lastModified = filemtime($filename);
if (
isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $lastModified
) {
header("{$_SERVER['SERVER_PROTOCOL']} 304 Not Modified");
exit;
}
header('Last-Modified: ' . date('r', $lastModified));
header('X-Powered-By: Luracast Restler v' . Restler::VERSION);
header('Content-type: ' . $mime);
header("Content-Length: " . filesize($filename));
if ($forceDownload) {
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename="' . $filename . '"');
}
readfile($filename);
exit;
}
}