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\PageInfo;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Core\Logger;
27 use Friendica\Core\System;
28 use Friendica\Database\Database;
29 use Friendica\Database\DBA;
31 use Friendica\Util\Images;
32 use Friendica\Util\ParseUrl;
33 use Friendica\Util\Strings;
38 * This Model class handles media interactions.
39 * This tables stores medias (images, videos, audio files) related to posts.
48 const APPLICATION = 5;
56 * Insert a post-media record
61 public static function insert(array $media, bool $force = false)
63 if (empty($media['url']) || empty($media['uri-id']) || !isset($media['type'])) {
64 Logger::warning('Incomplete media data', ['media' => $media]);
68 // "document" has got the lowest priority. So when the same file is both attached as document
69 // and embedded as picture then we only store the picture or replace the document
70 $found = DBA::selectFirst('post-media', ['type'], ['uri-id' => $media['uri-id'], 'url' => $media['url']]);
71 if (!$force && !empty($found) && (($found['type'] != self::DOCUMENT) || ($media['type'] == self::DOCUMENT))) {
72 Logger::info('Media already exists', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'callstack' => System::callstack()]);
76 $media = self::unsetEmptyFields($media);
78 // We are storing as fast as possible to avoid duplicated network requests
79 // when fetching additional information for pictures and other content.
80 $result = DBA::insert('post-media', $media, Database::INSERT_UPDATE);
81 Logger::info('Stored media', ['result' => $result, 'media' => $media, 'callstack' => System::callstack()]);
84 $media = self::fetchAdditionalData($media);
85 $media = self::unsetEmptyFields($media);
87 if (array_diff_assoc($media, $stored)) {
88 $result = DBA::insert('post-media', $media, Database::INSERT_UPDATE);
89 Logger::info('Updated media', ['result' => $result, 'media' => $media]);
91 Logger::info('Nothing to update', ['media' => $media]);
96 * Remove empty media fields
99 * @return array cleaned media array
101 private static function unsetEmptyFields(array $media)
103 $fields = ['mimetype', 'height', 'width', 'size', 'preview', 'preview-height', 'preview-width', 'description'];
104 foreach ($fields as $field) {
105 if (empty($media[$field])) {
106 unset($media[$field]);
113 * Copy attachments from one uri-id to another
115 * @param integer $from_uri_id
116 * @param integer $to_uri_id
119 public static function copy(int $from_uri_id, int $to_uri_id)
121 $attachments = self::getByURIId($from_uri_id);
122 foreach ($attachments as $attachment) {
123 $attachment['uri-id'] = $to_uri_id;
124 self::insert($attachment);
129 * Creates the "[attach]" element from the given attributes
131 * @param string $href
132 * @param integer $length
133 * @param string $type
134 * @param string $title
135 * @return string "[attach]" element
137 public static function getAttachElement(string $href, int $length, string $type, string $title = '')
139 $media = self::fetchAdditionalData(['type' => self::DOCUMENT, 'url' => $href,
140 'size' => $length, 'mimetype' => $type, 'description' => $title]);
142 return '[attach]href="' . $media['url'] . '" length="' . $media['size'] .
143 '" type="' . $media['mimetype'] . '" title="' . $media['description'] . '"[/attach]';
147 * Fetch additional data for the provided media array
149 * @param array $media
150 * @return array media array with additional data
152 public static function fetchAdditionalData(array $media)
154 // Fetch the mimetype or size if missing.
155 if (empty($media['mimetype']) || empty($media['size'])) {
156 $timeout = DI::config()->get('system', 'xrd_timeout');
157 $curlResult = DI::httpRequest()->head($media['url'], ['timeout' => $timeout]);
158 if ($curlResult->isSuccess()) {
159 if (empty($media['mimetype'])) {
160 $media['mimetype'] = $curlResult->getHeader('Content-Type');
162 if (empty($media['size'])) {
163 $media['size'] = (int)$curlResult->getHeader('Content-Length');
166 Logger::notice('Could not fetch head', ['media' => $media]);
170 $filetype = !empty($media['mimetype']) ? strtolower(current(explode('/', $media['mimetype']))) : '';
172 if (($media['type'] == self::IMAGE) || ($filetype == 'image')) {
173 $imagedata = Images::getInfoFromURLCached($media['url']);
174 if (!empty($imagedata)) {
175 $media['mimetype'] = $imagedata['mime'];
176 $media['size'] = $imagedata['size'];
177 $media['width'] = $imagedata[0];
178 $media['height'] = $imagedata[1];
180 Logger::notice('No image data', ['media' => $media]);
182 if (!empty($media['preview'])) {
183 $imagedata = Images::getInfoFromURLCached($media['preview']);
184 if (!empty($imagedata)) {
185 $media['preview-width'] = $imagedata[0];
186 $media['preview-height'] = $imagedata[1];
191 if ($media['type'] != self::DOCUMENT) {
192 $media = self::addType($media);
195 if ($media['type'] == self::HTML) {
196 $data = ParseUrl::getSiteinfoCached($media['url'], false);
197 $media['preview'] = $data['images'][0]['src'] ?? null;
198 $media['preview-height'] = $data['images'][0]['height'] ?? null;
199 $media['preview-width'] = $data['images'][0]['width'] ?? null;
200 $media['description'] = $data['text'] ?? null;
201 $media['name'] = $data['title'] ?? null;
202 $media['author-url'] = $data['author_url'] ?? null;
203 $media['author-name'] = $data['author_name'] ?? null;
204 $media['author-image'] = $data['author_img'] ?? null;
205 $media['publisher-url'] = $data['publisher_url'] ?? null;
206 $media['publisher-name'] = $data['publisher_name'] ?? null;
207 $media['publisher-image'] = $data['publisher_img'] ?? null;
213 * Add the detected type to the media array
216 * @return array data array with the detected type
218 public static function addType(array $data)
220 if (empty($data['mimetype'])) {
221 Logger::info('No MimeType provided', ['media' => $data]);
225 $type = explode('/', current(explode(';', $data['mimetype'])));
226 if (count($type) < 2) {
227 Logger::info('Unknown MimeType', ['type' => $type, 'media' => $data]);
228 $data['type'] = self::UNKNOWN;
232 $filetype = strtolower($type[0]);
233 $subtype = strtolower($type[1]);
235 if ($filetype == 'image') {
236 $data['type'] = self::IMAGE;
237 } elseif ($filetype == 'video') {
238 $data['type'] = self::VIDEO;
239 } elseif ($filetype == 'audio') {
240 $data['type'] = self::AUDIO;
241 } elseif (($filetype == 'text') && ($subtype == 'html')) {
242 $data['type'] = self::HTML;
243 } elseif (($filetype == 'text') && ($subtype == 'xml')) {
244 $data['type'] = self::XML;
245 } elseif (($filetype == 'text') && ($subtype == 'plain')) {
246 $data['type'] = self::PLAIN;
247 } elseif ($filetype == 'text') {
248 $data['type'] = self::TEXT;
249 } elseif (($filetype == 'application') && ($subtype == 'x-bittorrent')) {
250 $data['type'] = self::TORRENT;
251 } elseif ($filetype == 'application') {
252 $data['type'] = self::APPLICATION;
254 $data['type'] = self::UNKNOWN;
255 Logger::info('Unknown type', ['filetype' => $filetype, 'subtype' => $subtype, 'media' => $data]);
259 Logger::debug('Detected type', ['filetype' => $filetype, 'subtype' => $subtype, 'media' => $data]);
264 * Tests for path patterns that are usef for picture links in Friendica
266 * @param string $page Link to the image page
267 * @param string $preview Preview picture
270 private static function isPictureLink(string $page, string $preview)
272 return preg_match('#/photos/.*/image/#ism', $page) && preg_match('#/photo/.*-1\.#ism', $preview);
276 * Add media links and remove them from the body
278 * @param integer $uriid
279 * @param string $body
280 * @return string Body without media links
282 public static function insertFromBody(int $uriid, string $body)
284 // Simplify image codes
285 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
288 if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) {
289 foreach ($pictures as $picture) {
290 if (!self::isPictureLink($picture[1], $picture[2])) {
293 $body = str_replace($picture[0], '', $body);
294 $image = str_replace('-1.', '-0.', $picture[2]);
295 $attachments[$image] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $image,
296 'preview' => $picture[2], 'description' => $picture[3]];
300 if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
301 foreach ($pictures as $picture) {
302 $body = str_replace($picture[0], '', $body);
303 $attachments[$picture[1]] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1], 'description' => $picture[2]];
307 if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) {
308 foreach ($pictures as $picture) {
309 if (!self::isPictureLink($picture[1], $picture[2])) {
312 $body = str_replace($picture[0], '', $body);
313 $image = str_replace('-1.', '-0.', $picture[2]);
314 $attachments[$image] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $image,
315 'preview' => $picture[2], 'description' => null];
319 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/ism", $body, $pictures, PREG_SET_ORDER)) {
320 foreach ($pictures as $picture) {
321 $body = str_replace($picture[0], '', $body);
322 $attachments[$picture[1]] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1]];
326 if (preg_match_all("/\[audio\]([^\[\]]*)\[\/audio\]/ism", $body, $audios, PREG_SET_ORDER)) {
327 foreach ($audios as $audio) {
328 $body = str_replace($audio[0], '', $body);
329 $attachments[$audio[1]] = ['uri-id' => $uriid, 'type' => self::AUDIO, 'url' => $audio[1]];
333 if (preg_match_all("/\[video\]([^\[\]]*)\[\/video\]/ism", $body, $videos, PREG_SET_ORDER)) {
334 foreach ($videos as $video) {
335 $body = str_replace($video[0], '', $body);
336 $attachments[$video[1]] = ['uri-id' => $uriid, 'type' => self::VIDEO, 'url' => $video[1]];
340 $url = PageInfo::getRelevantUrlFromBody($body);
342 Logger::debug('Got page url', ['url' => $url]);
343 $attachments[$url] = ['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url];
346 foreach ($attachments as $attachment) {
347 self::insert($attachment);
354 * Add media links from the attachment field
356 * @param integer $uriid
357 * @param string $body
359 public static function insertFromAttachmentData(int $uriid, string $body)
361 $data = BBCode::getAttachmentData($body);
366 Logger::info('Adding attachment data', ['data' => $data]);
369 'type' => self::HTML,
370 'url' => $data['url'],
371 'preview' => $data['preview'] ?? null,
372 'description' => $data['description'] ?? null,
373 'name' => $data['title'] ?? null,
374 'author-url' => $data['author_url'] ?? null,
375 'author-name' => $data['author_name'] ?? null,
376 'publisher-url' => $data['provider_url'] ?? null,
377 'publisher-name' => $data['provider_name'] ?? null,
379 if (!empty($data['image'])) {
380 $attachment['preview'] = $data['image'];
382 self::insert($attachment);
386 * Add media links from the attach field
388 * @param integer $uriid
389 * @param string $attach
392 public static function insertFromAttachment(int $uriid, string $attach)
394 if (!preg_match_all('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\"(?: title=\"(.*?)\")?|', $attach, $matches, PREG_SET_ORDER)) {
398 foreach ($matches as $attachment) {
399 $media['type'] = self::DOCUMENT;
400 $media['uri-id'] = $uriid;
401 $media['url'] = $attachment[1];
402 $media['size'] = $attachment[2];
403 $media['mimetype'] = $attachment[3];
404 $media['description'] = $attachment[4] ?? '';
406 self::insert($media);
411 * Retrieves the media attachments associated with the provided item ID.
414 * @param array $types
418 public static function getByURIId(int $uri_id, array $types = [])
420 $condition = ['uri-id' => $uri_id];
422 if (!empty($types)) {
423 $condition = DBA::mergeConditions($condition, ['type' => $types]);
426 return DBA::selectToArray('post-media', [], $condition);
430 * Checks if media attachments are associated with the provided item ID.
433 * @param array $types
437 public static function existsByURIId(int $uri_id, array $types = [])
439 $condition = ['uri-id' => $uri_id];
441 if (!empty($types)) {
442 $condition = DBA::mergeConditions($condition, ['type' => $types]);
445 return DBA::exists('post-media', $condition);
449 * Split the attachment media in the three segments "visual", "link" and "additional"
452 * @param string $guid
453 * @param array $links ist of links that shouldn't be added
454 * @return array attachments
456 public static function splitAttachments(int $uri_id, string $guid = '', array $links = [])
458 $attachments = ['visual' => [], 'link' => [], 'additional' => []];
460 $media = self::getByURIId($uri_id);
468 foreach ($media as $medium) {
469 foreach ($links as $link) {
470 if (Strings::compareLink($link, $medium['url'])) {
475 $type = explode('/', current(explode(';', $medium['mimetype'])));
476 if (count($type) < 2) {
477 Logger::info('Unknown MimeType', ['type' => $type, 'media' => $medium]);
481 $filetype = strtolower($type[0]);
482 $subtype = strtolower($type[1]);
485 $medium['filetype'] = $filetype;
486 $medium['subtype'] = $subtype;
488 if ($medium['type'] == self::HTML || (($filetype == 'text') && ($subtype == 'html'))) {
489 $attachments['link'][] = $medium;
493 if (in_array($medium['type'], [self::AUDIO, self::IMAGE]) ||
494 in_array($filetype, ['audio', 'image'])) {
495 $attachments['visual'][] = $medium;
496 } elseif (($medium['type'] == self::VIDEO) || ($filetype == 'video')) {
497 if (strpos($medium['url'], $guid) !== false) {
498 // Peertube videos are delivered in many different resolutions. We pick a moderate one.
499 // By checking against the GUID we also ensure to only work this way on Peertube posts.
500 // This wouldn't be executed when someone for example on Mastodon was sharing multiple videos in a single post.
501 if (empty($height) || ($height > $medium['height']) && ($medium['height'] >= 480)) {
502 $height = $medium['height'];
503 $selected = $medium['url'];
505 $video[$medium['url']] = $medium;
507 $attachments['visual'][] = $medium;
510 $attachments['additional'][] = $medium;
513 if (!empty($selected)) {
514 $attachments['visual'][] = $video[$selected];
515 unset($video[$selected]);
516 foreach ($video as $element) {
517 $attachments['additional'][] = $element;