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