]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/Media.php
b253e516e73a1796e99e8d1abd2d762a06fe9be1
[friendica.git] / src / Model / Post / Media.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\System;
28 use Friendica\Database\Database;
29 use Friendica\Database\DBA;
30 use Friendica\DI;
31 use Friendica\Util\Images;
32 use Friendica\Util\ParseUrl;
33 use Friendica\Util\Strings;
34
35 /**
36  * Class Media
37  *
38  * This Model class handles media interactions.
39  * This tables stores medias (images, videos, audio files) related to posts.
40  */
41 class Media
42 {
43         const UNKNOWN     = 0;
44         const IMAGE       = 1;
45         const VIDEO       = 2;
46         const AUDIO       = 3;
47         const TEXT        = 4;
48         const APPLICATION = 5;
49         const TORRENT     = 16;
50         const HTML        = 17;
51         const XML         = 18;
52         const PLAIN       = 19;
53         const DOCUMENT    = 128;
54
55         /**
56          * Insert a post-media record
57          *
58          * @param array $media
59          * @return void
60          */
61         public static function insert(array $media, bool $force = false)
62         {
63                 if (empty($media['url']) || empty($media['uri-id']) || !isset($media['type'])) {
64                         Logger::warning('Incomplete media data', ['media' => $media]);
65                         return;
66                 }
67
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()]);
73                         return;
74                 }
75
76                 $media = self::unsetEmptyFields($media);
77
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()]);
82                 $stored = $media;
83
84                 $media = self::fetchAdditionalData($media);
85                 $media = self::unsetEmptyFields($media);
86
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]);
90                 } else {
91                         Logger::info('Nothing to update', ['media' => $media]);
92                 }
93         }
94
95         /**
96          * Remove empty media fields
97          *
98          * @param array $media
99          * @return array cleaned media array
100          */
101         private static function unsetEmptyFields(array $media)
102         {
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]);
107                         }
108                 }
109                 return $media;
110         }
111
112         /**
113          * Copy attachments from one uri-id to another
114          *
115          * @param integer $from_uri_id
116          * @param integer $to_uri_id
117          * @return void
118          */
119         public static function copy(int $from_uri_id, int $to_uri_id)
120         {
121                 $attachments = self::getByURIId($from_uri_id);
122                 foreach ($attachments as $attachment) {
123                         $attachment['uri-id'] = $to_uri_id;
124                         self::insert($attachment);
125                 }
126         }
127
128         /**
129          * Creates the "[attach]" element from the given attributes
130          *
131          * @param string $href
132          * @param integer $length
133          * @param string $type
134          * @param string $title
135          * @return string "[attach]" element
136          */
137         public static function getAttachElement(string $href, int $length, string $type, string $title = '')
138         {
139                 $media = self::fetchAdditionalData(['type' => self::DOCUMENT, 'url' => $href,
140                         'size' => $length, 'mimetype' => $type, 'description' => $title]);
141
142                 return '[attach]href="' . $media['url'] . '" length="' . $media['size'] .
143                         '" type="' . $media['mimetype'] . '" title="' . $media['description'] . '"[/attach]';
144         }
145
146         /**
147          * Fetch additional data for the provided media array
148          *
149          * @param array $media
150          * @return array media array with additional data
151          */
152         public static function fetchAdditionalData(array $media)
153         {
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');
161                                 }
162                                 if (empty($media['size'])) {
163                                         $media['size'] = (int)$curlResult->getHeader('Content-Length');
164                                 }
165                         } else {
166                                 Logger::notice('Could not fetch head', ['media' => $media]);
167                         }
168                 }
169
170                 $filetype = !empty($media['mimetype']) ? strtolower(current(explode('/', $media['mimetype']))) : '';
171
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];
179                         } else {
180                                 Logger::notice('No image data', ['media' => $media]);
181                         }
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];
187                                 }
188                         }
189                 }
190
191                 if ($media['type'] != self::DOCUMENT) {
192                         $media = self::addType($media);
193                 }
194
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;
208                 }
209                 return $media;
210         }
211
212         /**
213          * Add the detected type to the media array
214          *
215          * @param array $data
216          * @return array data array with the detected type
217          */
218         public static function addType(array $data)
219         {
220                 if (empty($data['mimetype'])) {
221                         Logger::info('No MimeType provided', ['media' => $data]);
222                         return $data;
223                 }
224
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;
229                         return $data;
230                 }
231
232                 $filetype = strtolower($type[0]);
233                 $subtype = strtolower($type[1]);
234
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;
253                 } else {
254                         $data['type'] = self::UNKNOWN;
255                         Logger::info('Unknown type', ['filetype' => $filetype, 'subtype' => $subtype, 'media' => $data]);
256                         return $data;
257                 }
258
259                 Logger::debug('Detected type', ['filetype' => $filetype, 'subtype' => $subtype, 'media' => $data]);
260                 return $data;
261         }
262
263         /**
264          * Tests for path patterns that are usef for picture links in Friendica
265          *
266          * @param string $page    Link to the image page
267          * @param string $preview Preview picture
268          * @return boolean
269          */
270         private static function isPictureLink(string $page, string $preview)
271         {
272                 return preg_match('#/photos/.*/image/#ism', $page) && preg_match('#/photo/.*-1\.#ism', $preview);
273         }
274
275         /**
276          * Add media links and remove them from the body
277          *
278          * @param integer $uriid
279          * @param string $body
280          * @return string Body without media links
281          */
282         public static function insertFromBody(int $uriid, string $body)
283         {
284                 // Simplify image codes
285                 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
286
287                 $attachments = [];
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])) {
291                                         continue;
292                                 }
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]];
297                         }
298                 }
299
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]];
304                         }
305                 }
306
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])) {
310                                         continue;
311                                 }
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];
316                         }
317                 }
318
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]];
323                         }
324                 }
325
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]];
330                         }
331                 }
332
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]];
337                         }
338                 }
339
340                 $url = PageInfo::getRelevantUrlFromBody($body);
341                 if (!empty($url)) {
342                         Logger::debug('Got page url', ['url' => $url]);
343                         $attachments[$url] = ['uri-id' => $uriid, 'type' => self::UNKNOWN, 'url' => $url];
344                 }
345
346                 foreach ($attachments as $attachment) {
347                         self::insert($attachment);
348                 }
349
350                 return trim($body);
351         }
352
353         /**
354          * Add media links from the attachment field
355          *
356          * @param integer $uriid
357          * @param string $body
358          */
359         public static function insertFromAttachmentData(int $uriid, string $body)
360         {
361                 $data = BBCode::getAttachmentData($body);
362                 if (empty($data))  {
363                         return;
364                 }
365
366                 Logger::info('Adding attachment data', ['data' => $data]);
367                 $attachment = [
368                         'uri-id' => $uriid,
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,
378                 ];
379                 if (!empty($data['image'])) {
380                         $attachment['preview'] = $data['image'];
381                 }
382                 self::insert($attachment);
383         }
384
385         /**
386          * Add media links from the attach field
387          *
388          * @param integer $uriid
389          * @param string $attach
390          * @return void
391          */
392         public static function insertFromAttachment(int $uriid, string $attach)
393         {
394                 if (!preg_match_all('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\"(?: title=\"(.*?)\")?|', $attach, $matches, PREG_SET_ORDER)) {
395                         return;
396                 }
397
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] ?? '';
405
406                         self::insert($media);
407                 }
408         }
409
410         /**
411          * Retrieves the media attachments associated with the provided item ID.
412          *
413          * @param int $uri_id
414          * @param array $types
415          * @return array
416          * @throws \Exception
417          */
418         public static function getByURIId(int $uri_id, array $types = [])
419         {
420                 $condition = ['uri-id' => $uri_id];
421
422                 if (!empty($types)) {
423                         $condition = DBA::mergeConditions($condition, ['type' => $types]);
424                 }
425
426                 return DBA::selectToArray('post-media', [], $condition);
427         }
428
429         /**
430          * Checks if media attachments are associated with the provided item ID.
431          *
432          * @param int $uri_id
433          * @param array $types
434          * @return array
435          * @throws \Exception
436          */
437         public static function existsByURIId(int $uri_id, array $types = [])
438         {
439                 $condition = ['uri-id' => $uri_id];
440
441                 if (!empty($types)) {
442                         $condition = DBA::mergeConditions($condition, ['type' => $types]);
443                 }
444
445                 return DBA::exists('post-media', $condition);
446         }
447
448         /**
449          * Split the attachment media in the three segments "visual", "link" and "additional"
450          * 
451          * @param int    $uri_id 
452          * @param string $guid
453          * @param array  $links ist of links that shouldn't be added 
454          * @return array attachments
455          */
456         public static function splitAttachments(int $uri_id, string $guid = '', array $links = [])
457         {
458                 $attachments = ['visual' => [], 'link' => [], 'additional' => []];
459
460                 $media = self::getByURIId($uri_id);
461                 if (empty($media)) {
462                         return $attachments;
463                 }
464
465                 $height = 0;
466                 $selected = '';
467
468                 foreach ($media as $medium) {
469                         foreach ($links as $link) {
470                                 if (Strings::compareLink($link, $medium['url'])) {
471                                         continue 2;
472                                 }
473                         }
474
475                         $type = explode('/', current(explode(';', $medium['mimetype'])));
476                         if (count($type) < 2) {
477                                 Logger::info('Unknown MimeType', ['type' => $type, 'media' => $medium]);
478                                 $filetype = 'unkn';
479                                 $subtype = 'unkn';
480                         } else {
481                                 $filetype = strtolower($type[0]);
482                                 $subtype = strtolower($type[1]);
483                         }
484
485                         $medium['filetype'] = $filetype;
486                         $medium['subtype'] = $subtype;
487
488                         if ($medium['type'] == self::HTML || (($filetype == 'text') && ($subtype == 'html'))) {
489                                 $attachments['link'][] = $medium;
490                                 continue;
491                         }
492
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'];
504                                         }
505                                         $video[$medium['url']] = $medium;
506                                 } else {
507                                         $attachments['visual'][] = $medium;
508                                 }
509                         } else {
510                                 $attachments['additional'][] = $medium;
511                         }
512                 }
513                 if (!empty($selected)) {
514                         $attachments['visual'][] = $video[$selected];
515                         unset($video[$selected]);
516                         foreach ($video as $element) {
517                                 $attachments['additional'][] = $element;
518                         }
519                 }
520                 return $attachments;
521         }
522 }