]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/FacebookBridge/FacebookBridgePlugin.php
Merge branch '1.1.x'
[quix0rs-gnu-social.git] / plugins / FacebookBridge / FacebookBridgePlugin.php
index 93427d5bdcaec44972e40c873f49e0bcf44acd8d..bf16da337dc7137852b1c17075cfd6e11e0edc60 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /**
  * StatusNet - the distributed open-source microblogging tool
- * Copyright (C) 2010, StatusNet, Inc.
+ * Copyright (C) 2010-2011, StatusNet, Inc.
  *
  * A plugin for integrating Facebook with StatusNet. Includes single-sign-on
  * and publishing notices to Facebook using Facebook's Graph API.
  * You should have received a copy of the GNU Affero General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
- * @category  Pugin
+ * @category  Plugin
  * @package   StatusNet
  * @author    Zach Copley <zach@status.net>
- * @copyright 2010 StatusNet, Inc.
+ * @copyright 2011 StatusNet, Inc.
  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  * @link      http://status.net/
  */
@@ -36,21 +36,22 @@ if (!defined('STATUSNET')) {
 define("FACEBOOK_SERVICE", 2);
 
 /**
- * Main class for Facebook plugin
+ * Main class for Facebook Bridge plugin
  *
  * @category  Plugin
  * @package   StatusNet
  * @author    Zach Copley <zach@status.net>
- * @copyright 2010 StatusNet, Inc.
+ * @copyright 2010-2011 StatusNet, Inc.
  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  * @link      http://status.net/
  */
 class FacebookBridgePlugin extends Plugin
 {
-    public $appId    = null; // Facebook application ID
-    public $secret   = null; // Facebook application secret
+    public $appId;  // Facebook application ID
+    public $secret; // Facebook application secret
+
     public $facebook = null; // Facebook application instance
-    public $dir      = null; // Facebook SSO plugin dir
+    public $dir      = null; // Facebook plugin dir
 
     /**
      * Initializer for this plugin
@@ -61,6 +62,28 @@ class FacebookBridgePlugin extends Plugin
      */
     function initialize()
     {
+
+        // Allow the id and key to be passed in
+        // Control panel will override
+
+        if (isset($this->appId)) {
+            $appId = common_config('facebook', 'appid');
+            if (empty($appId)) {
+                Config::save(
+                    'facebook',
+                    'appid',
+                    $this->appId
+                );
+            }
+        }
+
+        if (isset($this->secret)) {
+            $secret = common_config('facebook', 'secret');
+            if (empty($secret)) {
+                Config::save('facebook', 'secret', $this->secret);
+            }
+        }
+
         $this->facebook = Facebookclient::getFacebook(
             $this->appId,
             $this->secret
@@ -78,14 +101,12 @@ class FacebookBridgePlugin extends Plugin
      */
     function onAutoload($cls)
     {
-
         $dir = dirname(__FILE__);
 
-        //common_debug("class = " . $cls);
-
         switch ($cls)
         {
         case 'Facebook': // Facebook PHP SDK
+            include_once $dir . '/extlib/base_facebook.php';
             include_once $dir . '/extlib/facebook.php';
             return false;
         case 'FacebookloginAction':
@@ -105,7 +126,6 @@ class FacebookBridgePlugin extends Plugin
         default:
             return true;
         }
-
     }
 
     /**
@@ -154,30 +174,24 @@ class FacebookBridgePlugin extends Plugin
     function onRouterInitialized($m)
     {
         // Always add the admin panel route
-        $m->connect('admin/facebook', array('action' => 'facebookadminpanel'));
+        $m->connect('panel/facebook', array('action' => 'facebookadminpanel'));
 
-        // Only add these routes if an application has been setup on
-        // Facebook for the plugin to use.
-        if ($this->hasApplication()) {
-
-            $m->connect(
-                'main/facebooklogin',
-                array('action' => 'facebooklogin')
-            );
-            $m->connect(
-                'main/facebookfinishlogin',
-                array('action' => 'facebookfinishlogin')
-            );
-            $m->connect(
-                'settings/facebook',
-                array('action' => 'facebooksettings')
-            );
-            $m->connect(
-                'facebook/deauthorize',
-                array('action' => 'facebookdeauthorize')
-            );
-
-        }
+        $m->connect(
+            'main/facebooklogin',
+            array('action' => 'facebooklogin')
+        );
+        $m->connect(
+            'main/facebookfinishlogin',
+            array('action' => 'facebookfinishlogin')
+        );
+        $m->connect(
+            'settings/facebook',
+            array('action' => 'facebooksettings')
+        );
+        $m->connect(
+            'facebook/deauthorize',
+            array('action' => 'facebookdeauthorize')
+        );
 
         return true;
     }
@@ -186,21 +200,22 @@ class FacebookBridgePlugin extends Plugin
      * Add a login tab for Facebook, but only if there's a Facebook
      * application defined for the plugin to use.
      *
-     * @param Action &action the current action
+     * @param Action $action the current action
      *
      * @return void
      */
-    function onEndLoginGroupNav(&$action)
+    function onEndLoginGroupNav($action)
     {
         $action_name = $action->trimmed('action');
 
         if ($this->hasApplication()) {
 
             $action->menuItem(
+                // TRANS: Menu item for "Facebook" login.
                 common_local_url('facebooklogin'),
                 _m('MENU', 'Facebook'),
-                // TRANS: Tooltip for menu item "Facebook".
-                _m('Login or register using Facebook'),
+                // TRANS: Menu title for "Facebook" login.
+                _m('Login or register using Facebook.'),
                'facebooklogin' === $action_name
             );
         }
@@ -208,6 +223,19 @@ class FacebookBridgePlugin extends Plugin
         return true;
     }
 
+    /**
+     * If the plugin's installed, this should be accessible to admins
+     */
+    function onAdminPanelCheck($name, &$isOK)
+    {
+        if ($name == 'facebook') {
+            $isOK = true;
+            return false;
+        }
+
+        return true;
+    }
+
     /**
      * Add a Facebook tab to the admin panels
      *
@@ -223,10 +251,10 @@ class FacebookBridgePlugin extends Plugin
 
             $nav->out->menuItem(
                 common_local_url('facebookadminpanel'),
-                // TRANS: Menu item.
+                // TRANS: Menu item for "Facebook" in administration panel.
                 _m('MENU','Facebook'),
-                // TRANS: Tooltip for menu item "Facebook".
-                _m('Facebook integration configuration'),
+                // TRANS: Menu title for "Facebook" in administration panel.
+                _m('Facebook integration configuration.'),
                 $action_name == 'facebookadminpanel',
                 'nav_facebook_admin_panel'
             );
@@ -239,17 +267,16 @@ class FacebookBridgePlugin extends Plugin
      * Add a tab for user-level Facebook settings if the user
      * has a link to Facebook
      *
-     * @param Action &action the current action
+     * @param Action $action the current action
      *
      * @return void
      */
-    function onEndConnectSettingsNav(&$action)
+    function onEndConnectSettingsNav($action)
     {
         if ($this->hasApplication()) {
             $action_name = $action->trimmed('action');
 
-            // CurrentUserDesignAction stores the current user in $cur
-            $user = $action->getCurrentUser();
+            $user = common_current_user();
 
             $flink = null;
 
@@ -264,16 +291,14 @@ class FacebookBridgePlugin extends Plugin
 
                 $action->menuItem(
                     common_local_url('facebooksettings'),
-                    // TRANS: Menu item tab.
+                    // TRANS: Menu item for "Facebook" in user settings.
                     _m('MENU','Facebook'),
-                    // TRANS: Tooltip for menu item "Facebook".
-                    _m('Facebook settings'),
+                    // TRANS: Menu title for "Facebook" in user settings.
+                    _m('Facebook settings.'),
                     $action_name === 'facebooksettings'
                 );
-
             }
         }
-
     }
 
     /*
@@ -294,7 +319,6 @@ class FacebookBridgePlugin extends Plugin
             if (!empty($appId) && !empty($secret)) {
                 return true;
             }
-
         }
 
         return false;
@@ -326,26 +350,35 @@ class FacebookBridgePlugin extends Plugin
             $action->script('https://connect.facebook.net/en_US/all.js');
 
             $script = <<<ENDOFSCRIPT
-FB.init({appId: %1\$s, session: %2\$s, status: true, cookie: true, xfbml: true});
+function setCookie(name, value) {
+    var date = new Date();
+    date.setTime(date.getTime() + (5 * 60 * 1000)); // 5 mins
+    var expires = "; expires=" + date.toGMTString();
+    document.cookie = name + "=" + value + expires + "; path=/";
+}
+
+FB.init({appId: %1\$s, status: true, cookie: true, xfbml: true, oauth: true});
 
 $('#facebook_button').bind('click', function(event) {
 
     event.preventDefault();
 
     FB.login(function(response) {
-        if (response.session && response.perms) {
-            window.location.href = '%3\$s';
+        if (response.authResponse) {
+            // put the access token in a cookie for the next step
+            setCookie('fb_access_token', response.authResponse.accessToken);
+            window.location.href = '%2\$s';
         } else {
             // NOP (user cancelled login)
         }
-    }, {perms:'read_stream,publish_stream,offline_access,user_status,user_location,user_website,email'});
+    }, {scope:'read_stream,publish_stream,offline_access,user_status,user_location,user_website,email'});
 });
 ENDOFSCRIPT;
 
             $action->inlineScript(
-                sprintf($script,
+                sprintf(
+                    $script,
                     json_encode($this->facebook->getAppId()),
-                    json_encode($this->facebook->getSession()),
                     common_local_url('facebookfinishlogin')
                 )
             );
@@ -357,26 +390,30 @@ ENDOFSCRIPT;
      *
      * @param Action action the current action
      */
-    function onEndLogout($action)
+    function onStartLogout($action)
     {
         if ($this->hasApplication()) {
-            $session = $this->facebook->getSession();
-            $fbuser  = null;
-            $fbuid   = null;
-
-            if ($session) {
-                try {
-                    $fbuid  = $this->facebook->getUser();
-                    $fbuser = $this->facebook->api('/me');
-                 } catch (FacebookApiException $e) {
-                     common_log(LOG_ERROR, $e, __FILE__);
-                 }
-            }
 
-            if (!empty($fbuser)) {
+            $cur = common_current_user();
+            $flink = Foreign_link::getByUserID($cur->id, FACEBOOK_SERVICE);
+
+            if (!empty($flink)) {
+
+                $this->facebook->setAccessToken($flink->credentials);
+
+                if (common_config('singleuser', 'enabled')) {
+                    $user = User::singleUser();
+
+                    $destination = common_local_url(
+                        'showstream',
+                        array('nickname' => $user->nickname)
+                    );
+                } else {
+                    $destination = common_local_url('public');
+                }
 
                 $logoutUrl = $this->facebook->getLogoutUrl(
-                    array('next' => common_local_url('public'))
+                    array('next' => $destination)
                 );
 
                 common_log(
@@ -387,10 +424,14 @@ ENDOFSCRIPT;
                     ),
                     __FILE__
                 );
-                common_debug("LOGOUT URL = $logoutUrl");
+
+                $action->logout();
+
                 common_redirect($logoutUrl, 303);
+                return false; // probably never get here, but hey
             }
 
+            return true;
         }
     }
 
@@ -425,7 +466,7 @@ ENDOFSCRIPT;
      */
     function onStartEnqueueNotice($notice, &$transports)
     {
-        if (self::hasApplication() && $notice->isLocal()) {
+        if (self::hasApplication() && $notice->isLocal() && $notice->inScope(null)) {
             array_push($transports, 'facebook');
         }
         return true;
@@ -483,7 +524,7 @@ ENDOFSCRIPT;
     {
         $client = new Facebookclient($notice);
         $client->streamRemove();
-        
+
         return true;
     }
 
@@ -496,7 +537,7 @@ ENDOFSCRIPT;
      */
     function onEndFavorNotice(Profile $profile, Notice $notice)
     {
-        $client = new Facebookclient($notice);
+        $client = new Facebookclient($notice, $profile);
         $client->like();
 
         return true;
@@ -512,7 +553,7 @@ ENDOFSCRIPT;
      */
     function onEndDisfavorNotice(Profile $profile, Notice $notice)
     {
-        $client = new Facebookclient($notice);
+        $client = new Facebookclient($notice, $profile);
         $client->unLike();
 
         return true;
@@ -526,11 +567,12 @@ ENDOFSCRIPT;
     function onPluginVersion(&$versions)
     {
         $versions[] = array(
-            'name' => 'Facebook Single-Sign-On',
+            'name' => 'Facebook Bridge',
             'version' => STATUSNET_VERSION,
             'author' => 'Craig Andrews, Zach Copley',
             'homepage' => 'http://status.net/wiki/Plugin:FacebookBridge',
             'rawdescription' =>
+             // TRANS: Plugin description.
             _m('A plugin for integrating StatusNet with Facebook.')
         );