]> git.mxchange.org Git - friendica-addons.git/blobdiff - gpluspost/gpluspost.php
Merge pull request #110 from annando/master
[friendica-addons.git] / gpluspost / gpluspost.php
index 76cf237bac0179f956835bac19132433f48117c9..db240aa3eeee20229e0645ec3d61ba4237129ffa 100644 (file)
@@ -154,13 +154,13 @@ function gpluspost_init() {
        if (isset($a->argv[1])) {
                $uid = (int)$a->argv[1];
                if ($uid == 0) {
-                       $contacts = q("SELECT `name`, `id` FROM contact WHERE `nick` = '%s' LIMIT 1", dbesc($a->argv[1]));
+                       $contacts = q("SELECT `username`, `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1", dbesc($a->argv[1]));
                        if ($contacts) {
-                               $uid = $contacts[0]["id"];
+                               $uid = $contacts[0]["uid"];
                                $nick = $a->argv[1];
                        }
                } else {
-                       $contacts = q("SELECT `name` FROM contact WHERE ID=%d LIMIT 1", intval($uid));
+                       $contacts = q("SELECT `username` FROM `user` WHERE `uid`=%d LIMIT 1", intval($uid));
                        $nick = $uid;
                }
        }
@@ -170,7 +170,7 @@ function gpluspost_init() {
        echo '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
        echo "\t".'<title type="html"><![CDATA['.$a->config['sitename'].']]></title>'."\n";
        if ($uid != 0) {
-               echo "\t".'<subtitle type="html"><![CDATA['.$contacts[0]["name"]."]]></subtitle>\n";
+               echo "\t".'<subtitle type="html"><![CDATA['.$contacts[0]["username"]."]]></subtitle>\n";
                echo "\t".'<link rel="self" href="'.$a->get_baseurl().'/gpluspost/'.$nick.'"/>'."\n";
        } else
                echo "\t".'<link rel="self" href="'.$a->get_baseurl().'/gpluspost"/>'."\n";
@@ -195,6 +195,65 @@ function gpluspost_init() {
        killme();
 }
 
+function gpluspost_original_url($url, $depth=1) {
+
+       if ($depth > 10)
+               return($url);
+
+       $siteinfo = array();
+       $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, 3);
+       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+       curl_setopt($ch,CURLOPT_USERAGENT,'Opera/9.64(Windows NT 5.1; U; de) Presto/2.1.1');
+
+       $header = curl_exec($ch);
+       $curl_info = @curl_getinfo($ch);
+       $http_code = $curl_info['http_code'];
+       curl_close($ch);
+
+       if ((($curl_info['http_code'] == "301") OR ($curl_info['http_code'] == "302"))
+               AND (($curl_info['redirect_url'] != "") OR ($curl_info['location'] != ""))) {
+               if ($curl_info['redirect_url'] != "")
+                       return(gpluspost_original_url($curl_info['redirect_url'], ++$depth));
+               else
+                       return(gpluspost_original_url($curl_info['location'], ++$depth));
+       }
+
+       $pos = strpos($header, "\r\n\r\n");
+
+       if ($pos)
+               $body = trim(substr($header, $pos));
+       else
+               $body = $header;
+
+       $doc = new DOMDocument();
+       @$doc->loadHTML($body);
+
+       $xpath = new DomXPath($doc);
+
+       $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=")
+                                       return(gpluspost_original_url(substr($value, 4), ++$depth));
+               }
+       }
+
+       return($url);
+}
+
 function gpluspost_ShareAttributes($match) {
 
         $attributes = $match[1];
@@ -268,6 +327,16 @@ function gpluspost_feeditem($pid, $uid) {
                                $link = current($links);
                        }
                        $multiplelinks = (sizeof($links) > 1);
+
+                       if ($multiplelinks) {
+                               $html2 = bbcode($msg, false, false);
+                               $links2 = collecturls($html2);
+                               if (sizeof($links2) > 0) {
+                                       reset($links2);
+                                       $link = current($links2);
+                                       $multiplelinks = (sizeof($links2) > 1);
+                               }
+                       }
                }
 
                $msglink = "";
@@ -283,9 +352,6 @@ function gpluspost_feeditem($pid, $uid) {
                if ($msglink == "")
                        $msglink = $item["plink"];
 
-               if ($image != $msglink)
-                       $html = trim(str_replace($msglink, "", $html));
-
                // Fetching the title - or the first line
                if ($item["title"] != "")
                        $title = $item["title"];
@@ -294,9 +360,18 @@ function gpluspost_feeditem($pid, $uid) {
                        $title = $lines[0];
                }
 
+               //if ($image != $msglink)
+               //      $html = trim(str_replace($msglink, "", $html));
+
+               $title = trim(str_replace($msglink, "", $title));
+
+               $msglink = gpluspost_original_url($msglink);
+
                if ($uid == 0)
                        $title = $item["author-name"].": ".$title;
 
+               $msglink = htmlspecialchars(html_entity_decode($msglink));
+
                $title = str_replace("&", "&amp;", $title);
                //$html = str_replace("&", "&amp;", $html);