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