You are here

fbconnect.module in Facebook Connect 8.2

@todo.

File

fbconnect.module
View source
<?php

/**
 * @file
 * @todo.
 */
use Facebook\Facebook;
use Facebook\Authentication\AccessToken;

/**
 * Implements hook_menu().
 */
function fbconnect_menu() {
  $items['admin/config/people/fbconnect'] = array(
    'title' => 'Facebook Connect',
    'description' => 'Configure required settings for Facebook integration',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'fbconnect_api_keys_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'fbconnect.admin.inc',
  );
  $items['admin/config/people/fbconnect/api-keys'] = array(
    'title' => 'Api Keys',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'file' => 'fbconnect.admin.inc',
  );
  $items['admin/config/people/fbconnect/apperance'] = array(
    'title' => 'Appearance',
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'fbconnect_appearance_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'fbconnect.admin.inc',
  );
  $items['admin/config/people/fbconnect/fb-app'] = array(
    'title' => 'App Settings',
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'fbconnect_fbapp_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'fbconnect.admin.inc',
    'weight' => 3,
  );
  return $items;
}

/**
 * Locates and loads the Facebook PHP SDK library.
 */
function _facebook_client_load_include() {
  if (!class_exists('Facebook\\Facebook')) {
    $sdk_path = DRUPAL_ROOT . '/sites/all/libraries/facebook-php-sdk-v4/src/Facebook/autoload.php';
    $library = array(
      'loaded' => file_exists($sdk_path),
    );
    if ($library['loaded']) {
      require_once $sdk_path;
    }
    if (!$library['loaded']) {
      watchdog('fbconnect', 'Unable to load the required Facebook library, please check the README.txt for instructions on how to resolve this.');
    }
  }
  $exists = class_exists('Facebook\\Facebook') && defined('Facebook\\Facebook::VERSION');
  return $exists;
}

/**
 * Get the Facebook client object for easy access.
 * @return Facebook
 *   Facebook Api object
 */
function facebook_client() {
  static $fb = NULL;
  drupal_session_initialize();
  if (is_null($fb)) {
    $conf = fbconnect_get_config();
    if ($conf && _facebook_client_load_include()) {
      $initParams = array(
        'app_id' => $conf['app_id'],
        'app_secret' => $conf['secret_api_key'],
        'default_graph_version' => 'v2.3',
      );
      $fb = new Facebook($initParams);
    }
  }
  return $fb;
}

/**
 * Get the Facebook access token object for easy access.
 */
function facebook_get_access_token() {
  static $token = NULL;
  $facebook = facebook_client();
  $facebook_config = fbconnect_get_config();
  if (!empty($_SESSION['fbconnect_token'])) {
    $token = new AccessToken($_SESSION['fbconnect_token']);
  }
  elseif (!empty($_COOKIE['fbconnect_token_' . $facebook_config['app_id']])) {
    $_SESSION['fbconnect_token'] = $_COOKIE['fbconnect_token_' . $facebook_config['app_id']];
    $token = new AccessToken($_SESSION['fbconnect_token']);
  }
  elseif (is_null($token) && $facebook) {
    $helper = $facebook
      ->getJavaScriptHelper();
    try {
      $token = $helper
        ->getAccessToken();
    } catch (Exception $e) {
      $token = NULL;
    }
  }
  if (!empty($token)) {
    try {
      $request = $facebook
        ->request('GET', '/me', array(), $token);
      $response = $facebook
        ->getClient()
        ->sendRequest($request);
      $_SESSION['fbconnect_token'] = $token
        ->getValue();
    } catch (Exception $e) {
      $token = NULL;
      unset($_SESSION['fbconnect_token']);
      unset($_COOKIE['fbconnect_token_' . $facebook_config['app_id']]);
      setcookie('fbconnect_token_' . $facebook_config['app_id'], '');
    }
  }
  return $token;
}

/**
 * Get the user profile or return null if they are not logged in and registered.
 * @return user_profile array
 */
function fbconnect_user_profile() {
  $user_profile = NULL;
  if (($client = facebook_client()) && ($request = $client
    ->request('GET', '/me', array(), facebook_get_access_token()))) {
    try {

      // Proceed knowing you have a logged in user who's authenticated.
      $response = $client
        ->getClient()
        ->sendRequest($request);
      $user_profile = $response
        ->getGraphUser()
        ->asArray();
    } catch (Exception $e) {
      $user_profile = NULL;
    }
  }
  return $user_profile;
}

/**
 * Get fbconnect config parameter.
 * @return array
 */
function fbconnect_get_config() {
  global $base_url;
  static $config;
  if (!$config) {
    if (module_exists('i18n')) {
      global $language;
      $switch_code = variable_get('fbconnect_language_code_' . $language->language, '');
      if ($switch_code) {
        $config['language_code'] = $switch_code;
      }
    }
    $config['user_pictures'] = variable_get('fbconnect_pic_allow', 'allow');
    $config['language_code'] = variable_get('fbconnect_language_code', 'en_US');
    $config['app_id'] = variable_get('fbconnect_appid', NULL);
    $config['secret_api_key'] = variable_get('fbconnect_skey', NULL);
    $config['debug'] = variable_get('fbconnect_debug', FALSE);
    if (variable_get('fbconnect_ssl', FALSE)) {
      $config['connect_js'] = "'https://connect.facebook.net/{$config['language_code']}/sdk.js'";
    }
    else {
      $config['connect_js'] = "document.location.protocol + '//connect.facebook.net/{$config['language_code']}/sdk.js'";
    }
    $config['loginout_mode'] = variable_get('fbconnect_loginout_mode', 'manual');
    $config['invite_name'] = variable_get('fbconnect_invite_name', variable_get('site_name', $base_url));
    $config['fast_reg_mode'] = variable_get('fbconnect_fast_reg', NULL);
    $config['fast_reg_autoname'] = variable_get('fbconnect_fast_reg_autoname', 1);

    // Allow third party modules to change settings.
    drupal_alter('fbconnect_config', $config);
  }
  if ($config['app_id'] && $config['secret_api_key']) {
    return $config;
  }
}

/**
 * Check Facebook session.
 *
 * @param boolean $check_connected
 *   ensure that active user is connected with active Facebook account
 *
 * @return integer
 *   Facebook user id
 */
function fbconnect_get_fbuid($check_connected = FALSE) {
  global $user;
  $fbuid = 0;
  $fb = facebook_client();
  if ($fb && ($request = $fb
    ->request('GET', '/me', array(), facebook_get_access_token()))) {
    try {
      $client = $fb
        ->getClient();
      $response = $client
        ->sendRequest($request);
      $fbuid = $response
        ->getGraphUser()
        ->getId();
      if (module_exists('fbconnect_login') && $check_connected && $fbuid) {
        if (_get_user_fbuid($user->uid) != $fbuid) {
          $fbuid = NULL;
        }
      }
    } catch (Exception $e) {
      $fbuid = 0;
    }
  }
  return $fbuid;
}

/**
 * This function manage all javascripts used by this module.
 */
function fbconnect_render_js() {
  global $base_url;
  global $user;
  $module_path = drupal_get_path('module', 'fbconnect');
  if ($config = fbconnect_get_config()) {
    unset($config['secret_api_key']);
    $config['fbuid'] = fbconnect_get_fbuid();
    $config['user'] = array(
      'uid' => $user->uid,
    );
    if (module_exists('fbconnect_login')) {
      $user->fbuid = _get_user_fbuid($user->uid);
      $config['fbuid'] = @$user->fbuid;
    }
    drupal_add_js(array(
      'fbconnect' => $config,
    ), array(
      'type' => 'setting',
      'scope' => JS_DEFAULT,
    ));
    drupal_add_js($module_path . '/fbconnect.js');
    drupal_add_css($module_path . '/fbconnect.css');
  }
}

/**
 * Implements hook_page_alter().
 */
function fbconnect_page_alter(&$page) {
  if (facebook_client() && !_fbconnect_is_excluded_page($_GET['q']) && !variable_get('fbconnect_noroot')) {
    $config = fbconnect_get_config();
    $invite_msg = check_plain(variable_get('fbconnect_invite_msg', t('Enjoy!')));
    $channel_uri = preg_replace("@'@msi", "\\'", "http://" . $_SERVER['HTTP_HOST'] . "/" . drupal_get_path('module', 'fbconnect') . "/channel.html");
    $invite_code = arg(0) == "fbconnect" && arg(1) == "invite" && arg(2) == "friends" ? 'FB.ui({method: "apprequests", message: "' . $invite_msg . '"}, function() {});' : "";
    $page['page_bottom']['fb-init-code'] = array(
      '#type' => 'markup',
      '#markup' => '<div id="fb-root"></div>
      <script type="text/javascript">
        window.fbAsyncInit = function() {
          FB.init({
            appId : \'' . $config['app_id'] . '\',
            cookie : true, // enable cookies to allow the server to access the session
            xfbml : true,
            logging: \'' . $config['debug'] . '\',
            channelURL: \'' . $channel_uri . '\',
            oauth  : true,
            frictionlessRequests: true,
            status     : true,
            version    : \'v2.3\'
          });
          // whenever the user logs in, we tell our login service
          FB.Event.subscribe(\'auth.login\', function() {
            var token = FB.getAccessToken();
            if (token) {
              document.cookie = \'fbconnect_token_' . $config['app_id'] . '=\' + token;
            }
            else {
              window.location.reload(true);
            }
          });
          FB.Event.subscribe(\'auth.authResponseChange\', function(response) {
            if (response.status === \'connected\') {
              var token = FB.getAccessToken();
              if (token) {
                document.cookie = \'fbconnect_token_' . $config['app_id'] . '=\' + token;
              }
            }
          });
          FB.Event.subscribe(\'auth.logout\', function(response) {
            document.cookie = \'fbconnect_token_' . $config['app_id'] . '=\';
            window.location.reload(true);
          });

                    ' . $invite_code . '
          jQuery(document).trigger(\'fb:init\');
        };
        (function() {
          var e = document.createElement(\'script\');
          e.src = ' . $config['connect_js'] . ';
          e.async = true;
          document.getElementById(\'fb-root\').appendChild(e);
        }());
      </script>',
    );
  }
}

/**
 * Make Graph Query.
 *
 * @param string $path
 * @return array
 */
function fbconnect_graph_query($path, $params = array(), $method = 'GET', $use_app_token = FALSE) {
  if (($client = facebook_client()) && $path) {
    try {
      try {
        $token = $use_app_token ? $client
          ->getApp()
          ->getAccessToken() : facebook_get_access_token();
        $request = $client
          ->request($method, $path, $params, $token);
        $response = $client
          ->getClient()
          ->sendRequest($request);
        return $response
          ->getGraphObject();
      } catch (Exception $e) {
        drupal_set_message($e
          ->getMessage(), 'error');
        throw $e;
      }
    } catch (Exception $e) {
      $msg = 'Exception thrown while using fbconnect_graph_query : @code';
      watchdog('fbconnect', $msg, array(
        '@code' => $e
          ->getMessage(),
      ), WATCHDOG_WARNING);
    }
  }
}

/**
 * Check the users table to see if the email is already in the drupal system
 *  returns uid of user with the email.
 */
function _email_already_exist($email) {
  $query = db_select('users', 'u');
  $query
    ->fields('u', array(
    'uid',
  ));
  $query
    ->condition('mail', $email, '=');
  $query
    ->countQuery();
  return $query
    ->execute()
    ->fetchField();
}

/**
 * Query information from Facebook user table.
 *
 * @return array
 */
function fbconnect_get_user_info($fields = array(), $fbuid = NULL) {
  $params = array();
  if (!$fbuid) {
    $fbuid = fbconnect_get_fbuid();
  }
  if (is_array($fields) && count($fields)) {
    $params['fields'] = join(', ', $fields);
  }
  elseif ($fields) {
    $params['fields'] = $fields;
  }
  if ($fbuid) {
    return fbconnect_graph_query("/" . $fbuid, $params)
      ->asArray();
  }
}
function _fbconnect_is_excluded_page($path) {
  return drupal_match_path($path, variable_get('fbconnect_exclude_patterns', ''));
}

Functions

Namesort descending Description
facebook_client Get the Facebook client object for easy access.
facebook_get_access_token Get the Facebook access token object for easy access.
fbconnect_get_config Get fbconnect config parameter.
fbconnect_get_fbuid Check Facebook session.
fbconnect_get_user_info Query information from Facebook user table.
fbconnect_graph_query Make Graph Query.
fbconnect_menu Implements hook_menu().
fbconnect_page_alter Implements hook_page_alter().
fbconnect_render_js This function manage all javascripts used by this module.
fbconnect_user_profile Get the user profile or return null if they are not logged in and registered.
_email_already_exist Check the users table to see if the email is already in the drupal system returns uid of user with the email.
_facebook_client_load_include Locates and loads the Facebook PHP SDK library.
_fbconnect_is_excluded_page