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