45 $this->config = $config;
62 if (!
preg_match(
'~serve-file/e(\d+)/l(\d+)/d([ia])/c([01])/([a-zA-Z0-9\-_]+)/(.*)$~',
$path, $m)) {
63 return $response->setStatusCode(400)->setContent(
'Malformatted request URL');
66 list(,
$expires, $last_updated, $disposition, $use_cookie,
$mac, $path_from_dataroot) = $m;
69 return $response->setStatusCode(403)->setContent(
'URL has expired');
74 $path_from_dataroot =
str_replace(
'%20',
' ', $path_from_dataroot);
78 'last_updated' => (
int) $last_updated,
79 'disposition' => $disposition,
80 'path' => $path_from_dataroot,
81 'use_cookie' => (int) $use_cookie,
83 if ((
bool) $use_cookie) {
84 $hmac_data[
'cookie'] = $this->getCookieValue($request);
89 $hmac = $this->hmac->getHmac($hmac_data);
90 if (!$hmac->matchesToken(
$mac)) {
91 return $response->setStatusCode(403)->setContent(
'HMAC mismatch');
95 if (str_starts_with($path_from_dataroot,
':')) {
96 $path_from_dataroot = Base64Url::decode(
substr($path_from_dataroot, 1));
99 $filenameonfilestore =
"{$this->config->dataroot}{$path_from_dataroot}";
101 if (!is_readable($filenameonfilestore)) {
102 return $response->setStatusCode(404)->setContent(
'File not found');
105 $actual_last_updated = filemtime($filenameonfilestore);
106 if ($actual_last_updated != $last_updated) {
107 return $response->setStatusCode(403)->setContent(
'URL has expired');
110 $if_none_match = $request->headers->get(
'if_none_match');
111 if (!empty($if_none_match)) {
113 $request->headers->set(
'if_none_match',
str_replace(
'-gzip',
'', $if_none_match));
116 $etag =
'"' . $actual_last_updated .
'"';
118 if (
$response->isNotModified($request)) {
122 $public = !(bool) $use_cookie;
123 $content_disposition = $disposition ==
'i' ?
'inline' :
'attachment';
126 'Content-Type' => $this->mimetype->getMimeType($filenameonfilestore),
127 'X-Content-Type-Options' =>
'nosniff',
129 $response =
new BinaryFileResponse($filenameonfilestore, 200,
$headers, $public, $content_disposition);
131 $sendfile_type = $this->config->x_sendfile_type;
132 if ($sendfile_type) {
133 $request->headers->set(
'X-Sendfile-Type', $sendfile_type);
135 $mapping = (string) $this->config->x_accel_mapping;
136 $request->headers->set(
'X-Accel-Mapping', $mapping);
162 $config = $this->config->getCookieConfig();
163 $session_name = $config[
'session'][
'name'];
164 return $request->cookies->get($session_name,
'');
getResponse(Request $request)
Handle a request for a file.
__construct(HmacFactory $hmac, Config $config, MimeTypeService $mimetype)
Constructor.
getUrlSegments(bool $raw=false)
Get the Elgg URL segments.
Public service related to MIME type detection.
Provides a factory for HMAC objects.