]> git.mxchange.org Git - friendica.git/blob - src/Model/Post/Link.php
We now store the receivers as well
[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 $uri_id
44          * @param string $url
45          * @return bool
46          */
47         public static function exists(int $uri_id, string $url)
48         {
49                 return DBA::exists('post-link', ['uri-id' => $uri_id, 'url' => $url]);
50         }
51
52         public static function getByLink(int $uri_id, string $url, $size = '')
53         {
54                 if (empty($uri_id) || empty($url) || Proxy::isLocalImage($url)) {
55                         return $url;
56                 }
57
58                 if (!in_array(parse_url($url, PHP_URL_SCHEME), ['http', 'https'])) {
59                         Logger::info('Bad URL, quitting', ['uri-id' => $uri_id, 'url' => $url, 'callstack' => System::callstack(20)]);
60                         return $url;
61                 }
62
63                 $link = DBA::selectFirst('post-link', ['id'], ['uri-id' => $uri_id, 'url' => $url]);
64                 if (!empty($link['id'])) {
65                         $id = $link['id'];
66                         Logger::info('Found', ['id' => $id, 'uri-id' => $uri_id, 'url' => $url]);
67                 } else {
68                         $mime = self::fetchMimeType($url);
69
70                         DBA::insert('post-link', ['uri-id' => $uri_id, 'url' => $url, 'mimetype' => $mime], Database::INSERT_IGNORE);
71                         $id = DBA::lastInsertId();
72                         Logger::info('Inserted', ['id' => $id, 'uri-id' => $uri_id, 'url' => $url]);
73                 }
74
75                 if (empty($id)) {
76                         return $url;
77                 }
78
79                 $url = DI::baseUrl() . '/photo/link/';
80                 switch ($size) {
81                         case Proxy::SIZE_MICRO:
82                                 $url .= Proxy::PIXEL_MICRO . '/';
83                                 break;
84                         case Proxy::SIZE_THUMB:
85                                 $url .= Proxy::PIXEL_THUMB . '/';
86                                 break;
87                         case Proxy::SIZE_SMALL:
88                                 $url .= Proxy::PIXEL_SMALL . '/';
89                                 break;
90                         case Proxy::SIZE_MEDIUM:
91                                 $url .= Proxy::PIXEL_MEDIUM . '/';
92                                 break;
93                         case Proxy::SIZE_LARGE:
94                                 $url .= Proxy::PIXEL_LARGE . '/';
95                                 break;
96                 }
97                 return $url . $id;
98         }
99
100         private static function fetchMimeType(string $url, string $accept = HttpClientAccept::DEFAULT)
101         {
102                 $timeout = DI::config()->get('system', 'xrd_timeout');
103
104                 $curlResult = DI::httpClient()->head($url, [HttpClientOptions::TIMEOUT => $timeout, HttpClientOptions::ACCEPT_CONTENT => $accept]);
105                 if ($curlResult->isSuccess()) {
106                         if (empty($media['mimetype'])) {
107                                 return $curlResult->getHeader('Content-Type')[0] ?? '';
108                         }
109                 }
110                 return '';
111         }
112
113         /**
114          * Add external links and replace them in the body
115          *
116          * @param integer $uriid
117          * @param string $body
118          * @return string Body with replaced links
119          */
120         public static function insertFromBody(int $uriid, string $body)
121         {
122                 if (preg_match_all("/\[img\=([0-9]*)x([0-9]*)\](http.*?)\[\/img\]/ism", $body, $pictures, PREG_SET_ORDER)) {
123                         foreach ($pictures as $picture) {
124                                 $body = str_replace($picture[3], self::getByLink($uriid, $picture[3]), $body);
125                         }
126                 }
127
128                 if (preg_match_all("/\[img=(http[^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
129                         foreach ($pictures as $picture) {
130                                 $body = str_replace($picture[1], self::getByLink($uriid, $picture[1]), $body);
131                         }
132                 }
133
134                 if (preg_match_all("/\[img\](http[^\[\]]*)\[\/img\]/ism", $body, $pictures, PREG_SET_ORDER)) {
135                         foreach ($pictures as $picture) {
136                                 $body = str_replace($picture[1], self::getByLink($uriid, $picture[1]), $body);
137                         }
138                 }
139
140                 return trim($body);
141         }
142 }