2017-03-11 02:51:06 +07:00
< ? php
2024-02-26 14:38:04 +07:00
2017-03-11 02:51:06 +07:00
/**
2023-10-12 15:55:42 +07:00
* PHP Mikrotik Billing ( https :// github . com / hotspotbilling / phpnuxbill / )
* by https :// t . me / ibnux
**/
2017-03-11 02:51:06 +07:00
2024-02-26 14:38:04 +07:00
class Admin
{
2024-02-12 09:45:44 +07:00
2024-02-26 14:38:04 +07:00
public static function getID ()
{
2024-10-11 11:07:47 +07:00
global $db_pass , $config , $isApi ;
2024-09-12 11:39:45 +01:00
2024-10-11 11:07:47 +07:00
$enable_session_timeout = $config [ 'enable_session_timeout' ] == 1 ;
$session_timeout_duration = $config [ 'session_timeout_duration' ] ? intval ( $config [ 'session_timeout_duration' ] * 60 ) : intval ( 60 * 60 ); // Convert minutes to seconds
2024-10-11 11:42:38 +07:00
if ( $isApi ) {
2024-10-11 11:07:47 +07:00
$enable_session_timeout = false ;
}
2024-10-11 11:09:27 +07:00
if ( $enable_session_timeout && ! empty ( $_SESSION [ 'aid' ]) && ! empty ( $_SESSION [ 'aid_expiration' ])) {
2024-09-12 11:39:45 +01:00
if ( $_SESSION [ 'aid_expiration' ] > time ()) {
2024-10-10 14:33:27 +01:00
$isValid = self :: validateToken ( $_SESSION [ 'aid' ], $_COOKIE [ 'aid' ]);
if ( ! $isValid ) {
self :: removeCookie ();
_alert ( Lang :: T ( 'Token has expired. Please log in again.' ), 'danger' , " admin " );
return 0 ;
}
2024-10-11 11:07:47 +07:00
// extend timeout duration
$_SESSION [ 'aid_expiration' ] = time () + $session_timeout_duration ;
2024-10-10 14:33:27 +01:00
2024-09-12 11:39:45 +01:00
return $_SESSION [ 'aid' ];
2024-10-11 11:09:27 +07:00
} else {
2024-10-11 11:07:47 +07:00
// Session expired, log out the user
2024-09-12 11:39:45 +01:00
self :: removeCookie ();
_alert ( Lang :: T ( 'Session has expired. Please log in again.' ), 'danger' , " admin " );
return 0 ;
2024-08-01 17:55:58 +07:00
}
2024-10-11 11:09:27 +07:00
} else if ( ! empty ( $_SESSION [ 'aid' ])) {
2024-10-11 11:07:47 +07:00
$isValid = self :: validateToken ( $_SESSION [ 'aid' ], $_COOKIE [ 'aid' ]);
if ( ! $isValid ) {
self :: removeCookie ();
2024-10-25 14:05:57 +07:00
_alert ( Lang :: T ( 'Token has expired. Please log in again.' ) . '.' . $_SESSION [ 'aid' ], 'danger' , " admin " );
2024-10-11 11:07:47 +07:00
return 0 ;
}
return $_SESSION [ 'aid' ];
2024-08-01 17:55:58 +07:00
}
2024-09-12 11:39:45 +01:00
// Check if the cookie is set and valid
2024-07-27 00:56:48 +01:00
elseif ( isset ( $_COOKIE [ 'aid' ])) {
2024-02-26 14:38:04 +07:00
$tmp = explode ( '.' , $_COOKIE [ 'aid' ]);
2024-10-10 14:33:27 +01:00
if ( sha1 ( " $tmp[0] . $tmp[1] . $db_pass " ) == $tmp [ 2 ]) {
// Validate the token in the cookie
$isValid = self :: validateToken ( $tmp [ 0 ], $_COOKIE [ 'aid' ]);
2024-10-11 11:42:38 +07:00
if ( $isApi ) {
// For now API need to always return true, next need to add revoke token API
$isValid = true ;
}
2024-10-10 16:24:36 +01:00
if ( ! empty ( $_COOKIE [ 'aid' ]) && ! $isValid ) {
2024-10-10 14:33:27 +01:00
self :: removeCookie ();
2024-10-25 14:05:57 +07:00
_alert ( Lang :: T ( 'Token has expired. Please log in again.' ) . '..' , 'danger' , " admin " );
2024-10-10 14:33:27 +01:00
return 0 ;
2024-10-10 16:24:36 +01:00
} else {
if ( time () - $tmp [ 1 ] < 86400 * 7 ) {
$_SESSION [ 'aid' ] = $tmp [ 0 ];
if ( $enable_session_timeout ) {
$_SESSION [ 'aid_expiration' ] = time () + $session_timeout_duration ;
}
return $tmp [ 0 ];
2024-07-27 00:56:48 +01:00
}
2024-02-12 09:45:44 +07:00
}
}
}
2024-07-27 00:56:48 +01:00
2024-02-12 09:45:44 +07:00
return 0 ;
}
2024-10-17 09:35:26 +07:00
2024-02-26 14:38:04 +07:00
public static function setCookie ( $aid )
{
2024-10-11 11:07:47 +07:00
global $db_pass , $config ;
2024-07-27 00:56:48 +01:00
$enable_session_timeout = $config [ 'enable_session_timeout' ];
2024-10-09 15:47:41 +01:00
$session_timeout_duration = intval ( $config [ 'session_timeout_duration' ]) * 60 ; // Convert minutes to seconds
2024-02-26 14:38:04 +07:00
if ( isset ( $aid )) {
2024-02-12 09:45:44 +07:00
$time = time ();
2024-10-09 15:47:41 +01:00
$token = $aid . '.' . $time . '.' . sha1 ( " $aid . $time . $db_pass " );
// Detect the current protocol
$isSecure = ! empty ( $_SERVER [ 'HTTPS' ]) && $_SERVER [ 'HTTPS' ] !== 'off' ;
// Set cookie with security flags
setcookie ( 'aid' , $token , [
'expires' => time () + 86400 * 7 , // 7 days
'path' => '/' ,
2024-10-10 16:24:36 +01:00
'domain' => '' ,
2024-10-09 15:47:41 +01:00
'secure' => $isSecure ,
'httponly' => true ,
'samesite' => 'Lax' , // or Strict
]);
2024-07-27 00:56:48 +01:00
$_SESSION [ 'aid' ] = $aid ;
2024-10-09 15:47:41 +01:00
2024-07-27 00:56:48 +01:00
if ( $enable_session_timeout ) {
$_SESSION [ 'aid_expiration' ] = $time + $session_timeout_duration ;
}
2024-10-09 15:47:41 +01:00
2024-10-10 14:33:27 +01:00
self :: upsertToken ( $aid , $token );
2024-04-01 13:01:21 +07:00
return $token ;
2024-02-12 09:45:44 +07:00
}
2024-10-09 15:47:41 +01:00
2024-04-01 13:01:21 +07:00
return '' ;
2024-02-12 09:45:44 +07:00
}
2024-10-10 14:33:27 +01:00
2024-02-26 14:38:04 +07:00
public static function removeCookie ()
{
2024-10-10 14:33:27 +01:00
global $_app_stage ;
2024-02-26 14:38:04 +07:00
if ( isset ( $_COOKIE [ 'aid' ])) {
2024-10-09 15:47:41 +01:00
$isSecure = ! empty ( $_SERVER [ 'HTTPS' ]) && $_SERVER [ 'HTTPS' ] !== 'off' ;
setcookie ( 'aid' , '' , [
'expires' => time () - 3600 ,
'path' => '/' ,
2024-10-10 16:24:36 +01:00
'domain' => '' ,
2024-10-09 15:47:41 +01:00
'secure' => $isSecure ,
'httponly' => true ,
'samesite' => 'Lax' ,
]);
2024-10-11 10:37:35 +07:00
session_destroy ();
2024-10-25 14:05:57 +07:00
session_unset ();
session_start ();
unset ( $_COOKIE [ 'aid' ], $_SESSION [ 'aid' ]);
2024-02-12 09:45:44 +07:00
}
}
2024-02-26 14:38:04 +07:00
public static function _info ( $id = 0 )
{
if ( empty ( $id ) && $id == 0 ) {
2024-02-23 14:40:47 +07:00
$id = Admin :: getID ();
}
2024-02-26 14:38:04 +07:00
if ( $id ) {
2024-02-26 11:25:15 +07:00
return ORM :: for_table ( 'tbl_users' ) -> find_one ( $id );
2024-02-26 14:38:04 +07:00
} else {
2024-02-27 07:12:02 +07:00
return null ;
2024-02-26 11:25:15 +07:00
}
2017-03-11 02:51:06 +07:00
}
2024-10-10 14:33:27 +01:00
public static function upsertToken ( $aid , $token )
{
2024-10-11 11:07:47 +07:00
$query = ORM :: for_table ( 'tbl_users' ) -> findOne ( $aid );
$query -> login_token = sha1 ( $token );
2024-10-10 14:33:27 +01:00
$query -> save ();
}
public static function validateToken ( $aid , $cookieToken )
{
2024-10-18 10:55:07 +07:00
global $config ;
2024-10-11 11:07:47 +07:00
$query = ORM :: for_table ( 'tbl_users' ) -> select ( 'login_token' ) -> findOne ( $aid );
2024-10-25 14:05:57 +07:00
if ( $config [ 'single_session' ] != 'yes' ) {
2024-10-18 10:55:07 +07:00
return true ; // For multi-session, any token is valid
}
2024-10-25 14:05:57 +07:00
if ( empty ( $query )) {
2024-10-18 10:55:07 +07:00
return true ;
}
2024-10-11 11:07:47 +07:00
return $query -> login_token === sha1 ( $cookieToken );
2024-10-10 14:33:27 +01:00
}
2024-02-26 14:38:04 +07:00
}