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