4 * @file mod/parse_url.php
5 * @brief The parse_url module
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
13 use Friendica\Core\Hook;
14 use Friendica\Core\Logger;
15 use Friendica\Core\System;
16 use Friendica\Util\Network;
17 use Friendica\Util\ParseUrl;
19 function parse_url_content(App $a)
24 $ret= ['success' => false, 'contentType' => ''];
28 if (!empty($_GET['binurl'])) {
29 $url = trim(hex2bin($_GET['binurl']));
31 $url = trim($_GET['url']);
34 if (!empty($_GET['title'])) {
35 $title = strip_tags(trim($_GET['title']));
38 if (!empty($_GET['description'])) {
39 $text = strip_tags(trim($_GET['description']));
42 if (!empty($_GET['tags'])) {
43 $arr_tags = ParseUrl::convertTagsToArray($_GET['tags']);
44 if (count($arr_tags)) {
45 $str_tags = $br . implode(' ', $arr_tags) . $br;
49 if (isset($_GET['format']) && $_GET['format'] == 'json') {
53 // Add url scheme if it is missing
54 $arrurl = parse_url($url);
55 if (empty($arrurl['scheme'])) {
56 if (!empty($arrurl['host'])) {
57 $url = 'http:' . $url;
59 $url = 'http://' . $url;
65 // Check if the URL is an image, video or audio file. If so format
66 // the URL with the corresponding BBCode media tag
68 // Fetch the header of the URL
69 $curlResponse = Network::curl($url, false, $redirects, ['novalidate' => true, 'nobody' => true]);
71 if ($curlResponse->isSuccess()) {
72 // Convert the header fields into an array
74 $h = explode("\n", $curlResponse->getHeader());
76 $header = array_map('trim', explode(':', trim($l), 2));
77 if (count($header) == 2) {
78 list($k, $v) = $header;
85 if (array_key_exists('Content-Type', $hdrs)) {
86 $type = $hdrs['Content-Type'];
89 if (stripos($type, 'image/') !== false) {
90 $content_type = 'image';
91 $bbcode = $br . '[img]' . $url . '[/img]' . $br;
93 if (stripos($type, 'video/') !== false) {
94 $content_type = 'video';
95 $bbcode = $br . '[video]' . $url . '[/video]' . $br;
97 if (stripos($type, 'audio/') !== false) {
98 $content_type = 'audio';
99 $bbcode = $br . '[audio]' . $url . '[/audio]' . $br;
102 if (!empty($content_type)) {
103 if ($format == 'json') {
104 $ret['contentType'] = $content_type;
105 $ret['data'] = ['url' => $url];
106 $ret['success'] = true;
107 System::jsonExit($ret);
116 $template = '[bookmark=%s]%s[/bookmark]%s';
118 $arr = ['url' => $url, 'text' => ''];
120 Hook::callAll('parse_link', $arr);
122 if (strlen($arr['text'])) {
127 // If there is already some content information submitted we don't
128 // need to parse the url for content.
129 if (!empty($url) && !empty($title) && !empty($text)) {
130 $title = str_replace(["\r", "\n"], ['', ''], $title);
132 $text = '[quote]' . trim($text) . '[/quote]' . $br;
134 $result = sprintf($template, $url, ($title) ? $title : $url, $text) . $str_tags;
136 Logger::log('(unparsed): returns: ' . $result);
142 // Fetch the information directly from the webpage
143 $siteinfo = ParseUrl::getSiteinfo($url);
145 unset($siteinfo['keywords']);
147 // Bypass attachment if parse url for a comment
148 if (!empty($_GET['noAttachment'])) {
149 echo $br . '[url=' . $url . ']' . $siteinfo['title'] . '[/url]';
153 if ($format == 'json') {
154 $ret['data'] = $siteinfo;
155 $ret['contentType'] = 'attachment';
156 $ret['success'] = true;
158 System::jsonExit($ret);
161 // Format it as BBCode attachment
162 $info = add_page_info_data($siteinfo);
170 * @brief Legacy function to call ParseUrl::getSiteinfoCached
172 * Note: We have moved the function to ParseUrl.php. This function is only for
173 * legacy support and will be remove in the future
175 * @param string $url The url of the page which should be scraped
176 * @param bool $no_guessing If true the parse doens't search for
178 * @param bool $do_oembed The false option is used by the function fetch_oembed()
179 * to avoid endless loops
181 * @return array which contains needed data for embedding
183 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
184 * @see ParseUrl::getSiteinfoCached()
186 * @deprecated since version 3.6 use ParseUrl::getSiteinfoCached instead
188 function parseurl_getsiteinfo_cached($url, $no_guessing = false, $do_oembed = true)
190 $siteinfo = ParseUrl::getSiteinfoCached($url, $no_guessing, $do_oembed);