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