]> git.mxchange.org Git - friendica-addons.git/blobdiff - fromgplus/fromgplus.php
fromgplus: Using the new function to upload pictures to the wall
[friendica-addons.git] / fromgplus / fromgplus.php
index 711a73c3acd2d7c26d0d082018fecdfb4be9dff9..137b294042b7bb19eca920d90de5456ba673b5ab 100644 (file)
@@ -30,11 +30,22 @@ function fromgplus_addon_settings(&$a,&$s) {
        if(! local_user())
                return;
 
+       // If "gpluspost" is installed as well, then the settings are displayed there
+       $result = q("SELECT `installed` FROM `addon` WHERE `name` = 'gpluspost' AND `installed`");
+       if (count($result) > 0)
+               return;
+
        $enable_checked = (intval(get_pconfig(local_user(),'fromgplus','enable')) ? ' checked="checked"' : '');
        $account = get_pconfig(local_user(),'fromgplus','account');
 
-       $s .= '<div class="settings-block">';
-       $s .= '<h3>' . t('Google+ Import Settings').'</h3>';
+       $s .= '<span id="settings_fromgplus_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_fromgplus_expanded\'); openClose(\'settings_fromgplus_inflated\');">';
+       $s .= '<img class="connector" src="images/googleplus.png" /><h3 class="connector">'. t('Google+ Mirror').'</h3>';
+       $s .= '</span>';
+       $s .= '<div id="settings_fromgplus_expanded" class="settings-block" style="display: none;">';
+       $s .= '<span class="fakelink" onclick="openClose(\'settings_fromgplus_expanded\'); openClose(\'settings_fromgplus_inflated\');">';
+       $s .= '<img class="connector" src="images/googleplus.png" /><h3 class="connector">'. t('Google+ Mirror').'</h3>';
+       $s .= '</span>';
+
        $s .= '<div id="fromgplus-wrapper">';
 
        $s .= '<label id="fromgplus-enable-label" for="fromgplus-enable">'.t('Enable Google+ Import').'</label>';
@@ -45,7 +56,7 @@ function fromgplus_addon_settings(&$a,&$s) {
        $s .= '</div><div class="clear"></div>';
 
        $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="fromgplus-submit" name="fromgplus-submit" 
-class="settings-submit" value="' . t('Submit') . '" /></div>';
+class="settings-submit" value="' . t('Save Settings') . '" /></div>';
        $s .= '</div>';
 
        return;
@@ -60,6 +71,10 @@ function fromgplus_addon_settings_post(&$a,&$b) {
                set_pconfig(local_user(),'fromgplus','account',trim($_POST['fromgplus-account']));
                $enable = ((x($_POST,'fromgplus-enable')) ? intval($_POST['fromgplus-enable']) : 0);
                set_pconfig(local_user(),'fromgplus','enable', $enable);
+
+               if (!$enable)
+                       del_pconfig(local_user(),'fromgplus','lastdate');
+
                info( t('Google+ Import Settings saved.') . EOL);
        }
 }
@@ -180,14 +195,15 @@ function fromgplus_parse_query($var)
 }
 
 function fromgplus_cleanupgoogleproxy($fullImage, $image) {
+       //$preview = "/w".$fullImage->width."-h".$fullImage->height."/";
+       //$preview2 = "/w".$fullImage->width."-h".$fullImage->height."-p/";
+       //$fullImage = str_replace(array($preview, $preview2), array("/", "/"), $fullImage->url);
+       $fullImage = $fullImage->url;
 
-       $preview = "/w".$fullImage->width."-h".$fullImage->height."/";
-       $preview2 = "/w".$fullImage->width."-h".$fullImage->height."-p/";
-       $fullImage = str_replace(array($preview, $preview2), array("/", "/"), $fullImage->url);
-
-       $preview = "/w".$image->width."-h".$image->height."/";
-       $preview2 = "/w".$image->width."-h".$image->height."-p/";
-       $image = str_replace(array($preview, $preview2), array("/", "/"), $image->url);
+       //$preview = "/w".$image->width."-h".$image->height."/";
+       //$preview2 = "/w".$image->width."-h".$image->height."-p/";
+       //$image = str_replace(array($preview, $preview2), array("/", "/"), $image->url);
+       $image = $image->url;
 
                $cleaned = array();
 
@@ -212,7 +228,23 @@ function fromgplus_cleanupgoogleproxy($fullImage, $image) {
                $cleaned["preview"] = "";
        }
 
-       if ($cleaned["full"] == $cleaned["preview"])
+       if ($cleaned["full"] != "")
+               $infoFull = get_photo_info($cleaned["full"]);
+       else
+               $infoFull = array("0" => 0, "1" => 0);
+
+       if ($cleaned["preview"] != "")
+               $infoPreview = get_photo_info($cleaned["preview"]);
+       else
+               $infoFull = array("0" => 0, "1" => 0);
+
+       if (($infoPreview[0] >= $infoFull[0]) AND ($infoPreview[1] >= $infoFull[1])) {
+               $temp = $cleaned["full"];
+               $cleaned["full"] = $cleaned["preview"];
+               $cleaned["preview"] = $temp;
+       }
+
+       if (($cleaned["full"] == $cleaned["preview"]) OR (($infoPreview[0] == $infoFull[0]) AND ($infoPreview[1] == $infoFull[1])))
                $cleaned["preview"] = "";
 
        if ($cleaned["full"] == "")
@@ -221,7 +253,10 @@ function fromgplus_cleanupgoogleproxy($fullImage, $image) {
 
        if ($cleaned["full"] == "")
                if (@exif_imagetype($image) != 0)
-                       $cleaned["full"] = $fullImage;
+                       $cleaned["full"] = $image;
+
+       // Could be changed in the future to a link to the album
+       $cleaned["page"] = $cleaned["full"];
 
        return($cleaned);
 }
@@ -232,50 +267,52 @@ function fromgplus_cleantext($text) {
        $trash = html_entity_decode("&#xFEFF;", ENT_QUOTES, 'UTF-8');
 
        $text = strip_tags($text);
-       $text = html_entity_decode($text);
+       $text = html_entity_decode($text, ENT_QUOTES);
        $text = trim($text);
        $text = str_replace(array("\n", "\r", " ", $trash), array("", "", "", ""), $text);
        return($text);
 }
 
-function fromgplus_handleattachments($item, $displaytext) {
+function fromgplus_handleattachments($a, $uid, $item, $displaytext, $shared) {
+       require_once("include/Photo.php");
+
        $post = "";
        $quote = "";
+       $type = "";
 
        foreach ($item->object->attachments as $attachment) {
                switch($attachment->objectType) {
                        case "video":
-                               $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
-
-                               /*$images = cleanupgoogleproxy($attachment->fullImage, $attachment->image);
-                               if ($images["preview"] != "")
-                                       $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
-                               elseif ($images["full"] != "")
-                                       $post .= "\n[img]".$images["full"]."[/img]\n";*/
-
+                               $post .= "\n[class=type-video][bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n[/class]";
                                break;
 
                        case "article":
-                               $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
+                               $post .= "\n[class=type-link][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"] != "")
-                               //      $post .= "\n[img]".$images["full"]."[/img]\n";
                                if ($images["full"] != "")
-                                       $post .= "\n[img]".$images["full"]."[/img]\n";
+                                       $post .= "\n[img]".$images["full"]."[/img]";
 
-                               //$post .= "[quote]".trim(fromgplus_html2bbcode($attachment->content))."[/quote]";
                                $quote = trim(fromgplus_html2bbcode($attachment->content));
                                if ($quote != "")
                                        $quote = "\n[quote]".$quote."[/quote]";
+
+                               $quote .= "[/class]";
                                break;
 
                        case "photo":
-                               $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
+                               // Don't store shared pictures in your wall photos (to prevent a possible violating of licenses)
+                               if ($shared)
+                                       $images = fromgplus_cleanupgoogleproxy($attachment->fullImage, $attachment->image);
+                               else {
+                                       if ($attachment->fullImage->url != "")
+                                               $images = store_photo($a, $uid, "", $attachment->fullImage->url);
+                                       elseif ($attachment->image->url != "")
+                                               $images = store_photo($a, $uid, "", $attachment->image->url);
+                               }
+
                                if ($images["preview"] != "")
-                                       $post .= "\n[url=".$images["full"]."][img]".$images["preview"]."[/img][/url]\n";
+                                       $post .= "\n[url=".$images["page"]."][img]".$images["preview"]."[/img][/url]\n";
                                elseif ($images["full"] != "")
                                        $post .= "\n[img]".$images["full"]."[/img]\n";
 
@@ -295,13 +332,23 @@ function fromgplus_handleattachments($item, $displaytext) {
                                break;
 
                        case "album":
-                               foreach($attachment->thumbnails as $thumb) {
-                                       $preview = "/w".$thumb->image->width."-h".$thumb->image->height."/";
-                                       $preview2 = "/w".$thumb->image->width."-h".$thumb->image->height."-p/";
-                                       $image = str_replace(array($preview, $preview2), array("/", "/"), $thumb->image->url);
+                               $post .= "\n[class=type-link][bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]";
 
-                                       $post .= "\n[url=".$thumb->url."][img]".$image."[/img][/url]\n";
-                               }
+                               $thumb = $attachment->thumbnails[0];
+                               $post .= "\n[img]".$thumb->image->url."[/img]";
+
+                               $quote = trim(fromgplus_html2bbcode($thumb->description));
+                               if ($quote != "")
+                                       $quote = "\n[quote]".$quote."[/quote]";
+
+                               //foreach($attachment->thumbnails as $thumb) {
+                               //      $preview = "/w".$thumb->image->width."-h".$thumb->image->height."/";
+                               //      $preview2 = "/w".$thumb->image->width."-h".$thumb->image->height."-p/";
+                               //      $image = str_replace(array($preview, $preview2), array("/", "/"), $thumb->image->url);
+
+                               //      $post .= "\n[url=".$thumb->url."][img]".$image."[/img][/url]\n";
+                               //}
+                               $quote .= "[/class]";
                                break;
                        case "audio":
                                $post .= "\n\n[bookmark=".$attachment->url."]".fromgplus_html2bbcode($attachment->displayName)."[/bookmark]\n";
@@ -324,6 +371,7 @@ function fromgplus_fetch($a, $uid) {
 
        $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");
+       //$result = file_get_contents("addon/fromgplus/album.txt");
        //file_put_contents("google.txt", $result);
 
        $activities = json_decode($result);
@@ -364,7 +412,7 @@ function fromgplus_fetch($a, $uid) {
                                        $post = fromgplus_html2bbcode($item->object->content);
 
                                        if (is_array($item->object->attachments))
-                                               $post .= fromgplus_handleattachments($item, $item->object->content);
+                                               $post .= fromgplus_handleattachments($a, $uid, $item, $item->object->content, false);
 
                                        // geocode, placeName
                                        if (isset($item->address))
@@ -389,7 +437,7 @@ function fromgplus_fetch($a, $uid) {
                                                $post .= fromgplus_html2bbcode($item->object->content);
 
                                                if (is_array($item->object->attachments))
-                                                       $post .= "\n".trim(fromgplus_handleattachments($item, $item->object->content));
+                                                       $post .= "\n".trim(fromgplus_handleattachments($a, $uid, $item, $item->object->content, true));
 
                                                $post .= "[/share]";
                                        } else {
@@ -398,7 +446,7 @@ function fromgplus_fetch($a, $uid) {
                                                $post .= fromgplus_html2bbcode($item->object->content);
 
                                                if (is_array($item->object->attachments))
-                                                       $post .= "\n".trim(fromgplus_handleattachments($item, $item->object->content));
+                                                       $post .= "\n".trim(fromgplus_handleattachments($a, $uid, $item, $item->object->content, true));
                                        }
 
                                        if (isset($item->address))
@@ -414,22 +462,3 @@ function fromgplus_fetch($a, $uid) {
        if ($lastdate != 0)
                set_pconfig($uid,'fromgplus','lastdate', $lastdate);
 }
-
-/*
-// Test
-require_once("boot.php");
-
-if(@is_null($a)) {
-        $a = new App;
-}
-
-if(@is_null($db)) {
-        @include(".htconfig.php");
-        require_once("include/dba.php");
-        $db = new dba($db_host, $db_user, $db_pass, $db_data);
-        unset($db_host, $db_user, $db_pass, $db_data);
-};
-
-$test = array();
-fromgplus_cron($a, $test);
-*/