<commit_before><?php
header('Content-type: text/plain');

require('../config.php');

function append_hmac($msg, $password) {
	$mac = hash_hmac('sha256', $msg, $password);
	return $msg . '&h=' . $mac;
}

function validate_hmac($msg, $msg_mac, $password) {
	$new_mac = hash_hmac('sha256', $msg, $password);
	return $new_mac == $msg_mac;
}

function validate($remote_ip, $ip, $timestamp, $mac) {
	global $SECRET;

	if ($remote_ip != $ip) {
		echo "Error: IP address mismatch, $remote_ip != $ip";
		return false;
	}

	$params = sprintf('type=update&ip=%s&ts=%d', $ip, $timestamp);

	if (!validate_hmac($params, $mac, $SECRET)) {
		echo 'Error: MAC mismatch';
		return false;
	}
	return true;
}

function updatedb($ip, $timestamp) {
	global $DB_FILE;

	$db = new SQLite3($DB_FILE);
	$result = $db->exec('CREATE TABLE IF NOT EXISTS log
		(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
		ip VARCHAR NOT NULL,
		timestamp UNSIGNED INT NOT NULL);');

	$results = $db->query('SELECT max(id) as id, ip, timestamp FROM log;');
	$row = $results->fetchArray();

	if ($ip != $row['ip']) {
		$statement = $db->prepare('INSERT INTO log(ip,timestamp) VALUES (:ip,:timestamp);');
		$statement->bindValue(':ip', $ip);
		$statement->bindValue(':timestamp', $timestamp);
		$statement->execute();
		echo 'OK: New IP updated';

	} elseif ($timestamp > $row['timestamp']) {
		$statement = $db->prepare('UPDATE log SET timestamp=:timestamp WHERE id=:id');
		$statement->bindValue(':timestamp', $timestamp);
		$statement->bindValue(':id', $row['id']);
		$result = $statement->execute();
		echo 'OK: Timestamp updated';

	} else {
		echo 'Error: Old timestamp';
	}
}

$type      = $_POST['type'];
$timestamp = $_POST['ts'];
$remote_ip = $_SERVER['REMOTE_ADDR'];

if ($type == 'ip' ) {
	$params = sprintf('ip=%s&ts=%d', $remote_ip, $timestamp);

	echo append_hmac($params, $SECRET);

} elseif ($type == 'update') {
	$ip  = $_POST['ip'];
	$mac = $_POST['h'];

	if (validate($remote_ip, $ip, $timestamp, $mac))
		updatedb($ip, $timestamp);
}

?>
<commit_msg>Detect IPv4 from IPv6 and use different SQL tables<commit_after><?php
header('Content-type: text/plain');

require('../config.php');

define('IPv4', 1);
define('IPv6', 2);
define('IP_UNKNOWN', 3);

function append_hmac($msg, $password) {
	$mac = hash_hmac('sha256', $msg, $password);
	return $msg . '&h=' . $mac;
}

function validate_hmac($msg, $msg_mac, $password) {
	$new_mac = hash_hmac('sha256', $msg, $password);
	return $new_mac == $msg_mac;
}

function validate($remote_ip, $ip, $timestamp, $mac) {
	global $SECRET;

	if ($remote_ip != $ip) {
		echo "Error: IP address mismatch, $remote_ip != $ip";
		return false;
	}

	$params = sprintf('type=update&ip=%s&ts=%d', $ip, $timestamp);

	if (!validate_hmac($params, $mac, $SECRET)) {
		echo 'Error: MAC mismatch';
		return false;
	}
	return true;
}

function ip_version($ip) {

	# An IPv4 always exactly has 3 dots - A.B.C.D
	if (substr_count($ip, '.') == 3)
		return IPv4;

	# An IPv6 has at least 2 colons (::), altought less than 3 shouldn't happen (A:B::C)
	elseif (strpos($ip, ':') >= 3)
		return IPv6;

	else
		return IP_UNKNOWN;
}

function updatedb($ip, $timestamp) {
	global $DB_FILE;

	switch (ip_version($ip)) {
		case IPv4:
			$table = 'log_ipv4';
			break;
		case IPv6:
			$table = 'log_ipv6';
			break;
		default:
			echo 'Error: Unknown IP address format';
			break;
	}

	$db = new SQLite3($DB_FILE);
	$result = $db->exec('CREATE TABLE IF NOT EXISTS '.$table.'
	                     (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
	                      ip VARCHAR NOT NULL,
	                      timestamp UNSIGNED INT NOT NULL);');


	$results = $db->query('SELECT max(id) as id, ip, timestamp FROM '.$table.';');
	$row = $results->fetchArray();

	if ($ip != $row['ip']) {
		$statement = $db->prepare('INSERT INTO '.$table.'(ip,timestamp) VALUES (:ip,:timestamp);');
		$statement->bindValue(':ip', $ip);
		$statement->bindValue(':timestamp', $timestamp);
		$statement->execute();
		echo 'OK: New IP updated';

	} elseif ($timestamp > $row['timestamp']) {
		$statement = $db->prepare('UPDATE '.$table.' SET timestamp=:timestamp WHERE id=:id');
		$statement->bindValue(':timestamp', $timestamp);
		$statement->bindValue(':id', $row['id']);
		$result = $statement->execute();
		echo 'OK: Timestamp updated';

	} else {
		echo 'Error: Old timestamp';
	}
}

$type      = $_POST['type'];
$timestamp = $_POST['ts'];
$remote_ip = $_SERVER['REMOTE_ADDR'];

if ($type == 'ip' ) {
	$params = sprintf('ip=%s&ts=%d', $remote_ip, $timestamp);

	echo append_hmac($params, $SECRET);

} elseif ($type == 'update') {
	$ip  = $_POST['ip'];
	$mac = $_POST['h'];

	if (validate($remote_ip, $ip, $timestamp, $mac))
		updatedb($ip, $timestamp);
}

?>

{'fraction_non_alphanumeric': 0.20346869712351945, 'fraction_numerical': 0.0071912013536379014, 'mean_word_length': 4.42939150401837, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 30, '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': '7076070', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1705, 'n_words': 425}