]> git.mxchange.org Git - friendica.git/blob - mod/parse_url.php
bookmarks + bug #140
[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 = "<br /><a class=\"bookmark\" href=\"%s\" >%s</a>%s<br />";
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 = strip_tags(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 //      logger('parse_url: purified: ' . $s, LOGGER_DATA);
55
56         $dom = @HTML5_Parser::parse($s);
57
58         if(! $dom) {
59                 echo sprintf($template,$url,$url,'');
60                 killme();
61         }
62
63         $items = $dom->getElementsByTagName('title');
64
65         if($items) {
66                 foreach($items as $item) {
67                         $title = trim($item->textContent);
68                         break;
69                 }
70         }
71
72         $divs = $dom->getElementsByTagName('div');
73         if($divs) {
74                 foreach($divs as $div) {
75                         $class = $div->getAttribute('class');
76                         if($class && (stristr($class,'article') || stristr($class,'content'))) {
77                                 $items = $div->getElementsByTagName('p');
78                                 if($items) {
79                                         foreach($items as $item) {
80                                                 $text = $item->textContent;
81                                                 if(stristr($text,'<script')) {
82                                                         $text = '';
83                                                         continue;
84                                                 }
85                                                 $text = strip_tags($text);
86                                                 if(strlen($text) < 100) {
87                                                         $text = '';
88                                                         continue;
89                                                 }
90                                                 $text = substr($text,0,250) . '...' ;
91                                                 break;
92                                         }
93                                 }
94                         }
95                         if($text)
96                                 break;
97                 }
98         }
99
100         if(! $text) {
101                 $items = $dom->getElementsByTagName('p');
102                 if($items) {
103                         foreach($items as $item) {
104                                 $text = $item->textContent;
105                                 if(stristr($text,'<script'))
106                                         continue;
107                                 $text = strip_tags($text);
108                                 if(strlen($text) < 100) {
109                                         $text = '';
110                                         continue;
111                                 }
112                                 $text = substr($text,0,250) . '...' ;
113                                 break;
114                         }
115                 }
116         }
117
118         if(strlen($text)) {
119                 $text = '<br /><br /><blockquote>' . $text . '</blockquote><br />';
120         }
121
122         $title = str_replace("\n",'',$title);
123
124         $result = sprintf($template,$url,($title) ? $title : $url,$text);
125
126         logger('parse_url: returns: ' . $result); 
127
128         echo $result;
129         killme();
130 }