4 * @file mod/parse_url.php
5 * @brief The parse_url module
7 * This module does parse an url for embedable content (audio, video, image files or link)
8 * information and does format this information to BBCode or html (this depends
9 * on the user settings - default is BBCode output).
10 * If the user has enabled the richtext editor setting the output will be in html
11 * (Note: This is not always possible and in some case not useful because
12 * the richtext editor doesn't support all kind of html).
13 * Otherwise the output will be constructed BBCode.
15 * @see ParseUrl::getSiteinfo() for more information about scraping embeddable content
18 use \Friendica\ParseUrl;
20 require_once("include/items.php");
22 function parse_url_content(App &$a) {
29 if (local_user() && (!feature_enabled(local_user(), "richtext"))) {
33 $br = (($textmode) ? "\n" : "<br />");
35 if (x($_GET,"binurl")) {
36 $url = trim(hex2bin($_GET["binurl"]));
38 $url = trim($_GET["url"]);
42 $title = strip_tags(trim($_GET["title"]));
45 if ($_GET["description"]) {
46 $text = strip_tags(trim($_GET["description"]));
50 $arr_tags = ParseUrl::convertTagsToArray($_GET["tags"]);
51 if (count($arr_tags)) {
52 $str_tags = $br . implode(" ", $arr_tags) . $br;
56 // Add url scheme if it is missing
57 $arrurl = parse_url($url);
58 if (!x($arrurl, "scheme")) {
59 if (x($arrurl, "host")) {
62 $url = "http://".$url;
66 logger("prse_url: " . $url);
68 // Check if the URL is an image, video or audio file. If so format
69 // the URL with the corresponding BBCode media tag
71 // Fetch the header of the URL
72 $result = z_fetch_url($url, false, $redirects, array("novalidate" => true, "nobody" => true));
73 if($result["success"]) {
74 // Convert the header fields into an array
76 $h = explode("\n", $result["header"]);
78 list($k,$v) = array_map("trim", explode(":", trim($l), 2));
81 if (array_key_exists("Content-Type", $hdrs)) {
82 $type = $hdrs["Content-Type"];
85 if(stripos($type, "image/") !== false) {
86 echo $br . "[img]" . $url . "[/img]" . $br;
89 if (stripos($type, "video/") !== false) {
90 echo $br . "[video]" . $url . "[/video]" . $br;
93 if (stripos($type, "audio/") !== false) {
94 echo $br . "[audio]" . $url . "[/audio]" . $br;
101 $template = "[bookmark=%s]%s[/bookmark]%s";
103 $template = "<a class=\"bookmark\" href=\"%s\" >%s</a>%s";
106 $arr = array("url" => $url, "text" => "");
108 call_hooks("parse_link", $arr);
110 if (strlen($arr["text"])) {
115 // If there is allready some content information submitted we don't
116 // need to parse the url for content.
117 if ($url && $title && $text) {
119 $title = str_replace(array("\r","\n"),array("",""),$title);
122 $text = "[quote]" . trim($text) . "[/quote]" . $br;
124 $text = "<blockquote>" . htmlspecialchars(trim($text)) . "</blockquote><br />";
125 $title = htmlspecialchars($title);
128 $result = sprintf($template, $url, ($title) ? $title : $url, $text) . $str_tags;
130 logger("parse_url (unparsed): returns: " . $result);
136 // Fetch the information directly from the webpage
137 $siteinfo = ParseUrl::getSiteinfo($url);
139 unset($siteinfo["keywords"]);
141 // Format it as BBCode attachment
142 $info = add_page_info_data($siteinfo);
145 // Replace ' with ’ - not perfect - but the richtext editor has problems otherwise
146 $info = str_replace(array("'"), array("’"), $info);
155 * @brief Legacy function to call ParseUrl::getSiteinfoCached
157 * Note: We have moved the function to ParseUrl.php. This function is only for
158 * legacy support and will be remove in the future
160 * @param type $url The url of the page which should be scraped
161 * @param type $no_guessing If true the parse doens't search for
163 * @param type $do_oembed The false option is used by the function fetch_oembed()
164 * to avoid endless loops
166 * @return array which contains needed data for embedding
168 * @see ParseUrl::getSiteinfoCached()
170 * @todo Remove this function after all Addons has been changed to use
171 * ParseUrl::getSiteinfoCached
173 function parseurl_getsiteinfo_cached($url, $no_guessing = false, $do_oembed = true) {
174 $siteinfo = ParseUrl::getSiteinfoCached($url, $no_guessing, $do_oembed);