<commit_before><?php

/*

License: GPLv2 or later
  <http://www.gnu.org/licenses/gpl-2.0.html>

*/

error_reporting(E_ALL);

include_once '../pokemonshowdown.com/lib/ntbb-session.lib.php';
//include_once 'lib/ntbb-ladder.lib.php';
include_once '../pokemonshowdown.com/config/servers.inc.php';

function getServerToken($cookiepath) {
	global $PokemonServers;
	if (!preg_match('/\/~~([^:]*)(:[0-9]*)?/', $cookiepath, $matches)) {
		return NULL;
	}
	$serverid = $matches[1];
	$server = @$PokemonServers[$serverid];
	return $server ? $server['server'] : $serverid;
}

$reqs = array($_REQUEST);
$multiReqs = false;
if (@$_REQUEST['json']) {
	$reqs = json_decode($_REQUEST['json'], true);
	$multiReqs = true;
}

$outPrefix = ']'; // JSON output should not be valid JavaScript
$outArray = array();

foreach ($reqs as $reqData) {

	$reqData = array_merge($_REQUEST, $reqData);
	if (!ctype_alnum(@$reqData['act'])) die('{"error":"invalid action"}');
	$out = array(
		'action' => @$reqData['act']
	);

	// if ($_REQUEST['debug']) {
	// 	var_export($reqData);
	// }

	switch (@$reqData['act'])
	{
	case 'login':
		if (!$_POST) die();
		$users->login($reqData['name'], $reqData['pass']);
		unset($curuser['userdata']);
		$out['curuser'] = $curuser;
		$out['actionsuccess'] = !!$curuser;
		$servertoken = getServerToken($users->getCookiePath());
		if (!$servertoken) {
			die('Bogus request.');
		}
		if ($curuser && $servertoken)
		{
			$out['sessiontoken'] = $users->getSessionToken($servertoken) . '::' . $servertoken;
		}
		$challengekeyid = !isset($reqData['challengekeyid']) ? -1 : intval($reqData['challengekeyid']);
		$challenge = !isset($reqData['challenge']) ? '' : $reqData['challenge'];
		$out['assertion'] = $users->getAssertion($curuser['userid'], $servertoken, null,
			$challengekeyid, $challenge);
		break;
	case 'register':
		$servertoken = getServerToken($users->getCookiePath());
		if (!$servertoken) {
			die('Bogus request.');
		}
		$user = array();
		$user['username'] = @$_POST['username'];
		$userid = $users->userid($user['username']);
		if (strlen($userid) < 1)
		{
			$out['actionerror'] = 'Your username must contain at least one letter or number.';
		}
		else if (substr($userid, 0, 5) === 'guest')
		{
			$out['actionerror'] = 'Your username cannot start with \'guest\'.';
		}
		else if (strlen($user['username']) > 64)
		{
			$out['actionerror'] = 'Your username must be less than 64 characters long.';
		}
		else if (strlen(@$_POST['password']) < 5)
		{
			$out['actionerror'] = 'Your password must be at least 5 characters long.';
		}
		else if (@$_POST['password'] !== @$_POST['cpassword'])
		{
			$out['actionerror'] = 'Your passwords do not match.';
		}
		else if (trim(strtolower(@$_POST['captcha'])) !== 'pikachu')
		{
			$out['actionerror'] = 'Please answer the anti-spam question given.';
		}
		else if ($user = $users->addUser($user, $_POST['password']))
		{
			$challengekeyid = !isset($reqData['challengekeyid']) ? -1 : intval($reqData['challengekeyid']);
			$challenge = !isset($reqData['challenge']) ? '' : $reqData['challenge'];
			$out['curuser'] = $user;
			$out['assertion'] = $users->getAssertion($user['userid'],
					$servertoken, $user, $challengekeyid, $challenge);
			$out['actionsuccess'] = true;
			if ($curuser && $servertoken)
			{
				$out['sessiontoken'] = $users->getSessionToken($servertoken) . '::' . $servertoken;
			}
		}
		else
		{
			$out['actionerror'] = 'Your username is already taken.';
		}
		break;
	case 'upkeep':
		unset($curuser['userdata']);
		$out['curuser'] = $curuser;
		if (empty($reqData['name'])) $userid = $curuser['userid'];
		else $userid = $users->userid($reqData['name']);

		$servertoken = getServerToken($users->getCookiePath());
		if (!$servertoken) {
			die('Bogus request.'); // Will not happen with official client.
		}
		// The `upkeep` action does not support challenge-response authentication.
		$out['assertion'] = $users->getAssertion($userid, $servertoken);
		break;
	case 'checklogin':
		// direct
		header('Content-type: text/plain');
		die(!!$users->getUser($reqData['userid']));
		break;
	case 'logout':
		if (!$_POST) die();<commit_msg>Remove obsolete `upkeep` action from action.php<commit_after><?php

/*

License: GPLv2 or later
  <http://www.gnu.org/licenses/gpl-2.0.html>

*/

error_reporting(E_ALL);

include_once '../pokemonshowdown.com/lib/ntbb-session.lib.php';
//include_once 'lib/ntbb-ladder.lib.php';
include_once '../pokemonshowdown.com/config/servers.inc.php';

function getServerToken($cookiepath) {
	global $PokemonServers;
	if (!preg_match('/\/~~([^:]*)(:[0-9]*)?/', $cookiepath, $matches)) {
		return NULL;
	}
	$serverid = $matches[1];
	$server = @$PokemonServers[$serverid];
	return $server ? $server['server'] : $serverid;
}

$reqs = array($_REQUEST);
$multiReqs = false;
if (@$_REQUEST['json']) {
	$reqs = json_decode($_REQUEST['json'], true);
	$multiReqs = true;
}

$outPrefix = ']'; // JSON output should not be valid JavaScript
$outArray = array();

foreach ($reqs as $reqData) {

	$reqData = array_merge($_REQUEST, $reqData);
	if (!ctype_alnum(@$reqData['act'])) die('{"error":"invalid action"}');
	$out = array(
		'action' => @$reqData['act']
	);

	// if ($_REQUEST['debug']) {
	// 	var_export($reqData);
	// }

	switch (@$reqData['act'])
	{
	case 'login':
		if (!$_POST) die();
		$users->login($reqData['name'], $reqData['pass']);
		unset($curuser['userdata']);
		$out['curuser'] = $curuser;
		$out['actionsuccess'] = !!$curuser;
		$servertoken = getServerToken($users->getCookiePath());
		if (!$servertoken) {
			die('Bogus request.');
		}
		if ($curuser && $servertoken)
		{
			$out['sessiontoken'] = $users->getSessionToken($servertoken) . '::' . $servertoken;
		}
		$challengekeyid = !isset($reqData['challengekeyid']) ? -1 : intval($reqData['challengekeyid']);
		$challenge = !isset($reqData['challenge']) ? '' : $reqData['challenge'];
		$out['assertion'] = $users->getAssertion($curuser['userid'], $servertoken, null,
			$challengekeyid, $challenge);
		break;
	case 'register':
		$servertoken = getServerToken($users->getCookiePath());
		if (!$servertoken) {
			die('Bogus request.');
		}
		$user = array();
		$user['username'] = @$_POST['username'];
		$userid = $users->userid($user['username']);
		if (strlen($userid) < 1)
		{
			$out['actionerror'] = 'Your username must contain at least one letter or number.';
		}
		else if (substr($userid, 0, 5) === 'guest')
		{
			$out['actionerror'] = 'Your username cannot start with \'guest\'.';
		}
		else if (strlen($user['username']) > 64)
		{
			$out['actionerror'] = 'Your username must be less than 64 characters long.';
		}
		else if (strlen(@$_POST['password']) < 5)
		{
			$out['actionerror'] = 'Your password must be at least 5 characters long.';
		}
		else if (@$_POST['password'] !== @$_POST['cpassword'])
		{
			$out['actionerror'] = 'Your passwords do not match.';
		}
		else if (trim(strtolower(@$_POST['captcha'])) !== 'pikachu')
		{
			$out['actionerror'] = 'Please answer the anti-spam question given.';
		}
		else if ($user = $users->addUser($user, $_POST['password']))
		{
			$challengekeyid = !isset($reqData['challengekeyid']) ? -1 : intval($reqData['challengekeyid']);
			$challenge = !isset($reqData['challenge']) ? '' : $reqData['challenge'];
			$out['curuser'] = $user;
			$out['assertion'] = $users->getAssertion($user['userid'],
					$servertoken, $user, $challengekeyid, $challenge);
			$out['actionsuccess'] = true;
			if ($curuser && $servertoken)
			{
				$out['sessiontoken'] = $users->getSessionToken($servertoken) . '::' . $servertoken;
			}
		}
		else
		{
			$out['actionerror'] = 'Your username is already taken.';
		}
		break;
	case 'checklogin':
		// direct
		header('Content-type: text/plain');
		die(!!$users->getUser($reqData['userid']));
		break;
	case 'logout':
		if (!$_POST) die();
{'fraction_non_alphanumeric': 0.2184173400025651, 'fraction_numerical': 0.004360651532640759, 'mean_word_length': 4.708638360175695, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1392171', 'n_tokens_mistral': 3081, 'n_tokens_neox': 2792, 'n_words': 538}