]> git.mxchange.org Git - friendica.git/blob - mod/parse_url.php
Merge pull request #3047 from annando/1612-indexlength
[friendica.git] / mod / parse_url.php
1 <?php
2
3 /** 
4  * @file mod/parse_url.php
5  * @brief The parse_url module
6  * 
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.
14  * 
15  * @see ParseUrl::getSiteinfo() for more information about scraping embeddable content 
16 */
17
18 use \Friendica\ParseUrl;
19
20 require_once("include/items.php");
21
22 function parse_url_content(&$a) {
23
24         $text = null;
25         $str_tags = "";
26
27         $textmode = false;
28
29         if (local_user() && (!feature_enabled(local_user(), "richtext"))) {
30                 $textmode = true;
31         }
32
33         $br = (($textmode) ? "\n" : "<br />");
34
35         if (x($_GET,"binurl")) {
36                 $url = trim(hex2bin($_GET["binurl"]));
37         } else {
38                 $url = trim($_GET["url"]);
39         }
40
41         if ($_GET["title"]) {
42                 $title = strip_tags(trim($_GET["title"]));
43         }
44
45         if ($_GET["description"]) {
46                 $text = strip_tags(trim($_GET["description"]));
47         }
48
49         if ($_GET["tags"]) {
50                 $arr_tags = ParseUrl::convertTagsToArray($_GET["tags"]);
51                 if (count($arr_tags)) {
52                         $str_tags = $br . implode(" ", $arr_tags) . $br;
53                 }
54         }
55
56         // Add url scheme if it is missing
57         $arrurl = parse_url($url);
58         if (!x($arrurl, "scheme")) {
59                 if (x($arrurl, "host")) {
60                         $url = "http:".$url;
61                 } else {
62                         $url = "http://".$url;
63                 }
64         }
65
66         logger("prse_url: " . $url);
67
68         // Check if the URL is an image, video or audio file. If so format
69         // the URL with the corresponding BBCode media tag
70         $redirects = 0;
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
75                 $hdrs = array();
76                 $h = explode("\n", $result["header"]);
77                 foreach ($h as $l) {
78                         list($k,$v) = array_map("trim", explode(":", trim($l), 2));
79                         $hdrs[$k] = $v;
80                 }
81                 if (array_key_exists("Content-Type", $hdrs)) {
82                         $type = $hdrs["Content-Type"];
83                 }
84                 if ($type) {
85                         if(stripos($type, "image/") !== false) {
86                                 echo $br . "[img]" . $url . "[/img]" . $br;
87                                 killme();
88                         }
89                         if (stripos($type, "video/") !== false) {
90                                 echo $br . "[video]" . $url . "[/video]" . $br;
91                                 killme();
92                         }
93                         if (stripos($type, "audio/") !== false) {
94                                 echo $br . "[audio]" . $url . "[/audio]" . $br;
95                                 killme();
96                         }
97                 }
98         }
99
100         if ($textmode) {
101                 $template = "[bookmark=%s]%s[/bookmark]%s";
102         } else {
103                 $template = "<a class=\"bookmark\" href=\"%s\" >%s</a>%s";
104         }
105
106         $arr = array("url" => $url, "text" => "");
107
108         call_hooks("parse_link", $arr);
109
110         if (strlen($arr["text"])) {
111                 echo $arr["text"];
112                 killme();
113         }
114
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) {
118
119                 $title = str_replace(array("\r","\n"),array("",""),$title);
120
121                 if ($textmode) {
122                         $text = "[quote]" . trim($text) . "[/quote]" . $br;
123                 } else {
124                         $text = "<blockquote>" . htmlspecialchars(trim($text)) . "</blockquote><br />";
125                         $title = htmlspecialchars($title);
126                 }
127
128                 $result = sprintf($template, $url, ($title) ? $title : $url, $text) . $str_tags;
129
130                 logger("parse_url (unparsed): returns: " . $result);
131
132                 echo $result;
133                 killme();
134         }
135
136         // Fetch the information directly from the webpage
137         $siteinfo = ParseUrl::getSiteinfo($url);
138
139         unset($siteinfo["keywords"]);
140
141         // Format it as BBCode attachment
142         $info = add_page_info_data($siteinfo);
143
144         if (!$textmode) {
145                 // Replace ' with ’ - not perfect - but the richtext editor has problems otherwise
146                 $info = str_replace(array("&#039;"), array("&#8217;"), $info);
147         }
148
149         echo $info;
150
151         killme();
152 }
153
154 /**
155  * @brief Legacy function to call ParseUrl::getSiteinfoCached
156  * 
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
159  * 
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
162  *    preview pictures
163  * @param type $do_oembed The false option is used by the function fetch_oembed()
164  *    to avoid endless loops
165  * 
166  * @return array which contains needed data for embedding
167  * 
168  * @see ParseUrl::getSiteinfoCached()
169  * 
170  * @todo Remove this function after all Addons has been changed to use
171  *    ParseUrl::getSiteinfoCached
172  */
173 function parseurl_getsiteinfo_cached($url, $no_guessing = false, $do_oembed = true) {
174         $siteinfo = ParseUrl::getSiteinfoCached($url, $no_guessing, $do_oembed);
175         return $siteinfo;
176 }