$dbh = pg_pconnect("");
pg_exec ($dbh, "SET DateStyle = 'ISO'") or die("Datenbank-Abfrage!");
?>
function reauth()
{
header("WWW-Authenticate: Basic realm=\"LinuxTag\"");
header("HTTP/1.0 401 Unauthorized");
echo "Wrong Username or Password\n";
exit;
}
?>
if (empty ($PHP_AUTH_USER) || empty ($PHP_AUTH_PW)) {
reauth();
} else {
$query = sprintf ("SELECT id,name,project,admin FROM person "
."WHERE login = 1 AND list = 0 AND email = '%s' AND password = '%s'",
addslashes ($PHP_AUTH_USER), md5($PHP_AUTH_PW));
$sth = pg_exec ($dbh, $query) or die("Datenbank-Fehler");
if (pg_NumRows ($sth) == 1) {
$row = pg_fetch_array($sth, 0);
$LINUXTAG_AUTH['id'] = $row['id'];
$LINUXTAG_AUTH['name'] = $row['name'];
$LINUXTAG_AUTH['email'] = $PHP_AUTH_USER;
$LINUXTAG_AUTH['project'] = $row['project'];
$LINUXTAG_AUTH['admin'] = $row['admin'];
$query = sprintf ("SELECT project FROM junction WHERE person = %d AND project <> '%s'",
$LINUXTAG_AUTH['id'], $LINUXTAG_AUTH['project']);
$sth = pg_exec($dbh, $query) or die ("Cannot execute query");
for ($nr=0; $nr < pg_NumRows ($sth); $nr++) {
$row = pg_fetch_array($sth, $nr);
if (strlen($LINUXTAG_AUTH["projects"]) > 0) {
$LINUXTAG_AUTH["projects"] .= ", " . $row['project'];
} else {
$LINUXTAG_AUTH["projects"] = $row['project'];
}
}
####### This code is temporaly disabled
## update the last login timestamp
#$query = sprintf("UPDATE person set lastlogin = current_timestamp where email = '%s'",
# addslashes($LINUXTAG_AUTH['email']));
#pg_exec ($dbh, $query) or die ("Datenbank-Fehler");
} else {
reauth();
}
if (!is_supporter()) {
$query = sprintf ("SELECT person FROM junction WHERE person = %d AND project = 'Supporter'",
$LINUXTAG_AUTH['id']);
$sth = pg_exec($dbh, $query) or die ("Cannot execute query");
if (pg_NumRows($sth) > 0) {
$LINUXTAG_AUTH['supporter'] = 1;
} else {
$LINUXTAG_AUTH['supporter'] = 0;
}
} else {
$LINUXTAG_AUTH['supporter'] = 1;
}
}
?>
function spokendate ($date)
{
$mon = array ("January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December");
$rdate = explode ("-", $date);
if (($rdate[2] % 10) == 1) {
$appendix = "st";
} elseif (($rdate[2] % 10) == 2) {
$appendix = "nd";
} elseif (($rdate[2] % 10) == 3) {
$appendix = "rd";
} else {
$appendix = "th";
}
return (sprintf ("%s %d%s, %d", $mon[$rdate[1]-1], $rdate[2], $appendix, $rdate[0]));
}
?>
# Functions below will be included into all pages
# This function returns true, if the user is an admin
#
function is_admin()
{
global $LINUXTAG_AUTH;
return $LINUXTAG_AUTH['admin'] == 1 ? true : false;
}
# This function returns true, if the user is a supporter
#
function is_supporter()
{
global $LINUXTAG_AUTH;
return $LINUXTAG_AUTH['project'] == 'Supporter' ? true : false;
}
# Return true if the person is also a supporter but is a project person
# in the first place
#
function is_also_supporter()
{
global $LINUXTAG_AUTH;
return $LINUXTAG_AUTH['supporter'] == 1 ? true : false;
}
# This function returns true, if the user is a member of said project
#
function is_member($project)
{
global $LINUXTAG_AUTH, $dbh;
$query = sprintf ("SELECT * FROM junction,person "
."WHERE person = id AND person.email = '%s' AND junction.project = '%s'",
$LINUXTAG_AUTH['email'], $project);
$sth = pg_exec($dbh, $query) or die("Could not find user/project");
if (pg_NumRows($sth) == 0) {
return false;
}
return true;
}
# This function returns true, if the user is allowed to edit user $id
#
function allowed_to_edit($id)
{
global $LINUXTAG_AUTH, $dbh;
// First we check if the User is the current user
if ($id == $LINUXTAG_AUTH['id']) {
// OK, every user can edit their own data
return true;
} else {
// If he is not an admin, we must return false
if ($LINUXTAG_AUTH['admin'] <> 1) { return false; }
}
// If not, we will try to find out the project of this user
if (preg_match("/^[0-9]+$/", $id)) {
$query = sprintf ("SELECT junction.project FROM junction,person "
."WHERE person = id AND id=%d",
$id);
} else {
die ("ID needs to be numeric");
}
$sth = pg_exec($dbh, $query) or die("Could not find user");
if (pg_NumRows($sth) == 0) {
return false;
}
for ($nr=0; $nr < pg_NumRows ($sth); $nr++) {
$row = pg_fetch_array($sth, $nr);
if ($row['project'] == $LINUXTAG_AUTH["project"]) {
return true;
}
}
return false;
}
# Check whether a user is allowed to edit a particular workshop
#
function allowed_to_edit_workshop($oid)
{
global $LINUXTAG_AUTH, $dbh;
if (!isset ($oid)) {
echo ("No workshop selected, uh?
");
return false;
}
if (is_admin ()) {
$query = sprintf ("SELECT project FROM workshop WHERE oid = %d", $oid);
$sth = pg_exec ($dbh, $query) or die("Datenbank-Abfrage!");
if (pg_NumRows ($sth) > 0) {
$row = pg_fetch_array ($sth, 0);
if ($row['project'] == $LINUXTAG_AUTH['project']) {
return true;
}
}
}
$query = sprintf ("SELECT name,person FROM workshop,person WHERE person = id AND workshop.oid = %d", $oid);
$sth = pg_exec ($dbh, $query) or die("Datenbank-Abfrage!");
if (pg_NumRows ($sth) > 0) {
$row = pg_fetch_array ($sth, 0);
if ($row['person'] == $LINUXTAG_AUTH['id']) {
return true;
} else {
echo ("Only the admin of a project or the speaker may edit workshop data.
");
return false;
}
} else {
echo ("No such workshop found.
");
return false;
}
return false;
}
# This function will return a list of users the given user is allowed to edit
#
function list_of_users($user)
{
global $dbh;
$users = array();
// First, we check if the User is an admin
$query = sprintf("SELECT admin,name,email,project FROM person WHERE name='%s'", addslashes($user));
$sth = pg_exec($dbh, $query) or die ("Cannot find this user");
if (pg_NumRows($sth) > 0) {
$row = pg_fetch_array($sth, 0);
} else {
die ("User has disappeard");
}
if ($row[0] <> 1) {
// This user is only allowed to edit himself
$users[0] = array($row[1], $row[2]);
return $users;
} else {
// OK, this user is allowed to edit all persons from his project, so lets find this persons
$query = sprintf("SELECT name,email FROM person WHERE project='%s' ORDER BY upper(name)",
addslashes($row[3]));
$sth = pg_exec($dbh, $query) or die ("Cannot find persons from project");
if (pg_NumRows($sth) > 0) {
for ($x = 0; $x < pg_NumRows($sth); $x++) {
$row = pg_fetch_array($sth, $x);
$users[$x] = array($row[0], $row[1]);
}
} else {
die ("Cannot find any users of this project");
}
}
return $users;
}
?>
# This function is from phpdoc
function make_seed() {
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
function randpass()
{
mt_srand(make_seed());
# Fixme: Find a better solution for php3
$x = 0;
$values = array();
$helper = range(0,9);
while (list ($key, $val) = each ($helper)) {
$values[$x] = $val;
$x++;
}
$helper = range("a", "z");
while (list ($key, $val) = each ($helper)) {
$values[$x] = $val;
$x++;
}
$helper = range("A", "Z");
while (list ($key, $val) = each ($helper)) {
$values[$x] = $val;
$x++;
}
for ($x = 0; $x < 6; $x++)
{
$pass .= $values[mt_rand() % (sizeof($values) -1)];
}
return $pass;
}
function mail_password($email, $newpass)
{
if (strlen ($email) > 0) {
$body = "LinuxTag Projects Management\n";
$body .= "\n\n";
$body .= "Account: " . $email . "\n";
$body .= "Password: " . $newpass . "\n";
$body .= "\nThe password may be changed within the system.";
$body .= "\nSince this site does not use https, please don't use the same password\nsomewhere else.\n";
$body .= "\nYou are subscribed to the infomails.\nYou can change this using the web interface.\n";
$body .= "\nThis account is only valid for this years' LinuxTag.\n";
$body .= "\nThanks for your commitment.\n";
mail($email, "[LT Projects] New Password", $body, "From: joey@infodrom.org (LinuxTag Projects Management)");
}
return true;
}
?>
$errorlevel = array(
1 => "Permission",
2 => "IO",
3 => "SQL",
4 => "No Rows"
);
# WARNING!
# catch_error is not finished yet
# This function is for reporting errors
# $error_level is the kind of the error
# $error_message is a message we can give to the user
# $debugging_output is output we should log, for exampel a query which failed
function catch_error($error_level, $debugging_output = '', $error_message = '')
{
global $errorlevel;
// First we open a logfile
$file = fopen("logfile.txt", "a") or die ("Cannot open logfile");
// Lock it
flock($file, 2) or die ("Cannot lock logfile");
// Write a usefull message to this file
fwrite($file, strftime("%Y-%m-%d %T") . ": " . $errorlevel[$error_level] . ": " . $debugging_output);
echo "Error:
Sorry, we were unable to handle your request
";
if ($error_level == 1)
{ echo "Permission denied
"; }
elseif (($error_level == 3) or ($error_level == 4))
{ echo "Database problem
"; }
echo $error_message . "
";
// Unlock it
flock($file, 3);
}
$days = array(
"" => array( "dinner" => "dinner-",
"sleeping" => "sleeping-"),
"" => array( "breakfast" => "breakfast-",
"dinner" => "dinner-",
"sleeping" => "sleeping-"),
"" => array( "breakfast" => "breakfast-",
"dinner" => "dinner-",
"sleeping" => "sleeping-"),
"" => array( "breakfast" => "breakfast-",
"dinner" => "dinner-",
"sleeping" => "sleeping-"),
"" => array( "breakfast" => "breakfast-",
"dinner" => "dinner-",
"sleeping" => "sleeping-"),
"" => array( "breakfast" => "breakfast-")
);
?>
# Local variables:
# mode: indented-text
# mode: auto-fill
# end: