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_cached($url, $no_guessing = false, $do_oembed = true) {
55 $data = Cache::get("parse_url:".$no_guessing.":".$do_oembed.":".$url);
56 if (!is_null($data)) {
57 $data = unserialize($data);
61 $data = parseurl_getsiteinfo($url, $no_guessing, $do_oembed);
63 Cache::set("parse_url:".$no_guessing.":".$do_oembed.":".$url,serialize($data), CACHE_DAY);
68 function parseurl_getsiteinfo($url, $no_guessing = false, $do_oembed = true, $count = 1) {
69 require_once("include/network.php");
76 logger("parseurl_getsiteinfo: Endless loop detected for ".$url, LOGGER_DEBUG);
80 $url = trim($url, "'");
81 $url = trim($url, '"');
83 $url = original_url($url);
85 $siteinfo["url"] = $url;
86 $siteinfo["type"] = "link";
88 $stamp1 = microtime(true);
91 curl_setopt($ch, CURLOPT_URL, $url);
92 curl_setopt($ch, CURLOPT_HEADER, 1);
93 curl_setopt($ch, CURLOPT_NOBODY, 1);
94 curl_setopt($ch, CURLOPT_TIMEOUT, 3);
95 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
96 //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
97 curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
99 $header = curl_exec($ch);
100 $curl_info = @curl_getinfo($ch);
101 $http_code = $curl_info['http_code'];
104 $a->save_timestamp($stamp1, "network");
106 if ((($curl_info['http_code'] == "301") OR ($curl_info['http_code'] == "302") OR ($curl_info['http_code'] == "303") OR ($curl_info['http_code'] == "307"))
107 AND (($curl_info['redirect_url'] != "") OR ($curl_info['location'] != ""))) {
108 if ($curl_info['redirect_url'] != "")
109 $siteinfo = parseurl_getsiteinfo($curl_info['redirect_url'], $no_guessing, $do_oembed, ++$count);
111 $siteinfo = parseurl_getsiteinfo($curl_info['location'], $no_guessing, $do_oembed, ++$count);
115 // if the file is too large then exit
116 if ($curl_info["download_content_length"] > 1000000)
119 // if it isn't a HTML file then exit
120 if (($curl_info["content_type"] != "") AND !strstr(strtolower($curl_info["content_type"]),"html"))
124 require_once("include/oembed.php");
126 $oembed_data = oembed_fetch_url($url);
128 if ($oembed_data->type != "error")
129 $siteinfo["type"] = $oembed_data->type;
131 if (($oembed_data->type == "link") AND ($siteinfo["type"] != "photo")) {
132 if (isset($oembed_data->title))
133 $siteinfo["title"] = $oembed_data->title;
134 if (isset($oembed_data->description))
135 $siteinfo["text"] = trim($oembed_data->description);
136 if (isset($oembed_data->thumbnail_url))
137 $siteinfo["image"] = $oembed_data->thumbnail_url;
141 $stamp1 = microtime(true);
143 // Now fetch the body as well
145 curl_setopt($ch, CURLOPT_URL, $url);
146 curl_setopt($ch, CURLOPT_HEADER, 1);
147 curl_setopt($ch, CURLOPT_NOBODY, 0);
148 curl_setopt($ch, CURLOPT_TIMEOUT, 10);
149 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
150 curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
152 $header = curl_exec($ch);
153 $curl_info = @curl_getinfo($ch);
154 $http_code = $curl_info['http_code'];
157 $a->save_timestamp($stamp1, "network");
159 // Fetch the first mentioned charset. Can be in body or header
161 if (preg_match('/charset=(.*?)['."'".'"\s\n]/', $header, $matches))
162 $charset = trim(trim(trim(array_pop($matches)), ';,'));
167 $pos = strpos($header, "\r\n\r\n");
170 $body = trim(substr($header, $pos));
174 if (($charset != '') AND (strtoupper($charset) != "UTF-8")) {
175 logger("parseurl_getsiteinfo: detected charset ".$charset, LOGGER_DEBUG);
176 //$body = mb_convert_encoding($body, "UTF-8", $charset);
177 $body = iconv($charset, "UTF-8//TRANSLIT", $body);
180 $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
182 $doc = new DOMDocument();
183 @$doc->loadHTML($body);
185 deletenode($doc, 'style');
186 deletenode($doc, 'script');
187 deletenode($doc, 'option');
188 deletenode($doc, 'h1');
189 deletenode($doc, 'h2');
190 deletenode($doc, 'h3');
191 deletenode($doc, 'h4');
192 deletenode($doc, 'h5');
193 deletenode($doc, 'h6');
194 deletenode($doc, 'ol');
195 deletenode($doc, 'ul');
197 $xpath = new DomXPath($doc);
199 $list = $xpath->query("//meta[@content]");
200 foreach ($list as $node) {
202 if ($node->attributes->length)
203 foreach ($node->attributes as $attribute)
204 $attr[$attribute->name] = $attribute->value;
206 if (@$attr["http-equiv"] == 'refresh') {
207 $path = $attr["content"];
208 $pathinfo = explode(";", $path);
210 foreach ($pathinfo AS $value) {
211 if (substr(strtolower($value), 0, 4) == "url=")
212 $content = substr($value, 4);
214 if ($content != "") {
215 $siteinfo = parseurl_getsiteinfo($content, $no_guessing, $do_oembed, ++$count);
221 //$list = $xpath->query("head/title");
222 $list = $xpath->query("//title");
223 foreach ($list as $node)
224 $siteinfo["title"] = html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8");
226 //$list = $xpath->query("head/meta[@name]");
227 $list = $xpath->query("//meta[@name]");
228 foreach ($list as $node) {
230 if ($node->attributes->length)
231 foreach ($node->attributes as $attribute)
232 $attr[$attribute->name] = $attribute->value;
234 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
236 if ($attr["content"] != "")
237 switch (strtolower($attr["name"])) {
239 $siteinfo["title"] = $attr["content"];
242 $siteinfo["text"] = $attr["content"];
245 $siteinfo["image"] = $attr["content"];
247 case "twitter:image":
248 $siteinfo["image"] = $attr["content"];
250 case "twitter:image:src":
251 $siteinfo["image"] = $attr["content"];
254 if (($siteinfo["type"] == "") OR ($attr["content"] == "photo"))
255 $siteinfo["type"] = $attr["content"];
257 case "twitter:description":
258 $siteinfo["text"] = $attr["content"];
260 case "twitter:title":
261 $siteinfo["title"] = $attr["content"];
264 $siteinfo["title"] = $attr["content"];
266 case "dc.description":
267 $siteinfo["text"] = $attr["content"];
270 $keywords = explode(",", $attr["content"]);
272 case "news_keywords":
273 $keywords = explode(",", $attr["content"]);
276 if ($siteinfo["type"] == "summary")
277 $siteinfo["type"] = "link";
280 if (isset($keywords)) {
281 $siteinfo["keywords"] = array();
282 foreach ($keywords as $keyword)
283 $siteinfo["keywords"][] = trim($keyword);
286 //$list = $xpath->query("head/meta[@property]");
287 $list = $xpath->query("//meta[@property]");
288 foreach ($list as $node) {
290 if ($node->attributes->length)
291 foreach ($node->attributes as $attribute)
292 $attr[$attribute->name] = $attribute->value;
294 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
296 if ($attr["content"] != "")
297 switch (strtolower($attr["property"])) {
299 $siteinfo["image"] = $attr["content"];
302 $siteinfo["title"] = $attr["content"];
304 case "og:description":
305 $siteinfo["text"] = $attr["content"];
310 if ((@$siteinfo["image"] == "") AND !$no_guessing) {
311 $list = $xpath->query("//img[@src]");
312 foreach ($list as $node) {
314 if ($node->attributes->length)
315 foreach ($node->attributes as $attribute)
316 $attr[$attribute->name] = $attribute->value;
318 $src = completeurl($attr["src"], $url);
319 $photodata = @getimagesize($src);
321 if (($photodata) && ($photodata[0] > 150) and ($photodata[1] > 150)) {
322 if ($photodata[0] > 300) {
323 $photodata[1] = round($photodata[1] * (300 / $photodata[0]));
326 if ($photodata[1] > 300) {
327 $photodata[0] = round($photodata[0] * (300 / $photodata[1]));
330 $siteinfo["images"][] = array("src"=>$src,
331 "width"=>$photodata[0],
332 "height"=>$photodata[1]);
337 $src = completeurl($siteinfo["image"], $url);
339 unset($siteinfo["image"]);
341 $photodata = @getimagesize($src);
343 if (($photodata) && ($photodata[0] > 10) and ($photodata[1] > 10))
344 $siteinfo["images"][] = array("src"=>$src,
345 "width"=>$photodata[0],
346 "height"=>$photodata[1]);
349 if ((@$siteinfo["text"] == "") AND (@$siteinfo["title"] != "") AND !$no_guessing) {
352 $list = $xpath->query("//div[@class='article']");
353 foreach ($list as $node)
354 if (strlen($node->nodeValue) > 40)
355 $text .= " ".trim($node->nodeValue);
358 $list = $xpath->query("//div[@class='content']");
359 foreach ($list as $node)
360 if (strlen($node->nodeValue) > 40)
361 $text .= " ".trim($node->nodeValue);
364 // If none text was found then take the paragraph content
366 $list = $xpath->query("//p");
367 foreach ($list as $node)
368 if (strlen($node->nodeValue) > 40)
369 $text .= " ".trim($node->nodeValue);
373 $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text));
375 while (strpos($text, " "))
376 $text = trim(str_replace(" ", " ", $text));
378 $siteinfo["text"] = trim(html_entity_decode(substr($text,0,350), ENT_QUOTES, "UTF-8").'...');
382 logger("parseurl_getsiteinfo: Siteinfo for ".$url." ".print_r($siteinfo, true), LOGGER_DEBUG);
384 call_hooks('getsiteinfo', $siteinfo);
389 function arr_add_hashes(&$item,$k) {
393 function parse_url_content(&$a) {
400 if(local_user() && (! feature_enabled(local_user(),'richtext')))
404 $br = (($textmode) ? "\n" : '<br />');
406 if(x($_GET,'binurl'))
407 $url = trim(hex2bin($_GET['binurl']));
409 $url = trim($_GET['url']);
412 $title = strip_tags(trim($_GET['title']));
414 if($_GET['description'])
415 $text = strip_tags(trim($_GET['description']));
418 $arr_tags = str_getcsv($_GET['tags']);
419 if(count($arr_tags)) {
420 array_walk($arr_tags,'arr_add_hashes');
421 $str_tags = $br . implode(' ',$arr_tags) . $br;
425 // add url scheme if missing
426 $arrurl = parse_url($url);
427 if (!x($arrurl, 'scheme')) {
428 if (x($arrurl, 'host'))
431 $url = "http://".$url;
434 logger('parse_url: ' . $url);
437 $template = '[bookmark=%s]%s[/bookmark]%s';
439 $template = "<a class=\"bookmark\" href=\"%s\" >%s</a>%s";
441 $arr = array('url' => $url, 'text' => '');
443 call_hooks('parse_link', $arr);
445 if(strlen($arr['text'])) {
451 if($url && $title && $text) {
453 $title = str_replace(array("\r","\n"),array('',''),$title);
456 $text = '[quote]' . trim($text) . '[/quote]' . $br;
458 $text = '<blockquote>' . htmlspecialchars(trim($text)) . '</blockquote><br />';
459 $title = htmlspecialchars($title);
462 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
464 logger('parse_url (unparsed): returns: ' . $result);
470 $siteinfo = parseurl_getsiteinfo($url);
473 // require_once("include/items.php");
475 // echo add_page_info_data($siteinfo);
479 $url= $siteinfo["url"];
481 // If the link contains BBCode stuff, make a short link out of this to avoid parsing problems
482 if (strpos($url, '[') OR strpos($url, ']')) {
483 require_once("include/network.php");
484 $url = short_link($url);
489 if($siteinfo["title"] != "") {
490 $text = $siteinfo["text"];
491 $title = $siteinfo["title"];
496 if (($siteinfo["type"] != "video") AND (sizeof($siteinfo["images"]) > 0)){
497 /* Execute below code only if image is present in siteinfo */
500 $max_images = get_config('system','max_bookmark_images');
501 if($max_images === false)
504 $max_images = intval($max_images);
506 foreach ($siteinfo["images"] as $imagedata) {
508 $image .= '[img='.$imagedata["width"].'x'.$imagedata["height"].']'.$imagedata["src"].'[/img]' . "\n";
510 $image .= '<img height="'.$imagedata["height"].'" width="'.$imagedata["width"].'" src="'.$imagedata["src"].'" alt="photo" /><br />';
512 if($max_images && $max_images >= $total_images)
519 $text = '[quote]'.trim($text).'[/quote]';
521 $text = '<blockquote>'.htmlspecialchars(trim($text)).'</blockquote>';
525 $text = $br.$br.$image.$text;
529 $title = str_replace(array("\r","\n"),array('',''),$title);
531 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
533 logger('parse_url: returns: ' . $result);
535 $sitedata .= trim($result);
537 if (($siteinfo["type"] == "video") AND ($url != ""))
538 echo "[class=type-video]".$sitedata."[/class]";
539 elseif (($siteinfo["type"] != "photo"))
540 echo "[class=type-link]".$sitedata."[/class]";
542 echo "[class=type-photo]".$title.$br.$image."[/class]";