2010-02-28 13:04:07 +00:00
< ? php
2015-07-13 20:10:26 +02:00
2016-08-18 20:28:22 -05:00
function authenticate ( $username , $password )
{
2015-07-13 20:10:26 +02:00
$encrypted_old = md5 ( $password );
2015-09-30 15:20:06 +00:00
$row = dbFetchRow ( 'SELECT username,password FROM `users` WHERE `username`= ?' , array ( $username ), true );
2015-07-13 20:10:26 +02:00
if ( $row [ 'username' ] && $row [ 'username' ] == $username ) {
// Migrate from old, unhashed password
if ( $row [ 'password' ] == $encrypted_old ) {
$row_type = dbFetchRow ( 'DESCRIBE users password' );
if ( $row_type [ 'Type' ] == 'varchar(34)' ) {
changepassword ( $username , $password );
}
return 1 ;
2016-08-18 20:28:22 -05:00
} elseif ( substr ( $row [ 'password' ], 0 , 3 ) == '$1$' ) {
2015-07-13 20:10:26 +02:00
$row_type = dbFetchRow ( 'DESCRIBE users password' );
if ( $row_type [ 'Type' ] == 'varchar(60)' ) {
if ( $row [ 'password' ] == crypt ( $password , $row [ 'password' ])) {
changepassword ( $username , $password );
}
}
}
$hasher = new PasswordHash ( 8 , false );
if ( $hasher -> CheckPassword ( $password , $row [ 'password' ])) {
return 1 ;
2014-02-03 10:45:34 +00:00
}
2015-07-13 20:10:26 +02:00
} //end if
return 0 ;
} //end authenticate()
2016-08-18 20:28:22 -05:00
function reauthenticate ( $sess_id , $token )
{
2015-07-13 20:10:26 +02:00
list ( $uname , $hash ) = explode ( '|' , $token );
2015-09-30 15:20:06 +00:00
$session = dbFetchRow ( " SELECT * FROM `session` WHERE `session_username` = ' $uname ' AND session_value=' $sess_id ' " , array (), true );
2015-07-13 20:10:26 +02:00
$hasher = new PasswordHash ( 8 , false );
if ( $hasher -> CheckPassword ( $uname . $session [ 'session_token' ], $hash )) {
$_SESSION [ 'username' ] = $uname ;
return 1 ;
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
return 0 ;
2011-03-19 20:23:23 +00:00
}
2015-07-13 20:10:26 +02:00
} //end reauthenticate()
2016-08-18 20:28:22 -05:00
function passwordscanchange ( $username = '' )
{
2015-07-13 20:10:26 +02:00
/*
* By default allow the password to be modified , unless the existing
* user is explicitly prohibited to do so .
*/
if ( empty ( $username ) || ! user_exists ( $username )) {
return 1 ;
2016-08-18 20:28:22 -05:00
} else {
2015-09-30 15:20:06 +00:00
return dbFetchCell ( 'SELECT can_modify_passwd FROM users WHERE username = ?' , array ( $username ), true );
2015-07-13 20:10:26 +02:00
}
} //end passwordscanchange()
2010-03-06 00:00:05 +00:00
2011-03-19 20:23:23 +00:00
/**
* From : http :// code . activestate . com / recipes / 576894 - generate - a - salt /
* This function generates a password salt as a string of x ( default = 15 ) characters
* ranging from a - zA - Z0 - 9.
* @ param $max integer The number of characters in the string
* @ author AfroSoft < scripts @ afrosoft . co . cc >
*/
2016-08-18 20:28:22 -05:00
function generateSalt ( $max = 15 )
{
2015-07-13 20:10:26 +02:00
$characterList = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' ;
$i = 0 ;
$salt = '' ;
do {
$salt .= $characterList { mt_rand ( 0 , strlen ( $characterList ))};
$i ++ ;
} while ( $i <= $max );
return $salt ;
} //end generateSalt()
2016-08-18 20:28:22 -05:00
function changepassword ( $username , $password )
{
2015-07-13 20:10:26 +02:00
$hasher = new PasswordHash ( 8 , false );
2014-02-03 10:45:34 +00:00
$encrypted = $hasher -> HashPassword ( $password );
2015-07-13 20:10:26 +02:00
return dbUpdate ( array ( 'password' => $encrypted ), 'users' , '`username` = ?' , array ( $username ));
} //end changepassword()
2016-08-18 20:28:22 -05:00
function auth_usermanagement ()
{
2015-07-13 20:10:26 +02:00
return 1 ;
} //end auth_usermanagement()
2016-08-18 20:28:22 -05:00
function adduser ( $username , $password , $level , $email = '' , $realname = '' , $can_modify_passwd = 1 , $description = '' , $twofactor = 0 )
{
2015-07-13 20:10:26 +02:00
if ( ! user_exists ( $username )) {
$hasher = new PasswordHash ( 8 , false );
$encrypted = $hasher -> HashPassword ( $password );
2015-11-21 11:40:24 +00:00
$userid = dbInsert ( array ( 'username' => $username , 'password' => $encrypted , 'level' => $level , 'email' => $email , 'realname' => $realname , 'can_modify_passwd' => $can_modify_passwd , 'descr' => $description , 'twofactor' => $twofactor ), 'users' );
if ( $userid == false ) {
return false ;
2016-08-18 20:28:22 -05:00
} else {
foreach ( dbFetchRows ( 'select notifications.* from notifications where not exists( select 1 from notifications_attribs where notifications.notifications_id = notifications_attribs.notifications_id and notifications_attribs.user_id = ?) order by notifications.notifications_id desc' , array ( $userid )) as $notif ) {
dbInsert ( array ( 'notifications_id' => $notif [ 'notifications_id' ], 'user_id' => $userid , 'key' => 'read' , 'value' => 1 ), 'notifications_attribs' );
2015-11-21 11:40:24 +00:00
}
}
return $userid ;
2016-08-18 20:28:22 -05:00
} else {
2015-07-13 20:10:26 +02:00
return false ;
}
} //end adduser()
2016-08-18 20:28:22 -05:00
function user_exists ( $username )
{
2015-09-30 15:20:06 +00:00
$return = @ dbFetchCell ( 'SELECT COUNT(*) FROM users WHERE username = ?' , array ( $username ), true );
2015-07-13 20:10:26 +02:00
return $return ;
} //end user_exists()
2016-08-18 20:28:22 -05:00
function get_userlevel ( $username )
{
2015-09-30 15:20:06 +00:00
return dbFetchCell ( 'SELECT `level` FROM `users` WHERE `username` = ?' , array ( $username ), true );
2015-07-13 20:10:26 +02:00
} //end get_userlevel()
2016-08-18 20:28:22 -05:00
function get_userid ( $username )
{
2015-09-30 15:20:06 +00:00
return dbFetchCell ( 'SELECT `user_id` FROM `users` WHERE `username` = ?' , array ( $username ), true );
2015-07-13 20:10:26 +02:00
} //end get_userid()
2016-08-18 20:28:22 -05:00
function deluser ( $username )
{
2015-07-13 20:10:26 +02:00
dbDelete ( 'bill_perms' , '`user_name` = ?' , array ( $username ));
dbDelete ( 'devices_perms' , '`user_name` = ?' , array ( $username ));
dbDelete ( 'ports_perms' , '`user_name` = ?' , array ( $username ));
dbDelete ( 'users_prefs' , '`user_name` = ?' , array ( $username ));
dbDelete ( 'users' , '`user_name` = ?' , array ( $username ));
return dbDelete ( 'users' , '`username` = ?' , array ( $username ));
} //end deluser()
2016-08-18 20:28:22 -05:00
function get_userlist ()
{
2015-07-13 20:10:26 +02:00
return dbFetchRows ( 'SELECT * FROM `users`' );
} //end get_userlist()
2016-08-18 20:28:22 -05:00
function can_update_users ()
{
2015-07-13 20:10:26 +02:00
// supported so return 1
return 1 ;
} //end can_update_users()
2016-08-18 20:28:22 -05:00
function get_user ( $user_id )
{
2015-09-30 15:20:06 +00:00
return dbFetchRow ( 'SELECT * FROM `users` WHERE `user_id` = ?' , array ( $user_id ), true );
2015-07-13 20:10:26 +02:00
} //end get_user()
2016-08-18 20:28:22 -05:00
function update_user ( $user_id , $realname , $level , $can_modify_passwd , $email )
{
2015-07-13 20:10:26 +02:00
dbUpdate ( array ( 'realname' => $realname , 'level' => $level , 'can_modify_passwd' => $can_modify_passwd , 'email' => $email ), 'users' , '`user_id` = ?' , array ( $user_id ));
} //end update_user()