You are here

function fb_devel_fb in Drupal for Facebook 7.3

Same name and namespace in other branches
  1. 5.2 fb_devel.module \fb_devel_fb()
  2. 5 fb_devel.module \fb_devel_fb()
  3. 6.3 fb_devel.module \fb_devel_fb()
  4. 6.2 fb_devel.module \fb_devel_fb()
  5. 7.4 fb_devel.module \fb_devel_fb()

Implements hook_fb().

File

./fb_devel.module, line 155
Makes development with Drupal for Facebook much easier. Keep this module enabled until you're confident your app works perfectly.

Code

function fb_devel_fb($op, $data, &$return) {
  $fb_app = isset($data['fb_app']) ? $data['fb_app'] : NULL;
  $fb = isset($data['fb']) ? $data['fb'] : NULL;
  $errors = 0;
  if ($op == FB_OP_INITIALIZE) {
    if (fb_settings(FB_SETTINGS_APIKEY) && $fb_app->apikey != fb_settings(FB_SETTINGS_APIKEY)) {
      $message = t('Drupal for Facebook has detected a problem.  The initialized app has an apikey (%fb_app_apikey), while the settings indicates a different apikey (%fb_settings_apikey).', array(
        '%fb_app_apikey' => $fb_app->apikey,
        '%fb_settings_apikey' => fb_settings(FB_SETTINGS_APIKEY),
      ));
      drupal_set_message($message, 'error');
      watchdog('fb_devel', $message, array(), WATCHDOG_WARNING);
      $errors++;
    }

    // This value comes from url rewriting.  Recommended on canvas pages.
    $id = fb_settings(FB_SETTINGS_CB);

    // Sanity check.
    if ($id && $id != $fb_app->id) {
      $message = t('fb_app id (%fb_app_id) does not equal fb settings id (%fb_settings_id).  This is usually caused by the wrong callback url on your <a href="!url">facebook application settings form</a>.', array(
        '%fb_app_id' => $fb_app->id,
        '%fb_settings_id' => $id,
        '!url' => "http://www.facebook.com/developers/apps.php",
      ));
      drupal_set_message($message, 'warning');
      watchdog('fb_devel', $message, array(), WATCHDOG_WARNING);
      $errors++;
    }

    // Catch badly formed links ASAP.
    if ($id && !fb_is_canvas() && !fb_is_tab()) {

      // Skip check on callbacks from facebook.
      if (arg(0) != 'fb_app' || arg(1) != 'event') {
        $message = t('URL starts with %prefix.  This should never happen on connect pages.  Did the previous page have a badly formed link?', array(
          '%prefix' => FB_SETTINGS_CB . '/' . $id,
        ));
        drupal_set_message($message, 'error');
        $errors++;
      }
    }

    // path replacement sanity check
    global $base_path;
    if ($base_path == "/{$fb_app->canvas}/") {
      $message = t('Facebook canvas page matches Drupal base_path (%base_path).  This is currently not supported.', array(
        '%base_path' => $base_path,
      ));
      drupal_set_message($message, 'error');
      watchdog('fb_devel', $message);
    }

    // Old API Sanity check.
    if (isset($_REQUEST['fb_sig'])) {
      $message = t('Passed old-style fb_sig parameters.  Go to !url, edit your application.  Under "migrations" enable "new sdks".', array(
        '!url' => 'http://www.facebook.com/developers/apps.php',
      ));
      dpm($message);
      watchdog('fb_devel', $message);
    }

    // server URL sanity check
    // This is an expensive test, because it invokes api_client.
    try {

      // @todo admin.getAppProperties deprecated.  Use graph API instead.
      $props = $fb
        ->api(array(
        'method' => 'admin.getAppProperties',
        'access_token' => fb_get_token($fb),
        'properties' => array(
          'connect_url',
          'callback_url',
        ),
      ));
      $props = json_decode($props, TRUE);
      if (is_array($props)) {

        // Strip "http(s):" to avoid warnings when the only difference is http vs https
        $baseurl = str_replace(array(
          'http://',
          'https://',
        ), '', $GLOBALS['base_url']);
        foreach ($props as $prop => $url) {
          if ($url && strpos($url, $baseurl) === FALSE) {
            $message = t('The Facebook Application labeled %label has a suspicious %prop.  The value is %value. This server expected %url.  To repair !applink change the ID and secret, or sync remote settings.', array(
              '%label' => $fb_app->label,
              '%prop' => $prop,
              '%value' => $url,
              '%url' => $GLOBALS['base_url'],
              '!applink' => l($fb_app->label, FB_PATH_ADMIN_APPS . '/' . $fb_app->label),
            ));
            if (user_access('access administration pages')) {
              drupal_set_message($message, 'warning');
            }
            watchdog('fb_devel', $message);
          }
        }
      }
    } catch (Exception $e) {
      dpm($e, __FUNCTION__);
      if ($e
        ->getCode() == 102) {

        // Session key invalid or no longer valid 102, which we can ignore.
      }
      else {
        fb_log_exception($e, t('Failed to get app properties for %name.', array(
          '%name' => $fb_app->title,
        )));
      }
    }

    // App data sanity checks.
    $fb_app_data = fb_get_app_data($fb_app);

    // Account mapping format has changed!
    if (isset($fb_app_data['fb_user']) && !is_array($fb_app_data['fb_user']['map_account'])) {
      $message = 'The options for mapping facebook account to local accounts has changed.  You must manually <a href=!url>edit your application</a>.  Look for the map accounts options under facebook user settings.';
      $args = array(
        '!url' => url(FB_PATH_ADMIN_APPS . '/' . $fb_app->label),
      );
      if (user_access('access administration pages')) {
        drupal_set_message(t($message, $args), 'error');
      }
      watchdog('fb_devel', $message, $args, WATCHDOG_ERROR);
    }
  }
  elseif (module_exists('fb_app') && $op == FB_APP_OP_EVENT) {
    $type = $data['event_type'];

    // Facebook has notified us of some event.
    $message = t('Facebook has notified the %label application of a %type event.', array(
      '%label' => $fb_app->label,
      '%type' => $type,
    ));
    $message .= '<pre>' . print_r($data, 1);
    $message .= print_r($_REQUEST, 1) . '</pre>';
    watchdog('fb_devel', $message);
  }
  elseif ($op == FB_OP_JS) {

    // Start debugger

    //$return[] = "debugger; // added by fb_devel.module";
  }
  elseif ($op == FB_OP_POST_INIT) {
    if (FALSE) {

      // TRUE to enable this test
      // Placing a value in the session can help detect when session is persisted across pages.  But can also have subtle side effects.
      if (isset($_SESSION['fb_devel'])) {

        // Counter helps track how long session has been around.
        $_SESSION['fb_devel']['FB_OP_POST_INIT'] = $_SESSION['fb_devel']['FB_OP_POST_INIT'] + 1;
      }
      else {
        $_SESSION['fb_devel']['FB_OP_POST_INIT'] = 1;
      }
    }

    // Javascript to help us with sanity checks.
    drupal_add_js(array(
      'fb_devel' => array(
        'session_id' => session_id(),
        'session_name' => session_name(),
      ),
    ), 'setting');
  }
  elseif ($op == FB_OP_AJAX_EVENT) {
    extract($data);
    if (fb_verbose() == 'extreme' && FALSE) {

      // disabled to prevent console not defined error.
      $session_id = session_id();
      $session_name = session_name();
      $return[] = "console.log('fb_devel.module extreme verbosity: original session_id is ' + Drupal.settings.fb_devel.session_name + ':' + Drupal.settings.fb_devel.session_id + ', session_id during FB_OP_AJAX_EVENT is {$session_name}:{$session_id}');";
      $fbu = fb_facebook_user($data['fb']);
      $return[] = "console.log('fb_facebook_user() during FB_OP_AJAX_EVENT is {$fbu}, data[event_data][fbu] is {$data[event_data][fbu]}');";
      $return[] = "console.log('_COOKIE[fbu_{$fb_app->apikey}] is " . $_COOKIE['fbu_' . $fb_app->apikey] . "');";
      $return[] = "FB_JS.reload();";
    }
    if (fb_verbose() == 'extreme') {
      watchdog('fb_devel', "modules/fb ajax callback {$event_type}: <pre>" . dprint_r($event_data, 1) . "</pre>");
    }
  }
}