You are here

function fb_user_token in Drupal for Facebook 7.4

The user-specific token allows individual users to post to their own feeds.

14 calls to fb_user_token()
fb_access_token in ./fb.module
The access token allows the Fb app to publish and read posts.
fb_admin_add_token_form in ./fb.admin.inc
fb_ajax_event in ./fb.module
Menu callback for ajax event.
fb_connect_translated_menu_link_alter in ./fb_connect.module
Implements hook_translated_menu_link_alter().
fb_devel_page in ./fb_devel.module
Provides a page with useful debug info.

... See full list

1 string reference to 'fb_user_token'
fb_user.module in ./fb_user.module
This module manages relations between local Drupal user accounts and their accounts on facebook.com.

File

./fb.module, line 877

Code

function fb_user_token($app = NULL, $token = NULL) {
  static $cache = NULL;
  if (!$app) {
    $app = fb_get_app();
  }
  if (is_array($app) && !empty($app['client_id'])) {
    $client_id = $app['client_id'];
  }
  else {
    $client_id = $app;
  }
  if (!$client_id) {
    return FB_TOKEN_NONE;
  }

  // Set value.
  if ($token) {
    if ($token == 'null') {

      // @todo how does string "null" get here???
      $token = FB_TOKEN_NONE;
    }
    $cache[$client_id] = $token;
    if (empty($_SESSION['fb'][$client_id]) || $_SESSION['fb'][$client_id]['access_token'] != $token) {

      // Notify third-party modules of newly connected user.
      fb_invoke(FB_OP_TOKEN_USER, array(
        'client_id' => $client_id,
        'access_token' => $token,
      ));
    }
    $_SESSION['fb'][$client_id]['access_token'] = $token;
  }

  // Return value.
  if ($token === NULL && isset($_REQUEST['access_token'])) {

    // Token passed to us, usually via ajax event.
    return fb_user_token($client_id, $_REQUEST['access_token']);
  }
  elseif ($token === NULL && isset($_REQUEST['code']) && ($new_token = fb_auth_get_token($app))) {

    // Token provided via server auth URL.
    return fb_user_token($client_id, $new_token);
  }
  elseif ($token === NULL && !empty($_REQUEST['signed_request'])) {

    // Canvas pages and page tabs might have a signed request.
    try {
      $sr = fb_parse_signed_request($_REQUEST['signed_request'], is_array($app) && !empty($app['secret']) ? $app['secret'] : NULL);
      if (!empty($sr['oauth_token'])) {
        return fb_user_token($client_id, $sr['oauth_token']);
      }
    } catch (Exception $e) {

      // Fake signed request or out-of-date app secret.
      fb_log_exception($e, t('Failed to parse or validate signed_request.'));
    }
  }
  elseif (!empty($cache) && !empty($cache[$client_id])) {
    if ($cache[$client_id] == 'null') {

      // @todo why does this happen?
      $cache[$client_id] = FB_TOKEN_NONE;
    }
    return $cache[$client_id];
  }
  elseif ($token === NULL && !empty($_SESSION['fb']) && !empty($_SESSION['fb'][$client_id]) && !empty($_SESSION['fb'][$client_id]['access_token'])) {
    return fb_user_token($client_id, $_SESSION['fb'][$client_id]['access_token']);
  }
  return FB_TOKEN_NONE;
}