2021-02-16 15:20:51 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2023-01-01 14:36:24 +00:00
|
|
|
* @copyright Copyright (C) 2010-2023, the Friendica project
|
2021-02-16 15:20:51 +00:00
|
|
|
*
|
|
|
|
* @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\Module;
|
|
|
|
|
2022-10-20 21:35:01 +00:00
|
|
|
use Friendica\App;
|
2021-02-16 15:20:51 +00:00
|
|
|
use Friendica\BaseModule;
|
|
|
|
use Friendica\Content\Text\BBCode;
|
|
|
|
use Friendica\Core\Hook;
|
2022-10-20 21:35:01 +00:00
|
|
|
use Friendica\Core\L10n;
|
|
|
|
use Friendica\Core\Session\Capability\IHandleUserSessions;
|
2021-02-16 15:20:51 +00:00
|
|
|
use Friendica\Core\System;
|
|
|
|
use Friendica\Network\HTTPException\BadRequestException;
|
|
|
|
use Friendica\Util;
|
2022-10-20 21:35:01 +00:00
|
|
|
use Friendica\Util\Profiler;
|
|
|
|
use Psr\Log\LoggerInterface;
|
2021-02-16 15:20:51 +00:00
|
|
|
|
|
|
|
class ParseUrl extends BaseModule
|
|
|
|
{
|
2022-10-20 21:35:01 +00:00
|
|
|
/** @var IHandleUserSessions */
|
|
|
|
protected $userSession;
|
|
|
|
|
2022-10-23 06:18:01 +00:00
|
|
|
public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $userSession, $server, array $parameters = [])
|
2022-10-20 21:35:01 +00:00
|
|
|
{
|
|
|
|
parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
|
|
|
|
|
|
|
|
$this->userSession = $userSession;
|
|
|
|
}
|
|
|
|
|
2021-11-20 14:38:03 +00:00
|
|
|
protected function rawContent(array $request = [])
|
2021-02-16 15:20:51 +00:00
|
|
|
{
|
2022-10-20 21:35:01 +00:00
|
|
|
if (!$this->userSession->isAuthenticated()) {
|
2021-02-16 15:20:51 +00:00
|
|
|
throw new \Friendica\Network\HTTPException\ForbiddenException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$format = '';
|
|
|
|
$title = '';
|
|
|
|
$description = '';
|
|
|
|
$ret = ['success' => false, 'contentType' => ''];
|
|
|
|
|
|
|
|
if (!empty($_GET['binurl']) && Util\Strings::isHex($_GET['binurl'])) {
|
|
|
|
$url = trim(hex2bin($_GET['binurl']));
|
|
|
|
} elseif (!empty($_GET['url'])) {
|
|
|
|
$url = trim($_GET['url']);
|
|
|
|
// fallback in case no url is valid
|
|
|
|
} else {
|
|
|
|
throw new BadRequestException('No url given');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($_GET['title'])) {
|
|
|
|
$title = strip_tags(trim($_GET['title']));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($_GET['description'])) {
|
|
|
|
$description = strip_tags(trim($_GET['description']));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($_GET['tags'])) {
|
|
|
|
$arr_tags = Util\ParseUrl::convertTagsToArray($_GET['tags']);
|
|
|
|
if (count($arr_tags)) {
|
|
|
|
$str_tags = "\n" . implode(' ', $arr_tags) . "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_GET['format']) && $_GET['format'] == 'json') {
|
|
|
|
$format = 'json';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add url scheme if it is missing
|
|
|
|
$arrurl = parse_url($url);
|
|
|
|
if (empty($arrurl['scheme'])) {
|
|
|
|
if (!empty($arrurl['host'])) {
|
|
|
|
$url = 'http:' . $url;
|
|
|
|
} else {
|
|
|
|
$url = 'http://' . $url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$arr = ['url' => $url, 'format' => $format, 'text' => null];
|
|
|
|
|
|
|
|
Hook::callAll('parse_link', $arr);
|
|
|
|
|
|
|
|
if ($arr['text']) {
|
|
|
|
if ($format == 'json') {
|
|
|
|
System::jsonExit($arr['text']);
|
|
|
|
} else {
|
2022-04-10 11:03:24 +00:00
|
|
|
System::httpExit($arr['text']);
|
2021-02-16 15:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($format == 'json') {
|
2021-03-13 07:03:26 +00:00
|
|
|
$siteinfo = Util\ParseUrl::getSiteinfoCached($url);
|
2021-02-16 15:20:51 +00:00
|
|
|
|
2021-03-13 07:03:26 +00:00
|
|
|
if (in_array($siteinfo['type'], ['image', 'video', 'audio'])) {
|
|
|
|
switch ($siteinfo['type']) {
|
2021-02-16 15:20:51 +00:00
|
|
|
case 'video':
|
|
|
|
$content_type = 'video';
|
|
|
|
break;
|
|
|
|
case 'audio':
|
|
|
|
$content_type = 'audio';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$content_type = 'image';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$ret['contentType'] = $content_type;
|
|
|
|
$ret['data'] = ['url' => $url];
|
|
|
|
$ret['success'] = true;
|
|
|
|
} else {
|
|
|
|
unset($siteinfo['keywords']);
|
|
|
|
|
|
|
|
$ret['data'] = $siteinfo;
|
|
|
|
$ret['contentType'] = 'attachment';
|
|
|
|
$ret['success'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
System::jsonExit($ret);
|
|
|
|
} else {
|
2022-04-10 11:03:24 +00:00
|
|
|
System::httpExit(BBCode::embedURL($url, empty($_GET['noAttachment']), $title, $description, $_GET['tags'] ?? ''));
|
2021-02-16 15:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|