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