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
14 use Friendica\Core\Addon;
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 (x($_GET,"binurl")) {
28 $url = trim(hex2bin($_GET["binurl"]));
30 $url = trim($_GET["url"]);
34 $title = strip_tags(trim($_GET["title"]));
37 if ($_GET["description"]) {
38 $text = strip_tags(trim($_GET["description"]));
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")) {
54 $url = "http://".$url;
58 logger("prse_url: " . $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 $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 list($k,$v) = array_map("trim", explode(":", trim($l), 2));
73 if (array_key_exists("Content-Type", $hdrs)) {
74 $type = $hdrs["Content-Type"];
77 if(stripos($type, "image/") !== false) {
78 echo $br . "[img]" . $url . "[/img]" . $br;
81 if (stripos($type, "video/") !== false) {
82 echo $br . "[video]" . $url . "[/video]" . $br;
85 if (stripos($type, "audio/") !== false) {
86 echo $br . "[audio]" . $url . "[/audio]" . $br;
92 $template = "[bookmark=%s]%s[/bookmark]%s";
94 $arr = ["url" => $url, "text" => ""];
96 Addon::callHooks("parse_link", $arr);
98 if (strlen($arr["text"])) {
103 // If there is allready some content information submitted we don't
104 // need to parse the url for content.
105 if ($url && $title && $text) {
107 $title = str_replace(["\r","\n"],["",""],$title);
109 $text = "[quote]" . trim($text) . "[/quote]" . $br;
111 $result = sprintf($template, $url, ($title) ? $title : $url, $text) . $str_tags;
113 logger("parse_url (unparsed): returns: " . $result);
119 // Fetch the information directly from the webpage
120 $siteinfo = ParseUrl::getSiteinfo($url);
122 unset($siteinfo["keywords"]);
124 // Format it as BBCode attachment
125 $info = add_page_info_data($siteinfo);
133 * @brief Legacy function to call ParseUrl::getSiteinfoCached
135 * Note: We have moved the function to ParseUrl.php. This function is only for
136 * legacy support and will be remove in the future
138 * @param type $url The url of the page which should be scraped
139 * @param type $no_guessing If true the parse doens't search for
141 * @param type $do_oembed The false option is used by the function fetch_oembed()
142 * to avoid endless loops
144 * @return array which contains needed data for embedding
146 * @see ParseUrl::getSiteinfoCached()
148 * @todo Remove this function after all Addons has been changed to use
149 * ParseUrl::getSiteinfoCached
151 function parseurl_getsiteinfo_cached($url, $no_guessing = false, $do_oembed = true) {
152 $siteinfo = ParseUrl::getSiteinfoCached($url, $no_guessing, $do_oembed);