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) {
56 $siteinfo["type"] = "link";
59 curl_setopt($ch, CURLOPT_URL, $url);
60 curl_setopt($ch, CURLOPT_HEADER, 1);
61 curl_setopt($ch, CURLOPT_NOBODY, 0);
62 curl_setopt($ch, CURLOPT_TIMEOUT, 3);
63 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
64 //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
65 curl_setopt($ch,CURLOPT_USERAGENT,' Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0');
67 $header = curl_exec($ch);
68 $curl_info = @curl_getinfo($ch);
69 $http_code = $curl_info['http_code'];
72 if ((($curl_info['http_code'] == "301") OR ($curl_info['http_code'] == "302"))
73 AND (($curl_info['redirect_url'] != "") OR ($curl_info['location'] != ""))) {
74 if ($curl_info['redirect_url'] != "")
75 $siteinfo = parseurl_getsiteinfo($curl_info['redirect_url']);
77 $siteinfo = parseurl_getsiteinfo($curl_info['location']);
81 require_once("include/oembed.php");
83 $oembed_data = oembed_fetch_url($url);
85 if ($oembed_data->type != "error")
86 $siteinfo["type"] = $oembed_data->type;
88 if ($oembed_data->type == "link") {
89 if (isset($oembed_data->title))
90 $siteinfo["title"] = $oembed_data->title;
91 if (isset($oembed_data->description))
92 $siteinfo["text"] = trim($oembed_data->description);
93 if (isset($oembed_data->thumbnail_url))
94 $siteinfo["image"] = $oembed_data->thumbnail_url;
97 // Fetch the first mentioned charset. Can be in body or header
98 if (preg_match('/charset=(.*?)['."'".'"\s\n]/', $header, $matches))
99 $charset = trim(array_pop($matches));
103 $pos = strpos($header, "\r\n\r\n");
106 $body = trim(substr($header, $pos));
110 $body = mb_convert_encoding($body, "UTF-8", $charset);
111 $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
113 $doc = new DOMDocument();
114 @$doc->loadHTML($body);
116 deletenode($doc, 'style');
117 deletenode($doc, 'script');
118 deletenode($doc, 'option');
119 deletenode($doc, 'h1');
120 deletenode($doc, 'h2');
121 deletenode($doc, 'h3');
122 deletenode($doc, 'h4');
123 deletenode($doc, 'h5');
124 deletenode($doc, 'h6');
125 deletenode($doc, 'ol');
126 deletenode($doc, 'ul');
128 $xpath = new DomXPath($doc);
130 $list = $xpath->query("//meta[@content]");
131 foreach ($list as $node) {
133 if ($node->attributes->length)
134 foreach ($node->attributes as $attribute)
135 $attr[$attribute->name] = $attribute->value;
137 if (@$attr["http-equiv"] == 'refresh') {
138 $path = $attr["content"];
139 $pathinfo = explode(";", $path);
141 foreach ($pathinfo AS $value) {
142 if (substr(strtolower($value), 0, 4) == "url=")
143 $content = substr($value, 4);
145 if ($content != "") {
146 $siteinfo = parseurl_getsiteinfo($content);
152 //$list = $xpath->query("head/title");
153 $list = $xpath->query("//title");
154 foreach ($list as $node)
155 $siteinfo["title"] = html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8");
157 //$list = $xpath->query("head/meta[@name]");
158 $list = $xpath->query("//meta[@name]");
159 foreach ($list as $node) {
161 if ($node->attributes->length)
162 foreach ($node->attributes as $attribute)
163 $attr[$attribute->name] = $attribute->value;
165 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
167 switch (strtolower($attr["name"])) {
169 $siteinfo["title"] = $attr["content"];
172 $siteinfo["text"] = $attr["content"];
174 case "twitter:image":
175 $siteinfo["image"] = $attr["content"];
178 if ($siteinfo["type"] == "")
179 $siteinfo["type"] = $attr["content"];
181 case "twitter:description":
182 $siteinfo["text"] = $attr["content"];
184 case "twitter:title":
185 $siteinfo["title"] = $attr["content"];
188 $siteinfo["title"] = $attr["content"];
190 case "dc.description":
191 $siteinfo["text"] = $attr["content"];
194 if ($siteinfo["type"] == "summary")
195 $siteinfo["type"] = "link";
198 //$list = $xpath->query("head/meta[@property]");
199 $list = $xpath->query("//meta[@property]");
200 foreach ($list as $node) {
202 if ($node->attributes->length)
203 foreach ($node->attributes as $attribute)
204 $attr[$attribute->name] = $attribute->value;
206 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
208 switch (strtolower($attr["property"])) {
210 $siteinfo["image"] = $attr["content"];
213 $siteinfo["title"] = $attr["content"];
215 case "og:description":
216 $siteinfo["text"] = $attr["content"];
221 if ((@$siteinfo["image"] == "") AND !$no_guessing) {
222 $list = $xpath->query("//img[@src]");
223 foreach ($list as $node) {
225 if ($node->attributes->length)
226 foreach ($node->attributes as $attribute)
227 $attr[$attribute->name] = $attribute->value;
229 $src = completeurl($attr["src"], $url);
230 $photodata = @getimagesize($src);
232 if (($photodata) && ($photodata[0] > 150) and ($photodata[1] > 150)) {
233 if ($photodata[0] > 300) {
234 $photodata[1] = round($photodata[1] * (300 / $photodata[0]));
237 if ($photodata[1] > 300) {
238 $photodata[0] = round($photodata[0] * (300 / $photodata[1]));
241 $siteinfo["images"][] = array("src"=>$src,
242 "width"=>$photodata[0],
243 "height"=>$photodata[1]);
248 $src = completeurl($siteinfo["image"], $url);
250 unset($siteinfo["image"]);
252 $photodata = @getimagesize($src);
254 if (($photodata) && ($photodata[0] > 10) and ($photodata[1] > 10))
255 $siteinfo["images"][] = array("src"=>$src,
256 "width"=>$photodata[0],
257 "height"=>$photodata[1]);
260 if ((@$siteinfo["text"] == "") AND (@$siteinfo["title"] != "") AND !$no_guessing) {
263 $list = $xpath->query("//div[@class='article']");
264 foreach ($list as $node)
265 if (strlen($node->nodeValue) > 40)
266 $text .= " ".trim($node->nodeValue);
269 $list = $xpath->query("//div[@class='content']");
270 foreach ($list as $node)
271 if (strlen($node->nodeValue) > 40)
272 $text .= " ".trim($node->nodeValue);
275 // If none text was found then take the paragraph content
277 $list = $xpath->query("//p");
278 foreach ($list as $node)
279 if (strlen($node->nodeValue) > 40)
280 $text .= " ".trim($node->nodeValue);
284 $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text));
286 while (strpos($text, " "))
287 $text = trim(str_replace(" ", " ", $text));
289 $siteinfo["text"] = trim(html_entity_decode(substr($text,0,350), ENT_QUOTES, "UTF-8").'...');
296 function arr_add_hashes(&$item,$k) {
300 function parse_url_content(&$a) {
307 if(local_user() && (! feature_enabled(local_user(),'richtext')))
311 $br = (($textmode) ? "\n" : '<br />');
313 if(x($_GET,'binurl'))
314 $url = trim(hex2bin($_GET['binurl']));
316 $url = trim($_GET['url']);
319 $title = strip_tags(trim($_GET['title']));
321 if($_GET['description'])
322 $text = strip_tags(trim($_GET['description']));
325 $arr_tags = str_getcsv($_GET['tags']);
326 if(count($arr_tags)) {
327 array_walk($arr_tags,'arr_add_hashes');
328 $str_tags = $br . implode(' ',$arr_tags) . $br;
332 logger('parse_url: ' . $url);
335 $template = '[bookmark=%s]%s[/bookmark]%s';
337 $template = "<a class=\"bookmark\" href=\"%s\" >%s</a>%s";
339 $arr = array('url' => $url, 'text' => '');
341 call_hooks('parse_link', $arr);
343 if(strlen($arr['text'])) {
349 if($url && $title && $text) {
351 $title = str_replace(array("\r","\n"),array('',''),$title);
354 $text = '[quote]' . trim($text) . '[/quote]' . $br;
356 $text = '<blockquote>' . htmlspecialchars(trim($text)) . '</blockquote><br />';
357 $title = htmlspecialchars($title);
360 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
362 logger('parse_url (unparsed): returns: ' . $result);
368 $siteinfo = parseurl_getsiteinfo($url);
372 if($siteinfo["title"] == "") {
373 $sitedata .= sprintf($template,$url,$url,'') . $str_tags;
376 $text = $siteinfo["text"];
377 $title = $siteinfo["title"];
382 if(sizeof($siteinfo["images"]) > 0){
383 /* Execute below code only if image is present in siteinfo */
386 $max_images = get_config('system','max_bookmark_images');
387 if($max_images === false)
390 $max_images = intval($max_images);
392 foreach ($siteinfo["images"] as $imagedata) {
394 $image .= '[img='.$imagedata["width"].'x'.$imagedata["height"].']'.$imagedata["src"].'[/img]' . "\n";
396 $image .= '<img height="'.$imagedata["height"].'" width="'.$imagedata["width"].'" src="'.$imagedata["src"].'" alt="photo" /><br />';
398 if($max_images && $max_images >= $total_images)
405 $text = '[quote]'.trim($text).'[/quote]';
407 $text = '<blockquote>'.htmlspecialchars(trim($text)).'</blockquote>';
411 $text = $br.$br.$image.$text;
415 $title = str_replace(array("\r","\n"),array('',''),$title);
417 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
419 logger('parse_url: returns: ' . $result);
421 $sitedata .= trim($result);
423 if (($siteinfo["type"] == "video") AND ($url != ""))
424 echo "[video]".$url."[/video]";
425 elseif (($siteinfo["type"] != "photo"))
426 echo "[class=type-link]".$sitedata."[/class]";
428 echo "[class=type-photo]".$title.$br.$image."[/class]";