4 * @file mod/parse_url.php
7 * This module does parse an url for embeddable content (audio, video, image files or link)
8 * information and does format this information to BBCode
10 * @see ParseUrl::getSiteinfo() for more information about scraping embeddable content
14 use Friendica\Core\Hook;
15 use Friendica\Core\Logger;
16 use Friendica\Core\System;
17 use Friendica\Util\Network;
18 use Friendica\Util\ParseUrl;
19 use Friendica\Util\Strings;
21 function parse_url_content(App $a)
26 $ret= ['success' => false, 'contentType' => ''];
30 if (!empty($_GET['binurl']) && Strings::isHex($_GET['binurl'])) {
31 $url = trim(hex2bin($_GET['binurl']));
32 } elseif (!empty($_GET['url'])) {
33 $url = trim($_GET['url']);
34 // fallback in case no url is valid
36 Logger::info('No url given');
40 if (!empty($_GET['title'])) {
41 $title = strip_tags(trim($_GET['title']));
44 if (!empty($_GET['description'])) {
45 $text = strip_tags(trim($_GET['description']));
48 if (!empty($_GET['tags'])) {
49 $arr_tags = ParseUrl::convertTagsToArray($_GET['tags']);
50 if (count($arr_tags)) {
51 $str_tags = $br . implode(' ', $arr_tags) . $br;
55 if (isset($_GET['format']) && $_GET['format'] == 'json') {
59 // Add url scheme if it is missing
60 $arrurl = parse_url($url);
61 if (empty($arrurl['scheme'])) {
62 if (!empty($arrurl['host'])) {
63 $url = 'http:' . $url;
65 $url = 'http://' . $url;
71 // Check if the URL is an image, video or audio file. If so format
72 // the URL with the corresponding BBCode media tag
73 // Fetch the header of the URL
74 $curlResponse = Network::curl($url, false, ['novalidate' => true, 'nobody' => true]);
76 if ($curlResponse->isSuccess()) {
77 // Convert the header fields into an array
79 $h = explode("\n", $curlResponse->getHeader());
81 $header = array_map('trim', explode(':', trim($l), 2));
82 if (count($header) == 2) {
83 list($k, $v) = $header;
90 if (array_key_exists('Content-Type', $hdrs)) {
91 $type = $hdrs['Content-Type'];
94 if (stripos($type, 'image/') !== false) {
95 $content_type = 'image';
96 $bbcode = $br . '[img]' . $url . '[/img]' . $br;
98 if (stripos($type, 'video/') !== false) {
99 $content_type = 'video';
100 $bbcode = $br . '[video]' . $url . '[/video]' . $br;
102 if (stripos($type, 'audio/') !== false) {
103 $content_type = 'audio';
104 $bbcode = $br . '[audio]' . $url . '[/audio]' . $br;
107 if (!empty($content_type)) {
108 if ($format == 'json') {
109 $ret['contentType'] = $content_type;
110 $ret['data'] = ['url' => $url];
111 $ret['success'] = true;
112 System::jsonExit($ret);
121 $template = '[bookmark=%s]%s[/bookmark]%s';
123 $arr = ['url' => $url, 'text' => ''];
125 Hook::callAll('parse_link', $arr);
127 if (strlen($arr['text'])) {
132 // If there is already some content information submitted we don't
133 // need to parse the url for content.
134 if (!empty($url) && !empty($title) && !empty($text)) {
135 $title = str_replace(["\r", "\n"], ['', ''], $title);
137 $text = '[quote]' . trim($text) . '[/quote]' . $br;
139 $result = sprintf($template, $url, ($title) ? $title : $url, $text) . $str_tags;
141 Logger::log('(unparsed): returns: ' . $result);
147 // Fetch the information directly from the webpage
148 $siteinfo = ParseUrl::getSiteinfo($url);
150 unset($siteinfo['keywords']);
152 // Bypass attachment if parse url for a comment
153 if (!empty($_GET['noAttachment'])) {
154 echo $br . '[url=' . $url . ']' . $siteinfo['title'] . '[/url]';
158 if ($format == 'json') {
159 $ret['data'] = $siteinfo;
160 $ret['contentType'] = 'attachment';
161 $ret['success'] = true;
163 System::jsonExit($ret);
166 // Format it as BBCode attachment
167 $info = add_page_info_data($siteinfo);
175 * Legacy function to call ParseUrl::getSiteinfoCached
177 * Note: We have moved the function to ParseUrl.php. This function is only for
178 * legacy support and will be remove in the future
180 * @param string $url The url of the page which should be scraped
181 * @param bool $no_guessing If true the parse doens't search for
183 * @param bool $do_oembed The false option is used by the function fetch_oembed()
184 * to avoid endless loops
186 * @return array which contains needed data for embedding
188 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
189 * @see ParseUrl::getSiteinfoCached()
191 * @deprecated since version 3.6 use ParseUrl::getSiteinfoCached instead
193 function parseurl_getsiteinfo_cached($url, $no_guessing = false, $do_oembed = true)
195 $siteinfo = ParseUrl::getSiteinfoCached($url, $no_guessing, $do_oembed);