]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/Media.php
074cee5221217b6b743970a40323a18f59697598
[friendica.git] / src / Model / Post / Media.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\Model\Post;
23
24 use Friendica\Content\Text\BBCode;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\System;
28 use Friendica\Database\Database;
29 use Friendica\Database\DBA;
30 use Friendica\DI;
31 use Friendica\Model\Contact;
32 use Friendica\Model\Item;
33 use Friendica\Model\Photo;
34 use Friendica\Model\Post;
35 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
36 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
37 use Friendica\Util\Images;
38 use Friendica\Util\Network;
39 use Friendica\Util\ParseUrl;
40 use Friendica\Util\Proxy;
41 use Friendica\Util\Strings;
42
43 /**
44  * Class Media
45  *
46  * This Model class handles media interactions.
47  * This tables stores medias (images, videos, audio files) related to posts.
48  */
49 class Media
50 {
51         const UNKNOWN     = 0;
52         const IMAGE       = 1;
53         const VIDEO       = 2;
54         const AUDIO       = 3;
55         const TEXT        = 4;
56         const APPLICATION = 5;
57         const TORRENT     = 16;
58         const HTML        = 17;
59         const XML         = 18;
60         const PLAIN       = 19;
61         const ACTIVITY    = 20;
62         const ACCOUNT     = 21;
63         const DOCUMENT    = 128;
64
65         /**
66          * Insert a post-media record
67          *
68          * @param array $media
69          * @param bool  $force
70          * @return void
71          */
72         public static function insert(array $media, bool $force = false)
73         {
74                 if (empty($media['url']) || empty($media['uri-id']) || !isset($media['type'])) {
75                         Logger::warning('Incomplete media data', ['media' => $media]);
76                         return;
77                 }
78
79                 if (DBA::exists('post-media', ['uri-id' => $media['uri-id'], 'preview' => $media['url']])) {
80                         Logger::info('Media already exists as preview', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'callstack' => System::callstack()]);
81                         return;
82                 }
83
84                 // "document" has got the lowest priority. So when the same file is both attached as document
85                 // and embedded as picture then we only store the picture or replace the document
86                 $found = DBA::selectFirst('post-media', ['type'], ['uri-id' => $media['uri-id'], 'url' => $media['url']]);
87                 if (!$force && !empty($found) && (($found['type'] != self::DOCUMENT) || ($media['type'] == self::DOCUMENT))) {
88                         Logger::info('Media already exists', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'callstack' => System::callstack()]);
89                         return;
90                 }
91
92                 $media = self::unsetEmptyFields($media);
93                 $media = DI::dbaDefinition()->truncateFieldsForTable('post-media', $media);
94
95                 // We are storing as fast as possible to avoid duplicated network requests
96                 // when fetching additional information for pictures and other content.
97                 $result = DBA::insert('post-media', $media, Database::INSERT_UPDATE);
98                 Logger::info('Stored media', ['result' => $result, 'media' => $media, 'callstack' => System::callstack()]);
99                 $stored = $media;
100
101                 $media = self::fetchAdditionalData($media);
102                 $media = self::unsetEmptyFields($media);
103                 $media = DI::dbaDefinition()->truncateFieldsForTable('post-media', $media);
104
105                 if (array_diff_assoc($media, $stored)) {
106                         $result = DBA::insert('post-media', $media, Database::INSERT_UPDATE);
107                         Logger::info('Updated media', ['result' => $result, 'media' => $media]);
108                 } else {
109                         Logger::info('Nothing to update', ['media' => $media]);
110                 }
111         }
112
113         /**
114          * Remove empty media fields
115          *
116          * @param array $media
117          * @return array cleaned media array
118          */
119         private static function unsetEmptyFields(array $media): array
120         {
121                 $fields = ['mimetype', 'height', 'width', 'size', 'preview', 'preview-height', 'preview-width', 'blurhash', 'description'];
122                 foreach ($fields as $field) {
123                         if (empty($media[$field])) {
124                                 unset($media[$field]);
125                         }
126                 }
127                 return $media;
128         }
129
130         /**
131          * Copy attachments from one uri-id to another
132          *
133          * @param integer $from_uri_id
134          * @param integer $to_uri_id
135          * @return void
136          */
137         public static function copy(int $from_uri_id, int $to_uri_id)
138         {
139                 $attachments = self::getByURIId($from_uri_id);
140                 foreach ($attachments as $attachment) {
141                         $attachment['uri-id'] = $to_uri_id;
142                         self::insert($attachment);
143                 }
144         }
145
146         /**
147          * Creates the "[attach]" element from the given attributes
148          *
149          * @param string $href
150          * @param integer $length
151          * @param string $type
152          * @param string $title
153          * @return string "[attach]" element
154          */
155         public static function getAttachElement(string $href, int $length, string $type, string $title = ''): string
156         {
157                 $media = self::fetchAdditionalData(['type' => self::DOCUMENT, 'url' => $href,
158                         'size' => $length, 'mimetype' => $type, 'description' => $title]);
159
160                 return '[attach]href="' . $media['url'] . '" length="' . $media['size'] .
161                         '" type="' . $media['mimetype'] . '" title="' . $media['description'] . '"[/attach]';
162         }
163
164         /**
165          * Fetch additional data for the provided media array
166          *
167          * @param array $media
168          * @return array media array with additional data
169          */
170         public static function fetchAdditionalData(array $media): array
171         {
172                 if (Network::isLocalLink($media['url'])) {
173                         $media = self::fetchLocalData($media);
174                 }
175
176                 // Fetch the mimetype or size if missing.
177                 if (empty($media['mimetype']) || empty($media['size'])) {
178                         $timeout = DI::config()->get('system', 'xrd_timeout');
179                         $curlResult = DI::httpClient()->head($media['url'], [HttpClientOptions::TIMEOUT => $timeout]);
180
181                         // Workaround for systems that can't handle a HEAD request
182                         if (!$curlResult->isSuccess() && ($curlResult->getReturnCode() == 405)) {
183                                 $curlResult = DI::httpClient()->get($media['url'], HttpClientAccept::DEFAULT, [HttpClientOptions::TIMEOUT => $timeout]);
184                         }
185
186                         if ($curlResult->isSuccess()) {
187                                 if (empty($media['mimetype'])) {
188                                         $media['mimetype'] = $curlResult->getHeader('Content-Type')[0] ?? '';
189                                 }
190                                 if (empty($media['size'])) {
191                                         $media['size'] = (int)($curlResult->getHeader('Content-Length')[0] ?? 0);
192                                 }
193                         } else {
194                                 Logger::notice('Could not fetch head', ['media' => $media]);
195                         }
196                 }
197
198                 $filetype = !empty($media['mimetype']) ? strtolower(current(explode('/', $media['mimetype']))) : '';
199
200                 if (($media['type'] == self::IMAGE) || ($filetype == 'image')) {
201                         $imagedata = Images::getInfoFromURLCached($media['url']);
202                         if ($imagedata) {
203                                 $media['mimetype'] = $imagedata['mime'];
204                                 $media['size'] = $imagedata['size'];
205                                 $media['width'] = $imagedata[0];
206                                 $media['height'] = $imagedata[1];
207                                 $media['blurhash'] = $imagedata['blurhash'] ?? null;
208                         } else {
209                                 Logger::notice('No image data', ['media' => $media]);
210                         }
211                         if (!empty($media['preview'])) {
212                                 $imagedata = Images::getInfoFromURLCached($media['preview']);
213                                 if ($imagedata) {
214                                         $media['preview-width'] = $imagedata[0];
215                                         $media['preview-height'] = $imagedata[1];
216                                 }
217                         }
218                 }
219
220                 if ($media['type'] != self::DOCUMENT) {
221                         $media = self::addType($media);
222                 }
223
224                 if (in_array($media['type'], [self::TEXT, self::APPLICATION, self::HTML, self::XML, self::PLAIN])) {
225                         $media = self::addActivity($media);
226                 }
227
228                 if (in_array($media['type'], [self::TEXT, self::APPLICATION, self::HTML, self::XML, self::PLAIN])) {
229                         $media = self::addAccount($media);
230                 }
231
232                 if ($media['type'] == self::HTML) {
233                         $media = self::addPage($media);
234                 }
235
236                 return $media;
237         }
238
239         /**
240          * Adds the activity type if the media entry is linked to an activity
241          *
242          * @param array $media
243          * @return array
244          */
245         private static function addActivity(array $media): array
246         {
247                 $id = Item::fetchByLink($media['url']);
248                 if (empty($id)) {
249                         return $media;
250                 }
251
252                 $item = Post::selectFirst([], ['id' => $id, 'network' => Protocol::FEDERATED]);
253                 if (empty($item['id'])) {
254                         Logger::debug('Not a federated activity', ['id' => $id, 'uri-id' => $media['uri-id'], 'url' => $media['url']]);
255                         return $media;
256                 }
257
258                 if (!empty($item['plink']) && Strings::compareLink($item['plink'], $media['url']) &&
259                         parse_url($item['plink'], PHP_URL_HOST) != parse_url($item['uri'], PHP_URL_HOST)) {
260                         Logger::debug('Not a link to an activity', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'plink' => $item['plink'], 'uri' => $item['uri']]);
261                         return $media;
262                 }
263
264                 if (in_array($item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
265                         $media['mimetype'] = 'application/activity+json';
266                 } elseif ($item['network'] == Protocol::DIASPORA) {
267                         $media['mimetype'] = 'application/xml';
268                 }
269
270                 $contact = Contact::getById($item['author-id'], ['avatar', 'gsid']);
271                 if (!empty($contact['gsid'])) {
272                         $gserver = DBA::selectFirst('gserver', ['url', 'site_name'], ['id' => $contact['gsid']]);
273                 }
274
275                 $media['type'] = self::ACTIVITY;
276                 $media['media-uri-id'] = $item['uri-id'];
277                 $media['height'] = null;
278                 $media['width'] = null;
279                 $media['preview'] = null;
280                 $media['preview-height'] = null;
281                 $media['preview-width'] = null;
282                 $media['blurhash'] = null;
283                 $media['description'] = $item['body'];
284                 $media['name'] = $item['title'];
285                 $media['author-url'] = $item['author-link'];
286                 $media['author-name'] = $item['author-name'];
287                 $media['author-image'] = $contact['avatar'] ?? $item['author-avatar'];
288                 $media['publisher-url'] = $gserver['url'] ?? null;
289                 $media['publisher-name'] = $gserver['site_name'] ?? null;
290                 $media['publisher-image'] = null;
291
292                 Logger::debug('Activity detected', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'plink' => $item['plink'], 'uri' => $item['uri']]);
293                 return $media;
294         }
295
296         /**
297          * Adds the account type if the media entry is linked to an account
298          *
299          * @param array $media
300          * @return array
301          */
302         private static function addAccount(array $media): array
303         {
304                 $contact = Contact::getByURL($media['url'], false);
305                 if (empty($contact) || ($contact['network'] == Protocol::PHANTOM)) {
306                         return $media;
307                 }
308
309                 if (in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
310                         $media['mimetype'] = 'application/activity+json';
311                 }
312
313                 if (!empty($contact['gsid'])) {
314                         $gserver = DBA::selectFirst('gserver', ['url', 'site_name'], ['id' => $contact['gsid']]);
315                 }
316
317                 $media['type'] = self::ACCOUNT;
318                 $media['media-uri-id'] = $contact['uri-id'];
319                 $media['height'] = null;
320                 $media['width'] = null;
321                 $media['preview'] = null;
322                 $media['preview-height'] = null;
323                 $media['preview-width'] = null;
324                 $media['blurhash'] = null;
325                 $media['description'] = $contact['about'];
326                 $media['name'] = $contact['name'];
327                 $media['author-url'] = $contact['url'];
328                 $media['author-name'] = $contact['name'];
329                 $media['author-image'] = $contact['avatar'];
330                 $media['publisher-url'] = $gserver['url'] ?? null;
331                 $media['publisher-name'] = $gserver['site_name'] ?? null;
332                 $media['publisher-image'] = null;
333
334                 Logger::debug('Account detected', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'uri' => $contact['url']]);
335                 return $media;
336         }
337
338         /**
339          * Add page infos for HTML entries
340          *
341          * @param array $media
342          * @return array
343          */
344         private static function addPage(array $media): array
345         {
346                 $data = ParseUrl::getSiteinfoCached($media['url'], false);
347                 $media['preview'] = $data['images'][0]['src'] ?? null;
348                 $media['preview-height'] = $data['images'][0]['height'] ?? null;
349                 $media['preview-width'] = $data['images'][0]['width'] ?? null;
350                 $media['blurhash'] = $data['images'][0]['blurhash'] ?? null;
351                 $media['description'] = $data['text'] ?? null;
352                 $media['name'] = $data['title'] ?? null;
353                 $media['author-url'] = $data['author_url'] ?? null;
354                 $media['author-name'] = $data['author_name'] ?? null;
355                 $media['author-image'] = $data['author_img'] ?? null;
356                 $media['publisher-url'] = $data['publisher_url'] ?? null;
357                 $media['publisher-name'] = $data['publisher_name'] ?? null;
358                 $media['publisher-image'] = $data['publisher_img'] ?? null;
359
360                 return $media;
361         }
362
363         /**
364          * Fetch media data from local resources
365          * @param array $media
366          * @return array media with added data
367          */
368         private static function fetchLocalData(array $media): array
369         {
370                 if (!preg_match('|.*?/photo/(.*[a-fA-F0-9])\-(.*[0-9])\..*[\w]|', $media['url'] ?? '', $matches)) {
371                         return $media;
372                 }
373                 $photo = Photo::selectFirst([], ['resource-id' => $matches[1], 'scale' => $matches[2]]);
374                 if (!empty($photo)) {
375                         $media['mimetype'] = $photo['type'];
376                         $media['size'] = $photo['datasize'];
377                         $media['width'] = $photo['width'];
378                         $media['height'] = $photo['height'];
379                         $media['blurhash'] = $photo['blurhash'];
380                 }
381
382                 if (!preg_match('|.*?/photo/(.*[a-fA-F0-9])\-(.*[0-9])\..*[\w]|', $media['preview'] ?? '', $matches)) {
383                         return $media;
384                 }
385                 $photo = Photo::selectFirst([], ['resource-id' => $matches[1], 'scale' => $matches[2]]);
386                 if (!empty($photo)) {
387                         $media['preview-width'] = $photo['width'];
388                         $media['preview-height'] = $photo['height'];
389                 }
390
391                 return $media;
392         }
393
394         /**
395          * Add the detected type to the media array
396          *
397          * @param array $data
398          * @return array data array with the detected type
399          */
400         public static function addType(array $data): array
401         {
402                 if (empty($data['mimetype'])) {
403                         Logger::info('No MimeType provided', ['media' => $data]);
404                         return $data;
405                 }
406
407                 $type = explode('/', current(explode(';', $data['mimetype'])));
408                 if (count($type) < 2) {
409                         Logger::info('Unknown MimeType', ['type' => $type, 'media' => $data]);
410                         $data['type'] = self::UNKNOWN;
411                         return $data;
412                 }
413
414                 $filetype = strtolower($type[0]);
415                 $subtype = strtolower($type[1]);
416
417                 if ($filetype == 'image') {
418                         $data['type'] = self::IMAGE;
419                 } elseif ($filetype == 'video') {
420                         $data['type'] = self::VIDEO;
421                 } elseif ($filetype == 'audio') {
422                         $data['type'] = self::AUDIO;
423                 } elseif (($filetype == 'text') && ($subtype == 'html')) {
424                         $data['type'] = self::HTML;
425                 } elseif (($filetype == 'text') && ($subtype == 'xml')) {
426                         $data['type'] = self::XML;
427                 } elseif (($filetype == 'text') && ($subtype == 'plain')) {
428                         $data['type'] = self::PLAIN;
429                 } elseif ($filetype == 'text') {
430                         $data['type'] = self::TEXT;
431                 } elseif (($filetype == 'application') && ($subtype == 'x-bittorrent')) {
432                         $data['type'] = self::TORRENT;
433                 } elseif ($filetype == 'application') {
434                         $data['type'] = self::APPLICATION;
435                 } else {
436                         $data['type'] = self::UNKNOWN;
437                         Logger::info('Unknown type', ['filetype' => $filetype, 'subtype' => $subtype, 'media' => $data]);
438                         return $data;
439                 }
440
441                 Logger::debug('Detected type', ['filetype' => $filetype, 'subtype' => $subtype, 'media' => $data]);
442                 return $data;
443         }
444
445         /**
446          * Tests for path patterns that are usef for picture links in Friendica
447          *
448          * @param string $page    Link to the image page
449          * @param string $preview Preview picture
450          * @return boolean
451          */
452         private static function isPictureLink(string $page, string $preview): bool
453         {
454                 return preg_match('#/photos/.*/image/#ism', $page) && preg_match('#/photo/.*-1\.#ism', $preview);
455         }
456
457         /**
458          * Add media links and remove them from the body
459          *
460          * @param integer $uriid
461          * @param string $body
462          * @return string Body without media links
463          */
464         public static function insertFromBody(int $uriid, string $body, bool $endmatch = false): string
465         {
466                 $endmatchpattern = $endmatch ? '\z' : '';
467                 // Simplify image codes
468                 $unshared_body = $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]$endmatchpattern/ism", '[img]$3[/img]', $body);
469
470                 $attachments = [];
471                 if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]\s*\[/url\]$endmatchpattern#ism", $body, $pictures, PREG_SET_ORDER)) {
472                         foreach ($pictures as $picture) {
473                                 if (!self::isPictureLink($picture[1], $picture[2])) {
474                                         continue;
475                                 }
476                                 $body = str_replace($picture[0], '', $body);
477                                 $image = str_replace('-1.', '-0.', $picture[2]);
478                                 $attachments[$image] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $image,
479                                         'preview' => $picture[2], 'description' => $picture[3]];
480                         }
481                 }
482
483                 if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]$endmatchpattern/Usi", $body, $pictures, PREG_SET_ORDER)) {
484                         foreach ($pictures as $picture) {
485                                 $body = str_replace($picture[0], '', $body);
486                                 $attachments[$picture[1]] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1], 'description' => $picture[2]];
487                         }
488                 }
489
490                 if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]$endmatchpattern#ism", $body, $pictures, PREG_SET_ORDER)) {
491                         foreach ($pictures as $picture) {
492                                 if (!self::isPictureLink($picture[1], $picture[2])) {
493                                         continue;
494                                 }
495                                 $body = str_replace($picture[0], '', $body);
496                                 $image = str_replace('-1.', '-0.', $picture[2]);
497                                 $attachments[$image] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $image,
498                                         'preview' => $picture[2], 'description' => null];
499                         }
500                 }
501
502                 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]$endmatchpattern/ism", $body, $pictures, PREG_SET_ORDER)) {
503                         foreach ($pictures as $picture) {
504                                 $body = str_replace($picture[0], '', $body);
505                                 $attachments[$picture[1]] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1]];
506                         }
507                 }
508
509                 if (preg_match_all("/\[audio\]([^\[\]]*)\[\/audio\]$endmatchpattern/ism", $body, $audios, PREG_SET_ORDER)) {
510                         foreach ($audios as $audio) {
511                                 $body = str_replace($audio[0], '', $body);
512                                 $attachments[$audio[1]] = ['uri-id' => $uriid, 'type' => self::AUDIO, 'url' => $audio[1]];
513                         }
514                 }
515
516                 if (preg_match_all("/\[video\]([^\[\]]*)\[\/video\]$endmatchpattern/ism", $body, $videos, PREG_SET_ORDER)) {
517                         foreach ($videos as $video) {
518                                 $body = str_replace($video[0], '', $body);
519                                 $attachments[$video[1]] = ['uri-id' => $uriid, 'type' => self::VIDEO, 'url' => $video[1]];
520                         }
521                 }
522
523                 if ($uriid != 0) {
524                         foreach ($attachments as $attachment) {
525                                 if (Post\Link::exists($uriid, $attachment['preview'] ?? $attachment['url'])) {
526                                         continue;
527                                 }
528
529                                 // Only store attachments that are part of the unshared body
530                                 if (Item::containsLink($unshared_body, $attachment['preview'] ?? $attachment['url'], $attachment['type'])) {
531                                         self::insert($attachment);
532                                 }
533                         }
534                 }
535
536                 return trim($body);
537         }
538
539         /**
540          * Remove media that is at the end of the body
541          *
542          * @param string $body
543          * @return string
544          */
545         public static function removeFromEndOfBody(string $body): string
546         {
547                 do {
548                         $prebody = $body;
549                         $body = self::insertFromBody(0, $body, true);
550                 } while ($prebody != $body);
551                 return $body;
552         }
553
554         /**
555          * Add media links from a relevant url in the body
556          *
557          * @param integer $uriid
558          * @param string $body
559          * @return void
560          */
561         public static function insertFromRelevantUrl(int $uriid, string $body, string $fullbody, string $network)
562         {
563                 // Remove all hashtags and mentions
564                 $body = preg_replace("/([#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", '', $body);
565
566                 // Search for pure links
567                 if (preg_match_all("/\[url\](https?:.*?)\[\/url\]/ism", $body, $matches)) {
568                         foreach ($matches[1] as $url) {
569                                 Logger::info('Got page url (link without description)', ['uri-id' => $uriid, 'url' => $url]);
570                                 self::insert(['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url], false, $network);
571                                 if ($network == Protocol::DFRN) {
572                                         self::revertHTMLType($uriid, $url, $fullbody);
573                                 }
574                         }
575                 }
576
577                 // Search for links with descriptions
578                 if (preg_match_all("/\[url\=(https?:.*?)\].*?\[\/url\]/ism", $body, $matches)) {
579                         foreach ($matches[1] as $url) {
580                                 Logger::info('Got page url (link with description)', ['uri-id' => $uriid, 'url' => $url]);
581                                 self::insert(['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url], false, $network);
582                                 if ($network == Protocol::DFRN) {
583                                         self::revertHTMLType($uriid, $url, $fullbody);
584                                 }
585                         }
586                 }
587         }
588
589         /**
590          * Revert the media type of links to UNKNOWN for DFRN posts when they aren't attached
591          *
592          * @param integer $uriid
593          * @param string $url
594          * @param string $body
595          * @return void
596          */
597         private static function revertHTMLType(int $uriid, string $url, string $body)
598         {
599                 $attachment = BBCode::getAttachmentData($body);
600                 if (!empty($attachment['url']) && Network::getUrlMatch($attachment['url'], $url)) {
601                         return;
602                 }
603                 DBA::update('post-media', ['type' => self::UNKNOWN], ['uri-id' => $uriid, 'type' => self::HTML, 'url' => $url]);
604         }
605
606         /**
607          * Add media links from the attachment field
608          *
609          * @param integer $uriid
610          * @param string $body
611          * @return void
612          */
613         public static function insertFromAttachmentData(int $uriid, string $body)
614         {
615                 $data = BBCode::getAttachmentData($body);
616                 if (empty($data))  {
617                         return;
618                 }
619
620                 Logger::info('Adding attachment data', ['data' => $data]);
621                 $attachment = [
622                         'uri-id' => $uriid,
623                         'type' => self::HTML,
624                         'url' => $data['url'],
625                         'preview' => $data['preview'] ?? null,
626                         'description' => $data['description'] ?? null,
627                         'name' => $data['title'] ?? null,
628                         'author-url' => $data['author_url'] ?? null,
629                         'author-name' => $data['author_name'] ?? null,
630                         'publisher-url' => $data['provider_url'] ?? null,
631                         'publisher-name' => $data['provider_name'] ?? null,
632                 ];
633                 if (!empty($data['image'])) {
634                         $attachment['preview'] = $data['image'];
635                 }
636                 self::insert($attachment);
637         }
638
639         /**
640          * Add media links from the attach field
641          *
642          * @param integer $uriid
643          * @param string $attach
644          * @return void
645          */
646         public static function insertFromAttachment(int $uriid, string $attach)
647         {
648                 if (!preg_match_all('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\"(?: title=\"(.*?)\")?|', $attach, $matches, PREG_SET_ORDER)) {
649                         return;
650                 }
651
652                 foreach ($matches as $attachment) {
653                         $media['type'] = self::DOCUMENT;
654                         $media['uri-id'] = $uriid;
655                         $media['url'] = $attachment[1];
656                         $media['size'] = $attachment[2];
657                         $media['mimetype'] = $attachment[3];
658                         $media['description'] = $attachment[4] ?? '';
659
660                         self::insert($media);
661                 }
662         }
663
664         /**
665          * Retrieves the media attachments associated with the provided item ID.
666          *
667          * @param int $uri_id URI id
668          * @param array $types Media types
669          * @return array|bool Array on success, false on error
670          * @throws \Exception
671          */
672         public static function getByURIId(int $uri_id, array $types = [])
673         {
674                 $condition = ["`uri-id` = ? AND `type` != ?", $uri_id, self::UNKNOWN];
675
676                 if (!empty($types)) {
677                         $condition = DBA::mergeConditions($condition, ['type' => $types]);
678                 }
679
680                 return DBA::selectToArray('post-media', [], $condition, ['order' => ['id']]);
681         }
682
683         /**
684          * Checks if media attachments are associated with the provided item ID.
685          *
686          * @param int $uri_id URI id
687          * @param array $types Media types
688          * @return bool Whether media attachment exists
689          * @throws \Exception
690          */
691         public static function existsByURIId(int $uri_id, array $types = []): bool
692         {
693                 $condition = ["`uri-id` = ? AND `type` != ?", $uri_id, self::UNKNOWN];
694
695                 if (!empty($types)) {
696                         $condition = DBA::mergeConditions($condition, ['type' => $types]);
697                 }
698
699                 return DBA::exists('post-media', $condition);
700         }
701
702         /**
703          * Split the attachment media in the three segments "visual", "link" and "additional"
704          *
705          * @param int    $uri_id URI id
706          * @param array  $links list of links that shouldn't be added
707          * @param bool   $has_media
708          * @return array attachments
709          */
710         public static function splitAttachments(int $uri_id, array $links = [], bool $has_media = true): array
711         {
712                 $attachments = ['visual' => [], 'link' => [], 'additional' => []];
713
714                 if (!$has_media) {
715                         return $attachments;
716                 }
717
718                 $media = self::getByURIId($uri_id);
719                 if (empty($media)) {
720                         return $attachments;
721                 }
722
723                 $heights = [];
724                 $selected = '';
725                 $previews = [];
726
727                 foreach ($media as $medium) {
728                         foreach ($links as $link) {
729                                 if (Strings::compareLink($link, $medium['url'])) {
730                                         continue 2;
731                                 }
732                         }
733
734                         // Avoid adding separate media entries for previews
735                         foreach ($previews as $preview) {
736                                 if (Strings::compareLink($preview, $medium['url'])) {
737                                         continue 2;
738                                 }
739                         }
740
741                         // Currently these two types are ignored here.
742                         // Posts are added differently and contacts are not displayed as attachments.
743                         if (in_array($medium['type'], [self::ACCOUNT, self::ACTIVITY])) {
744                                 continue;
745                         }
746
747                         if (!empty($medium['preview'])) {
748                                 $previews[] = $medium['preview'];
749                         }
750
751                         $type = explode('/', explode(';', $medium['mimetype'] ?? '')[0]);
752                         if (count($type) < 2) {
753                                 Logger::info('Unknown MimeType', ['type' => $type, 'media' => $medium]);
754                                 $filetype = 'unkn';
755                                 $subtype = 'unkn';
756                         } else {
757                                 $filetype = strtolower($type[0]);
758                                 $subtype = strtolower($type[1]);
759                         }
760
761                         $medium['filetype'] = $filetype;
762                         $medium['subtype'] = $subtype;
763
764                         if ($medium['type'] == self::HTML || (($filetype == 'text') && ($subtype == 'html'))) {
765                                 $attachments['link'][] = $medium;
766                                 continue;
767                         }
768
769                         if (in_array($medium['type'], [self::AUDIO, self::IMAGE]) ||
770                                 in_array($filetype, ['audio', 'image'])) {
771                                 $attachments['visual'][] = $medium;
772                         } elseif (($medium['type'] == self::VIDEO) || ($filetype == 'video')) {
773                                 if (!empty($medium['height'])) {
774                                         // Peertube videos are delivered in many different resolutions. We pick a moderate one.
775                                         // Since only Peertube provides a "height" parameter, this wouldn't be executed
776                                         // when someone for example on Mastodon was sharing multiple videos in a single post.
777                                         $heights[$medium['height']] = $medium['url'];
778                                         $video[$medium['url']] = $medium;
779                                 } else {
780                                         $attachments['visual'][] = $medium;
781                                 }
782                         } else {
783                                 $attachments['additional'][] = $medium;
784                         }
785                 }
786
787                 if (!empty($heights)) {
788                         ksort($heights);
789                         foreach ($heights as $height => $url) {
790                                 if (empty($selected) || $height <= 480) {
791                                         $selected = $url;
792                                 }
793                         }
794
795                         if (!empty($selected)) {
796                                 $attachments['visual'][] = $video[$selected];
797                                 unset($video[$selected]);
798                                 foreach ($video as $element) {
799                                         $attachments['additional'][] = $element;
800                                 }
801                         }
802                 }
803
804                 return $attachments;
805         }
806
807         /**
808          * Add media attachments to the body
809          *
810          * @param int    $uriid
811          * @param string $body
812          * @param array  $types
813          *
814          * @return string body
815          */
816         public static function addAttachmentsToBody(int $uriid, string $body = '', array $types = [self::IMAGE, self::AUDIO, self::VIDEO]): string
817         {
818                 if (empty($body)) {
819                         $item = Post::selectFirst(['body'], ['uri-id' => $uriid]);
820                         if (!DBA::isResult($item)) {
821                                 return '';
822                         }
823                         $body = $item['body'];
824                 }
825                 $original_body = $body;
826
827                 $body = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", '', $body);
828
829                 foreach (self::getByURIId($uriid, $types) as $media) {
830                         if (Item::containsLink($body, $media['preview'] ?? $media['url'], $media['type'])) {
831                                 continue;
832                         }
833
834                         if ($media['type'] == self::IMAGE) {
835                                 if (!empty($media['preview'])) {
836                                         if (!empty($media['description'])) {
837                                                 $body .= "\n[url=" . $media['url'] . "][img=" . $media['preview'] . ']' . $media['description'] .'[/img][/url]';
838                                         } else {
839                                                 $body .= "\n[url=" . $media['url'] . "][img]" . $media['preview'] .'[/img][/url]';
840                                         }
841                                 } else {
842                                         if (!empty($media['description'])) {
843                                                 $body .= "\n[img=" . $media['url'] . ']' . $media['description'] .'[/img]';
844                                         } else {
845                                                 $body .= "\n[img]" . $media['url'] .'[/img]';
846                                         }
847                                 }
848                         } elseif ($media['type'] == self::AUDIO) {
849                                 $body .= "\n[audio]" . $media['url'] . "[/audio]\n";
850                         } elseif ($media['type'] == self::VIDEO) {
851                                 $body .= "\n[video]" . $media['url'] . "[/video]\n";
852                         }
853                 }
854
855                 if (preg_match("/.*(\[attachment.*?\].*?\[\/attachment\]).*/ism", $original_body, $match)) {
856                         $body .= "\n" . $match[1];
857                 }
858
859                 return $body;
860         }
861
862         /**
863          * Get preview link for given media id
864          *
865          * @param integer $id   media id
866          * @param string  $size One of the Proxy::SIZE_* constants
867          * @return string preview link
868          */
869         public static function getPreviewUrlForId(int $id, string $size = ''): string
870         {
871                 $url = DI::baseUrl() . '/photo/preview/';
872                 switch ($size) {
873                         case Proxy::SIZE_MICRO:
874                                 $url .= Proxy::PIXEL_MICRO . '/';
875                                 break;
876                         case Proxy::SIZE_THUMB:
877                                 $url .= Proxy::PIXEL_THUMB . '/';
878                                 break;
879                         case Proxy::SIZE_SMALL:
880                                 $url .= Proxy::PIXEL_SMALL . '/';
881                                 break;
882                         case Proxy::SIZE_MEDIUM:
883                                 $url .= Proxy::PIXEL_MEDIUM . '/';
884                                 break;
885                         case Proxy::SIZE_LARGE:
886                                 $url .= Proxy::PIXEL_LARGE . '/';
887                                 break;
888                 }
889                 return $url . $id;
890         }
891
892         /**
893          * Get media link for given media id
894          *
895          * @param integer $id   media id
896          * @param string  $size One of the Proxy::SIZE_* constants
897          * @return string media link
898          */
899         public static function getUrlForId(int $id, string $size = ''): string
900         {
901                 $url = DI::baseUrl() . '/photo/media/';
902                 switch ($size) {
903                         case Proxy::SIZE_MICRO:
904                                 $url .= Proxy::PIXEL_MICRO . '/';
905                                 break;
906                         case Proxy::SIZE_THUMB:
907                                 $url .= Proxy::PIXEL_THUMB . '/';
908                                 break;
909                         case Proxy::SIZE_SMALL:
910                                 $url .= Proxy::PIXEL_SMALL . '/';
911                                 break;
912                         case Proxy::SIZE_MEDIUM:
913                                 $url .= Proxy::PIXEL_MEDIUM . '/';
914                                 break;
915                         case Proxy::SIZE_LARGE:
916                                 $url .= Proxy::PIXEL_LARGE . '/';
917                                 break;
918                 }
919                 return $url . $id;
920         }
921 }