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> / tests / http_confirm.php (8c1a4760f6bfb758d196f0121cdd8a02b96af81e) (2,791B) (mode 100644) [raw]
<?php
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");

$INC = dirname(__FILE__) . "/../inc";
require_once(dirname(__FILE__) . "/config.php");
require_once($INC . "/init.inc.php");
require_once($INC . "/util.inc.php");
require_once("http.inc.php");

rg_log_set_file("http_confirm.log");

$rg_sql = "host=localhost user=rocketgit dbname=rocketgit connect_timeout=10";
$rg_no_db = TRUE;
require_once("common.php");

$_testns = 'http_confirm';
$rg_cache_enable = TRUE;
$rg_cache_debug = TRUE;
$rg_event_socket = "/var/lib/rocketgit/sockets/event.sock";

$uniq = rg_id(10);

rg_log_enter('Loading confirmation form...');
// First we need to load the form so we can get the token
$r = do_req($test_url . "/op/create_account", $data, $headers);
if ($r === FALSE) {
	rg_log("Cannot load create_account page!");
	exit(1);
}
$good_sid = $r['sid'];
$good_token = $r['tokens']['user_edit_hl'];
rg_log_exit();


rg_log_enter('Adding an account...');
$data['token'] = $r['tokens']['user_edit_hl'];
$data['username'] = 'http_confirm-' . time();
$data['email'] = 'http_confirm-' . $uniq . '@a.b';
$data['pass'] = $uniq;
$data['pass2'] = $uniq;
$data['plan_id'] = 0;
$data['tos'] = 1;
$data['force_confirm'] = 1;
$data['doit'] = 1;
$headers = array("Cookie: sid=" . $good_sid);
$r = do_req($test_url . '/op/create_account?t=create_account', $data, $headers);
if (!strstr($r['body'], 'Your account has been successfully created')) {
	rg_log_ml('body: ' . print_r($r['body'], TRUE));
	rg_log('Cannot create account!');
	exit(1);
}
rg_log_exit();


rg_log_enter('Finding uid...');
$sql = 'SELECT uid FROM users WHERE username = \'' . $data['username'] . '\'';
$res = rg_sql_query($db, $sql);
if ($res === FALSE) {
	rg_log('Query error!');
	exit(1);
}
$row = rg_sql_fetch_array($res);
rg_sql_free_result($res);
$uid = $row['uid'];
rg_log_exit();


rg_log_enter('Finding confirmation link...');
$key = 'DEBUG::' . $uid . '::mail';
$tries = 10;
while ($tries) {
	$c = rg_cache_get($key);
	if ($c !== FALSE)
		break;

	$tries--;
	sleep(1);
}
rg_log_ml('c: ' . print_r($c, TRUE));
$t = explode('below:', $c['body']);
$t = ltrim($t[1]);
$t = explode("\n", $t);
$link = $t[0];
rg_log('Link=' . $link);
rg_log_exit();


rg_log_enter('Trying with the wrong link...');
$data = array(); $headers = array();
$r = do_req($link . 'XXX?t=http_confirm', $data, $headers);
if (!strstr($r['body'], 'Invalid token')) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Somethign wrong with the token!');
	exit(1);
}
rg_log_exit();


rg_log_enter('Trying with the good link...');
$r = do_req($link . '?t=http_confirm', $data, $headers);
if (!strstr($r['body'], 'Home page of user')) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Somethign wrong with the token!');
	exit(1);
}
rg_log_exit();


rg_log('OK!');
?>
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