3 * @copyright Copyright (C) 2010-2021, 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\System;
27 use Friendica\Database\Database;
28 use Friendica\Database\DBA;
30 use Friendica\Model\Item;
31 use Friendica\Model\Post;
32 use Friendica\Util\Images;
33 use Friendica\Util\ParseUrl;
34 use Friendica\Util\Proxy;
35 use Friendica\Util\Strings;
40 * This Model class handles media interactions.
41 * This tables stores medias (images, videos, audio files) related to posts.
50 const APPLICATION = 5;
58 * Insert a post-media record
63 public static function insert(array $media, bool $force = false)
65 if (empty($media['url']) || empty($media['uri-id']) || !isset($media['type'])) {
66 Logger::warning('Incomplete media data', ['media' => $media]);
70 if (DBA::exists('post-media', ['uri-id' => $media['uri-id'], 'preview' => $media['url']])) {
71 Logger::info('Media already exists as preview', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'callstack' => System::callstack()]);
75 // "document" has got the lowest priority. So when the same file is both attached as document
76 // and embedded as picture then we only store the picture or replace the document
77 $found = DBA::selectFirst('post-media', ['type'], ['uri-id' => $media['uri-id'], 'url' => $media['url']]);
78 if (!$force && !empty($found) && (($found['type'] != self::DOCUMENT) || ($media['type'] == self::DOCUMENT))) {
79 Logger::info('Media already exists', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'callstack' => System::callstack()]);
83 $media = self::unsetEmptyFields($media);
85 // We are storing as fast as possible to avoid duplicated network requests
86 // when fetching additional information for pictures and other content.
87 $result = DBA::insert('post-media', $media, Database::INSERT_UPDATE);
88 Logger::info('Stored media', ['result' => $result, 'media' => $media, 'callstack' => System::callstack()]);
91 $media = self::fetchAdditionalData($media);
92 $media = self::unsetEmptyFields($media);
94 if (array_diff_assoc($media, $stored)) {
95 $result = DBA::insert('post-media', $media, Database::INSERT_UPDATE);
96 Logger::info('Updated media', ['result' => $result, 'media' => $media]);
98 Logger::info('Nothing to update', ['media' => $media]);
103 * Remove empty media fields
105 * @param array $media
106 * @return array cleaned media array
108 private static function unsetEmptyFields(array $media)
110 $fields = ['mimetype', 'height', 'width', 'size', 'preview', 'preview-height', 'preview-width', 'description'];
111 foreach ($fields as $field) {
112 if (empty($media[$field])) {
113 unset($media[$field]);
120 * Copy attachments from one uri-id to another
122 * @param integer $from_uri_id
123 * @param integer $to_uri_id
126 public static function copy(int $from_uri_id, int $to_uri_id)
128 $attachments = self::getByURIId($from_uri_id);
129 foreach ($attachments as $attachment) {
130 $attachment['uri-id'] = $to_uri_id;
131 self::insert($attachment);
136 * Creates the "[attach]" element from the given attributes
138 * @param string $href
139 * @param integer $length
140 * @param string $type
141 * @param string $title
142 * @return string "[attach]" element
144 public static function getAttachElement(string $href, int $length, string $type, string $title = '')
146 $media = self::fetchAdditionalData(['type' => self::DOCUMENT, 'url' => $href,
147 'size' => $length, 'mimetype' => $type, 'description' => $title]);
149 return '[attach]href="' . $media['url'] . '" length="' . $media['size'] .
150 '" type="' . $media['mimetype'] . '" title="' . $media['description'] . '"[/attach]';
154 * Fetch additional data for the provided media array
156 * @param array $media
157 * @return array media array with additional data
159 public static function fetchAdditionalData(array $media)
161 // Fetch the mimetype or size if missing.
162 if (empty($media['mimetype']) || empty($media['size'])) {
163 $timeout = DI::config()->get('system', 'xrd_timeout');
164 $curlResult = DI::httpRequest()->head($media['url'], ['timeout' => $timeout]);
165 if ($curlResult->isSuccess()) {
166 if (empty($media['mimetype'])) {
167 $media['mimetype'] = $curlResult->getHeader('Content-Type');
169 if (empty($media['size'])) {
170 $media['size'] = (int)$curlResult->getHeader('Content-Length');
173 Logger::notice('Could not fetch head', ['media' => $media]);
177 $filetype = !empty($media['mimetype']) ? strtolower(current(explode('/', $media['mimetype']))) : '';
179 if (($media['type'] == self::IMAGE) || ($filetype == 'image')) {
180 $imagedata = Images::getInfoFromURLCached($media['url']);
181 if (!empty($imagedata)) {
182 $media['mimetype'] = $imagedata['mime'];
183 $media['size'] = $imagedata['size'];
184 $media['width'] = $imagedata[0];
185 $media['height'] = $imagedata[1];
187 Logger::notice('No image data', ['media' => $media]);
189 if (!empty($media['preview'])) {
190 $imagedata = Images::getInfoFromURLCached($media['preview']);
191 if (!empty($imagedata)) {
192 $media['preview-width'] = $imagedata[0];
193 $media['preview-height'] = $imagedata[1];
198 if ($media['type'] != self::DOCUMENT) {
199 $media = self::addType($media);
202 if ($media['type'] == self::HTML) {
203 $data = ParseUrl::getSiteinfoCached($media['url'], false);
204 $media['preview'] = $data['images'][0]['src'] ?? null;
205 $media['preview-height'] = $data['images'][0]['height'] ?? null;
206 $media['preview-width'] = $data['images'][0]['width'] ?? null;
207 $media['description'] = $data['text'] ?? null;
208 $media['name'] = $data['title'] ?? null;
209 $media['author-url'] = $data['author_url'] ?? null;
210 $media['author-name'] = $data['author_name'] ?? null;
211 $media['author-image'] = $data['author_img'] ?? null;
212 $media['publisher-url'] = $data['publisher_url'] ?? null;
213 $media['publisher-name'] = $data['publisher_name'] ?? null;
214 $media['publisher-image'] = $data['publisher_img'] ?? null;
220 * Add the detected type to the media array
223 * @return array data array with the detected type
225 public static function addType(array $data)
227 if (empty($data['mimetype'])) {
228 Logger::info('No MimeType provided', ['media' => $data]);
232 $type = explode('/', current(explode(';', $data['mimetype'])));
233 if (count($type) < 2) {
234 Logger::info('Unknown MimeType', ['type' => $type, 'media' => $data]);
235 $data['type'] = self::UNKNOWN;
239 $filetype = strtolower($type[0]);
240 $subtype = strtolower($type[1]);
242 if ($filetype == 'image') {
243 $data['type'] = self::IMAGE;
244 } elseif ($filetype == 'video') {
245 $data['type'] = self::VIDEO;
246 } elseif ($filetype == 'audio') {
247 $data['type'] = self::AUDIO;
248 } elseif (($filetype == 'text') && ($subtype == 'html')) {
249 $data['type'] = self::HTML;
250 } elseif (($filetype == 'text') && ($subtype == 'xml')) {
251 $data['type'] = self::XML;
252 } elseif (($filetype == 'text') && ($subtype == 'plain')) {
253 $data['type'] = self::PLAIN;
254 } elseif ($filetype == 'text') {
255 $data['type'] = self::TEXT;
256 } elseif (($filetype == 'application') && ($subtype == 'x-bittorrent')) {
257 $data['type'] = self::TORRENT;
258 } elseif ($filetype == 'application') {
259 $data['type'] = self::APPLICATION;
261 $data['type'] = self::UNKNOWN;
262 Logger::info('Unknown type', ['filetype' => $filetype, 'subtype' => $subtype, 'media' => $data]);
266 Logger::debug('Detected type', ['filetype' => $filetype, 'subtype' => $subtype, 'media' => $data]);
271 * Tests for path patterns that are usef for picture links in Friendica
273 * @param string $page Link to the image page
274 * @param string $preview Preview picture
277 private static function isPictureLink(string $page, string $preview)
279 return preg_match('#/photos/.*/image/#ism', $page) && preg_match('#/photo/.*-1\.#ism', $preview);
283 * Add media links and remove them from the body
285 * @param integer $uriid
286 * @param string $body
287 * @return string Body without media links
289 public static function insertFromBody(int $uriid, string $body)
291 // Simplify image codes
292 $unshared_body = $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
294 // Only remove the shared data from "real" reshares
295 $shared = BBCode::fetchShareAttributes($body);
296 if (!empty($shared['guid'])) {
297 $unshared_body = preg_replace("/\s*\[share .*?\].*?\[\/share\]\s*/ism", '', $body);
301 if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) {
302 foreach ($pictures as $picture) {
303 if (!self::isPictureLink($picture[1], $picture[2])) {
306 $body = str_replace($picture[0], '', $body);
307 $image = str_replace('-1.', '-0.', $picture[2]);
308 $attachments[$image] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $image,
309 'preview' => $picture[2], 'description' => $picture[3]];
313 if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
314 foreach ($pictures as $picture) {
315 $body = str_replace($picture[0], '', $body);
316 $attachments[$picture[1]] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1], 'description' => $picture[2]];
320 if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) {
321 foreach ($pictures as $picture) {
322 if (!self::isPictureLink($picture[1], $picture[2])) {
325 $body = str_replace($picture[0], '', $body);
326 $image = str_replace('-1.', '-0.', $picture[2]);
327 $attachments[$image] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $image,
328 'preview' => $picture[2], 'description' => null];
332 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/ism", $body, $pictures, PREG_SET_ORDER)) {
333 foreach ($pictures as $picture) {
334 $body = str_replace($picture[0], '', $body);
335 $attachments[$picture[1]] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1]];
339 if (preg_match_all("/\[audio\]([^\[\]]*)\[\/audio\]/ism", $body, $audios, PREG_SET_ORDER)) {
340 foreach ($audios as $audio) {
341 $body = str_replace($audio[0], '', $body);
342 $attachments[$audio[1]] = ['uri-id' => $uriid, 'type' => self::AUDIO, 'url' => $audio[1]];
346 if (preg_match_all("/\[video\]([^\[\]]*)\[\/video\]/ism", $body, $videos, PREG_SET_ORDER)) {
347 foreach ($videos as $video) {
348 $body = str_replace($video[0], '', $body);
349 $attachments[$video[1]] = ['uri-id' => $uriid, 'type' => self::VIDEO, 'url' => $video[1]];
353 foreach ($attachments as $attachment) {
354 // Only store attachments that are part of the unshared body
355 if (Item::containsLink($unshared_body, $attachment['url'], $attachment['type'])) {
356 self::insert($attachment);
364 * Add media links from a relevant url in the body
366 * @param integer $uriid
367 * @param string $body
369 public static function insertFromRelevantUrl(int $uriid, string $body)
371 // Only remove the shared data from "real" reshares
372 $shared = BBCode::fetchShareAttributes($body);
373 if (!empty($shared['guid'])) {
374 // Don't look at the shared content
375 $body = preg_replace("/\s*\[share .*?\].*?\[\/share\]\s*/ism", '', $body);
378 // Remove all hashtags and mentions
379 $body = preg_replace("/([#@!])\[url\=(.*?)\](.*?)\[\/url\]/ism", '', $body);
381 // Search for pure links
382 if (preg_match_all("/\[url\](https?:.*?)\[\/url\]/ism", $body, $matches)) {
383 foreach ($matches[1] as $url) {
384 Logger::info('Got page url (link without description)', ['uri-id' => $uriid, 'url' => $url]);
385 self::insert(['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url]);
389 // Search for links with descriptions
390 if (preg_match_all("/\[url\=(https?:.*?)\].*?\[\/url\]/ism", $body, $matches)) {
391 foreach ($matches[1] as $url) {
392 Logger::info('Got page url (link with description)', ['uri-id' => $uriid, 'url' => $url]);
393 self::insert(['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url]);
399 * Add media links from the attachment field
401 * @param integer $uriid
402 * @param string $body
404 public static function insertFromAttachmentData(int $uriid, string $body)
406 // Don't look at the shared content
407 $body = preg_replace("/\s*\[share .*?\].*?\[\/share\]\s*/ism", '', $body);
409 $data = BBCode::getAttachmentData($body);
414 Logger::info('Adding attachment data', ['data' => $data]);
417 'type' => self::HTML,
418 'url' => $data['url'],
419 'preview' => $data['preview'] ?? null,
420 'description' => $data['description'] ?? null,
421 'name' => $data['title'] ?? null,
422 'author-url' => $data['author_url'] ?? null,
423 'author-name' => $data['author_name'] ?? null,
424 'publisher-url' => $data['provider_url'] ?? null,
425 'publisher-name' => $data['provider_name'] ?? null,
427 if (!empty($data['image'])) {
428 $attachment['preview'] = $data['image'];
430 self::insert($attachment);
434 * Add media links from the attach field
436 * @param integer $uriid
437 * @param string $attach
440 public static function insertFromAttachment(int $uriid, string $attach)
442 if (!preg_match_all('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\"(?: title=\"(.*?)\")?|', $attach, $matches, PREG_SET_ORDER)) {
446 foreach ($matches as $attachment) {
447 $media['type'] = self::DOCUMENT;
448 $media['uri-id'] = $uriid;
449 $media['url'] = $attachment[1];
450 $media['size'] = $attachment[2];
451 $media['mimetype'] = $attachment[3];
452 $media['description'] = $attachment[4] ?? '';
454 self::insert($media);
459 * Retrieves the media attachments associated with the provided item ID.
462 * @param array $types
466 public static function getByURIId(int $uri_id, array $types = [])
468 $condition = ['uri-id' => $uri_id];
470 if (!empty($types)) {
471 $condition = DBA::mergeConditions($condition, ['type' => $types]);
474 return DBA::selectToArray('post-media', [], $condition);
478 * Checks if media attachments are associated with the provided item ID.
481 * @param array $types
485 public static function existsByURIId(int $uri_id, array $types = [])
487 $condition = ['uri-id' => $uri_id];
489 if (!empty($types)) {
490 $condition = DBA::mergeConditions($condition, ['type' => $types]);
493 return DBA::exists('post-media', $condition);
497 * Split the attachment media in the three segments "visual", "link" and "additional"
500 * @param string $guid
501 * @param array $links ist of links that shouldn't be added
502 * @return array attachments
504 public static function splitAttachments(int $uri_id, string $guid = '', array $links = [])
506 $attachments = ['visual' => [], 'link' => [], 'additional' => []];
508 $media = self::getByURIId($uri_id);
517 foreach ($media as $medium) {
518 foreach ($links as $link) {
519 if (Strings::compareLink($link, $medium['url'])) {
524 // Avoid adding separate media entries for previews
525 foreach ($previews as $preview) {
526 if (Strings::compareLink($preview, $medium['url'])) {
531 if (!empty($medium['preview'])) {
532 $previews[] = $medium['preview'];
535 $type = explode('/', current(explode(';', $medium['mimetype'])));
536 if (count($type) < 2) {
537 Logger::info('Unknown MimeType', ['type' => $type, 'media' => $medium]);
541 $filetype = strtolower($type[0]);
542 $subtype = strtolower($type[1]);
545 $medium['filetype'] = $filetype;
546 $medium['subtype'] = $subtype;
548 if ($medium['type'] == self::HTML || (($filetype == 'text') && ($subtype == 'html'))) {
549 $attachments['link'][] = $medium;
553 if (in_array($medium['type'], [self::AUDIO, self::IMAGE]) ||
554 in_array($filetype, ['audio', 'image'])) {
555 $attachments['visual'][] = $medium;
556 } elseif (($medium['type'] == self::VIDEO) || ($filetype == 'video')) {
557 if (strpos($medium['url'], $guid) !== false) {
558 // Peertube videos are delivered in many different resolutions. We pick a moderate one.
559 // By checking against the GUID we also ensure to only work this way on Peertube posts.
560 // This wouldn't be executed when someone for example on Mastodon was sharing multiple videos in a single post.
561 if (empty($height) || ($height > $medium['height']) && ($medium['height'] >= 480)) {
562 $height = $medium['height'];
563 $selected = $medium['url'];
565 $video[$medium['url']] = $medium;
567 $attachments['visual'][] = $medium;
570 $attachments['additional'][] = $medium;
573 if (!empty($selected)) {
574 $attachments['visual'][] = $video[$selected];
575 unset($video[$selected]);
576 foreach ($video as $element) {
577 $attachments['additional'][] = $element;
584 * Add media attachments to the body
587 * @param string $body
588 * @return string body
590 public static function addAttachmentsToBody(int $uriid, string $body = '')
593 $item = Post::selectFirst(['body'], ['uri-id' => $uriid]);
594 if (!DBA::isResult($item)) {
597 $body = $item['body'];
599 $original_body = $body;
601 $body = preg_replace("/\s*\[attachment .*?\].*?\[\/attachment\]\s*/ism", '', $body);
603 foreach (self::getByURIId($uriid, [self::IMAGE, self::AUDIO, self::VIDEO]) as $media) {
604 if (Item::containsLink($body, $media['url'], $media['type'])) {
608 if ($media['type'] == self::IMAGE) {
609 if (!empty($media['preview'])) {
610 if (!empty($media['description'])) {
611 $body .= "\n[url=" . $media['url'] . "][img=" . $media['preview'] . ']' . $media['description'] .'[/img][/url]';
613 $body .= "\n[url=" . $media['url'] . "][img]" . $media['preview'] .'[/img][/url]';
616 if (!empty($media['description'])) {
617 $body .= "\n[img=" . $media['url'] . ']' . $media['description'] .'[/img]';
619 $body .= "\n[img]" . $media['url'] .'[/img]';
622 } elseif ($media['type'] == self::AUDIO) {
623 $body .= "\n[audio]" . $media['url'] . "[/audio]\n";
624 } elseif ($media['type'] == self::VIDEO) {
625 $body .= "\n[video]" . $media['url'] . "[/video]\n";
629 if (preg_match("/.*(\[attachment.*?\].*?\[\/attachment\]).*/ism", $original_body, $match)) {
630 $body .= "\n" . $match[1];
637 * Get preview link for given media id
639 * @param integer $id media id
640 * @param string $size One of the ProxyUtils::SIZE_* constants
641 * @return string preview link
643 public static function getPreviewUrlForId(int $id, string $size = ''):string
645 $url = DI::baseUrl() . '/photo/preview/';
647 case Proxy::SIZE_MICRO:
648 $url .= Proxy::PIXEL_MICRO . '/';
650 case Proxy::SIZE_THUMB:
651 $url .= Proxy::PIXEL_THUMB . '/';
653 case Proxy::SIZE_SMALL:
654 $url .= Proxy::PIXEL_SMALL . '/';
656 case Proxy::SIZE_MEDIUM:
657 $url .= Proxy::PIXEL_MEDIUM . '/';
659 case Proxy::SIZE_LARGE:
660 $url .= Proxy::PIXEL_LARGE . '/';
667 * Get media link for given media id
669 * @param integer $id media id
670 * @param string $size One of the ProxyUtils::SIZE_* constants
671 * @return string media link
673 public static function getUrlForId(int $id, string $size = ''):string
675 $url = DI::baseUrl() . '/photo/media/';
677 case Proxy::SIZE_MICRO:
678 $url .= Proxy::PIXEL_MICRO . '/';
680 case Proxy::SIZE_THUMB:
681 $url .= Proxy::PIXEL_THUMB . '/';
683 case Proxy::SIZE_SMALL:
684 $url .= Proxy::PIXEL_SMALL . '/';
686 case Proxy::SIZE_MEDIUM:
687 $url .= Proxy::PIXEL_MEDIUM . '/';
689 case Proxy::SIZE_LARGE:
690 $url .= Proxy::PIXEL_LARGE . '/';