xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
<root> / inc / keys.inc.php (f813cdda4640fb1655bef51ef573537ae85ca656) (2,094B) (mode 100644) [raw]
<?php
require_once($INC . "/db.inc.php");
require_once($INC . "/state.inc.php");

$keys_error = "";

function keys_set_error($str)
{
	global $keys_error;
	$keys_error = $str;
}

function keys_error()
{
	global $keys_error;
	return $keys_error;
}

/*
 * Remove a key from database
 */
function keys_remove($db, $uid, $key_id)
{
	// mark dirty
	state_set($db, "authorized_keys", 1);

	$e_uid = sprintf("%u", $uid);
	$e_key_id = sprintf("%u", $key_id);

	$sql = "DELETE FROM keys"
		. " WHERE uid = $e_uid"
		. " AND key_id = $e_key_id";
	$res = sql_query($db, $sql);
	if ($res === FALSE) {
		keys_set_error("Cannot delete key $key_id (" . sql_error() . ")");
		return FALSE;
	}
	sql_free_result($res);

	return TRUE;
}

/*
 * Add a key
 * Returns the key_id of the key.
 */
function keys_add($db, $uid, $key)
{
	$itime = time();
	$e_uid = sprintf("%u", $uid);
	$e_key = sql_escape($db, $key);

	// set dirty
	if (state_set($db, "authorized_keys", 1) === FALSE)
		return FALSE;

	$sql = "INSERT INTO keys (itime, uid, key)"
		. " VALUES ($itime, $e_uid, '$e_key')";
	$res = sql_query($db, $sql);
	if ($res === FALSE) {
		keys_set_error("Cannot insert key (" . sql_error() . ")");
		return FALSE;
	}
	sql_free_result($res);

	return sql_last_id($db);
}

/*
 * Regenerates authorized_keys files
 */
function keys_regen($db, $file)
{
	$tmp = $file . ".tmp";
	$f = @fopen($tmp, "w+");
	if (!$f)
		return FALSE;

	$sql = "SELECT uid, key FROM keys";
	$res = sql_query($db, $sql);
	while (($row = sql_fetch_array($res))) {
		$buf = "command=\"/usr/bin/xxx " . $row['uid'] . "\""
			. ",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty"
			. " " . $row['key'] . "\n";
		if (fwrite($f, $buf) === FALSE) {
			keys_set_error("Cannot write. Disk space problems?");
			fclose($f);
			unlink($tmp);
			sql_free_result($res);
			return FALSE;
		}
	}
	sql_free_result($res);

	fclose($f);

	if (rename($tmp, $file) === FALSE) {
		keys_set_error("Cannot rename $tmp to $file!");
		unlink($tmp);
		return FALSE;
	}

	// mark file as clean
	state_set($db, "authorized_keys", 0);

	return TRUE;
}

?>
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master