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 parseurl_getsiteinfo($url) {
16 curl_setopt($ch, CURLOPT_URL, $url);
17 curl_setopt($ch, CURLOPT_HEADER, 1);
18 curl_setopt($ch, CURLOPT_NOBODY, 0);
19 curl_setopt($ch, CURLOPT_TIMEOUT, 3);
20 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
21 curl_setopt($ch,CURLOPT_USERAGENT,'Opera/9.64(Windows NT 5.1; U; de) Presto/2.1.1');
23 $header = curl_exec($ch);
26 if (preg_match('/charset=(.*?)\n/', $header, $matches))
27 $charset = trim(array_pop($matches));
31 $pos = strpos($header, "\r\n\r\n");
34 $body = trim(substr($header, $pos));
38 $body = mb_convert_encoding($body, "UTF-8", $charset);
39 $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
41 $doc = new DOMDocument();
42 @$doc->loadHTML($body);
44 deletenode($doc, 'style');
45 deletenode($doc, 'script');
46 deletenode($doc, 'option');
47 deletenode($doc, 'h1');
48 deletenode($doc, 'h2');
49 deletenode($doc, 'h3');
50 deletenode($doc, 'h4');
51 deletenode($doc, 'h5');
52 deletenode($doc, 'h6');
53 deletenode($doc, 'ol');
54 deletenode($doc, 'ul');
56 $xpath = new DomXPath($doc);
58 $list = $xpath->query("head/title");
59 foreach ($list as $node)
60 $siteinfo["title"] = html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8");
62 $list = $xpath->query("head/meta[@name]");
63 foreach ($list as $node) {
65 if ($node->attributes->length)
66 foreach ($node->attributes as $attribute)
67 $attr[$attribute->name] = $attribute->value;
69 $attr["content"] = html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8");
71 switch (strtolower($attr["name"])) {
73 $siteinfo["title"] = $attr["content"];
76 $siteinfo["text"] = $attr["content"];
79 $siteinfo["title"] = $attr["content"];
81 case "dc.description":
82 $siteinfo["text"] = $attr["content"];
87 $list = $xpath->query("head/meta[@property]");
88 foreach ($list as $node) {
90 if ($node->attributes->length)
91 foreach ($node->attributes as $attribute)
92 $attr[$attribute->name] = $attribute->value;
94 $attr["content"] = html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8");
96 switch (strtolower($attr["property"])) {
98 $siteinfo["image"] = $attr["content"];
101 $siteinfo["title"] = $attr["content"];
103 case "og:description":
104 $siteinfo["text"] = $attr["content"];
109 if ($siteinfo["image"] == "") {
110 require_once('include/Photo.php');
111 $list = $xpath->query("//img[@src]");
112 foreach ($list as $node) {
114 if ($node->attributes->length)
115 foreach ($node->attributes as $attribute)
116 $attr[$attribute->name] = $attribute->value;
118 // guess mimetype from headers or filename
119 $type = guess_image_type($attr["src"],true);
121 $i = fetch_url($attr["src"]);
122 $ph = new Photo($i, $type);
124 if(($ph->getWidth() > 200) and ($ph->getHeight() > 200))
125 $siteinfo["image"] = $attr["src"];
129 if ($siteinfo["text"] == "") {
132 $list = $xpath->query("//div[@class='article']");
133 foreach ($list as $node)
134 $text .= " ".trim($node->nodeValue);
137 $list = $xpath->query("//div[@class='content']");
138 foreach ($list as $node)
139 $text .= " ".trim($node->nodeValue);
142 // If none text was found then take the paragraph content
144 $list = $xpath->query("//p");
145 foreach ($list as $node)
146 $text .= " ".trim($node->nodeValue);
150 $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text));
152 while (strpos($text, " "))
153 $text = trim(str_replace(" ", " ", $text));
155 $siteinfo["text"] = html_entity_decode(substr($text,0,350), ENT_QUOTES, "UTF-8").'...';
162 function arr_add_hashes(&$item,$k) {
166 function parse_url_content(&$a) {
172 if(local_user() && intval(get_pconfig(local_user(),'system','plaintext')))
176 $br = (($textmode) ? "\n" : '<br /?');
178 if(x($_GET,'binurl'))
179 $url = trim(hex2bin($_GET['binurl']));
181 $url = trim($_GET['url']);
184 $title = strip_tags(trim($_GET['title']));
186 if($_GET['description'])
187 $text = strip_tags(trim($_GET['description']));
190 $arr_tags = str_getcsv($_GET['tags']);
191 if(count($arr_tags)) {
192 array_walk($arr_tags,'arr_add_hashes');
193 $str_tags = $br . implode(' ',$arr_tags) . $br;
197 logger('parse_url: ' . $url);
200 $template = $br . '[bookmark=%s]%s[/bookmark]%s' . $br;
202 $template = "<br /><a class=\"bookmark\" href=\"%s\" >%s</a>%s<br />";
204 $arr = array('url' => $url, 'text' => '');
206 call_hooks('parse_link', $arr);
208 if(strlen($arr['text'])) {
214 if($url && $title && $text) {
217 $text = $br . $br . '[quote]' . trim($text) . '[/quote]' . $br;
219 $text = '<br /><br /><blockquote>' . trim($text) . '</blockquote><br />';
221 $title = str_replace(array("\r","\n"),array('',''),$title);
223 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
225 logger('parse_url (unparsed): returns: ' . $result);
231 $siteinfo = parseurl_getsiteinfo($url);
233 if($siteinfo["title"] == "") {
234 echo sprintf($template,$url,$url,'') . $str_tags;
237 $image = $siteinfo["image"];
238 $text = $siteinfo["text"];
239 $title = $siteinfo["title"];
243 $i = fetch_url($image);
245 require_once('include/Photo.php');
246 // guess mimetype from headers or filename
247 $type = guess_image_type($image,true);
249 $ph = new Photo($i, $type);
250 if($ph->is_valid()) {
251 if($ph->getWidth() > 300 || $ph->getHeight() > 300) {
252 $ph->scaleImage(300);
253 $new_width = $ph->getWidth();
254 $new_height = $ph->getHeight();
256 $image = $br . $br . '[img=' . $new_width . 'x' . $new_height . ']' . $image . '[/img]';
258 $image = '<br /><br /><img height="' . $new_height . '" width="' . $new_width . '" src="' .$image . '" alt="photo" />';
261 $image = $br.$br.'[img]'.$image.'[/img]';
263 $image = '<br /><br /><img src="'.$image.'" alt="photo" />';
271 $text = $br.$br.'[quote]'.trim($text).'[/quote]'.$br ;
273 $text = '<br /><br /><blockquote>'.trim($text).'</blockquote><br />';
277 $text = $image.$br.$text;
279 $title = str_replace(array("\r","\n"),array('',''),$title);
281 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
283 logger('parse_url: returns: ' . $result);