2 if(!function_exists('deletenode')) {
3 function deletenode(&$doc, $node)
5 $xpath = new DomXPath($doc);
6 $list = $xpath->query("//".$node);
7 foreach ($list as $child)
8 $child->parentNode->removeChild($child);
12 function completeurl($url, $scheme) {
13 $urlarr = parse_url($url);
15 if (isset($urlarr["scheme"]))
18 $schemearr = parse_url($scheme);
20 $complete = $schemearr["scheme"]."://".$schemearr["host"];
22 if ($schemearr["port"] != "")
23 $complete .= ":".$schemearr["port"];
25 $complete .= $urlarr["path"];
27 if ($urlarr["query"] != "")
28 $complete .= "?".$urlarr["query"];
30 if ($urlarr["fragment"] != "")
31 $complete .= "#".$urlarr["fragment"];
36 function parseurl_getsiteinfo($url) {
40 curl_setopt($ch, CURLOPT_URL, $url);
41 curl_setopt($ch, CURLOPT_HEADER, 1);
42 curl_setopt($ch, CURLOPT_NOBODY, 0);
43 curl_setopt($ch, CURLOPT_TIMEOUT, 3);
44 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
45 curl_setopt($ch,CURLOPT_USERAGENT,'Opera/9.64(Windows NT 5.1; U; de) Presto/2.1.1');
47 $header = curl_exec($ch);
50 // Fetch the first mentioned charset. Can be in body or header
51 if (preg_match('/charset=(.*?)['."'".'"\s\n]/', $header, $matches))
52 $charset = trim(array_pop($matches));
56 $pos = strpos($header, "\r\n\r\n");
59 $body = trim(substr($header, $pos));
63 $body = mb_convert_encoding($body, "UTF-8", $charset);
64 $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
66 $doc = new DOMDocument();
67 @$doc->loadHTML($body);
69 deletenode($doc, 'style');
70 deletenode($doc, 'script');
71 deletenode($doc, 'option');
72 deletenode($doc, 'h1');
73 deletenode($doc, 'h2');
74 deletenode($doc, 'h3');
75 deletenode($doc, 'h4');
76 deletenode($doc, 'h5');
77 deletenode($doc, 'h6');
78 deletenode($doc, 'ol');
79 deletenode($doc, 'ul');
81 $xpath = new DomXPath($doc);
83 //$list = $xpath->query("head/title");
84 $list = $xpath->query("//title");
85 foreach ($list as $node)
86 $siteinfo["title"] = html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8");
88 //$list = $xpath->query("head/meta[@name]");
89 $list = $xpath->query("//meta[@name]");
90 foreach ($list as $node) {
92 if ($node->attributes->length)
93 foreach ($node->attributes as $attribute)
94 $attr[$attribute->name] = $attribute->value;
96 $attr["content"] = html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8");
98 switch (strtolower($attr["name"])) {
100 $siteinfo["title"] = $attr["content"];
103 $siteinfo["text"] = $attr["content"];
106 $siteinfo["title"] = $attr["content"];
108 case "dc.description":
109 $siteinfo["text"] = $attr["content"];
114 //$list = $xpath->query("head/meta[@property]");
115 $list = $xpath->query("//meta[@property]");
116 foreach ($list as $node) {
118 if ($node->attributes->length)
119 foreach ($node->attributes as $attribute)
120 $attr[$attribute->name] = $attribute->value;
122 $attr["content"] = html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8");
124 switch (strtolower($attr["property"])) {
126 $siteinfo["image"] = $attr["content"];
129 $siteinfo["title"] = $attr["content"];
131 case "og:description":
132 $siteinfo["text"] = $attr["content"];
137 if ($siteinfo["image"] == "") {
138 $list = $xpath->query("//img[@src]");
139 foreach ($list as $node) {
141 if ($node->attributes->length)
142 foreach ($node->attributes as $attribute)
143 $attr[$attribute->name] = $attribute->value;
145 $src = completeurl($attr["src"], $url);
146 $photodata = getimagesize($src);
148 if (($photodata[0] > 150) and ($photodata[1] > 150)) {
149 if ($photodata[0] > 300) {
150 $photodata[1] = round($photodata[1] * (300 / $photodata[0]));
153 if ($photodata[1] > 300) {
154 $photodata[0] = round($photodata[0] * (300 / $photodata[1]));
157 $siteinfo["images"][] = array("src"=>$src,
158 "width"=>$photodata[0],
159 "height"=>$photodata[1]);
164 $src = completeurl($siteinfo["image"], $url);
165 $photodata = getimagesize($src);
167 if (($photodata[0] > 10) and ($photodata[1] > 10))
168 $siteinfo["images"][] = array("src"=>$src,
169 "width"=>$photodata[0],
170 "height"=>$photodata[1]);
173 if ($siteinfo["text"] == "") {
176 $list = $xpath->query("//div[@class='article']");
177 foreach ($list as $node)
178 if (strlen($node->nodeValue) > 40)
179 $text .= " ".trim($node->nodeValue);
182 $list = $xpath->query("//div[@class='content']");
183 foreach ($list as $node)
184 if (strlen($node->nodeValue) > 40)
185 $text .= " ".trim($node->nodeValue);
188 // If none text was found then take the paragraph content
190 $list = $xpath->query("//p");
191 foreach ($list as $node)
192 if (strlen($node->nodeValue) > 40)
193 $text .= " ".trim($node->nodeValue);
197 $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text));
199 while (strpos($text, " "))
200 $text = trim(str_replace(" ", " ", $text));
202 $siteinfo["text"] = html_entity_decode(substr($text,0,350), ENT_QUOTES, "UTF-8").'...';
209 function arr_add_hashes(&$item,$k) {
213 function parse_url_content(&$a) {
219 if(local_user() && intval(get_pconfig(local_user(),'system','plaintext')))
223 $br = (($textmode) ? "\n" : '<br />');
225 if(x($_GET,'binurl'))
226 $url = trim(hex2bin($_GET['binurl']));
228 $url = trim($_GET['url']);
231 $title = strip_tags(trim($_GET['title']));
233 if($_GET['description'])
234 $text = strip_tags(trim($_GET['description']));
237 $arr_tags = str_getcsv($_GET['tags']);
238 if(count($arr_tags)) {
239 array_walk($arr_tags,'arr_add_hashes');
240 $str_tags = $br . implode(' ',$arr_tags) . $br;
244 logger('parse_url: ' . $url);
247 $template = $br . '[bookmark=%s]%s[/bookmark]%s' . $br;
249 $template = "<br /><a class=\"bookmark\" href=\"%s\" >%s</a>%s<br />";
251 $arr = array('url' => $url, 'text' => '');
253 call_hooks('parse_link', $arr);
255 if(strlen($arr['text'])) {
261 if($url && $title && $text) {
264 $text = $br . '[quote]' . trim($text) . '[/quote]' . $br;
266 $text = '<br /><blockquote>' . trim($text) . '</blockquote><br />';
268 $title = str_replace(array("\r","\n"),array('',''),$title);
270 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
272 logger('parse_url (unparsed): returns: ' . $result);
278 $siteinfo = parseurl_getsiteinfo($url);
280 if($siteinfo["title"] == "") {
281 echo print_r($siteinfo, true);
282 //echo sprintf($template,$url,$url,'') . $str_tags;
285 $text = $siteinfo["text"];
286 $title = $siteinfo["title"];
291 foreach ($siteinfo["images"] as $imagedata)
293 $image .= '[img='.$imagedata["width"].'x'.$imagedata["height"].']'.$imagedata["src"].'[/img]';
295 $image .= '<img height="'.$imagedata["height"].'" width="'.$imagedata["width"].'" src="'.$imagedata["src"].'" alt="photo" />';
299 $text = $br.'[quote]'.trim($text).'[/quote]'.$br ;
301 $text = '<br /><blockquote>'.trim($text).'</blockquote><br />';
305 $text = $br.$br.$image.$text;
307 $title = str_replace(array("\r","\n"),array('',''),$title);
309 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
311 logger('parse_url: returns: ' . $result);