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) {
57 curl_setopt($ch, CURLOPT_URL, $url);
58 curl_setopt($ch, CURLOPT_HEADER, 1);
59 curl_setopt($ch, CURLOPT_NOBODY, 0);
60 curl_setopt($ch, CURLOPT_TIMEOUT, 3);
61 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
62 curl_setopt($ch,CURLOPT_USERAGENT,'Opera/9.64(Windows NT 5.1; U; de) Presto/2.1.1');
64 $header = curl_exec($ch);
67 // Fetch the first mentioned charset. Can be in body or header
68 if (preg_match('/charset=(.*?)['."'".'"\s\n]/', $header, $matches))
69 $charset = trim(array_pop($matches));
73 $pos = strpos($header, "\r\n\r\n");
76 $body = trim(substr($header, $pos));
80 $body = mb_convert_encoding($body, "UTF-8", $charset);
81 $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
83 $doc = new DOMDocument();
84 @$doc->loadHTML($body);
86 deletenode($doc, 'style');
87 deletenode($doc, 'script');
88 deletenode($doc, 'option');
89 deletenode($doc, 'h1');
90 deletenode($doc, 'h2');
91 deletenode($doc, 'h3');
92 deletenode($doc, 'h4');
93 deletenode($doc, 'h5');
94 deletenode($doc, 'h6');
95 deletenode($doc, 'ol');
96 deletenode($doc, 'ul');
98 $xpath = new DomXPath($doc);
100 $list = $xpath->query("//meta[@content]");
101 foreach ($list as $node) {
103 if ($node->attributes->length)
104 foreach ($node->attributes as $attribute)
105 $attr[$attribute->name] = $attribute->value;
107 if (@$attr["http-equiv"] == 'refresh') {
108 $path = $attr["content"];
109 $pathinfo = explode(";", $path);
111 foreach ($pathinfo AS $value) {
112 if (substr(strtolower($value), 0, 4) == "url=")
113 $content = substr($value, 4);
115 if ($content != "") {
116 $siteinfo = parseurl_getsiteinfo($content);
122 //$list = $xpath->query("head/title");
123 $list = $xpath->query("//title");
124 foreach ($list as $node)
125 $siteinfo["title"] = html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8");
127 //$list = $xpath->query("head/meta[@name]");
128 $list = $xpath->query("//meta[@name]");
129 foreach ($list as $node) {
131 if ($node->attributes->length)
132 foreach ($node->attributes as $attribute)
133 $attr[$attribute->name] = $attribute->value;
135 $attr["content"] = html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8");
137 switch (strtolower($attr["name"])) {
139 $siteinfo["title"] = $attr["content"];
142 $siteinfo["text"] = $attr["content"];
145 $siteinfo["title"] = $attr["content"];
147 case "dc.description":
148 $siteinfo["text"] = $attr["content"];
153 //$list = $xpath->query("head/meta[@property]");
154 $list = $xpath->query("//meta[@property]");
155 foreach ($list as $node) {
157 if ($node->attributes->length)
158 foreach ($node->attributes as $attribute)
159 $attr[$attribute->name] = $attribute->value;
161 $attr["content"] = html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8");
163 switch (strtolower($attr["property"])) {
165 $siteinfo["image"] = $attr["content"];
168 $siteinfo["title"] = $attr["content"];
170 case "og:description":
171 $siteinfo["text"] = $attr["content"];
176 if ($siteinfo["image"] == "") {
177 $list = $xpath->query("//img[@src]");
178 foreach ($list as $node) {
180 if ($node->attributes->length)
181 foreach ($node->attributes as $attribute)
182 $attr[$attribute->name] = $attribute->value;
184 $src = completeurl($attr["src"], $url);
185 $photodata = @getimagesize($src);
187 if (($photodata) && ($photodata[0] > 150) and ($photodata[1] > 150)) {
188 if ($photodata[0] > 300) {
189 $photodata[1] = round($photodata[1] * (300 / $photodata[0]));
192 if ($photodata[1] > 300) {
193 $photodata[0] = round($photodata[0] * (300 / $photodata[1]));
196 $siteinfo["images"][] = array("src"=>$src,
197 "width"=>$photodata[0],
198 "height"=>$photodata[1]);
203 $src = completeurl($siteinfo["image"], $url);
205 unset($siteinfo["image"]);
207 $photodata = @getimagesize($src);
209 if (($photodata) && ($photodata[0] > 10) and ($photodata[1] > 10))
210 $siteinfo["images"][] = array("src"=>$src,
211 "width"=>$photodata[0],
212 "height"=>$photodata[1]);
215 if ($siteinfo["text"] == "") {
218 $list = $xpath->query("//div[@class='article']");
219 foreach ($list as $node)
220 if (strlen($node->nodeValue) > 40)
221 $text .= " ".trim($node->nodeValue);
224 $list = $xpath->query("//div[@class='content']");
225 foreach ($list as $node)
226 if (strlen($node->nodeValue) > 40)
227 $text .= " ".trim($node->nodeValue);
230 // If none text was found then take the paragraph content
232 $list = $xpath->query("//p");
233 foreach ($list as $node)
234 if (strlen($node->nodeValue) > 40)
235 $text .= " ".trim($node->nodeValue);
239 $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text));
241 while (strpos($text, " "))
242 $text = trim(str_replace(" ", " ", $text));
244 $siteinfo["text"] = html_entity_decode(substr($text,0,350), ENT_QUOTES, "UTF-8").'...';
251 function arr_add_hashes(&$item,$k) {
255 function parse_url_content(&$a) {
262 if(local_user() && (! feature_enabled(local_user(),'richtext')))
266 $br = (($textmode) ? "\n" : '<br />');
268 if(x($_GET,'binurl'))
269 $url = trim(hex2bin($_GET['binurl']));
271 $url = trim($_GET['url']);
274 $title = strip_tags(trim($_GET['title']));
276 if($_GET['description'])
277 $text = strip_tags(trim($_GET['description']));
280 $arr_tags = str_getcsv($_GET['tags']);
281 if(count($arr_tags)) {
282 array_walk($arr_tags,'arr_add_hashes');
283 $str_tags = $br . implode(' ',$arr_tags) . $br;
287 logger('parse_url: ' . $url);
290 $template = $br . '[bookmark=%s]%s[/bookmark]%s' . $br;
292 $template = "<br /><a class=\"bookmark\" href=\"%s\" >%s</a>%s<br />";
294 $arr = array('url' => $url, 'text' => '');
296 call_hooks('parse_link', $arr);
298 if(strlen($arr['text'])) {
304 if($url && $title && $text) {
307 $text = $br . '[quote]' . trim($text) . '[/quote]' . $br;
309 $text = '<br /><blockquote>' . trim($text) . '</blockquote><br />';
311 $title = str_replace(array("\r","\n"),array('',''),$title);
313 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
315 logger('parse_url (unparsed): returns: ' . $result);
321 $siteinfo = parseurl_getsiteinfo($url);
323 if($siteinfo["title"] == "") {
324 echo sprintf($template,$url,$url,'') . $str_tags;
327 $text = $siteinfo["text"];
328 $title = $siteinfo["title"];
333 if(sizeof($siteinfo["images"]) > 0){
334 /* Execute below code only if image is present in siteinfo */
337 $max_images = get_config('system','max_bookmark_images');
338 if($max_images === false)
341 $max_images = intval($max_images);
343 foreach ($siteinfo["images"] as $imagedata) {
345 $image .= '[img='.$imagedata["width"].'x'.$imagedata["height"].']'.$imagedata["src"].'[/img]' . "\n";
347 $image .= '<img height="'.$imagedata["height"].'" width="'.$imagedata["width"].'" src="'.$imagedata["src"].'" alt="photo" /><br />';
349 if($max_images && $max_images >= $total_images)
356 $text = $br.'[quote]'.trim($text).'[/quote]'.$br ;
358 $text = '<br /><blockquote>'.trim($text).'</blockquote><br />';
362 $text = $br.$br.$image.$text;
364 $title = str_replace(array("\r","\n"),array('',''),$title);
366 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
368 logger('parse_url: returns: ' . $result);