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);
100 // if the file is too large then exit
101 if ($curl_info["download_content_length"] > 1000000)
104 // if it isn't a HTML file then exit
105 if (($curl_info["content_type"] != "") AND !strstr(strtolower($curl_info["content_type"]),"html"))
109 require_once("include/oembed.php");
111 $oembed_data = oembed_fetch_url($url);
113 if ($oembed_data->type != "error")
114 $siteinfo["type"] = $oembed_data->type;
116 if (($oembed_data->type == "link") AND ($siteinfo["type"] != "photo")) {
117 if (isset($oembed_data->title))
118 $siteinfo["title"] = $oembed_data->title;
119 if (isset($oembed_data->description))
120 $siteinfo["text"] = trim($oembed_data->description);
121 if (isset($oembed_data->thumbnail_url))
122 $siteinfo["image"] = $oembed_data->thumbnail_url;
126 $stamp1 = microtime(true);
128 // Now fetch the body as well
130 curl_setopt($ch, CURLOPT_URL, $url);
131 curl_setopt($ch, CURLOPT_HEADER, 1);
132 curl_setopt($ch, CURLOPT_NOBODY, 0);
133 curl_setopt($ch, CURLOPT_TIMEOUT, 10);
134 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
135 curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
137 $header = curl_exec($ch);
138 $curl_info = @curl_getinfo($ch);
139 $http_code = $curl_info['http_code'];
142 $a->save_timestamp($stamp1, "network");
144 // Fetch the first mentioned charset. Can be in body or header
146 if (preg_match('/charset=(.*?)['."'".'"\s\n]/', $header, $matches))
147 $charset = trim(trim(trim(array_pop($matches)), ';,'));
152 $pos = strpos($header, "\r\n\r\n");
155 $body = trim(substr($header, $pos));
159 if (($charset != '') AND (strtoupper($charset) != "UTF-8")) {
160 logger("parseurl_getsiteinfo: detected charset ".$charset, LOGGER_DEBUG);
161 //$body = mb_convert_encoding($body, "UTF-8", $charset);
162 $body = iconv($charset, "UTF-8//TRANSLIT", $body);
165 $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
167 $doc = new DOMDocument();
168 @$doc->loadHTML($body);
170 deletenode($doc, 'style');
171 deletenode($doc, 'script');
172 deletenode($doc, 'option');
173 deletenode($doc, 'h1');
174 deletenode($doc, 'h2');
175 deletenode($doc, 'h3');
176 deletenode($doc, 'h4');
177 deletenode($doc, 'h5');
178 deletenode($doc, 'h6');
179 deletenode($doc, 'ol');
180 deletenode($doc, 'ul');
182 $xpath = new DomXPath($doc);
184 $list = $xpath->query("//meta[@content]");
185 foreach ($list as $node) {
187 if ($node->attributes->length)
188 foreach ($node->attributes as $attribute)
189 $attr[$attribute->name] = $attribute->value;
191 if (@$attr["http-equiv"] == 'refresh') {
192 $path = $attr["content"];
193 $pathinfo = explode(";", $path);
195 foreach ($pathinfo AS $value) {
196 if (substr(strtolower($value), 0, 4) == "url=")
197 $content = substr($value, 4);
199 if ($content != "") {
200 $siteinfo = parseurl_getsiteinfo($content, $no_guessing, $do_oembed, ++$count);
206 //$list = $xpath->query("head/title");
207 $list = $xpath->query("//title");
208 foreach ($list as $node)
209 $siteinfo["title"] = html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8");
211 //$list = $xpath->query("head/meta[@name]");
212 $list = $xpath->query("//meta[@name]");
213 foreach ($list as $node) {
215 if ($node->attributes->length)
216 foreach ($node->attributes as $attribute)
217 $attr[$attribute->name] = $attribute->value;
219 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
221 if ($attr["content"] != "")
222 switch (strtolower($attr["name"])) {
224 $siteinfo["title"] = $attr["content"];
227 $siteinfo["text"] = $attr["content"];
230 $siteinfo["image"] = $attr["content"];
232 case "twitter:image":
233 $siteinfo["image"] = $attr["content"];
235 case "twitter:image:src":
236 $siteinfo["image"] = $attr["content"];
239 if (($siteinfo["type"] == "") OR ($attr["content"] == "photo"))
240 $siteinfo["type"] = $attr["content"];
242 case "twitter:description":
243 $siteinfo["text"] = $attr["content"];
245 case "twitter:title":
246 $siteinfo["title"] = $attr["content"];
249 $siteinfo["title"] = $attr["content"];
251 case "dc.description":
252 $siteinfo["text"] = $attr["content"];
255 $keywords = explode(",", $attr["content"]);
257 case "news_keywords":
258 $keywords = explode(",", $attr["content"]);
261 if ($siteinfo["type"] == "summary")
262 $siteinfo["type"] = "link";
265 if (isset($keywords)) {
266 $siteinfo["keywords"] = array();
267 foreach ($keywords as $keyword)
268 $siteinfo["keywords"][] = trim($keyword);
271 //$list = $xpath->query("head/meta[@property]");
272 $list = $xpath->query("//meta[@property]");
273 foreach ($list as $node) {
275 if ($node->attributes->length)
276 foreach ($node->attributes as $attribute)
277 $attr[$attribute->name] = $attribute->value;
279 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
281 if ($attr["content"] != "")
282 switch (strtolower($attr["property"])) {
284 $siteinfo["image"] = $attr["content"];
287 $siteinfo["title"] = $attr["content"];
289 case "og:description":
290 $siteinfo["text"] = $attr["content"];
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]";