3 * @file mod/parse_url.php
5 * @todo https://developers.google.com/+/plugins/snippet/
8 * <meta itemprop="name" content="Toller Titel">
9 * <meta itemprop="description" content="Eine tolle Beschreibung">
10 * <meta itemprop="image" content="http://maple.libertreeproject.org/images/tree-icon.png">
12 * <body itemscope itemtype="http://schema.org/Product">
13 * <h1 itemprop="name">Shiny Trinket</h1>
14 * <img itemprop="image" src="{image-url}" />
15 * <p itemprop="description">Shiny trinkets are shiny.</p>
20 if(!function_exists('deletenode')) {
21 function deletenode(&$doc, $node)
23 $xpath = new DomXPath($doc);
24 $list = $xpath->query("//".$node);
25 foreach ($list as $child)
26 $child->parentNode->removeChild($child);
30 function completeurl($url, $scheme) {
31 $urlarr = parse_url($url);
33 if (isset($urlarr["scheme"]))
36 $schemearr = parse_url($scheme);
38 $complete = $schemearr["scheme"]."://".$schemearr["host"];
40 if (@$schemearr["port"] != "")
41 $complete .= ":".$schemearr["port"];
43 if(strpos($urlarr['path'],'/') !== 0)
46 $complete .= $urlarr["path"];
48 if (@$urlarr["query"] != "")
49 $complete .= "?".$urlarr["query"];
51 if (@$urlarr["fragment"] != "")
52 $complete .= "#".$urlarr["fragment"];
57 function parseurl_getsiteinfo_cached($url, $no_guessing = false, $do_oembed = true) {
59 $data = Cache::get("parse_url:".$no_guessing.":".$do_oembed.":".$url);
60 if (!is_null($data)) {
61 $data = unserialize($data);
65 $data = parseurl_getsiteinfo($url, $no_guessing, $do_oembed);
67 Cache::set("parse_url:".$no_guessing.":".$do_oembed.":".$url,serialize($data), CACHE_DAY);
72 function parseurl_getsiteinfo($url, $no_guessing = false, $do_oembed = true, $count = 1) {
73 require_once("include/network.php");
74 require_once("include/Photo.php");
81 logger("parseurl_getsiteinfo: Endless loop detected for ".$url, LOGGER_DEBUG);
85 $url = trim($url, "'");
86 $url = trim($url, '"');
88 $url = original_url($url);
90 $siteinfo["url"] = $url;
91 $siteinfo["type"] = "link";
93 $stamp1 = microtime(true);
96 curl_setopt($ch, CURLOPT_URL, $url);
97 curl_setopt($ch, CURLOPT_HEADER, 1);
98 curl_setopt($ch, CURLOPT_NOBODY, 1);
99 curl_setopt($ch, CURLOPT_TIMEOUT, 3);
100 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
101 //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
102 curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
104 $header = curl_exec($ch);
105 $curl_info = @curl_getinfo($ch);
106 $http_code = $curl_info['http_code'];
109 $a->save_timestamp($stamp1, "network");
111 if ((($curl_info['http_code'] == "301") OR ($curl_info['http_code'] == "302") OR ($curl_info['http_code'] == "303") OR ($curl_info['http_code'] == "307"))
112 AND (($curl_info['redirect_url'] != "") OR ($curl_info['location'] != ""))) {
113 if ($curl_info['redirect_url'] != "")
114 $siteinfo = parseurl_getsiteinfo($curl_info['redirect_url'], $no_guessing, $do_oembed, ++$count);
116 $siteinfo = parseurl_getsiteinfo($curl_info['location'], $no_guessing, $do_oembed, ++$count);
120 // if the file is too large then exit
121 if ($curl_info["download_content_length"] > 1000000)
124 // if it isn't a HTML file then exit
125 if (($curl_info["content_type"] != "") AND !strstr(strtolower($curl_info["content_type"]),"html"))
129 require_once("include/oembed.php");
131 $oembed_data = oembed_fetch_url($url);
133 if ($oembed_data->type != "error")
134 $siteinfo["type"] = $oembed_data->type;
136 if (($oembed_data->type == "link") AND ($siteinfo["type"] != "photo")) {
137 if (isset($oembed_data->title))
138 $siteinfo["title"] = $oembed_data->title;
139 if (isset($oembed_data->description))
140 $siteinfo["text"] = trim($oembed_data->description);
141 if (isset($oembed_data->thumbnail_url))
142 $siteinfo["image"] = $oembed_data->thumbnail_url;
146 $stamp1 = microtime(true);
148 // Now fetch the body as well
150 curl_setopt($ch, CURLOPT_URL, $url);
151 curl_setopt($ch, CURLOPT_HEADER, 1);
152 curl_setopt($ch, CURLOPT_NOBODY, 0);
153 curl_setopt($ch, CURLOPT_TIMEOUT, 10);
154 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
155 curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
157 $header = curl_exec($ch);
158 $curl_info = @curl_getinfo($ch);
159 $http_code = $curl_info['http_code'];
162 $a->save_timestamp($stamp1, "network");
164 // Fetch the first mentioned charset. Can be in body or header
166 if (preg_match('/charset=(.*?)['."'".'"\s\n]/', $header, $matches))
167 $charset = trim(trim(trim(array_pop($matches)), ';,'));
172 $pos = strpos($header, "\r\n\r\n");
175 $body = trim(substr($header, $pos));
179 if (($charset != '') AND (strtoupper($charset) != "UTF-8")) {
180 logger("parseurl_getsiteinfo: detected charset ".$charset, LOGGER_DEBUG);
181 //$body = mb_convert_encoding($body, "UTF-8", $charset);
182 $body = iconv($charset, "UTF-8//TRANSLIT", $body);
185 $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
187 $doc = new DOMDocument();
188 @$doc->loadHTML($body);
190 deletenode($doc, 'style');
191 deletenode($doc, 'script');
192 deletenode($doc, 'option');
193 deletenode($doc, 'h1');
194 deletenode($doc, 'h2');
195 deletenode($doc, 'h3');
196 deletenode($doc, 'h4');
197 deletenode($doc, 'h5');
198 deletenode($doc, 'h6');
199 deletenode($doc, 'ol');
200 deletenode($doc, 'ul');
202 $xpath = new DomXPath($doc);
204 $list = $xpath->query("//meta[@content]");
205 foreach ($list as $node) {
207 if ($node->attributes->length)
208 foreach ($node->attributes as $attribute)
209 $attr[$attribute->name] = $attribute->value;
211 if (@$attr["http-equiv"] == 'refresh') {
212 $path = $attr["content"];
213 $pathinfo = explode(";", $path);
215 foreach ($pathinfo AS $value) {
216 if (substr(strtolower($value), 0, 4) == "url=")
217 $content = substr($value, 4);
219 if ($content != "") {
220 $siteinfo = parseurl_getsiteinfo($content, $no_guessing, $do_oembed, ++$count);
226 //$list = $xpath->query("head/title");
227 $list = $xpath->query("//title");
228 foreach ($list as $node)
229 $siteinfo["title"] = html_entity_decode($node->nodeValue, ENT_QUOTES, "UTF-8");
231 //$list = $xpath->query("head/meta[@name]");
232 $list = $xpath->query("//meta[@name]");
233 foreach ($list as $node) {
235 if ($node->attributes->length)
236 foreach ($node->attributes as $attribute)
237 $attr[$attribute->name] = $attribute->value;
239 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
241 if ($attr["content"] != "")
242 switch (strtolower($attr["name"])) {
244 $siteinfo["title"] = $attr["content"];
247 $siteinfo["text"] = $attr["content"];
250 $siteinfo["image"] = $attr["content"];
252 case "twitter:image":
253 $siteinfo["image"] = $attr["content"];
255 case "twitter:image:src":
256 $siteinfo["image"] = $attr["content"];
259 if (($siteinfo["type"] == "") OR ($attr["content"] == "photo"))
260 $siteinfo["type"] = $attr["content"];
262 case "twitter:description":
263 $siteinfo["text"] = $attr["content"];
265 case "twitter:title":
266 $siteinfo["title"] = $attr["content"];
269 $siteinfo["title"] = $attr["content"];
271 case "dc.description":
272 $siteinfo["text"] = $attr["content"];
275 $keywords = explode(",", $attr["content"]);
277 case "news_keywords":
278 $keywords = explode(",", $attr["content"]);
281 if ($siteinfo["type"] == "summary")
282 $siteinfo["type"] = "link";
285 if (isset($keywords)) {
286 $siteinfo["keywords"] = array();
287 foreach ($keywords as $keyword)
288 if (!in_array(trim($keyword), $siteinfo["keywords"]))
289 $siteinfo["keywords"][] = trim($keyword);
292 //$list = $xpath->query("head/meta[@property]");
293 $list = $xpath->query("//meta[@property]");
294 foreach ($list as $node) {
296 if ($node->attributes->length)
297 foreach ($node->attributes as $attribute)
298 $attr[$attribute->name] = $attribute->value;
300 $attr["content"] = trim(html_entity_decode($attr["content"], ENT_QUOTES, "UTF-8"));
302 if ($attr["content"] != "")
303 switch (strtolower($attr["property"])) {
305 $siteinfo["image"] = $attr["content"];
308 $siteinfo["title"] = $attr["content"];
310 case "og:description":
311 $siteinfo["text"] = $attr["content"];
316 if ((@$siteinfo["image"] == "") AND !$no_guessing) {
317 $list = $xpath->query("//img[@src]");
318 foreach ($list as $node) {
320 if ($node->attributes->length)
321 foreach ($node->attributes as $attribute)
322 $attr[$attribute->name] = $attribute->value;
324 $src = completeurl($attr["src"], $url);
325 $photodata = get_photo_info($src);
327 if (($photodata) && ($photodata[0] > 150) and ($photodata[1] > 150)) {
328 if ($photodata[0] > 300) {
329 $photodata[1] = round($photodata[1] * (300 / $photodata[0]));
332 if ($photodata[1] > 300) {
333 $photodata[0] = round($photodata[0] * (300 / $photodata[1]));
336 $siteinfo["images"][] = array("src"=>$src,
337 "width"=>$photodata[0],
338 "height"=>$photodata[1]);
342 } elseif ($siteinfo["image"] != "") {
343 $src = completeurl($siteinfo["image"], $url);
345 unset($siteinfo["image"]);
347 $photodata = get_photo_info($src);
349 if (($photodata) && ($photodata[0] > 10) and ($photodata[1] > 10))
350 $siteinfo["images"][] = array("src"=>$src,
351 "width"=>$photodata[0],
352 "height"=>$photodata[1]);
355 if ((@$siteinfo["text"] == "") AND (@$siteinfo["title"] != "") AND !$no_guessing) {
358 $list = $xpath->query("//div[@class='article']");
359 foreach ($list as $node)
360 if (strlen($node->nodeValue) > 40)
361 $text .= " ".trim($node->nodeValue);
364 $list = $xpath->query("//div[@class='content']");
365 foreach ($list as $node)
366 if (strlen($node->nodeValue) > 40)
367 $text .= " ".trim($node->nodeValue);
370 // If none text was found then take the paragraph content
372 $list = $xpath->query("//p");
373 foreach ($list as $node)
374 if (strlen($node->nodeValue) > 40)
375 $text .= " ".trim($node->nodeValue);
379 $text = trim(str_replace(array("\n", "\r"), array(" ", " "), $text));
381 while (strpos($text, " "))
382 $text = trim(str_replace(" ", " ", $text));
384 $siteinfo["text"] = trim(html_entity_decode(substr($text,0,350), ENT_QUOTES, "UTF-8").'...');
388 logger("parseurl_getsiteinfo: Siteinfo for ".$url." ".print_r($siteinfo, true), LOGGER_DEBUG);
390 call_hooks('getsiteinfo', $siteinfo);
395 function arr_add_hashes(&$item,$k) {
399 function parse_url_content(&$a) {
406 if(local_user() && (! feature_enabled(local_user(),'richtext')))
410 $br = (($textmode) ? "\n" : '<br />');
412 if(x($_GET,'binurl'))
413 $url = trim(hex2bin($_GET['binurl']));
415 $url = trim($_GET['url']);
418 $title = strip_tags(trim($_GET['title']));
420 if($_GET['description'])
421 $text = strip_tags(trim($_GET['description']));
424 $arr_tags = str_getcsv($_GET['tags']);
425 if(count($arr_tags)) {
426 array_walk($arr_tags,'arr_add_hashes');
427 $str_tags = $br . implode(' ',$arr_tags) . $br;
431 // add url scheme if missing
432 $arrurl = parse_url($url);
433 if (!x($arrurl, 'scheme')) {
434 if (x($arrurl, 'host'))
437 $url = "http://".$url;
440 logger('parse_url: ' . $url);
443 $template = '[bookmark=%s]%s[/bookmark]%s';
445 $template = "<a class=\"bookmark\" href=\"%s\" >%s</a>%s";
447 $arr = array('url' => $url, 'text' => '');
449 call_hooks('parse_link', $arr);
451 if(strlen($arr['text'])) {
457 if($url && $title && $text) {
459 $title = str_replace(array("\r","\n"),array('',''),$title);
462 $text = '[quote]' . trim($text) . '[/quote]' . $br;
464 $text = '<blockquote>' . htmlspecialchars(trim($text)) . '</blockquote><br />';
465 $title = htmlspecialchars($title);
468 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
470 logger('parse_url (unparsed): returns: ' . $result);
476 $siteinfo = parseurl_getsiteinfo($url);
479 // require_once("include/items.php");
481 // echo add_page_info_data($siteinfo);
485 $url= $siteinfo["url"];
487 // If the link contains BBCode stuff, make a short link out of this to avoid parsing problems
488 if (strpos($url, '[') OR strpos($url, ']')) {
489 require_once("include/network.php");
490 $url = short_link($url);
495 if($siteinfo["title"] != "") {
496 $text = $siteinfo["text"];
497 $title = $siteinfo["title"];
502 if (($siteinfo["type"] != "video") AND (sizeof($siteinfo["images"]) > 0)){
503 /* Execute below code only if image is present in siteinfo */
506 $max_images = get_config('system','max_bookmark_images');
507 if($max_images === false)
510 $max_images = intval($max_images);
512 foreach ($siteinfo["images"] as $imagedata) {
514 $image .= '[img='.$imagedata["width"].'x'.$imagedata["height"].']'.$imagedata["src"].'[/img]' . "\n";
516 $image .= '<img height="'.$imagedata["height"].'" width="'.$imagedata["width"].'" src="'.$imagedata["src"].'" alt="photo" /><br />';
518 if($max_images && $max_images >= $total_images)
525 $text = '[quote]'.trim($text).'[/quote]';
527 $text = '<blockquote>'.htmlspecialchars(trim($text)).'</blockquote>';
531 $text = $br.$br.$image.$text;
535 $title = str_replace(array("\r","\n"),array('',''),$title);
537 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
539 logger('parse_url: returns: ' . $result);
541 $sitedata .= trim($result);
543 if (($siteinfo["type"] == "video") AND ($url != ""))
544 echo "[class=type-video]".$sitedata."[/class]";
545 elseif (($siteinfo["type"] != "photo"))
546 echo "[class=type-link]".$sitedata."[/class]";
548 echo "[class=type-photo]".$title.$br.$image."[/class]";