]> git.mxchange.org Git - friendica.git/blob - mod/parse_url.php
0a9b096cb40515d746f3cec7df2181ff48132d90
[friendica.git] / mod / parse_url.php
1 <?php
2 /** 
3  * @file mod/parse_url.php
4  * @brief The parse_url module
5  * 
6  * This module does parse an url for embedable content (audio, video, image files or link)
7  * information and does format this information to BBCode or html (this depends
8  * on the user settings - default is BBCode output).
9  * If the user has enabled the richtext editor setting the output will be in html
10  * (Note: This is not always possible and in some case not useful because
11  * the richtext editor doesn't support all kind of html).
12  * Otherwise the output will be constructed BBCode.
13  * 
14  * @todo https://developers.google.com/+/plugins/snippet/
15  * 
16  * @verbatim
17  * <meta itemprop="name" content="Toller Titel">
18  * <meta itemprop="description" content="Eine tolle Beschreibung">
19  * <meta itemprop="image" content="http://maple.libertreeproject.org/images/tree-icon.png">
20  * 
21  * <body itemscope itemtype="http://schema.org/Product">
22  *   <h1 itemprop="name">Shiny Trinket</h1>
23  *   <img itemprop="image" src="{image-url}" />
24  *   <p itemprop="description">Shiny trinkets are shiny.</p>
25  * </body>
26  * @endverbatim
27 */
28
29 use \Friendica\ParseUrl;
30
31 require_once("include/items.php");
32
33 function parse_url_content(&$a) {
34
35         $text = null;
36         $str_tags = "";
37
38         $textmode = false;
39
40         if (local_user() && (!feature_enabled(local_user(), "richtext"))) {
41                 $textmode = true;
42         }
43
44         $br = (($textmode) ? "\n" : "<br />");
45
46         if (x($_GET,"binurl")) {
47                 $url = trim(hex2bin($_GET["binurl"]));
48         } else {
49                 $url = trim($_GET["url"]);
50         }
51
52         if ($_GET["title"]) {
53                 $title = strip_tags(trim($_GET["title"]));
54         }
55
56         if ($_GET["description"]) {
57                 $text = strip_tags(trim($_GET["description"]));
58         }
59
60         if ($_GET["tags"]) {
61                 $arr_tags = ParseUrl::convertTagsToArray($_GET["tags"]);
62                 if (count($arr_tags)) {
63                         $str_tags = $br . implode(" ", $arr_tags) . $br;
64                 }
65         }
66
67         // Add url scheme if it is missing
68         $arrurl = parse_url($url);
69         if (!x($arrurl, "scheme")) {
70                 if (x($arrurl, "host")) {
71                         $url = "http:".$url;
72                 } else {
73                         $url = "http://".$url;
74                 }
75         }
76
77         logger("prse_url: " . $url);
78
79         // Check if the URL is an image, video or audio file. If so format
80         // the URL with the corresponding BBCode media tag
81         $redirects = 0;
82         // Fetch the header of the URL
83         $result = z_fetch_url($url, false, $redirects, array("novalidate" => true, "nobody" => true));
84         if($result["success"]) {
85                 // Convert the header fields into an array
86                 $hdrs = array();
87                 $h = explode("\n", $result["header"]);
88                 foreach ($h as $l) {
89                         list($k,$v) = array_map("trim", explode(":", trim($l), 2));
90                         $hdrs[$k] = $v;
91                 }
92                 if (array_key_exists("Content-Type", $hdrs)) {
93                         $type = $hdrs["Content-Type"];
94                 }
95                 if ($type) {
96                         if(stripos($type, "image/") !== false) {
97                                 echo $br . "[img]" . $url . "[/img]" . $br;
98                                 killme();
99                         }
100                         if (stripos($type, "video/") !== false) {
101                                 echo $br . "[video]" . $url . "[/video]" . $br;
102                                 killme();
103                         }
104                         if (stripos($type, "audio/") !== false) {
105                                 echo $br . "[audio]" . $url . "[/audio]" . $br;
106                                 killme();
107                         }
108                 }
109         }
110
111         if ($textmode) {
112                 $template = "[bookmark=%s]%s[/bookmark]%s";
113         } else {
114                 $template = "<a class=\"bookmark\" href=\"%s\" >%s</a>%s";
115         }
116
117         $arr = array("url" => $url, "text" => "");
118
119         call_hooks("parse_link", $arr);
120
121         if (strlen($arr["text"])) {
122                 echo $arr["text"];
123                 killme();
124         }
125
126         // If there is allready some content information submitted we don't
127         // need to parse the url for content.
128         if ($url && $title && $text) {
129
130                 $title = str_replace(array("\r","\n"),array("",""),$title);
131
132                 if ($textmode) {
133                         $text = "[quote]" . trim($text) . "[/quote]" . $br;
134                 } else {
135                         $text = "<blockquote>" . htmlspecialchars(trim($text)) . "</blockquote><br />";
136                         $title = htmlspecialchars($title);
137                 }
138
139                 $result = sprintf($template, $url, ($title) ? $title : $url, $text) . $str_tags;
140
141                 logger("parse_url (unparsed): returns: " . $result);
142
143                 echo $result;
144                 killme();
145         }
146
147         // Fetch the information directly from the webpage
148         $siteinfo = ParseUrl::getSiteinfo($url);
149
150         unset($siteinfo["keywords"]);
151
152         // Format it as BBCode attachment
153         $info = add_page_info_data($siteinfo);
154
155         if (!$textmode) {
156                 // Replace ' with ’ - not perfect - but the richtext editor has problems otherwise
157                 $info = str_replace(array("&#039;"), array("&#8217;"), $info);
158         }
159
160         echo $info;
161
162         killme();
163 }