Elgg
Version master
engine
classes
Elgg
Controllers
ServeIcon.php
Go to the documentation of this file.
1
<?php
2
3
namespace
Elgg\Controllers
;
4
5
use
Elgg\Exceptions\Http\EntityNotFoundException
;
6
use
Elgg\Http\OkResponse
;
7
use
Elgg\Http\ResponseBuilder
;
8
use
Elgg\Request
;
9
use Elgg\Traits\TimeUsing;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\HttpFoundation\BinaryFileResponse;
12
19
class
ServeIcon
{
20
21
use TimeUsing;
22
31
public
function
__invoke
(
Request
$request
) {
32
33
// clear cache-boosting headers set by PHP session
34
if
(!isset(
$request
->getHttpRequest()->_integration_testing)) {
35
header_remove(
'Cache-Control'
);
36
header_remove(
'Pragma'
);
37
header_remove(
'Expires'
);
38
}
39
40
$guid
= (int)
$request
->getParam(
'guid'
);
41
$size
= (string)
$request
->getParam(
'size'
);
42
43
$entity
=
get_entity
(
$guid
);
44
if
(!
$entity
instanceof \
ElggEntity
) {
45
throw
new
EntityNotFoundException
();
46
}
47
48
$thumbnail =
$entity
->getIcon(
$size
);
49
if
(!$thumbnail->exists()) {
50
// @todo replace this with a PageNotFoundException in 7.0
51
throw
new
EntityNotFoundException
(
'Icon does not exist'
);
52
}
53
54
$if_none_match =
$request
->getHttpRequest()->headers->get(
'if_none_match'
);
55
if
(!empty($if_none_match)) {
56
// strip mod_deflate suffixes
57
$request
->getHttpRequest()->headers->set(
'if_none_match'
, str_replace(
'-gzip'
,
''
, $if_none_match));
58
}
59
60
$etag =
'"'
. $thumbnail->getModifiedTime().
'"'
;
61
62
$response
=
new
Response();
63
$response
->setExpires($this->getCurrentTime(
'-1 day'
))
64
->prepare(
$request
->getHttpRequest())
65
->setPrivate()
66
->setEtag($etag)
67
->setExpires($this->getCurrentTime(
'+1 day'
))
68
->setMaxAge(86400);
69
70
if
(
$response
->isNotModified(
$request
->getHttpRequest())) {
71
return
new
OkResponse
(
''
,
ELGG_HTTP_NOT_MODIFIED
);
72
}
73
74
$headers
= [
75
'Content-Type'
=> $thumbnail->getMimeType(),
76
'X-Content-Type-Options'
=>
'nosniff'
,
77
];
78
$response
=
new
BinaryFileResponse($thumbnail->getFilenameOnFilestore(),
ELGG_HTTP_OK
,
$headers
,
false
,
'inline'
);
79
$response
->prepare(
$request
->getHttpRequest());
80
81
$response
->setPrivate()
82
->setEtag($etag)
83
->setExpires($this->getCurrentTime(
'+1 day'
))
84
->setMaxAge(86400);
85
86
if
(!
$response
->headers->hasCacheControlDirective(
'no-cache'
)) {
87
$response
->headers->addCacheControlDirective(
'no-cache'
,
'Set-Cookie'
);
88
}
89
90
$response
->send();
91
92
return
elgg_ok_response
();
93
}
94
}
$entity
$entity
Definition:
reset.php:8
$guid
$guid
Reset an ElggUpgrade.
Definition:
reset.php:6
ElggEntity
Definition:
ElggEntity.php:52
Elgg\Controllers\ServeIcon
Controller to handle /serve-icon requests.
Definition:
ServeIcon.php:19
Elgg\Controllers\ServeIcon\__invoke
__invoke(Request $request)
Respond to a request.
Definition:
ServeIcon.php:31
Elgg\Exceptions\Http\EntityNotFoundException
Thrown when entity can not be found.
Definition:
EntityNotFoundException.php:10
Elgg\Http\OkResponse
OK response builder.
Definition:
OkResponse.php:8
Elgg\Request
Request container.
Definition:
Request.php:12
ELGG_HTTP_OK
const ELGG_HTTP_OK
Definition:
constants.php:45
ELGG_HTTP_NOT_MODIFIED
const ELGG_HTTP_NOT_MODIFIED
Definition:
constants.php:59
get_entity
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition:
entities.php:70
Elgg\Http\ResponseBuilder
HTTP response builder interface.
Definition:
ResponseBuilder.php:13
$request
$request
Definition:
livesearch.php:12
Elgg\Controllers
Definition:
CommentEntityRedirector.php:3
$headers
$headers
Definition:
section.php:21
elgg_ok_response
elgg_ok_response($content='', string|array $message='', ?string $forward_url=null, int $status_code=ELGG_HTTP_OK)
Prepares a successful response to be returned by a page or an action handler.
Definition:
pagehandler.php:224
$size
$size
Definition:
thumb.php:23
$response
$response
Definition:
content.php:10
Generated on Fri Aug 29 2025 00:01:57 for Elgg by
1.9.1