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 (41c1b09eb8df2f1a46c15b228701ec3731309164) (3,824B) (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;
}

/*
 * This is used for forms
 */
function rg_re_post($op)
{
	if (isset($_REQUEST['rewrite_engine']))
		return "/+" . $op;

	return $_SERVER['PHP_SELF'];
}

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

	return $_SERVER['PHP_SELF'] . "?op=repopage&amp;name=" . $repo_name;
}

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;
}

/*
 * Deletes a folder and the files inside it
 */
function rg_rmdir($dir)
{
	$scan = glob($dir . "/*");
	if ($scan === FALSE) {
		rg_log("\tInvalid pattern [$dir/*]!");
		return FALSE;
	}

	if (count($scan) > 0) {
		foreach ($scan as $junk => $path) {
			if (!@unlink($path)) {
				rg_log("\tCannot remove [$path] ($php_errormsg)!");
				return FALSE;
			}
		}
	}

	if (!@rmdir($dir)) {
		rg_log("\tCannot remove main dir ($php_errormsg)!");
		return FALSE;
	}

	return TRUE;
}

function rg_fatal($msg)
{
	rg_log("FATAL: $msg");
	echo "==========\n";
	echo "RocketGit: $msg\n";
	echo "==========\n";
	exit(1);
}

/*
 * Generates a menu
 */
function rg_menu($a, $rg_ui)
{
	$menu = "";
	$menu2 = "";
	$add = "";
	foreach ($a as $_op => $_info) {
		// we ignore fake menus like 'home'
		if (!isset($_info['text']))
			continue;

		if (isset($_info['needs_admin']) && ($rg_ui['is_admin'] == 0))
			continue;

		if (isset($_info['uid0']) && ($rg_ui['uid'] > 0))
			continue;

		if (!isset($_info['uid0']) && ($rg_ui['uid'] == 0))
			continue;

		$_text = $_info['text'];
		$_url = rg_re_url($_op);
		$menu .= $add . "<a class=\"menu\" href=\""
			. $_url . "\">$_text</a>\n";
		$add = "\t";

		if (!isset($_info['sub']) || (count($_info['sub']) == 0))
			continue;

		foreach ($_info['sub'] as $junk => $_info2) {
			$_text2 = $_info2['text'];
			$menu2 .= "\t<a class=\"menu\" href=\""
				. $_url . $_info2['url']
				. "\">$_text2</a>\n";
		}
	}

	$ret = array();
	$ret[0] = $menu;
	if (!empty($menu2))
		$ret[1] = $menu2;

	return $ret;
}

?>
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