]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/FacebookBridge/lib/facebookclient.php
Merge branch '1.0.x' of gitorious.org:statusnet/mainline into 1.0.x
[quix0rs-gnu-social.git] / plugins / FacebookBridge / lib / facebookclient.php
index 33edf5c6b125e7f2b9b78667826b9de7b354d1a4..0469ce8aca2aa7d98c9cebc8e2d8135a09630ed7 100644 (file)
@@ -48,17 +48,32 @@ class Facebookclient
     protected $notice        = null; // The user's notice
     protected $user          = null; // Sender of the notice
 
-    function __construct($notice)
+    /**
+     *
+     * @param Notice $notice the notice to manipulate
+     * @param Profile $profile local user to act as; if left empty, the notice's poster will be used.
+     */
+    function __construct($notice, $profile=null)
     {
         $this->facebook = self::getFacebook();
-        $this->notice   = $notice;
 
+        if (empty($this->facebook)) {
+            throw new FacebookApiException(
+                "Could not create Facebook client! Bad application ID or secret?"
+            );
+        }
+
+        $this->notice = $notice;
+
+        $profile_id = $profile ? $profile->id : $notice->profile_id;
         $this->flink = Foreign_link::getByUserID(
-            $notice->profile_id,
+            $profile_id,
             FACEBOOK_SERVICE
         );
 
-        $this->user = $this->flink->getUser();
+        if (!empty($this->flink)) {
+            $this->user = $this->flink->getUser();
+        }
     }
 
     /*
@@ -87,6 +102,22 @@ class Facebookclient
             $secret = common_config('facebook', 'global_secret');
         }
 
+        if (empty($appId)) {
+            common_log(
+                LOG_WARNING,
+                "Couldn't find Facebook application ID!",
+                __FILE__
+            );
+        }
+
+        if (empty($secret)) {
+            common_log(
+                LOG_WARNING,
+                "Couldn't find Facebook application ID!",
+                __FILE__
+            );
+        }
+
         return new Facebook(
             array(
                'appId'  => $appId,
@@ -103,7 +134,6 @@ class Facebookclient
      */
     static function facebookBroadcastNotice($notice)
     {
-        common_debug('Facebook broadcast');
         $client = new Facebookclient($notice);
         return $client->sendNotice();
     }
@@ -114,14 +144,7 @@ class Facebookclient
     function isFacebookBound() {
 
         if (empty($this->flink)) {
-            common_log(
-                LOG_WARN,
-                sprintf(
-                    "No Foreign_link to Facebook for the author of notice %d.",
-                    $this->notice->id
-                ),
-                __FILE__
-            );
+            // User hasn't setup bridging
             return false;
         }
 
@@ -179,16 +202,10 @@ class Facebookclient
                 // Otherwise we most likely have an access token
                 return $this->sendGraph();
             }
-
-        } else {
-            common_debug(
-                sprintf(
-                    "Skipping notice %d - not bound for Facebook",
-                    $this->notice->id,
-                    __FILE__
-                )
-            );
         }
+
+        // dequeue
+        return true;
     }
 
     /*
@@ -202,7 +219,7 @@ class Facebookclient
 
             common_debug(
                 sprintf(
-                    "Attempting use Graph API to post notice %d as a stream item for %s (%d), fbuid %s",
+                    "Attempting use Graph API to post notice %d as a stream item for %s (%d), fbuid %d",
                     $this->notice->id,
                     $this->user->nickname,
                     $this->user->id,
@@ -247,7 +264,7 @@ class Facebookclient
             common_log(
                 LOG_INFO,
                 sprintf(
-                    "Posted notice %d as a stream item for %s (%d), fbuid %s",
+                    "Posted notice %d as a stream item for %s (%d), fbuid %d",
                     $this->notice->id,
                     $this->user->nickname,
                     $this->user->id,
@@ -287,7 +304,7 @@ class Facebookclient
             } else {
 
                 $msg = 'Not sending notice %d to Facebook because user %s '
-                     . '(%d), fbuid %s,  does not have \'status_update\' '
+                     . '(%d), fbuid %d,  does not have \'status_update\' '
                      . 'or \'publish_stream\' permission.';
 
                 common_log(
@@ -323,14 +340,15 @@ class Facebookclient
     function checkPermission($permission)
     {
         if (!in_array($permission, array('publish_stream', 'status_update'))) {
-             throw new ServerException("No such permission!");
+             // TRANS: Server exception thrown when permission check fails.
+             throw new ServerException(_('No such permission!'));
         }
 
         $fbuid = $this->flink->foreign_id;
 
         common_debug(
             sprintf(
-                'Checking for %s permission for user %s (%d), fbuid %s',
+                'Checking for %s permission for user %s (%d), fbuid %d',
                 $permission,
                 $this->user->nickname,
                 $this->user->id,
@@ -351,7 +369,7 @@ class Facebookclient
 
             common_debug(
                 sprintf(
-                    '%s (%d), fbuid %s has %s permission',
+                    '%s (%d), fbuid %d has %s permission',
                     $permission,
                     $this->user->nickname,
                     $this->user->id,
@@ -380,7 +398,6 @@ class Facebookclient
             );
 
             return false;
-
         }
     }
 
@@ -444,7 +461,7 @@ class Facebookclient
                     ),
                     __FILE__
                 );
-            // @fixme: We want to rety at a later time when the throttling has expired
+            // @todo FIXME: We want to rety at a later time when the throttling has expired
             // instead of just giving up.
             return true;
             break;
@@ -485,7 +502,7 @@ class Facebookclient
 
         common_debug(
             sprintf(
-                "Attempting to post notice %d as a status update for %s (%d), fbuid %s",
+                "Attempting to post notice %d as a status update for %s (%d), fbuid %d",
                 $this->notice->id,
                 $this->user->nickname,
                 $this->user->id,
@@ -508,7 +525,7 @@ class Facebookclient
             common_log(
                 LOG_INFO,
                 sprintf(
-                    "Posted notice %s as a status update for %s (%d), fbuid %s",
+                    "Posted notice %s as a status update for %s (%d), fbuid %d",
                     $this->notice->id,
                     $this->user->nickname,
                     $this->user->id,
@@ -523,7 +540,7 @@ class Facebookclient
         } else {
 
             $msg = sprintf(
-                "Error posting notice %s as a status update for %s (%d), fbuid %s - error code: %s",
+                "Error posting notice %s as a status update for %s (%d), fbuid %d - error code: %s",
                 $this->notice->id,
                 $this->user->nickname,
                 $this->user->id,
@@ -544,7 +561,7 @@ class Facebookclient
 
         common_debug(
             sprintf(
-                'Attempting to post notice %d as stream item for %s (%d) fbuid %s',
+                'Attempting to post notice %d as stream item for %s (%d) fbuid %d',
                 $this->notice->id,
                 $this->user->nickname,
                 $this->user->id,
@@ -565,14 +582,13 @@ class Facebookclient
         );
 
         if (!empty($result)) { // result will contain the item ID
-
             // Save a mapping
             Notice_to_item::saveNew($this->notice->id, $result);
 
             common_log(
                 LOG_INFO,
                 sprintf(
-                    'Posted notice %d as a %s for %s (%d), fbuid %s',
+                    'Posted notice %d as a %s for %s (%d), fbuid %d',
                     $this->notice->id,
                     empty($fbattachment) ? 'stream item' : 'stream item with attachment',
                     $this->user->nickname,
@@ -581,11 +597,10 @@ class Facebookclient
                 ),
                 __FILE__
             );
-
         } else {
 
             $msg = sprintf(
-                'Could not post notice %d as a %s for %s (%d), fbuid %s - error code: %s',
+                'Could not post notice %d as a %s for %s (%d), fbuid %d - error code: %s',
                 $this->notice->id,
                 empty($fbattachment) ? 'stream item' : 'stream item with attachment',
                 $this->user->nickname,
@@ -694,7 +709,7 @@ class Facebookclient
         common_log(
             LOG_INFO,
             sprintf(
-                'Removing Facebook link for %s (%d), fbuid %s',
+                'Removing Facebook link for %s (%d), fbuid %d',
                 $this->user->nickname,
                 $this->user->id,
                 $fbuid
@@ -708,7 +723,7 @@ class Facebookclient
             common_log(
                 LOG_ERR,
                 sprintf(
-                    'Could not remove Facebook link for %s (%d), fbuid %s',
+                    'Could not remove Facebook link for %s (%d), fbuid %d',
                     $this->user->nickname,
                     $this->user->id,
                     $fbuid
@@ -719,13 +734,28 @@ class Facebookclient
         }
 
         // Notify the user that we are removing their Facebook link
+        if (!empty($this->user->email)) {
+            $result = $this->mailFacebookDisconnect();
 
-        $result = $this->mailFacebookDisconnect();
+            if (!$result) {
+                $msg = 'Unable to send email to notify %s (%d), fbuid %d '
+                     . 'about his/her Facebook link being removed.';
 
-        if (!$result) {
-
-            $msg = 'Unable to send email to notify %s (%d), fbuid %s '
-                 . 'about his/her Facebook link being removed.';
+                common_log(
+                    LOG_WARNING,
+                    sprintf(
+                        $msg,
+                        $this->user->nickname,
+                        $this->user->id,
+                        $fbuid
+                    ),
+                    __FILE__
+                );
+            }
+        } else {
+            $msg = 'Unable to send email to notify %s (%d), fbuid %d '
+                 . 'about his/her Facebook link being removed because the '
+                 . 'user has not set an email address.';
 
             common_log(
                 LOG_WARNING,
@@ -754,33 +784,105 @@ class Facebookclient
 
         common_switch_locale($this->user->language);
 
+        // TRANS: E-mail subject.
         $subject = _m('Your Facebook connection has been removed');
 
-        $msg = <<<BODY
-Hi %1$s,
+        // TRANS: E-mail body. %1$s is a username, %2$s is the StatusNet sitename.
+        $msg = _m("Hi %1\$s,\n\n".
+                  "We are sorry to inform you we are unable to publish your notice to\n".
+                  "Facebook, and have removed the connection between your %2\$s account and\n".
+                  "Facebook.\n\n".
+                  "This may have happened because you have removed permission for %2\$s\n".
+                  "to post on your behalf, or perhaps you have deactivated your Facebook\n".
+                  "account. You can reconnect your %2\$s account to Facebook at any time by\n".
+                  "logging in with Facebook again.\n\n".
+                  "Sincerely,\n\n".
+                  "%2\$s\n");
 
-We're sorry to inform you we are unable to publish your notice to
-Facebook, and have removed the connection between your %2$s account and
-Facebook.
+        $body = sprintf(
+            $msg,
+            $this->user->nickname,
+            $siteName
+        );
 
-This may have happened because you have removed permission for %2$s
-to post on your behalf, or perhaps you have deactivated your Facebook
-account. You can reconnect your %s account to Facebook at any time by
-logging in with Facebook again.
+        common_switch_locale();
+
+        $result = mail_to_user($this->user, $subject, $body);
+
+        if (empty($this->user->password)) {
+            $result = self::emailWarn($this->user);
+        }
+
+        return $result;
+    }
+
+    /*
+     * Send the user an email warning that their account has been
+     * disconnected and he/she has no way to login and must contact
+     * the site administrator for help.
+     *
+     * @param User $user the deauthorizing user
+     *
+     */
+    static function emailWarn($user)
+    {
+        $profile = $user->getProfile();
+
+        $siteName  = common_config('site', 'name');
+        $siteEmail = common_config('site', 'email');
 
-Sincerely,
+        if (empty($siteEmail)) {
+            common_log(
+                LOG_WARNING,
+                    "No site email address configured. Please set one."
+            );
+        }
+
+        common_switch_locale($user->language);
+
+        // TRANS: E-mail subject. %s is the StatusNet sitename.
+        $subject = _m('Contact the %s administrator to retrieve your account');
+
+        // TRANS: E-mail body. %1$s is a username,
+        // TRANS: %2$s is the StatusNet sitename, %3$s is the site contact e-mail address.
+        $msg = _m("Hi %1\$s,\n\n".
+                  "We have noticed you have deauthorized the Facebook connection for your\n".
+                  "%2\$s account.  You have not set a password for your %2\$s account yet, so\n".
+                  "you will not be able to login. If you wish to continue using your %2\$s\n".
+                  "account, please contact the site administrator (%3\$s) to set a password.\n\n".
+                  "Sincerely,\n\n".
+                  "%2\$s\n");
 
-%2$s
-BODY;
         $body = sprintf(
-            _m($msg),
-            $this->user->nickname,
-            $siteName
+            $msg,
+            $user->nickname,
+            $siteName,
+            $siteEmail
         );
 
         common_switch_locale();
 
-        return mail_to_user($this->user, $subject, $body);
+        if (mail_to_user($user, $subject, $body)) {
+            common_log(
+                LOG_INFO,
+                sprintf(
+                    'Sent account lockout warning to %s (%d)',
+                    $user->nickname,
+                    $user->id
+                ),
+                __FILE__
+            );
+        } else {
+            common_log(
+                LOG_WARNING,
+                sprintf(
+                    'Unable to send account lockout warning to %s (%d)',
+                    $user->nickname,
+                    $user->id
+                ),
+                __FILE__
+            );
+        }
     }
 
     /*
@@ -879,41 +981,45 @@ BODY;
         $n2i = Notice_to_item::staticGet('notice_id', $this->notice->id);
 
         if (!empty($this->flink) && !empty($n2i)) {
-
-            $result = $this->facebook->api(
-                array(
-                    'method'  => 'stream.remove',
-                    'post_id' => $n2i->item_id,
-                    'uid'     => $this->flink->foreign_id
-                )
-            );
-
-            if (!empty($result) && result == true) {
-
-                common_log(
-                  LOG_INFO,
-                    sprintf(
-                        'Deleted Facebook item: %s for %s (%d), fbuid %d',
-                        $n2i->item_id,
-                        $this->user->nickname,
-                        $this->user->id,
-                        $this->flink->foreign_id
-                    ),
-                    __FILE__
+            try {
+                $result = $this->facebook->api(
+                    array(
+                        'method'  => 'stream.remove',
+                        'post_id' => $n2i->item_id,
+                        'uid'     => $this->flink->foreign_id
+                    )
                 );
 
-                $n2i->delete();
-
-            } else {
-
+                if (!empty($result) && result == true) {
+                    common_log(
+                      LOG_INFO,
+                        sprintf(
+                            'Deleted Facebook item: %s for %s (%d), fbuid %d',
+                            $n2i->item_id,
+                            $this->user->nickname,
+                            $this->user->id,
+                            $this->flink->foreign_id
+                        ),
+                        __FILE__
+                    );
+
+                    $n2i->delete();
+
+                } else {
+                    throw new FaceboookApiException(var_export($result, true));
+                }
+            } catch (FacebookApiException $e) {
                 common_log(
                   LOG_WARNING,
                     sprintf(
-                        'Could not deleted Facebook item: %s for %s (%d), fbuid %d',
+                        'Could not deleted Facebook item: %s for %s (%d), '
+                            . 'fbuid %d - (API error: %s) item already deleted '
+                            . 'on Facebook? ',
                         $n2i->item_id,
                         $this->user->nickname,
                         $this->user->id,
-                        $this->flink->foreign_id
+                        $this->flink->foreign_id,
+                        $e
                     ),
                     __FILE__
                 );
@@ -930,39 +1036,41 @@ BODY;
         $n2i = Notice_to_item::staticGet('notice_id', $this->notice->id);
 
         if (!empty($this->flink) && !empty($n2i)) {
-
-            $result = $this->facebook->api(
-                array(
-                    'method'  => 'stream.addlike',
-                    'post_id' => $n2i->item_id,
-                    'uid'     => $this->flink->foreign_id
-                )
-            );
-
-            if (!empty($result) && result == true) {
-
-                common_log(
-                  LOG_INFO,
-                    sprintf(
-                        'Added like for item: %s for %s (%d), fbuid %d',
-                        $n2i->item_id,
-                        $this->user->nickname,
-                        $this->user->id,
-                        $this->flink->foreign_id
-                    ),
-                    __FILE__
+            try {
+                $result = $this->facebook->api(
+                    array(
+                        'method'  => 'stream.addlike',
+                        'post_id' => $n2i->item_id,
+                        'uid'     => $this->flink->foreign_id
+                    )
                 );
 
-            } else {
-
+                if (!empty($result) && result == true) {
+                    common_log(
+                      LOG_INFO,
+                        sprintf(
+                            'Added like for item: %s for %s (%d), fbuid %d',
+                            $n2i->item_id,
+                            $this->user->nickname,
+                            $this->user->id,
+                            $this->flink->foreign_id
+                        ),
+                        __FILE__
+                    );
+                } else {
+                    throw new FacebookApiException(var_export($result, true));
+                }
+            } catch (FacebookApiException $e) {
                 common_log(
                   LOG_WARNING,
                     sprintf(
-                        'Could not like Facebook item: %s for %s (%d), fbuid %d',
+                        'Could not like Facebook item: %s for %s (%d), '
+                            . 'fbuid %d (API error: %s)',
                         $n2i->item_id,
                         $this->user->nickname,
                         $this->user->id,
-                        $this->flink->foreign_id
+                        $this->flink->foreign_id,
+                        $e
                     ),
                     __FILE__
                 );
@@ -979,44 +1087,46 @@ BODY;
         $n2i = Notice_to_item::staticGet('notice_id', $this->notice->id);
 
         if (!empty($this->flink) && !empty($n2i)) {
-
-            $result = $this->facebook->api(
-                array(
-                    'method'  => 'stream.removeLike',
-                    'post_id' => $n2i->item_id,
-                    'uid'     => $this->flink->foreign_id
-                )
-            );
-
-            if (!empty($result) && result == true) {
-
-                common_log(
-                  LOG_INFO,
-                    sprintf(
-                        'Removed like for item: %s for %s (%d), fbuid %d',
-                        $n2i->item_id,
-                        $this->user->nickname,
-                        $this->user->id,
-                        $this->flink->foreign_id
-                    ),
-                    __FILE__
+            try {
+                $result = $this->facebook->api(
+                    array(
+                        'method'  => 'stream.removeLike',
+                        'post_id' => $n2i->item_id,
+                        'uid'     => $this->flink->foreign_id
+                    )
                 );
 
-            } else {
-
-                common_log(
+                if (!empty($result) && result == true) {
+                    common_log(
+                      LOG_INFO,
+                        sprintf(
+                            'Removed like for item: %s for %s (%d), fbuid %d',
+                            $n2i->item_id,
+                            $this->user->nickname,
+                            $this->user->id,
+                            $this->flink->foreign_id
+                        ),
+                        __FILE__
+                    );
+
+                } else {
+                    throw new FacebookApiException(var_export($result, true));
+                }
+            } catch (FacebookApiException $e) {
+                  common_log(
                   LOG_WARNING,
                     sprintf(
-                        'Could not remove like for Facebook item: %s for %s (%d), fbuid %d',
+                        'Could not remove like for Facebook item: %s for %s '
+                          . '(%d), fbuid %d (API error: %s)',
                         $n2i->item_id,
                         $this->user->nickname,
                         $this->user->id,
-                        $this->flink->foreign_id
+                        $this->flink->foreign_id,
+                        $e
                     ),
                     __FILE__
                 );
             }
         }
     }
-
 }