]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/Media.php
5dfb227d643d18a14acbc8aa12115723fe2d5f19
[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(['type' => self::DOCUMENT, 'url' => $href,
166                         'size' => $length, 'mimetype' => $type, 'description' => $title]);
167
168                 return '[attach]href="' . $media['url'] . '" length="' . $media['size'] .
169                         '" type="' . $media['mimetype'] . '" title="' . $media['description'] . '"[/attach]';
170         }
171
172         /**
173          * Fetch additional data for the provided media array
174          *
175          * @param array $media
176          * @return array media array with additional data
177          */
178         public static function fetchAdditionalData(array $media): array
179         {
180                 if (Network::isLocalLink($media['url'])) {
181                         $media = self::fetchLocalData($media);
182                 }
183
184                 // Fetch the mimetype or size if missing.
185                 if (Network::isValidHttpUrl($media['url']) && (empty($media['mimetype']) || empty($media['size']))) {
186                         $timeout = DI::config()->get('system', 'xrd_timeout');
187                         $curlResult = DI::httpClient()->head($media['url'], [HttpClientOptions::TIMEOUT => $timeout]);
188
189                         // Workaround for systems that can't handle a HEAD request
190                         if (!$curlResult->isSuccess() && ($curlResult->getReturnCode() == 405)) {
191                                 $curlResult = DI::httpClient()->get($media['url'], HttpClientAccept::DEFAULT, [HttpClientOptions::TIMEOUT => $timeout]);
192                         }
193
194                         if ($curlResult->isSuccess()) {
195                                 if (empty($media['mimetype'])) {
196                                         $media['mimetype'] = $curlResult->getHeader('Content-Type')[0] ?? '';
197                                 }
198                                 if (empty($media['size'])) {
199                                         $media['size'] = (int)($curlResult->getHeader('Content-Length')[0] ?? 0);
200                                 }
201                         } else {
202                                 Logger::notice('Could not fetch head', ['media' => $media]);
203                         }
204                 }
205
206                 $filetype = !empty($media['mimetype']) ? strtolower(current(explode('/', $media['mimetype']))) : '';
207
208                 if (($media['type'] == self::IMAGE) || ($filetype == 'image')) {
209                         $imagedata = Images::getInfoFromURLCached($media['url']);
210                         if ($imagedata) {
211                                 $media['mimetype'] = $imagedata['mime'];
212                                 $media['size'] = $imagedata['size'];
213                                 $media['width'] = $imagedata[0];
214                                 $media['height'] = $imagedata[1];
215                                 $media['blurhash'] = $imagedata['blurhash'] ?? null;
216                         } else {
217                                 Logger::notice('No image data', ['media' => $media]);
218                         }
219                         if (!empty($media['preview'])) {
220                                 $imagedata = Images::getInfoFromURLCached($media['preview']);
221                                 if ($imagedata) {
222                                         $media['preview-width'] = $imagedata[0];
223                                         $media['preview-height'] = $imagedata[1];
224                                 }
225                         }
226                 }
227
228                 if ($media['type'] != self::DOCUMENT) {
229                         $media = self::addType($media);
230                 }
231
232                 if (in_array($media['type'], [self::TEXT, self::APPLICATION, self::HTML, self::XML, self::PLAIN])) {
233                         $media = self::addActivity($media);
234                 }
235
236                 if (in_array($media['type'], [self::TEXT, self::APPLICATION, self::HTML, self::XML, self::PLAIN])) {
237                         $media = self::addAccount($media);
238                 }
239
240                 if ($media['type'] == self::HTML) {
241                         $media = self::addPage($media);
242                 }
243
244                 return $media;
245         }
246
247         /**
248          * Adds the activity type if the media entry is linked to an activity
249          *
250          * @param array $media
251          * @return array
252          */
253         private static function addActivity(array $media): array
254         {
255                 $id = Item::fetchByLink($media['url']);
256                 if (empty($id)) {
257                         return $media;
258                 }
259
260                 $item = Post::selectFirst([], ['id' => $id, 'network' => Protocol::FEDERATED]);
261                 if (empty($item['id'])) {
262                         Logger::debug('Not a federated activity', ['id' => $id, 'uri-id' => $media['uri-id'], 'url' => $media['url']]);
263                         return $media;
264                 }
265
266                 if (!empty($item['plink']) && Strings::compareLink($item['plink'], $media['url']) &&
267                         parse_url($item['plink'], PHP_URL_HOST) != parse_url($item['uri'], PHP_URL_HOST)) {
268                         Logger::debug('Not a link to an activity', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'plink' => $item['plink'], 'uri' => $item['uri']]);
269                         return $media;
270                 }
271
272                 if (in_array($item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
273                         $media['mimetype'] = 'application/activity+json';
274                 } elseif ($item['network'] == Protocol::DIASPORA) {
275                         $media['mimetype'] = 'application/xml';
276                 }
277
278                 $contact = Contact::getById($item['author-id'], ['avatar', 'gsid']);
279                 if (!empty($contact['gsid'])) {
280                         $gserver = DBA::selectFirst('gserver', ['url', 'site_name'], ['id' => $contact['gsid']]);
281                 }
282
283                 $media['type'] = self::ACTIVITY;
284                 $media['media-uri-id'] = $item['uri-id'];
285                 $media['height'] = null;
286                 $media['width'] = null;
287                 $media['preview'] = null;
288                 $media['preview-height'] = null;
289                 $media['preview-width'] = null;
290                 $media['blurhash'] = null;
291                 $media['description'] = $item['body'];
292                 $media['name'] = $item['title'];
293                 $media['author-url'] = $item['author-link'];
294                 $media['author-name'] = $item['author-name'];
295                 $media['author-image'] = $contact['avatar'] ?? $item['author-avatar'];
296                 $media['publisher-url'] = $gserver['url'] ?? null;
297                 $media['publisher-name'] = $gserver['site_name'] ?? null;
298                 $media['publisher-image'] = null;
299
300                 Logger::debug('Activity detected', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'plink' => $item['plink'], 'uri' => $item['uri']]);
301                 return $media;
302         }
303
304         /**
305          * Adds the account type if the media entry is linked to an account
306          *
307          * @param array $media
308          * @return array
309          */
310         private static function addAccount(array $media): array
311         {
312                 $contact = Contact::getByURL($media['url'], false);
313                 if (empty($contact) || ($contact['network'] == Protocol::PHANTOM)) {
314                         return $media;
315                 }
316
317                 if (in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
318                         $media['mimetype'] = 'application/activity+json';
319                 }
320
321                 if (!empty($contact['gsid'])) {
322                         $gserver = DBA::selectFirst('gserver', ['url', 'site_name'], ['id' => $contact['gsid']]);
323                 }
324
325                 $media['type'] = self::ACCOUNT;
326                 $media['media-uri-id'] = $contact['uri-id'];
327                 $media['height'] = null;
328                 $media['width'] = null;
329                 $media['preview'] = null;
330                 $media['preview-height'] = null;
331                 $media['preview-width'] = null;
332                 $media['blurhash'] = null;
333                 $media['description'] = $contact['about'];
334                 $media['name'] = $contact['name'];
335                 $media['author-url'] = $contact['url'];
336                 $media['author-name'] = $contact['name'];
337                 $media['author-image'] = $contact['avatar'];
338                 $media['publisher-url'] = $gserver['url'] ?? null;
339                 $media['publisher-name'] = $gserver['site_name'] ?? null;
340                 $media['publisher-image'] = null;
341
342                 Logger::debug('Account detected', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'uri' => $contact['url']]);
343                 return $media;
344         }
345
346         /**
347          * Add page infos for HTML entries
348          *
349          * @param array $media
350          * @return array
351          */
352         private static function addPage(array $media): array
353         {
354                 $data = ParseUrl::getSiteinfoCached($media['url'], false);
355                 $media['preview'] = $data['images'][0]['src'] ?? null;
356                 $media['preview-height'] = $data['images'][0]['height'] ?? null;
357                 $media['preview-width'] = $data['images'][0]['width'] ?? null;
358                 $media['blurhash'] = $data['images'][0]['blurhash'] ?? null;
359                 $media['description'] = $data['text'] ?? null;
360                 $media['name'] = $data['title'] ?? null;
361                 $media['author-url'] = $data['author_url'] ?? null;
362                 $media['author-name'] = $data['author_name'] ?? null;
363                 $media['author-image'] = $data['author_img'] ?? null;
364                 $media['publisher-url'] = $data['publisher_url'] ?? null;
365                 $media['publisher-name'] = $data['publisher_name'] ?? null;
366                 $media['publisher-image'] = $data['publisher_img'] ?? null;
367
368                 return $media;
369         }
370
371         /**
372          * Fetch media data from local resources
373          * @param array $media
374          * @return array media with added data
375          */
376         private static function fetchLocalData(array $media): array
377         {
378                 if (!preg_match('|.*?/photo/(.*[a-fA-F0-9])\-(.*[0-9])\..*[\w]|', $media['url'] ?? '', $matches)) {
379                         return $media;
380                 }
381                 $photo = Photo::selectFirst([], ['resource-id' => $matches[1], 'scale' => $matches[2]]);
382                 if (!empty($photo)) {
383                         $media['mimetype'] = $photo['type'];
384                         $media['size'] = $photo['datasize'];
385                         $media['width'] = $photo['width'];
386                         $media['height'] = $photo['height'];
387                         $media['blurhash'] = $photo['blurhash'];
388                 }
389
390                 if (!preg_match('|.*?/photo/(.*[a-fA-F0-9])\-(.*[0-9])\..*[\w]|', $media['preview'] ?? '', $matches)) {
391                         return $media;
392                 }
393                 $photo = Photo::selectFirst([], ['resource-id' => $matches[1], 'scale' => $matches[2]]);
394                 if (!empty($photo)) {
395                         $media['preview-width'] = $photo['width'];
396                         $media['preview-height'] = $photo['height'];
397                 }
398
399                 return $media;
400         }
401
402         /**
403          * Add the detected type to the media array
404          *
405          * @param array $data
406          * @return array data array with the detected type
407          */
408         public static function addType(array $data): array
409         {
410                 if (empty($data['mimetype'])) {
411                         Logger::info('No MimeType provided', ['media' => $data]);
412                         return $data;
413                 }
414
415                 $type = explode('/', current(explode(';', $data['mimetype'])));
416                 if (count($type) < 2) {
417                         Logger::info('Unknown MimeType', ['type' => $type, 'media' => $data]);
418                         $data['type'] = self::UNKNOWN;
419                         return $data;
420                 }
421
422                 $filetype = strtolower($type[0]);
423                 $subtype = strtolower($type[1]);
424
425                 if ($filetype == 'image') {
426                         $data['type'] = self::IMAGE;
427                 } elseif ($filetype == 'video') {
428                         $data['type'] = self::VIDEO;
429                 } elseif ($filetype == 'audio') {
430                         $data['type'] = self::AUDIO;
431                 } elseif (($filetype == 'text') && ($subtype == 'html')) {
432                         $data['type'] = self::HTML;
433                 } elseif (($filetype == 'text') && ($subtype == 'xml')) {
434                         $data['type'] = self::XML;
435                 } elseif (($filetype == 'text') && ($subtype == 'plain')) {
436                         $data['type'] = self::PLAIN;
437                 } elseif ($filetype == 'text') {
438                         $data['type'] = self::TEXT;
439                 } elseif (($filetype == 'application') && ($subtype == 'x-bittorrent')) {
440                         $data['type'] = self::TORRENT;
441                 } elseif ($filetype == 'application') {
442                         $data['type'] = self::APPLICATION;
443                 } else {
444                         $data['type'] = self::UNKNOWN;
445                         Logger::info('Unknown type', ['filetype' => $filetype, 'subtype' => $subtype, 'media' => $data]);
446                         return $data;
447                 }
448
449                 Logger::debug('Detected type', ['filetype' => $filetype, 'subtype' => $subtype, 'media' => $data]);
450                 return $data;
451         }
452
453         /**
454          * Tests for path patterns that are usef for picture links in Friendica
455          *
456          * @param string $page    Link to the image page
457          * @param string $preview Preview picture
458          * @return boolean
459          */
460         private static function isPictureLink(string $page, string $preview): bool
461         {
462                 return preg_match('#/photos/.*/image/#ism', $page) && preg_match('#/photo/.*-1\.#ism', $preview);
463         }
464
465         /**
466          * Add media links and remove them from the body
467          *
468          * @param integer $uriid
469          * @param string $body
470          * @return string Body without media links
471          */
472         public static function insertFromBody(int $uriid, string $body, bool $endmatch = false): string
473         {
474                 $endmatchpattern = $endmatch ? '\z' : '';
475                 // Simplify image codes
476                 $unshared_body = $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]$endmatchpattern/ism", '[img]$3[/img]', $body);
477
478                 $attachments = [];
479                 if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]\s*\[/url\]$endmatchpattern#ism", $body, $pictures, PREG_SET_ORDER)) {
480                         foreach ($pictures as $picture) {
481                                 if (!self::isPictureLink($picture[1], $picture[2])) {
482                                         continue;
483                                 }
484                                 $body = str_replace($picture[0], '', $body);
485                                 $image = str_replace('-1.', '-0.', $picture[2]);
486                                 $attachments[$image] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $image,
487                                         'preview' => $picture[2], 'description' => $picture[3]];
488                         }
489                 }
490
491                 if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]$endmatchpattern/Usi", $body, $pictures, PREG_SET_ORDER)) {
492                         foreach ($pictures as $picture) {
493                                 $body = str_replace($picture[0], '', $body);
494                                 $attachments[$picture[1]] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1], 'description' => $picture[2]];
495                         }
496                 }
497
498                 if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]$endmatchpattern#ism", $body, $pictures, PREG_SET_ORDER)) {
499                         foreach ($pictures as $picture) {
500                                 if (!self::isPictureLink($picture[1], $picture[2])) {
501                                         continue;
502                                 }
503                                 $body = str_replace($picture[0], '', $body);
504                                 $image = str_replace('-1.', '-0.', $picture[2]);
505                                 $attachments[$image] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $image,
506                                         'preview' => $picture[2], 'description' => null];
507                         }
508                 }
509
510                 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]$endmatchpattern/ism", $body, $pictures, PREG_SET_ORDER)) {
511                         foreach ($pictures as $picture) {
512                                 $body = str_replace($picture[0], '', $body);
513                                 $attachments[$picture[1]] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1]];
514                         }
515                 }
516
517                 if (preg_match_all("/\[audio\]([^\[\]]*)\[\/audio\]$endmatchpattern/ism", $body, $audios, PREG_SET_ORDER)) {
518                         foreach ($audios as $audio) {
519                                 $body = str_replace($audio[0], '', $body);
520                                 $attachments[$audio[1]] = ['uri-id' => $uriid, 'type' => self::AUDIO, 'url' => $audio[1]];
521                         }
522                 }
523
524                 if (preg_match_all("/\[video\]([^\[\]]*)\[\/video\]$endmatchpattern/ism", $body, $videos, PREG_SET_ORDER)) {
525                         foreach ($videos as $video) {
526                                 $body = str_replace($video[0], '', $body);
527                                 $attachments[$video[1]] = ['uri-id' => $uriid, 'type' => self::VIDEO, 'url' => $video[1]];
528                         }
529                 }
530
531                 if ($uriid != 0) {
532                         foreach ($attachments as $attachment) {
533                                 if (Post\Link::exists($uriid, $attachment['preview'] ?? $attachment['url'])) {
534                                         continue;
535                                 }
536
537                                 // Only store attachments that are part of the unshared body
538                                 if (Item::containsLink($unshared_body, $attachment['preview'] ?? $attachment['url'], $attachment['type'])) {
539                                         self::insert($attachment);
540                                 }
541                         }
542                 }
543
544                 return trim($body);
545         }
546
547         /**
548          * Remove media that is at the end of the body
549          *
550          * @param string $body
551          * @return string
552          */
553         public static function removeFromEndOfBody(string $body): string
554         {
555                 do {
556                         $prebody = $body;
557                         $body = self::insertFromBody(0, $body, true);
558                 } while ($prebody != $body);
559                 return $body;
560         }
561
562         /**
563          * Add media links from a relevant url in the body
564          *
565          * @param integer $uriid
566          * @param string $body
567          * @return void
568          */
569         public static function insertFromRelevantUrl(int $uriid, string $body, string $fullbody, string $network)
570         {
571                 // Remove all hashtags and mentions
572                 $body = preg_replace("/([#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", '', $body);
573
574                 // Search for pure links
575                 if (preg_match_all("/\[url\](https?:.*?)\[\/url\]/ism", $body, $matches)) {
576                         foreach ($matches[1] as $url) {
577                                 Logger::info('Got page url (link without description)', ['uri-id' => $uriid, 'url' => $url]);
578                                 $result = self::insert(['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url], false, $network);
579                                 if ($result && ($network == Protocol::DFRN)) {
580                                         self::revertHTMLType($uriid, $url, $fullbody);
581                                         Logger::debug('Revert HTML type', ['uri-id' => $uriid, 'url' => $url]);
582                                 } elseif ($result) {
583                                         Logger::debug('Media had been added', ['uri-id' => $uriid, 'url' => $url]);
584                                 } else {
585                                         Logger::debug('Media had not been added', ['uri-id' => $uriid, 'url' => $url]);
586                                 }
587                         }
588                 }
589
590                 // Search for links with descriptions
591                 if (preg_match_all("/\[url\=(https?:.*?)\].*?\[\/url\]/ism", $body, $matches)) {
592                         foreach ($matches[1] as $url) {
593                                 Logger::info('Got page url (link with description)', ['uri-id' => $uriid, 'url' => $url]);
594                                 $result = self::insert(['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url], false, $network);
595                                 if ($result && ($network == Protocol::DFRN)) {
596                                         self::revertHTMLType($uriid, $url, $fullbody);
597                                         Logger::debug('Revert HTML type', ['uri-id' => $uriid, 'url' => $url]);
598                                 } elseif ($result) {
599                                         Logger::debug('Media has been added', ['uri-id' => $uriid, 'url' => $url]);
600                                 } else {
601                                         Logger::debug('Media has not been added', ['uri-id' => $uriid, 'url' => $url]);
602                                 }
603                         }
604                 }
605         }
606
607         /**
608          * Revert the media type of links to UNKNOWN for DFRN posts when they aren't attached
609          *
610          * @param integer $uriid
611          * @param string $url
612          * @param string $body
613          * @return void
614          */
615         private static function revertHTMLType(int $uriid, string $url, string $body)
616         {
617                 $attachment = BBCode::getAttachmentData($body);
618                 if (!empty($attachment['url']) && Network::getUrlMatch($attachment['url'], $url)) {
619                         return;
620                 }
621                 DBA::update('post-media', ['type' => self::UNKNOWN], ['uri-id' => $uriid, 'type' => self::HTML, 'url' => $url]);
622         }
623
624         /**
625          * Add media links from the attachment field
626          *
627          * @param integer $uriid
628          * @param string $body
629          * @return void
630          */
631         public static function insertFromAttachmentData(int $uriid, string $body)
632         {
633                 $data = BBCode::getAttachmentData($body);
634                 if (empty($data))  {
635                         return;
636                 }
637
638                 Logger::info('Adding attachment data', ['data' => $data]);
639                 $attachment = [
640                         'uri-id' => $uriid,
641                         'type' => self::HTML,
642                         'url' => $data['url'],
643                         'preview' => $data['preview'] ?? null,
644                         'description' => $data['description'] ?? null,
645                         'name' => $data['title'] ?? null,
646                         'author-url' => $data['author_url'] ?? null,
647                         'author-name' => $data['author_name'] ?? null,
648                         'publisher-url' => $data['provider_url'] ?? null,
649                         'publisher-name' => $data['provider_name'] ?? null,
650                 ];
651                 if (!empty($data['image'])) {
652                         $attachment['preview'] = $data['image'];
653                 }
654                 self::insert($attachment);
655         }
656
657         /**
658          * Add media links from the attach field
659          *
660          * @param integer $uriid
661          * @param string $attach
662          * @return void
663          */
664         public static function insertFromAttachment(int $uriid, string $attach)
665         {
666                 if (!preg_match_all('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\"(?: title=\"(.*?)\")?|', $attach, $matches, PREG_SET_ORDER)) {
667                         return;
668                 }
669
670                 foreach ($matches as $attachment) {
671                         $media['type'] = self::DOCUMENT;
672                         $media['uri-id'] = $uriid;
673                         $media['url'] = $attachment[1];
674                         $media['size'] = $attachment[2];
675                         $media['mimetype'] = $attachment[3];
676                         $media['description'] = $attachment[4] ?? '';
677
678                         self::insert($media);
679                 }
680         }
681
682         /**
683          * Retrieves the media attachments associated with the provided item ID.
684          *
685          * @param int $uri_id URI id
686          * @param array $types Media types
687          * @return array|bool Array on success, false on error
688          * @throws \Exception
689          */
690         public static function getByURIId(int $uri_id, array $types = [])
691         {
692                 $condition = ["`uri-id` = ? AND `type` != ?", $uri_id, self::UNKNOWN];
693
694                 if (!empty($types)) {
695                         $condition = DBA::mergeConditions($condition, ['type' => $types]);
696                 }
697
698                 return DBA::selectToArray('post-media', [], $condition, ['order' => ['id']]);
699         }
700
701         /**
702          * Checks if media attachments are associated with the provided item ID.
703          *
704          * @param int $uri_id URI id
705          * @param array $types Media types
706          * @return bool Whether media attachment exists
707          * @throws \Exception
708          */
709         public static function existsByURIId(int $uri_id, array $types = []): bool
710         {
711                 $condition = ["`uri-id` = ? AND `type` != ?", $uri_id, self::UNKNOWN];
712
713                 if (!empty($types)) {
714                         $condition = DBA::mergeConditions($condition, ['type' => $types]);
715                 }
716
717                 return DBA::exists('post-media', $condition);
718         }
719
720         /**
721          * Delete media by uri-id and media type
722          *
723          * @param int $uri_id URI id
724          * @param array $types Media types
725          * @return bool Whether media attachment exists
726          * @throws \Exception
727          */
728         public static function deleteByURIId(int $uri_id, array $types = []): bool
729         {
730                 $condition = ['uri-id' => $uri_id];
731
732                 if (!empty($types)) {
733                         $condition = DBA::mergeConditions($condition, ['type' => $types]);
734                 }
735
736                 return DBA::delete('post-media', $condition);
737         }
738
739         /**
740          * Split the attachment media in the three segments "visual", "link" and "additional"
741          *
742          * @param int    $uri_id URI id
743          * @param array  $links list of links that shouldn't be added
744          * @param bool   $has_media
745          * @return array attachments
746          */
747         public static function splitAttachments(int $uri_id, array $links = [], bool $has_media = true): array
748         {
749                 $attachments = ['visual' => [], 'link' => [], 'additional' => []];
750
751                 if (!$has_media) {
752                         return $attachments;
753                 }
754
755                 $media = self::getByURIId($uri_id);
756                 if (empty($media)) {
757                         return $attachments;
758                 }
759
760                 $heights = [];
761                 $selected = '';
762                 $previews = [];
763
764                 foreach ($media as $medium) {
765                         foreach ($links as $link) {
766                                 if (Strings::compareLink($link, $medium['url'])) {
767                                         continue 2;
768                                 }
769                         }
770
771                         // Avoid adding separate media entries for previews
772                         foreach ($previews as $preview) {
773                                 if (Strings::compareLink($preview, $medium['url'])) {
774                                         continue 2;
775                                 }
776                         }
777
778                         // Currently these two types are ignored here.
779                         // Posts are added differently and contacts are not displayed as attachments.
780                         if (in_array($medium['type'], [self::ACCOUNT, self::ACTIVITY])) {
781                                 continue;
782                         }
783
784                         if (!empty($medium['preview'])) {
785                                 $previews[] = $medium['preview'];
786                         }
787
788                         $type = explode('/', explode(';', $medium['mimetype'] ?? '')[0]);
789                         if (count($type) < 2) {
790                                 Logger::info('Unknown MimeType', ['type' => $type, 'media' => $medium]);
791                                 $filetype = 'unkn';
792                                 $subtype = 'unkn';
793                         } else {
794                                 $filetype = strtolower($type[0]);
795                                 $subtype = strtolower($type[1]);
796                         }
797
798                         $medium['filetype'] = $filetype;
799                         $medium['subtype'] = $subtype;
800
801                         if ($medium['type'] == self::HTML || (($filetype == 'text') && ($subtype == 'html'))) {
802                                 $attachments['link'][] = $medium;
803                                 continue;
804                         }
805
806                         if (in_array($medium['type'], [self::AUDIO, self::IMAGE]) ||
807                                 in_array($filetype, ['audio', 'image'])) {
808                                 $attachments['visual'][] = $medium;
809                         } elseif (($medium['type'] == self::VIDEO) || ($filetype == 'video')) {
810                                 if (!empty($medium['height'])) {
811                                         // Peertube videos are delivered in many different resolutions. We pick a moderate one.
812                                         // Since only Peertube provides a "height" parameter, this wouldn't be executed
813                                         // when someone for example on Mastodon was sharing multiple videos in a single post.
814                                         $heights[$medium['height']] = $medium['url'];
815                                         $video[$medium['url']] = $medium;
816                                 } else {
817                                         $attachments['visual'][] = $medium;
818                                 }
819                         } else {
820                                 $attachments['additional'][] = $medium;
821                         }
822                 }
823
824                 if (!empty($heights)) {
825                         ksort($heights);
826                         foreach ($heights as $height => $url) {
827                                 if (empty($selected) || $height <= 480) {
828                                         $selected = $url;
829                                 }
830                         }
831
832                         if (!empty($selected)) {
833                                 $attachments['visual'][] = $video[$selected];
834                                 unset($video[$selected]);
835                                 foreach ($video as $element) {
836                                         $attachments['additional'][] = $element;
837                                 }
838                         }
839                 }
840
841                 return $attachments;
842         }
843
844         /**
845          * Add media attachments to the body
846          *
847          * @param int    $uriid
848          * @param string $body
849          * @param array  $types
850          *
851          * @return string body
852          */
853         public static function addAttachmentsToBody(int $uriid, string $body = '', array $types = [self::IMAGE, self::AUDIO, self::VIDEO]): string
854         {
855                 if (empty($body)) {
856                         $item = Post::selectFirst(['body'], ['uri-id' => $uriid]);
857                         if (!DBA::isResult($item)) {
858                                 return '';
859                         }
860                         $body = $item['body'];
861                 }
862                 $original_body = $body;
863
864                 $body = BBCode::removeAttachment($body);
865
866                 foreach (self::getByURIId($uriid, $types) as $media) {
867                         if (Item::containsLink($body, $media['preview'] ?? $media['url'], $media['type'])) {
868                                 continue;
869                         }
870
871                         if ($media['type'] == self::IMAGE) {
872                                 if (!empty($media['preview'])) {
873                                         if (!empty($media['description'])) {
874                                                 $body .= "\n[url=" . $media['url'] . "][img=" . $media['preview'] . ']' . $media['description'] .'[/img][/url]';
875                                         } else {
876                                                 $body .= "\n[url=" . $media['url'] . "][img]" . $media['preview'] .'[/img][/url]';
877                                         }
878                                 } else {
879                                         if (!empty($media['description'])) {
880                                                 $body .= "\n[img=" . $media['url'] . ']' . $media['description'] .'[/img]';
881                                         } else {
882                                                 $body .= "\n[img]" . $media['url'] .'[/img]';
883                                         }
884                                 }
885                         } elseif ($media['type'] == self::AUDIO) {
886                                 $body .= "\n[audio]" . $media['url'] . "[/audio]\n";
887                         } elseif ($media['type'] == self::VIDEO) {
888                                 $body .= "\n[video]" . $media['url'] . "[/video]\n";
889                         }
890                 }
891
892                 if (preg_match("/.*(\[attachment.*?\].*?\[\/attachment\]).*/ism", $original_body, $match)) {
893                         $body .= "\n" . $match[1];
894                 }
895
896                 return $body;
897         }
898
899         public static function addHTMLAttachmentToBody(int $uriid, string $body): string
900         {
901                 if (preg_match("/.*(\[attachment.*?\].*?\[\/attachment\]).*/ism", $body, $match)) {
902                         return $body;
903                 }
904
905                 $links = self::getByURIId($uriid, [self::HTML]);
906                 if (empty($links)) {
907                         return $body;
908                 }
909
910                 $data = [
911                         'type' => 'link',
912                         'url'  => $links[0]['url'],
913                         'title' => $links[0]['name'],
914                         'text' => $links[0]['description'],
915                         'publisher_name' => $links[0]['publisher-name'],
916                         'publisher_url' => $links[0]['publisher-url'],
917                         'publisher_img' => $links[0]['publisher-image'],
918                         'author_name' => $links[0]['author-name'],
919                         'author_url' => $links[0]['author-url'],
920                         'author_img' => $links[0]['author-image'],
921                         'images' => [[
922                                 'src' => $links[0]['preview'],
923                                 'height' => $links[0]['preview-height'],
924                                 'width' => $links[0]['preview-width'],
925                         ]]
926                 ];
927                 $body .= "\n" . PageInfo::getFooterFromData($data);
928
929                 return $body;
930         }
931
932         public static function addHTMLLinkToBody(int $uriid, string $body): string
933         {
934                 $links = self::getByURIId($uriid, [self::HTML]);
935                 if (empty($links)) {
936                         return $body;
937                 }
938
939                 if (strpos($body, $links[0]['url'])) {
940                         return $body;
941                 }
942
943                 if (!empty($links[0]['name']) && ($links[0]['name'] != $links[0]['url'])) {
944                         return $body . "\n[url=" . $links[0]['url'] . ']' . $links[0]['name'] . "[/url]";
945                 } else {
946                         return $body . "\n[url]" . $links[0]['url'] . "[/url]";
947                 }
948         }
949
950         public static function addHTMLAttachmentToItem(array $item): array
951         {
952                 if (($item['gravity'] == Item::GRAVITY_ACTIVITY) || empty($item['uri-id'])) {
953                         return $item;
954                 }
955
956                 $item['body'] = self::addHTMLAttachmentToBody($item['uri-id'], $item['body']);
957
958                 if (!empty($item['raw-body'])) {
959                         $item['raw-body'] = self::addHTMLLinkToBody($item['uri-id'], $item['raw-body']);
960                 }
961
962                 return $item;
963         }
964
965         /**
966          * Get preview link for given media id
967          *
968          * @param integer $id   media id
969          * @param string  $size One of the Proxy::SIZE_* constants
970          * @return string preview link
971          */
972         public static function getPreviewUrlForId(int $id, string $size = ''): string
973         {
974                 $url = DI::baseUrl() . '/photo/preview/';
975                 switch ($size) {
976                         case Proxy::SIZE_MICRO:
977                                 $url .= Proxy::PIXEL_MICRO . '/';
978                                 break;
979                         case Proxy::SIZE_THUMB:
980                                 $url .= Proxy::PIXEL_THUMB . '/';
981                                 break;
982                         case Proxy::SIZE_SMALL:
983                                 $url .= Proxy::PIXEL_SMALL . '/';
984                                 break;
985                         case Proxy::SIZE_MEDIUM:
986                                 $url .= Proxy::PIXEL_MEDIUM . '/';
987                                 break;
988                         case Proxy::SIZE_LARGE:
989                                 $url .= Proxy::PIXEL_LARGE . '/';
990                                 break;
991                 }
992                 return $url . $id;
993         }
994
995         /**
996          * Get media link for given media id
997          *
998          * @param integer $id   media id
999          * @param string  $size One of the Proxy::SIZE_* constants
1000          * @return string media link
1001          */
1002         public static function getUrlForId(int $id, string $size = ''): string
1003         {
1004                 $url = DI::baseUrl() . '/photo/media/';
1005                 switch ($size) {
1006                         case Proxy::SIZE_MICRO:
1007                                 $url .= Proxy::PIXEL_MICRO . '/';
1008                                 break;
1009                         case Proxy::SIZE_THUMB:
1010                                 $url .= Proxy::PIXEL_THUMB . '/';
1011                                 break;
1012                         case Proxy::SIZE_SMALL:
1013                                 $url .= Proxy::PIXEL_SMALL . '/';
1014                                 break;
1015                         case Proxy::SIZE_MEDIUM:
1016                                 $url .= Proxy::PIXEL_MEDIUM . '/';
1017                                 break;
1018                         case Proxy::SIZE_LARGE:
1019                                 $url .= Proxy::PIXEL_LARGE . '/';
1020                                 break;
1021                 }
1022                 return $url . $id;
1023         }
1024 }