3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Model\Post;
24 use Friendica\Content\Text\BBCode;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\System;
28 use Friendica\Database\Database;
29 use Friendica\Database\DBA;
31 use Friendica\Model\Contact;
32 use Friendica\Model\Item;
33 use Friendica\Model\Photo;
34 use Friendica\Model\Post;
35 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
36 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
37 use Friendica\Util\Images;
38 use Friendica\Util\Network;
39 use Friendica\Util\ParseUrl;
40 use Friendica\Util\Proxy;
41 use Friendica\Util\Strings;
46 * This Model class handles media interactions.
47 * This tables stores medias (images, videos, audio files) related to posts.
56 const APPLICATION = 5;
66 * Insert a post-media record
72 public static function insert(array $media, bool $force = false)
74 if (empty($media['url']) || empty($media['uri-id']) || !isset($media['type'])) {
75 Logger::warning('Incomplete media data', ['media' => $media]);
79 if (DBA::exists('post-media', ['uri-id' => $media['uri-id'], 'preview' => $media['url']])) {
80 Logger::info('Media already exists as preview', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'callstack' => System::callstack()]);
84 // "document" has got the lowest priority. So when the same file is both attached as document
85 // and embedded as picture then we only store the picture or replace the document
86 $found = DBA::selectFirst('post-media', ['type'], ['uri-id' => $media['uri-id'], 'url' => $media['url']]);
87 if (!$force && !empty($found) && (($found['type'] != self::DOCUMENT) || ($media['type'] == self::DOCUMENT))) {
88 Logger::info('Media already exists', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'callstack' => System::callstack()]);
92 $media = self::unsetEmptyFields($media);
93 $media = DI::dbaDefinition()->truncateFieldsForTable('post-media', $media);
95 // We are storing as fast as possible to avoid duplicated network requests
96 // when fetching additional information for pictures and other content.
97 $result = DBA::insert('post-media', $media, Database::INSERT_UPDATE);
98 Logger::info('Stored media', ['result' => $result, 'media' => $media, 'callstack' => System::callstack()]);
101 $media = self::fetchAdditionalData($media);
102 $media = self::unsetEmptyFields($media);
103 $media = DI::dbaDefinition()->truncateFieldsForTable('post-media', $media);
105 if (array_diff_assoc($media, $stored)) {
106 $result = DBA::insert('post-media', $media, Database::INSERT_UPDATE);
107 Logger::info('Updated media', ['result' => $result, 'media' => $media]);
109 Logger::info('Nothing to update', ['media' => $media]);
114 * Remove empty media fields
116 * @param array $media
117 * @return array cleaned media array
119 private static function unsetEmptyFields(array $media): array
121 $fields = ['mimetype', 'height', 'width', 'size', 'preview', 'preview-height', 'preview-width', 'blurhash', 'description'];
122 foreach ($fields as $field) {
123 if (empty($media[$field])) {
124 unset($media[$field]);
131 * Copy attachments from one uri-id to another
133 * @param integer $from_uri_id
134 * @param integer $to_uri_id
137 public static function copy(int $from_uri_id, int $to_uri_id)
139 $attachments = self::getByURIId($from_uri_id);
140 foreach ($attachments as $attachment) {
141 $attachment['uri-id'] = $to_uri_id;
142 self::insert($attachment);
147 * Creates the "[attach]" element from the given attributes
149 * @param string $href
150 * @param integer $length
151 * @param string $type
152 * @param string $title
153 * @return string "[attach]" element
155 public static function getAttachElement(string $href, int $length, string $type, string $title = ''): string
157 $media = self::fetchAdditionalData(['type' => self::DOCUMENT, 'url' => $href,
158 'size' => $length, 'mimetype' => $type, 'description' => $title]);
160 return '[attach]href="' . $media['url'] . '" length="' . $media['size'] .
161 '" type="' . $media['mimetype'] . '" title="' . $media['description'] . '"[/attach]';
165 * Fetch additional data for the provided media array
167 * @param array $media
168 * @return array media array with additional data
170 public static function fetchAdditionalData(array $media): array
172 if (Network::isLocalLink($media['url'])) {
173 $media = self::fetchLocalData($media);
176 // Fetch the mimetype or size if missing.
177 if (empty($media['mimetype']) || empty($media['size'])) {
178 $timeout = DI::config()->get('system', 'xrd_timeout');
179 $curlResult = DI::httpClient()->head($media['url'], [HttpClientOptions::TIMEOUT => $timeout]);
181 // Workaround for systems that can't handle a HEAD request
182 if (!$curlResult->isSuccess() && ($curlResult->getReturnCode() == 405)) {
183 $curlResult = DI::httpClient()->get($media['url'], HttpClientAccept::DEFAULT, [HttpClientOptions::TIMEOUT => $timeout]);
186 if ($curlResult->isSuccess()) {
187 if (empty($media['mimetype'])) {
188 $media['mimetype'] = $curlResult->getHeader('Content-Type')[0] ?? '';
190 if (empty($media['size'])) {
191 $media['size'] = (int)($curlResult->getHeader('Content-Length')[0] ?? 0);
194 Logger::notice('Could not fetch head', ['media' => $media]);
198 $filetype = !empty($media['mimetype']) ? strtolower(current(explode('/', $media['mimetype']))) : '';
200 if (($media['type'] == self::IMAGE) || ($filetype == 'image')) {
201 $imagedata = Images::getInfoFromURLCached($media['url']);
203 $media['mimetype'] = $imagedata['mime'];
204 $media['size'] = $imagedata['size'];
205 $media['width'] = $imagedata[0];
206 $media['height'] = $imagedata[1];
207 $media['blurhash'] = $imagedata['blurhash'] ?? null;
209 Logger::notice('No image data', ['media' => $media]);
211 if (!empty($media['preview'])) {
212 $imagedata = Images::getInfoFromURLCached($media['preview']);
214 $media['preview-width'] = $imagedata[0];
215 $media['preview-height'] = $imagedata[1];
220 if ($media['type'] != self::DOCUMENT) {
221 $media = self::addType($media);
224 if (in_array($media['type'], [self::TEXT, self::APPLICATION, self::HTML, self::XML, self::PLAIN])) {
225 $media = self::addActivity($media);
228 if (in_array($media['type'], [self::TEXT, self::APPLICATION, self::HTML, self::XML, self::PLAIN])) {
229 $media = self::addAccount($media);
232 if ($media['type'] == self::HTML) {
233 $media = self::addPage($media);
240 * Adds the activity type if the media entry is linked to an activity
242 * @param array $media
245 private static function addActivity(array $media): array
247 $id = Item::fetchByLink($media['url']);
252 $item = Post::selectFirst([], ['id' => $id, 'network' => Protocol::FEDERATED]);
253 if (empty($item['id'])) {
254 Logger::debug('Not a federated activity', ['id' => $id, 'uri-id' => $media['uri-id'], 'url' => $media['url']]);
258 if (!empty($item['plink']) && Strings::compareLink($item['plink'], $media['url']) &&
259 parse_url($item['plink'], PHP_URL_HOST) != parse_url($item['uri'], PHP_URL_HOST)) {
260 Logger::debug('Not a link to an activity', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'plink' => $item['plink'], 'uri' => $item['uri']]);
264 if (in_array($item['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
265 $media['mimetype'] = 'application/activity+json';
266 } elseif ($item['network'] == Protocol::DIASPORA) {
267 $media['mimetype'] = 'application/xml';
270 $contact = Contact::getById($item['author-id'], ['avatar', 'gsid']);
271 if (!empty($contact['gsid'])) {
272 $gserver = DBA::selectFirst('gserver', ['url', 'site_name'], ['id' => $contact['gsid']]);
275 $media['type'] = self::ACTIVITY;
276 $media['media-uri-id'] = $item['uri-id'];
277 $media['height'] = null;
278 $media['width'] = null;
279 $media['preview'] = null;
280 $media['preview-height'] = null;
281 $media['preview-width'] = null;
282 $media['blurhash'] = null;
283 $media['description'] = $item['body'];
284 $media['name'] = $item['title'];
285 $media['author-url'] = $item['author-link'];
286 $media['author-name'] = $item['author-name'];
287 $media['author-image'] = $contact['avatar'] ?? $item['author-avatar'];
288 $media['publisher-url'] = $gserver['url'] ?? null;
289 $media['publisher-name'] = $gserver['site_name'] ?? null;
290 $media['publisher-image'] = null;
292 Logger::debug('Activity detected', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'plink' => $item['plink'], 'uri' => $item['uri']]);
297 * Adds the account type if the media entry is linked to an account
299 * @param array $media
302 private static function addAccount(array $media): array
304 $contact = Contact::getByURL($media['url'], false);
305 if (empty($contact) || ($contact['network'] == Protocol::PHANTOM)) {
309 if (in_array($contact['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN])) {
310 $media['mimetype'] = 'application/activity+json';
313 if (!empty($contact['gsid'])) {
314 $gserver = DBA::selectFirst('gserver', ['url', 'site_name'], ['id' => $contact['gsid']]);
317 $media['type'] = self::ACCOUNT;
318 $media['media-uri-id'] = $contact['uri-id'];
319 $media['height'] = null;
320 $media['width'] = null;
321 $media['preview'] = null;
322 $media['preview-height'] = null;
323 $media['preview-width'] = null;
324 $media['blurhash'] = null;
325 $media['description'] = $contact['about'];
326 $media['name'] = $contact['name'];
327 $media['author-url'] = $contact['url'];
328 $media['author-name'] = $contact['name'];
329 $media['author-image'] = $contact['avatar'];
330 $media['publisher-url'] = $gserver['url'] ?? null;
331 $media['publisher-name'] = $gserver['site_name'] ?? null;
332 $media['publisher-image'] = null;
334 Logger::debug('Account detected', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'uri' => $contact['url']]);
339 * Add page infos for HTML entries
341 * @param array $media
344 private static function addPage(array $media): array
346 $data = ParseUrl::getSiteinfoCached($media['url'], false);
347 $media['preview'] = $data['images'][0]['src'] ?? null;
348 $media['preview-height'] = $data['images'][0]['height'] ?? null;
349 $media['preview-width'] = $data['images'][0]['width'] ?? null;
350 $media['blurhash'] = $data['images'][0]['blurhash'] ?? null;
351 $media['description'] = $data['text'] ?? null;
352 $media['name'] = $data['title'] ?? null;
353 $media['author-url'] = $data['author_url'] ?? null;
354 $media['author-name'] = $data['author_name'] ?? null;
355 $media['author-image'] = $data['author_img'] ?? null;
356 $media['publisher-url'] = $data['publisher_url'] ?? null;
357 $media['publisher-name'] = $data['publisher_name'] ?? null;
358 $media['publisher-image'] = $data['publisher_img'] ?? null;
364 * Fetch media data from local resources
365 * @param array $media
366 * @return array media with added data
368 private static function fetchLocalData(array $media): array
370 if (!preg_match('|.*?/photo/(.*[a-fA-F0-9])\-(.*[0-9])\..*[\w]|', $media['url'] ?? '', $matches)) {
373 $photo = Photo::selectFirst([], ['resource-id' => $matches[1], 'scale' => $matches[2]]);
374 if (!empty($photo)) {
375 $media['mimetype'] = $photo['type'];
376 $media['size'] = $photo['datasize'];
377 $media['width'] = $photo['width'];
378 $media['height'] = $photo['height'];
379 $media['blurhash'] = $photo['blurhash'];
382 if (!preg_match('|.*?/photo/(.*[a-fA-F0-9])\-(.*[0-9])\..*[\w]|', $media['preview'] ?? '', $matches)) {
385 $photo = Photo::selectFirst([], ['resource-id' => $matches[1], 'scale' => $matches[2]]);
386 if (!empty($photo)) {
387 $media['preview-width'] = $photo['width'];
388 $media['preview-height'] = $photo['height'];
395 * Add the detected type to the media array
398 * @return array data array with the detected type
400 public static function addType(array $data): array
402 if (empty($data['mimetype'])) {
403 Logger::info('No MimeType provided', ['media' => $data]);
407 $type = explode('/', current(explode(';', $data['mimetype'])));
408 if (count($type) < 2) {
409 Logger::info('Unknown MimeType', ['type' => $type, 'media' => $data]);
410 $data['type'] = self::UNKNOWN;
414 $filetype = strtolower($type[0]);
415 $subtype = strtolower($type[1]);
417 if ($filetype == 'image') {
418 $data['type'] = self::IMAGE;
419 } elseif ($filetype == 'video') {
420 $data['type'] = self::VIDEO;
421 } elseif ($filetype == 'audio') {
422 $data['type'] = self::AUDIO;
423 } elseif (($filetype == 'text') && ($subtype == 'html')) {
424 $data['type'] = self::HTML;
425 } elseif (($filetype == 'text') && ($subtype == 'xml')) {
426 $data['type'] = self::XML;
427 } elseif (($filetype == 'text') && ($subtype == 'plain')) {
428 $data['type'] = self::PLAIN;
429 } elseif ($filetype == 'text') {
430 $data['type'] = self::TEXT;
431 } elseif (($filetype == 'application') && ($subtype == 'x-bittorrent')) {
432 $data['type'] = self::TORRENT;
433 } elseif ($filetype == 'application') {
434 $data['type'] = self::APPLICATION;
436 $data['type'] = self::UNKNOWN;
437 Logger::info('Unknown type', ['filetype' => $filetype, 'subtype' => $subtype, 'media' => $data]);
441 Logger::debug('Detected type', ['filetype' => $filetype, 'subtype' => $subtype, 'media' => $data]);
446 * Tests for path patterns that are usef for picture links in Friendica
448 * @param string $page Link to the image page
449 * @param string $preview Preview picture
452 private static function isPictureLink(string $page, string $preview): bool
454 return preg_match('#/photos/.*/image/#ism', $page) && preg_match('#/photo/.*-1\.#ism', $preview);
458 * Add media links and remove them from the body
460 * @param integer $uriid
461 * @param string $body
462 * @return string Body without media links
464 public static function insertFromBody(int $uriid, string $body, bool $endmatch = false): string
466 $endmatchpattern = $endmatch ? '\z' : '';
467 // Simplify image codes
468 $unshared_body = $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]$endmatchpattern/ism", '[img]$3[/img]', $body);
471 if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]\s*\[/url\]$endmatchpattern#ism", $body, $pictures, PREG_SET_ORDER)) {
472 foreach ($pictures as $picture) {
473 if (!self::isPictureLink($picture[1], $picture[2])) {
476 $body = str_replace($picture[0], '', $body);
477 $image = str_replace('-1.', '-0.', $picture[2]);
478 $attachments[$image] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $image,
479 'preview' => $picture[2], 'description' => $picture[3]];
483 if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]$endmatchpattern/Usi", $body, $pictures, PREG_SET_ORDER)) {
484 foreach ($pictures as $picture) {
485 $body = str_replace($picture[0], '', $body);
486 $attachments[$picture[1]] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1], 'description' => $picture[2]];
490 if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]$endmatchpattern#ism", $body, $pictures, PREG_SET_ORDER)) {
491 foreach ($pictures as $picture) {
492 if (!self::isPictureLink($picture[1], $picture[2])) {
495 $body = str_replace($picture[0], '', $body);
496 $image = str_replace('-1.', '-0.', $picture[2]);
497 $attachments[$image] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $image,
498 'preview' => $picture[2], 'description' => null];
502 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]$endmatchpattern/ism", $body, $pictures, PREG_SET_ORDER)) {
503 foreach ($pictures as $picture) {
504 $body = str_replace($picture[0], '', $body);
505 $attachments[$picture[1]] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1]];
509 if (preg_match_all("/\[audio\]([^\[\]]*)\[\/audio\]$endmatchpattern/ism", $body, $audios, PREG_SET_ORDER)) {
510 foreach ($audios as $audio) {
511 $body = str_replace($audio[0], '', $body);
512 $attachments[$audio[1]] = ['uri-id' => $uriid, 'type' => self::AUDIO, 'url' => $audio[1]];
516 if (preg_match_all("/\[video\]([^\[\]]*)\[\/video\]$endmatchpattern/ism", $body, $videos, PREG_SET_ORDER)) {
517 foreach ($videos as $video) {
518 $body = str_replace($video[0], '', $body);
519 $attachments[$video[1]] = ['uri-id' => $uriid, 'type' => self::VIDEO, 'url' => $video[1]];
524 foreach ($attachments as $attachment) {
525 if (Post\Link::exists($uriid, $attachment['preview'] ?? $attachment['url'])) {
529 // Only store attachments that are part of the unshared body
530 if (Item::containsLink($unshared_body, $attachment['preview'] ?? $attachment['url'], $attachment['type'])) {
531 self::insert($attachment);
540 * Remove media that is at the end of the body
542 * @param string $body
545 public static function removeFromEndOfBody(string $body): string
549 $body = self::insertFromBody(0, $body, true);
550 } while ($prebody != $body);
555 * Add media links from a relevant url in the body
557 * @param integer $uriid
558 * @param string $body
561 public static function insertFromRelevantUrl(int $uriid, string $body, string $fullbody, string $network)
563 // Remove all hashtags and mentions
564 $body = preg_replace("/([#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", '', $body);
566 // Search for pure links
567 if (preg_match_all("/\[url\](https?:.*?)\[\/url\]/ism", $body, $matches)) {
568 foreach ($matches[1] as $url) {
569 Logger::info('Got page url (link without description)', ['uri-id' => $uriid, 'url' => $url]);
570 self::insert(['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url], false, $network);
571 if ($network == Protocol::DFRN) {
572 self::revertHTMLType($uriid, $url, $fullbody);
577 // Search for links with descriptions
578 if (preg_match_all("/\[url\=(https?:.*?)\].*?\[\/url\]/ism", $body, $matches)) {
579 foreach ($matches[1] as $url) {
580 Logger::info('Got page url (link with description)', ['uri-id' => $uriid, 'url' => $url]);
581 self::insert(['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url], false, $network);
582 if ($network == Protocol::DFRN) {
583 self::revertHTMLType($uriid, $url, $fullbody);
590 * Revert the media type of links to UNKNOWN for DFRN posts when they aren't attached
592 * @param integer $uriid
594 * @param string $body
597 private static function revertHTMLType(int $uriid, string $url, string $body)
599 $attachment = BBCode::getAttachmentData($body);
600 if (!empty($attachment['url']) && Network::getUrlMatch($attachment['url'], $url)) {
603 DBA::update('post-media', ['type' => self::UNKNOWN], ['uri-id' => $uriid, 'type' => self::HTML, 'url' => $url]);
607 * Add media links from the attachment field
609 * @param integer $uriid
610 * @param string $body
613 public static function insertFromAttachmentData(int $uriid, string $body)
615 $data = BBCode::getAttachmentData($body);
620 Logger::info('Adding attachment data', ['data' => $data]);
623 'type' => self::HTML,
624 'url' => $data['url'],
625 'preview' => $data['preview'] ?? null,
626 'description' => $data['description'] ?? null,
627 'name' => $data['title'] ?? null,
628 'author-url' => $data['author_url'] ?? null,
629 'author-name' => $data['author_name'] ?? null,
630 'publisher-url' => $data['provider_url'] ?? null,
631 'publisher-name' => $data['provider_name'] ?? null,
633 if (!empty($data['image'])) {
634 $attachment['preview'] = $data['image'];
636 self::insert($attachment);
640 * Add media links from the attach field
642 * @param integer $uriid
643 * @param string $attach
646 public static function insertFromAttachment(int $uriid, string $attach)
648 if (!preg_match_all('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\"(?: title=\"(.*?)\")?|', $attach, $matches, PREG_SET_ORDER)) {
652 foreach ($matches as $attachment) {
653 $media['type'] = self::DOCUMENT;
654 $media['uri-id'] = $uriid;
655 $media['url'] = $attachment[1];
656 $media['size'] = $attachment[2];
657 $media['mimetype'] = $attachment[3];
658 $media['description'] = $attachment[4] ?? '';
660 self::insert($media);
665 * Retrieves the media attachments associated with the provided item ID.
667 * @param int $uri_id URI id
668 * @param array $types Media types
669 * @return array|bool Array on success, false on error
672 public static function getByURIId(int $uri_id, array $types = [])
674 $condition = ["`uri-id` = ? AND `type` != ?", $uri_id, self::UNKNOWN];
676 if (!empty($types)) {
677 $condition = DBA::mergeConditions($condition, ['type' => $types]);
680 return DBA::selectToArray('post-media', [], $condition, ['order' => ['id']]);
684 * Checks if media attachments are associated with the provided item ID.
686 * @param int $uri_id URI id
687 * @param array $types Media types
688 * @return bool Whether media attachment exists
691 public static function existsByURIId(int $uri_id, array $types = []): bool
693 $condition = ["`uri-id` = ? AND `type` != ?", $uri_id, self::UNKNOWN];
695 if (!empty($types)) {
696 $condition = DBA::mergeConditions($condition, ['type' => $types]);
699 return DBA::exists('post-media', $condition);
703 * Split the attachment media in the three segments "visual", "link" and "additional"
705 * @param int $uri_id URI id
706 * @param array $links list of links that shouldn't be added
707 * @param bool $has_media
708 * @return array attachments
710 public static function splitAttachments(int $uri_id, array $links = [], bool $has_media = true): array
712 $attachments = ['visual' => [], 'link' => [], 'additional' => []];
718 $media = self::getByURIId($uri_id);
727 foreach ($media as $medium) {
728 foreach ($links as $link) {
729 if (Strings::compareLink($link, $medium['url'])) {
734 // Avoid adding separate media entries for previews
735 foreach ($previews as $preview) {
736 if (Strings::compareLink($preview, $medium['url'])) {
741 // Currently these two types are ignored here.
742 // Posts are added differently and contacts are not displayed as attachments.
743 if (in_array($medium['type'], [self::ACCOUNT, self::ACTIVITY])) {
747 if (!empty($medium['preview'])) {
748 $previews[] = $medium['preview'];
751 $type = explode('/', explode(';', $medium['mimetype'] ?? '')[0]);
752 if (count($type) < 2) {
753 Logger::info('Unknown MimeType', ['type' => $type, 'media' => $medium]);
757 $filetype = strtolower($type[0]);
758 $subtype = strtolower($type[1]);
761 $medium['filetype'] = $filetype;
762 $medium['subtype'] = $subtype;
764 if ($medium['type'] == self::HTML || (($filetype == 'text') && ($subtype == 'html'))) {
765 $attachments['link'][] = $medium;
769 if (in_array($medium['type'], [self::AUDIO, self::IMAGE]) ||
770 in_array($filetype, ['audio', 'image'])) {
771 $attachments['visual'][] = $medium;
772 } elseif (($medium['type'] == self::VIDEO) || ($filetype == 'video')) {
773 if (!empty($medium['height'])) {
774 // Peertube videos are delivered in many different resolutions. We pick a moderate one.
775 // Since only Peertube provides a "height" parameter, this wouldn't be executed
776 // when someone for example on Mastodon was sharing multiple videos in a single post.
777 $heights[$medium['height']] = $medium['url'];
778 $video[$medium['url']] = $medium;
780 $attachments['visual'][] = $medium;
783 $attachments['additional'][] = $medium;
787 if (!empty($heights)) {
789 foreach ($heights as $height => $url) {
790 if (empty($selected) || $height <= 480) {
795 if (!empty($selected)) {
796 $attachments['visual'][] = $video[$selected];
797 unset($video[$selected]);
798 foreach ($video as $element) {
799 $attachments['additional'][] = $element;
808 * Add media attachments to the body
811 * @param string $body
812 * @param array $types
814 * @return string body
816 public static function addAttachmentsToBody(int $uriid, string $body = '', array $types = [self::IMAGE, self::AUDIO, self::VIDEO]): string
819 $item = Post::selectFirst(['body'], ['uri-id' => $uriid]);
820 if (!DBA::isResult($item)) {
823 $body = $item['body'];
825 $original_body = $body;
827 $body = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", '', $body);
829 foreach (self::getByURIId($uriid, $types) as $media) {
830 if (Item::containsLink($body, $media['preview'] ?? $media['url'], $media['type'])) {
834 if ($media['type'] == self::IMAGE) {
835 if (!empty($media['preview'])) {
836 if (!empty($media['description'])) {
837 $body .= "\n[url=" . $media['url'] . "][img=" . $media['preview'] . ']' . $media['description'] .'[/img][/url]';
839 $body .= "\n[url=" . $media['url'] . "][img]" . $media['preview'] .'[/img][/url]';
842 if (!empty($media['description'])) {
843 $body .= "\n[img=" . $media['url'] . ']' . $media['description'] .'[/img]';
845 $body .= "\n[img]" . $media['url'] .'[/img]';
848 } elseif ($media['type'] == self::AUDIO) {
849 $body .= "\n[audio]" . $media['url'] . "[/audio]\n";
850 } elseif ($media['type'] == self::VIDEO) {
851 $body .= "\n[video]" . $media['url'] . "[/video]\n";
855 if (preg_match("/.*(\[attachment.*?\].*?\[\/attachment\]).*/ism", $original_body, $match)) {
856 $body .= "\n" . $match[1];
863 * Get preview link for given media id
865 * @param integer $id media id
866 * @param string $size One of the Proxy::SIZE_* constants
867 * @return string preview link
869 public static function getPreviewUrlForId(int $id, string $size = ''): string
871 $url = DI::baseUrl() . '/photo/preview/';
873 case Proxy::SIZE_MICRO:
874 $url .= Proxy::PIXEL_MICRO . '/';
876 case Proxy::SIZE_THUMB:
877 $url .= Proxy::PIXEL_THUMB . '/';
879 case Proxy::SIZE_SMALL:
880 $url .= Proxy::PIXEL_SMALL . '/';
882 case Proxy::SIZE_MEDIUM:
883 $url .= Proxy::PIXEL_MEDIUM . '/';
885 case Proxy::SIZE_LARGE:
886 $url .= Proxy::PIXEL_LARGE . '/';
893 * Get media link for given media id
895 * @param integer $id media id
896 * @param string $size One of the Proxy::SIZE_* constants
897 * @return string media link
899 public static function getUrlForId(int $id, string $size = ''): string
901 $url = DI::baseUrl() . '/photo/media/';
903 case Proxy::SIZE_MICRO:
904 $url .= Proxy::PIXEL_MICRO . '/';
906 case Proxy::SIZE_THUMB:
907 $url .= Proxy::PIXEL_THUMB . '/';
909 case Proxy::SIZE_SMALL:
910 $url .= Proxy::PIXEL_SMALL . '/';
912 case Proxy::SIZE_MEDIUM:
913 $url .= Proxy::PIXEL_MEDIUM . '/';
915 case Proxy::SIZE_LARGE:
916 $url .= Proxy::PIXEL_LARGE . '/';