X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=mod%2Fparse_url.php;h=71d767675f8c51a1b3cba44b3edae4e2bd0c7bf8;hb=5f89cb09234bc79aab2be93778609b344239c3da;hp=46c6b46e97998e3db59b542d11b3b15b937c8fe4;hpb=f35a61761a9868c09f2b7f26102c176dd9b8de34;p=friendica.git
diff --git a/mod/parse_url.php b/mod/parse_url.php
index 46c6b46e97..71d767675f 100644
--- a/mod/parse_url.php
+++ b/mod/parse_url.php
@@ -1,124 +1,531 @@
+
+
-function parse_url_content(&$a) {
+
+ Shiny Trinket
+
+ Shiny trinkets are shiny.
+
+*/
- logger('parse_url: ' . $_GET['url']);
+if(!function_exists('deletenode')) {
+ function deletenode(&$doc, $node)
+ {
+ $xpath = new DomXPath($doc);
+ $list = $xpath->query("//".$node);
+ foreach ($list as $child)
+ $child->parentNode->removeChild($child);
+ }
+}
- $url = trim(hex2bin($_GET['url']));
+function completeurl($url, $scheme) {
+ $urlarr = parse_url($url);
- logger('parse_url: ' . $url);
+ if (isset($urlarr["scheme"]))
+ return($url);
- $text = null;
+ $schemearr = parse_url($scheme);
- $template = "
%s%s
";
+ $complete = $schemearr["scheme"]."://".$schemearr["host"];
+ if (@$schemearr["port"] != "")
+ $complete .= ":".$schemearr["port"];
- $arr = array('url' => $url, 'text' => '');
+ if(strpos($urlarr['path'],'/') !== 0)
+ $complete .= '/';
- call_hooks('parse_link', $arr);
+ $complete .= $urlarr["path"];
- if(strlen($arr['text'])) {
- echo $arr['text'];
- killme();
- }
+ if (@$urlarr["query"] != "")
+ $complete .= "?".$urlarr["query"];
- if($url) {
- $s = fetch_url($url);
- } else {
- echo '';
- killme();
+ if (@$urlarr["fragment"] != "")
+ $complete .= "#".$urlarr["fragment"];
+
+ return($complete);
+}
+
+function parseurl_getsiteinfo($url, $no_guessing = false, $do_oembed = true, $count = 1) {
+ require_once("include/network.php");
+
+ $a = get_app();
+
+ $siteinfo = array();
+
+ if ($count > 10) {
+ logger("parseurl_getsiteinfo: Endless loop detected for ".$url, LOGGER_DEBUG);
+ return($siteinfo);
}
- logger('parse_url: data: ' . $s, LOGGER_DATA);
+ $url = trim($url, "'");
+ $url = trim($url, '"');
- if(! $s) {
- echo sprintf($template,$url,$url,'');
- killme();
+ $url = original_url($url);
+
+ $siteinfo["url"] = $url;
+ $siteinfo["type"] = "link";
+
+ $stamp1 = microtime(true);
+
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_URL, $url);
+ curl_setopt($ch, CURLOPT_HEADER, 1);
+ curl_setopt($ch, CURLOPT_NOBODY, 1);
+ curl_setopt($ch, CURLOPT_TIMEOUT, 3);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
+ curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
+
+ $header = curl_exec($ch);
+ $curl_info = @curl_getinfo($ch);
+ $http_code = $curl_info['http_code'];
+ curl_close($ch);
+
+ $a->save_timestamp($stamp1, "network");
+
+ if ((($curl_info['http_code'] == "301") OR ($curl_info['http_code'] == "302") OR ($curl_info['http_code'] == "303") OR ($curl_info['http_code'] == "307"))
+ AND (($curl_info['redirect_url'] != "") OR ($curl_info['location'] != ""))) {
+ if ($curl_info['redirect_url'] != "")
+ $siteinfo = parseurl_getsiteinfo($curl_info['redirect_url'], $no_guessing, $do_oembed, ++$count);
+ else
+ $siteinfo = parseurl_getsiteinfo($curl_info['location'], $no_guessing, $do_oembed, ++$count);
+ return($siteinfo);
}
- if(strpos($s,'')) {
- $title = substr($s,strpos($s,'')+7,64);
- if(strpos($title,'<') !== false)
- $title = strip_tags(substr($title,0,strpos($title,'<')));
+ // if the file is too large then exit
+ if ($curl_info["download_content_length"] > 1000000)
+ return($siteinfo);
+
+ // if it isn't a HTML file then exit
+ if (($curl_info["content_type"] != "") AND !strstr(strtolower($curl_info["content_type"]),"html"))
+ return($siteinfo);
+
+ if ($do_oembed) {
+ require_once("include/oembed.php");
+
+ $oembed_data = oembed_fetch_url($url);
+
+ if ($oembed_data->type != "error")
+ $siteinfo["type"] = $oembed_data->type;
+
+ if (($oembed_data->type == "link") AND ($siteinfo["type"] != "photo")) {
+ if (isset($oembed_data->title))
+ $siteinfo["title"] = $oembed_data->title;
+ if (isset($oembed_data->description))
+ $siteinfo["text"] = trim($oembed_data->description);
+ if (isset($oembed_data->thumbnail_url))
+ $siteinfo["image"] = $oembed_data->thumbnail_url;
+ }
}
- $config = HTMLPurifier_Config::createDefault();
- $config->set('Cache.DefinitionImpl', null);
+ $stamp1 = microtime(true);
- $purifier = new HTMLPurifier($config);
- $s = $purifier->purify($s);
+ // Now fetch the body as well
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_URL, $url);
+ curl_setopt($ch, CURLOPT_HEADER, 1);
+ curl_setopt($ch, CURLOPT_NOBODY, 0);
+ curl_setopt($ch, CURLOPT_TIMEOUT, 10);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($ch, CURLOPT_USERAGENT, $a->get_useragent());
-// logger('parse_url: purified: ' . $s, LOGGER_DATA);
+ $header = curl_exec($ch);
+ $curl_info = @curl_getinfo($ch);
+ $http_code = $curl_info['http_code'];
+ curl_close($ch);
- $dom = @HTML5_Parser::parse($s);
+ $a->save_timestamp($stamp1, "network");
- if(! $dom) {
- echo sprintf($template,$url,$url,'');
- killme();
+ // Fetch the first mentioned charset. Can be in body or header
+ $charset = "";
+ if (preg_match('/charset=(.*?)['."'".'"\s\n]/', $header, $matches))
+ $charset = trim(trim(trim(array_pop($matches)), ';,'));
+
+ if ($charset == "")
+ $charset = "utf-8";
+
+ $pos = strpos($header, "\r\n\r\n");
+
+ if ($pos)
+ $body = trim(substr($header, $pos));
+ else
+ $body = $header;
+
+ if (($charset != '') AND (strtoupper($charset) != "UTF-8")) {
+ logger("parseurl_getsiteinfo: detected charset ".$charset, LOGGER_DEBUG);
+ //$body = mb_convert_encoding($body, "UTF-8", $charset);
+ $body = iconv($charset, "UTF-8//TRANSLIT", $body);
}
- $items = $dom->getElementsByTagName('title');
+ $body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
+
+ $doc = new DOMDocument();
+ @$doc->loadHTML($body);
+
+ deletenode($doc, 'style');
+ deletenode($doc, 'script');
+ deletenode($doc, 'option');
+ deletenode($doc, 'h1');
+ deletenode($doc, 'h2');
+ deletenode($doc, 'h3');
+ deletenode($doc, 'h4');
+ deletenode($doc, 'h5');
+ deletenode($doc, 'h6');
+ deletenode($doc, 'ol');
+ deletenode($doc, 'ul');
+
+ $xpath = new DomXPath($doc);
- if($items) {
- foreach($items as $item) {
- $title = trim($item->textContent);
- break;
+ $list = $xpath->query("//meta[@content]");
+ foreach ($list as $node) {
+ $attr = array();
+ if ($node->attributes->length)
+ foreach ($node->attributes as $attribute)
+ $attr[$attribute->name] = $attribute->value;
+
+ if (@$attr["http-equiv"] == 'refresh') {
+ $path = $attr["content"];
+ $pathinfo = explode(";", $path);
+ $content = "";
+ foreach ($pathinfo AS $value) {
+ if (substr(strtolower($value), 0, 4) == "url=")
+ $content = substr($value, 4);
+ }
+ if ($content != "") {
+ $siteinfo = parseurl_getsiteinfo($content, $no_guessing, $do_oembed, ++$count);
+ return($siteinfo);
+ }
}
}
- $divs = $dom->getElementsByTagName('div');
- if($divs) {
- foreach($divs as $div) {
- $class = $div->getAttribute('class');
- if($class && (stristr($class,'article') || stristr($class,'content'))) {
- $items = $div->getElementsByTagName('p');
- if($items) {
- foreach($items as $item) {
- $text = $item->textContent;
- if(stristr($text,'