<commit_before><?php

require 'config/database.php';
require 'vendor/mikecao/flight/flight/Flight.php';
require '../dbWrapper/dbWrapper.class.php';
require '../userManagement/userManager.class.php';
require 'validation.class.php';

$dbSettings = Array();
// DBName
$dbSettings[] = DBName;
//DBUser
$dbSettings[] = DBUser;
//DBPassword
$dbSettings[] = DBPassword;

Flight::register( 'DB', 'dbWrapper', $dbSettings );
$DB = Flight::DB();
Flight::register( 'userManager', 'userManager', array($DB) );

//////////////////////////////////////////////////////
// Routes
//////////////////////////////////////////////////////

Flight::route('/', function () {

    $userManager = Flight::userManager();

    if ($userManager->getLoginState()) {
    	echo "Yep. It works.";
    } else {
    	Flight::halt(401, "401 Unauthorized - You are not logged in, sports.");
    }
});

Flight::route('GET /login', function () {

	$DB = Flight::DB();
	$userManager = Flight::userManager();

	$headers = apache_request_headers();
	$request = Flight::request();

	if (isset($headers["username"], $headers["password"])) {
		try {
			$userdata = $userManager->login($headers["username"], $headers["password"]);

			if ($userdata) {
				echo json_encode(true);
			} else {
				echo json_encode(false);
			}
		} catch (Exception $e) {
			Flight::halt(401, "401 - User suspended");
		}
	} else {
		Flight::halt(400, "400 - Bad Request");
	}

});

Flight::route('GET /logout', function () {

	$userManager = Flight::userManager();

	$userManager->logout();
	echo "";
});

// Registration
Flight::route('POST /user', function () {

	$userManager = Flight::userManager();

	if (!$userManager->getLoginState()) {
		$request = Flight::request();
		$json = json_decode($request->body, true);
<commit_msg>Add the backend bits for userlists and activating users<commit_after><?php

require 'config/database.php';
require 'vendor/mikecao/flight/flight/Flight.php';
require '../dbWrapper/dbWrapper.class.php';
require '../userManagement/userManager.class.php';
require 'validation.class.php';


$dbSettings = Array();
// DBName
$dbSettings[] = DBName;
//DBUser
$dbSettings[] = DBUser;
//DBPassword
$dbSettings[] = DBPassword;

Flight::register( 'DB', 'dbWrapper', $dbSettings );
$DB = Flight::DB();
Flight::register( 'userManager', 'userManager', array($DB) );

//////////////////////////////////////////////////////
// Routes
//////////////////////////////////////////////////////

Flight::route('/', function () {

    $userManager = Flight::userManager();

    if ($userManager->getLoginState()) {
    	echo "Yep. It works.";
    } else {
    	Flight::halt(401, "401 Unauthorized - You are not logged in, sports.");
    }
});

Flight::route('GET /login', function () {

	$DB = Flight::DB();
	$userManager = Flight::userManager();

	$headers = apache_request_headers();
	$request = Flight::request();

	if (isset($headers["username"], $headers["password"])) {
		try {
			$userdata = $userManager->login($headers["username"], $headers["password"]);

			if ($userdata) {
				echo json_encode(true);
			} else {
				echo json_encode(false);
			}
		} catch (Exception $e) {
			Flight::halt(401, "401 - User suspended");
		}
	} else {
		Flight::halt(400, "400 - Bad Request");
	}

});

Flight::route('GET /logout', function () {

	$userManager = Flight::userManager();

	$userManager->logout();
	echo "";
});

Flight::route('GET /isAdministratorLoggedIn', function() {
	$DB = Flight::DB();
	$userManager = Flight::userManager();
	if ($userManager->getIsLoggedInAsAdministrator())
		echo json_encode(true);
	else
		echo json_encode(false);
});

Flight::route('GET /activeUsers', function() {
	$DB = Flight::DB();
	$userManager = Flight::userManager();
	$allUsers = $userManager->getAllActiveUsers();
	if ($allUsers) {
		echo json_encode($allUsers);
	} else {
		Flight::halt(400, "400 - Bad Request");//TODO: BEtter error code?
	}
});

Flight::route('GET /suspendedUsers', function() {
	$DB = Flight::DB();
	$userManager = Flight::userManager();
	$allUsers = $userManager->getAllSuspendedUsers();
	if ($allUsers) {
		echo json_encode($allUsers);
	} else {
		$allUsers = Array();
		echo json_encode($allUsers);
	}
});

//Activate user
Flight::route('POST /activateUser', function() {
	$DB = Flight::DB();
	$userManager = Flight::userManager();
	if (!$userManager->getIsLoggedInAsAdministrator())
		Flight::halt(401, "401 Unauthorized - You are not logged in as administrator.");
	
	$request = Flight::request();
	$json = json_decode($request->body, true);
	if (isset($json["Username"])) {
		$userManager->activateUser($json["Username"]);
		echo json_encode(true);
	} else {
		echo json_encode(false);
	}
});

// Registration
Flight::route('POST /user', function () {

	$userManager = Flight::userManager();

	if (!$userManager->getLoginState()) {
		$request = Flight::request();
		$json = json_decode($request->body, true);

{'fraction_non_alphanumeric': 0.2329440688383528, 'fraction_numerical': 0.009834050399508297, 'mean_word_length': 3.781586679725759, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '3242992', 'n_tokens_mistral': 1655, 'n_tokens_neox': 1541, 'n_words': 315}