You are here

function fbconnect_page_alter in Facebook Connect 8.2

Same name and namespace in other branches
  1. 7.2 fbconnect.module \fbconnect_page_alter()

Implements hook_page_alter().

File

./fbconnect.module, line 271
@todo.

Code

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>',
    );
  }
}