2011-06-02 00:48:29 +02:00
< ? php
2016-09-29 11:28:42 +02:00
/**
* Testet ob ein User eingeloggt ist und lädt die entsprechenden Privilegien
*/
2011-06-02 00:48:29 +02:00
function load_auth () {
2013-10-13 00:52:44 +02:00
global $user , $privileges ;
2016-09-29 11:28:42 +02:00
2013-10-13 00:52:44 +02:00
$user = null ;
if ( isset ( $_SESSION [ 'uid' ])) {
2014-12-28 13:44:56 +01:00
$user = sql_select ( " SELECT * FROM `User` WHERE `UID`=' " . sql_escape ( $_SESSION [ 'uid' ]) . " ' LIMIT 1 " );
2013-10-13 00:52:44 +02:00
if ( count ( $user ) > 0 ) {
// User ist eingeloggt, Datensatz zur Verfügung stellen und Timestamp updaten
2016-09-29 11:28:42 +02:00
list ( $user ) = $user ;
2013-10-13 00:52:44 +02:00
sql_query ( " UPDATE `User` SET " . " `lastLogIn` = ' " . time () . " ' " . " WHERE `UID` = ' " . sql_escape ( $_SESSION [ 'uid' ]) . " ' LIMIT 1; " );
2016-09-30 18:49:33 +02:00
$privileges = privileges_for_user ( $user [ 'UID' ]);
return ;
2016-09-29 11:28:42 +02:00
}
2016-09-30 18:49:33 +02:00
unset ( $_SESSION [ 'uid' ]);
2013-10-13 00:52:44 +02:00
}
2016-09-29 11:28:42 +02:00
2016-09-30 18:49:33 +02:00
// guest privileges
$privileges = privileges_for_group ( - 1 );
2011-06-02 00:48:29 +02:00
}
2016-09-29 11:28:42 +02:00
/**
* generate a salt ( random string ) of arbitrary length suitable for the use with crypt ()
*/
2012-12-12 02:31:54 +01:00
function generate_salt ( $length = 16 ) {
2013-10-13 00:52:44 +02:00
$alphabet = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ " ;
$salt = " " ;
2016-09-29 11:28:42 +02:00
for ( $i = 0 ; $i < $length ; $i ++ ) {
2013-10-13 00:52:44 +02:00
$salt .= $alphabet [ rand ( 0 , strlen ( $alphabet ) - 1 )];
}
return $salt ;
2012-12-12 02:31:54 +01:00
}
2016-09-29 11:28:42 +02:00
/**
* set the password of a user
*/
2012-12-12 02:31:54 +01:00
function set_password ( $uid , $password ) {
2016-11-15 22:08:41 +01:00
$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 ;
2012-12-12 02:31:54 +01:00
}
2016-09-29 11:28:42 +02:00
/**
* verify a password given a precomputed salt .
* if $uid is given and $salt is an old - style salt ( plain md5 ), we convert it automatically
*/
2012-12-12 02:31:54 +01:00
function verify_password ( $password , $salt , $uid = false ) {
2013-10-13 00:52:44 +02:00
$correct = false ;
2016-09-29 11:28:42 +02:00
if ( substr ( $salt , 0 , 1 ) == '$' ) { // new-style crypt()
2013-10-13 00:52:44 +02:00
$correct = crypt ( $password , $salt ) == $salt ;
2016-09-29 11:28:42 +02:00
} elseif ( substr ( $salt , 0 , 7 ) == '{crypt}' ) { // old-style crypt() with DES and static salt - not used anymore
2013-10-13 00:52:44 +02:00
$correct = crypt ( $password , '77' ) == $salt ;
2016-09-29 11:28:42 +02:00
} elseif ( strlen ( $salt ) == 32 ) { // old-style md5 without salt - not used anymore
2013-10-13 00:52:44 +02:00
$correct = md5 ( $password ) == $salt ;
2016-09-29 11:28:42 +02:00
}
2013-10-13 00:52:44 +02:00
if ( $correct && substr ( $salt , 0 , strlen ( CRYPT_ALG )) != CRYPT_ALG && $uid ) {
// 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 " );
}
return $correct ;
2011-06-02 00:48:29 +02:00
}
2011-06-03 15:30:17 +02:00
function privileges_for_user ( $user_id ) {
2016-09-29 11:28:42 +02:00
$privileges = [];
2014-12-28 13:44:56 +01:00
$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 ) . " ' " );
2016-09-29 11:28:42 +02:00
foreach ( $user_privs as $user_priv ) {
2013-10-13 00:52:44 +02:00
$privileges [] = $user_priv [ 'name' ];
2016-09-29 11:28:42 +02:00
}
2013-10-13 00:52:44 +02:00
return $privileges ;
2011-06-03 15:30:17 +02:00
}
function privileges_for_group ( $group_id ) {
2016-09-29 11:28:42 +02:00
$privileges = [];
2014-12-28 13:44:56 +01:00
$groups_privs = sql_select ( " SELECT * FROM `GroupPrivileges` JOIN `Privileges` ON (`GroupPrivileges`.`privilege_id` = `Privileges`.`id`) WHERE `group_id`=' " . sql_escape ( $group_id ) . " ' " );
2016-09-29 11:28:42 +02:00
foreach ( $groups_privs as $guest_priv ) {
2013-10-13 00:52:44 +02:00
$privileges [] = $guest_priv [ 'name' ];
2016-09-29 11:28:42 +02:00
}
2013-10-13 00:52:44 +02:00
return $privileges ;
2011-06-03 15:30:17 +02:00
}
2011-06-03 11:24:22 +02:00
?>