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\Module;
24 use Friendica\BaseModule;
25 use Friendica\Content\Text\BBCode;
26 use Friendica\Core\Hook;
27 use Friendica\Core\Session;
28 use Friendica\Core\System;
29 use Friendica\Network\HTTPException\BadRequestException;
32 class ParseUrl extends BaseModule
34 public static function rawContent(array $parameters = [])
36 if (!Session::isAuthenticated()) {
37 throw new \Friendica\Network\HTTPException\ForbiddenException();
43 $ret = ['success' => false, 'contentType' => ''];
45 if (!empty($_GET['binurl']) && Util\Strings::isHex($_GET['binurl'])) {
46 $url = trim(hex2bin($_GET['binurl']));
47 } elseif (!empty($_GET['url'])) {
48 $url = trim($_GET['url']);
49 // fallback in case no url is valid
51 throw new BadRequestException('No url given');
54 if (!empty($_GET['title'])) {
55 $title = strip_tags(trim($_GET['title']));
58 if (!empty($_GET['description'])) {
59 $description = strip_tags(trim($_GET['description']));
62 if (!empty($_GET['tags'])) {
63 $arr_tags = Util\ParseUrl::convertTagsToArray($_GET['tags']);
64 if (count($arr_tags)) {
65 $str_tags = "\n" . implode(' ', $arr_tags) . "\n";
69 if (isset($_GET['format']) && $_GET['format'] == 'json') {
73 // Add url scheme if it is missing
74 $arrurl = parse_url($url);
75 if (empty($arrurl['scheme'])) {
76 if (!empty($arrurl['host'])) {
77 $url = 'http:' . $url;
79 $url = 'http://' . $url;
83 $arr = ['url' => $url, 'format' => $format, 'text' => null];
85 Hook::callAll('parse_link', $arr);
88 if ($format == 'json') {
89 System::jsonExit($arr['text']);
96 if ($format == 'json') {
97 $siteinfo = Util\ParseUrl::getSiteinfoCached($url);
99 if (in_array($siteinfo['type'], ['image', 'video', 'audio'])) {
100 switch ($siteinfo['type']) {
102 $content_type = 'video';
105 $content_type = 'audio';
108 $content_type = 'image';
112 $ret['contentType'] = $content_type;
113 $ret['data'] = ['url' => $url];
114 $ret['success'] = true;
116 unset($siteinfo['keywords']);
118 $ret['data'] = $siteinfo;
119 $ret['contentType'] = 'attachment';
120 $ret['success'] = true;
123 System::jsonExit($ret);
125 echo BBCode::embedURL($url, empty($_GET['noAttachment']), $title, $description, $_GET['tags'] ?? '');