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 if (!in_array(trim($keyword), $siteinfo["keywords"]))
284 $siteinfo["keywords"][] = trim($keyword);
287 //$list = $xpath->query("head/meta[@property]");
288 $list = $xpath->query("//meta[@property]");
289 foreach ($list as $node) {
291 if ($node->attributes->length)
292 foreach ($node->attributes as $attribute)
293 $attr[$attribute->name] = $attribute->value;
295 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
297 if ($attr["content"] != "")
298 switch (strtolower($attr["property"])) {
300 $siteinfo["image"] = $attr["content"];
303 $siteinfo["title"] = $attr["content"];
305 case "og:description":
306 $siteinfo["text"] = $attr["content"];
311 if ((@$siteinfo["image"] == "") AND !$no_guessing) {
312 $list = $xpath->query("//img[@src]");
313 foreach ($list as $node) {
315 if ($node->attributes->length)
316 foreach ($node->attributes as $attribute)
317 $attr[$attribute->name] = $attribute->value;
319 $src = completeurl($attr["src"], $url);
320 $photodata = @getimagesize($src);
322 if (($photodata) && ($photodata[0] > 150) and ($photodata[1] > 150)) {
323 if ($photodata[0] > 300) {
324 $photodata[1] = round($photodata[1] * (300 / $photodata[0]));
327 if ($photodata[1] > 300) {
328 $photodata[0] = round($photodata[0] * (300 / $photodata[1]));
331 $siteinfo["images"][] = array("src"=>$src,
332 "width"=>$photodata[0],
333 "height"=>$photodata[1]);
338 $src = completeurl($siteinfo["image"], $url);
340 unset($siteinfo["image"]);
342 $photodata = @getimagesize($src);
344 if (($photodata) && ($photodata[0] > 10) and ($photodata[1] > 10))
345 $siteinfo["images"][] = array("src"=>$src,
346 "width"=>$photodata[0],
347 "height"=>$photodata[1]);
350 if ((@$siteinfo["text"] == "") AND (@$siteinfo["title"] != "") AND !$no_guessing) {
353 $list = $xpath->query("//div[@class='article']");
354 foreach ($list as $node)
355 if (strlen($node->nodeValue) > 40)
356 $text .= " ".trim($node->nodeValue);
359 $list = $xpath->query("//div[@class='content']");
360 foreach ($list as $node)
361 if (strlen($node->nodeValue) > 40)
362 $text .= " ".trim($node->nodeValue);
365 // If none text was found then take the paragraph content
367 $list = $xpath->query("//p");
368 foreach ($list as $node)
369 if (strlen($node->nodeValue) > 40)
370 $text .= " ".trim($node->nodeValue);
374 $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text));
376 while (strpos($text, " "))
377 $text = trim(str_replace(" ", " ", $text));
379 $siteinfo["text"] = trim(html_entity_decode(substr($text,0,350), ENT_QUOTES, "UTF-8").'...');
383 logger("parseurl_getsiteinfo: Siteinfo for ".$url." ".print_r($siteinfo, true), LOGGER_DEBUG);
385 call_hooks('getsiteinfo', $siteinfo);
390 function arr_add_hashes(&$item,$k) {
394 function parse_url_content(&$a) {
401 if(local_user() && (! feature_enabled(local_user(),'richtext')))
405 $br = (($textmode) ? "\n" : '<br />');
407 if(x($_GET,'binurl'))
408 $url = trim(hex2bin($_GET['binurl']));
410 $url = trim($_GET['url']);
413 $title = strip_tags(trim($_GET['title']));
415 if($_GET['description'])
416 $text = strip_tags(trim($_GET['description']));
419 $arr_tags = str_getcsv($_GET['tags']);
420 if(count($arr_tags)) {
421 array_walk($arr_tags,'arr_add_hashes');
422 $str_tags = $br . implode(' ',$arr_tags) . $br;
426 // add url scheme if missing
427 $arrurl = parse_url($url);
428 if (!x($arrurl, 'scheme')) {
429 if (x($arrurl, 'host'))
432 $url = "http://".$url;
435 logger('parse_url: ' . $url);
438 $template = '[bookmark=%s]%s[/bookmark]%s';
440 $template = "<a class=\"bookmark\" href=\"%s\" >%s</a>%s";
442 $arr = array('url' => $url, 'text' => '');
444 call_hooks('parse_link', $arr);
446 if(strlen($arr['text'])) {
452 if($url && $title && $text) {
454 $title = str_replace(array("\r","\n"),array('',''),$title);
457 $text = '[quote]' . trim($text) . '[/quote]' . $br;
459 $text = '<blockquote>' . htmlspecialchars(trim($text)) . '</blockquote><br />';
460 $title = htmlspecialchars($title);
463 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
465 logger('parse_url (unparsed): returns: ' . $result);
471 $siteinfo = parseurl_getsiteinfo($url);
474 // require_once("include/items.php");
476 // echo add_page_info_data($siteinfo);
480 $url= $siteinfo["url"];
482 // If the link contains BBCode stuff, make a short link out of this to avoid parsing problems
483 if (strpos($url, '[') OR strpos($url, ']')) {
484 require_once("include/network.php");
485 $url = short_link($url);
490 if($siteinfo["title"] != "") {
491 $text = $siteinfo["text"];
492 $title = $siteinfo["title"];
497 if (($siteinfo["type"] != "video") AND (sizeof($siteinfo["images"]) > 0)){
498 /* Execute below code only if image is present in siteinfo */
501 $max_images = get_config('system','max_bookmark_images');
502 if($max_images === false)
505 $max_images = intval($max_images);
507 foreach ($siteinfo["images"] as $imagedata) {
509 $image .= '[img='.$imagedata["width"].'x'.$imagedata["height"].']'.$imagedata["src"].'[/img]' . "\n";
511 $image .= '<img height="'.$imagedata["height"].'" width="'.$imagedata["width"].'" src="'.$imagedata["src"].'" alt="photo" /><br />';
513 if($max_images && $max_images >= $total_images)
520 $text = '[quote]'.trim($text).'[/quote]';
522 $text = '<blockquote>'.htmlspecialchars(trim($text)).'</blockquote>';
526 $text = $br.$br.$image.$text;
530 $title = str_replace(array("\r","\n"),array('',''),$title);
532 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
534 logger('parse_url: returns: ' . $result);
536 $sitedata .= trim($result);
538 if (($siteinfo["type"] == "video") AND ($url != ""))
539 echo "[class=type-video]".$sitedata."[/class]";
540 elseif (($siteinfo["type"] != "photo"))
541 echo "[class=type-link]".$sitedata."[/class]";
543 echo "[class=type-photo]".$title.$br.$image."[/class]";