You are here

function fb_actions_profile_fbml in Drupal for Facebook 5.2

Same name and namespace in other branches
  1. 5 fb_actions.module \fb_actions_profile_fbml()

File

./fb_actions.module, line 101
Actions defined here interact with Facebook's API. This makes it possible to notify facebook of various activities as they happen.

Code

function fb_actions_profile_fbml(&$object, $values) {

  //dpm(func_get_args(), "fb_actions_profile_fbml");
  if ($values['hook'] == 'nodeapi') {
    $node = $values['node'];
  }
  else {
    if ($values['hook'] == 'comment') {
      $comment = $values['comment'];
      $node = node_load($comment->nid);
    }
    else {
      if ($values['hook'] == 'user') {
        $account = $values['user'];

        // untested
      }
    }
  }

  // Determine which app to use
  if ($object) {
    $fb_app = $object;
  }
  else {
    if ($values['fb_app_nid'] && is_numeric($values['fb_app_nid'])) {
      $fb_app = fb_get_app(array(
        'nid' => $values['fb_app_nid'],
      ));
    }
    else {
      if ($values['fb_app_nid'] == FB_APP_CURRENT) {
        $fb_app = $GLOBALS['fb_app'];
      }
    }
  }

  // Use already configured $fb if its passed in (cron jobs)
  $fb = $values['fb'];
  if (!$fb && $fb_app) {

    // Otherwise, login to facebook as either the current user or the infinite session
    $fb = fb_api_init($fb_app, FB_FBU_ANY);
  }
  if ($fb) {
    global $user;
    $fbu = fb_get_fbu($user->uid, $fb_app);

    // Replace fb_app related tokens
    $body = token_replace($values['body'], 'fb_app', $fb_app);
    $body = check_markup($body, $values['body_filter'], FALSE);
    if ($profile_main) {
      $profile_main = check_markup($profile_main, $values['body_filter'], FALSE);
    }
    if ($mobile_profile) {
      $mobile_profile = check_markup($mobile_profile, $values['body_filter'], FALSE);
    }

    // Links in profile FBML must be fully-qualified.  Here we attempt to
    // replace relative links with fully qualified ones.
    global $base_url, $base_path;
    $patterns[] = '/"' . str_replace('/', '\\/', $base_path) . '([^"]*)"/';
    $replacements[] = '"' . $base_url . '/$1"';
    $body = preg_replace($patterns, $replacements, $body);
    if ($profile_main) {
      $profile_main = preg_replace($patterns, $replacements, $profile_main);
    }
    if ($mobile_profile) {
      $mobile_profile = preg_replace($patterns, $replacements, $mobile_profile);
    }

    // Rewrite links to reference canvas pages
    if ($fb_app->canvas && function_exists('fb_canvas_process_fbml')) {
      $body = fb_canvas_process_fbml($body, $fb_app);
      if ($profile_main) {
        $profile_main = fb_canvas_process_fbml($profile_main, $fb_app);
      }
      if ($mobile_profile) {
        $mobile_profile = fb_canvas_process_fbml($mobile_profile, $fb_app);
      }
    }
    if (variable_get('fb_actions_verbose', FALSE)) {
      watchdog('fb_actions', t('Setting Facebook profile markup for !user to !body', array(
        '!user' => theme('username', $user),
        '!body' => htmlentities($body),
      )));
    }
    try {

      // http://wiki.developers.facebook.com/index.php/Profile.setFBML
      $fb->api_client
        ->profile_setFBML(NULL, $fbu, $body, NULL, $mobile_profile, $profile_main);
    } catch (Exception $e) {
      fb_log_exception($e, t('Failed to set profile FBML'), $fb);
    }
  }
}