]> git.mxchange.org Git - friendica.git/blob - mod/parse_url.php
bug #102
[friendica.git] / mod / parse_url.php
1 <?php
2
3 require_once('library/HTML5/Parser.php');
4 require_once('library/HTMLPurifier.auto.php');
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         logger('parse_url: data: ' . $s, LOGGER_DATA);
36
37         if(! $s) {
38                 echo sprintf($template,$url,$url,'');
39                 killme();
40         }
41
42         if(strpos($s,'<title>')) {
43                 $title = substr($s,strpos($s,'<title>')+7,64);
44                 if(strpos($title,'<') !== false)
45                         $title = substr($title,0,strpos($title,'<'));
46         }
47
48         $config = HTMLPurifier_Config::createDefault();
49         $config->set('Cache.DefinitionImpl', null);
50
51         $purifier = new HTMLPurifier($config);
52         $s = $purifier->purify($s);
53
54         $dom = @HTML5_Parser::parse($s);
55
56         if(! $dom) {
57                 echo sprintf($template,$url,$url,'');
58                 killme();
59         }
60
61         $items = $dom->getElementsByTagName('title');
62
63         if($items) {
64                 foreach($items as $item) {
65                         $title = trim($item->textContent);
66                         break;
67                 }
68         }
69
70         $divs = $dom->getElementsByTagName('div');
71         if($divs) {
72                 foreach($divs as $div) {
73                         $class = $div->getAttribute('class');
74                         if($class && stristr($class,'article')) {
75                                 $items = $div->getElementsByTagName('p');
76                                 if($items) {
77                                         foreach($items as $item) {
78                                                 if($item->getElementsByTagName('script'))
79                                                         continue;
80                                                 $text = $item->textContent;
81                                                 $text = strip_tags($text);
82                                                 if(strlen($text) < 100)
83                                                         continue;
84                                                 $text = substr($text,0,250) . '...' ;
85                                                 break;
86                                         }
87                                 }
88                         }
89                 }
90         }
91
92         if(! $text) {
93                 $items = $dom->getElementsByTagName('p');
94                 if($items) {
95                         foreach($items as $item) {
96                                 if($item->getElementsByTagName('script'))
97                                         continue;
98                                 $text = $item->textContent;
99                                 $text = strip_tags($text);
100                                 if(strlen($text) < 100)
101                                         continue;
102                                 $text = substr($text,0,250) . '...' ;
103                                 break;
104                         }
105                 }
106         }
107
108         if(strlen($text)) {
109                 $text = '<br />' . $text;
110         }
111
112         echo sprintf($template,$url,($title) ? $title : $url,$text);
113         killme();
114 }