You are here

function fb_ajax_event in Drupal for Facebook 7.4

Same name and namespace in other branches
  1. 6.3 fb.module \fb_ajax_event()
  2. 7.3 fb.module \fb_ajax_event()

Menu callback for ajax event.

Returns json encoded array of javascript to execute in response to this event..

1 string reference to 'fb_ajax_event'
fb_menu in ./fb.module
Implements hook_menu().

File

./fb.module, line 1560

Code

function fb_ajax_event($event_type) {
  if (!empty($_REQUEST['client_id'])) {
    $fba = $_REQUEST['client_id'];
  }
  else {
    watchdog('fb', 'FB ajax event invoked without application id.', array(), WATCHDOG_ERROR);
  }

  // The JS tells fb.js to perform actions after the ajax event has been handled.
  $js_array = array();
  if ($event_type == 'token_invalid') {

    // fb.js has detected that a token has expired.
    if (fb_user_token() == $_REQUEST['invalid_token']) {

      // End the current drupal session.
      fb_logout();
      $js_array['fb_ajax_event'] = "FB_JS.reload()";
    }
    fb_token_invalid($_REQUEST['invalid_token']);
  }
  elseif ($event_type == 'session_change') {

    // session_change means a new token, or an old token has expired.
    if (!empty($_SESSION['fb'])) {
      if (!empty($_SESSION['fb'][$fba]) && !empty($_SESSION['fb'][$fba]['access_token'])) {

        // Forget session cache associated with old token.
        fb_token_invalid($_SESSION['fb'][$fba]['access_token']);
      }

      // Forget session cache associated with app id.
      unset($_SESSION['fb'][$fba]);
    }
  }

  // Remember the new token.
  if (!empty($_REQUEST['access_token'])) {
    fb_user_token($fba, $_REQUEST['access_token']);
  }

  // We might be passed signed_request in addition to access token.
  if (!empty($_REQUEST['signed_request'])) {
    $sr = fb_parse_signed_request($_REQUEST['signed_request']);
    $_SESSION['fb'][$fba]['signed_request'] = $sr;
    $_SESSION['fb'][$fba]['fbu'] = $sr['user_id'];
    $_SESSION['fb'][$fba]['status'] = $_REQUEST['status'];
  }

  // Allow third-parties to act.
  $data = isset($_SESSION['fb'][$fba]) ? $_SESSION['fb'][$fba] : array();
  $data['fba'] = $fba;
  $data['event_type'] = $event_type;
  $js_array = fb_invoke(FB_OP_AJAX, $data, $js_array);
  drupal_json_output(array_values($js_array));
}