<commit_before><?php
require_once("config.php");
require_once("code/autoload.php");
require_once("code/StatTracker.class.php");
require_once("code/Agent.class.php");
require_once("vendor/autoload.php");

use BlueHerons\StatTracker\AuthenticationProvider;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

$db = new PDO(sprintf("mysql:host=%s;dbname=%s;charset=utf8", DB_HOST, DB_NAME), DB_USER, DB_PASS, array(
	PDO::ATTR_EMULATE_PREPARES   => false,
	PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
	PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
));

$app = new Silex\Application();
$app['debug'] = true;
$app->register(new Silex\Provider\SessionServiceProvider());
$app->register(new Silex\Provider\TwigServiceProvider(), array(
	'twig.path' => array(
		__DIR__ . "/views",
		__DIR__ . "/resources",
		__DIR__ . "/resources/scripts",
	)
));

$agent = new Agent();
if ($app['session']->get("agent") !== null) {
	$agent = $app['session']->get("agent");
}

// Default handler. Will match any alphnumeric string. If the page doesn't exist,
// 404
$app->get('/{page}', function ($page) use ($app) {
	if ($page == "dashboard" ||
	    $page == "submit-stats" ||
	    $page == "leaderboards") {
		$app['session']->set("page_after_login", $page);
		return $app['twig']->render("index.twig", array(
			"constants" => array(
				"ga_id" => GOOGLE_ANALYTICS_ID,
				"version" => empty(VERSION) ? "development" : VERSION,
			),
			"page" => $page
		));
	}
	else if ($page == "terms-of-use") {
		return $app['twig']->render("terms.html");
	}
	else if ($page == "authenticate") {
		switch ($_REQUEST['action']) {
			case "login":
				return $app->json(AuthenticationProvider::getInstance()->login());
				break;
			case "callback":
				AuthenticationProvider::getInstance()->callback();
				$page = $app['session']->get("page_after_login");
				$page = empty($page) ? "dashboard" : $page;
				return $app->redirect("./{$page}");
				break;
			case "logout":
				return $app->json(\BlueHerons\StatTracker\AuthenticationProvider::getInstance()->logout());
				break;
			default:
				$app->abort(405, "Invalid Authentication action");
		}
	}
	else {
		$app->abort(404);
	}
})->assert('page', '[a-z-]+')
  ->value('page', 'dashboard');

$app->get('/page/{page}', function(Request $request, $page) use ($app, $agent) {
	$page_parameters = array();
	
	if ($page == "submit-stats") {
		$date = $request->get("date");
		if ($date == null || new DateTime() < new DateTime($date)) {
			$agent->getStats("latest", true);
			$date = date("Y-m-d");
		}
		else {
			$agent->getStats($date, true);
		}

		$page_parameters['date'] = $date;
		$page_parameters['today'] = $date == date("Y-m-d");
	}
	else {
		$agent->getStats("latest", true);
	}

	return $app['twig']->render($page.".twig", array(
		"agent" => $agent,
		"constants" => array("email_submission" => EMAIL_SUBMISSION),
		"faction_class" => $agent->faction == "R" ? "resistance-agent" : "enlightened-agent",
		"faction_color" => $agent->faction == "R" ? RES_BLUE : ENL_GREEN,
		"parameters" => $page_parameters,
		"stats" => StatTracker::getStats(),
	));
});

$app->get("/resources/{resource_dir}/{resource}", function(Request $request, $resource) use ($app) {
	switch ($resource) {
		case "style.css":<commit_msg>Removing unused page parameter<commit_after><?php
require_once("config.php");
require_once("code/autoload.php");
require_once("code/StatTracker.class.php");
require_once("code/Agent.class.php");
require_once("vendor/autoload.php");

use BlueHerons\StatTracker\AuthenticationProvider;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

$db = new PDO(sprintf("mysql:host=%s;dbname=%s;charset=utf8", DB_HOST, DB_NAME), DB_USER, DB_PASS, array(
	PDO::ATTR_EMULATE_PREPARES   => false,
	PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
	PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
));

$app = new Silex\Application();
$app['debug'] = true;
$app->register(new Silex\Provider\SessionServiceProvider());
$app->register(new Silex\Provider\TwigServiceProvider(), array(
	'twig.path' => array(
		__DIR__ . "/views",
		__DIR__ . "/resources",
		__DIR__ . "/resources/scripts",
	)
));

$agent = new Agent();
if ($app['session']->get("agent") !== null) {
	$agent = $app['session']->get("agent");
}

// Default handler. Will match any alphnumeric string. If the page doesn't exist,
// 404
$app->get('/{page}', function ($page) use ($app) {
	if ($page == "dashboard" ||
	    $page == "submit-stats" ||
	    $page == "leaderboards") {
		$app['session']->set("page_after_login", $page);
		return $app['twig']->render("index.twig", array(
			"constants" => array(
				"ga_id" => GOOGLE_ANALYTICS_ID,
				"version" => empty(VERSION) ? "development" : VERSION,
			),
			"page" => $page
		));
	}
	else if ($page == "terms-of-use") {
		return $app['twig']->render("terms.html");
	}
	else if ($page == "authenticate") {
		switch ($_REQUEST['action']) {
			case "login":
				return $app->json(AuthenticationProvider::getInstance()->login());
				break;
			case "callback":
				AuthenticationProvider::getInstance()->callback();
				$page = $app['session']->get("page_after_login");
				$page = empty($page) ? "dashboard" : $page;
				return $app->redirect("./{$page}");
				break;
			case "logout":
				return $app->json(\BlueHerons\StatTracker\AuthenticationProvider::getInstance()->logout());
				break;
			default:
				$app->abort(405, "Invalid Authentication action");
		}
	}
	else {
		$app->abort(404);
	}
})->assert('page', '[a-z-]+')
  ->value('page', 'dashboard');

$app->get('/page/{page}', function(Request $request, $page) use ($app, $agent) {
	$page_parameters = array();
	
	if ($page == "submit-stats") {
		$date = $request->get("date");
		if ($date == null || new DateTime() < new DateTime($date)) {
			$agent->getStats("latest", true);
			$date = date("Y-m-d");
		}
		else {
			$agent->getStats($date, true);
		}

		$page_parameters['date'] = $date;
	}
	else {
		$agent->getStats("latest", true);
	}

	return $app['twig']->render($page.".twig", array(
		"agent" => $agent,
		"constants" => array("email_submission" => EMAIL_SUBMISSION),
		"faction_class" => $agent->faction == "R" ? "resistance-agent" : "enlightened-agent",
		"faction_color" => $agent->faction == "R" ? RES_BLUE : ENL_GREEN,
		"parameters" => $page_parameters,
		"stats" => StatTracker::getStats(),
	));
});

$app->get("/resources/{resource_dir}/{resource}", function(Request $request, $resource) use ($app) {
	switch ($resource) {
		case "style.css":
{'fraction_non_alphanumeric': 0.215623572407492, 'fraction_numerical': 0.0030455306837216386, 'mean_word_length': 4.7766051011433595, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 89, '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': '1710870', 'n_tokens_mistral': 2494, 'n_tokens_neox': 2301, 'n_words': 409}