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, 1);
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 // if the file is too large then exit
106 if ($curl_info["download_content_length"] > 1000000)
109 // if it isn't a HTML file then exit
110 if (($curl_info["content_type"] != "") AND !strstr(strtolower($curl_info["content_type"]),"html"))
113 // Now fetch the body as well
115 curl_setopt($ch, CURLOPT_URL, $url);
116 curl_setopt($ch, CURLOPT_HEADER, 1);
117 curl_setopt($ch, CURLOPT_NOBODY, 0);
118 curl_setopt($ch, CURLOPT_TIMEOUT, 10);
119 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
120 curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
122 $header = curl_exec($ch);
123 $curl_info = @curl_getinfo($ch);
124 $http_code = $curl_info['http_code'];
127 // Fetch the first mentioned charset. Can be in body or header
129 if (preg_match('/charset=(.*?)['."'".'"\s\n]/', $header, $matches))
130 $charset = trim(trim(trim(array_pop($matches)), ';,'));
135 $pos = strpos($header, "\r\n\r\n");
138 $body = trim(substr($header, $pos));
142 if (($charset != '') AND (strtoupper($charset) != "UTF-8")) {
143 logger("parseurl_getsiteinfo: detected charset ".$charset, LOGGER_DEBUG);
144 //$body = mb_convert_encoding($body, "UTF-8", $charset);
145 $body = iconv($charset, "UTF-8//TRANSLIT", $body);
148 $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
150 $doc = new DOMDocument();
151 @$doc->loadHTML($body);
153 deletenode($doc, 'style');
154 deletenode($doc, 'script');
155 deletenode($doc, 'option');
156 deletenode($doc, 'h1');
157 deletenode($doc, 'h2');
158 deletenode($doc, 'h3');
159 deletenode($doc, 'h4');
160 deletenode($doc, 'h5');
161 deletenode($doc, 'h6');
162 deletenode($doc, 'ol');
163 deletenode($doc, 'ul');
165 $xpath = new DomXPath($doc);
167 $list = $xpath->query("//meta[@content]");
168 foreach ($list as $node) {
170 if ($node->attributes->length)
171 foreach ($node->attributes as $attribute)
172 $attr[$attribute->name] = $attribute->value;
174 if (@$attr["http-equiv"] == 'refresh') {
175 $path = $attr["content"];
176 $pathinfo = explode(";", $path);
178 foreach ($pathinfo AS $value) {
179 if (substr(strtolower($value), 0, 4) == "url=")
180 $content = substr($value, 4);
182 if ($content != "") {
183 $siteinfo = parseurl_getsiteinfo($content, $no_guessing, $do_oembed, ++$count);
189 //$list = $xpath->query("head/title");
190 $list = $xpath->query("//title");
191 foreach ($list as $node)
192 $siteinfo["title"] = html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8");
194 //$list = $xpath->query("head/meta[@name]");
195 $list = $xpath->query("//meta[@name]");
196 foreach ($list as $node) {
198 if ($node->attributes->length)
199 foreach ($node->attributes as $attribute)
200 $attr[$attribute->name] = $attribute->value;
202 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
204 if ($attr["content"] != "")
205 switch (strtolower($attr["name"])) {
207 $siteinfo["title"] = $attr["content"];
210 $siteinfo["text"] = $attr["content"];
213 $siteinfo["image"] = $attr["content"];
215 case "twitter:image":
216 $siteinfo["image"] = $attr["content"];
218 case "twitter:image:src":
219 $siteinfo["image"] = $attr["content"];
222 if (($siteinfo["type"] == "") OR ($attr["content"] == "photo"))
223 $siteinfo["type"] = $attr["content"];
225 case "twitter:description":
226 $siteinfo["text"] = $attr["content"];
228 case "twitter:title":
229 $siteinfo["title"] = $attr["content"];
232 $siteinfo["title"] = $attr["content"];
234 case "dc.description":
235 $siteinfo["text"] = $attr["content"];
238 $keywords = explode(",", $attr["content"]);
240 case "news_keywords":
241 $keywords = explode(",", $attr["content"]);
244 if ($siteinfo["type"] == "summary")
245 $siteinfo["type"] = "link";
248 if (isset($keywords)) {
249 $siteinfo["keywords"] = array();
250 foreach ($keywords as $keyword)
251 $siteinfo["keywords"][] = trim($keyword);
254 //$list = $xpath->query("head/meta[@property]");
255 $list = $xpath->query("//meta[@property]");
256 foreach ($list as $node) {
258 if ($node->attributes->length)
259 foreach ($node->attributes as $attribute)
260 $attr[$attribute->name] = $attribute->value;
262 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
264 if ($attr["content"] != "")
265 switch (strtolower($attr["property"])) {
267 $siteinfo["image"] = $attr["content"];
270 $siteinfo["title"] = $attr["content"];
272 case "og:description":
273 $siteinfo["text"] = $attr["content"];
278 if (isset($oembed_data) AND ($oembed_data->type == "link") AND ($siteinfo["type"] != "photo")) {
279 if (isset($oembed_data->title) AND (trim($oembed_data->title) != ""))
280 $siteinfo["title"] = $oembed_data->title;
281 if (isset($oembed_data->description) AND (trim($oembed_data->description) != ""))
282 $siteinfo["text"] = trim($oembed_data->description);
283 if (isset($oembed_data->thumbnail_url) AND (trim($oembed_data->thumbnail_url) != ""))
284 $siteinfo["image"] = $oembed_data->thumbnail_url;
287 if ((@$siteinfo["image"] == "") AND !$no_guessing) {
288 $list = $xpath->query("//img[@src]");
289 foreach ($list as $node) {
291 if ($node->attributes->length)
292 foreach ($node->attributes as $attribute)
293 $attr[$attribute->name] = $attribute->value;
295 $src = completeurl($attr["src"], $url);
296 $photodata = @getimagesize($src);
298 if (($photodata) && ($photodata[0] > 150) and ($photodata[1] > 150)) {
299 if ($photodata[0] > 300) {
300 $photodata[1] = round($photodata[1] * (300 / $photodata[0]));
303 if ($photodata[1] > 300) {
304 $photodata[0] = round($photodata[0] * (300 / $photodata[1]));
307 $siteinfo["images"][] = array("src"=>$src,
308 "width"=>$photodata[0],
309 "height"=>$photodata[1]);
314 $src = completeurl($siteinfo["image"], $url);
316 unset($siteinfo["image"]);
318 $photodata = @getimagesize($src);
320 if (($photodata) && ($photodata[0] > 10) and ($photodata[1] > 10))
321 $siteinfo["images"][] = array("src"=>$src,
322 "width"=>$photodata[0],
323 "height"=>$photodata[1]);
326 if ((@$siteinfo["text"] == "") AND (@$siteinfo["title"] != "") AND !$no_guessing) {
329 $list = $xpath->query("//div[@class='article']");
330 foreach ($list as $node)
331 if (strlen($node->nodeValue) > 40)
332 $text .= " ".trim($node->nodeValue);
335 $list = $xpath->query("//div[@class='content']");
336 foreach ($list as $node)
337 if (strlen($node->nodeValue) > 40)
338 $text .= " ".trim($node->nodeValue);
341 // If none text was found then take the paragraph content
343 $list = $xpath->query("//p");
344 foreach ($list as $node)
345 if (strlen($node->nodeValue) > 40)
346 $text .= " ".trim($node->nodeValue);
350 $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text));
352 while (strpos($text, " "))
353 $text = trim(str_replace(" ", " ", $text));
355 $siteinfo["text"] = trim(html_entity_decode(substr($text,0,350), ENT_QUOTES, "UTF-8").'...');
359 logger("parseurl_getsiteinfo: Siteinfo for ".$url." ".print_r($siteinfo, true), LOGGER_DEBUG);
361 call_hooks('getsiteinfo', $siteinfo);
366 function arr_add_hashes(&$item,$k) {
370 function parse_url_content(&$a) {
377 if(local_user() && (! feature_enabled(local_user(),'richtext')))
381 $br = (($textmode) ? "\n" : '<br />');
383 if(x($_GET,'binurl'))
384 $url = trim(hex2bin($_GET['binurl']));
386 $url = trim($_GET['url']);
389 $title = strip_tags(trim($_GET['title']));
391 if($_GET['description'])
392 $text = strip_tags(trim($_GET['description']));
395 $arr_tags = str_getcsv($_GET['tags']);
396 if(count($arr_tags)) {
397 array_walk($arr_tags,'arr_add_hashes');
398 $str_tags = $br . implode(' ',$arr_tags) . $br;
402 logger('parse_url: ' . $url);
405 $template = '[bookmark=%s]%s[/bookmark]%s';
407 $template = "<a class=\"bookmark\" href=\"%s\" >%s</a>%s";
409 $arr = array('url' => $url, 'text' => '');
411 call_hooks('parse_link', $arr);
413 if(strlen($arr['text'])) {
419 if($url && $title && $text) {
421 $title = str_replace(array("\r","\n"),array('',''),$title);
424 $text = '[quote]' . trim($text) . '[/quote]' . $br;
426 $text = '<blockquote>' . htmlspecialchars(trim($text)) . '</blockquote><br />';
427 $title = htmlspecialchars($title);
430 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
432 logger('parse_url (unparsed): returns: ' . $result);
438 $siteinfo = parseurl_getsiteinfo($url);
441 // require_once("include/items.php");
443 // echo add_page_info_data($siteinfo);
447 $url= $siteinfo["url"];
449 // If the link contains BBCode stuff, make a short link out of this to avoid parsing problems
450 if (strpos($url, '[') OR strpos($url, ']')) {
451 require_once("include/network.php");
452 $url = short_link($url);
457 if($siteinfo["title"] == "") {
458 $sitedata .= sprintf($template,$url,$url,'') . $str_tags;
461 $text = $siteinfo["text"];
462 $title = $siteinfo["title"];
467 if (($siteinfo["type"] != "video") AND (sizeof($siteinfo["images"]) > 0)){
468 /* Execute below code only if image is present in siteinfo */
471 $max_images = get_config('system','max_bookmark_images');
472 if($max_images === false)
475 $max_images = intval($max_images);
477 foreach ($siteinfo["images"] as $imagedata) {
479 $image .= '[img='.$imagedata["width"].'x'.$imagedata["height"].']'.$imagedata["src"].'[/img]' . "\n";
481 $image .= '<img height="'.$imagedata["height"].'" width="'.$imagedata["width"].'" src="'.$imagedata["src"].'" alt="photo" /><br />';
483 if($max_images && $max_images >= $total_images)
490 $text = '[quote]'.trim($text).'[/quote]';
492 $text = '<blockquote>'.htmlspecialchars(trim($text)).'</blockquote>';
496 $text = $br.$br.$image.$text;
500 $title = str_replace(array("\r","\n"),array('',''),$title);
502 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
504 logger('parse_url: returns: ' . $result);
506 $sitedata .= trim($result);
508 if (($siteinfo["type"] == "video") AND ($url != ""))
509 echo "[class=type-video]".$sitedata."[/class]";
510 elseif (($siteinfo["type"] != "photo"))
511 echo "[class=type-link]".$sitedata."[/class]";
513 echo "[class=type-photo]".$title.$br.$image."[/class]";