1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 26.04.16 at 10:43 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
use samsonphp\resource\ResourceValidator; |
8
|
|
|
|
9
|
|
|
// Define this resource identifier |
10
|
|
|
define('STATIC_RESOURCE_HANDLER', 'resourcer'); |
11
|
|
|
|
12
|
|
|
// Get current project web root directory |
13
|
|
|
ResourceValidator::$webRoot = getcwd(); |
14
|
|
|
// Get current project root directory |
15
|
|
|
ResourceValidator::$projectRoot = dirname(ResourceValidator::$webRoot); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Static resource path builder. |
19
|
|
|
* |
20
|
|
|
* @deprecated Moving to use new samsonphp/view resource an $this->src() should be used |
21
|
|
|
* in view file for generating paths to static resources |
22
|
|
|
* |
23
|
|
|
* @param string $path Relative static resource resource path |
24
|
|
|
* @param null $module Entity for path building, if not passed current resource is used |
25
|
|
|
* |
26
|
|
|
* @return string Static resource path |
27
|
|
|
* @throws \samsonphp\resource\exception\ResourceNotFound |
28
|
|
|
*/ |
29
|
|
|
function src($path, $module = null) |
30
|
|
|
{ |
31
|
|
|
// Define resource |
32
|
|
|
switch (gettype($module)) { |
33
|
|
|
case 'string': // Find resource by identifier |
34
|
|
|
$module = m($module); |
35
|
|
|
break; |
36
|
|
|
case 'object': // Do nothing |
37
|
|
|
break; |
38
|
|
|
default: // Get current resource |
39
|
|
|
$module = m(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
echo '/' . STATIC_RESOURCE_HANDLER . '/?p=' . ResourceValidator::getRelativePath($path, $module->path(), dirname(getcwd())); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** Collection of supported mime types */ |
46
|
|
|
$mimeTypes = array( |
47
|
|
|
'css' => 'text/css', |
48
|
|
|
'woff' => 'application/x-font-woff', |
49
|
|
|
'woff2' => 'application/x-font-woff2', |
50
|
|
|
'otf' => 'application/octet-stream', |
51
|
|
|
'ttf' => 'application/octet-stream', |
52
|
|
|
'eot' => 'application/vnd.ms-fontobject', |
53
|
|
|
'js' => 'application/x-javascript', |
54
|
|
|
'htm' => 'text/html;charset=utf-8', |
55
|
|
|
'htc' => 'text/x-component', |
56
|
|
|
'jpeg' => 'image/jpeg', |
57
|
|
|
'png' => 'image/png', |
58
|
|
|
'jpg' => 'image/jpg', |
59
|
|
|
'gif' => 'image/gif', |
60
|
|
|
'txt' => 'text/plain', |
61
|
|
|
'pdf' => 'application/pdf', |
62
|
|
|
'rtf' => 'application/rtf', |
63
|
|
|
'doc' => 'application/msword', |
64
|
|
|
'xls' => 'application/msexcel', |
65
|
|
|
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
66
|
|
|
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
67
|
|
|
'svg' => 'image/svg+xml', |
68
|
|
|
'mp4' => 'video/mp4', |
69
|
|
|
'ogg' => 'video/ogg' |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
// Perform custom simple URL parsing to match needed URL for static resource serving |
73
|
|
|
$url = array_key_exists('REQUEST_URI', $_SERVER) ? $_SERVER['REQUEST_URI'] : ''; |
74
|
|
|
|
75
|
|
|
// Get URL path from URL and split with "/" |
76
|
|
|
$url = array_values(array_filter(explode('/', parse_url($url, PHP_URL_PATH)))); |
77
|
|
|
|
78
|
|
|
// Special hook to avoid further framework loading if this is static resource request |
79
|
|
|
if (array_key_exists(0, $url) && $url[0] === STATIC_RESOURCE_HANDLER) { |
80
|
|
|
// Get full path to static resource |
81
|
|
|
$filename = realpath('../' . $_GET['p']); |
82
|
|
|
|
83
|
|
|
if (file_exists($filename)) { |
84
|
|
|
// Receive current ETag for resource(if present) |
85
|
|
|
$clientETag = array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER) ? $_SERVER['HTTP_IF_NONE_MATCH'] : ''; |
86
|
|
|
|
87
|
|
|
// Generate ETag for resource |
88
|
|
|
$serverETag = filemtime($filename); |
89
|
|
|
|
90
|
|
|
// Set old cache headers |
91
|
|
|
header('Cache-Control:max-age=1800'); |
92
|
|
|
|
93
|
|
|
// Always set new ETag header independently on further actions |
94
|
|
|
header('ETag:' . $serverETag); |
95
|
|
|
|
96
|
|
|
// Get file extension |
97
|
|
|
$extension = pathinfo($filename, PATHINFO_EXTENSION); |
98
|
|
|
|
99
|
|
|
// If server and client ETags are equal |
100
|
|
|
if ($clientETag === $serverETag) { |
101
|
|
|
header('HTTP/1.1 304 Not Modified'); |
102
|
|
|
} elseif (array_key_exists($extension, $mimeTypes)) { |
103
|
|
|
// Set mime type |
104
|
|
|
header('Content-type: ' . $mimeTypes[$extension]); |
105
|
|
|
|
106
|
|
|
// Output resource |
107
|
|
|
echo file_get_contents($filename); |
108
|
|
|
} else { // File type is not supported |
109
|
|
|
header('HTTP/1.0 404 Not Found'); |
110
|
|
|
} |
111
|
|
|
} else { // File not found |
112
|
|
|
header('HTTP/1.0 404 Not Found'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// Avoid further request processing |
116
|
|
|
die(); |
117
|
|
|
} |
118
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: