2021-07-05 14:45:49 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Model\Post;
|
|
|
|
|
|
|
|
use Friendica\Core\Logger;
|
2021-07-06 13:40:40 -04:00
|
|
|
use Friendica\Core\System;
|
2021-07-20 23:43:30 -04:00
|
|
|
use Friendica\Database\Database;
|
2021-07-05 14:45:49 -04:00
|
|
|
use Friendica\Database\DBA;
|
|
|
|
use Friendica\DI;
|
2021-08-25 15:45:15 -04:00
|
|
|
use Friendica\Network\HTTPClientOptions;
|
2021-07-05 14:45:49 -04:00
|
|
|
use Friendica\Util\Proxy;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Link
|
|
|
|
*
|
|
|
|
* This Model class handles post related external links
|
|
|
|
*/
|
|
|
|
class Link
|
|
|
|
{
|
|
|
|
public static function getByLink(int $uri_id, string $url, $size = '')
|
|
|
|
{
|
2021-07-06 02:45:53 -04:00
|
|
|
if (empty($uri_id) || empty($url) || Proxy::isLocalImage($url)) {
|
2021-07-05 14:45:49 -04:00
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2021-07-06 11:06:15 -04:00
|
|
|
if (!in_array(parse_url($url, PHP_URL_SCHEME), ['http', 'https'])) {
|
2021-07-06 13:40:40 -04:00
|
|
|
Logger::info('Bad URL, quitting', ['uri-id' => $uri_id, 'url' => $url, 'callstack' => System::callstack(20)]);
|
2021-07-06 11:06:15 -04:00
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2021-07-05 14:45:49 -04:00
|
|
|
$link = DBA::selectFirst('post-link', ['id'], ['uri-id' => $uri_id, 'url' => $url]);
|
|
|
|
if (!empty($link['id'])) {
|
|
|
|
$id = $link['id'];
|
2021-07-06 11:06:15 -04:00
|
|
|
Logger::info('Found', ['id' => $id, 'uri-id' => $uri_id, 'url' => $url]);
|
2021-07-05 14:45:49 -04:00
|
|
|
} else {
|
|
|
|
$mime = self::fetchMimeType($url);
|
2021-07-05 15:42:17 -04:00
|
|
|
|
2021-07-20 23:43:30 -04:00
|
|
|
DBA::insert('post-link', ['uri-id' => $uri_id, 'url' => $url, 'mimetype' => $mime], Database::INSERT_IGNORE);
|
2021-07-05 14:45:49 -04:00
|
|
|
$id = DBA::lastInsertId();
|
2021-07-06 11:06:15 -04:00
|
|
|
Logger::info('Inserted', ['id' => $id, 'uri-id' => $uri_id, 'url' => $url]);
|
2021-07-05 14:45:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($id)) {
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = DI::baseUrl() . '/photo/link/';
|
|
|
|
switch ($size) {
|
|
|
|
case Proxy::SIZE_MICRO:
|
|
|
|
$url .= Proxy::PIXEL_MICRO . '/';
|
|
|
|
break;
|
|
|
|
case Proxy::SIZE_THUMB:
|
|
|
|
$url .= Proxy::PIXEL_THUMB . '/';
|
|
|
|
break;
|
|
|
|
case Proxy::SIZE_SMALL:
|
|
|
|
$url .= Proxy::PIXEL_SMALL . '/';
|
|
|
|
break;
|
|
|
|
case Proxy::SIZE_MEDIUM:
|
|
|
|
$url .= Proxy::PIXEL_MEDIUM . '/';
|
|
|
|
break;
|
|
|
|
case Proxy::SIZE_LARGE:
|
|
|
|
$url .= Proxy::PIXEL_LARGE . '/';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return $url . $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function fetchMimeType(string $url)
|
|
|
|
{
|
|
|
|
$timeout = DI::config()->get('system', 'xrd_timeout');
|
2021-07-05 15:42:17 -04:00
|
|
|
|
2021-08-25 15:54:54 -04:00
|
|
|
$curlResult = DI::httpClient()->head($url, [HTTPClientOptions::TIMEOUT => $timeout]);
|
2021-07-05 14:45:49 -04:00
|
|
|
if ($curlResult->isSuccess()) {
|
|
|
|
if (empty($media['mimetype'])) {
|
2021-08-20 14:05:41 -04:00
|
|
|
return $curlResult->getHeader('Content-Type')[0] ?? '';
|
2021-07-05 14:45:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add external links and replace them in the body
|
|
|
|
*
|
|
|
|
* @param integer $uriid
|
|
|
|
* @param string $body
|
|
|
|
* @return string Body with replaced links
|
|
|
|
*/
|
|
|
|
public static function insertFromBody(int $uriid, string $body)
|
|
|
|
{
|
2021-07-06 12:10:10 -04:00
|
|
|
if (preg_match_all("/\[img\=([0-9]*)x([0-9]*)\](http.*?)\[\/img\]/ism", $body, $pictures, PREG_SET_ORDER)) {
|
2021-07-05 14:45:49 -04:00
|
|
|
foreach ($pictures as $picture) {
|
|
|
|
$body = str_replace($picture[3], self::getByLink($uriid, $picture[3]), $body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 12:10:10 -04:00
|
|
|
if (preg_match_all("/\[img=(http[^\[\]]*)\]([^\[\]]*)\[\/img\]/Usi", $body, $pictures, PREG_SET_ORDER)) {
|
2021-07-05 14:45:49 -04:00
|
|
|
foreach ($pictures as $picture) {
|
2021-07-06 11:06:15 -04:00
|
|
|
$body = str_replace($picture[1], self::getByLink($uriid, $picture[1]), $body);
|
2021-07-05 14:45:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 12:10:10 -04:00
|
|
|
if (preg_match_all("/\[img\](http[^\[\]]*)\[\/img\]/ism", $body, $pictures, PREG_SET_ORDER)) {
|
2021-07-05 14:45:49 -04:00
|
|
|
foreach ($pictures as $picture) {
|
|
|
|
$body = str_replace($picture[1], self::getByLink($uriid, $picture[1]), $body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return trim($body);
|
|
|
|
}
|
|
|
|
}
|