We don't do any redirects in library code, nuke this unused function.
authorMichael J. Rubinsky <mrubinsk@horde.org>
Fri, 27 Feb 2009 20:26:16 +0000 (15:26 -0500)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Fri, 27 Feb 2009 20:26:16 +0000 (15:26 -0500)
Update examples, add some doc/comments.

framework/Service_Facebook/doc/facebook_example.php
framework/Service_Facebook/lib/Horde/Service/Facebook.php

index 631d10c..1b0822a 100644 (file)
@@ -1,36 +1,65 @@
 <?php
-/**
- * A test callback function for login redirects.
- * @param $url
- * @return unknown_type
- */
-function testFBCallback($url)
-{
-    echo '<script type="text/javascript">alert("testing callback:' . $url . '");</script>';
-    exit;
-}
-
 define('HORDE_BASE', '/private/var/www/html/horde');
 require_once HORDE_BASE . '/lib/base.php';
 
-$appapikey = 'xxx';    //CHANGE THIS
-$appsecret = 'xxx';    //CHANGE THIS
 
-// Horde_Service_Facebook *requires* an http_client, http_request objects
-// it can optionally use a callback function to handle login redirects
+// To call Facebook API methods, you will need to set up the application in
+// Facebook, and obtain both the api_key and the app_secret.
+// See:
+//  http://developers.facebook.com/get_started.php?tab=tutorial
+
+$apikey = 'xxx';    //CHANGE THIS
+$secret = 'xxx';    //CHANGE THIS
+
+/**
+ * Horde_Service_Facebook *requires* an http_client, http_request objects
+ * and a Horde_Log_Logger object
+ */
 $context = array('http_client' => new Horde_Http_Client(),
-                 'http_request' => new Horde_Controller_Request_Http(),);
-                 //'login_redirect_callback' => 'testFBCallback');
+                 'http_request' => new Horde_Controller_Request_Http());
 
-// Create the facebook object and make sure we have an active, authenticated
-// session.
-$facebook = new Horde_Service_Facebook($appapikey, $appsecret, $context);
-$user_id = $facebook->require_login();
+/** Create the facebook object **/
+$facebook = new Horde_Service_Facebook($apikey, $secret, $context);
+
+
+/**
+ * Authenticating and logging into a Facebook app from an external site is
+ * a complicated and multi-stage process.  For these examples, we are assuming
+ * that we have authenticated the application and are either already logged into
+ * Facebook or we have authorized 'offline_access'.
+ */
+
+/**
+ * If we have a valid cookie, this will know about it. This method should also
+ * be called both after the user has authorized the application and again after
+ * the user has (optionally) authorized infinite sessions (offline_access). Then
+ * you would obtain the infinite session_key by calling auth->getSessionKey().
+ * This is the *only* way to obtain the session key.
+ */
+//$facebook->auth->validateSession();
+
+// Current uid can be obtained with:
+//$uid = $facebook->auth->getUser();
+
+/** session_key, if you need it, can be obtained via: **/
+//$sid = $facebook->auth->getSessionKey();
+
+/**
+ * Otherwise, you would use uid and session_key from prefs or other local
+ * storage and set up the session by calling setUser(). This is how you would
+ * need to do this when utilizing an infinite session_key, since FB will only
+ * send the infinite session_key to you one time only - it's up to client code
+ * to store it.
+ */
+ $fbp = unserialize($prefs->getValue('facebook'));
+ $uid = $fbp['uid'];
+ $sid = $fbp['sid'];
+ $facebook->auth->setUser($uid, $sid, 0);
 
 
 /** Use a FQL query to get some friend info **/
-$result = $facebook->fql_query('SELECT name, status FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = ' . $user_id . ')');
-var_dump($result);
+//$result = $facebook->fql->run('SELECT name, status FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = ' . $uid . ')');
+//var_dump($result);
 
 /** Similar can be done as so using individual api calls...but takes a *long* time **/
 //$friends = $facebook->friends_get();
@@ -43,22 +72,41 @@ var_dump($result);
 //    echo ' ' . $f['name'] . '<br />';
 //  }
 
-
 /** Calling code that requires extended permissions **/
-try {
-    $facebook->users_setStatus('is testing my facebook code...again.');
-} catch (Horde_Service_Facebook_Exception $e) {
-  // Check that we failed because of insufficient app permissions.
-  // then redirect if needed...
-}
-
-/** Batch mode. **/
-// When calling in batch mode, you must assign the results of the method calls
-// as a reference so when run() is called, you still have a handle to the
-// results.
+//try {
+//    // Set your Facebook status (requires 'status_update' extended perm)
+//    $facebook->users->setStatus('is testing my Horde_Service_Facebook client library code...again.');
+//} catch (Horde_Service_Facebook_Exception $e) {
+//    // Check that we failed because of insufficient app permissions.
+//    // then redirect if needed...
+//    if ($e->getCode() == Horde_Service_Facebook_ErrorCodes::API_EC_PERMISSION_STATUS_UPDATE) {
+//      // Don't have status_update...tell user/provide link to authorize page etc...
+//    } else {
+//      // Something else
+//      echo $e->getMessage();
+//    }
+//}
+
+/**
+ * Alternatively, you could check for the necessary perms first, but IMO, it's
+ * more effecient to get the error since checking the perms and then performing
+ * the action require two round trips to the server.
+ */
+//$hasPerm = $facebook->users->hasAppPermissions('status_update');
+//if ($hasPerm) {
+//    //.....
+//}
+
+
+/**
+ *  Batch mode.
+ * When calling in batch mode, you must assign the results of the method calls
+ * as a reference so when run() is called, you still have a handle to the
+ * results.
+ */
 $facebook->batchBegin();
-$notifications = &$facebook->notifications_get();
-$friends = &$facebook->friends_get();
+$notifications = &$facebook->notifications->get();
+$friends = &$facebook->friends->get();
 $facebook->batchEnd();
 var_dump($friends);
 var_dump($notifications);
\ No newline at end of file
index fa380f2..411e91c 100644 (file)
@@ -119,7 +119,6 @@ class Horde_Service_Facebook
      *  <pre>
      *      http_client - required
      *      http_response - required
-     *      login_redirect_callback - optional
      *      logger
      *      no_resolve - set to true to prevent attempting to obtain a session
      *                   from an auth_token. Useful if client code wants to
@@ -197,32 +196,6 @@ class Horde_Service_Facebook
     }
 
     /**
-     * Either redirect to the FB login page, or call a callback function to let
-     * the client code handle the redirect.
-     *
-     * @param string $url  The URL to redirect to.
-     *
-     * @return void
-     */
-    protected function _redirect($url)
-    {
-        // If we have a callback, call it then return.
-        if (!empty($this->_context['login_redirect_callback'])) {
-            call_user_func($this->_context['login_redirect_callback'], $url);
-            return;
-        }
-
-        if (preg_match('/^https?:\/\/([^\/]*\.)?facebook\.com(:\d+)?/i', $url)) {
-            // make sure facebook.com url's load in the full frame so that we don't
-            // get a frame within a frame.
-            echo "<script type=\"text/javascript\">\ntop.location.href = \"$url\";\n</script>";
-        } else {
-            header('Location: ' . $url);
-        }
-        exit;
-    }
-
-    /**
      * Return the current request's url
      *
      * @return string