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