<commit_before><?php

include_once 'container/users.php';

$app->get('/auth', function () use ($app, $req, $res) {
  // check if a user session already exists
  session_start();
  if (isset($_SESSION['userdata']) and isset($_SESSION['userdata']['id'])) {
    
    // if user has been timeouted (network problem)
    $uid = $_SESSION['userdata']['id'];
    $uid = Container_users::generateUid($uid); // needed to recreate directories
    if (!Container_users::checkUserExists($uid)) {
      Container_users::setUserData($uid, $_SESSION['userdata']);
    }
    Container_users::setIsAlive($uid);
    
    $res->status(200); // , 'User authenticated'
    $res['Content-Type'] = 'application/json; charset=utf-8';
    $res->body(json_encode($_SESSION['userdata']));
    return;
  } 

  // apply the user defined auth hook
  $app->applyHook('pfc.before.auth', $hr = new stdClass);
  $hr->login = isset($hr->login) ? $hr->login : '';
  // TODO: check if $hr->login is defined and use it to overload default login step
  
  // check if a login/password has been set
  // allow Pfc-Authorization header because Authorization can be disabled by reverse proxy
  $auth = $req->headers('Authorization') ?
    $req->headers('Authorization') :
    ($req->headers('Pfc-Authorization') ? $req->headers('Pfc-Authorization') : '');
  if (!$auth and $hr->login == '') {
    $res->status(403);
    $res['Content-Type'] = 'application/json; charset=utf-8';
    $res['Pfc-WWW-Authenticate'] = 'Basic realm="Authentication"';
    $res->body('{ "error": "Need authentication" }');
    return;
  }

  if ($auth) {
    // decode basic http auth header
    $auth = @explode(':', @base64_decode(@array_pop(@explode(' ', $auth))));
    if (!isset($auth[0]) && !$auth[0]) {
      $res->status(400);
      $res['Content-Type'] = 'application/json; charset=utf-8';
      $res->body('{ "error": "Login is missing" }');
      return;
    }
    $login    = trim($auth[0]);
    $password = isset($auth[1]) ? $auth[1] : '';
  } else {
    // or get the login from the before.auth hook
    $login = $hr->login;
  }
  
  // check login/password<commit_msg>handle redirections on the before.auth hook<commit_after><?php

include_once 'container/users.php';

$app->get('/auth', function () use ($app, $req, $res) {
  // check if a user session already exists
  session_start();
  if (isset($_SESSION['userdata']) and isset($_SESSION['userdata']['id'])) {
    
    // if user has been timeouted (network problem)
    $uid = $_SESSION['userdata']['id'];
    $uid = Container_users::generateUid($uid); // needed to recreate directories
    if (!Container_users::checkUserExists($uid)) {
      Container_users::setUserData($uid, $_SESSION['userdata']);
    }
    Container_users::setIsAlive($uid);
    
    $res->status(200); // , 'User authenticated'
    $res['Content-Type'] = 'application/json; charset=utf-8';
    $res->body(json_encode($_SESSION['userdata']));
    return;
  } 

  // apply the user defined auth hook
  $app->applyHook('pfc.before.auth', $hr = new stdClass);
  $hr->login = isset($hr->login) ? $hr->login : '';
  
  // check if the hook want to redirect the response
  if ($res->isRedirection()) {
    return;
  }
  
  // check if a login/password has been set
  // allow Pfc-Authorization header because Authorization can be disabled by reverse proxy
  $auth = $req->headers('Authorization') ?
    $req->headers('Authorization') :
    ($req->headers('Pfc-Authorization') ? $req->headers('Pfc-Authorization') : '');
  if (!$auth and $hr->login == '') {
    $res->status(403);
    $res['Content-Type'] = 'application/json; charset=utf-8';
    $res['Pfc-WWW-Authenticate'] = 'Basic realm="Authentication"';
    $res->body('{ "error": "Need authentication" }');
    return;
  }

  if ($auth) {
    // decode basic http auth header
    $auth = @explode(':', @base64_decode(@array_pop(@explode(' ', $auth))));
    if (!isset($auth[0]) && !$auth[0]) {
      $res->status(400);
      $res['Content-Type'] = 'application/json; charset=utf-8';
      $res->body('{ "error": "Login is missing" }');
      return;
    }
    $login    = trim($auth[0]);
    $password = isset($auth[1]) ? $auth[1] : '';
  } else {
    // or get the login from the before.auth hook
    $login = $hr->login;
  }
  
  // check login/password
{'fraction_non_alphanumeric': 0.19542376838664488, 'fraction_numerical': 0.00887228578099463, 'mean_word_length': 3.5477707006369426, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6502368', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1383, 'n_words': 316}