3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module;
24 use Friendica\BaseModule;
25 use Friendica\Contact\Header;
26 use Friendica\Core\Logger;
27 use Friendica\Core\Protocol;
28 use Friendica\Database\DBA;
30 use Friendica\Model\Contact;
31 use Friendica\Model\Photo as MPhoto;
32 use Friendica\Model\Post;
33 use Friendica\Model\Profile;
34 use Friendica\Core\Storage\Type\ExternalResource;
35 use Friendica\Core\Storage\Type\SystemResource;
36 use Friendica\Core\System;
37 use Friendica\Core\Worker;
38 use Friendica\Model\User;
39 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
40 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
41 use Friendica\Network\HTTPException;
42 use Friendica\Network\HTTPException\NotModifiedException;
43 use Friendica\Object\Image;
44 use Friendica\Util\Images;
45 use Friendica\Util\Network;
46 use Friendica\Util\ParseUrl;
47 use Friendica\Util\Proxy;
48 use Friendica\Worker\UpdateContact;
53 class Photo extends BaseApi
58 * Fetch a photo or an avatar, in optional size, check for permissions and
61 protected function rawContent(array $request = [])
63 $totalstamp = microtime(true);
65 if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
66 header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
67 if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) {
68 header('Etag: ' . $_SERVER['HTTP_IF_NONE_MATCH']);
70 header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (31536000)) . ' GMT');
71 header('Cache-Control: max-age=31536000');
72 if (function_exists('header_remove')) {
73 header_remove('Last-Modified');
74 header_remove('Expires');
75 header_remove('Cache-Control');
77 throw new NotModifiedException();
80 Profile::addVisitorCookieForHTTPSigner($this->server);
83 $square_resize = true;
85 $stamp = microtime(true);
87 if (!empty($this->parameters['type'])) {
88 if (!empty($this->parameters['customsize'])) {
89 $customsize = intval($this->parameters['customsize']);
90 $square_resize = !in_array($this->parameters['type'], ['media', 'preview']);
93 if (!empty($this->parameters['guid'])) {
94 $guid = $this->parameters['guid'];
95 $account = DBA::selectFirst('account-user-view', ['id'], ['guid' => $guid], ['order' => ['uid' => true]]);
96 if (empty($account)) {
97 throw new HTTPException\NotFoundException();
100 $id = $account['id'];
103 // Contact Id Fallback, to remove after version 2021.12
104 if (isset($this->parameters['contact_id'])) {
105 $id = intval($this->parameters['contact_id']);
108 if (!empty($this->parameters['nickname_ext'])) {
109 $nickname = pathinfo($this->parameters['nickname_ext'], PATHINFO_FILENAME);
110 $user = User::getByNickname($nickname, ['uid']);
112 throw new HTTPException\NotFoundException();
118 // User Id Fallback, to remove after version 2021.12
119 if (!empty($this->parameters['uid_ext'])) {
120 $id = intval(pathinfo($this->parameters['uid_ext'], PATHINFO_FILENAME));
123 // Please refactor this for the love of everything that's good
124 if (isset($this->parameters['id'])) {
125 $id = $this->parameters['id'];
129 Logger::notice('No picture id was detected', ['parameters' => $this->parameters, 'query' => DI::args()->getQueryString()]);
130 throw new HTTPException\NotFoundException(DI::l10n()->t('The Photo is not available.'));
133 $photo = self::getPhotoById($id, $this->parameters['type'], $customsize ?: Proxy::PIXEL_SMALL);
135 $photoid = pathinfo($this->parameters['name'], PATHINFO_FILENAME);
137 if (substr($photoid, -2, 1) == '-') {
138 $scale = intval(substr($photoid, -1, 1));
139 $photoid = substr($photoid, 0, -2);
142 if (!empty($this->parameters['size'])) {
143 switch ($this->parameters['size']) {
153 $photo = MPhoto::getPhoto($photoid, $scale, self::getCurrentUserID());
154 if ($photo === false) {
155 throw new HTTPException\NotFoundException(DI::l10n()->t('The Photo with id %s is not available.', $photoid));
159 $fetch = microtime(true) - $stamp;
161 if ($photo === false) {
162 throw new HTTPException\NotFoundException();
165 $cacheable = ($photo['allow_cid'] . $photo['allow_gid'] . $photo['deny_cid'] . $photo['deny_gid'] === '') && (isset($photo['cacheable']) ? $photo['cacheable'] : true);
167 $stamp = microtime(true);
169 $imgdata = MPhoto::getImageDataForPhoto($photo);
170 if (empty($imgdata) && empty($photo['blurhash'])) {
171 throw new HTTPException\NotFoundException();
172 } elseif (empty($imgdata) && !empty($photo['blurhash'])) {
173 $image = New Image('', 'image/png');
174 $image->getFromBlurHash($photo['blurhash'], $photo['width'], $photo['height']);
175 $imgdata = $image->asString();
178 // The mimetype for an external or system resource can only be known reliably after it had been fetched
179 if (in_array($photo['backend-class'], [ExternalResource::NAME, SystemResource::NAME])) {
180 $mimetype = Images::getMimeTypeByData($imgdata);
181 if (!empty($mimetype)) {
182 $photo['type'] = $mimetype;
186 $data = microtime(true) - $stamp;
188 if (empty($imgdata)) {
189 Logger::warning('Invalid photo', ['id' => $photo['id']]);
190 if (in_array($photo['backend-class'], [ExternalResource::NAME])) {
191 $reference = json_decode($photo['backend-ref'], true);
192 $error = DI::l10n()->t('Invalid external resource with url %s.', $reference['url']);
194 $error = DI::l10n()->t('Invalid photo with id %s.', $photo['id']);
196 throw new HTTPException\InternalServerErrorException($error);
199 if (!empty($request['static'])) {
200 $img = new Image($imgdata, $photo['type']);
202 $imgdata = $img->asString();
205 // if customsize is set and image is not a gif, resize it
206 if ($photo['type'] !== 'image/gif' && $customsize > 0 && $customsize <= Proxy::PIXEL_THUMB && $square_resize) {
207 $img = new Image($imgdata, $photo['type']);
208 $img->scaleToSquare($customsize);
209 $imgdata = $img->asString();
210 } elseif ($photo['type'] !== 'image/gif' && $customsize > 0) {
211 $img = new Image($imgdata, $photo['type']);
212 $img->scaleDown($customsize);
213 $imgdata = $img->asString();
216 if (function_exists('header_remove')) {
217 header_remove('Pragma');
218 header_remove('pragma');
221 header('Content-type: ' . $photo['type']);
223 $stamp = microtime(true);
225 // it is a private photo that they have no permission to view.
226 // tell the browser not to cache it, in case they authenticate
227 // and subsequently have permission to see it
228 header('Cache-Control: no-store, no-cache, must-revalidate');
230 $md5 = $photo['hash'] ?: md5($imgdata);
231 header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
232 header("Etag: \"{$md5}\"");
233 header('Expires: ' . gmdate('D, d M Y H:i:s', time() + (31536000)) . ' GMT');
234 header('Cache-Control: max-age=31536000');
236 $checksum = microtime(true) - $stamp;
238 $stamp = microtime(true);
240 $output = microtime(true) - $stamp;
242 $total = microtime(true) - $totalstamp;
243 $rest = $total - ($fetch + $data + $checksum + $output);
245 if (!is_null($scale) && ($scale < 4)) {
246 Logger::debug('Performance:', ['scale' => $scale, 'resource' => $photo['resource-id'],
247 'total' => number_format($total, 3), 'fetch' => number_format($fetch, 3),
248 'data' => number_format($data, 3), 'checksum' => number_format($checksum, 3),
249 'output' => number_format($output, 3), 'rest' => number_format($rest, 3)]);
256 * Fetches photo record by given id number, type and custom size
258 * @param int $id Photo id
259 * @param string $type Photo type
260 * @param int $customsize Custom size (?)
261 * @return array|bool Array on success, false on error
263 private static function getPhotoById(int $id, string $type, int $customsize)
267 $media = DBA::selectFirst('post-media', ['preview', 'url', 'preview-height', 'preview-width', 'height', 'width', 'mimetype', 'type', 'uri-id', 'blurhash'], ['id' => $id]);
271 $url = $media['preview'];
272 $width = $media['preview-width'];
273 $height = $media['preview-height'];
275 if (empty($url) && ($media['type'] == Post\Media::IMAGE)) {
276 $url = $media['url'];
277 $width = $media['width'];
278 $height = $media['height'];
285 if (Network::isLocalLink($url) && preg_match('|.*?/photo/(.*[a-fA-F0-9])\-(.*[0-9])\..*[\w]|', $url, $matches)) {
286 return MPhoto::getPhoto($matches[1], $matches[2], self::getCurrentUserID());
289 return MPhoto::createPhotoForExternalResource($url, (int)DI::userSession()->getLocalUserId(), $media['mimetype'] ?? '', $media['blurhash'], $width, $height);
291 $media = DBA::selectFirst('post-media', ['url', 'height', 'width', 'mimetype', 'uri-id', 'blurhash'], ['id' => $id, 'type' => Post\Media::IMAGE]);
296 if (Network::isLocalLink($media['url']) && preg_match('|.*?/photo/(.*[a-fA-F0-9])\-(.*[0-9])\..*[\w]|', $media['url'], $matches)) {
297 return MPhoto::getPhoto($matches[1], $matches[2], self::getCurrentUserID());
300 return MPhoto::createPhotoForExternalResource($media['url'], (int)DI::userSession()->getLocalUserId(), $media['mimetype'], $media['blurhash'], $media['width'], $media['height']);
302 $link = DBA::selectFirst('post-link', ['url', 'mimetype', 'blurhash', 'width', 'height'], ['id' => $id]);
307 return MPhoto::createPhotoForExternalResource($link['url'], (int)DI::userSession()->getLocalUserId(), $link['mimetype'] ?? '', $link['blurhash'] ?? '', $link['width'] ?? 0, $link['height'] ?? 0);
309 $fields = ['uid', 'uri-id', 'url', 'nurl', 'avatar', 'photo', 'blurhash', 'xmpp', 'addr', 'network', 'failed', 'updated'];
310 $contact = Contact::getById($id, $fields);
311 if (empty($contact)) {
315 // For local users directly use the photo record that is marked as the profile
316 if (Network::isLocalLink($contact['url'])) {
317 $contact = Contact::selectFirst($fields, ['nurl' => $contact['nurl'], 'self' => true]);
318 if (!empty($contact)) {
319 if ($customsize <= Proxy::PIXEL_MICRO) {
321 } elseif ($customsize <= Proxy::PIXEL_THUMB) {
326 $photo = MPhoto::selectFirst([], ['scale' => $scale, 'uid' => $contact['uid'], 'profile' => 1]);
327 if (!empty($photo)) {
333 if (!empty($contact['uid']) && empty($contact['photo']) && empty($contact['avatar'])) {
334 $contact = Contact::getByURL($contact['url'], false, $fields);
337 if (!empty($contact['photo']) && !empty($contact['avatar'])) {
338 // Fetch photo directly
339 $resourceid = MPhoto::ridFromURI($contact['photo']);
340 if (!empty($resourceid)) {
341 $photo = MPhoto::selectFirst([], ['resource-id' => $resourceid], ['order' => ['scale']]);
342 if (!empty($photo)) {
345 $url = $contact['avatar'];
348 $url = $contact['photo'];
350 } elseif (!empty($contact['avatar'])) {
351 $url = $contact['avatar'];
354 // If it is a local link, we save resources by just redirecting to it.
355 if (!empty($url) && Network::isLocalLink($url)) {
356 System::externalRedirect($url);
361 $mime = ParseUrl::getContentType($url, HttpClientAccept::IMAGE);
363 $mimetext = $mime[0] . '/' . $mime[1];
365 // Only update federated accounts that hadn't failed before and hadn't been updated recently
366 $update = in_array($contact['network'], Protocol::FEDERATED) && !$contact['failed']
367 && ((time() - strtotime($contact['updated']) > 86400));
369 $curlResult = DI::httpClient()->head($url, [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::IMAGE]);
370 $update = !$curlResult->isSuccess() && ($curlResult->getReturnCode() == 404);
371 Logger::debug('Got return code for avatar', ['return code' => $curlResult->getReturnCode(), 'cid' => $id, 'url' => $contact['url'], 'avatar' => $url]);
375 UpdateContact::add(Worker::PRIORITY_LOW, $id);
376 Logger::info('Invalid file, contact update initiated', ['cid' => $id, 'url' => $contact['url'], 'avatar' => $url]);
377 } catch (\InvalidArgumentException $e) {
378 Logger::notice($e->getMessage(), ['id' => $id, 'contact' => $contact]);
381 Logger::info('Invalid file', ['cid' => $id, 'url' => $contact['url'], 'avatar' => $url]);
384 if (!empty($mimetext) && ($mime[0] != 'image') && ($mimetext != 'application/octet-stream')) {
385 Logger::info('Unexpected Content-Type', ['mime' => $mimetext, 'url' => $url]);
387 } if (!empty($mimetext)) {
388 Logger::debug('Expected Content-Type', ['mime' => $mimetext, 'url' => $url]);
391 if (empty($mimetext) && !empty($contact['blurhash'])) {
392 $image = New Image('', 'image/png');
393 $image->getFromBlurHash($contact['blurhash'], $customsize, $customsize);
394 return MPhoto::createPhotoForImageData($image->asString());
395 } elseif (empty($mimetext)) {
396 if ($customsize <= Proxy::PIXEL_MICRO) {
397 $url = Contact::getDefaultAvatar($contact ?: [], Proxy::SIZE_MICRO);
398 } elseif ($customsize <= Proxy::PIXEL_THUMB) {
399 $url = Contact::getDefaultAvatar($contact ?: [], Proxy::SIZE_THUMB);
401 $url = Contact::getDefaultAvatar($contact ?: [], Proxy::SIZE_SMALL);
403 if (Network::isLocalLink($url)) {
404 System::externalRedirect($url);
407 return MPhoto::createPhotoForExternalResource($url, 0, $mimetext, $contact['blurhash'] ?? null, $customsize, $customsize);
409 $fields = ['uid', 'url', 'header', 'network', 'gsid'];
410 $contact = Contact::getById($id, $fields);
411 if (empty($contact)) {
415 if (Network::isLocalLink($contact['url'])) {
416 $header_uid = User::getIdForURL($contact['url']);
417 if (empty($header_uid)) {
418 throw new HTTPException\NotFoundException();
420 return self::getBannerForUser($header_uid);
423 If (($contact['uid'] != 0) && empty($contact['header'])) {
424 $contact = Contact::getByURL($contact['url'], false, $fields);
426 if (!empty($contact['header'])) {
427 $url = $contact['header'];
429 $url = Contact::getDefaultHeader($contact);
430 if (Network::isLocalLink($url)) {
431 System::externalRedirect($url);
434 return MPhoto::createPhotoForExternalResource($url);
436 return self::getBannerForUser($id);
449 $photo = MPhoto::selectFirst([], ['scale' => $scale, 'uid' => $id, 'profile' => 1]);
451 $contact = DBA::selectFirst('contact', [], ['uid' => $id, 'self' => true]) ?: [];
456 $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_SMALL);
459 $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_MICRO);
463 $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_THUMB);
466 if (Network::isLocalLink($default)) {
467 System::externalRedirect($default);
470 $parts = parse_url($default);
471 if (!empty($parts['scheme']) || !empty($parts['host'])) {
472 $photo = MPhoto::createPhotoForExternalResource($default);
474 $photo = MPhoto::createPhotoForSystemResource($default);
480 private static function getBannerForUser(int $uid): array
482 $photo = MPhoto::selectFirst([], ['scale' => 3, 'uid' => $uid, 'photo-type' => MPhoto::USER_BANNER]);
483 if (!empty($photo)) {
486 return MPhoto::createPhotoForImageData(file_get_contents(DI::basePath() . (new Header(DI::config()))->getMastodonBannerPath()));