Elgg  Version 5.1
ImageFetcherService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Assets;
4 
6 use Elgg\Config;
11 
18 
19  protected const CACHE_PREFIX = 'image_fetcher_';
20 
24  protected $cache;
25 
29  protected $client;
30 
34  protected $config;
35 
39  protected $session;
40 
49  $this->config = $config;
50  $this->cache = $cache;
51  $this->session = $session;
52 
53  $this->client = elgg_get_http_client();
54  }
55 
68  public function getImage(string $image_url) {
69  if (empty($image_url)) {
70  throw new InvalidArgumentException('a non-empty image url is required for image fetching');
71  }
72 
73  $image_url = htmlspecialchars_decode($image_url);
74  $image_url = elgg_normalize_url($image_url);
75 
76  $cache = $this->loadFromCache($image_url);
77  if (!empty($cache)) {
78  return $cache;
79  }
80 
82  $options = [];
83 
84  if (stripos($image_url, $site->getURL()) === 0) {
85  // internal url, can use session cookie
86  $cookie_config = $this->config->getCookieConfig();
87 
88  $cookies = [
89  $cookie_config['session']['name'] => $this->session->getID(),
90  ];
91 
92  $domain = $cookie_config['session']['domain'] ?: $site->getDomain();
93 
94  $cookiejar = CookieJar::fromArray($cookies, $domain);
95  $options[RequestOptions::COOKIES] = $cookiejar;
96  }
97 
98  try {
99  $response = $this->client->get($image_url, $options);
100  } catch (TransferException $e) {
101  // this shouldn't happen, but just in case
102  return false;
103  }
104 
105  if ($response->getStatusCode() !== ELGG_HTTP_OK) {
106  return false;
107  }
108 
109  $result = [
110  'data' => $response->getBody()->getContents(),
111  'content-type' => $response->getHeaderLine('content-type') ?: 'application/octet-stream',
112  'name' => basename($image_url),
113  ];
114 
115  $this->saveToCache($image_url, $result);
116 
117  return $result;
118  }
119 
127  protected function loadFromCache(string $image_url): array {
128  $cache = $this->cache->load(self::CACHE_PREFIX . md5($image_url));
129 
130  return is_array($cache) ? $cache : [];
131  }
132 
141  protected function saveToCache(string $image_url, array $data): bool {
142  return $this->cache->save(self::CACHE_PREFIX . md5($image_url), $data);
143  }
144 }
Exception thrown if an argument is not of the expected type.
__construct(Config $config, SystemCache $cache,\ElggSession $session)
Constructor.
$response
Definition: content.php:10
const ELGG_HTTP_OK
Definition: constants.php:45
getImage(string $image_url)
Get an image.
$site
Definition: icons.php:5
$options
Elgg admin footer.
Definition: footer.php:6
Elgg Session Management.
Definition: ElggSession.php:19
if(!$entity instanceof\ElggUser) $data
Definition: attributes.php:13
loadFromCache(string $image_url)
Load an image url from cache.
Fetch external images server side.
saveToCache(string $image_url, array $data)
Save image data in system cache for easy reuse.
elgg_get_site_entity()
Get the current site entity.
Definition: entities.php:98
$domain
Definition: layout.php:32
elgg_get_http_client(array $options=[])
Returns a Guzzle HTTP client.
Definition: elgglib.php:235
elgg_normalize_url(string $url)
Definition: output.php:163