<?php
require '../../include/gnomoradio.inc';

function rb_header ($no_header=false)
{
	global $uid, $email, $password, $cpassword;
	global $db;

	// determine if logged in
	if ($email != '') {
		$r = pg_exec($db, "select email, password, id from users where email='$email'");
		if (pg_numrows($r) == 0) {
			rb_auth_failed();
		}
		$c = pg_result($r, 0, 1);
		$uid = pg_result($r, 0, 2);
		pg_freeresult($r);
		if ($password == '' || crypt($password, $c) != $c) {
			rb_auth_failed();
		}

		setcookie('uid', $uid, 0, '/');
		setcookie('cpassword', $password, 0, '/');
	} else if ($uid != 0) {
		$r = pg_exec($db, "select id, password from users where id='$uid'");
		if (pg_numrows($r) == 0) {
			rb_auth_failed();
		}
		$c = pg_result($r, 0, 1);
		pg_freeresult($r);
		if ($cpassword == '' || crypt($cpassword, $c) != $c) {
			rb_auth_failed();
		}
	} else {
		rb_auth_failed();
	}

	if (!$no_header) {
		my_header(array('title'=>'Rainbow Builder', 'topdir'=>'../../', 'nosidebar'=>'true'));
	}
}

function rb_auth_failed ()
{
	my_header(array('title'=>'Rainbow Builder', 'topdir'=>'../../', 'nosidebar'=>'true'));
	echo '<p>Authorization failed.</p>', "\n";
	rb_footer();
	exit(1);
}

function rb_footer ()
{
	my_footer(array());
}

function license_summary ($l)
{
	if ($l == '')
		return '<i>None</i>';
	return $l;
}

?>
