]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/FBConnect/FBConnectPlugin.php
Extract out Facebook app stuff into a plugin
[quix0rs-gnu-social.git] / plugins / FBConnect / FBConnectPlugin.php
index 2e32ad198fa20607ad042e4b2729b7a822ae8758..0dacf90122dcfa8653337a98324f1d73d2aee7f3 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * Laconica, the distributed open-source microblogging tool
+ * StatusNet, the distributed open-source microblogging tool
  *
  * Plugin to enable Facebook Connect
  *
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  * @category  Plugin
- * @package   Laconica
- * @author    Zach Copley <zach@controlyourself.ca>
- * @copyright 2009 Control Yourself, Inc.
+ * @package   StatusNet
+ * @author    Zach Copley <zach@status.net>
+ * @copyright 2009 StatusNet, Inc.
  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
- * @link      http://laconi.ca/
+ * @link      http://status.net/
  */
 
-if (!defined('LACONICA')) {
+if (!defined('STATUSNET') && !defined('LACONICA')) {
     exit(1);
 }
 
@@ -45,10 +45,10 @@ require_once INSTALLDIR . '/plugins/FBConnect/FBC_XDReceiver.php';
  * Plugin to enable Facebook Connect
  *
  * @category Plugin
- * @package  Laconica
- * @author   Zach Copley <zach@controlyourself.ca>
+ * @package  StatusNet
+ * @author   Zach Copley <zach@status.net>
  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
- * @link     http://laconi.ca/
+ * @link     http://status.net/
  */
 
 class FBConnectPlugin extends Plugin
@@ -82,9 +82,7 @@ class FBConnectPlugin extends Plugin
 
             $action->extraHeaders();
 
-            $action->startXML('html',
-                '-//W3C//DTD XHTML 1.0 Strict//EN',
-                'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
+            $action->startXML('html');
 
             $language = $action->getLanguage();
 
@@ -104,7 +102,7 @@ class FBConnectPlugin extends Plugin
 
     // Note: this script needs to appear in the <body>
 
-    function onStartShowHeader($action)
+    function onEndShowScripts($action)
     {
         if ($this->reqFbScripts($action)) {
 
@@ -117,28 +115,48 @@ class FBConnectPlugin extends Plugin
             // XXX: Facebook says we don't need this FB_RequireFeatures(),
             // but we actually do, for IE and Safari. Gar.
 
-            $html = sprintf('<script type="text/javascript">
-                                window.onload = function () {
-                                    FB_RequireFeatures(
-                                        ["XFBML"],
-                                            function() {
-                                                FB.init("%s", "../xd_receiver.html",
-                                                 {"doNotUseCachedConnectState":true });
-
-                                            }
-                                        ); }
-
-                                function goto_login() {
-                                    window.location = "%s";
-                                }
-
-                                function goto_logout() {
-                                    window.location = "%s";
-                                }
-                              </script>', $apikey,
-                                  $login_url, $logout_url);
-
-            $action->raw($html);
+            $js =  '<script type="text/javascript">';
+            $js .= '    $(document).ready(function () {';
+            $js .= '         FB_RequireFeatures(';
+            $js .= '             ["XFBML"], function() {';
+            $js .= '                 FB.init("%1$s", "../xd_receiver.html");';
+            $js .= '             }';
+            $js .= '         );';
+            $js .= '    });';
+
+            $js .= '    function goto_login() {';
+            $js .= '        window.location = "%2$s";';
+            $js .= '    }';
+
+            // The below function alters the logout link so that it logs the user out
+            // of Facebook Connect as well as the site.  However, for some pages
+            // (FB Connect Settings) we need to output the FB Connect scripts (to
+            // show an existing FB connection even if the user isn't authenticated
+            // with Facebook connect) but NOT alter the logout link. And the only
+            // way to reliably do that is with the FB Connect .js libs.  Crazy.
+
+            $js .= '    FB.ensureInit(function() {';
+            $js .= '        FB.Connect.ifUserConnected(';
+            $js .= '            function() { ';
+            $js .= '                $(\'#nav_logout a\').attr(\'href\', \'#\');';
+            $js .= '                $(\'#nav_logout a\').click(function() {';
+            $js .= '                   FB.Connect.logoutAndRedirect(\'%3$s\');';
+            $js .= '                   return false;';
+            $js .= '                })';
+            $js .= '            },';
+            $js .= '            function() {';
+            $js .= '                return false;';
+            $js .= '            }';
+            $js .= '        );';
+            $js .= '     });';
+            $js .= '</script>';
+
+            $js = sprintf($js, $apikey, $login_url, $logout_url);
+
+            // Compress the bugger down a bit
+            $js = str_replace('  ', '', $js);
+
+            $action->raw("  $js");  // leading two spaces to make it line up
         }
 
     }
@@ -148,22 +166,14 @@ class FBConnectPlugin extends Plugin
     function onEndShowFooter($action)
     {
         if ($this->reqFbScripts($action)) {
-
-            $action->element('script',
-                array('type' => 'text/javascript',
-                      'src'  => 'http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php'),
-                      '');
+            $action->script('http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php');
         }
     }
 
-    function onEndShowLaconicaStyles($action)
+    function onEndShowStatusNetStyles($action)
     {
-
         if ($this->reqFbScripts($action)) {
-
-            $action->element('link', array('rel' => 'stylesheet',
-                'type' => 'text/css',
-                'href' => common_path('plugins/FBConnect/FBConnectPlugin.css')));
+            $action->cssLink('plugins/FBConnect/FBConnectPlugin.css');
         }
     }
 
@@ -222,10 +232,10 @@ class FBConnectPlugin extends Plugin
                 try {
 
                     $facebook = getFacebook();
-                    $fbuid    = $facebook->api_client->users_getLoggedInUser();
+                    $fbuid    = $facebook->get_loggedin_user();
 
                 } catch (Exception $e) {
-                    common_log(LOG_WARNING,
+                    common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
                         'Problem getting Facebook user: ' .
                             $e->getMessage());
                 }
@@ -243,6 +253,14 @@ class FBConnectPlugin extends Plugin
     {
 
         $user = common_current_user();
+        $connect = 'FBConnectSettings';
+        if (common_config('xmpp', 'enabled')) {
+            $connect = 'imsettings';
+        } else if (common_config('sms', 'enabled')) {
+            $connect = 'smssettings';
+        } else if (common_config('twitter', 'enabled')) {
+            $connect = 'twittersettings';
+        }
 
         if (!empty($user)) {
 
@@ -272,59 +290,9 @@ class FBConnectPlugin extends Plugin
                 $action->elementEnd('li');
 
             }
+        }
 
-            $action->menuItem(common_local_url('all', array('nickname' => $user->nickname)),
-                _('Home'), _('Personal profile and friends timeline'), false, 'nav_home');
-            $action->menuItem(common_local_url('profilesettings'),
-                _('Account'), _('Change your email, avatar, password, profile'), false, 'nav_account');
-            if (common_config('xmpp', 'enabled')) {
-                $action->menuItem(common_local_url('imsettings'),
-                    _('Connect'), _('Connect to IM, SMS, Twitter'), false, 'nav_connect');
-            } else {
-             $action->menuItem(common_local_url('smssettings'),
-                 _('Connect'), _('Connect to SMS, Twitter'), false, 'nav_connect');
-            }
-            if (common_config('invite', 'enabled')) {
-                $action->menuItem(common_local_url('invite'),
-                    _('Invite'),
-                    sprintf(_('Invite friends and colleagues to join you on %s'),
-                    common_config('site', 'name')),
-                    false, 'nav_invitecontact');
-            }
-
-            // Need to override the Logout link to make it do FB stuff
-            if (!empty($fbuid)) {
-
-                $logout_url = common_local_url('logout');
-                $title =  _('Logout from the site');
-                $text = _('Logout');
-
-                $html = sprintf('<li id="nav_logout"><a href="#" title="%s" ' .
-                    'onclick="FB.Connect.logoutAndRedirect(\'%s\');">%s</a></li>',
-                    $title, $logout_url, $text);
-
-                $action->raw($html);
-
-             } else {
-                 $action->menuItem(common_local_url('logout'),
-                     _('Logout'), _('Logout from the site'), false, 'nav_logout');
-             }
-         }
-         else {
-             if (!common_config('site', 'closed')) {
-                 $action->menuItem(common_local_url('register'),
-                     _('Register'), _('Create an account'), false, 'nav_register');
-             }
-             $action->menuItem(common_local_url('login'),
-                 _('Login'), _('Login to the site'), false, 'nav_login');
-         }
-
-         $action->menuItem(common_local_url('doc', array('title' => 'help')),
-             _('Help'), _('Help me!'), false, 'nav_help');
-         $action->menuItem(common_local_url('peoplesearch'),
-             _('Search'), _('Search for people or text'), false, 'nav_search');
-
-        return false;
+        return true;
     }
 
     function onStartShowLocalNavBlock($action)
@@ -362,8 +330,9 @@ class FBConnectPlugin extends Plugin
                 $facebook = getFacebook();
                 $facebook->expire_session();
             } catch (Exception $e) {
-                common_log(LOG_WARNING, 'Could\'t logout of Facebook: ' .
-                    $e->getMessage());
+                common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
+                           'Could\'t logout of Facebook: ' .
+                           $e->getMessage());
             }
         }
 
@@ -387,7 +356,8 @@ class FBConnectPlugin extends Plugin
             }
 
         } catch (Exception $e) {
-            common_log(LOG_WARNING, "Facebook client failure requesting profile pic!");
+            common_log(LOG_WARNING, 'Facebook Connect Plugin - ' .
+                       "Facebook client failure requesting profile pic!");
         }
 
        return $url;