You are here

function fb_user_app_fb in Drupal for Facebook 7.3

Same name and namespace in other branches
  1. 6.3 contrib/fb_user_app.module \fb_user_app_fb()

Implementation of hook_fb()

File

contrib/fb_user_app.module, line 42
This module manages relations between local Drupal user accounts and their accounts on facebook.com by application.

Code

function fb_user_app_fb($op, $data, &$return) {
  $fb_app = isset($data['fb_app']) ? $data['fb_app'] : NULL;
  $fb = isset($data['fb']) ? $data['fb'] : NULL;
  global $user;
  if ($op == FB_OP_APP_IS_AUTHORIZED && variable_get(FB_USER_APP_VAR_TRACK_EVERY_PAGE, FALSE)) {

    // This hook is called on every page request, if the user has authorized
    // the app/page and permission has been granted in the settings. We used
    // to create accounts and maps here.  That code is now
    // in FB_OP_AJAX_EVENT, because it turns out this hook is invoked even on
    // page not found and access denied pages.
    fb_user_app_track($fb, $fb_app, $user);
  }
  elseif ($op == FB_APP_OP_EVENT) {

    // Facebook has notified us of some event.
    // We handle some of the events here.
    $event_type = $data['event_type'];

    // Ensure fb_user_app table accurately reflects whether user has authorized.
    if ($event_type == FB_APP_EVENT_POST_AUTHORIZE) {

      // Track new facebook user, $GLOBAL['user'] not valid during post-authorize.
      fb_user_app_track($fb, $fb_app);
    }
    elseif ($event_type == FB_APP_EVENT_POST_REMOVE) {
      $fbu = fb_settings(FB_SETTINGS_FBU);

      // User has removed the app from their account.
      db_query("DELETE FROM {fb_user_app} WHERE apikey=:apikey AND fbu=:fbu", array(
        ':apikey' => $fb_app->apikey,
        ':fbu' => $fbu,
      ));
    }
  }
  elseif ($op == FB_OP_GET_USER_SESSION) {

    // The fb module is asking for session login information.  For example, to
    // log in as the user when not on a canvas page.  This module may be able
    // to provide it, depending on whether the user has logged in, and whether
    // the session has expired.
    $fbu = $data['fbu'];
    $result = db_query("SELECT * FROM {fb_user_app} WHERE apikey = :apikey and fbu = :fbu", array(
      ':apikey' => $fb_app->apikey,
      ':fbu' => $fbu,
    ));
    $data = $result
      ->fetchObject();
    if ($data && $data->session_key) {

      // Return array with FB id and apikey.
      $return = array(
        'fbu' => $data->fbu,
        'access_token' => $data->session_key,
        'expires' => $data->session_key_expires,
      );
    }
  }
  elseif ($op == FB_OP_AJAX_EVENT) {

    // handle internal login
    // @TODO - global user is not correct here.
    // fb.js has notified us of an event via AJAX.  Not the same as facebook event callback above.
    if ($data['event_type'] == 'session_change' && isset($data['event_data']['fbu'])) {

      // A user has logged in.
      fb_user_app_track($fb, $fb_app, $user);
    }
  }
}