Elgg  Version 6.0
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 $client;
25 
33  public function __construct(
34  protected Config $config,
35  protected SystemCache $cache,
36  protected \ElggSession $session
37  ) {
38  $this->client = elgg_get_http_client();
39  }
40 
53  public function getImage(string $image_url) {
54  if (empty($image_url)) {
55  throw new InvalidArgumentException('a non-empty image url is required for image fetching');
56  }
57 
58  $image_url = htmlspecialchars_decode($image_url);
59  $image_url = elgg_normalize_url($image_url);
60 
61  $cache = $this->loadFromCache($image_url);
62  if (!empty($cache)) {
63  return $cache;
64  }
65 
67  $options = [];
68 
69  if (stripos($image_url, $site->getURL()) === 0) {
70  // internal url, can use session cookie
71  $cookie_config = $this->config->getCookieConfig();
72 
73  $cookies = [
74  $cookie_config['session']['name'] => $this->session->getID(),
75  ];
76 
77  $domain = $cookie_config['session']['domain'] ?: $site->getDomain();
78 
79  $cookiejar = CookieJar::fromArray($cookies, $domain);
80  $options[RequestOptions::COOKIES] = $cookiejar;
81  }
82 
83  try {
84  $response = $this->client->get($image_url, $options);
85  } catch (TransferException $e) {
86  // this shouldn't happen, but just in case
87  return false;
88  }
89 
90  if ($response->getStatusCode() !== ELGG_HTTP_OK) {
91  return false;
92  }
93 
94  $result = [
95  'data' => $response->getBody()->getContents(),
96  'content-type' => $response->getHeaderLine('content-type') ?: 'application/octet-stream',
97  'name' => basename($image_url),
98  ];
99 
100  $this->saveToCache($image_url, $result);
101 
102  return $result;
103  }
104 
112  protected function loadFromCache(string $image_url): array {
113  $cache = $this->cache->load(self::CACHE_PREFIX . md5($image_url));
114 
115  return is_array($cache) ? $cache : [];
116  }
117 
126  protected function saveToCache(string $image_url, array $data): bool {
127  return $this->cache->save(self::CACHE_PREFIX . md5($image_url), $data);
128  }
129 }
Exception thrown if an argument is not of the expected type.
$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
Elgg Session Management.
Definition: ElggSession.php:19
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
if($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
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:101
if(isset($_COOKIE['elggperm'])) $session
Definition: login_as.php:29
$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
__construct(protected Config $config, protected SystemCache $cache, protected\ElggSession $session)
Constructor.