]> git.mxchange.org Git - friendica.git/blob - mod/parse_url.php
if no FB image and fb link is on our site, use Friendika logo for fb image. Otherwise...
[friendica.git] / mod / parse_url.php
1 <?php
2
3 require_once('library/HTML5/Parser.php');
4
5
6 function parse_url_content(&$a) {
7
8         logger('parse_url: ' . $_GET['url']);
9
10         $url = trim(hex2bin($_GET['url']));
11
12         logger('parse_url: ' . $url);
13
14         $text = null;
15
16         $template = "<a href=\"%s\" >%s</a>\n%s";
17
18
19         $arr = array('url' => $url, 'text' => '');
20
21         call_hooks('parse_link', $arr);
22
23         if(strlen($arr['text'])) {
24                 echo $arr['text'];
25                 killme();
26         }
27
28         if($url) 
29                 $s = fetch_url($url);
30         else {
31                 echo '';
32                 killme();
33         }
34
35
36         if(! $s) {
37                 echo sprintf($template,$url,$url,'');
38                 killme();
39         }
40
41         $dom = @HTML5_Parser::parse($s);
42
43         if(! $dom)
44                 return $ret;
45
46         $items = $dom->getElementsByTagName('title');
47
48         if($items) {
49                 foreach($items as $item) {
50                         $title = trim($item->textContent);
51                         break;
52                 }
53         }
54
55
56         $divs = $dom->getElementsByTagName('div');
57         if($divs) {
58                 foreach($divs as $div) {
59                         $class = $div->getAttribute('class');
60                         if($class && stristr($class,'article')) {
61                                 $items = $div->getElementsByTagName('p');
62                                 if($items) {
63                                         foreach($items as $item) {
64                                                 if($item->getElementsByTagName('script'))
65                                                         continue;
66                                                 $text = $item->textContent;
67                                                 $text = strip_tags($text);
68                                                 if(strlen($text) < 100)
69                                                         continue;
70                                                 $text = substr($text,0,250) . '...' ;
71                                                 break;
72                                         }
73                                 }
74                         }
75                 }
76         }
77
78         if(! $text) {
79                 $items = $dom->getElementsByTagName('p');
80                 if($items) {
81                         foreach($items as $item) {
82                                 if($item->getElementsByTagName('script'))
83                                         continue;
84                                 $text = $item->textContent;
85                                 $text = strip_tags($text);
86                                 if(strlen($text) < 100)
87                                         continue;
88                                 $text = substr($text,0,250) . '...' ;
89                                 break;
90                         }
91                 }
92         }
93
94         if(strlen($text)) {
95                 $text = '<br />' . $text;
96         }
97
98         echo sprintf($template,$url,$title,$text);
99         killme();
100 }