3 https://developers.google.com/+/plugins/snippet/
5 <meta itemprop="name" content="Toller Titel">
6 <meta itemprop="description" content="Eine tolle Beschreibung">
7 <meta itemprop="image" content="http://maple.libertreeproject.org/images/tree-icon.png">
9 <body itemscope itemtype="http://schema.org/Product">
10 <h1 itemprop="name">Shiny Trinket</h1>
11 <img itemprop="image" src="{image-url}" />
12 <p itemprop="description">Shiny trinkets are shiny.</p>
16 if(!function_exists('deletenode')) {
17 function deletenode(&$doc, $node)
19 $xpath = new DomXPath($doc);
20 $list = $xpath->query("//".$node);
21 foreach ($list as $child)
22 $child->parentNode->removeChild($child);
26 function completeurl($url, $scheme) {
27 $urlarr = parse_url($url);
29 if (isset($urlarr["scheme"]))
32 $schemearr = parse_url($scheme);
34 $complete = $schemearr["scheme"]."://".$schemearr["host"];
36 if (@$schemearr["port"] != "")
37 $complete .= ":".$schemearr["port"];
39 if(strpos($urlarr['path'],'/') !== 0)
42 $complete .= $urlarr["path"];
44 if (@$urlarr["query"] != "")
45 $complete .= "?".$urlarr["query"];
47 if (@$urlarr["fragment"] != "")
48 $complete .= "#".$urlarr["fragment"];
53 function parseurl_getsiteinfo($url, $no_guessing = false, $do_oembed = true, $count = 1) {
54 require_once("include/network.php");
61 logger("parseurl_getsiteinfo: Endless loop detected for ".$url, LOGGER_DEBUG);
65 $url = trim($url, "'");
66 $url = trim($url, '"');
68 $url = original_url($url);
70 $siteinfo["url"] = $url;
71 $siteinfo["type"] = "link";
74 curl_setopt($ch, CURLOPT_URL, $url);
75 curl_setopt($ch, CURLOPT_HEADER, 1);
76 curl_setopt($ch, CURLOPT_NOBODY, 0);
77 curl_setopt($ch, CURLOPT_TIMEOUT, 3);
78 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
79 //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
80 curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
82 $header = curl_exec($ch);
83 $curl_info = @curl_getinfo($ch);
84 $http_code = $curl_info['http_code'];
87 if ((($curl_info['http_code'] == "301") OR ($curl_info['http_code'] == "302") OR ($curl_info['http_code'] == "303") OR ($curl_info['http_code'] == "307"))
88 AND (($curl_info['redirect_url'] != "") OR ($curl_info['location'] != ""))) {
89 if ($curl_info['redirect_url'] != "")
90 $siteinfo = parseurl_getsiteinfo($curl_info['redirect_url'], $no_guessing, $do_oembed, ++$count);
92 $siteinfo = parseurl_getsiteinfo($curl_info['location'], $no_guessing, $do_oembed, ++$count);
97 require_once("include/oembed.php");
99 $oembed_data = oembed_fetch_url($url);
101 if ($oembed_data->type != "error")
102 $siteinfo["type"] = $oembed_data->type;
105 // Fetch the first mentioned charset. Can be in body or header
107 if (preg_match('/charset=(.*?)['."'".'"\s\n]/', $header, $matches))
108 $charset = trim(trim(trim(array_pop($matches)), ';,'));
113 $pos = strpos($header, "\r\n\r\n");
116 $body = trim(substr($header, $pos));
120 if (($charset != '') AND (strtoupper($charset) != "UTF-8")) {
121 logger("parseurl_getsiteinfo: detected charset ".$charset, LOGGER_DEBUG);
122 //$body = mb_convert_encoding($body, "UTF-8", $charset);
123 $body = iconv($charset, "UTF-8//TRANSLIT", $body);
126 $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
128 $doc = new DOMDocument();
129 @$doc->loadHTML($body);
131 deletenode($doc, 'style');
132 deletenode($doc, 'script');
133 deletenode($doc, 'option');
134 deletenode($doc, 'h1');
135 deletenode($doc, 'h2');
136 deletenode($doc, 'h3');
137 deletenode($doc, 'h4');
138 deletenode($doc, 'h5');
139 deletenode($doc, 'h6');
140 deletenode($doc, 'ol');
141 deletenode($doc, 'ul');
143 $xpath = new DomXPath($doc);
145 $list = $xpath->query("//meta[@content]");
146 foreach ($list as $node) {
148 if ($node->attributes->length)
149 foreach ($node->attributes as $attribute)
150 $attr[$attribute->name] = $attribute->value;
152 if (@$attr["http-equiv"] == 'refresh') {
153 $path = $attr["content"];
154 $pathinfo = explode(";", $path);
156 foreach ($pathinfo AS $value) {
157 if (substr(strtolower($value), 0, 4) == "url=")
158 $content = substr($value, 4);
160 if ($content != "") {
161 $siteinfo = parseurl_getsiteinfo($content, $no_guessing, $do_oembed, ++$count);
167 //$list = $xpath->query("head/title");
168 $list = $xpath->query("//title");
169 foreach ($list as $node)
170 $siteinfo["title"] = html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8");
172 //$list = $xpath->query("head/meta[@name]");
173 $list = $xpath->query("//meta[@name]");
174 foreach ($list as $node) {
176 if ($node->attributes->length)
177 foreach ($node->attributes as $attribute)
178 $attr[$attribute->name] = $attribute->value;
180 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
182 if ($attr["content"] != "")
183 switch (strtolower($attr["name"])) {
185 $siteinfo["title"] = $attr["content"];
188 $siteinfo["text"] = $attr["content"];
191 $siteinfo["image"] = $attr["content"];
193 case "twitter:image":
194 $siteinfo["image"] = $attr["content"];
196 case "twitter:image:src":
197 $siteinfo["image"] = $attr["content"];
200 if (($siteinfo["type"] == "") OR ($attr["content"] == "photo"))
201 $siteinfo["type"] = $attr["content"];
203 case "twitter:description":
204 $siteinfo["text"] = $attr["content"];
206 case "twitter:title":
207 $siteinfo["title"] = $attr["content"];
210 $siteinfo["title"] = $attr["content"];
212 case "dc.description":
213 $siteinfo["text"] = $attr["content"];
216 $keywords = explode(",", $attr["content"]);
218 case "news_keywords":
219 $keywords = explode(",", $attr["content"]);
222 if ($siteinfo["type"] == "summary")
223 $siteinfo["type"] = "link";
226 if (isset($keywords)) {
227 $siteinfo["keywords"] = array();
228 foreach ($keywords as $keyword)
229 $siteinfo["keywords"][] = trim($keyword);
232 //$list = $xpath->query("head/meta[@property]");
233 $list = $xpath->query("//meta[@property]");
234 foreach ($list as $node) {
236 if ($node->attributes->length)
237 foreach ($node->attributes as $attribute)
238 $attr[$attribute->name] = $attribute->value;
240 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
242 if ($attr["content"] != "")
243 switch (strtolower($attr["property"])) {
245 $siteinfo["image"] = $attr["content"];
248 $siteinfo["title"] = $attr["content"];
250 case "og:description":
251 $siteinfo["text"] = $attr["content"];
256 if (isset($oembed_data) AND ($oembed_data->type == "link") AND ($siteinfo["type"] != "photo")) {
257 if (isset($oembed_data->title) AND (trim($oembed_data->title) != ""))
258 $siteinfo["title"] = $oembed_data->title;
259 if (isset($oembed_data->description) AND (trim($oembed_data->description) != ""))
260 $siteinfo["text"] = trim($oembed_data->description);
261 if (isset($oembed_data->thumbnail_url) AND (trim($oembed_data->thumbnail_url) != ""))
262 $siteinfo["image"] = $oembed_data->thumbnail_url;
265 if ((@$siteinfo["image"] == "") AND !$no_guessing) {
266 $list = $xpath->query("//img[@src]");
267 foreach ($list as $node) {
269 if ($node->attributes->length)
270 foreach ($node->attributes as $attribute)
271 $attr[$attribute->name] = $attribute->value;
273 $src = completeurl($attr["src"], $url);
274 $photodata = @getimagesize($src);
276 if (($photodata) && ($photodata[0] > 150) and ($photodata[1] > 150)) {
277 if ($photodata[0] > 300) {
278 $photodata[1] = round($photodata[1] * (300 / $photodata[0]));
281 if ($photodata[1] > 300) {
282 $photodata[0] = round($photodata[0] * (300 / $photodata[1]));
285 $siteinfo["images"][] = array("src"=>$src,
286 "width"=>$photodata[0],
287 "height"=>$photodata[1]);
292 $src = completeurl($siteinfo["image"], $url);
294 unset($siteinfo["image"]);
296 $photodata = @getimagesize($src);
298 if (($photodata) && ($photodata[0] > 10) and ($photodata[1] > 10))
299 $siteinfo["images"][] = array("src"=>$src,
300 "width"=>$photodata[0],
301 "height"=>$photodata[1]);
304 if ((@$siteinfo["text"] == "") AND (@$siteinfo["title"] != "") AND !$no_guessing) {
307 $list = $xpath->query("//div[@class='article']");
308 foreach ($list as $node)
309 if (strlen($node->nodeValue) > 40)
310 $text .= " ".trim($node->nodeValue);
313 $list = $xpath->query("//div[@class='content']");
314 foreach ($list as $node)
315 if (strlen($node->nodeValue) > 40)
316 $text .= " ".trim($node->nodeValue);
319 // If none text was found then take the paragraph content
321 $list = $xpath->query("//p");
322 foreach ($list as $node)
323 if (strlen($node->nodeValue) > 40)
324 $text .= " ".trim($node->nodeValue);
328 $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text));
330 while (strpos($text, " "))
331 $text = trim(str_replace(" ", " ", $text));
333 $siteinfo["text"] = trim(html_entity_decode(substr($text,0,350), ENT_QUOTES, "UTF-8").'...');
337 logger("parseurl_getsiteinfo: Siteinfo for ".$url." ".print_r($siteinfo, true), LOGGER_DEBUG);
339 call_hooks('getsiteinfo', $siteinfo);
344 function arr_add_hashes(&$item,$k) {
348 function parse_url_content(&$a) {
355 if(local_user() && (! feature_enabled(local_user(),'richtext')))
359 $br = (($textmode) ? "\n" : '<br />');
361 if(x($_GET,'binurl'))
362 $url = trim(hex2bin($_GET['binurl']));
364 $url = trim($_GET['url']);
367 $title = strip_tags(trim($_GET['title']));
369 if($_GET['description'])
370 $text = strip_tags(trim($_GET['description']));
373 $arr_tags = str_getcsv($_GET['tags']);
374 if(count($arr_tags)) {
375 array_walk($arr_tags,'arr_add_hashes');
376 $str_tags = $br . implode(' ',$arr_tags) . $br;
380 logger('parse_url: ' . $url);
383 $template = '[bookmark=%s]%s[/bookmark]%s';
385 $template = "<a class=\"bookmark\" href=\"%s\" >%s</a>%s";
387 $arr = array('url' => $url, 'text' => '');
389 call_hooks('parse_link', $arr);
391 if(strlen($arr['text'])) {
397 if($url && $title && $text) {
399 $title = str_replace(array("\r","\n"),array('',''),$title);
402 $text = '[quote]' . trim($text) . '[/quote]' . $br;
404 $text = '<blockquote>' . htmlspecialchars(trim($text)) . '</blockquote><br />';
405 $title = htmlspecialchars($title);
408 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
410 logger('parse_url (unparsed): returns: ' . $result);
416 $siteinfo = parseurl_getsiteinfo($url);
419 // require_once("include/items.php");
421 // echo add_page_info_data($siteinfo);
425 $url= $siteinfo["url"];
427 // If the link contains BBCode stuff, make a short link out of this to avoid parsing problems
428 if (strpos($url, '[') OR strpos($url, ']')) {
429 require_once("include/network.php");
430 $url = short_link($url);
435 if($siteinfo["title"] == "") {
436 $sitedata .= sprintf($template,$url,$url,'') . $str_tags;
439 $text = $siteinfo["text"];
440 $title = $siteinfo["title"];
445 if (($siteinfo["type"] != "video") AND (sizeof($siteinfo["images"]) > 0)){
446 /* Execute below code only if image is present in siteinfo */
449 $max_images = get_config('system','max_bookmark_images');
450 if($max_images === false)
453 $max_images = intval($max_images);
455 foreach ($siteinfo["images"] as $imagedata) {
457 $image .= '[img='.$imagedata["width"].'x'.$imagedata["height"].']'.$imagedata["src"].'[/img]' . "\n";
459 $image .= '<img height="'.$imagedata["height"].'" width="'.$imagedata["width"].'" src="'.$imagedata["src"].'" alt="photo" /><br />';
461 if($max_images && $max_images >= $total_images)
468 $text = '[quote]'.trim($text).'[/quote]';
470 $text = '<blockquote>'.htmlspecialchars(trim($text)).'</blockquote>';
474 $text = $br.$br.$image.$text;
478 $title = str_replace(array("\r","\n"),array('',''),$title);
480 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
482 logger('parse_url: returns: ' . $result);
484 $sitedata .= trim($result);
486 if (($siteinfo["type"] == "video") AND ($url != ""))
487 echo "[class=type-video]".$sitedata."[/class]";
488 elseif (($siteinfo["type"] != "photo"))
489 echo "[class=type-link]".$sitedata."[/class]";
491 echo "[class=type-photo]".$title.$br.$image."[/class]";