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 require_once 'include/items.php';
20 function parse_url_content(App $a)
27 if (!empty($_GET['binurl'])) {
28 $url = trim(hex2bin($_GET['binurl']));
30 $url = trim($_GET['url']);
33 if (!empty($_GET['title'])) {
34 $title = strip_tags(trim($_GET['title']));
37 if (!empty($_GET['description'])) {
38 $text = strip_tags(trim($_GET['description']));
41 if (!empty($_GET['tags'])) {
42 $arr_tags = ParseUrl::convertTagsToArray($_GET['tags']);
43 if (count($arr_tags)) {
44 $str_tags = $br . implode(' ', $arr_tags) . $br;
48 // Add url scheme if it is missing
49 $arrurl = parse_url($url);
50 if (!x($arrurl, 'scheme')) {
51 if (x($arrurl, 'host')) {
52 $url = 'http:' . $url;
54 $url = 'http://' . $url;
60 // Check if the URL is an image, video or audio file. If so format
61 // the URL with the corresponding BBCode media tag
63 // Fetch the header of the URL
64 $curlResponse = Network::curl($url, false, $redirects, ['novalidate' => true, 'nobody' => true]);
66 if ($curlResponse->isSuccess()) {
67 // Convert the header fields into an array
69 $h = explode("\n", $curlResponse->getHeader());
71 $header = array_map('trim', explode(':', trim($l), 2));
72 if (count($header) == 2) {
73 list($k, $v) = $header;
78 if (array_key_exists('Content-Type', $hdrs)) {
79 $type = $hdrs['Content-Type'];
82 if (stripos($type, 'image/') !== false) {
83 echo $br . '[img]' . $url . '[/img]' . $br;
86 if (stripos($type, 'video/') !== false) {
87 echo $br . '[video]' . $url . '[/video]' . $br;
90 if (stripos($type, 'audio/') !== false) {
91 echo $br . '[audio]' . $url . '[/audio]' . $br;
98 $template = '[bookmark=%s]%s[/bookmark]%s';
100 $arr = ['url' => $url, 'text' => ''];
102 Addon::callHooks('parse_link', $arr);
104 if (strlen($arr['text'])) {
109 // If there is already some content information submitted we don't
110 // need to parse the url for content.
111 if (!empty($url) && !empty($title) && !empty($text)) {
112 $title = str_replace(["\r", "\n"], ['', ''], $title);
114 $text = '[quote]' . trim($text) . '[/quote]' . $br;
116 $result = sprintf($template, $url, ($title) ? $title : $url, $text) . $str_tags;
118 Logger::log('(unparsed): returns: ' . $result);
124 // Fetch the information directly from the webpage
125 $siteinfo = ParseUrl::getSiteinfo($url);
127 unset($siteinfo['keywords']);
129 // Bypass attachment if parse url for a comment
130 if (!empty($_GET['noAttachment'])) {
131 echo $br . '[url=' . $url . ']' . $siteinfo['title'] . '[/url]';
135 // Format it as BBCode attachment
136 $info = add_page_info_data($siteinfo);
144 * @brief Legacy function to call ParseUrl::getSiteinfoCached
146 * Note: We have moved the function to ParseUrl.php. This function is only for
147 * legacy support and will be remove in the future
149 * @param type $url The url of the page which should be scraped
150 * @param type $no_guessing If true the parse doens't search for
152 * @param type $do_oembed The false option is used by the function fetch_oembed()
153 * to avoid endless loops
155 * @return array which contains needed data for embedding
157 * @see ParseUrl::getSiteinfoCached()
159 * @todo Remove this function after all Addons has been changed to use
160 * ParseUrl::getSiteinfoCached
162 function parseurl_getsiteinfo_cached($url, $no_guessing = false, $do_oembed = true)
164 $siteinfo = ParseUrl::getSiteinfoCached($url, $no_guessing, $do_oembed);