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 / wh / core.inc.php (3597caeb58bbfa68817967fbc9ab12b65c6f5485) (8,948B) (mode 100644) [raw]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
<?php
require_once($INC . "/util.inc.php");
require_once($INC . "/log.inc.php");
require_once($INC . "/sql.inc.php");
require_once($INC . "/prof.inc.php");

$rg_wh_error = "";

function rg_wh_set_error($str)
{
	global $rg_wh_error;

	$rg_wh_error = $str;
	rg_log($str);
}

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

// Here plugins will store the functions
$rg_wh_plugins = array();

$rg_wh_events = array(
	'C' => 'Create repository',
	'P' => 'Push',
	'B' => 'Create branch'
);
/*
 * Generates event list as html
 */
function rg_wh_check_events($events)
{
	global $rg_wh_events;

	$ret = '<fieldset>';
	$ret .= '<legend>Select trigger events</legend>';
	$br = '';
	foreach ($rg_wh_events as $id => $name) {
		$add = '';
		if (strchr($events, $id))
			$add = ' checked="checked"';

		$ret .= $br
			. '<input type="checkbox" name="wh::events[' . $id . ']"'
			. ' id="events-' . $id . '"'
			. $add . ' />'
			. "\n"
			. '<label for="events-' . $id . '">' . $name . '</label>';
		$br = '<br />' . "\n";
	}
	$ret .= '</fieldset>' . "\n";

	return $ret;
}

/*
 * Generates an events list as text
 */
function rg_wh_events($events)
{
	global $rg_wh_events;

	$a = array();
	foreach ($rg_wh_events as $id => $name) {
		if (strchr($events, $id))
			$a[] = $name;
	}

	return implode(', ', $a);
}

/*
 * Some cosmetics applied to a webhook
 */
function rg_wh_cosmetic(&$list)
{
	global $rg_wh_plugins;

	foreach ($list as $id => &$row) {
		if (isset($row['itime']))
			$row['itime_nice'] = gmdate('Y-m-d H:i', $row['itime']);

		if (isset($row['description'])) {
			if (empty($row['description']))
				$row['HTML:description_nice'] = 'n/a';
			else
				$row['HTML:description_nice'] =
					nl2br(rg_xss_safe($row['description']));
		}

		if (isset($row['events']))
			$row['events_text'] = rg_wh_events($row['events']);

		if (isset($row['last_output'])) {
			if (empty($row['last_output']))
				$row['HTML:last_output_nice'] = 'n/a';
			else
				$row['HTML:last_output_nice'] =
					nl2br(rg_xss_safe($row['last_output']));
		}

		$t = $row['htype'];
		if (isset($rg_wh_plugins[$t]['cosmetic']))
			$rg_wh_plugins[$t]['cosmetic']($row);
	}
}

/*
 * Set last_output field of a webhook
 */
function rg_wh_set_last_output($db, $uid, $id, $output)
{
	rg_prof_start('wh_set_last_output');
	rg_log_enter('wh_set_last_output id=$id');

	$ret = FALSE;
	while (1) {
		$params = array('id' => $id, 'last_output' => $output);
		$sql = 'UPDATE webhooks'
			. ' SET last_output = @@last_output@@'
			. ' WHERE id = @@id@@';
		$res = rg_sql_query_params($db, $sql, $params);
		if ($res === FALSE) {
			rg_wh_set_error('cannot insert/update data');
			break;
		}
		rg_sql_free_result($res);

		$key = 'wh' . '::' . $uid . '::' . 'list'
			. '::' . $id . '::' . 'last_output';
		rg_cache_set($key, $output, RG_SOCKET_NO_WAIT);

		$ret = TRUE;
		break;
	}

	rg_log_exit();
	rg_prof_end('wh_set_last_output');
	return $ret;
}

/*
 * Sorting the webhooks list by itime DESC
 */
function rg_wh_sort_helper($a, $b)
{
	if ($a['itime'] > $b['itime'])
		return -1;

	if ($a['itime'] == $b['itime'])
		return 0;

	return 1;
}

/*
 * Returns a list of webhooks associated with a user
 * @repo_id may be 0 => hooks installed on user account
 */
function rg_wh_list($db, $uid)
{
	rg_prof_start('wh_list');
	rg_log_enter('wh_list');

	$ret = array('ok' => 0, 'list' => array());
	while (1) {
		$key = 'wh' . '::' . $uid;
		$r = rg_cache_get($key);
		if (($r !== FALSE) && isset($r['LIST_LOADED'])) {
			$ret['list'] = $r['list'];
			$ret['ok'] = 1;
			break;
		}

		$params = array('uid' => $uid);
		$sql = 'SELECT * FROM webhooks'
			. ' WHERE uid = @@uid@@'
			. ' ORDER BY itime DESC';
		$res = rg_sql_query_params($db, $sql, $params);
		if ($res === FALSE) {
			rg_wh_set_error('cannot load data');
			break;
		}

		while (($row = rg_sql_fetch_array($res))) {
			//rg_log_ml('DEBUG: wh_list: row: ' . print_r($row, TRUE));
			$id = $row['id'];
			if (strcmp($row['idata'], "") != 0) {
				$row['idata'] = unserialize($row['idata']);
				if ($row['idata'] === FALSE) {
					rg_internal_error('cannot unserialize data');
					// we try to continue
					$row['idata'] = array();
				}
			} else {
				$row['idata'] = array();
			}

			$ret['list'][$id] = $row;
		}
		rg_sql_free_result($res);

		$a = array('LIST_LOADED' => 1, 'list' => $ret['list']);
		rg_cache_merge($key, $a, RG_SOCKET_NO_WAIT);
		$ret['ok'] = 1;
		break;
	}
	uasort($ret['list'], 'rg_wh_sort_helper');

	rg_log_exit();
	rg_prof_end('wh_list');
	return $ret;
}

/*
 * Adds/edits a webhook
 */
function rg_wh_add($db, $uid, $data)
{
	rg_prof_start('wh_add');
	rg_log_enter('wh_add');

	$ret = array('ok' => 0);
	while (1) {
		$data['uid'] = $uid;
		$data['itime'] = time();
		$params = $data;
		$params['idata'] = serialize($params['idata']);

		if ($data['id'] == 0) {
			$data['last_output'] = '';
			$sql = 'INSERT INTO webhooks (uid, repo_id, itime'
				. ', htype, events, url'
				. ', add_ip, description, key, opaque, idata)'
				. ' VALUES (@@uid@@, @@repo_id@@, @@itime@@'
				. ', @@htype@@, @@events@@, @@url@@'
				. ', @@add_ip@@'
				. ', @@description@@, @@key@@, @@opaque@@'
				. ', @@idata@@)'
				. ' RETURNING id';
		} else {
			$sql = 'UPDATE webhooks'
				. ' SET events = @@events@@'
				. ', url = @@url@@'
				. ', description = @@description@@'
				. ', key = @@key@@'
				. ', opaque = @@opaque@@'
				. ', idata = @@idata@@'
				. ' WHERE uid = @@uid@@'
				. ' AND id = @@id@@';
		}

		$res = rg_sql_query_params($db, $sql, $params);
		if ($res === FALSE) {
			rg_wh_set_error('cannot insert/update data');
			break;
		}
		if ($data['id'] == 0)
			$row = rg_sql_fetch_array($res);
		rg_sql_free_result($res);

		if ($data['id'] == 0)
			$data['id'] = $row['id'];
		$key = 'wh' . '::' . $uid . '::' . 'list'
			. '::' . $data['id'];
		rg_cache_merge($key, $data, RG_SOCKET_NO_WAIT);

		$ret['ok'] = 1;
		break;
	}

	rg_log_exit();
	rg_prof_end('wh_add');
	return $ret;
}

/*
 * Removes a list of webhooks
 */
function rg_wh_remove($db, $uid, $list)
{
	rg_prof_start('wh_remove');
	rg_log_enter('wh_remove');

	$ret = array('ok' => 0);
	while (1) {
		if (empty($list)) {
			rg_wh_set_error('you did not select anything');
			break;
		}

		$my_list = array();
		foreach ($list as $id => $junk)
			$my_list[] = sprintf("%u", $id);

		$params = array('uid' => $uid);
		$sql_list = implode(', ', $my_list);

		$sql = 'DELETE FROM webhooks'
			. ' WHERE uid = @@uid@@'
			. ' AND id IN (' . $sql_list . ')';
		$res = rg_sql_query_params($db, $sql, $params);
		if ($res === FALSE) {
			rg_wh_set_error('cannot remove webhooks');
			break;
		}
		rg_sql_free_result($res);

		foreach ($my_list as $junk => $id) {
			$key = 'wh' . '::' . $uid
				. '::' . 'list' . '::' . $id;
			rg_cache_unset($key, RG_SOCKET_NO_WAIT);
		}

		$ret['ok'] = 1;
		break;
	}

	rg_log_exit();
	rg_prof_end('wh_remove');
	return $ret;
}

/*
 * Extract vars from request
 */
function rg_wh_fill_vars(&$rg)
{
	global $rg_wh_plugins;

	$ret = FALSE;
	while (1) {
		$t = $rg['wh']['htype'];

		if (!isset($rg_wh_plugins[$t])) {
			$errmsg[] = rg_template('user/settings/wh/invalid_htype',
				$rg, TRUE /*xss*/);
			break;
		}

		if (isset($rg_wh_plugins[$t]['fill_vars'])) {
			$rg_wh_plugins[$t]['fill_vars']($rg);
			break;
		}

		$ret = TRUE;
		break;
	}

	return $ret;
}

/*
 * Validate parameters
 */
function rg_wh_validate_vars($rg, &$errmsg)
{
	global $rg_wh_plugins;

	$ret = FALSE;
	while (1) {
		$t = $rg['wh']['htype'];

		if (!isset($rg_wh_plugins[$t])) {
			$errmsg[] = rg_template('user/settings/wh/invalid_htype',
				$rg, TRUE /*xss*/);
			break;
		}

		if (isset($rg_wh_plugins[$t]['validate_vars'])) {
			$r = $rg_wh_plugins[$t]['validate_vars']($rg, $errmsg);
			if ($r !== TRUE)
				break;
		}

		$ret = TRUE;
		break;
	}

	return $ret;
}

/*
 * Generic add_form function
 */
function rg_wh_add_form(&$rg)
{
	global $rg_wh_plugins;

	while (1) {
		$t = $rg['wh']['htype'];

		if (!isset($rg_wh_plugins[$t]))
			break;

		if (isset($rg_wh_plugins[$t]['add_form'])) {
			$rg_wh_plugins[$t]['add_form']($rg);
			break;
		}

		break;
	}
}

/*
 * Generic default_paras function
 */
function rg_wh_default_paras(&$rg)
{
	global $rg_wh_plugins;

	while (1) {
		$t = $rg['wh']['htype'];

		if (!isset($rg_wh_plugins[$t]))
			break;

		if (isset($rg_wh_plugins[$t]['default_paras'])) {
			$rg_wh_plugins[$t]['default_paras']($rg);
			break;
		}

		break;
	}
}

/*
 * Generic hints add function
 */
function rg_wh_fill_hints(&$rg, &$hints)
{
	global $rg_wh_plugins;

	while (1) {
		$t = $rg['wh']['htype'];

		if (!isset($rg_wh_plugins[$t]))
			break;

		if (isset($rg_wh_plugins[$t]['fill_hints'])) {
			$rg_wh_plugins[$t]['fill_hints']($rg, $hints);
			break;
		}

		break;
	}
}

/*
 * Returns a HTML list with possible htypes
 */
function rg_wh_htypes($rg)
{
	global $rg_wh_plugins;

	return rg_template_table('user/settings/wh/plugins_list',
		$rg_wh_plugins, $rg);
}

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