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) {
57 $url = trim($url, "'");
58 $url = trim($url, '"');
59 $siteinfo["url"] = $url;
60 $siteinfo["type"] = "link";
63 curl_setopt($ch, CURLOPT_URL, $url);
64 curl_setopt($ch, CURLOPT_HEADER, 1);
65 curl_setopt($ch, CURLOPT_NOBODY, 0);
66 curl_setopt($ch, CURLOPT_TIMEOUT, 3);
67 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
68 //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
69 curl_setopt($ch,CURLOPT_USERAGENT,' Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0');
71 $header = curl_exec($ch);
72 $curl_info = @curl_getinfo($ch);
73 $http_code = $curl_info['http_code'];
76 if ((($curl_info['http_code'] == "301") OR ($curl_info['http_code'] == "302") OR ($curl_info['http_code'] == "303") OR ($curl_info['http_code'] == "307"))
77 AND (($curl_info['redirect_url'] != "") OR ($curl_info['location'] != ""))) {
78 if ($curl_info['redirect_url'] != "")
79 $siteinfo = parseurl_getsiteinfo($curl_info['redirect_url']);
81 $siteinfo = parseurl_getsiteinfo($curl_info['location']);
85 require_once("include/oembed.php");
87 $oembed_data = oembed_fetch_url($url);
89 if ($oembed_data->type != "error")
90 $siteinfo["type"] = $oembed_data->type;
92 // Fetch the first mentioned charset. Can be in body or header
94 if (preg_match('/charset=(.*?)['."'".'"\s\n]/', $header, $matches))
95 $charset = trim(array_pop($matches));
100 $pos = strpos($header, "\r\n\r\n");
103 $body = trim(substr($header, $pos));
107 $body = mb_convert_encoding($body, "UTF-8", $charset);
108 $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
110 $doc = new DOMDocument();
111 @$doc->loadHTML($body);
113 deletenode($doc, 'style');
114 deletenode($doc, 'script');
115 deletenode($doc, 'option');
116 deletenode($doc, 'h1');
117 deletenode($doc, 'h2');
118 deletenode($doc, 'h3');
119 deletenode($doc, 'h4');
120 deletenode($doc, 'h5');
121 deletenode($doc, 'h6');
122 deletenode($doc, 'ol');
123 deletenode($doc, 'ul');
125 $xpath = new DomXPath($doc);
127 $list = $xpath->query("//meta[@content]");
128 foreach ($list as $node) {
130 if ($node->attributes->length)
131 foreach ($node->attributes as $attribute)
132 $attr[$attribute->name] = $attribute->value;
134 if (@$attr["http-equiv"] == 'refresh') {
135 $path = $attr["content"];
136 $pathinfo = explode(";", $path);
138 foreach ($pathinfo AS $value) {
139 if (substr(strtolower($value), 0, 4) == "url=")
140 $content = substr($value, 4);
142 if ($content != "") {
143 $siteinfo = parseurl_getsiteinfo($content);
149 //$list = $xpath->query("head/title");
150 $list = $xpath->query("//title");
151 foreach ($list as $node)
152 $siteinfo["title"] = html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8");
154 //$list = $xpath->query("head/meta[@name]");
155 $list = $xpath->query("//meta[@name]");
156 foreach ($list as $node) {
158 if ($node->attributes->length)
159 foreach ($node->attributes as $attribute)
160 $attr[$attribute->name] = $attribute->value;
162 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
164 if ($attr["content"] != "")
165 switch (strtolower($attr["name"])) {
167 $siteinfo["title"] = $attr["content"];
170 $siteinfo["text"] = $attr["content"];
172 case "twitter:image":
173 $siteinfo["image"] = $attr["content"];
176 if (($siteinfo["type"] == "") OR ($attr["content"] == "photo"))
177 $siteinfo["type"] = $attr["content"];
179 case "twitter:description":
180 $siteinfo["text"] = $attr["content"];
182 case "twitter:title":
183 $siteinfo["title"] = $attr["content"];
186 $siteinfo["title"] = $attr["content"];
188 case "dc.description":
189 $siteinfo["text"] = $attr["content"];
192 if ($siteinfo["type"] == "summary")
193 $siteinfo["type"] = "link";
196 //$list = $xpath->query("head/meta[@property]");
197 $list = $xpath->query("//meta[@property]");
198 foreach ($list as $node) {
200 if ($node->attributes->length)
201 foreach ($node->attributes as $attribute)
202 $attr[$attribute->name] = $attribute->value;
204 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
206 if ($attr["content"] != "")
207 switch (strtolower($attr["property"])) {
209 $siteinfo["image"] = $attr["content"];
212 $siteinfo["title"] = $attr["content"];
214 case "og:description":
215 $siteinfo["text"] = $attr["content"];
220 if ($oembed_data->type == "link") {
221 if (isset($oembed_data->title) AND (trim($oembed_data->title) != ""))
222 $siteinfo["title"] = $oembed_data->title;
223 if (isset($oembed_data->description) AND (trim($oembed_data->description) != ""))
224 $siteinfo["text"] = trim($oembed_data->description);
225 if (isset($oembed_data->thumbnail_url) AND (trim($oembed_data->thumbnail_url) != ""))
226 $siteinfo["image"] = $oembed_data->thumbnail_url;
229 if ((@$siteinfo["image"] == "") AND !$no_guessing) {
230 $list = $xpath->query("//img[@src]");
231 foreach ($list as $node) {
233 if ($node->attributes->length)
234 foreach ($node->attributes as $attribute)
235 $attr[$attribute->name] = $attribute->value;
237 $src = completeurl($attr["src"], $url);
238 $photodata = @getimagesize($src);
240 if (($photodata) && ($photodata[0] > 150) and ($photodata[1] > 150)) {
241 if ($photodata[0] > 300) {
242 $photodata[1] = round($photodata[1] * (300 / $photodata[0]));
245 if ($photodata[1] > 300) {
246 $photodata[0] = round($photodata[0] * (300 / $photodata[1]));
249 $siteinfo["images"][] = array("src"=>$src,
250 "width"=>$photodata[0],
251 "height"=>$photodata[1]);
256 $src = completeurl($siteinfo["image"], $url);
258 unset($siteinfo["image"]);
260 $photodata = @getimagesize($src);
262 if (($photodata) && ($photodata[0] > 10) and ($photodata[1] > 10))
263 $siteinfo["images"][] = array("src"=>$src,
264 "width"=>$photodata[0],
265 "height"=>$photodata[1]);
268 if ((@$siteinfo["text"] == "") AND (@$siteinfo["title"] != "") AND !$no_guessing) {
271 $list = $xpath->query("//div[@class='article']");
272 foreach ($list as $node)
273 if (strlen($node->nodeValue) > 40)
274 $text .= " ".trim($node->nodeValue);
277 $list = $xpath->query("//div[@class='content']");
278 foreach ($list as $node)
279 if (strlen($node->nodeValue) > 40)
280 $text .= " ".trim($node->nodeValue);
283 // If none text was found then take the paragraph content
285 $list = $xpath->query("//p");
286 foreach ($list as $node)
287 if (strlen($node->nodeValue) > 40)
288 $text .= " ".trim($node->nodeValue);
292 $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text));
294 while (strpos($text, " "))
295 $text = trim(str_replace(" ", " ", $text));
297 $siteinfo["text"] = trim(html_entity_decode(substr($text,0,350), ENT_QUOTES, "UTF-8").'...');
301 logger("parseurl_getsiteinfo: Siteinfo for ".$url." ".print_r($siteinfo, true), LOGGER_DEBUG);
306 function arr_add_hashes(&$item,$k) {
310 function parse_url_content(&$a) {
317 if(local_user() && (! feature_enabled(local_user(),'richtext')))
321 $br = (($textmode) ? "\n" : '<br />');
323 if(x($_GET,'binurl'))
324 $url = trim(hex2bin($_GET['binurl']));
326 $url = trim($_GET['url']);
329 $title = strip_tags(trim($_GET['title']));
331 if($_GET['description'])
332 $text = strip_tags(trim($_GET['description']));
335 $arr_tags = str_getcsv($_GET['tags']);
336 if(count($arr_tags)) {
337 array_walk($arr_tags,'arr_add_hashes');
338 $str_tags = $br . implode(' ',$arr_tags) . $br;
342 logger('parse_url: ' . $url);
345 $template = '[bookmark=%s]%s[/bookmark]%s';
347 $template = "<a class=\"bookmark\" href=\"%s\" >%s</a>%s";
349 $arr = array('url' => $url, 'text' => '');
351 call_hooks('parse_link', $arr);
353 if(strlen($arr['text'])) {
359 if($url && $title && $text) {
361 $title = str_replace(array("\r","\n"),array('',''),$title);
364 $text = '[quote]' . trim($text) . '[/quote]' . $br;
366 $text = '<blockquote>' . htmlspecialchars(trim($text)) . '</blockquote><br />';
367 $title = htmlspecialchars($title);
370 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
372 logger('parse_url (unparsed): returns: ' . $result);
378 $siteinfo = parseurl_getsiteinfo($url);
380 $url= $siteinfo["url"];
384 if($siteinfo["title"] == "") {
385 $sitedata .= sprintf($template,$url,$url,'') . $str_tags;
388 $text = $siteinfo["text"];
389 $title = $siteinfo["title"];
394 if (($siteinfo["type"] != "video") AND (sizeof($siteinfo["images"]) > 0)){
395 /* Execute below code only if image is present in siteinfo */
398 $max_images = get_config('system','max_bookmark_images');
399 if($max_images === false)
402 $max_images = intval($max_images);
404 foreach ($siteinfo["images"] as $imagedata) {
406 $image .= '[img='.$imagedata["width"].'x'.$imagedata["height"].']'.$imagedata["src"].'[/img]' . "\n";
408 $image .= '<img height="'.$imagedata["height"].'" width="'.$imagedata["width"].'" src="'.$imagedata["src"].'" alt="photo" /><br />';
410 if($max_images && $max_images >= $total_images)
417 $text = '[quote]'.trim($text).'[/quote]';
419 $text = '<blockquote>'.htmlspecialchars(trim($text)).'</blockquote>';
423 $text = $br.$br.$image.$text;
427 $title = str_replace(array("\r","\n"),array('',''),$title);
429 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
431 logger('parse_url: returns: ' . $result);
433 $sitedata .= trim($result);
435 if (($siteinfo["type"] == "video") AND ($url != ""))
436 echo "[class=type-video]".$sitedata."[/class]";
437 elseif (($siteinfo["type"] != "photo"))
438 echo "[class=type-link]".$sitedata."[/class]";
440 echo "[class=type-photo]".$title.$br.$image."[/class]";