]> git.mxchange.org Git - friendica.git/blob - mod/parse_url.php
* Add MIME types for file attachments
[friendica.git] / mod / parse_url.php
1 <?php
2 /* To-Do
3 https://developers.google.com/+/plugins/snippet/
4
5 <meta itemprop="name" content="Toller Titel">
6 <meta itemprop="description" content="Eine tolle Beschreibung">
7 <meta itemprop="image" content="http://maple.libertreeproject.org/images/tree-icon.png">
8
9 <body itemscope itemtype="http://schema.org/Product">
10   <h1 itemprop="name">Shiny Trinket</h1>
11   <img itemprop="image" src="{image-url}" />
12   <p itemprop="description">Shiny trinkets are shiny.</p>
13 </body>
14 */
15
16 if(!function_exists('deletenode')) {
17         function deletenode(&$doc, $node)
18         {
19                 $xpath = new DomXPath($doc);
20                 $list = $xpath->query("//".$node);
21                 foreach ($list as $child)
22                         $child->parentNode->removeChild($child);
23         }
24 }
25
26 function completeurl($url, $scheme) {
27         $urlarr = parse_url($url);
28
29         if (isset($urlarr["scheme"]))
30                 return($url);
31
32         $schemearr = parse_url($scheme);
33
34         $complete = $schemearr["scheme"]."://".$schemearr["host"];
35
36         if ($schemearr["port"] != "")
37                 $complete .= ":".$schemearr["port"];
38
39                 if(strpos($urlarr['path'],'/') !== 0)
40                         $complete .= '/';
41
42         $complete .= $urlarr["path"];
43
44         if ($urlarr["query"] != "")
45                 $complete .= "?".$urlarr["query"];
46
47         if ($urlarr["fragment"] != "")
48                 $complete .= "#".$urlarr["fragment"];
49
50         return($complete);
51 }
52
53 function parseurl_getsiteinfo($url) {
54         $siteinfo = array();
55
56         $ch = curl_init();
57         curl_setopt($ch, CURLOPT_URL, $url);
58         curl_setopt($ch, CURLOPT_HEADER, 1);
59         curl_setopt($ch, CURLOPT_NOBODY, 0);
60         curl_setopt($ch, CURLOPT_TIMEOUT, 3);
61         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
62         curl_setopt($ch,CURLOPT_USERAGENT,'Opera/9.64(Windows NT 5.1; U; de) Presto/2.1.1');
63
64         $header = curl_exec($ch);
65         curl_close($ch);
66
67         // Fetch the first mentioned charset. Can be in body or header
68         if (preg_match('/charset=(.*?)['."'".'"\s\n]/', $header, $matches))
69                 $charset = trim(array_pop($matches));
70         else
71                 $charset = "utf-8";
72
73         $pos = strpos($header, "\r\n\r\n");
74
75         if ($pos)
76                 $body = trim(substr($header, $pos));
77         else
78                 $body = $header;
79
80         $body = mb_convert_encoding($body, "UTF-8", $charset);
81         $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
82
83         $doc = new DOMDocument();
84         @$doc->loadHTML($body);
85
86         deletenode($doc, 'style');
87         deletenode($doc, 'script');
88         deletenode($doc, 'option');
89         deletenode($doc, 'h1');
90         deletenode($doc, 'h2');
91         deletenode($doc, 'h3');
92         deletenode($doc, 'h4');
93         deletenode($doc, 'h5');
94         deletenode($doc, 'h6');
95         deletenode($doc, 'ol');
96         deletenode($doc, 'ul');
97
98         $xpath = new DomXPath($doc);
99
100         //$list = $xpath->query("head/title");
101         $list = $xpath->query("//title");
102         foreach ($list as $node)
103                 $siteinfo["title"] =  html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8");
104
105         //$list = $xpath->query("head/meta[@name]");
106         $list = $xpath->query("//meta[@name]");
107         foreach ($list as $node) {
108                 $attr = array();
109                 if ($node->attributes->length)
110                         foreach ($node->attributes as $attribute)
111                                 $attr[$attribute->name] = $attribute->value;
112
113                 $attr["content"] = html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8");
114
115                 switch (strtolower($attr["name"])) {
116                         case "fulltitle":
117                                 $siteinfo["title"] = $attr["content"];
118                                 break;
119                         case "description":
120                                 $siteinfo["text"] = $attr["content"];
121                                 break;
122                         case "dc.title":
123                                 $siteinfo["title"] = $attr["content"];
124                                 break;
125                         case "dc.description":
126                                 $siteinfo["text"] = $attr["content"];
127                                 break;
128                 }
129         }
130
131         //$list = $xpath->query("head/meta[@property]");
132         $list = $xpath->query("//meta[@property]");
133         foreach ($list as $node) {
134                 $attr = array();
135                 if ($node->attributes->length)
136                         foreach ($node->attributes as $attribute)
137                                 $attr[$attribute->name] = $attribute->value;
138
139                 $attr["content"] = html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8");
140
141                 switch (strtolower($attr["property"])) {
142                         case "og:image":
143                                 $siteinfo["image"] = $attr["content"];
144                                 break;
145                         case "og:title":
146                                 $siteinfo["title"] = $attr["content"];
147                                 break;
148                         case "og:description":
149                                 $siteinfo["text"] = $attr["content"];
150                                 break;
151                 }
152         }
153
154         if ($siteinfo["image"] == "") {
155             $list = $xpath->query("//img[@src]");
156             foreach ($list as $node) {
157                 $attr = array();
158                 if ($node->attributes->length)
159                     foreach ($node->attributes as $attribute)
160                         $attr[$attribute->name] = $attribute->value;
161
162                         $src = completeurl($attr["src"], $url);
163                         $photodata = @getimagesize($src);
164
165                         if (($photodata) && ($photodata[0] > 150) and ($photodata[1] > 150)) {
166                                 if ($photodata[0] > 300) {
167                                         $photodata[1] = round($photodata[1] * (300 / $photodata[0]));
168                                         $photodata[0] = 300;
169                                 }
170                                 if ($photodata[1] > 300) {
171                                         $photodata[0] = round($photodata[0] * (300 / $photodata[1]));
172                                         $photodata[1] = 300;
173                                 }
174                                 $siteinfo["images"][] = array("src"=>$src,
175                                                                 "width"=>$photodata[0],
176                                                                 "height"=>$photodata[1]);
177                         }
178
179                 }
180     } else {
181                 $src = completeurl($siteinfo["image"], $url);
182
183                 unset($siteinfo["image"]);
184
185                 $photodata = @getimagesize($src);
186
187                 if (($photodata) && ($photodata[0] > 10) and ($photodata[1] > 10))
188                         $siteinfo["images"][] = array("src"=>$src,
189                                                         "width"=>$photodata[0],
190                                                         "height"=>$photodata[1]);
191         }
192
193         if ($siteinfo["text"] == "") {
194                 $text = "";
195
196                 $list = $xpath->query("//div[@class='article']");
197                 foreach ($list as $node)
198                         if (strlen($node->nodeValue) > 40)
199                                 $text .= " ".trim($node->nodeValue);
200
201                 if ($text == "") {
202                         $list = $xpath->query("//div[@class='content']");
203                         foreach ($list as $node)
204                                 if (strlen($node->nodeValue) > 40)
205                                         $text .= " ".trim($node->nodeValue);
206                 }
207
208                 // If none text was found then take the paragraph content
209                 if ($text == "") {
210                         $list = $xpath->query("//p");
211                         foreach ($list as $node)
212                                 if (strlen($node->nodeValue) > 40)
213                                         $text .= " ".trim($node->nodeValue);
214                 }
215
216                 if ($text != "") {
217                         $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text));
218
219                         while (strpos($text, "  "))
220                                 $text = trim(str_replace("  ", " ", $text));
221
222                         $siteinfo["text"] = html_entity_decode(substr($text,0,350), ENT_QUOTES, "UTF-8").'...';
223                 }
224         }
225
226         return($siteinfo);
227 }
228
229 function arr_add_hashes(&$item,$k) {
230         $item = '#' . $item;
231 }
232
233 function parse_url_content(&$a) {
234
235         $text = null;
236         $str_tags = '';
237
238         $textmode = false;
239
240         if(local_user() && (! feature_enabled(local_user(),'richtext')))
241                 $textmode = true;
242
243         //if($textmode)
244         $br = (($textmode) ? "\n" : '<br />');
245
246         if(x($_GET,'binurl'))
247                 $url = trim(hex2bin($_GET['binurl']));
248         else
249                 $url = trim($_GET['url']);
250
251         if($_GET['title'])
252                 $title = strip_tags(trim($_GET['title']));
253
254         if($_GET['description'])
255                 $text = strip_tags(trim($_GET['description']));
256
257         if($_GET['tags']) {
258                 $arr_tags = str_getcsv($_GET['tags']);
259                 if(count($arr_tags)) {
260                         array_walk($arr_tags,'arr_add_hashes');
261                         $str_tags = $br . implode(' ',$arr_tags) . $br;
262                 }
263         }
264
265         logger('parse_url: ' . $url);
266
267         if($textmode)
268                 $template = $br . '[bookmark=%s]%s[/bookmark]%s' . $br;
269         else
270                 $template = "<br /><a class=\"bookmark\" href=\"%s\" >%s</a>%s<br />";
271
272         $arr = array('url' => $url, 'text' => '');
273
274         call_hooks('parse_link', $arr);
275
276         if(strlen($arr['text'])) {
277                 echo $arr['text'];
278                 killme();
279         }
280
281
282         if($url && $title && $text) {
283
284                 if($textmode)
285                         $text = $br . '[quote]' . trim($text) . '[/quote]' . $br;
286                 else
287                         $text = '<br /><blockquote>' . trim($text) . '</blockquote><br />';
288
289                 $title = str_replace(array("\r","\n"),array('',''),$title);
290
291                 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
292
293                 logger('parse_url (unparsed): returns: ' . $result);
294
295                 echo $result;
296                 killme();
297         }
298
299         $siteinfo = parseurl_getsiteinfo($url);
300
301         if($siteinfo["title"] == "") {
302                 echo sprintf($template,$url,$url,'') . $str_tags;
303                 killme();
304         } else {
305                 $text = $siteinfo["text"];
306                 $title = $siteinfo["title"];
307         }
308
309         $image = "";
310
311         if(sizeof($siteinfo["images"]) > 0){
312                 /* Execute below code only if image is present in siteinfo */
313
314                 $total_images = 0;
315                 $max_images = get_config('system','max_bookmark_images');
316                 if($max_images === false)
317                         $max_images = 2;
318                 else
319                         $max_images = intval($max_images);
320
321                 foreach ($siteinfo["images"] as $imagedata) {
322                         if($textmode)
323                                 $image .= '[img='.$imagedata["width"].'x'.$imagedata["height"].']'.$imagedata["src"].'[/img]' . "\n";
324                         else
325                                 $image .= '<img height="'.$imagedata["height"].'" width="'.$imagedata["width"].'" src="'.$imagedata["src"].'" alt="photo" /><br />';
326                         $total_images ++;
327                         if($max_images && $max_images >= $total_images)
328                                 break;
329         }
330         }
331
332         if(strlen($text)) {
333                 if($textmode)
334                         $text = $br.'[quote]'.trim($text).'[/quote]'.$br ;
335                 else
336                         $text = '<br /><blockquote>'.trim($text).'</blockquote><br />';
337         }
338
339         if($image) {
340                 $text = $br.$br.$image.$text;
341         }
342         $title = str_replace(array("\r","\n"),array('',''),$title);
343
344         $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
345
346         logger('parse_url: returns: ' . $result);
347
348         echo trim($result);
349         killme();
350 }