]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/Media.php
Merge remote-tracking branch 'upstream/develop' into item-lock
[friendica.git] / src / Model / Post / Media.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Core\Logger;
25 use Friendica\Core\System;
26 use Friendica\Database\DBA;
27 use Friendica\Util\Images;
28
29 /**
30  * Class Media
31  *
32  * This Model class handles media interactions.
33  * This tables stores medias (images, videos, audio files) related to posts.
34  */
35 class Media
36 {
37         const UNKNOWN = 0;
38         const IMAGE   = 1;
39         const VIDEO   = 2;
40         const AUDIO   = 3;
41         const TORRENT = 16;
42
43         /**
44          * Insert a post-media record
45          *
46          * @param array $media
47          * @return void
48          */
49         public static function insert(array $media)
50         {
51                 if (empty($media['url']) || empty($media['uri-id'])) {
52                         return;
53                 }
54
55                 if (DBA::exists('post-media', ['uri-id' => $media['uri-id'], 'url' => $media['url']])) {
56                         Logger::info('Media already exists', ['uri-id' => $media['uri-id'], 'url' => $media['url'], 'callstack' => System::callstack()]);
57                         return;
58                 }
59
60                 $fields = ['type', 'mimetype', 'height', 'width', 'size', 'preview', 'preview-height', 'preview-width', 'description'];
61                 foreach ($fields as $field) {
62                         if (empty($media[$field])) {
63                                 unset($media[$field]);
64                         }
65                 }
66
67                 if ($media['type'] == self::IMAGE) {
68                         $imagedata = Images::getInfoFromURLCached($media['url']);
69                         if (!empty($imagedata)) {
70                                 $media['mimetype'] = $imagedata['mime'];
71                                 $media['size'] = $imagedata['size'];
72                                 $media['width'] = $imagedata[0];
73                                 $media['height'] = $imagedata[1];
74                         }
75                         if (!empty($media['preview'])) {
76                                 $imagedata = Images::getInfoFromURLCached($media['preview']);
77                                 if (!empty($imagedata)) {
78                                         $media['preview-width'] = $imagedata[0];
79                                         $media['preview-height'] = $imagedata[1];
80                                 }
81                         }
82                 }
83
84                 $result = DBA::insert('post-media', $media, true);
85                 Logger::info('Stored media', ['result' => $result, 'media' => $media, 'callstack' => System::callstack()]);
86         }
87
88         /**
89          * Tests for path patterns that are usef for picture links in Friendica
90          *
91          * @param string $page    Link to the image page
92          * @param string $preview Preview picture
93          * @return boolean
94          */
95         private static function isPictureLink(string $page, string $preview)
96         {
97                 return preg_match('#/photos/.*/image/#ism', $page) && preg_match('#/photo/.*-1\.#ism', $preview);
98         }
99
100         /**
101          * Add media links and remove them from the body
102          *
103          * @param integer $uriid
104          * @param string $body
105          * @return string Body without media links
106          */
107         public static function insertFromBody(int $uriid, string $body)
108         {
109                 // Simplify image codes
110                 $body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
111
112                 $attachments = [];
113                 if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) {
114                         foreach ($pictures as $picture) {
115                                 if (!self::isPictureLink($picture[1], $picture[2])) {
116                                         continue;
117                                 }
118                                 $body = str_replace($picture[0], '', $body);
119                                 $image = str_replace('-1.', '-0.', $picture[2]);
120                                 $attachments[] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $image,
121                                         'preview' => $picture[2], 'description' => $picture[3]];
122                         }
123                 }
124
125                 if (preg_match_all("/\[img=([^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
126                         foreach ($pictures as $picture) {
127                                 $body = str_replace($picture[0], '', $body);
128                                 $attachments[] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1], 'description' => $picture[2]];
129                         }
130                 }
131
132                 if (preg_match_all("#\[url=([^\]]+?)\]\s*\[img\]([^\[]+?)\[/img\]\s*\[/url\]#ism", $body, $pictures, PREG_SET_ORDER)) {
133                         foreach ($pictures as $picture) {
134                                 if (!self::isPictureLink($picture[1], $picture[2])) {
135                                         continue;
136                                 }
137                                 $body = str_replace($picture[0], '', $body);
138                                 $image = str_replace('-1.', '-0.', $picture[2]);
139                                 $attachments[] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $image,
140                                         'preview' => $picture[2], 'description' => null];
141                         }
142                 }
143
144                 if (preg_match_all("/\[img\]([^\[\]]*)\[\/img\]/ism", $body, $pictures, PREG_SET_ORDER)) {
145                         foreach ($pictures as $picture) {
146                                 $body = str_replace($picture[0], '', $body);
147                                 $attachments[] = ['uri-id' => $uriid, 'type' => self::IMAGE, 'url' => $picture[1]];
148                         }
149                 }
150
151                 if (preg_match_all("/\[audio\]([^\[\]]*)\[\/audio\]/ism", $body, $audios, PREG_SET_ORDER)) {
152                         foreach ($audios as $audio) {
153                                 $body = str_replace($audio[0], '', $body);
154                                 $attachments[] = ['uri-id' => $uriid, 'type' => self::AUDIO, 'url' => $audio[1]];
155                         }
156                 }
157
158                 if (preg_match_all("/\[video\]([^\[\]]*)\[\/video\]/ism", $body, $videos, PREG_SET_ORDER)) {
159                         foreach ($videos as $video) {
160                                 $body = str_replace($video[0], '', $body);
161                                 $attachments[] = ['uri-id' => $uriid, 'type' => self::VIDEO, 'url' => $video[1]];
162                         }
163                 }
164
165                 foreach ($attachments as $attachment) {
166                         self::insert($attachment);
167                 }
168
169                 return trim($body);
170         }
171 }