protected function set_current_user()
 {
     $user = null;
     $AnonymousUser = array('id' => 0, 'level' => 0, 'name' => "Anonymous", 'show_samples' => true, 'language' => '', 'secondary_languages' => '', 'pool_browse_mode' => 1, 'always_resize_images' => true, 'ip_addr' => $this->request()->remoteIp());
     if (!current_user() && $this->session()->user_id) {
         $user = User::where(['id' => $this->session()->user_id])->first();
     } else {
         if ($this->cookies()->login && $this->cookies()->pass_hash) {
             $user = User::authenticate_hash($this->cookies()->login, $this->cookies()->pass_hash);
         } elseif (isset($this->params()->login) && isset($this->params()->password_hash)) {
             $user = User::authenticate($this->params()->login, $this->params()->password_hash);
         } elseif (isset($this->params()->user['name']) && isset($this->params()->user['password'])) {
             $user = User::authenticate($this->params()->user['name'], $this->params()->user['password']);
         }
         $user && $user->updateAttribute('last_logged_in_at', date('Y-m-d H:i:s'));
     }
     if ($user) {
         if ($user->is_blocked() && $user->ban && $user->ban->expires_at < date('Y-m-d H:i:s')) {
             $user->updateAttribute('level', CONFIG()->starting_level);
             Ban::destroyAll("user_id = " . $user->id);
         }
         $this->session()->user_id = $user->id;
     } else {
         $user = new User();
         $user->assignAttributes($AnonymousUser, ['without_protection' => true]);
     }
     User::set_current_user($user);
     $this->current_user = $user;
     # For convenient access in activerecord models
     $user->ip_addr = $this->request()->remoteIp();
     Moebooru\Versioning\Versioning::init_history();
     if (!current_user()->is_anonymous()) {
         current_user()->log($this->request()->remoteIp());
     }
 }
function set_current_user()
{
    $AnonymousUser = array('id' => null, 'level' => 0, 'name' => "Anonymous", 'pretty_name' => "Anonymous", 'is_anonymous' => true, 'show_samples' => true, 'has_avatar' => false, 'language' => '', 'secondary_languages' => '', 'secondary_language_array' => array(), 'ip_addr' => $_SERVER['REMOTE_ADDR'], 'pool_browse_mode' => 1);
    // if(!empty(User::$current)) {
    if (!empty($_SESSION[CONFIG::app_name]['user_id'])) {
        User::$current = User::find($_SESSION[CONFIG::app_name]['user_id']);
    } elseif (isset($_COOKIE['login']) && isset($_COOKIE['pass_hash'])) {
        User::$current = User::authenticate_hash($_COOKIE['login'], $_COOKIE['pass_hash']);
    } elseif (isset(Request::$params->login) && isset(Request::$params->password_hash)) {
        User::$current = User::authenticate(Request::$params->login, Request::$params->password_hash);
    } elseif (isset(Request::$params->user['name']) && isset(Request::$params->user['password'])) {
        User::$current = User::authenticate(Request::$params->user['name'], Request::$params->user['password']);
    }
    // vde(User::$current);
    if (User::$current) {
        # TODO:
        // if(User::$current->is_blocked && User::$current->ban && User::$current->ban->expires_at < gmd()) {
        // User::$current->update_attribute(array('level'->CONFIG["starting_level"]));
        // Ban::destroy_all("user_id = #{@current_user.id}")
        // }
    } else {
        User::$current = User::create_from_array($AnonymousUser);
    }
    // User::$current = new User('from_array', $AnonymousUser);
    // vde(User::$current);
}