3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Model\Post;
24 use Friendica\Core\Logger;
25 use Friendica\Core\System;
26 use Friendica\Database\Database;
27 use Friendica\Database\DBA;
29 use Friendica\Network\HTTPRequestOptions;
30 use Friendica\Util\Proxy;
35 * This Model class handles post related external links
39 public static function getByLink(int $uri_id, string $url, $size = '')
41 if (empty($uri_id) || empty($url) || Proxy::isLocalImage($url)) {
45 if (!in_array(parse_url($url, PHP_URL_SCHEME), ['http', 'https'])) {
46 Logger::info('Bad URL, quitting', ['uri-id' => $uri_id, 'url' => $url, 'callstack' => System::callstack(20)]);
50 $link = DBA::selectFirst('post-link', ['id'], ['uri-id' => $uri_id, 'url' => $url]);
51 if (!empty($link['id'])) {
53 Logger::info('Found', ['id' => $id, 'uri-id' => $uri_id, 'url' => $url]);
55 $mime = self::fetchMimeType($url);
57 DBA::insert('post-link', ['uri-id' => $uri_id, 'url' => $url, 'mimetype' => $mime], Database::INSERT_IGNORE);
58 $id = DBA::lastInsertId();
59 Logger::info('Inserted', ['id' => $id, 'uri-id' => $uri_id, 'url' => $url]);
66 $url = DI::baseUrl() . '/photo/link/';
68 case Proxy::SIZE_MICRO:
69 $url .= Proxy::PIXEL_MICRO . '/';
71 case Proxy::SIZE_THUMB:
72 $url .= Proxy::PIXEL_THUMB . '/';
74 case Proxy::SIZE_SMALL:
75 $url .= Proxy::PIXEL_SMALL . '/';
77 case Proxy::SIZE_MEDIUM:
78 $url .= Proxy::PIXEL_MEDIUM . '/';
80 case Proxy::SIZE_LARGE:
81 $url .= Proxy::PIXEL_LARGE . '/';
87 private static function fetchMimeType(string $url)
89 $timeout = DI::config()->get('system', 'xrd_timeout');
91 $curlResult = DI::httpRequest()->head($url, [HTTPRequestOptions::TIMEOUT => $timeout]);
92 if ($curlResult->isSuccess()) {
93 if (empty($media['mimetype'])) {
94 return $curlResult->getHeader('Content-Type')[0] ?? '';
101 * Add external links and replace them in the body
103 * @param integer $uriid
104 * @param string $body
105 * @return string Body with replaced links
107 public static function insertFromBody(int $uriid, string $body)
109 if (preg_match_all("/\[img\=([0-9]*)x([0-9]*)\](http.*?)\[\/img\]/ism", $body, $pictures, PREG_SET_ORDER)) {
110 foreach ($pictures as $picture) {
111 $body = str_replace($picture[3], self::getByLink($uriid, $picture[3]), $body);
115 if (preg_match_all("/\[img=(http[^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
116 foreach ($pictures as $picture) {
117 $body = str_replace($picture[1], self::getByLink($uriid, $picture[1]), $body);
121 if (preg_match_all("/\[img\](http[^\[\]]*)\[\/img\]/ism", $body, $pictures, PREG_SET_ORDER)) {
122 foreach ($pictures as $picture) {
123 $body = str_replace($picture[1], self::getByLink($uriid, $picture[1]), $body);