]> git.mxchange.org Git - friendica.git/blob - src/Module/Photo.php
Replace remaining references to default banner image by api.mastodon_banner configura...
[friendica.git] / src / Module / Photo.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module;
23
24 use Friendica\BaseModule;
25 use Friendica\Contact\Header;
26 use Friendica\Core\Logger;
27 use Friendica\Core\Protocol;
28 use Friendica\Database\DBA;
29 use Friendica\DI;
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;
49
50 /**
51  * Photo Module
52  */
53 class Photo extends BaseApi
54 {
55         /**
56          * Module initializer
57          *
58          * Fetch a photo or an avatar, in optional size, check for permissions and
59          * return the image
60          */
61         protected function rawContent(array $request = [])
62         {
63                 $totalstamp = microtime(true);
64
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']);
69                         }
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');
76                         }
77                         throw new NotModifiedException();
78                 }
79
80                 Profile::addVisitorCookieForHTTPSigner();
81
82                 $customsize = 0;
83                 $square_resize = true;
84                 $scale = null;
85                 $stamp = microtime(true);
86                 // User avatar
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']);
91                         }
92
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();
98                                 }
99
100                                 $id = $account['id'];
101                         }
102
103                         // Contact Id Fallback, to remove after version 2021.12
104                         if (isset($this->parameters['contact_id'])) {
105                                 $id = intval($this->parameters['contact_id']);
106                         }
107
108                         if (!empty($this->parameters['nickname_ext'])) {
109                                 $nickname = pathinfo($this->parameters['nickname_ext'], PATHINFO_FILENAME);
110                                 $user = User::getByNickname($nickname, ['uid']);
111                                 if (empty($user)) {
112                                         throw new HTTPException\NotFoundException();
113                                 }
114
115                                 $id = $user['uid'];
116                         }
117
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));
121                         }
122
123                         // Please refactor this for the love of everything that's good
124                         if (isset($this->parameters['id'])) {
125                                 $id = $this->parameters['id'];
126                         }
127
128                         if (empty($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.'));
131                         }
132
133                         $photo = self::getPhotoById($id, $this->parameters['type'], $customsize ?: Proxy::PIXEL_SMALL);
134                 } else {
135                         $photoid = pathinfo($this->parameters['name'], PATHINFO_FILENAME);
136                         $scale = 0;
137                         if (substr($photoid, -2, 1) == '-') {
138                                 $scale = intval(substr($photoid, -1, 1));
139                                 $photoid = substr($photoid, 0, -2);
140                         }
141
142                         if (!empty($this->parameters['size'])) {
143                                 switch ($this->parameters['size']) {
144                                         case 'thumb_small':
145                                                 $scale = 2;
146                                                 break;
147                                         case 'scaled_full':
148                                                 $scale = 1;
149                                                 break;
150                                         }
151                         }
152
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));
156                         }
157                 }
158
159                 $fetch = microtime(true) - $stamp;
160
161                 if ($photo === false) {
162                         throw new HTTPException\NotFoundException();
163                 }
164
165                 $cacheable = ($photo['allow_cid'] . $photo['allow_gid'] . $photo['deny_cid'] . $photo['deny_gid'] === '') && (isset($photo['cacheable']) ? $photo['cacheable'] : true);
166
167                 $stamp = microtime(true);
168
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();
176                 }
177
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;
183                         }
184                 }
185
186                 $data = microtime(true) - $stamp;
187
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']);
193                         } else {
194                                 $error = DI::l10n()->t('Invalid photo with id %s.', $photo['id']);
195                         }
196                         throw new HTTPException\InternalServerErrorException($error);
197                 }
198
199                 if (!empty($request['static'])) {
200                         $img = new Image($imgdata, $photo['type']);
201                         $img->toStatic();
202                         $imgdata = $img->asString();
203                 }
204
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();
214                 }
215
216                 if (function_exists('header_remove')) {
217                         header_remove('Pragma');
218                         header_remove('pragma');
219                 }
220
221                 header('Content-type: ' . $photo['type']);
222
223                 $stamp = microtime(true);
224                 if (!$cacheable) {
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');
229                 } else {
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');
235                 }
236                 $checksum = microtime(true) - $stamp;
237
238                 $stamp = microtime(true);
239                 echo $imgdata;
240                 $output = microtime(true) - $stamp;
241
242                 $total = microtime(true) - $totalstamp;
243                 $rest = $total - ($fetch + $data + $checksum + $output);
244
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)]);
250                 }
251
252                 System::exit();
253         }
254
255         /**
256          * Fetches photo record by given id number, type and custom size
257          *
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
262          */
263         private static function getPhotoById(int $id, string $type, int $customsize)
264         {
265                 switch($type) {
266                         case 'preview':
267                                 $media = DBA::selectFirst('post-media', ['preview', 'url', 'preview-height', 'preview-width', 'height', 'width', 'mimetype', 'type', 'uri-id', 'blurhash'], ['id' => $id]);
268                                 if (empty($media)) {
269                                         return false;
270                                 }
271                                 $url    = $media['preview'];
272                                 $width  = $media['preview-width'];
273                                 $height = $media['preview-height'];
274
275                                 if (empty($url) && ($media['type'] == Post\Media::IMAGE)) {
276                                         $url    = $media['url'];
277                                         $width  = $media['width'];
278                                         $height = $media['height'];
279                                 }
280
281                                 if (empty($url)) {
282                                         return false;
283                                 }
284
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());
287                                 }
288
289                                 return MPhoto::createPhotoForExternalResource($url, (int)DI::userSession()->getLocalUserId(), $media['mimetype'] ?? '', $media['blurhash'], $width, $height);
290                         case 'media':
291                                 $media = DBA::selectFirst('post-media', ['url', 'height', 'width', 'mimetype', 'uri-id', 'blurhash'], ['id' => $id, 'type' => Post\Media::IMAGE]);
292                                 if (empty($media)) {
293                                         return false;
294                                 }
295
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());
298                                 }
299
300                                 return MPhoto::createPhotoForExternalResource($media['url'], (int)DI::userSession()->getLocalUserId(), $media['mimetype'], $media['blurhash'], $media['width'], $media['height']);
301                         case 'link':
302                                 $link = DBA::selectFirst('post-link', ['url', 'mimetype', 'blurhash', 'width', 'height'], ['id' => $id]);
303                                 if (empty($link)) {
304                                         return false;
305                                 }
306
307                                 return MPhoto::createPhotoForExternalResource($link['url'], (int)DI::userSession()->getLocalUserId(), $link['mimetype'] ?? '', $link['blurhash'] ?? '', $link['width'] ?? 0, $link['height'] ?? 0);
308                         case 'contact':
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)) {
312                                         return false;
313                                 }
314
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) {
320                                                         $scale = 6;
321                                                 } elseif ($customsize <= Proxy::PIXEL_THUMB) {
322                                                         $scale = 5;
323                                                 } else {
324                                                         $scale = 4;
325                                                 }
326                                                 $photo = MPhoto::selectFirst([], ['scale' => $scale, 'uid' => $contact['uid'], 'profile' => 1]);
327                                                 if (!empty($photo)) {
328                                                         return $photo;
329                                                 }
330                                         }
331                                 }
332
333                                 if (!empty($contact['uid']) && empty($contact['photo']) && empty($contact['avatar'])) {
334                                         $contact = Contact::getByURL($contact['url'], false, $fields);
335                                 }
336
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)) {
343                                                         return $photo;
344                                                 } else {
345                                                         $url = $contact['avatar'];
346                                                 }
347                                         } else {
348                                                 $url = $contact['photo'];
349                                         }
350                                 } elseif (!empty($contact['avatar'])) {
351                                         $url = $contact['avatar'];
352                                 }
353
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);
357                                 }
358
359                                 $mimetext = '';
360                                 if (!empty($url)) {
361                                         $mime = ParseUrl::getContentType($url, HttpClientAccept::IMAGE);
362                                         if (!empty($mime)) {
363                                                 $mimetext = $mime[0] . '/' . $mime[1];
364                                         } else {
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));
368                                                 if ($update) {
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]);
372                                                 }
373                                                 if ($update) {
374                                                         try {
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]);
379                                                         }
380                                                 } else {
381                                                         Logger::info('Invalid file', ['cid' => $id, 'url' => $contact['url'], 'avatar' => $url]);
382                                                 }
383                                         }
384                                         if (!empty($mimetext) && ($mime[0] != 'image') && ($mimetext != 'application/octet-stream')) {
385                                                 Logger::info('Unexpected Content-Type', ['mime' => $mimetext, 'url' => $url]);
386                                                 $mimetext = '';
387                                         } if (!empty($mimetext)) {
388                                                 Logger::debug('Expected Content-Type', ['mime' => $mimetext, 'url' => $url]);
389                                         }
390                                 }
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);
400                                         } else {
401                                                 $url = Contact::getDefaultAvatar($contact ?: [], Proxy::SIZE_SMALL);
402                                         }
403                                         if (Network::isLocalLink($url)) {
404                                                 System::externalRedirect($url);
405                                         }
406                                 }
407                                 return MPhoto::createPhotoForExternalResource($url, 0, $mimetext, $contact['blurhash'] ?? null, $customsize, $customsize);
408                         case 'header':
409                                 $fields = ['uid', 'url', 'header', 'network', 'gsid'];
410                                 $contact = Contact::getById($id, $fields);
411                                 if (empty($contact)) {
412                                         return false;
413                                 }
414
415                                 if (Network::isLocalLink($contact['url'])) {
416                                         $header_uid = User::getIdForURL($contact['url']);
417                                         if (empty($header_uid)) {
418                                                 throw new HTTPException\NotFoundException();
419                                         }
420                                         return self::getBannerForUser($header_uid);
421                                 }
422
423                                 If (($contact['uid'] != 0) && empty($contact['header'])) {
424                                         $contact = Contact::getByURL($contact['url'], false, $fields);
425                                 }
426                                 if (!empty($contact['header'])) {
427                                         $url = $contact['header'];
428                                 } else {
429                                         $url = Contact::getDefaultHeader($contact);
430                                         if (Network::isLocalLink($url)) {
431                                                 System::externalRedirect($url);
432                                         }
433                                 }
434                                 return MPhoto::createPhotoForExternalResource($url);
435                         case 'banner':
436                                 return self::getBannerForUser($id);
437                         case 'profile':
438                         case 'custom':
439                                 $scale = 4;
440                                 break;
441                         case 'micro':
442                                 $scale = 6;
443                                 break;
444                         case 'avatar':
445                         default:
446                                 $scale = 5;
447                 }
448
449                 $photo = MPhoto::selectFirst([], ['scale' => $scale, 'uid' => $id, 'profile' => 1]);
450                 if (empty($photo)) {
451                         $contact = DBA::selectFirst('contact', [], ['uid' => $id, 'self' => true]) ?: [];
452
453                         switch($type) {
454                                 case 'profile':
455                                 case 'custom':
456                                         $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_SMALL);
457                                         break;
458                                 case 'micro':
459                                         $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_MICRO);
460                                         break;
461                                 case 'avatar':
462                                 default:
463                                         $default = Contact::getDefaultAvatar($contact, Proxy::SIZE_THUMB);
464                         }
465
466                         if (Network::isLocalLink($default)) {
467                                 System::externalRedirect($default);
468                         }
469
470                         $parts = parse_url($default);
471                         if (!empty($parts['scheme']) || !empty($parts['host'])) {
472                                 $photo = MPhoto::createPhotoForExternalResource($default);
473                         } else {
474                                 $photo = MPhoto::createPhotoForSystemResource($default);
475                         }
476                 }
477                 return $photo;
478         }
479
480         private static function getBannerForUser(int $uid): array
481         {
482                 $photo = MPhoto::selectFirst([], ['scale' => 3, 'uid' => $uid, 'photo-type' => MPhoto::USER_BANNER]);
483                 if (!empty($photo)) {
484                         return $photo;
485                 }
486                 return MPhoto::createPhotoForImageData(file_get_contents(DI::basePath() . (new Header(DI::config()))->getMastodonBannerPath()));
487         }
488 }