Elgg  Version master
ServeIcon.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Controllers;
4 
8 use Elgg\Request;
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 
44  if (!$entity instanceof \ElggEntity) {
45  throw new EntityNotFoundException();
46  }
47 
48  $thumbnail = $entity->getIcon($size);
49  if (!$thumbnail->exists()) {
50  throw new EntityNotFoundException('Icon does not exist');
51  }
52 
53  $if_none_match = $request->getHttpRequest()->headers->get('if_none_match');
54  if (!empty($if_none_match)) {
55  // strip mod_deflate suffixes
56  $request->getHttpRequest()->headers->set('if_none_match', str_replace('-gzip', '', $if_none_match));
57  }
58 
59  $etag = '"' . $thumbnail->getModifiedTime(). '"';
60 
61  $response = new Response();
62  $response->setExpires($this->getCurrentTime('-1 day'))
63  ->prepare($request->getHttpRequest())
64  ->setPrivate()
65  ->setEtag($etag)
66  ->setExpires($this->getCurrentTime('+1 day'))
67  ->setMaxAge(86400);
68 
69  if ($response->isNotModified($request->getHttpRequest())) {
70  return new OkResponse('', ELGG_HTTP_NOT_MODIFIED);
71  }
72 
73  $headers = [
74  'Content-Type' => $thumbnail->getMimeType(),
75  'X-Content-Type-Options' => 'nosniff',
76  ];
77  $response = new BinaryFileResponse($thumbnail->getFilenameOnFilestore(), ELGG_HTTP_OK, $headers, false, 'inline');
78  $response->prepare($request->getHttpRequest());
79 
80  $response->setPrivate()
81  ->setEtag($etag)
82  ->setExpires($this->getCurrentTime('+1 day'))
83  ->setMaxAge(86400);
84 
85  if (!$response->headers->hasCacheControlDirective('no-cache')) {
86  $response->headers->addCacheControlDirective('no-cache', 'Set-Cookie');
87  }
88 
89  $response->send();
90 
91  return elgg_ok_response();
92  }
93 }
getHttpRequest()
Get the HttpRequest for this request.
Definition: Request.php:166
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.
$response
Definition: content.php:10
const ELGG_HTTP_OK
Definition: constants.php:45
$request
Definition: livesearch.php:12
Controller to handle /serve-icon requests.
Definition: ServeIcon.php:19
trait TimeUsing
Adds methods for setting the current time (for testing)
Definition: TimeUsing.php:10
getParam($key, $default=null, $filter=true)
Get an element of the params array.
Definition: Request.php:67
getCurrentTime($modifier= '')
Get the (cloned) time.
Definition: TimeUsing.php:25
$entity
Definition: reset.php:8
get_entity(int $guid)
Loads and returns an entity object from a guid.
Definition: entities.php:70
__invoke(Request $request)
Respond to a request.
Definition: ServeIcon.php:31
$size
Definition: thumb.php:23
const ELGG_HTTP_NOT_MODIFIED
Definition: constants.php:59
Request container.
Definition: Request.php:12
Aggregate action for saving settings.
OK response builder.
Definition: OkResponse.php:8
$guid
Reset an ElggUpgrade.
Definition: reset.php:6