<commit_before><?php

define('DEVPANEL_TOKEN_LEN', 50);
define('DEVPANEL_DIR', "/opt/webenabled");

function dp_has_valid_token($vhost, $app, $token_str) {
  $ret = false;
  $token_file = sprintf("%s/var/tokens/%s.%s.%s", DEVPANEL_DIR, $vhost, $app, $token_str);

  /* open file for read/write, fail if doesn't exist */
  $token_fd = fopen($token_file, 'r');
  if (!$token_fd) {
    return false;
  }

  $ret =
    /* try to acquire exclusive lock, fail otherwise */
    flock($token_fd, LOCK_EX | LOCK_NB) &&
    /* mtime == 0 means the token has been already used */
    (filemtime($token_file) != 0) &&
    /* touch the file to mark it as used */
    touch($token_file, 0);

  /* closing the file releases the lock */
  fclose($token_fd);
  return $ret;
}

function dp_is_already_logged_to_app($app) {
  ini_get('session.auto_start') || session_start();

  if(!empty($_SESSION) && !empty($_SESSION['app']) && $_SESSION['app'] == $app) {
    return true;
  } else {
    return false;
  }
}

function dp_derive_gen_vhost() {
  $user_info = posix_getpwuid(posix_geteuid());

  if(strlen($user_info['name']) > 2 && substr($user_info['name'], 0, 2) == "w_") {
    $vhost = substr($user_info['name'], 2);
  } else {
    $vhost = $user_info['name'];
  }

  return $vhost;
}

function dp_get_app_token_from_url() {
  if(empty($_SERVER['PATH_INFO'])) {
    return false;
  }

  $token = trim($_SERVER['PATH_INFO'], "/");

  if(strlen($token) == DEVPANEL_TOKEN_LEN && preg_match('/^[A-Za-z0-9]+$/', $token)) {
    return $token;
  } else {
    return false;
  }
}

function dp_get_token_from_params() {
  if(!isset($_GET['token'])) {
    return false;
  }

  $token = $_GET['token'];

  if(strlen($token) == DEVPANEL_TOKEN_LEN && preg_match('/^[A-Za-z0-9]+$/', $token)) {
    return $token;
  } else {
    return false;
  }
}

function dp_start_app_session($vhost, $app, $token) {
  session_start();

  session_cache_expire(120);
  session_cache_limiter("private");
  $_SESSION["app"]   = $app;
  $_SESSION["token"] = $token;

  session_write_close();
}

?><commit_msg>apps token validation: fixed case when web app flushes all sessions<commit_after><?php

define('DEVPANEL_TOKEN_LEN', 50);
define('DEVPANEL_DIR', "/opt/webenabled");

function dp_has_valid_token($vhost, $app, $token_str) {
  $ret = false;
  $token_file = sprintf("%s/var/tokens/%s.%s.%s", DEVPANEL_DIR, $vhost, $app, $token_str);

  /* open file for read/write, fail if doesn't exist */
  $token_fd = fopen($token_file, 'r');
  if (!$token_fd) {
    return false;
  }

  $ret =
    /* try to acquire exclusive lock, fail otherwise */
    flock($token_fd, LOCK_EX | LOCK_NB) &&
    /* mtime == 0 means the token has been already used */
    (filemtime($token_file) != 0) &&
    /* touch the file to mark it as used */
    touch($token_file, 0);

  /* closing the file releases the lock */
  fclose($token_fd);
  return $ret;
}

function dp_is_already_logged_to_app($app) {
  if(session_status() == PHP_SESSION_ACTIVE) {
    // there's an existing session
    if(session_name != "devpanel_$app") {
      $previous_session_name = session_name();
      session_write_close();
      if(!empty($_COOKIE["devpanel_$app"])) {
        session_id($_COOKIE["devpanel_$app"]);
      } else {
        return false;
      }
      session_name("devpanel_$app");
      session_start();
    }
  } else {
    // no session available
    if(!empty($_COOKIE["devpanel_$app"])) {
      session_id($_COOKIE["devpanel_$app"]);
    } else {
      return false;
    }
    session_name("devpanel_$app");
    session_start();
  }

  if(!empty($_SESSION) && !empty($_SESSION['app']) && $_SESSION['app'] == $app) {
    session_write_close();

    // if there was another session before, then re-open it
    if(!empty($previous_session_name)) {
      session_name($previous_session_name);
      session_start();
    }

    return true;
  } else {
    return false;
  }
}

function dp_derive_gen_vhost() {
  $user_info = posix_getpwuid(posix_geteuid());

  if(strlen($user_info['name']) > 2 && substr($user_info['name'], 0, 2) == "w_") {
    $vhost = substr($user_info['name'], 2);
  } else {
    $vhost = $user_info['name'];
  }

  return $vhost;
}

function dp_get_app_token_from_url() {
  if(empty($_SERVER['PATH_INFO'])) {
    return false;
  }

  $token = trim($_SERVER['PATH_INFO'], "/");

  if(strlen($token) == DEVPANEL_TOKEN_LEN && preg_match('/^[A-Za-z0-9]+$/', $token)) {
    return $token;
  } else {
    return false;
  }
}

function dp_get_token_from_params() {
  if(!isset($_GET['token'])) {
    return false;
  }

  $token = $_GET['token'];

  if(strlen($token) == DEVPANEL_TOKEN_LEN && preg_match('/^[A-Za-z0-9]+$/', $token)) {
    return $token;
  } else {
    return false;
  }
}

function dp_start_app_session($vhost, $app, $token) {
  if(session_status() != PHP_SESSION_ACTIVE) {
    session_start();
  }

  session_cache_expire(120);
  session_cache_limiter("private");
  $_SESSION["app"]   = $app;
  $_SESSION["token"] = $token;

  session_write_close();
}

?>
{'fraction_non_alphanumeric': 0.18172750847795732, 'fraction_numerical': 0.006383403151805306, 'mean_word_length': 3.7436140018921478, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '955666', 'n_tokens_mistral': 1896, 'n_tokens_neox': 1845, 'n_words': 348}