]> git.mxchange.org Git - friendica.git/blob - mod/parse_url.php
community: Only show top postings, no comments
[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("//meta[@content]");
101         foreach ($list as $node) {
102                 $attr = array();
103                 if ($node->attributes->length)
104                         foreach ($node->attributes as $attribute)
105                                 $attr[$attribute->name] = $attribute->value;
106
107                 if (@$attr["http-equiv"] == 'refresh') {
108                         $path = $attr["content"];
109                         $pathinfo = explode(";", $path);
110                         $content = "";
111                         foreach ($pathinfo AS $value) {
112                                 if (substr(strtolower($value), 0, 4) == "url=")
113                                         $content = substr($value, 4);
114                         }
115                         if ($content != "") {
116                                 $siteinfo = parseurl_getsiteinfo($content);
117                                 return($siteinfo);
118                         }
119                 }
120         }
121
122         //$list = $xpath->query("head/title");
123         $list = $xpath->query("//title");
124         foreach ($list as $node)
125                 $siteinfo["title"] =  html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8");
126
127         //$list = $xpath->query("head/meta[@name]");
128         $list = $xpath->query("//meta[@name]");
129         foreach ($list as $node) {
130                 $attr = array();
131                 if ($node->attributes->length)
132                         foreach ($node->attributes as $attribute)
133                                 $attr[$attribute->name] = $attribute->value;
134
135                 $attr["content"] = html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8");
136
137                 switch (strtolower($attr["name"])) {
138                         case "fulltitle":
139                                 $siteinfo["title"] = $attr["content"];
140                                 break;
141                         case "description":
142                                 $siteinfo["text"] = $attr["content"];
143                                 break;
144                         case "dc.title":
145                                 $siteinfo["title"] = $attr["content"];
146                                 break;
147                         case "dc.description":
148                                 $siteinfo["text"] = $attr["content"];
149                                 break;
150                 }
151         }
152
153         //$list = $xpath->query("head/meta[@property]");
154         $list = $xpath->query("//meta[@property]");
155         foreach ($list as $node) {
156                 $attr = array();
157                 if ($node->attributes->length)
158                         foreach ($node->attributes as $attribute)
159                                 $attr[$attribute->name] = $attribute->value;
160
161                 $attr["content"] = html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8");
162
163                 switch (strtolower($attr["property"])) {
164                         case "og:image":
165                                 $siteinfo["image"] = $attr["content"];
166                                 break;
167                         case "og:title":
168                                 $siteinfo["title"] = $attr["content"];
169                                 break;
170                         case "og:description":
171                                 $siteinfo["text"] = $attr["content"];
172                                 break;
173                 }
174         }
175
176         if ($siteinfo["image"] == "") {
177             $list = $xpath->query("//img[@src]");
178             foreach ($list as $node) {
179                 $attr = array();
180                 if ($node->attributes->length)
181                     foreach ($node->attributes as $attribute)
182                         $attr[$attribute->name] = $attribute->value;
183
184                         $src = completeurl($attr["src"], $url);
185                         $photodata = @getimagesize($src);
186
187                         if (($photodata) && ($photodata[0] > 150) and ($photodata[1] > 150)) {
188                                 if ($photodata[0] > 300) {
189                                         $photodata[1] = round($photodata[1] * (300 / $photodata[0]));
190                                         $photodata[0] = 300;
191                                 }
192                                 if ($photodata[1] > 300) {
193                                         $photodata[0] = round($photodata[0] * (300 / $photodata[1]));
194                                         $photodata[1] = 300;
195                                 }
196                                 $siteinfo["images"][] = array("src"=>$src,
197                                                                 "width"=>$photodata[0],
198                                                                 "height"=>$photodata[1]);
199                         }
200
201                 }
202     } else {
203                 $src = completeurl($siteinfo["image"], $url);
204
205                 unset($siteinfo["image"]);
206
207                 $photodata = @getimagesize($src);
208
209                 if (($photodata) && ($photodata[0] > 10) and ($photodata[1] > 10))
210                         $siteinfo["images"][] = array("src"=>$src,
211                                                         "width"=>$photodata[0],
212                                                         "height"=>$photodata[1]);
213         }
214
215         if ($siteinfo["text"] == "") {
216                 $text = "";
217
218                 $list = $xpath->query("//div[@class='article']");
219                 foreach ($list as $node)
220                         if (strlen($node->nodeValue) > 40)
221                                 $text .= " ".trim($node->nodeValue);
222
223                 if ($text == "") {
224                         $list = $xpath->query("//div[@class='content']");
225                         foreach ($list as $node)
226                                 if (strlen($node->nodeValue) > 40)
227                                         $text .= " ".trim($node->nodeValue);
228                 }
229
230                 // If none text was found then take the paragraph content
231                 if ($text == "") {
232                         $list = $xpath->query("//p");
233                         foreach ($list as $node)
234                                 if (strlen($node->nodeValue) > 40)
235                                         $text .= " ".trim($node->nodeValue);
236                 }
237
238                 if ($text != "") {
239                         $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text));
240
241                         while (strpos($text, "  "))
242                                 $text = trim(str_replace("  ", " ", $text));
243
244                         $siteinfo["text"] = html_entity_decode(substr($text,0,350), ENT_QUOTES, "UTF-8").'...';
245                 }
246         }
247
248         return($siteinfo);
249 }
250
251 function arr_add_hashes(&$item,$k) {
252         $item = '#' . $item;
253 }
254
255 function parse_url_content(&$a) {
256
257         $text = null;
258         $str_tags = '';
259
260         $textmode = false;
261
262         if(local_user() && (! feature_enabled(local_user(),'richtext')))
263                 $textmode = true;
264
265         //if($textmode)
266         $br = (($textmode) ? "\n" : '<br />');
267
268         if(x($_GET,'binurl'))
269                 $url = trim(hex2bin($_GET['binurl']));
270         else
271                 $url = trim($_GET['url']);
272
273         if($_GET['title'])
274                 $title = strip_tags(trim($_GET['title']));
275
276         if($_GET['description'])
277                 $text = strip_tags(trim($_GET['description']));
278
279         if($_GET['tags']) {
280                 $arr_tags = str_getcsv($_GET['tags']);
281                 if(count($arr_tags)) {
282                         array_walk($arr_tags,'arr_add_hashes');
283                         $str_tags = $br . implode(' ',$arr_tags) . $br;
284                 }
285         }
286
287         logger('parse_url: ' . $url);
288
289         if($textmode)
290                 $template = $br . '[bookmark=%s]%s[/bookmark]%s' . $br;
291         else
292                 $template = "<br /><a class=\"bookmark\" href=\"%s\" >%s</a>%s<br />";
293
294         $arr = array('url' => $url, 'text' => '');
295
296         call_hooks('parse_link', $arr);
297
298         if(strlen($arr['text'])) {
299                 echo $arr['text'];
300                 killme();
301         }
302
303
304         if($url && $title && $text) {
305
306                 if($textmode)
307                         $text = $br . '[quote]' . trim($text) . '[/quote]' . $br;
308                 else
309                         $text = '<br /><blockquote>' . trim($text) . '</blockquote><br />';
310
311                 $title = str_replace(array("\r","\n"),array('',''),$title);
312
313                 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
314
315                 logger('parse_url (unparsed): returns: ' . $result);
316
317                 echo $result;
318                 killme();
319         }
320
321         $siteinfo = parseurl_getsiteinfo($url);
322
323         if($siteinfo["title"] == "") {
324                 echo sprintf($template,$url,$url,'') . $str_tags;
325                 killme();
326         } else {
327                 $text = $siteinfo["text"];
328                 $title = $siteinfo["title"];
329         }
330
331         $image = "";
332
333         if(sizeof($siteinfo["images"]) > 0){
334                 /* Execute below code only if image is present in siteinfo */
335
336                 $total_images = 0;
337                 $max_images = get_config('system','max_bookmark_images');
338                 if($max_images === false)
339                         $max_images = 2;
340                 else
341                         $max_images = intval($max_images);
342
343                 foreach ($siteinfo["images"] as $imagedata) {
344                         if($textmode)
345                                 $image .= '[img='.$imagedata["width"].'x'.$imagedata["height"].']'.$imagedata["src"].'[/img]' . "\n";
346                         else
347                                 $image .= '<img height="'.$imagedata["height"].'" width="'.$imagedata["width"].'" src="'.$imagedata["src"].'" alt="photo" /><br />';
348                         $total_images ++;
349                         if($max_images && $max_images >= $total_images)
350                                 break;
351         }
352         }
353
354         if(strlen($text)) {
355                 if($textmode)
356                         $text = $br.'[quote]'.trim($text).'[/quote]'.$br ;
357                 else
358                         $text = '<br /><blockquote>'.trim($text).'</blockquote><br />';
359         }
360
361         if($image) {
362                 $text = $br.$br.$image.$text;
363         }
364         $title = str_replace(array("\r","\n"),array('',''),$title);
365
366         $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
367
368         logger('parse_url: returns: ' . $result);
369
370         echo trim($result);
371         killme();
372 }