]> git.mxchange.org Git - friendica.git/blob - mod/parse_url.php
purify html before trying to parse wild urls. This way at least it should parse.
[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         $config = HTMLPurifier_Config::createDefault();
43         $config->set('Cache.DefinitionImpl', null);
44
45         $purifier = new HTMLPurifier($config);
46         $s = $purifier->purify($s);
47
48         $dom = @HTML5_Parser::parse($s);
49
50         if(! $dom) {
51                 echo sprintf($template,$url,$url,'');
52                 killme();
53         }
54
55         $items = $dom->getElementsByTagName('title');
56
57         if($items) {
58                 foreach($items as $item) {
59                         $title = trim($item->textContent);
60                         break;
61                 }
62         }
63
64         $divs = $dom->getElementsByTagName('div');
65         if($divs) {
66                 foreach($divs as $div) {
67                         $class = $div->getAttribute('class');
68                         if($class && stristr($class,'article')) {
69                                 $items = $div->getElementsByTagName('p');
70                                 if($items) {
71                                         foreach($items as $item) {
72                                                 if($item->getElementsByTagName('script'))
73                                                         continue;
74                                                 $text = $item->textContent;
75                                                 $text = strip_tags($text);
76                                                 if(strlen($text) < 100)
77                                                         continue;
78                                                 $text = substr($text,0,250) . '...' ;
79                                                 break;
80                                         }
81                                 }
82                         }
83                 }
84         }
85
86         if(! $text) {
87                 $items = $dom->getElementsByTagName('p');
88                 if($items) {
89                         foreach($items as $item) {
90                                 if($item->getElementsByTagName('script'))
91                                         continue;
92                                 $text = $item->textContent;
93                                 $text = strip_tags($text);
94                                 if(strlen($text) < 100)
95                                         continue;
96                                 $text = substr($text,0,250) . '...' ;
97                                 break;
98                         }
99                 }
100         }
101
102         if(strlen($text)) {
103                 $text = '<br />' . $text;
104         }
105
106         echo sprintf($template,$url,($title) ? $title : $url,$text);
107         killme();
108 }