<commit_before><?php
require_once("config.php");
require_once("src/autoload.php");
require_once("vendor/autoload.php");

use BlueHerons\StatTracker\Agent;
use BlueHerons\StatTracker\OCR;
use BlueHerons\StatTracker\StatTracker;

use Curl\Curl;
use Endroid\QrCode\QrCode;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\HttpKernelInterface;

$StatTracker = new StatTracker();

// Assert that token and stat parameters, if present, match expected format
$validateRequest = function(Request $request, Silex\Application $StatTracker) {
	function validateParameter($param, $regex) {
		if (strlen($param) > 0) {
			return preg_match($regex, $param) === 1;
		}
		else {
			return true;
		}
	}

	// Ensure {token} is 6 hexidecimal digits
	if (!validateParameter($request->get("token"), "/^[a-f0-9]{64}$/i")) { return $StatTracker->abort(400); }
	// Ensure {stat} is alpha characters and an underscore
	if (!validateParameter($request->get("stat"), "/^[a-z_]+$/")) { return $StatTracker->abort(400); }
};

$StatTracker->get("/api/{token}/profile/{when}.{format}", function($token, $when, $format) use ($StatTracker) {
	$agent = Agent::lookupAgentByToken($token);

	$response = new stdClass;

	$response->agent = $agent->name;
	
	$t = new stdClass;

	if ($StatTracker->isValidDate($when)) {
		$ts = $agent->getUpdateTimestamp($when, true);

		if ($ts == null) {
			return $StatTracker->abort(403);
		}
		else {
			$response->date = date("c", $ts);
			$response->badges = $agent->getBadges($when, true);
			$response->stats = $agent->getStats($when, true);
		}
	}
	else if ($when == "latest") {
		$response->date = date("c", $agent->getUpdateTimestamp());
		$response->badges = $agent->getBadges();
		$response->stats = $agent->getStats("latest", true);
	}
	else {
		return $StatTracker->abort(403);
	}

	switch ($format) {
		case "json":
			return $StatTracker->json($response);
			break;
	}
})->before($validateRequest)
  ->assert("format", "json")
  ->assert("when",   "latest|[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}")
  ->value ("format", "json")
  ->value ("when",   "latest");

// Retrieve basic information about the agent
$StatTracker->get("/api/{token}", function($token) use ($StatTracker) {
	$agent = Agent::lookupAgentByToken($token);

	if (!$agent->isValid()) {
		return $StatTracker->abort(403);
	}

	return $StatTracker->json($agent);
})->before($validateRequest);

$StatTracker->match("/api/{token}/token", function(Request $request, $token) use ($StatTracker) {
    $agent = Agent::lookupAgentByToken($token);

    if (!$agent->isValid()) {
        return $StatTracker->abort(403);
    }

    switch ($request->getMethod()) {
        case "GET":
            $name = strtoupper(substr(str_shuffle(md5(time() . $token . rand())), 0, 6));
            $token = $agent->createToken($name);

            $url = sprintf("%s://%s", $request->getScheme(), $request->getHost());
            $url = $url . $request->getBaseUrl();

            $uri = "stattracker://token?token=%s&name=%s&agent=%s&issuer=%s";
            $uri = sprintf($uri, $token, $name, $agent->name, $url);

            $qr = new QRCode();
            $qr->setText($uri)
               ->setSize(200)
               ->setPadding(10);

            if ($token === false) {
                return new Response(null, 202);
            }
            else {
                $data = array(
                    "name" => $name,
                    "token" => $token,
                    "qr" => $qr->getDataUri(),
                    "uri" => $uri
                );
                return $StatTracker->json($data);
            }
            break;
        case "DELETE":
            if (!$request->request->has("name")) {
                return $StatTracker->abort(400);
            }

            $name = strtoupper($request->request->get("name"));
            $r = $agent->revokeToken($name);
            if ($r === true) {
                return new Response(null, 200);<commit_msg>Encode URL<commit_after><?php
require_once("config.php");
require_once("src/autoload.php");
require_once("vendor/autoload.php");

use BlueHerons\StatTracker\Agent;
use BlueHerons\StatTracker\OCR;
use BlueHerons\StatTracker\StatTracker;

use Curl\Curl;
use Endroid\QrCode\QrCode;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\HttpKernelInterface;

$StatTracker = new StatTracker();

// Assert that token and stat parameters, if present, match expected format
$validateRequest = function(Request $request, Silex\Application $StatTracker) {
	function validateParameter($param, $regex) {
		if (strlen($param) > 0) {
			return preg_match($regex, $param) === 1;
		}
		else {
			return true;
		}
	}

	// Ensure {token} is 6 hexidecimal digits
	if (!validateParameter($request->get("token"), "/^[a-f0-9]{64}$/i")) { return $StatTracker->abort(400); }
	// Ensure {stat} is alpha characters and an underscore
	if (!validateParameter($request->get("stat"), "/^[a-z_]+$/")) { return $StatTracker->abort(400); }
};

$StatTracker->get("/api/{token}/profile/{when}.{format}", function($token, $when, $format) use ($StatTracker) {
	$agent = Agent::lookupAgentByToken($token);

	$response = new stdClass;

	$response->agent = $agent->name;
	
	$t = new stdClass;

	if ($StatTracker->isValidDate($when)) {
		$ts = $agent->getUpdateTimestamp($when, true);

		if ($ts == null) {
			return $StatTracker->abort(403);
		}
		else {
			$response->date = date("c", $ts);
			$response->badges = $agent->getBadges($when, true);
			$response->stats = $agent->getStats($when, true);
		}
	}
	else if ($when == "latest") {
		$response->date = date("c", $agent->getUpdateTimestamp());
		$response->badges = $agent->getBadges();
		$response->stats = $agent->getStats("latest", true);
	}
	else {
		return $StatTracker->abort(403);
	}

	switch ($format) {
		case "json":
			return $StatTracker->json($response);
			break;
	}
})->before($validateRequest)
  ->assert("format", "json")
  ->assert("when",   "latest|[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}")
  ->value ("format", "json")
  ->value ("when",   "latest");

// Retrieve basic information about the agent
$StatTracker->get("/api/{token}", function($token) use ($StatTracker) {
	$agent = Agent::lookupAgentByToken($token);

	if (!$agent->isValid()) {
		return $StatTracker->abort(403);
	}

	return $StatTracker->json($agent);
})->before($validateRequest);

$StatTracker->match("/api/{token}/token", function(Request $request, $token) use ($StatTracker) {
    $agent = Agent::lookupAgentByToken($token);

    if (!$agent->isValid()) {
        return $StatTracker->abort(403);
    }

    switch ($request->getMethod()) {
        case "GET":
            $name = strtoupper(substr(str_shuffle(md5(time() . $token . rand())), 0, 6));
            $token = $agent->createToken($name);

            $url = sprintf("%s://%s", $request->getScheme(), $request->getHost());
            $url = $url . $request->getBaseUrl();

            $uri = "stattracker://token?token=%s&name=%s&agent=%s&issuer=%s";
            $uri = sprintf($uri, $token, $name, $agent->name, urlencode($url));

            $qr = new QRCode();
            $qr->setText($uri)
               ->setSize(200)
               ->setPadding(10);

            if ($token === false) {
                return new Response(null, 202);
            }
            else {
                $data = array(
                    "name" => $name,
                    "token" => $token,
                    "qr" => $qr->getDataUri(),
                    "uri" => $uri
                );
                return $StatTracker->json($data);
            }
            break;
        case "DELETE":
            if (!$request->request->has("name")) {
                return $StatTracker->abort(400);
            }

            $name = strtoupper($request->request->get("name"));
            $r = $agent->revokeToken($name);
            if ($r === true) {
                return new Response(null, 200);
{'fraction_non_alphanumeric': 0.19071024821823543, 'fraction_numerical': 0.013025313344802163, 'mean_word_length': 2.9780058651026393, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 119, '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': '147902', 'n_tokens_mistral': 2704, 'n_tokens_neox': 2553, 'n_words': 498}