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 curl_setopt($ch, CURLOPT_URL, $url);
57 curl_setopt($ch, CURLOPT_HEADER, 1);
58 curl_setopt($ch, CURLOPT_NOBODY, 0);
59 curl_setopt($ch, CURLOPT_TIMEOUT, 3);
60 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
61 //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
62 curl_setopt($ch,CURLOPT_USERAGENT,' Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0');
64 $header = curl_exec($ch);
65 $curl_info = @curl_getinfo($ch);
66 $http_code = $curl_info['http_code'];
69 if ((($curl_info['http_code'] == "301") OR ($curl_info['http_code'] == "302"))
70 AND (($curl_info['redirect_url'] != "") OR ($curl_info['location'] != ""))) {
71 if ($curl_info['redirect_url'] != "")
72 $siteinfo = parseurl_getsiteinfo($curl_info['redirect_url']);
74 $siteinfo = parseurl_getsiteinfo($curl_info['location']);
78 require_once("include/oembed.php");
80 $oembed_data = oembed_fetch_url($url);
82 if ($oembed_data->type == "link") {
83 if (isset($oembed_data->title))
84 $siteinfo["title"] = $oembed_data->title;
85 if (isset($oembed_data->description))
86 $siteinfo["text"] = $oembed_data->description;
87 if (isset($oembed_data->thumbnail_url))
88 $siteinfo["image"] = $oembed_data->thumbnail_url;
91 // Fetch the first mentioned charset. Can be in body or header
92 if (preg_match('/charset=(.*?)['."'".'"\s\n]/', $header, $matches))
93 $charset = trim(array_pop($matches));
97 $pos = strpos($header, "\r\n\r\n");
100 $body = trim(substr($header, $pos));
104 $body = mb_convert_encoding($body, "UTF-8", $charset);
105 $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
107 $doc = new DOMDocument();
108 @$doc->loadHTML($body);
110 deletenode($doc, 'style');
111 deletenode($doc, 'script');
112 deletenode($doc, 'option');
113 deletenode($doc, 'h1');
114 deletenode($doc, 'h2');
115 deletenode($doc, 'h3');
116 deletenode($doc, 'h4');
117 deletenode($doc, 'h5');
118 deletenode($doc, 'h6');
119 deletenode($doc, 'ol');
120 deletenode($doc, 'ul');
122 $xpath = new DomXPath($doc);
124 $list = $xpath->query("//meta[@content]");
125 foreach ($list as $node) {
127 if ($node->attributes->length)
128 foreach ($node->attributes as $attribute)
129 $attr[$attribute->name] = $attribute->value;
131 if (@$attr["http-equiv"] == 'refresh') {
132 $path = $attr["content"];
133 $pathinfo = explode(";", $path);
135 foreach ($pathinfo AS $value) {
136 if (substr(strtolower($value), 0, 4) == "url=")
137 $content = substr($value, 4);
139 if ($content != "") {
140 $siteinfo = parseurl_getsiteinfo($content);
146 //$list = $xpath->query("head/title");
147 $list = $xpath->query("//title");
148 foreach ($list as $node)
149 $siteinfo["title"] = html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8");
151 //$list = $xpath->query("head/meta[@name]");
152 $list = $xpath->query("//meta[@name]");
153 foreach ($list as $node) {
155 if ($node->attributes->length)
156 foreach ($node->attributes as $attribute)
157 $attr[$attribute->name] = $attribute->value;
159 $attr["content"] = html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8");
161 switch (strtolower($attr["name"])) {
163 $siteinfo["title"] = $attr["content"];
166 $siteinfo["text"] = $attr["content"];
169 $siteinfo["title"] = $attr["content"];
171 case "dc.description":
172 $siteinfo["text"] = $attr["content"];
177 //$list = $xpath->query("head/meta[@property]");
178 $list = $xpath->query("//meta[@property]");
179 foreach ($list as $node) {
181 if ($node->attributes->length)
182 foreach ($node->attributes as $attribute)
183 $attr[$attribute->name] = $attribute->value;
185 $attr["content"] = html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8");
187 switch (strtolower($attr["property"])) {
189 $siteinfo["image"] = $attr["content"];
192 $siteinfo["title"] = $attr["content"];
194 case "og:description":
195 $siteinfo["text"] = $attr["content"];
200 if ((@$siteinfo["image"] == "") AND !$no_guessing) {
201 $list = $xpath->query("//img[@src]");
202 foreach ($list as $node) {
204 if ($node->attributes->length)
205 foreach ($node->attributes as $attribute)
206 $attr[$attribute->name] = $attribute->value;
208 $src = completeurl($attr["src"], $url);
209 $photodata = @getimagesize($src);
211 if (($photodata) && ($photodata[0] > 150) and ($photodata[1] > 150)) {
212 if ($photodata[0] > 300) {
213 $photodata[1] = round($photodata[1] * (300 / $photodata[0]));
216 if ($photodata[1] > 300) {
217 $photodata[0] = round($photodata[0] * (300 / $photodata[1]));
220 $siteinfo["images"][] = array("src"=>$src,
221 "width"=>$photodata[0],
222 "height"=>$photodata[1]);
227 $src = completeurl($siteinfo["image"], $url);
229 unset($siteinfo["image"]);
231 $photodata = @getimagesize($src);
233 if (($photodata) && ($photodata[0] > 10) and ($photodata[1] > 10))
234 $siteinfo["images"][] = array("src"=>$src,
235 "width"=>$photodata[0],
236 "height"=>$photodata[1]);
239 if ((@$siteinfo["text"] == "") AND (@$siteinfo["title"] != "") AND !$no_guessing) {
242 $list = $xpath->query("//div[@class='article']");
243 foreach ($list as $node)
244 if (strlen($node->nodeValue) > 40)
245 $text .= " ".trim($node->nodeValue);
248 $list = $xpath->query("//div[@class='content']");
249 foreach ($list as $node)
250 if (strlen($node->nodeValue) > 40)
251 $text .= " ".trim($node->nodeValue);
254 // If none text was found then take the paragraph content
256 $list = $xpath->query("//p");
257 foreach ($list as $node)
258 if (strlen($node->nodeValue) > 40)
259 $text .= " ".trim($node->nodeValue);
263 $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text));
265 while (strpos($text, " "))
266 $text = trim(str_replace(" ", " ", $text));
268 $siteinfo["text"] = html_entity_decode(substr($text,0,350), ENT_QUOTES, "UTF-8").'...';
275 function arr_add_hashes(&$item,$k) {
279 function parse_url_content(&$a) {
286 if(local_user() && (! feature_enabled(local_user(),'richtext')))
290 $br = (($textmode) ? "\n" : '<br />');
292 if(x($_GET,'binurl'))
293 $url = trim(hex2bin($_GET['binurl']));
295 $url = trim($_GET['url']);
298 $title = strip_tags(trim($_GET['title']));
300 if($_GET['description'])
301 $text = strip_tags(trim($_GET['description']));
304 $arr_tags = str_getcsv($_GET['tags']);
305 if(count($arr_tags)) {
306 array_walk($arr_tags,'arr_add_hashes');
307 $str_tags = $br . implode(' ',$arr_tags) . $br;
311 logger('parse_url: ' . $url);
314 $template = '[bookmark=%s]%s[/bookmark]%s' . $br;
316 $template = "<a class=\"bookmark\" href=\"%s\" >%s</a>%s<br />";
318 $arr = array('url' => $url, 'text' => '');
320 call_hooks('parse_link', $arr);
322 if(strlen($arr['text'])) {
328 if($url && $title && $text) {
331 $text = '[quote]' . trim($text) . '[/quote]' . $br;
333 $text = '<blockquote>' . trim($text) . '</blockquote><br />';
335 $title = str_replace(array("\r","\n"),array('',''),$title);
337 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
339 logger('parse_url (unparsed): returns: ' . $result);
345 $siteinfo = parseurl_getsiteinfo($url);
349 if($siteinfo["title"] == "") {
350 $sitedata .= sprintf($template,$url,$url,'') . $str_tags;
353 $text = $siteinfo["text"];
354 $title = $siteinfo["title"];
359 if(sizeof($siteinfo["images"]) > 0){
360 /* Execute below code only if image is present in siteinfo */
363 $max_images = get_config('system','max_bookmark_images');
364 if($max_images === false)
367 $max_images = intval($max_images);
369 foreach ($siteinfo["images"] as $imagedata) {
371 $image .= '[img='.$imagedata["width"].'x'.$imagedata["height"].']'.$imagedata["src"].'[/img]' . "\n";
373 $image .= '<img height="'.$imagedata["height"].'" width="'.$imagedata["width"].'" src="'.$imagedata["src"].'" alt="photo" /><br />';
375 if($max_images && $max_images >= $total_images)
382 $text = '[quote]'.trim($text).'[/quote]';
384 $text = '<blockquote>'.trim($text).'</blockquote>';
388 $text = $br.$br.$image.$text;
391 $title = str_replace(array("\r","\n"),array('',''),$title);
393 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
395 logger('parse_url: returns: ' . $result);
397 $sitedata .= trim($result);
399 echo "[class=type-link]".$sitedata."[/class]";