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\Addon;
14 use Friendica\Core\Logger;
15 use Friendica\Util\Network;
16 use Friendica\Util\ParseUrl;
18 function parse_url_content(App $a)
25 if (!empty($_GET['binurl'])) {
26 $url = trim(hex2bin($_GET['binurl']));
28 $url = trim($_GET['url']);
31 if (!empty($_GET['title'])) {
32 $title = strip_tags(trim($_GET['title']));
35 if (!empty($_GET['description'])) {
36 $text = strip_tags(trim($_GET['description']));
39 if (!empty($_GET['tags'])) {
40 $arr_tags = ParseUrl::convertTagsToArray($_GET['tags']);
41 if (count($arr_tags)) {
42 $str_tags = $br . implode(' ', $arr_tags) . $br;
46 // Add url scheme if it is missing
47 $arrurl = parse_url($url);
48 if (empty($arrurl['scheme'])) {
49 if (!empty($arrurl['host'])) {
50 $url = 'http:' . $url;
52 $url = 'http://' . $url;
58 // Check if the URL is an image, video or audio file. If so format
59 // the URL with the corresponding BBCode media tag
61 // Fetch the header of the URL
62 $curlResponse = Network::curl($url, false, $redirects, ['novalidate' => true, 'nobody' => true]);
64 if ($curlResponse->isSuccess()) {
65 // Convert the header fields into an array
67 $h = explode("\n", $curlResponse->getHeader());
69 $header = array_map('trim', explode(':', trim($l), 2));
70 if (count($header) == 2) {
71 list($k, $v) = $header;
76 if (array_key_exists('Content-Type', $hdrs)) {
77 $type = $hdrs['Content-Type'];
80 if (stripos($type, 'image/') !== false) {
81 echo $br . '[img]' . $url . '[/img]' . $br;
84 if (stripos($type, 'video/') !== false) {
85 echo $br . '[video]' . $url . '[/video]' . $br;
88 if (stripos($type, 'audio/') !== false) {
89 echo $br . '[audio]' . $url . '[/audio]' . $br;
96 $template = '[bookmark=%s]%s[/bookmark]%s';
98 $arr = ['url' => $url, 'text' => ''];
100 Addon::callHooks('parse_link', $arr);
102 if (strlen($arr['text'])) {
107 // If there is already some content information submitted we don't
108 // need to parse the url for content.
109 if (!empty($url) && !empty($title) && !empty($text)) {
110 $title = str_replace(["\r", "\n"], ['', ''], $title);
112 $text = '[quote]' . trim($text) . '[/quote]' . $br;
114 $result = sprintf($template, $url, ($title) ? $title : $url, $text) . $str_tags;
116 Logger::log('(unparsed): returns: ' . $result);
122 // Fetch the information directly from the webpage
123 $siteinfo = ParseUrl::getSiteinfo($url);
125 unset($siteinfo['keywords']);
127 // Bypass attachment if parse url for a comment
128 if (!empty($_GET['noAttachment'])) {
129 echo $br . '[url=' . $url . ']' . $siteinfo['title'] . '[/url]';
133 // Format it as BBCode attachment
134 $info = add_page_info_data($siteinfo);
142 * @brief Legacy function to call ParseUrl::getSiteinfoCached
144 * Note: We have moved the function to ParseUrl.php. This function is only for
145 * legacy support and will be remove in the future
147 * @param type $url The url of the page which should be scraped
148 * @param type $no_guessing If true the parse doens't search for
150 * @param type $do_oembed The false option is used by the function fetch_oembed()
151 * to avoid endless loops
153 * @return array which contains needed data for embedding
155 * @see ParseUrl::getSiteinfoCached()
157 * @todo Remove this function after all Addons has been changed to use
158 * ParseUrl::getSiteinfoCached
160 function parseurl_getsiteinfo_cached($url, $no_guessing = false, $do_oembed = true)
162 $siteinfo = ParseUrl::getSiteinfoCached($url, $no_guessing, $do_oembed);