<commit_before><?php
header("Content-Type: text/plain; charset=utf-8");

const DB_PATH = "/home/grawity/lib/cgi-data/rwho.db";

function ut_insert($db, $host, $entry) {
	list ($user, $rhost, $line, $time) = $entry;
	$st = $db->prepare('
		INSERT INTO `utmp` (host, user, rhost, line, time, updated)
		VALUES (:host, :user, :rhost, :line, :time, :updated)');
	$st->bindValue(":host", $host);
	$st->bindValue(":user", $user);
	$st->bindValue(":rhost", $rhost);
	$st->bindValue(":line", $line);
	$st->bindValue(":time", $time);
	$st->bindValue(":updated", time());
	return $st->execute();
}

function ut_delete($db, $host, $entry) {
	list ($user, $rhost, $line, $time) = $entry;
	$st = $db->prepare('DELETE FROM `utmp` WHERE host=:host AND user=:user AND line=:line');
	$st->bindValue(":host", $host);
	$st->bindValue(":user", $user);
	$st->bindValue(":line", $line);
	$st->bindValue(":time", $time);
	return $st->execute();
}

function ut_delete_host($db, $host) {
	$st = $db->prepare('DELETE FROM utmp WHERE host=:host');
	$st->bindValue(":host", $host);
	return $st->execute();
}

$actions = array(
	"query" => function() {
		$db = new SQLite3(DB_PATH, SQLITE3_OPEN_READONLY);

		$res = $db->query('select * from `utmp`');

		$utmp = array();
		while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
			$utmp[] = $row;
		}
		print json_encode($utmp);
	},

	"insert" => function() {
		$host = $_POST["host"];
		if (!strlen($host)) {
			print "error: host not specified\n";
			return false;
		}
		$data = json_decode($_POST["utmp"]);
		if (!$data) {
			print "error: no data\n";
			return false;
		}

		$db = new SQLite3(DB_PATH);
		foreach ($data as $entry)
			ut_insert($db, $host, $entry);
		print "OK\n";
	},

	"delete" => function() {
		$host = $_POST["host"];
		if (!strlen($host)) {
			print "error: host not specified\n";
			return false;
		}
		$data = json_decode($_POST["utmp"]);
		if (!$data) {
			print "error: no data\n";
			return false;
		}

		$db = new SQLite3(DB_PATH);
		foreach ($data as $entry)
			ut_delete($db, $host, $entry);
		print "OK\n";
	},

	"put" => function() {
		$host = $_POST["host"];
		if (!strlen($host)) {
			print "error: host not specified\n";
			return false;
		}
		$data = json_decode($_POST["utmp"]);
		if (!$data) {
			print "error: no data\n";
			return false;
		}

		$db = new SQLite3(DB_PATH);
		ut_delete_host($db, $host);
		foreach ($data as $entry)<commit_msg>Fix issue with action=put and empty utmp<commit_after><?php
header("Content-Type: text/plain; charset=utf-8");

const DB_PATH = "/home/grawity/lib/cgi-data/rwho.db";

function ut_insert($db, $host, $entry) {
	list ($user, $rhost, $line, $time) = $entry;
	$st = $db->prepare('
		INSERT INTO `utmp` (host, user, rhost, line, time, updated)
		VALUES (:host, :user, :rhost, :line, :time, :updated)');
	$st->bindValue(":host", $host);
	$st->bindValue(":user", $user);
	$st->bindValue(":rhost", $rhost);
	$st->bindValue(":line", $line);
	$st->bindValue(":time", $time);
	$st->bindValue(":updated", time());
	return $st->execute();
}

function ut_delete($db, $host, $entry) {
	list ($user, $rhost, $line, $time) = $entry;
	$st = $db->prepare('DELETE FROM `utmp` WHERE host=:host AND user=:user AND line=:line');
	$st->bindValue(":host", $host);
	$st->bindValue(":user", $user);
	$st->bindValue(":line", $line);
	$st->bindValue(":time", $time);
	return $st->execute();
}

function ut_delete_host($db, $host) {
	$st = $db->prepare('DELETE FROM utmp WHERE host=:host');
	$st->bindValue(":host", $host);
	return $st->execute();
}

$actions = array(
	"query" => function() {
		$db = new SQLite3(DB_PATH, SQLITE3_OPEN_READONLY);

		$res = $db->query('select * from `utmp`');

		$utmp = array();
		while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
			$utmp[] = $row;
		}
		print json_encode($utmp);
	},

	"insert" => function() {
		$host = $_POST["host"];
		if (!strlen($host)) {
			print "error: host not specified\n";
			return false;
		}
		$data = json_decode($_POST["utmp"]);
		if (!$data) {
			print "error: no data\n";
			return false;
		}

		$db = new SQLite3(DB_PATH);
		foreach ($data as $entry)
			ut_insert($db, $host, $entry);
		print "OK\n";
	},

	"delete" => function() {
		$host = $_POST["host"];
		if (!strlen($host)) {
			print "error: host not specified\n";
			return false;
		}
		$data = json_decode($_POST["utmp"]);
		if (!$data) {
			print "error: no data\n";
			return false;
		}

		$db = new SQLite3(DB_PATH);
		foreach ($data as $entry)
			ut_delete($db, $host, $entry);
		print "OK\n";
	},

	"put" => function() {
		$host = $_POST["host"];
		if (!strlen($host)) {
			print "error: host not specified\n";
			return false;
		}
		$data = json_decode($_POST["utmp"]);
		if ($data === false) {
			print "error: no data\n";
			return false;
		}

		$db = new SQLite3(DB_PATH);
		ut_delete_host($db, $host);
		foreach ($data as $entry)
{'fraction_non_alphanumeric': 0.22757337742868955, 'fraction_numerical': 0.0028937577511368336, 'mean_word_length': 4.231351351351352, 'pattern_counts': {'":': 22, '<': 5, '<?xml version=': 0, '>': 49, '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': '5474539', 'n_tokens_mistral': 2048, 'n_tokens_neox': 1905, 'n_words': 436}