You are here

function fb_user_user in Drupal for Facebook 6.3

Same name and namespace in other branches
  1. 5.2 fb_user.module \fb_user_user()
  2. 5 fb_user.module \fb_user_user()
  3. 6.2 fb_user.module \fb_user_user()

Implementation of hook_user().

File

./fb_user.module, line 619
This module manages relations between local Drupal user accounts and their accounts on facebook.com.

Code

function fb_user_user($op, &$edit, &$account, $category = NULL) {
  global $user, $_fb_app;
  if ($op == 'load' && $account->uid && $_fb_app) {
    if (!$account->mail && ($fbu = _fb_user_get_fbu($account->uid))) {

      // Use proxied email, if facebook app is active and user uses it.
      // TODO: confirm drupal never saves proxied address to users.mail.
      $account->mail = fb_user_get_proxied_email($fbu, $_fb_app);
      $account->fb_user_proxied_mail = $account->mail;

      // Remember where we got address.
    }
  }
  if (!$_fb_app && $op == 'insert' || $op == 'login') {

    // A facebook user has logged in.  We can map the two accounts together.
    $fb_user_data = _fb_user_get_config($_fb_app);
    if (($fbu = fb_facebook_user()) && $fb_user_data['map_account'][FB_USER_OPTION_MAP_ALWAYS] && !fb_controls(FB_USER_CONTROL_NO_CREATE_MAP)) {

      // Create fb_user record if it doesn't exist or update existing one
      _fb_user_set_map($account, $fbu);

      // @TODO - if the app has a role, make sure the user gets that role. (presently, that will not happen until their next request)
    }
  }

  // Add tabs on user edit pages to manage maps between local accounts and facebook accounts.
  if ($op == 'categories') {

    // A tab allowing authmaps to be changed.
    $items[] = array(
      'name' => 'fb_user',
      'title' => t('Facebook'),
      'access callback' => 'fb_user_access_own',
      'access arguments' => array(
        1,
        'delete own fb_user authmap',
        TRUE,
      ),
      'weight' => 1,
    );
    return $items;
  }
  elseif ($op == 'form' && $category == 'fb_user') {
    if (!user_access('administer users') && !(user_access('delete own fb_user authmap') && $user->uid == $account->uid)) {
      return;
    }

    // hide from this user
    $fb_user_data = _fb_user_get_config($_fb_app);
    $fbu = _fb_user_get_fbu($account->uid);
    if ($fbu) {

      // The drupal user is a facebook user.
      $form['map'] = array(
        '#type' => 'checkbox',
        '#title' => t('Connect to facebook.com'),
        '#default_value' => $fbu,
        '#return_value' => $fbu,
        '#description' => '',
      );

      // Now, learn more from facebook.
      try {
        $data = fb_api($fbu, array(
          'access_token' => fb_get_token(),
        ));
        if (count($data)) {
          $form['map']['#description'] .= t('Local account !username corresponds to !profile_page on Facebook.com.', array(
            '!username' => l($account->name, 'user/' . $account->uid),
            '!profile_page' => l($data['name'], $data['link']),
          ));
        }
      } catch (Exception $e) {
        fb_log_exception($e, t('Failed to get user data from facebook.'));
      }
      if (fb_facebook_user() == $fbu) {

        // The user is currently connected to facebook.  Depending on
        // config, they may not be able to break the connection.
        $form['map']['#disabled'] = TRUE;
        $form['map']['#description'] .= '<br/>' . t('(Checkbox disabled because you are currently connected to facebook.)');
      }
      else {
        $form['map']['#description'] .= '<br/>' . t('Uncheck then click save to delete this connection.');
      }
    }
    if (!$fbu) {

      // this tells us that a mapping hasn't been created
      if ($user->uid == $account->uid) {

        // Could not obtain the $fbu from an existing map.
        $fbu = fb_facebook_user();
        if ($fbu) {

          // they are connected to facebook; give option to map
          $form['map'] = array(
            '#type' => 'checkbox',
            '#title' => t('Connect account to facebook.com'),
            '#default_value' => 0,
            '#return_value' => $fbu,
            '#description' => '',
          );
          $form[$_fb_app->label] = array(
            '#type' => 'markup',
            '#value' => t('Check box and save to connect local account !username to facebook.com.', array(
              '!username' => theme('username', $account),
            )),
            '#prefix' => "\n<p>",
            '#suffix' => "</p>\n",
          );
        }
        elseif (!$fbu && $_fb_app) {

          // they are not connected to facebook; give option to connect here
          $fb_button = theme('fb_login_button', t('Connect with Facebook'));
          $form['fb_user'] = array(
            '#value' => $fb_button,
            '#type' => 'markup',
            '#weight' => -1,
            '#prefix' => "\n<p>",
            '#suffix' => "</p>\n",
          );
        }
      }
      else {
        $form[$_fb_app->label] = array(
          '#type' => 'markup',
          '#value' => t('Local account !username is not connected to facebook.com.', array(
            '!username' => theme('username', $account),
          )),
          '#prefix' => "\n<p>",
          '#suffix' => "</p>\n",
        );
      }
    }
    if (isset($form)) {
      $form['map']['#tree'] = TRUE;
    }
    else {

      // Could add a facebook connect button or canvas page authorization link.
      $form['description'] = array(
        '#type' => 'markup',
        '#value' => t('This account is not associated with a Facebook Application.'),
        '#prefix' => '<p>',
        '#suffix' => '</p>',
      );
    }
    return $form;
  }
  elseif ($op == 'update' && $category == 'fb_user') {
    if ($edit['map']) {
      _fb_user_set_map($account, $edit['map']);
    }
    else {

      // Delete account mapping, because administrator has unchecked the connect option.
      db_query('DELETE FROM {fb_user} WHERE uid=%d', $account->uid);
      fb_invoke(FB_USER_OP_POST_USER_DISCONNECT, array(
        'account' => $account,
      ), NULL, 'fb_user');
    }
  }
  elseif ($op == 'delete') {
    db_query('DELETE FROM {fb_user} WHERE uid=%d', $account->uid);
  }
  elseif ($op == 'logout') {
    if (fb_facebook_user() && fb_api_check_session($GLOBALS['_fb'])) {

      // Log out of facebook, as well as Drupal.  Note that code in
      // fb_connect.js and fb_canvas.js attempts to call FB.logout.  However,
      // that code is not reached if the user types "/logout" directly into
      // the browser URL.  Also, a sometimes-occuring bug in firefox prevents
      // FB.logout from always succeeding.
      // Figure out where to send the user.
      if (isset($_REQUEST['destination'])) {
        $next_url = url($_REQUEST['destination'], array(
          'absolute' => TRUE,
          'fb_canvas' => fb_is_canvas(),
        ));

        // Unset desination so drupal_goto() below does what we need it to do.
        unset($_REQUEST['destination']);
      }
      else {
        $next_url = url('<front>', array(
          'absolute' => TRUE,
          'fb_canvas' => fb_is_canvas(),
        ));
      }
      $logout_url = $GLOBALS['_fb']
        ->getLogoutUrl(array(
        'next' => $next_url,
        'cancel_url' => $next_url,
      ));

      //drupal_goto($logout_url);
    }
  }
}