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";
73 $stamp1 = microtime(true);
76 curl_setopt($ch, CURLOPT_URL, $url);
77 curl_setopt($ch, CURLOPT_HEADER, 1);
78 curl_setopt($ch, CURLOPT_NOBODY, 1);
79 curl_setopt($ch, CURLOPT_TIMEOUT, 3);
80 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
81 //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
82 curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
84 $header = curl_exec($ch);
85 $curl_info = @curl_getinfo($ch);
86 $http_code = $curl_info['http_code'];
89 $a->save_timestamp($stamp1, "network");
91 if ((($curl_info['http_code'] == "301") OR ($curl_info['http_code'] == "302") OR ($curl_info['http_code'] == "303") OR ($curl_info['http_code'] == "307"))
92 AND (($curl_info['redirect_url'] != "") OR ($curl_info['location'] != ""))) {
93 if ($curl_info['redirect_url'] != "")
94 $siteinfo = parseurl_getsiteinfo($curl_info['redirect_url'], $no_guessing, $do_oembed, ++$count);
96 $siteinfo = parseurl_getsiteinfo($curl_info['location'], $no_guessing, $do_oembed, ++$count);
101 require_once("include/oembed.php");
103 $oembed_data = oembed_fetch_url($url);
105 if ($oembed_data->type != "error")
106 $siteinfo["type"] = $oembed_data->type;
109 // if the file is too large then exit
110 if ($curl_info["download_content_length"] > 1000000)
113 // if it isn't a HTML file then exit
114 if (($curl_info["content_type"] != "") AND !strstr(strtolower($curl_info["content_type"]),"html"))
117 $stamp1 = microtime(true);
119 // Now fetch the body as well
121 curl_setopt($ch, CURLOPT_URL, $url);
122 curl_setopt($ch, CURLOPT_HEADER, 1);
123 curl_setopt($ch, CURLOPT_NOBODY, 0);
124 curl_setopt($ch, CURLOPT_TIMEOUT, 10);
125 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
126 curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
128 $header = curl_exec($ch);
129 $curl_info = @curl_getinfo($ch);
130 $http_code = $curl_info['http_code'];
133 $a->save_timestamp($stamp1, "network");
135 // Fetch the first mentioned charset. Can be in body or header
137 if (preg_match('/charset=(.*?)['."'".'"\s\n]/', $header, $matches))
138 $charset = trim(trim(trim(array_pop($matches)), ';,'));
143 $pos = strpos($header, "\r\n\r\n");
146 $body = trim(substr($header, $pos));
150 if (($charset != '') AND (strtoupper($charset) != "UTF-8")) {
151 logger("parseurl_getsiteinfo: detected charset ".$charset, LOGGER_DEBUG);
152 //$body = mb_convert_encoding($body, "UTF-8", $charset);
153 $body = iconv($charset, "UTF-8//TRANSLIT", $body);
156 $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
158 $doc = new DOMDocument();
159 @$doc->loadHTML($body);
161 deletenode($doc, 'style');
162 deletenode($doc, 'script');
163 deletenode($doc, 'option');
164 deletenode($doc, 'h1');
165 deletenode($doc, 'h2');
166 deletenode($doc, 'h3');
167 deletenode($doc, 'h4');
168 deletenode($doc, 'h5');
169 deletenode($doc, 'h6');
170 deletenode($doc, 'ol');
171 deletenode($doc, 'ul');
173 $xpath = new DomXPath($doc);
175 $list = $xpath->query("//meta[@content]");
176 foreach ($list as $node) {
178 if ($node->attributes->length)
179 foreach ($node->attributes as $attribute)
180 $attr[$attribute->name] = $attribute->value;
182 if (@$attr["http-equiv"] == 'refresh') {
183 $path = $attr["content"];
184 $pathinfo = explode(";", $path);
186 foreach ($pathinfo AS $value) {
187 if (substr(strtolower($value), 0, 4) == "url=")
188 $content = substr($value, 4);
190 if ($content != "") {
191 $siteinfo = parseurl_getsiteinfo($content, $no_guessing, $do_oembed, ++$count);
197 //$list = $xpath->query("head/title");
198 $list = $xpath->query("//title");
199 foreach ($list as $node)
200 $siteinfo["title"] = html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8");
202 //$list = $xpath->query("head/meta[@name]");
203 $list = $xpath->query("//meta[@name]");
204 foreach ($list as $node) {
206 if ($node->attributes->length)
207 foreach ($node->attributes as $attribute)
208 $attr[$attribute->name] = $attribute->value;
210 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
212 if ($attr["content"] != "")
213 switch (strtolower($attr["name"])) {
215 $siteinfo["title"] = $attr["content"];
218 $siteinfo["text"] = $attr["content"];
221 $siteinfo["image"] = $attr["content"];
223 case "twitter:image":
224 $siteinfo["image"] = $attr["content"];
226 case "twitter:image:src":
227 $siteinfo["image"] = $attr["content"];
230 if (($siteinfo["type"] == "") OR ($attr["content"] == "photo"))
231 $siteinfo["type"] = $attr["content"];
233 case "twitter:description":
234 $siteinfo["text"] = $attr["content"];
236 case "twitter:title":
237 $siteinfo["title"] = $attr["content"];
240 $siteinfo["title"] = $attr["content"];
242 case "dc.description":
243 $siteinfo["text"] = $attr["content"];
246 $keywords = explode(",", $attr["content"]);
248 case "news_keywords":
249 $keywords = explode(",", $attr["content"]);
252 if ($siteinfo["type"] == "summary")
253 $siteinfo["type"] = "link";
256 if (isset($keywords)) {
257 $siteinfo["keywords"] = array();
258 foreach ($keywords as $keyword)
259 $siteinfo["keywords"][] = trim($keyword);
262 //$list = $xpath->query("head/meta[@property]");
263 $list = $xpath->query("//meta[@property]");
264 foreach ($list as $node) {
266 if ($node->attributes->length)
267 foreach ($node->attributes as $attribute)
268 $attr[$attribute->name] = $attribute->value;
270 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
272 if ($attr["content"] != "")
273 switch (strtolower($attr["property"])) {
275 $siteinfo["image"] = $attr["content"];
278 $siteinfo["title"] = $attr["content"];
280 case "og:description":
281 $siteinfo["text"] = $attr["content"];
286 if (isset($oembed_data) AND ($oembed_data->type == "link") AND ($siteinfo["type"] != "photo")) {
287 if (isset($oembed_data->title) AND (trim($oembed_data->title) != ""))
288 $siteinfo["title"] = $oembed_data->title;
289 if (isset($oembed_data->description) AND (trim($oembed_data->description) != ""))
290 $siteinfo["text"] = trim($oembed_data->description);
291 if (isset($oembed_data->thumbnail_url) AND (trim($oembed_data->thumbnail_url) != ""))
292 $siteinfo["image"] = $oembed_data->thumbnail_url;
295 if ((@$siteinfo["image"] == "") AND !$no_guessing) {
296 $list = $xpath->query("//img[@src]");
297 foreach ($list as $node) {
299 if ($node->attributes->length)
300 foreach ($node->attributes as $attribute)
301 $attr[$attribute->name] = $attribute->value;
303 $src = completeurl($attr["src"], $url);
304 $photodata = @getimagesize($src);
306 if (($photodata) && ($photodata[0] > 150) and ($photodata[1] > 150)) {
307 if ($photodata[0] > 300) {
308 $photodata[1] = round($photodata[1] * (300 / $photodata[0]));
311 if ($photodata[1] > 300) {
312 $photodata[0] = round($photodata[0] * (300 / $photodata[1]));
315 $siteinfo["images"][] = array("src"=>$src,
316 "width"=>$photodata[0],
317 "height"=>$photodata[1]);
322 $src = completeurl($siteinfo["image"], $url);
324 unset($siteinfo["image"]);
326 $photodata = @getimagesize($src);
328 if (($photodata) && ($photodata[0] > 10) and ($photodata[1] > 10))
329 $siteinfo["images"][] = array("src"=>$src,
330 "width"=>$photodata[0],
331 "height"=>$photodata[1]);
334 if ((@$siteinfo["text"] == "") AND (@$siteinfo["title"] != "") AND !$no_guessing) {
337 $list = $xpath->query("//div[@class='article']");
338 foreach ($list as $node)
339 if (strlen($node->nodeValue) > 40)
340 $text .= " ".trim($node->nodeValue);
343 $list = $xpath->query("//div[@class='content']");
344 foreach ($list as $node)
345 if (strlen($node->nodeValue) > 40)
346 $text .= " ".trim($node->nodeValue);
349 // If none text was found then take the paragraph content
351 $list = $xpath->query("//p");
352 foreach ($list as $node)
353 if (strlen($node->nodeValue) > 40)
354 $text .= " ".trim($node->nodeValue);
358 $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text));
360 while (strpos($text, " "))
361 $text = trim(str_replace(" ", " ", $text));
363 $siteinfo["text"] = trim(html_entity_decode(substr($text,0,350), ENT_QUOTES, "UTF-8").'...');
367 logger("parseurl_getsiteinfo: Siteinfo for ".$url." ".print_r($siteinfo, true), LOGGER_DEBUG);
369 call_hooks('getsiteinfo', $siteinfo);
374 function arr_add_hashes(&$item,$k) {
378 function parse_url_content(&$a) {
385 if(local_user() && (! feature_enabled(local_user(),'richtext')))
389 $br = (($textmode) ? "\n" : '<br />');
391 if(x($_GET,'binurl'))
392 $url = trim(hex2bin($_GET['binurl']));
394 $url = trim($_GET['url']);
397 $title = strip_tags(trim($_GET['title']));
399 if($_GET['description'])
400 $text = strip_tags(trim($_GET['description']));
403 $arr_tags = str_getcsv($_GET['tags']);
404 if(count($arr_tags)) {
405 array_walk($arr_tags,'arr_add_hashes');
406 $str_tags = $br . implode(' ',$arr_tags) . $br;
410 // add url scheme if missing
411 $arrurl = parse_url($url);
412 if (!x($arrurl, 'scheme')) {
413 if (x($arrurl, 'host'))
416 $url = "http://".$url;
419 logger('parse_url: ' . $url);
422 $template = '[bookmark=%s]%s[/bookmark]%s';
424 $template = "<a class=\"bookmark\" href=\"%s\" >%s</a>%s";
426 $arr = array('url' => $url, 'text' => '');
428 call_hooks('parse_link', $arr);
430 if(strlen($arr['text'])) {
436 if($url && $title && $text) {
438 $title = str_replace(array("\r","\n"),array('',''),$title);
441 $text = '[quote]' . trim($text) . '[/quote]' . $br;
443 $text = '<blockquote>' . htmlspecialchars(trim($text)) . '</blockquote><br />';
444 $title = htmlspecialchars($title);
447 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
449 logger('parse_url (unparsed): returns: ' . $result);
455 $siteinfo = parseurl_getsiteinfo($url);
458 // require_once("include/items.php");
460 // echo add_page_info_data($siteinfo);
464 $url= $siteinfo["url"];
466 // If the link contains BBCode stuff, make a short link out of this to avoid parsing problems
467 if (strpos($url, '[') OR strpos($url, ']')) {
468 require_once("include/network.php");
469 $url = short_link($url);
474 if($siteinfo["title"] != "") {
475 $text = $siteinfo["text"];
476 $title = $siteinfo["title"];
481 if (($siteinfo["type"] != "video") AND (sizeof($siteinfo["images"]) > 0)){
482 /* Execute below code only if image is present in siteinfo */
485 $max_images = get_config('system','max_bookmark_images');
486 if($max_images === false)
489 $max_images = intval($max_images);
491 foreach ($siteinfo["images"] as $imagedata) {
493 $image .= '[img='.$imagedata["width"].'x'.$imagedata["height"].']'.$imagedata["src"].'[/img]' . "\n";
495 $image .= '<img height="'.$imagedata["height"].'" width="'.$imagedata["width"].'" src="'.$imagedata["src"].'" alt="photo" /><br />';
497 if($max_images && $max_images >= $total_images)
504 $text = '[quote]'.trim($text).'[/quote]';
506 $text = '<blockquote>'.htmlspecialchars(trim($text)).'</blockquote>';
510 $text = $br.$br.$image.$text;
514 $title = str_replace(array("\r","\n"),array('',''),$title);
516 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
518 logger('parse_url: returns: ' . $result);
520 $sitedata .= trim($result);
522 if (($siteinfo["type"] == "video") AND ($url != ""))
523 echo "[class=type-video]".$sitedata."[/class]";
524 elseif (($siteinfo["type"] != "photo"))
525 echo "[class=type-link]".$sitedata."[/class]";
527 echo "[class=type-photo]".$title.$br.$image."[/class]";