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\Util\Network;
15 use Friendica\Util\ParseUrl;
17 require_once 'include/items.php';
19 function parse_url_content(App $a)
26 if (!empty($_GET['binurl'])) {
27 $url = trim(hex2bin($_GET['binurl']));
29 $url = trim($_GET['url']);
32 if (!empty($_GET['title'])) {
33 $title = strip_tags(trim($_GET['title']));
36 if (!empty($_GET['description'])) {
37 $text = strip_tags(trim($_GET['description']));
40 if (!empty($_GET['tags'])) {
41 $arr_tags = ParseUrl::convertTagsToArray($_GET['tags']);
42 if (count($arr_tags)) {
43 $str_tags = $br . implode(' ', $arr_tags) . $br;
47 // Add url scheme if it is missing
48 $arrurl = parse_url($url);
49 if (!x($arrurl, 'scheme')) {
50 if (x($arrurl, 'host')) {
51 $url = 'http:' . $url;
53 $url = 'http://' . $url;
59 // Check if the URL is an image, video or audio file. If so format
60 // the URL with the corresponding BBCode media tag
62 // Fetch the header of the URL
63 $result = Network::curl($url, false, $redirects, ['novalidate' => true, 'nobody' => true]);
65 if ($result['success']) {
66 // Convert the header fields into an array
68 $h = explode("\n", $result['header']);
70 $header = array_map('trim', explode(':', trim($l), 2));
71 if (count($header) == 2) {
72 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;
95 $template = '[bookmark=%s]%s[/bookmark]%s';
97 $arr = ['url' => $url, 'text' => ''];
99 Addon::callHooks('parse_link', $arr);
101 if (strlen($arr['text'])) {
106 // If there is already some content information submitted we don't
107 // need to parse the url for content.
108 if (!empty($url) && !empty($title) && !empty($text)) {
109 $title = str_replace(["\r", "\n"], ['', ''], $title);
111 $text = '[quote]' . trim($text) . '[/quote]' . $br;
113 $result = sprintf($template, $url, ($title) ? $title : $url, $text) . $str_tags;
115 logger('(unparsed): returns: ' . $result);
121 // Fetch the information directly from the webpage
122 $siteinfo = ParseUrl::getSiteinfo($url);
124 unset($siteinfo['keywords']);
126 // Format it as BBCode attachment
127 $info = add_page_info_data($siteinfo);
135 * @brief Legacy function to call ParseUrl::getSiteinfoCached
137 * Note: We have moved the function to ParseUrl.php. This function is only for
138 * legacy support and will be remove in the future
140 * @param type $url The url of the page which should be scraped
141 * @param type $no_guessing If true the parse doens't search for
143 * @param type $do_oembed The false option is used by the function fetch_oembed()
144 * to avoid endless loops
146 * @return array which contains needed data for embedding
148 * @see ParseUrl::getSiteinfoCached()
150 * @todo Remove this function after all Addons has been changed to use
151 * ParseUrl::getSiteinfoCached
153 function parseurl_getsiteinfo_cached($url, $no_guessing = false, $do_oembed = true)
155 $siteinfo = ParseUrl::getSiteinfoCached($url, $no_guessing, $do_oembed);