]> git.mxchange.org Git - friendica-addons.git/blobdiff - gpluspost/gpluspost.php
Merge remote-tracking branch 'upstream/master'
[friendica-addons.git] / gpluspost / gpluspost.php
index 22fccdefc2f6921d27a33ebf5088224059118d33..5a638ed9fadd7144c2dc1306b2f0919627af327e 100644 (file)
@@ -2,7 +2,7 @@
 
 /**
  * Name: G+ Post
- * Description: Posts to a Google+ page with the help of Seesmic
+ * Description: Posts to a Google+ page with the help of Hootsuite
  * Version: 0.1
  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
  */
@@ -42,6 +42,10 @@ function gpluspost_settings(&$a,&$s) {
        if(! local_user())
                return;
 
+       /* Add our stylesheet to the page so we can make our settings look nice */
+
+       $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/gpluspost/gpluspost.css' . '" media="all" />' . "\r\n";
+
        $enabled = get_pconfig(local_user(),'gpluspost','post');
        $checked = (($enabled) ? ' checked="checked" ' : '');
 
@@ -51,8 +55,17 @@ function gpluspost_settings(&$a,&$s) {
        $noloop_enabled = get_pconfig(local_user(),'gpluspost','no_loop_prevention');
        $noloop_checked = (($noloop_enabled) ? ' checked="checked" ' : '');
 
-       $s .= '<div class="settings-block">';
+       $skip_enabled = get_pconfig(local_user(),'gpluspost','skip_without_link');
+       $skip_checked = (($skip_enabled) ? ' checked="checked" ' : '');
+
+       $s .= '<span id="settings_gpluspost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_gpluspost_expanded\'); openClose(\'settings_gpluspost_inflated\');">';
        $s .= '<h3>' . t('Google+ Post Settings') . '</h3>';
+       $s .= '</span>';
+       $s .= '<div id="settings_gpluspost_expanded" class="settings-block" style="display: none;">';
+       $s .= '<span class="fakelink" onclick="openClose(\'settings_gpluspost_expanded\'); openClose(\'settings_gpluspost_inflated\');">';
+       $s .= '<h3>' . t('Google+ Post Settings') . '</h3>';
+       $s .= '</span>';
+
        $s .= '<div id="gpluspost-enable-wrapper">';
        $s .= '<label id="gpluspost-enable-label" for="gpluspost-checkbox">' . t('Enable Google+ Post Plugin') . '</label>';
        $s .= '<input id="gpluspost-checkbox" type="checkbox" name="gpluspost" value="1" ' . $checked . '/>';
@@ -68,11 +81,16 @@ function gpluspost_settings(&$a,&$s) {
        $s .= '<input id="gpluspost-noloopprevention" type="checkbox" name="gpluspost_noloopprevention" value="1" ' . $noloop_checked . '/>';
        $s .= '</div><div class="clear"></div>';
 
+       $s .= '<div id="gpluspost-skipwithoutlink-wrapper">';
+       $s .= '<label id="gpluspost-skipwithoutlink-label" for="gpluspost-skipwithoutlink">' . t('Skip messages without links') . '</label>';
+       $s .= '<input id="gpluspost-skipwithoutlink" type="checkbox" name="gpluspost_skipwithoutlink" value="1" ' . $skip_checked . '/>';
+       $s .= '</div><div class="clear"></div>';
+
        /* provide a submit button */
 
-       $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="gpluspost-submit" name="gpluspost-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
-       $s .= 'Register an account at <a href="https://hootsuite.com">Hootsuite</a>, add your G+ page and add the feed-url there.<br />';
-       $s .= 'Feed-url: '.$a->get_baseurl().'/gpluspost/'.urlencode($a->user["nickname"]).'</div>';
+       $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="gpluspost-submit" name="gpluspost-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div>';
+       $s .= '<p>Register an account at <a href="https://hootsuite.com">Hootsuite</a>, add your G+ page and add the feed-url there.<br />';
+       $s .= 'Feed-url: '.$a->get_baseurl().'/gpluspost/'.urlencode($a->user["nickname"]).'</p></div>';
 }
 
 function gpluspost_settings_post(&$a,&$b) {
@@ -81,6 +99,7 @@ function gpluspost_settings_post(&$a,&$b) {
                set_pconfig(local_user(),'gpluspost','post',intval($_POST['gpluspost']));
                set_pconfig(local_user(),'gpluspost','post_by_default',intval($_POST['gpluspost_bydefault']));
                set_pconfig(local_user(),'gpluspost','no_loop_prevention',intval($_POST['gpluspost_noloopprevention']));
+               set_pconfig(local_user(),'gpluspost','skip_without_link',intval($_POST['gpluspost_skipwithoutlink']));
        }
 }
 
@@ -154,13 +173,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 +189,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,30 +214,63 @@ function gpluspost_init() {
        killme();
 }
 
-function gpluspost_ShareAttributes($match) {
-
-        $attributes = $match[1];
+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,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0');
+
+       $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));
+       }
 
-        $author = "";
-        preg_match("/author='(.*?)'/ism", $attributes, $matches);
-        if ($matches[1] != "")
-                $author = $matches[1];
+       $pos = strpos($header, "\r\n\r\n");
 
-        preg_match('/author="(.*?)"/ism', $attributes, $matches);
-        if ($matches[1] != "")
-                $author = $matches[1];
+       if ($pos)
+               $body = trim(substr($header, $pos));
+       else
+               $body = $header;
 
-        $headline = '<div class="shared_header">';
+       $doc = new DOMDocument();
+       @$doc->loadHTML($body);
 
-        $headline .= sprintf(t('%s:'), $author);
+       $xpath = new DomXPath($doc);
 
-        $headline .= "</div>";
+       $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;
 
-        //$text = "<br />".$headline."</strong><blockquote>".$match[2]."</blockquote>";
-       //$text = "\n\t".$match[2].":\t";
-       $text = $author.": ".$match[2];
+               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($text);
+       return($url);
 }
 
 function gpluspost_feeditem($pid, $uid) {
@@ -227,7 +279,7 @@ function gpluspost_feeditem($pid, $uid) {
        require_once('include/bbcode.php');
        require_once("include/html2plain.php");
 
-       $max_char = 140;
+       $skipwithoutlink = get_pconfig($uid,'gpluspost','skip_without_link');
 
        $items = q("SELECT `uri`, `plink`, `author-link`, `author-name`, `created`, `edited`, `id`, `title`, `body` from `item` WHERE id=%d", intval($pid));
        foreach ($items AS $item) {
@@ -255,10 +307,13 @@ function gpluspost_feeditem($pid, $uid) {
                $multiplelinks = (strpos($item['body'], "[bookmark") != strrpos($item['body'], "[bookmark"));
 
                $body = $item['body'];
-               $body = preg_replace_callback("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]/ism","gpluspost_ShareAttributes", $body);
 
-               $html = bbcode($body, false, false);
+               // At first convert the text to html
+               $html = bbcode($body, false, false, 2);
+
+               // Then convert it to plain text
                $msg = trim(html2plain($html, 0, true));
+               $msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
 
                // If there is no bookmark element then take the first link
                if ($link == '') {
@@ -268,6 +323,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 = "";
@@ -280,12 +345,11 @@ function gpluspost_feeditem($pid, $uid) {
                else if ($image != "")
                        $msglink = $image;
 
-               if ($msglink == "")
+               if (($msglink == "") AND $skipwithoutlink)
+                       continue;
+               else 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 +358,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);