<?php 
  
/*
-----------------------
COOKIE + SESSION + HASH
-----------------------
*/
  
IF (CONFIG('AUT_MODE') == 0){
  
  IF (ISSET($_SESSION['HASH']) && DB::GET_COLUMN(("SELECT COUNT(*) FROM `USERS` WHERE `HASH` = '".ADSL($_SESSION['HASH'])."' LIMIT 1"), 0) == 1) {
    
    $user = DB::GET_STRING("SELECT * FROM `USERS` WHERE `HASH` = '".ADSL($_SESSION['HASH'])."' LIMIT 1");
    
    IF (EMPTY(COOKIE('PASSWORD')) OR EMPTY(COOKIE('USER_ID'))){
      
      SETCOOKIE('USER_ID', $user['ID'], TIME() + 60 * 60 * 24 * 365);
      SETCOOKIE('PASSWORD', ENCRYPT($user['PASSWORD']), $user['ID'], TIME() + 60 * 60 * 24 * 365);
    
    }
  
  }ELSE IF (DB::GET_COLUMN("SELECT COUNT(*) FROM `USERS` WHERE `ID` = '".COOKIE('USER_ID')."' AND `PASSWORD` = '".SHIF(DECRYPT(COOKIE('PASSWORD'), COOKIE('USER_ID'))). "' LIMIT 1") == 1) {
    
    $user = DB::GET_STRING("SELECT * FROM `USERS` WHERE `ID` = '".COOKIE('USER_ID')."' LIMIT 1");
    
    $hash = USER_HASH($user['ID']);
    
    DB::GET_SET("UPDATE `USERS` SET `HASH` = '".$hash."' WHERE `ID` = '".$user['ID']."' LIMIT 1");
    
    $_SESSION['HASH'] = $hash;
  
  }
  
}

/*
-------------------
IP + SESSION + HASH
-------------------
*/

IF (CONFIG('AUT_MODE') == 1){
  
  IF (DB::GET_COLUMN(("SELECT COUNT(*) FROM `USERS` WHERE `IP` = '".IP."' AND `HASH` = '".ADSL($_SESSION['HASH'])."' LIMIT 1"), 0) == 1) {
    
    $user = DB::GET_STRING("SELECT * FROM `USERS` WHERE `HASH` = '".ADSL($_SESSION['HASH'])."' LIMIT 1");
  
  }
  
}

/*
-----------------------------
IP + BROWSER + SESSION + HASH
-----------------------------
*/

IF (CONFIG('AUT_MODE') == 2){
  
  IF (DB::GET_COLUMN(("SELECT COUNT(*) FROM `USERS` WHERE `IP` = '".IP."' AND `BROWSER` = '".USER_BROWSER."' AND `HASH` = '".ADSL($_SESSION['HASH'])."' LIMIT 1"), 0) == 1) {
    
    $user = DB::GET_STRING("SELECT * FROM `USERS` WHERE `HASH` = '".ADSL($_SESSION['HASH'])."' LIMIT 1");
  
  }
  
}

IF (!ISSET($user)){
  
  $user = NULL;
  
}

/*
---------------
Доступ в панель
---------------
*/

IF (ISSET($user) && $user['MANAGEMENT'] == 1){
  
  DEFINE('MANAGEMENT', 1);
  
}ELSE{
  
  DEFINE('MANAGEMENT', 0);

}

/*
------------------------------
Создание настроек пользователя
------------------------------
*/

IF (ISSET($user)){
  
  IF (DB::GET_COLUMN("SELECT COUNT(`ID`) FROM `USERS_SETTINGS` WHERE `USER_ID` = '".$user['ID']."' LIMIT 1") == 1) {
    
    $settings = DB::GET_STRING("SELECT * FROM `USERS_SETTINGS` WHERE `USER_ID` = '".$user['ID']."' LIMIT 1");
  
  }ELSE{
    
    $avatar_rand_param = ARRAY('#EB6156', '#FD8B2C', '#72C375', '#B970C5', '#31ACB8', '#5498CE', '#997445', '#4EA771', '#828D92', '#F55448');
    $avatar_rand = ARRAY_RAND($avatar_rand_param, 1);
    $avatar = $avatar_rand_param[$avatar_rand];
    
    DB::GET_ADD("INSERT INTO `USERS_SETTINGS` (`USER_ID`, `AVATAR_PHONE`) VALUES ('".$user['ID']."', '".$avatar."')");
    
    REDIRECT('/id'.$user['ID'].'');
  
  }
  
}

/*
------------------
Подгрузка плагинов
------------------
*/

$dir_data = OPENDIR(ROOT.'/system/connections/global/users/');

WHILE ($file = READDIR($dir_data))
{
  
  IF (PREG_MATCH('#\.php$#i', $file))
  {
    
    INCLUDE_ONCE(ROOT.'/system/connections/global/users/'.$file);
  
  }

}
  
?>