<commit_before><?php

/**
 * bootstrap
 * sets up all required supporting services like:
 * - a transient objectCache to store objects in until they are removed or
 *   stored in the persistent store
 * - process login events
 * - process logout events
 * - process federated (openid and facebook) login events
 * - restart a cookie-based long-lived session
 * - start a URL-provided session (used for one-time logins)
 * @author <NAME>
 */

// short hand to SessionManager
$sm = SessionManager::getInstance();

// create initial structure for the transient object cache store
if( ! is_array( $sm->ObjectCache ) ) {
  $sm->ObjectCache = array();
}

// get an instance of the client and configure it
$openid = LightOpenIDClient::getInstance( Config::$server )
            ->withRequired( 'contact/email' )
            ->cacheInSession();

// setup facebook connection
$facebook = new Facebook(array(
  'appId'  => Config::$facebookAppId,
  'secret' => Config::$facebookSecret
));

// process login post
if( isset($_POST['login']) && isset($_POST['pass']) ) {
  $sm->login( $_POST['login'], $_POST['pass'] );
}

// process logout get
if( isset($_GET['action']) && $_GET['action'] == 'logout' ) {
  $sm->logout();
  $openid->logoff();
  // remove session cookie
  unset( $_COOKIE['session'] );
  setcookie( 'session', '', time() - 42000, '/');
  // remove XSFR session
  XSFR::clearSession();
  // redirect to home
  header("Location: home" );
}

function getFederatedUser() {
  global $openid, $facebook;
  if($user = $openid->getUser()) {
    return $user->identity;
  }
  if( $user = $facebook->getUser()) {
    return $user;
  }
  return false;
}

function clearFederatedUser() {
  global $openid, $facebook;
  $openid->logoff();
  $facebook->destroySession();
}

// if we have no current user but have a federated user try to log it on
if( $sm->currentUser->isAnonymous() && $federatedUser = getFederatedUser() ) {
  $sm->login_federated( $federatedUser );
  // if we have no current user now, the federated user is unknown
  if( $sm->currentUser->isAnonymous() ) {
    Messages::getInstance()->addWarning( I18N::$UNKNOWN_FEDERATED_LOGIN );
  } else {
    // clear it: we used it to log in, no longer of any use now.
    clearFederatedUser();
  }
}

// if we still don't have a user, try to restart an existing session
if( $sm->currentUser->isAnonymous()
    && isset( $_COOKIE['session'] ) && $_COOKIE['session'] != '' )
{
  $sm->login_federated( $_COOKIE['session'] );
}

// if we still don't have a user, try to use a one time password<commit_msg>workaround for possible bug in LightOpenIdClient<commit_after><?php

/**
 * bootstrap
 * sets up all required supporting services like:
 * - a transient objectCache to store objects in until they are removed or
 *   stored in the persistent store
 * - process login events
 * - process logout events
 * - process federated (openid and facebook) login events
 * - restart a cookie-based long-lived session
 * - start a URL-provided session (used for one-time logins)
 * @author <NAME>
 */

// short hand to SessionManager
$sm = SessionManager::getInstance();

// create initial structure for the transient object cache store
if( ! is_array( $sm->ObjectCache ) ) {
  $sm->ObjectCache = array();
}

// get an instance of the client and configure it
$openid = LightOpenIDClient::getInstance( Config::$server )
            ->withRequired( 'contact/email' )
            ->cacheInSession();

// setup facebook connection
$facebook = new Facebook(array(
  'appId'  => Config::$facebookAppId,
  'secret' => Config::$facebookSecret
));

// process login post
if( isset($_POST['login']) && isset($_POST['pass']) ) {
  $sm->login( $_POST['login'], $_POST['pass'] );
}

// process logout get
if( isset($_GET['action']) && $_GET['action'] == 'logout' ) {
  $sm->logout();
  $openid->logoff();
  // remove session cookie
  unset( $_COOKIE['session'] );
  setcookie( 'session', '', time() - 42000, '/');
  // remove XSFR session
  XSFR::clearSession();
  // redirect to home
  header("Location: home" );
}

function getFederatedUser() {
  global $openid, $facebook;
  if(isset($openid) && $user = $openid->getUser()) {
    return $user->identity;
  }
  if( $user = $facebook->getUser()) {
    return $user;
  }
  return false;
}

function clearFederatedUser() {
  global $openid, $facebook;
  $openid->logoff();
  $openid = null; // FIXME: somehow Yahoo's log-in session is restarted
  $facebook->destroySession();
}

// if we have no current user but have a federated user try to log it on
if( $sm->currentUser->isAnonymous() && $federatedUser = getFederatedUser() ) {
  $sm->login_federated( $federatedUser );
  // if we have no current user now, the federated user is unknown
  if( $sm->currentUser->isAnonymous() ) {
    Messages::getInstance()->addWarning( I18N::$UNKNOWN_FEDERATED_LOGIN );
  } else {
    // clear it: we used it to log in, no longer of any use now.
    clearFederatedUser();
  }
}

// if we still don't have a user, try to restart an existing session
if( $sm->currentUser->isAnonymous()
    && isset( $_COOKIE['session'] ) && $_COOKIE['session'] != '' )
{
  $sm->login_federated( $_COOKIE['session'] );
}

// if we still don't have a user, try to use a one time password
{'fraction_non_alphanumeric': 0.15471409447313061, 'fraction_numerical': 0.002677376171352075, 'mean_word_length': 3.806985294117647, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1246459', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1618, 'n_words': 506}