]> git.mxchange.org Git - friendica.git/blob - mod/parse_url.php
"notes" app
[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                                                 $text = $item->textContent;
65                                                 $text = strip_tags($text);
66                                                 if(strlen($text) < 100)
67                                                         continue;
68                                                 $text = substr($text,0,250) . '...' ;
69                                                 break;
70                                         }
71                                 }
72                         }
73                 }
74         }
75
76         if(! $text) {
77                 $items = $dom->getElementsByTagName('p');
78                 if($items) {
79                         foreach($items as $item) {
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         if(strlen($text)) {
91                 $text = '<br />' . $text;
92         }
93
94         echo sprintf($template,$url,$title,$text);
95         killme();
96 }