]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/Link.php
Individual callstacks are removed from the logger
[friendica.git] / src / Model / Post / Link.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\Core\Logger;
25 use Friendica\Database\Database;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
29 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
30 use Friendica\Util\HTTPSignature;
31 use Friendica\Util\Images;
32 use Friendica\Util\Proxy;
33 use Friendica\Object\Image;
34
35 /**
36  * Class Link
37  *
38  * This Model class handles post related external links
39  */
40 class Link
41 {
42         /**
43          * Check if the link is stored
44          *
45          * @param int $uriId
46          * @param string $url URL
47          * @return bool Whether record has been found
48          */
49         public static function exists(int $uriId, string $url): bool
50         {
51                 return DBA::exists('post-link', ['uri-id' => $uriId, 'url' => $url]);
52         }
53
54         /**
55          * Returns URL by URI id and other URL
56          *
57          * @param int $uriId
58          * @param string $url
59          * @param string $size
60          * @return string Found link URL + id on success, $url on failure
61          */
62         public static function getByLink(int $uriId, string $url, string $size = ''): string
63         {
64                 if (empty($uriId) || empty($url) || Proxy::isLocalImage($url)) {
65                         return $url;
66                 }
67
68                 if (!in_array(parse_url($url, PHP_URL_SCHEME), ['http', 'https'])) {
69                         Logger::info('Bad URL, quitting', ['uri-id' => $uriId, 'url' => $url]);
70                         return $url;
71                 }
72
73                 $link = DBA::selectFirst('post-link', ['id'], ['uri-id' => $uriId, 'url' => $url]);
74                 if (!empty($link['id'])) {
75                         $id = $link['id'];
76                         Logger::info('Found', ['id' => $id, 'uri-id' => $uriId, 'url' => $url]);
77                 } else {
78                         $fields = self::fetchMimeType($url);
79                         $fields['uri-id'] = $uriId;
80                         $fields['url'] = $url;
81
82                         DBA::insert('post-link', $fields, Database::INSERT_IGNORE);
83                         $id = DBA::lastInsertId();
84                         Logger::info('Inserted', $fields);
85                 }
86
87                 if (empty($id)) {
88                         return $url;
89                 }
90
91                 $url = DI::baseUrl() . '/photo/link/';
92                 switch ($size) {
93                         case Proxy::SIZE_MICRO:
94                                 $url .= Proxy::PIXEL_MICRO . '/';
95                                 break;
96
97                         case Proxy::SIZE_THUMB:
98                                 $url .= Proxy::PIXEL_THUMB . '/';
99                                 break;
100
101                         case Proxy::SIZE_SMALL:
102                                 $url .= Proxy::PIXEL_SMALL . '/';
103                                 break;
104
105                         case Proxy::SIZE_MEDIUM:
106                                 $url .= Proxy::PIXEL_MEDIUM . '/';
107                                 break;
108
109                         case Proxy::SIZE_LARGE:
110                                 $url .= Proxy::PIXEL_LARGE . '/';
111                                 break;
112                 }
113                 return $url . $id;
114         }
115
116         /**
117          * Fetches MIME type by URL and Accept: header
118          *
119          * @param string $url URL to fetch
120          * @param string $accept Comma-separated list of expected response MIME type(s)
121          * @return array Discovered MIME type and blurhash or empty array on failure
122          */
123         private static function fetchMimeType(string $url, string $accept = HttpClientAccept::DEFAULT): array
124         {
125                 $timeout = DI::config()->get('system', 'xrd_timeout');
126
127                 try {
128                         $curlResult = HTTPSignature::fetchRaw($url, 0, [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::ACCEPT_CONTENT => $accept]);
129                         if (empty($curlResult) || !$curlResult->isSuccess()) {
130                                 return [];
131                         }
132                 } catch (\Exception $exception) {
133                         Logger::notice('Error fetching url', ['url' => $url, 'exception' => $exception]);
134                         return [];
135                 }
136                 $fields = ['mimetype' => $curlResult->getHeader('Content-Type')[0]];
137
138                 $img_str = $curlResult->getBody();
139                 $image = new Image($img_str, Images::getMimeTypeByData($img_str));
140                 if ($image->isValid()) {
141                         $fields['mimetype'] = $image->getType();
142                         $fields['width']    = $image->getWidth();
143                         $fields['height']   = $image->getHeight();
144                         $fields['blurhash'] = $image->getBlurHash();
145                 }
146
147                 return $fields;
148         }
149
150         /**
151          * Add external links and replace them in the body
152          *
153          * @param integer $uriId
154          * @param string $body Item body formatted with BBCodes
155          * @return string Body with replaced links
156          */
157         public static function insertFromBody(int $uriId, string $body): string
158         {
159                 if (preg_match_all("/\[img\=([0-9]*)x([0-9]*)\](http.*?)\[\/img\]/ism", $body, $pictures, PREG_SET_ORDER)) {
160                         foreach ($pictures as $picture) {
161                                 $body = str_replace($picture[3], self::getByLink($uriId, $picture[3]), $body);
162                         }
163                 }
164
165                 if (preg_match_all("/\[img=(http[^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
166                         foreach ($pictures as $picture) {
167                                 $body = str_replace($picture[1], self::getByLink($uriId, $picture[1]), $body);
168                         }
169                 }
170
171                 if (preg_match_all("/\[img\](http[^\[\]]*)\[\/img\]/ism", $body, $pictures, PREG_SET_ORDER)) {
172                         foreach ($pictures as $picture) {
173                                 $body = str_replace($picture[1], self::getByLink($uriId, $picture[1]), $body);
174                         }
175                 }
176
177                 return trim($body);
178         }
179 }