You are here

function hybridauth_popup in HybridAuth Social Login 7

1 string reference to 'hybridauth_popup'
hybridauth_menu in ./hybridauth.module
Implements hook_menu().

File

./hybridauth.pages.inc, line 21

Code

function hybridauth_popup() {
  global $user;
  $provider_id = isset($_REQUEST['provider']) ? $_REQUEST['provider'] : NULL;

  // If no provider was given then look for saved preference
  if (empty($provider_id) || $provider_id == 'none') {
    if (isset($_COOKIE['__ha_provider']) && !empty($_COOKIE['__ha_provider'])) {
      $provider_id = $_COOKIE['__ha_provider'];
    }
  }
  else {
    setcookie('__ha_provider', $provider_id, REQUEST_TIME + 31536000, base_path(), variable_get('cookie_domain', ''));
  }

  // Make sure it's Drupal that starts the session then load the lib
  drupal_session_start();
  module_load_include('inc', 'hybridauth', 'hybridauth.auth');

  // Try to get HybridAuth instance
  try {
    $hybridauth = hybridauth_get_instance();
  } catch (ErrorException $e) {
    drupal_set_message(t('There was an error processing your request!'), 'error');
    watchdog_exception('hybridauth', $e);

    // Cleanly close popup and redirect
    $GLOBALS['devel_shutdown'] = FALSE;

    // Prevent devel module from spewing.
    $destination = isset($_GET['destination']) ? $_GET['destination'] : 'user';
    drupal_add_js('
      if (window.opener){
        try { window.opener.parent.$.colorbox.close(); } catch(err) {}
        window.opener.parent.location.href = "' . url($destination, array(
      'absolute' => TRUE,
    )) . '";
      }
      window.self.close();
    ', 'inline');
    $page = array(
      'page_top' => '',
      '#children' => 'Closing...',
      'page_bottom' => '',
    );
    print theme('html', array(
      'page' => $page,
    ));
    drupal_exit();
  } catch (Exception $e) {

    //watchdog_exception('hybridauth', $e);
    $redirect = TRUE;
    switch ($e
      ->getCode()) {
      case 5:

        // Authentification failed. The user has canceled the authentication or the provider refused the connection.
        // Close popup window and leave overlay alone.
        $redirect = FALSE;
        break;
      case 0:

      // Unspecified error.
      case 1:

      // Hybriauth configuration error.
      case 2:

      // Provider not properly configured.
      case 3:

      // Unknown or disabled provider.
      case 4:

      // Missing provider application credentials.
      default:

        // Report the error and log it.
        drupal_set_message(t('There was an error processing your request!'), 'error');
        watchdog_exception('hybridauth', $e);
    }

    // Cleanly close popup and redirect
    $GLOBALS['devel_shutdown'] = FALSE;

    // Prevent devel module from spewing.
    $destination = isset($_GET['destination']) ? $_GET['destination'] : 'user';
    drupal_add_js('
      var redirect = ' . ($redirect ? 'true' : 'false') . ';
      if (window.opener && redirect) {
        try { window.opener.parent.$.colorbox.close(); } catch(err) {}
        window.opener.parent.location.href = "' . url($destination, array(
      'absolute' => TRUE,
    )) . '";
      }
      window.self.close();
    ', 'inline');
    $page = array(
      'page_top' => '',
      '#children' => 'Closing...',
      'page_bottom' => '',
    );
    print theme('html', array(
      'page' => $page,
    ));
    drupal_exit();
  }

  // If we don't have a provider ID show a list of providers.
  if (empty($provider_id) || $provider_id == 'none' || $provider_id == 'list') {
    return _hybridauth_popup_list($hybridauth);
  }
  elseif ($provider_id == 'OpenID' && !isset($_GET['openid_identifier'])) {
    $destination = isset($_GET['destination']) ? $_GET['destination'] : 'user';
    unset($_GET['destination']);
    $query = drupal_get_query_parameters();
    $query['destination'] = $destination;
    drupal_goto('hybridauth/popup/openid', array(
      'query' => $query,
    ));
  }
  elseif (isset($_GET['authenticate']) && $_GET['authenticate']) {
    return _hybridauth_popup_auth($hybridauth, $provider_id);
  }
  else {
    return _hybridauth_popup_provider($hybridauth, $provider_id);
  }

  // We shouldn't get here
  return MENU_ACCESS_DENIED;
}