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) {
60 logger("parseurl_getsiteinfo: Endless loop detected for ".$url, LOGGER_DEBUG);
64 $url = trim($url, "'");
65 $url = trim($url, '"');
66 $siteinfo["url"] = $url;
67 $siteinfo["type"] = "link";
70 curl_setopt($ch, CURLOPT_URL, $url);
71 curl_setopt($ch, CURLOPT_HEADER, 1);
72 curl_setopt($ch, CURLOPT_NOBODY, 0);
73 curl_setopt($ch, CURLOPT_TIMEOUT, 3);
74 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
75 //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
76 curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
78 $header = curl_exec($ch);
79 $curl_info = @curl_getinfo($ch);
80 $http_code = $curl_info['http_code'];
83 if ((($curl_info['http_code'] == "301") OR ($curl_info['http_code'] == "302") OR ($curl_info['http_code'] == "303") OR ($curl_info['http_code'] == "307"))
84 AND (($curl_info['redirect_url'] != "") OR ($curl_info['location'] != ""))) {
85 if ($curl_info['redirect_url'] != "")
86 $siteinfo = parseurl_getsiteinfo($curl_info['redirect_url'], $no_guessing, $do_oembed, ++$count);
88 $siteinfo = parseurl_getsiteinfo($curl_info['location'], $no_guessing, $do_oembed, ++$count);
93 require_once("include/oembed.php");
95 $oembed_data = oembed_fetch_url($url);
97 if ($oembed_data->type != "error")
98 $siteinfo["type"] = $oembed_data->type;
101 // Fetch the first mentioned charset. Can be in body or header
103 if (preg_match('/charset=(.*?)['."'".'"\s\n]/', $header, $matches))
104 $charset = trim(trim(trim(array_pop($matches)), ';,'));
109 $pos = strpos($header, "\r\n\r\n");
112 $body = trim(substr($header, $pos));
116 if (($charset != '') AND (strtoupper($charset) != "UTF-8")) {
117 logger("parseurl_getsiteinfo: detected charset ".$charset, LOGGER_DEBUG);
118 //$body = mb_convert_encoding($body, "UTF-8", $charset);
119 $body = iconv($charset, "UTF-8//TRANSLIT", $body);
122 $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
124 $doc = new DOMDocument();
125 @$doc->loadHTML($body);
127 deletenode($doc, 'style');
128 deletenode($doc, 'script');
129 deletenode($doc, 'option');
130 deletenode($doc, 'h1');
131 deletenode($doc, 'h2');
132 deletenode($doc, 'h3');
133 deletenode($doc, 'h4');
134 deletenode($doc, 'h5');
135 deletenode($doc, 'h6');
136 deletenode($doc, 'ol');
137 deletenode($doc, 'ul');
139 $xpath = new DomXPath($doc);
141 $list = $xpath->query("//meta[@content]");
142 foreach ($list as $node) {
144 if ($node->attributes->length)
145 foreach ($node->attributes as $attribute)
146 $attr[$attribute->name] = $attribute->value;
148 if (@$attr["http-equiv"] == 'refresh') {
149 $path = $attr["content"];
150 $pathinfo = explode(";", $path);
152 foreach ($pathinfo AS $value) {
153 if (substr(strtolower($value), 0, 4) == "url=")
154 $content = substr($value, 4);
156 if ($content != "") {
157 $siteinfo = parseurl_getsiteinfo($content, $no_guessing, $do_oembed, ++$count);
163 //$list = $xpath->query("head/title");
164 $list = $xpath->query("//title");
165 foreach ($list as $node)
166 $siteinfo["title"] = html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8");
168 //$list = $xpath->query("head/meta[@name]");
169 $list = $xpath->query("//meta[@name]");
170 foreach ($list as $node) {
172 if ($node->attributes->length)
173 foreach ($node->attributes as $attribute)
174 $attr[$attribute->name] = $attribute->value;
176 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
178 if ($attr["content"] != "")
179 switch (strtolower($attr["name"])) {
181 $siteinfo["title"] = $attr["content"];
184 $siteinfo["text"] = $attr["content"];
186 case "twitter:image":
187 $siteinfo["image"] = $attr["content"];
189 case "twitter:image:src":
190 $siteinfo["image"] = $attr["content"];
193 if (($siteinfo["type"] == "") OR ($attr["content"] == "photo"))
194 $siteinfo["type"] = $attr["content"];
196 case "twitter:description":
197 $siteinfo["text"] = $attr["content"];
199 case "twitter:title":
200 $siteinfo["title"] = $attr["content"];
203 $siteinfo["title"] = $attr["content"];
205 case "dc.description":
206 $siteinfo["text"] = $attr["content"];
209 if ($siteinfo["type"] == "summary")
210 $siteinfo["type"] = "link";
213 //$list = $xpath->query("head/meta[@property]");
214 $list = $xpath->query("//meta[@property]");
215 foreach ($list as $node) {
217 if ($node->attributes->length)
218 foreach ($node->attributes as $attribute)
219 $attr[$attribute->name] = $attribute->value;
221 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
223 if ($attr["content"] != "")
224 switch (strtolower($attr["property"])) {
226 $siteinfo["image"] = $attr["content"];
229 $siteinfo["title"] = $attr["content"];
231 case "og:description":
232 $siteinfo["text"] = $attr["content"];
237 if (isset($oembed_data) AND ($oembed_data->type == "link") AND ($siteinfo["type"] != "photo")) {
238 if (isset($oembed_data->title) AND (trim($oembed_data->title) != ""))
239 $siteinfo["title"] = $oembed_data->title;
240 if (isset($oembed_data->description) AND (trim($oembed_data->description) != ""))
241 $siteinfo["text"] = trim($oembed_data->description);
242 if (isset($oembed_data->thumbnail_url) AND (trim($oembed_data->thumbnail_url) != ""))
243 $siteinfo["image"] = $oembed_data->thumbnail_url;
246 if ((@$siteinfo["image"] == "") AND !$no_guessing) {
247 $list = $xpath->query("//img[@src]");
248 foreach ($list as $node) {
250 if ($node->attributes->length)
251 foreach ($node->attributes as $attribute)
252 $attr[$attribute->name] = $attribute->value;
254 $src = completeurl($attr["src"], $url);
255 $photodata = @getimagesize($src);
257 if (($photodata) && ($photodata[0] > 150) and ($photodata[1] > 150)) {
258 if ($photodata[0] > 300) {
259 $photodata[1] = round($photodata[1] * (300 / $photodata[0]));
262 if ($photodata[1] > 300) {
263 $photodata[0] = round($photodata[0] * (300 / $photodata[1]));
266 $siteinfo["images"][] = array("src"=>$src,
267 "width"=>$photodata[0],
268 "height"=>$photodata[1]);
273 $src = completeurl($siteinfo["image"], $url);
275 unset($siteinfo["image"]);
277 $photodata = @getimagesize($src);
279 if (($photodata) && ($photodata[0] > 10) and ($photodata[1] > 10))
280 $siteinfo["images"][] = array("src"=>$src,
281 "width"=>$photodata[0],
282 "height"=>$photodata[1]);
285 if ((@$siteinfo["text"] == "") AND (@$siteinfo["title"] != "") AND !$no_guessing) {
288 $list = $xpath->query("//div[@class='article']");
289 foreach ($list as $node)
290 if (strlen($node->nodeValue) > 40)
291 $text .= " ".trim($node->nodeValue);
294 $list = $xpath->query("//div[@class='content']");
295 foreach ($list as $node)
296 if (strlen($node->nodeValue) > 40)
297 $text .= " ".trim($node->nodeValue);
300 // If none text was found then take the paragraph content
302 $list = $xpath->query("//p");
303 foreach ($list as $node)
304 if (strlen($node->nodeValue) > 40)
305 $text .= " ".trim($node->nodeValue);
309 $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text));
311 while (strpos($text, " "))
312 $text = trim(str_replace(" ", " ", $text));
314 $siteinfo["text"] = trim(html_entity_decode(substr($text,0,350), ENT_QUOTES, "UTF-8").'...');
318 logger("parseurl_getsiteinfo: Siteinfo for ".$url." ".print_r($siteinfo, true), LOGGER_DEBUG);
323 function arr_add_hashes(&$item,$k) {
327 function parse_url_content(&$a) {
334 if(local_user() && (! feature_enabled(local_user(),'richtext')))
338 $br = (($textmode) ? "\n" : '<br />');
340 if(x($_GET,'binurl'))
341 $url = trim(hex2bin($_GET['binurl']));
343 $url = trim($_GET['url']);
346 $title = strip_tags(trim($_GET['title']));
348 if($_GET['description'])
349 $text = strip_tags(trim($_GET['description']));
352 $arr_tags = str_getcsv($_GET['tags']);
353 if(count($arr_tags)) {
354 array_walk($arr_tags,'arr_add_hashes');
355 $str_tags = $br . implode(' ',$arr_tags) . $br;
359 logger('parse_url: ' . $url);
362 $template = '[bookmark=%s]%s[/bookmark]%s';
364 $template = "<a class=\"bookmark\" href=\"%s\" >%s</a>%s";
366 $arr = array('url' => $url, 'text' => '');
368 call_hooks('parse_link', $arr);
370 if(strlen($arr['text'])) {
376 if($url && $title && $text) {
378 $title = str_replace(array("\r","\n"),array('',''),$title);
381 $text = '[quote]' . trim($text) . '[/quote]' . $br;
383 $text = '<blockquote>' . htmlspecialchars(trim($text)) . '</blockquote><br />';
384 $title = htmlspecialchars($title);
387 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
389 logger('parse_url (unparsed): returns: ' . $result);
395 $siteinfo = parseurl_getsiteinfo($url);
397 $url= $siteinfo["url"];
401 if($siteinfo["title"] == "") {
402 $sitedata .= sprintf($template,$url,$url,'') . $str_tags;
405 $text = $siteinfo["text"];
406 $title = $siteinfo["title"];
411 if (($siteinfo["type"] != "video") AND (sizeof($siteinfo["images"]) > 0)){
412 /* Execute below code only if image is present in siteinfo */
415 $max_images = get_config('system','max_bookmark_images');
416 if($max_images === false)
419 $max_images = intval($max_images);
421 foreach ($siteinfo["images"] as $imagedata) {
423 $image .= '[img='.$imagedata["width"].'x'.$imagedata["height"].']'.$imagedata["src"].'[/img]' . "\n";
425 $image .= '<img height="'.$imagedata["height"].'" width="'.$imagedata["width"].'" src="'.$imagedata["src"].'" alt="photo" /><br />';
427 if($max_images && $max_images >= $total_images)
434 $text = '[quote]'.trim($text).'[/quote]';
436 $text = '<blockquote>'.htmlspecialchars(trim($text)).'</blockquote>';
440 $text = $br.$br.$image.$text;
444 $title = str_replace(array("\r","\n"),array('',''),$title);
446 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
448 logger('parse_url: returns: ' . $result);
450 $sitedata .= trim($result);
452 if (($siteinfo["type"] == "video") AND ($url != ""))
453 echo "[class=type-video]".$sitedata."[/class]";
454 elseif (($siteinfo["type"] != "photo"))
455 echo "[class=type-link]".$sitedata."[/class]";
457 echo "[class=type-photo]".$title.$br.$image."[/class]";