]> git.mxchange.org Git - friendica.git/blob - mod/parse_url.php
Bugfix: The user id wasn't fetched correctly.
[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, $no_guessing = false, $do_oembed = true, $count = 1) {
54         require_once("include/network.php");
55
56         $a = get_app();
57
58         $siteinfo = array();
59
60         if ($count > 10) {
61                 logger("parseurl_getsiteinfo: Endless loop detected for ".$url, LOGGER_DEBUG);
62                 return($siteinfo);
63         }
64
65         $url = trim($url, "'");
66         $url = trim($url, '"');
67
68         $url = original_url($url);
69
70         $siteinfo["url"] = $url;
71         $siteinfo["type"] = "link";
72
73         $stamp1 = microtime(true);
74
75         $ch = curl_init();
76         curl_setopt($ch, CURLOPT_URL, $url);
77         curl_setopt($ch, CURLOPT_HEADER, 1);
78         curl_setopt($ch, CURLOPT_NOBODY, 1);
79         curl_setopt($ch, CURLOPT_TIMEOUT, 3);
80         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
81         //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
82         curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
83
84         $header = curl_exec($ch);
85         $curl_info = @curl_getinfo($ch);
86         $http_code = $curl_info['http_code'];
87         curl_close($ch);
88
89         $a->save_timestamp($stamp1, "network");
90
91         if ((($curl_info['http_code'] == "301") OR ($curl_info['http_code'] == "302") OR ($curl_info['http_code'] == "303") OR ($curl_info['http_code'] == "307"))
92                 AND (($curl_info['redirect_url'] != "") OR ($curl_info['location'] != ""))) {
93                 if ($curl_info['redirect_url'] != "")
94                         $siteinfo = parseurl_getsiteinfo($curl_info['redirect_url'], $no_guessing, $do_oembed, ++$count);
95                 else
96                         $siteinfo = parseurl_getsiteinfo($curl_info['location'], $no_guessing, $do_oembed, ++$count);
97                 return($siteinfo);
98         }
99
100         // if the file is too large then exit
101         if ($curl_info["download_content_length"] > 1000000)
102                 return($siteinfo);
103
104         // if it isn't a HTML file then exit
105         if (($curl_info["content_type"] != "") AND !strstr(strtolower($curl_info["content_type"]),"html"))
106                 return($siteinfo);
107
108         if ($do_oembed) {
109                 require_once("include/oembed.php");
110
111                 $oembed_data = oembed_fetch_url($url);
112
113                 if ($oembed_data->type != "error")
114                         $siteinfo["type"] = $oembed_data->type;
115
116                 if (($oembed_data->type == "link") AND ($siteinfo["type"] != "photo")) {
117                         if (isset($oembed_data->title))
118                                 $siteinfo["title"] = $oembed_data->title;
119                         if (isset($oembed_data->description))
120                                 $siteinfo["text"] = trim($oembed_data->description);
121                         if (isset($oembed_data->thumbnail_url))
122                                 $siteinfo["image"] = $oembed_data->thumbnail_url;
123                 }
124         }
125
126         $stamp1 = microtime(true);
127
128         // Now fetch the body as well
129         $ch = curl_init();
130         curl_setopt($ch, CURLOPT_URL, $url);
131         curl_setopt($ch, CURLOPT_HEADER, 1);
132         curl_setopt($ch, CURLOPT_NOBODY, 0);
133         curl_setopt($ch, CURLOPT_TIMEOUT, 10);
134         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
135         curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
136
137         $header = curl_exec($ch);
138         $curl_info = @curl_getinfo($ch);
139         $http_code = $curl_info['http_code'];
140         curl_close($ch);
141
142         $a->save_timestamp($stamp1, "network");
143
144         // Fetch the first mentioned charset. Can be in body or header
145         $charset = "";
146         if (preg_match('/charset=(.*?)['."'".'"\s\n]/', $header, $matches))
147                 $charset = trim(trim(trim(array_pop($matches)), ';,'));
148
149         if ($charset == "")
150                 $charset = "utf-8";
151
152         $pos = strpos($header, "\r\n\r\n");
153
154         if ($pos)
155                 $body = trim(substr($header, $pos));
156         else
157                 $body = $header;
158
159         if (($charset != '') AND (strtoupper($charset) != "UTF-8")) {
160                 logger("parseurl_getsiteinfo: detected charset ".$charset, LOGGER_DEBUG);
161                 //$body = mb_convert_encoding($body, "UTF-8", $charset);
162                 $body = iconv($charset, "UTF-8//TRANSLIT", $body);
163         }
164
165         $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
166
167         $doc = new DOMDocument();
168         @$doc->loadHTML($body);
169
170         deletenode($doc, 'style');
171         deletenode($doc, 'script');
172         deletenode($doc, 'option');
173         deletenode($doc, 'h1');
174         deletenode($doc, 'h2');
175         deletenode($doc, 'h3');
176         deletenode($doc, 'h4');
177         deletenode($doc, 'h5');
178         deletenode($doc, 'h6');
179         deletenode($doc, 'ol');
180         deletenode($doc, 'ul');
181
182         $xpath = new DomXPath($doc);
183
184         $list = $xpath->query("//meta[@content]");
185         foreach ($list as $node) {
186                 $attr = array();
187                 if ($node->attributes->length)
188                         foreach ($node->attributes as $attribute)
189                                 $attr[$attribute->name] = $attribute->value;
190
191                 if (@$attr["http-equiv"] == 'refresh') {
192                         $path = $attr["content"];
193                         $pathinfo = explode(";", $path);
194                         $content = "";
195                         foreach ($pathinfo AS $value) {
196                                 if (substr(strtolower($value), 0, 4) == "url=")
197                                         $content = substr($value, 4);
198                         }
199                         if ($content != "") {
200                                 $siteinfo = parseurl_getsiteinfo($content, $no_guessing, $do_oembed, ++$count);
201                                 return($siteinfo);
202                         }
203                 }
204         }
205
206         //$list = $xpath->query("head/title");
207         $list = $xpath->query("//title");
208         foreach ($list as $node)
209                 $siteinfo["title"] =  html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8");
210
211         //$list = $xpath->query("head/meta[@name]");
212         $list = $xpath->query("//meta[@name]");
213         foreach ($list as $node) {
214                 $attr = array();
215                 if ($node->attributes->length)
216                         foreach ($node->attributes as $attribute)
217                                 $attr[$attribute->name] = $attribute->value;
218
219                 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
220
221                 if ($attr["content"] != "")
222                         switch (strtolower($attr["name"])) {
223                                 case "fulltitle":
224                                         $siteinfo["title"] = $attr["content"];
225                                         break;
226                                 case "description":
227                                         $siteinfo["text"] = $attr["content"];
228                                         break;
229                                 case "thumbnail":
230                                         $siteinfo["image"] = $attr["content"];
231                                         break;
232                                 case "twitter:image":
233                                         $siteinfo["image"] = $attr["content"];
234                                         break;
235                                 case "twitter:image:src":
236                                         $siteinfo["image"] = $attr["content"];
237                                         break;
238                                 case "twitter:card":
239                                         if (($siteinfo["type"] == "") OR ($attr["content"] == "photo"))
240                                                 $siteinfo["type"] = $attr["content"];
241                                         break;
242                                 case "twitter:description":
243                                         $siteinfo["text"] = $attr["content"];
244                                         break;
245                                 case "twitter:title":
246                                         $siteinfo["title"] = $attr["content"];
247                                         break;
248                                 case "dc.title":
249                                         $siteinfo["title"] = $attr["content"];
250                                         break;
251                                 case "dc.description":
252                                         $siteinfo["text"] = $attr["content"];
253                                         break;
254                                 case "keywords":
255                                         $keywords = explode(",", $attr["content"]);
256                                         break;
257                                 case "news_keywords":
258                                         $keywords = explode(",", $attr["content"]);
259                                         break;
260                         }
261                 if ($siteinfo["type"] == "summary")
262                         $siteinfo["type"] = "link";
263         }
264
265         if (isset($keywords)) {
266                 $siteinfo["keywords"] = array();
267                 foreach ($keywords as $keyword)
268                         $siteinfo["keywords"][] = trim($keyword);
269         }
270
271         //$list = $xpath->query("head/meta[@property]");
272         $list = $xpath->query("//meta[@property]");
273         foreach ($list as $node) {
274                 $attr = array();
275                 if ($node->attributes->length)
276                         foreach ($node->attributes as $attribute)
277                                 $attr[$attribute->name] = $attribute->value;
278
279                 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
280
281                 if ($attr["content"] != "")
282                         switch (strtolower($attr["property"])) {
283                                 case "og:image":
284                                         $siteinfo["image"] = $attr["content"];
285                                         break;
286                                 case "og:title":
287                                         $siteinfo["title"] = $attr["content"];
288                                         break;
289                                 case "og:description":
290                                         $siteinfo["text"] = $attr["content"];
291                                         break;
292                         }
293         }
294
295         if ((@$siteinfo["image"] == "") AND !$no_guessing) {
296             $list = $xpath->query("//img[@src]");
297             foreach ($list as $node) {
298                 $attr = array();
299                 if ($node->attributes->length)
300                     foreach ($node->attributes as $attribute)
301                         $attr[$attribute->name] = $attribute->value;
302
303                         $src = completeurl($attr["src"], $url);
304                         $photodata = @getimagesize($src);
305
306                         if (($photodata) && ($photodata[0] > 150) and ($photodata[1] > 150)) {
307                                 if ($photodata[0] > 300) {
308                                         $photodata[1] = round($photodata[1] * (300 / $photodata[0]));
309                                         $photodata[0] = 300;
310                                 }
311                                 if ($photodata[1] > 300) {
312                                         $photodata[0] = round($photodata[0] * (300 / $photodata[1]));
313                                         $photodata[1] = 300;
314                                 }
315                                 $siteinfo["images"][] = array("src"=>$src,
316                                                                 "width"=>$photodata[0],
317                                                                 "height"=>$photodata[1]);
318                         }
319
320                 }
321     } else {
322                 $src = completeurl($siteinfo["image"], $url);
323
324                 unset($siteinfo["image"]);
325
326                 $photodata = @getimagesize($src);
327
328                 if (($photodata) && ($photodata[0] > 10) and ($photodata[1] > 10))
329                         $siteinfo["images"][] = array("src"=>$src,
330                                                         "width"=>$photodata[0],
331                                                         "height"=>$photodata[1]);
332         }
333
334         if ((@$siteinfo["text"] == "") AND (@$siteinfo["title"] != "") AND !$no_guessing) {
335                 $text = "";
336
337                 $list = $xpath->query("//div[@class='article']");
338                 foreach ($list as $node)
339                         if (strlen($node->nodeValue) > 40)
340                                 $text .= " ".trim($node->nodeValue);
341
342                 if ($text == "") {
343                         $list = $xpath->query("//div[@class='content']");
344                         foreach ($list as $node)
345                                 if (strlen($node->nodeValue) > 40)
346                                         $text .= " ".trim($node->nodeValue);
347                 }
348
349                 // If none text was found then take the paragraph content
350                 if ($text == "") {
351                         $list = $xpath->query("//p");
352                         foreach ($list as $node)
353                                 if (strlen($node->nodeValue) > 40)
354                                         $text .= " ".trim($node->nodeValue);
355                 }
356
357                 if ($text != "") {
358                         $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text));
359
360                         while (strpos($text, "  "))
361                                 $text = trim(str_replace("  ", " ", $text));
362
363                         $siteinfo["text"] = trim(html_entity_decode(substr($text,0,350), ENT_QUOTES, "UTF-8").'...');
364                 }
365         }
366
367         logger("parseurl_getsiteinfo: Siteinfo for ".$url." ".print_r($siteinfo, true), LOGGER_DEBUG);
368
369         call_hooks('getsiteinfo', $siteinfo);
370
371         return($siteinfo);
372 }
373
374 function arr_add_hashes(&$item,$k) {
375         $item = '#' . $item;
376 }
377
378 function parse_url_content(&$a) {
379
380         $text = null;
381         $str_tags = '';
382
383         $textmode = false;
384
385         if(local_user() && (! feature_enabled(local_user(),'richtext')))
386                 $textmode = true;
387
388         //if($textmode)
389         $br = (($textmode) ? "\n" : '<br />');
390
391         if(x($_GET,'binurl'))
392                 $url = trim(hex2bin($_GET['binurl']));
393         else
394                 $url = trim($_GET['url']);
395
396         if($_GET['title'])
397                 $title = strip_tags(trim($_GET['title']));
398
399         if($_GET['description'])
400                 $text = strip_tags(trim($_GET['description']));
401
402         if($_GET['tags']) {
403                 $arr_tags = str_getcsv($_GET['tags']);
404                 if(count($arr_tags)) {
405                         array_walk($arr_tags,'arr_add_hashes');
406                         $str_tags = $br . implode(' ',$arr_tags) . $br;
407                 }
408         }
409
410         // add url scheme if missing
411         $arrurl = parse_url($url);
412         if (!x($arrurl, 'scheme')) {
413                 if (x($arrurl, 'host'))
414                         $url = "http:".$url;
415                 else
416                         $url = "http://".$url;
417         }
418
419         logger('parse_url: ' . $url);
420
421         if($textmode)
422                 $template = '[bookmark=%s]%s[/bookmark]%s';
423         else
424                 $template = "<a class=\"bookmark\" href=\"%s\" >%s</a>%s";
425
426         $arr = array('url' => $url, 'text' => '');
427
428         call_hooks('parse_link', $arr);
429
430         if(strlen($arr['text'])) {
431                 echo $arr['text'];
432                 killme();
433         }
434
435
436         if($url && $title && $text) {
437
438                 $title = str_replace(array("\r","\n"),array('',''),$title);
439
440                 if($textmode)
441                         $text = '[quote]' . trim($text) . '[/quote]' . $br;
442                 else {
443                         $text = '<blockquote>' . htmlspecialchars(trim($text)) . '</blockquote><br />';
444                         $title = htmlspecialchars($title);
445                 }
446
447                 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
448
449                 logger('parse_url (unparsed): returns: ' . $result);
450
451                 echo $result;
452                 killme();
453         }
454
455         $siteinfo = parseurl_getsiteinfo($url);
456
457 //      if ($textmode) {
458 //              require_once("include/items.php");
459 //
460 //              echo add_page_info_data($siteinfo);
461 //              killme();
462 //      }
463
464         $url= $siteinfo["url"];
465
466         // If the link contains BBCode stuff, make a short link out of this to avoid parsing problems
467         if (strpos($url, '[') OR strpos($url, ']')) {
468                 require_once("include/network.php");
469                 $url = short_link($url);
470         }
471
472         $sitedata = "";
473
474         if($siteinfo["title"] != "") {
475                 $text = $siteinfo["text"];
476                 $title = $siteinfo["title"];
477         }
478
479         $image = "";
480
481         if (($siteinfo["type"] != "video") AND (sizeof($siteinfo["images"]) > 0)){
482                 /* Execute below code only if image is present in siteinfo */
483
484                 $total_images = 0;
485                 $max_images = get_config('system','max_bookmark_images');
486                 if($max_images === false)
487                         $max_images = 2;
488                 else
489                         $max_images = intval($max_images);
490
491                 foreach ($siteinfo["images"] as $imagedata) {
492                         if($textmode)
493                                 $image .= '[img='.$imagedata["width"].'x'.$imagedata["height"].']'.$imagedata["src"].'[/img]' . "\n";
494                         else
495                                 $image .= '<img height="'.$imagedata["height"].'" width="'.$imagedata["width"].'" src="'.$imagedata["src"].'" alt="photo" /><br />';
496                         $total_images ++;
497                         if($max_images && $max_images >= $total_images)
498                                 break;
499                 }
500         }
501
502         if(strlen($text)) {
503                 if($textmode)
504                         $text = '[quote]'.trim($text).'[/quote]';
505                 else
506                         $text = '<blockquote>'.htmlspecialchars(trim($text)).'</blockquote>';
507         }
508
509         if($image)
510                 $text = $br.$br.$image.$text;
511         else
512                 $text = $br.$text;
513
514         $title = str_replace(array("\r","\n"),array('',''),$title);
515
516         $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
517
518         logger('parse_url: returns: ' . $result);
519
520         $sitedata .=  trim($result);
521
522         if (($siteinfo["type"] == "video") AND ($url != ""))
523                 echo "[class=type-video]".$sitedata."[/class]";
524         elseif (($siteinfo["type"] != "photo"))
525                 echo "[class=type-link]".$sitedata."[/class]";
526         else
527                 echo "[class=type-photo]".$title.$br.$image."[/class]";
528
529         killme();
530 }
531 ?>