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.

162 lines
4.4 KiB
PHTML

14 years ago
<?php
use Engelsystem\Database\DB;
/**
* Testet ob ein User eingeloggt ist und lädt die entsprechenden Privilegien
*/
8 years ago
function load_auth()
{
global $user, $privileges;
8 years ago
8 years ago
$user = null;
if (isset($_SESSION['uid'])) {
$user = DB::select('SELECT * FROM `User` WHERE `UID`=? LIMIT 1', [$_SESSION['uid']]);
8 years ago
if (count($user) > 0) {
// User ist eingeloggt, Datensatz zur Verfügung stellen und Timestamp updaten
$user = array_shift($user);
DB::update('
8 years ago
UPDATE `User`
SET `lastLogIn` = ?
WHERE `UID` = ?
8 years ago
LIMIT 1
', [
time(),
$_SESSION['uid'],
]);
8 years ago
$privileges = privileges_for_user($user['UID']);
return;
}
unset($_SESSION['uid']);
}
8 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()
*
* @param int $length
* @return string
*/
8 years ago
function generate_salt($length = 16)
{
$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
$salt = '';
8 years ago
for ($i = 0; $i < $length; $i++) {
8 years ago
$salt .= $alphabet[rand(0, strlen($alphabet) - 1)];
}
return $salt;
}
/**
* set the password of a user
*
* @param int $uid
* @param string $password
* @return bool
*/
8 years ago
function set_password($uid, $password)
{
global $crypt_alg;
$result = DB::update('
8 years ago
UPDATE `User`
SET `Passwort` = ?,
8 years ago
`password_recovery_token`=NULL
WHERE `UID` = ?
8 years ago
LIMIT 1
',
[
crypt($password, $crypt_alg . '$' . generate_salt(16) . '$'),
$uid
]
);
if (DB::getStm()->errorCode() != '00000') {
8 years ago
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
*
* @param string $password
* @param string $salt
* @param int $uid
* @return bool
*/
function verify_password($password, $salt, $uid = null)
8 years ago
{
global $crypt_alg;
$correct = false;
if (substr($salt, 0, 1) == '$') { // new-style crypt()
8 years ago
$correct = crypt($password, $salt) == $salt;
8 years ago
} elseif (substr($salt, 0, 7) == '{crypt}') { // old-style crypt() with DES and static salt - not used anymore
8 years ago
$correct = crypt($password, '77') == $salt;
8 years ago
} elseif (strlen($salt) == 32) { // old-style md5 without salt - not used anymore
8 years ago
$correct = md5($password) == $salt;
8 years ago
}
if ($correct && substr($salt, 0, strlen($crypt_alg)) != $crypt_alg && intval($uid)) {
8 years ago
// this password is stored in another format than we want it to be.
8 years ago
// let's update it!
// we duplicate the query from the above set_password() function to have the extra safety of checking the old hash
DB::update('
UPDATE `User`
SET `Passwort` = ?
WHERE `UID` = ?
AND `Passwort` = ?
LIMIT 1
',
[
crypt($password, $crypt_alg . '$' . generate_salt() . '$'),
$uid,
$salt,
]
);
8 years ago
}
return $correct;
14 years ago
}
/**
* @param int $user_id
* @return array
*/
8 years ago
function privileges_for_user($user_id)
{
$privileges = [];
$user_privileges = DB::select('
8 years ago
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`=?
', [$user_id]);
foreach ($user_privileges as $user_privilege) {
$privileges[] = $user_privilege['name'];
8 years ago
}
return $privileges;
}
/**
* @param int $group_id
* @return array
*/
8 years ago
function privileges_for_group($group_id)
{
$privileges = [];
$groups_privileges = DB::select('
SELECT `name`
8 years ago
FROM `GroupPrivileges`
JOIN `Privileges` ON (`GroupPrivileges`.`privilege_id` = `Privileges`.`id`)
WHERE `group_id`=?
', [$group_id]);
foreach ($groups_privileges as $guest_privilege) {
$privileges[] = $guest_privilege['name'];
8 years ago
}
return $privileges;
}