You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.5 KiB
PHTML

14 years ago
<?php
/**
* Testet ob ein User eingeloggt ist und lädt die entsprechenden Privilegien
*/
14 years ago
function load_auth() {
11 years ago
global $user, $privileges;
11 years ago
$user = null;
if (isset($_SESSION['uid'])) {
$user = sql_select("SELECT * FROM `User` WHERE `UID`='" . sql_escape($_SESSION['uid']) . "' LIMIT 1");
11 years ago
if (count($user) > 0) {
// User ist eingeloggt, Datensatz zur Verfügung stellen und Timestamp updaten
list($user) = $user;
11 years ago
sql_query("UPDATE `User` SET " . "`lastLogIn` = '" . time() . "'" . " WHERE `UID` = '" . sql_escape($_SESSION['uid']) . "' LIMIT 1;");
$privileges = privileges_for_user($user['UID']);
return;
}
unset($_SESSION['uid']);
11 years ago
}
// guest privileges
$privileges = privileges_for_group(- 1);
14 years ago
}
/**
* generate a salt (random string) of arbitrary length suitable for the use with crypt()
*/
function generate_salt($length = 16) {
11 years ago
$alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
$salt = "";
for ($i = 0; $i < $length; $i ++) {
11 years ago
$salt .= $alphabet[rand(0, strlen($alphabet) - 1)];
}
return $salt;
}
/**
* set the password of a user
*/
function set_password($uid, $password) {
global $crypt_alg;
$result = sql_query("UPDATE `User` SET `Passwort` = '" . sql_escape(crypt($password, $crypt_alg . '$' . generate_salt(16) . '$')) . "', `password_recovery_token`=NULL WHERE `UID` = " . intval($uid) . " LIMIT 1");
if ($result === false) {
engelsystem_error('Unable to update password.');
}
return $result;
}
/**
* verify a password given a precomputed salt.
* if $uid is given and $salt is an old-style salt (plain md5), we convert it automatically
*/
function verify_password($password, $salt, $uid = false) {
global $crypt_alg;
11 years ago
$correct = false;
if (substr($salt, 0, 1) == '$') { // new-style crypt()
11 years ago
$correct = crypt($password, $salt) == $salt;
} elseif (substr($salt, 0, 7) == '{crypt}') { // old-style crypt() with DES and static salt - not used anymore
11 years ago
$correct = crypt($password, '77') == $salt;
} elseif (strlen($salt) == 32) { // old-style md5 without salt - not used anymore
11 years ago
$correct = md5($password) == $salt;
}
if ($correct && substr($salt, 0, strlen($crypt_alg)) != $crypt_alg && $uid) {
11 years ago
// this password is stored in another format than we want it to be.
// let's update it!
// we duplicate the query from the above set_password() function to have the extra safety of checking the old hash
sql_query("UPDATE `User` SET `Passwort` = '" . sql_escape(crypt($password, $crypt_alg . '$' . generate_salt() . '$')) . "' WHERE `UID` = " . intval($uid) . " AND `Passwort` = '" . sql_escape($salt) . "' LIMIT 1");
11 years ago
}
return $correct;
14 years ago
}
function privileges_for_user($user_id) {
$privileges = [];
$user_privs = sql_select("SELECT `Privileges`.`name` FROM `User` JOIN `UserGroups` ON (`User`.`UID` = `UserGroups`.`uid`) JOIN `GroupPrivileges` ON (`UserGroups`.`group_id` = `GroupPrivileges`.`group_id`) JOIN `Privileges` ON (`GroupPrivileges`.`privilege_id` = `Privileges`.`id`) WHERE `User`.`UID`='" . sql_escape($user_id) . "'");
foreach ($user_privs as $user_priv) {
11 years ago
$privileges[] = $user_priv['name'];
}
11 years ago
return $privileges;
}
function privileges_for_group($group_id) {
$privileges = [];
$groups_privs = sql_select("SELECT * FROM `GroupPrivileges` JOIN `Privileges` ON (`GroupPrivileges`.`privilege_id` = `Privileges`.`id`) WHERE `group_id`='" . sql_escape($group_id) . "'");
foreach ($groups_privs as $guest_priv) {
11 years ago
$privileges[] = $guest_priv['name'];
}
11 years ago
return $privileges;
}
?>