2 require_once('include/Photo.php');
4 if(!function_exists('deletenode')) {
5 function deletenode(&$doc, $node)
7 $xpath = new DomXPath($doc);
8 $list = $xpath->query("//".$node);
9 foreach ($list as $child)
10 $child->parentNode->removeChild($child);
14 function parseurl_getsiteinfo($url) {
18 curl_setopt($ch, CURLOPT_URL, $url);
19 curl_setopt($ch, CURLOPT_HEADER, 1);
20 curl_setopt($ch, CURLOPT_NOBODY, 0);
21 curl_setopt($ch, CURLOPT_TIMEOUT, 3);
22 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
23 curl_setopt($ch,CURLOPT_USERAGENT,'Opera/9.64(Windows NT 5.1; U; de) Presto/2.1.1');
25 $header = curl_exec($ch);
28 if (preg_match('/charset=(.*?)\n/', $header, $matches))
29 $charset = trim(array_pop($matches));
33 $pos = strpos($header, "\r\n\r\n");
36 $body = trim(substr($header, $pos));
40 $body = mb_convert_encoding($body, "UTF-8", $charset);
41 $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
43 $doc = new DOMDocument();
44 @$doc->loadHTML($body);
46 deletenode($doc, 'style');
47 deletenode($doc, 'script');
48 deletenode($doc, 'option');
49 deletenode($doc, 'h1');
50 deletenode($doc, 'h2');
51 deletenode($doc, 'h3');
52 deletenode($doc, 'h4');
53 deletenode($doc, 'h5');
54 deletenode($doc, 'h6');
55 deletenode($doc, 'ol');
56 deletenode($doc, 'ul');
58 $xpath = new DomXPath($doc);
60 $list = $xpath->query("head/title");
61 foreach ($list as $node)
62 $siteinfo["title"] = html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8");
64 $list = $xpath->query("head/meta[@name]");
65 foreach ($list as $node) {
67 if ($node->attributes->length)
68 foreach ($node->attributes as $attribute)
69 $attr[$attribute->name] = $attribute->value;
71 $attr["content"] = html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8");
73 switch (strtolower($attr["name"])) {
75 $siteinfo["title"] = $attr["content"];
78 $siteinfo["text"] = $attr["content"];
81 $siteinfo["title"] = $attr["content"];
83 case "dc.description":
84 $siteinfo["text"] = $attr["content"];
89 $list = $xpath->query("head/meta[@property]");
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["property"])) {
100 $siteinfo["image"] = $attr["content"];
103 $siteinfo["title"] = $attr["content"];
105 case "og:description":
106 $siteinfo["text"] = $attr["content"];
111 if ($siteinfo["image"] == "") {
112 $list = $xpath->query("//img[@src]");
113 foreach ($list as $node) {
115 if ($node->attributes->length)
116 foreach ($node->attributes as $attribute)
117 $attr[$attribute->name] = $attribute->value;
119 // guess mimetype from headers or filename
120 $type = guess_image_type($attr["src"],true);
122 $i = fetch_url($attr["src"]);
123 $ph = new Photo($i, $type);
125 if($ph->is_valid() and ($ph->getWidth() > 200) and ($ph->getHeight() > 200)) {
126 if ($siteinfo["image"] == "")
127 $siteinfo["image"] = $attr["src"];
129 if($ph->getWidth() > 300 || $ph->getHeight() > 300) {
130 $ph->scaleImage(300);
131 $siteinfo["images"][] = array("src"=>$attr["src"],
132 "width"=>$ph->getWidth(),
133 "height"=>$ph->getHeight());
135 $siteinfo["images"][] = array("src"=>$attr["src"],
136 "width"=>$ph->getWidth(),
137 "height"=>$ph->getHeight());
141 // guess mimetype from headers or filename
142 $type = guess_image_type($siteinfo["image"],true);
144 $i = fetch_url($siteinfo["image"]);
145 $ph = new Photo($i, $type);
148 $siteinfo["images"][] = array("src"=>$siteinfo["image"],
149 "width"=>$ph->getWidth(),
150 "height"=>$ph->getHeight());
153 if ($siteinfo["text"] == "") {
156 $list = $xpath->query("//div[@class='article']");
157 foreach ($list as $node)
158 $text .= " ".trim($node->nodeValue);
161 $list = $xpath->query("//div[@class='content']");
162 foreach ($list as $node)
163 $text .= " ".trim($node->nodeValue);
166 // If none text was found then take the paragraph content
168 $list = $xpath->query("//p");
169 foreach ($list as $node)
170 $text .= " ".trim($node->nodeValue);
174 $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text));
176 while (strpos($text, " "))
177 $text = trim(str_replace(" ", " ", $text));
179 $siteinfo["text"] = html_entity_decode(substr($text,0,350), ENT_QUOTES, "UTF-8").'...';
186 function arr_add_hashes(&$item,$k) {
190 function parse_url_content(&$a) {
196 if(local_user() && intval(get_pconfig(local_user(),'system','plaintext')))
200 $br = (($textmode) ? "\n" : '<br />');
202 if(x($_GET,'binurl'))
203 $url = trim(hex2bin($_GET['binurl']));
205 $url = trim($_GET['url']);
208 $title = strip_tags(trim($_GET['title']));
210 if($_GET['description'])
211 $text = strip_tags(trim($_GET['description']));
214 $arr_tags = str_getcsv($_GET['tags']);
215 if(count($arr_tags)) {
216 array_walk($arr_tags,'arr_add_hashes');
217 $str_tags = $br . implode(' ',$arr_tags) . $br;
221 logger('parse_url: ' . $url);
224 $template = $br . '[bookmark=%s]%s[/bookmark]%s' . $br;
226 $template = "<br /><a class=\"bookmark\" href=\"%s\" >%s</a>%s<br />";
228 $arr = array('url' => $url, 'text' => '');
230 call_hooks('parse_link', $arr);
232 if(strlen($arr['text'])) {
238 if($url && $title && $text) {
241 $text = $br . $br . '[quote]' . trim($text) . '[/quote]' . $br;
243 $text = '<br /><br /><blockquote>' . trim($text) . '</blockquote><br />';
245 $title = str_replace(array("\r","\n"),array('',''),$title);
247 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
249 logger('parse_url (unparsed): returns: ' . $result);
255 $siteinfo = parseurl_getsiteinfo($url);
257 if($siteinfo["title"] == "") {
258 echo sprintf($template,$url,$url,'') . $str_tags;
261 $text = $siteinfo["text"];
262 $title = $siteinfo["title"];
267 foreach ($siteinfo["images"] as $imagedata)
269 $image .= '[img='.$imagedata["width"].'x'.$imagedata["height"].']'.$imagedata["src"].'[/img]';
271 $image .= '<img height="'.$imagedata["height"].'" width="'.$imagedata["width"].'" src="'.$imagedata["src"].'" alt="photo" />';
273 /* if ($image != "") {
274 $i = fetch_url($image);
276 require_once('include/Photo.php');
277 // guess mimetype from headers or filename
278 $type = guess_image_type($image,true);
280 $ph = new Photo($i, $type);
281 if($ph->is_valid()) {
282 if($ph->getWidth() > 300 || $ph->getHeight() > 300) {
283 $ph->scaleImage(300);
284 $new_width = $ph->getWidth();
285 $new_height = $ph->getHeight();
287 $image = $br . $br . '[img=' . $new_width . 'x' . $new_height . ']' . $image . '[/img]';
289 $image = '<br /><br /><img height="' . $new_height . '" width="' . $new_width . '" src="' .$image . '" alt="photo" />';
292 $image = $br.$br.'[img]'.$image.'[/img]';
294 $image = '<br /><br /><img src="'.$image.'" alt="photo" />';
302 $text = $br.'[quote]'.trim($text).'[/quote]'.$br ;
304 $text = '<br /><blockquote>'.trim($text).'</blockquote><br />';
308 $text = $br.$br.$image.$br.$text;
310 $title = str_replace(array("\r","\n"),array('',''),$title);
312 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
314 logger('parse_url: returns: ' . $result);