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) {
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,'Opera/9.64(Windows NT 5.1; U; de) Presto/2.1.1');
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 // Fetch the first mentioned charset. Can be in body or header
79 if (preg_match('/charset=(.*?)['."'".'"\s\n]/', $header, $matches))
80 $charset = trim(array_pop($matches));
84 $pos = strpos($header, "\r\n\r\n");
87 $body = trim(substr($header, $pos));
91 $body = mb_convert_encoding($body, "UTF-8", $charset);
92 $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
94 $doc = new DOMDocument();
95 @$doc->loadHTML($body);
97 deletenode($doc, 'style');
98 deletenode($doc, 'script');
99 deletenode($doc, 'option');
100 deletenode($doc, 'h1');
101 deletenode($doc, 'h2');
102 deletenode($doc, 'h3');
103 deletenode($doc, 'h4');
104 deletenode($doc, 'h5');
105 deletenode($doc, 'h6');
106 deletenode($doc, 'ol');
107 deletenode($doc, 'ul');
109 $xpath = new DomXPath($doc);
111 $list = $xpath->query("//meta[@content]");
112 foreach ($list as $node) {
114 if ($node->attributes->length)
115 foreach ($node->attributes as $attribute)
116 $attr[$attribute->name] = $attribute->value;
118 if (@$attr["http-equiv"] == 'refresh') {
119 $path = $attr["content"];
120 $pathinfo = explode(";", $path);
122 foreach ($pathinfo AS $value) {
123 if (substr(strtolower($value), 0, 4) == "url=")
124 $content = substr($value, 4);
126 if ($content != "") {
127 $siteinfo = parseurl_getsiteinfo($content);
133 //$list = $xpath->query("head/title");
134 $list = $xpath->query("//title");
135 foreach ($list as $node)
136 $siteinfo["title"] = html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8");
138 //$list = $xpath->query("head/meta[@name]");
139 $list = $xpath->query("//meta[@name]");
140 foreach ($list as $node) {
142 if ($node->attributes->length)
143 foreach ($node->attributes as $attribute)
144 $attr[$attribute->name] = $attribute->value;
146 $attr["content"] = html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8");
148 switch (strtolower($attr["name"])) {
150 $siteinfo["title"] = $attr["content"];
153 $siteinfo["text"] = $attr["content"];
156 $siteinfo["title"] = $attr["content"];
158 case "dc.description":
159 $siteinfo["text"] = $attr["content"];
164 //$list = $xpath->query("head/meta[@property]");
165 $list = $xpath->query("//meta[@property]");
166 foreach ($list as $node) {
168 if ($node->attributes->length)
169 foreach ($node->attributes as $attribute)
170 $attr[$attribute->name] = $attribute->value;
172 $attr["content"] = html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8");
174 switch (strtolower($attr["property"])) {
176 $siteinfo["image"] = $attr["content"];
179 $siteinfo["title"] = $attr["content"];
181 case "og:description":
182 $siteinfo["text"] = $attr["content"];
187 if (@$siteinfo["image"] == "") {
188 $list = $xpath->query("//img[@src]");
189 foreach ($list as $node) {
191 if ($node->attributes->length)
192 foreach ($node->attributes as $attribute)
193 $attr[$attribute->name] = $attribute->value;
195 $src = completeurl($attr["src"], $url);
196 $photodata = @getimagesize($src);
198 if (($photodata) && ($photodata[0] > 150) and ($photodata[1] > 150)) {
199 if ($photodata[0] > 300) {
200 $photodata[1] = round($photodata[1] * (300 / $photodata[0]));
203 if ($photodata[1] > 300) {
204 $photodata[0] = round($photodata[0] * (300 / $photodata[1]));
207 $siteinfo["images"][] = array("src"=>$src,
208 "width"=>$photodata[0],
209 "height"=>$photodata[1]);
214 $src = completeurl($siteinfo["image"], $url);
216 unset($siteinfo["image"]);
218 $photodata = @getimagesize($src);
220 if (($photodata) && ($photodata[0] > 10) and ($photodata[1] > 10))
221 $siteinfo["images"][] = array("src"=>$src,
222 "width"=>$photodata[0],
223 "height"=>$photodata[1]);
226 if (@$siteinfo["text"] == "") {
229 $list = $xpath->query("//div[@class='article']");
230 foreach ($list as $node)
231 if (strlen($node->nodeValue) > 40)
232 $text .= " ".trim($node->nodeValue);
235 $list = $xpath->query("//div[@class='content']");
236 foreach ($list as $node)
237 if (strlen($node->nodeValue) > 40)
238 $text .= " ".trim($node->nodeValue);
241 // If none text was found then take the paragraph content
243 $list = $xpath->query("//p");
244 foreach ($list as $node)
245 if (strlen($node->nodeValue) > 40)
246 $text .= " ".trim($node->nodeValue);
250 $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text));
252 while (strpos($text, " "))
253 $text = trim(str_replace(" ", " ", $text));
255 $siteinfo["text"] = html_entity_decode(substr($text,0,350), ENT_QUOTES, "UTF-8").'...';
262 function arr_add_hashes(&$item,$k) {
266 function parse_url_content(&$a) {
273 if(local_user() && (! feature_enabled(local_user(),'richtext')))
277 $br = (($textmode) ? "\n" : '<br />');
279 if(x($_GET,'binurl'))
280 $url = trim(hex2bin($_GET['binurl']));
282 $url = trim($_GET['url']);
285 $title = strip_tags(trim($_GET['title']));
287 if($_GET['description'])
288 $text = strip_tags(trim($_GET['description']));
291 $arr_tags = str_getcsv($_GET['tags']);
292 if(count($arr_tags)) {
293 array_walk($arr_tags,'arr_add_hashes');
294 $str_tags = $br . implode(' ',$arr_tags) . $br;
298 logger('parse_url: ' . $url);
301 $template = $br . '[bookmark=%s]%s[/bookmark]%s' . $br;
303 $template = "<br /><a class=\"bookmark\" href=\"%s\" >%s</a>%s<br />";
305 $arr = array('url' => $url, 'text' => '');
307 call_hooks('parse_link', $arr);
309 if(strlen($arr['text'])) {
315 if($url && $title && $text) {
318 $text = $br . '[quote]' . trim($text) . '[/quote]' . $br;
320 $text = '<br /><blockquote>' . trim($text) . '</blockquote><br />';
322 $title = str_replace(array("\r","\n"),array('',''),$title);
324 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
326 logger('parse_url (unparsed): returns: ' . $result);
332 $siteinfo = parseurl_getsiteinfo($url);
334 if($siteinfo["title"] == "") {
335 echo sprintf($template,$url,$url,'') . $str_tags;
338 $text = $siteinfo["text"];
339 $title = $siteinfo["title"];
344 if(sizeof($siteinfo["images"]) > 0){
345 /* Execute below code only if image is present in siteinfo */
348 $max_images = get_config('system','max_bookmark_images');
349 if($max_images === false)
352 $max_images = intval($max_images);
354 foreach ($siteinfo["images"] as $imagedata) {
356 $image .= '[img='.$imagedata["width"].'x'.$imagedata["height"].']'.$imagedata["src"].'[/img]' . "\n";
358 $image .= '<img height="'.$imagedata["height"].'" width="'.$imagedata["width"].'" src="'.$imagedata["src"].'" alt="photo" /><br />';
360 if($max_images && $max_images >= $total_images)
367 $text = $br.'[quote]'.trim($text).'[/quote]'.$br ;
369 $text = '<br /><blockquote>'.trim($text).'</blockquote><br />';
373 $text = $br.$br.$image.$text;
375 $title = str_replace(array("\r","\n"),array('',''),$title);
377 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
379 logger('parse_url: returns: ' . $result);