]> git.mxchange.org Git - friendica-addons.git/blobdiff - fromgplus/fromgplus.php
Merge pull request #151 from tazmandevil/master
[friendica-addons.git] / fromgplus / fromgplus.php
index ab93b04230f8ad5ad7f1a18b6414411bf6a75047..9fc13c5ec97e8bad1946831d7cf9a5514d938ae7 100644 (file)
@@ -1,13 +1,13 @@
 <?php
 /**
  * Name: From GPlus
- * Description: Imports posts from a Google+ account and repeats them - not working by now
+ * Description: Imports posts from a Google+ account and repeats them
  * Version: 0.1
  * Author: Michael Vogel <ike@piratenpartei.de>
  *
  */
 
-define('FACEBOOK_DEFAULT_POLL_INTERVAL', 60); // given in minutes
+define('FROMGPLUS_DEFAULT_POLL_INTERVAL', 30); // given in minutes
 
 function fromgplus_install() {
        register_hook('plugin_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings');
@@ -95,7 +95,7 @@ function fromgplus_cron($a,$b) {
 
 function fromgplus_post($a, $uid, $source, $body, $location) {
 
-       $uid = 2;
+       //$uid = 2;
 
        $body = trim($body);
 
@@ -109,6 +109,7 @@ function fromgplus_post($a, $uid, $source, $body, $location) {
        $_SESSION['authenticated'] = true;
        $_SESSION['uid'] = $uid;
 
+       unset($_REQUEST);
        $_REQUEST['type'] = 'wall';
        $_REQUEST['api_source'] = true;
 
@@ -123,11 +124,16 @@ function fromgplus_post($a, $uid, $source, $body, $location) {
        $_REQUEST['body'] = $body;
        $_REQUEST['location'] = $location;
 
-        logger('fromgplus: posting for user '.$uid);
+       if (($_REQUEST['title'] == "") AND ($_REQUEST['body'] == "")) {
+               logger('fromgplus: empty post for user '.$uid." ".print_r($_REQUEST, true));
+               return;
+       }
 
        require_once('mod/item.php');
        //print_r($_REQUEST);
+        logger('fromgplus: posting for user '.$uid." ".print_r($_REQUEST, true));
        item_post($a);
+        logger('fromgplus: done for user '.$uid);
 }
 
 function fromgplus_html2bbcode($html) {
@@ -212,7 +218,15 @@ function fromgplus_cleanupgoogleproxy($fullImage, $image) {
        return($cleaned);
 }
 
-function fromgplus_handleattachments($item) {
+function fromgplus_cleantext($text) {
+       $text = strip_tags($text);
+       $text = html_entity_decode($text);
+       $text = trim($text);
+       $text = str_replace(array("\n", "\r", " "), array("", "", ""), $text);
+       return($text);
+}
+
+function fromgplus_handleattachments($item, $displaytext) {
        $post = "";
        $quote = "";
 
@@ -233,9 +247,11 @@ function fromgplus_handleattachments($item) {
                                $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
 
                                $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
-                               if ($images["preview"] != "")
-                                       $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
-                               elseif ($images["full"] != "")
+                               //if ($images["preview"] != "")
+                               //      $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
+                               //elseif ($images["full"] != "")
+                               //      $post .= "\n[img]".$images["full"]."[/img]\n";
+                               if ($images["full"] != "")
                                        $post .= "\n[img]".$images["full"]."[/img]\n";
 
                                //$post .= "[quote]".trim(fromgplus_html2bbcode($attachment->content))."[/quote]";
@@ -251,7 +267,7 @@ function fromgplus_handleattachments($item) {
                                elseif ($images["full"] != "")
                                        $post .= "\n[img]".$images["full"]."[/img]\n";
 
-                               if ($attachment->displayName != "")
+                               if (($attachment->displayName != "") AND (fromgplus_cleantext($attachment->displayName) != fromgplus_cleantext($displaytext)))
                                        $post .= fromgplus_html2bbcode($attachment->displayName)."\n";
                                break;
 
@@ -275,6 +291,9 @@ function fromgplus_handleattachments($item) {
                                        $post .= "\n[url=".$thumb->url."][img]".$image."[/img][/url]\n";
                                }
                                break;
+                       case "audio":
+                               $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
+                               break;
                        //default:
                        //      die($attachment->objectType);
                }
@@ -288,7 +307,7 @@ function fromgplus_fetch($a, $uid) {
        $account = get_pconfig($uid,'fromgplus','account');
        $key = get_config('fromgplus','key');
 
-       $result = file_get_contents("https://www.googleapis.com/plus/v1/people/".$account."/activities/public?alt=json&pp=1&key=".$key."&maxResults=".$maxfetch);
+       $result = fetch_url("https://www.googleapis.com/plus/v1/people/".$account."/activities/public?alt=json&pp=1&key=".$key."&maxResults=".$maxfetch);
        //$result = file_get_contents("google.txt");
        //file_put_contents("google.txt", $result);
 
@@ -298,6 +317,9 @@ function fromgplus_fetch($a, $uid) {
 
        $lastdate = 0;
 
+       if (!is_array($activities->items))
+               return;
+
        $reversed = array_reverse($activities->items);
 
        foreach($reversed as $item) {
@@ -313,7 +335,7 @@ function fromgplus_fetch($a, $uid) {
                                        $post = fromgplus_html2bbcode($item->object->content);
 
                                        if (is_array($item->object->attachments))
-                                               $post .= fromgplus_handleattachments($item);
+                                               $post .= fromgplus_handleattachments($item, $item->object->content);
 
                                        // geocode, placeName
                                        if (isset($item->address))
@@ -321,27 +343,46 @@ function fromgplus_fetch($a, $uid) {
                                        else
                                                $location = "";
 
-                                       fromgplus_post($a, $uid, $item->provider->title, $post, $location);
+                                       // Loop prevention - should be made better
+                                       if ($item->provider->title != "HootSuite")
+                                               fromgplus_post($a, $uid, "Google+", $post, $location);
+                                       //fromgplus_post($a, $uid, $item->provider->title, $post, $location);
 
                                        break;
 
                                case "activity":
                                        $post = fromgplus_html2bbcode($item->annotation)."\n";
-                                       $post .= fromgplus_html2bbcode("&#x2672;");
-                                       //$post .= html2bbcode("&#x267B;");
-                                       //$post .= fromgplus_html2bbcode("&#x25CC;");
-                                       $post .= " [url=".$item->object->actor->url."]".$item->object->actor->displayName."[/url] \n";
-                                       $post .= fromgplus_html2bbcode($item->object->content);
 
-                                       if (is_array($item->object->attachments))
-                                               $post .= "\n".trim(fromgplus_handleattachments($item));
+                                       if (intval(get_config('system','new_share'))) {
+                                               $post .= "[share author='".str_replace("'", "&#039;",$item->object->actor->displayName).
+                                                               "' profile='".$item->object->actor->url.
+                                                               "' avatar='".$item->object->actor->image->url.
+                                                               "' link='".$item->object->url."']";
+
+                                               $post .= fromgplus_html2bbcode($item->object->content);
+
+                                               if (is_array($item->object->attachments))
+                                                       $post .= "\n".trim(fromgplus_handleattachments($item, $item->object->content));
+
+                                               $post .= "[/share]";
+                                       } else {
+                                               $post .= fromgplus_html2bbcode("&#x2672;");
+                                               $post .= " [url=".$item->object->actor->url."]".$item->object->actor->displayName."[/url] \n";
+                                               $post .= fromgplus_html2bbcode($item->object->content);
+
+                                               if (is_array($item->object->attachments))
+                                                       $post .= "\n".trim(fromgplus_handleattachments($item, $item->object->content));
+                                       }
 
                                        if (isset($item->address))
                                                $location = $item->address;
                                        else
                                                $location = "";
 
-                                       fromgplus_post($a, $uid, $item->provider->title, $post, $location);
+                                       // Loop prevention - should be made better
+                                       if ($item->provider->title != "HootSuite")
+                                               fromgplus_post($a, $uid, "Google+", $post, $location);
+                                       //fromgplus_post($a, $uid, $item->provider->title, $post, $location);
                                        break;
                        }
        }
@@ -351,11 +392,7 @@ function fromgplus_fetch($a, $uid) {
 
 /*
 // Test
-$test1 = array();
-$test2 = array();
 require_once("boot.php");
-require_once("include/bbcode.php");
-require_once("include/html2plain.php");
 
 if(@is_null($a)) {
         $a = new App;
@@ -368,5 +405,6 @@ if(@is_null($db)) {
         unset($db_host, $db_user, $db_pass, $db_data);
 };
 
-fromgplus_cron($a, $test2);
+$test = array();
+fromgplus_cron($a, $test);
 */