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