You are here

function betterlogin_init in Better Login 7

Implements hook_init().

File

./betterlogin.module, line 35
Make the login screens better :).

Code

function betterlogin_init() {

  // Make sure that the user is not logged in.
  global $user;
  if (!$user->uid) {

    // We need to collect where they were going in the first place
    // because they may get pissed if
    // they don't get there after logging in :).
    $destination = '';
    if (isset($_GET['destination'])) {
      $destination = drupal_get_destination();
    }
    elseif (isset($_GET['q'])) {
      $destination = array(
        'destination' => $_GET['q'],
      );
    }

    // If this site is set to private we want to redirect all anonymous
    // users to the login form.
    if (variable_get('betterlogin_private')) {

      // Because of Drush we only want to block anything not from CLI.
      if (strtolower(arg(0)) !== 'user' && php_sapi_name() !== 'cli') {
        drupal_goto('user/login', array(
          'query' => $destination,
        ));
      }
    }

    // Make sure that anon users cannot go to just /user but directly
    // to the login form.
    if (strtolower(arg(0)) == 'user' && !arg(1) && php_sapi_name() !== 'cli') {
      if (isset($destination)) {
        unset($_GET['destination']);
      }
      drupal_goto('user/login', array(
        'query' => $destination,
      ));
    }
  }
}