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 / util.inc.php (86962e8d6c37970a5d20625050f92e5f40a3c2e6) (2,136B) (mode 100644) [raw]
<?php

function rg_1024($v)
{
	if ($v <= 9999)
		return number_format($v);
	$v /= 1024;

	if ($v <= 9999)
		return number_format($v) . "KiB";
	$v /= 1024;

	if ($v <= 9999)
		return number_format($v) . "MiB";
	$v /= 1024;

	if ($v <= 9999)
		return number_format($v) . "GiB";
	$v /= 1024;

	return number_format($v) . "TiB";
}

/*
 * Unique ID generator
 */
function rg_id($len)
{
	$id = "";

	$f = @fopen("/dev/urandom", "r");
	if ($f !== NULL) {
		$buf = @fread($f, 128);
		if ($buf !== NULL)
			$id = sha1($buf);
		fclose($f);
	}

	if (empty($id))
		$id = sha1(mt_rand() . serialize($_REQUEST));

	return substr($id, 0, $len);
}

$_lock = FALSE;
function rg_lock_or_exit($file)
{
	global $_lock;

	if ($_lock !== FALSE) {
		rg_log("\tYou already have a lock on $file! Bad!");
		exit(1);
	}

	$_lock = @fopen($file, "w");
	if ($_lock === FALSE) {
		rg_log("\tCannot open $file!");
		exit(1);
	}

	if (!flock($_lock, LOCK_EX | LOCK_NB)) {
		fclose($_lock);
		exit(0);
	}

	fwrite($_lock, getmypid() . "\n");
}

function rg_load()
{
	return intval(file_get_contents("/proc/loadavg"));
}

/*
 * Builds URLs
 */
function rg_re_url($op)
{
	if (isset($_REQUEST['rewrite_engine']))
		return "/+" . $op;

	return $_SERVER['PHP_SELF'] . "?op=" . $op;
}

function rg_re_repopage($repo_id, $repo_name)
{
	if (isset($_REQUEST['rewrite_engine']))
		return "/" . $repo_name;

	return $_SERVER['PHP_SELF'] . "?op=repo&amp;subop=2&amp;repo_id=" . $repo_id;
}

function rg_var_str($name)
{
	$ret = "";

	if (isset($_COOKIE[$name]))
		$ret = $_COOKIE[$name];

	if (isset($_POST[$name]))
		$ret = $_POST[$name];

	if (isset($_GET[$name]))
		$ret = $_GET[$name];

	return htmlspecialchars($ret, ENT_QUOTES);
}

function rg_var_int($name)
{
	return sprintf("%d", rg_var_str($name));
}

function rg_var_uint($name)
{
	return sprintf("%u", rg_var_str($name));
}

function rg_var_re($name, $re)
{
	$a = rg_var_str($name);
	return preg_replace($re, "", $a);
}

/*
 * Enforce chars in a name. It is used for user and repo.
 */
function rg_chars_allow($name, $allowed_regexp)
{
	if (preg_match($allowed_regexp, $name) === 0)
		return FALSE;

	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