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\ParseUrl;
17 require_once("include/items.php");
19 function parse_url_content(App $a) {
26 if (x($_GET,"binurl")) {
27 $url = trim(hex2bin($_GET["binurl"]));
29 $url = trim($_GET["url"]);
33 $title = strip_tags(trim($_GET["title"]));
36 if ($_GET["description"]) {
37 $text = strip_tags(trim($_GET["description"]));
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")) {
53 $url = "http://".$url;
57 logger("prse_url: " . $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 = z_fetch_url($url, false, $redirects, ["novalidate" => true, "nobody" => true]);
64 if($result["success"]) {
65 // Convert the header fields into an array
67 $h = explode("\n", $result["header"]);
69 list($k,$v) = array_map("trim", explode(":", trim($l), 2));
72 if (array_key_exists("Content-Type", $hdrs)) {
73 $type = $hdrs["Content-Type"];
76 if(stripos($type, "image/") !== false) {
77 echo $br . "[img]" . $url . "[/img]" . $br;
80 if (stripos($type, "video/") !== false) {
81 echo $br . "[video]" . $url . "[/video]" . $br;
84 if (stripos($type, "audio/") !== false) {
85 echo $br . "[audio]" . $url . "[/audio]" . $br;
91 $template = "[bookmark=%s]%s[/bookmark]%s";
93 $arr = ["url" => $url, "text" => ""];
95 Addon::callHooks("parse_link", $arr);
97 if (strlen($arr["text"])) {
102 // If there is allready some content information submitted we don't
103 // need to parse the url for content.
104 if ($url && $title && $text) {
106 $title = str_replace(["\r","\n"],["",""],$title);
108 $text = "[quote]" . trim($text) . "[/quote]" . $br;
110 $result = sprintf($template, $url, ($title) ? $title : $url, $text) . $str_tags;
112 logger("parse_url (unparsed): returns: " . $result);
118 // Fetch the information directly from the webpage
119 $siteinfo = ParseUrl::getSiteinfo($url);
121 unset($siteinfo["keywords"]);
123 // Format it as BBCode attachment
124 $info = add_page_info_data($siteinfo);
132 * @brief Legacy function to call ParseUrl::getSiteinfoCached
134 * Note: We have moved the function to ParseUrl.php. This function is only for
135 * legacy support and will be remove in the future
137 * @param type $url The url of the page which should be scraped
138 * @param type $no_guessing If true the parse doens't search for
140 * @param type $do_oembed The false option is used by the function fetch_oembed()
141 * to avoid endless loops
143 * @return array which contains needed data for embedding
145 * @see ParseUrl::getSiteinfoCached()
147 * @todo Remove this function after all Addons has been changed to use
148 * ParseUrl::getSiteinfoCached
150 function parseurl_getsiteinfo_cached($url, $no_guessing = false, $do_oembed = true) {
151 $siteinfo = ParseUrl::getSiteinfoCached($url, $no_guessing, $do_oembed);