3 * @copyright Copyright (C) 2010-2023, 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\Module;
25 use Friendica\BaseModule;
26 use Friendica\Content\Text\BBCode;
27 use Friendica\Core\Hook;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Session\Capability\IHandleUserSessions;
30 use Friendica\Core\System;
31 use Friendica\Network\HTTPException\BadRequestException;
33 use Friendica\Util\Profiler;
34 use Psr\Log\LoggerInterface;
36 class ParseUrl extends BaseModule
38 /** @var IHandleUserSessions */
39 protected $userSession;
41 public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $userSession, $server, array $parameters = [])
43 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
45 $this->userSession = $userSession;
48 protected function rawContent(array $request = [])
50 if (!$this->userSession->isAuthenticated()) {
51 throw new \Friendica\Network\HTTPException\ForbiddenException();
57 $ret = ['success' => false, 'contentType' => ''];
59 if (!empty($_GET['binurl']) && Util\Strings::isHex($_GET['binurl'])) {
60 $url = trim(hex2bin($_GET['binurl']));
61 } elseif (!empty($_GET['url'])) {
62 $url = trim($_GET['url']);
63 // fallback in case no url is valid
65 throw new BadRequestException('No url given');
68 if (!empty($_GET['title'])) {
69 $title = strip_tags(trim($_GET['title']));
72 if (!empty($_GET['description'])) {
73 $description = strip_tags(trim($_GET['description']));
76 if (!empty($_GET['tags'])) {
77 $arr_tags = Util\ParseUrl::convertTagsToArray($_GET['tags']);
78 if (count($arr_tags)) {
79 $str_tags = "\n" . implode(' ', $arr_tags) . "\n";
83 if (isset($_GET['format']) && $_GET['format'] == 'json') {
87 // Add url scheme if it is missing
88 $arrurl = parse_url($url);
89 if (empty($arrurl['scheme'])) {
90 if (!empty($arrurl['host'])) {
91 $url = 'http:' . $url;
93 $url = 'http://' . $url;
97 $arr = ['url' => $url, 'format' => $format, 'text' => null];
99 Hook::callAll('parse_link', $arr);
102 if ($format == 'json') {
103 System::jsonExit($arr['text']);
105 System::httpExit($arr['text']);
109 if ($format == 'json') {
110 $siteinfo = Util\ParseUrl::getSiteinfoCached($url);
112 if (in_array($siteinfo['type'], ['image', 'video', 'audio'])) {
113 switch ($siteinfo['type']) {
115 $content_type = 'video';
118 $content_type = 'audio';
121 $content_type = 'image';
125 $ret['contentType'] = $content_type;
126 $ret['data'] = ['url' => $url];
127 $ret['success'] = true;
129 unset($siteinfo['keywords']);
131 $ret['data'] = $siteinfo;
132 $ret['contentType'] = 'attachment';
133 $ret['success'] = true;
136 System::jsonExit($ret);
138 System::httpExit(BBCode::embedURL($url, empty($_GET['noAttachment']), $title, $description, $_GET['tags'] ?? ''));