<commit_before><?php

if (!defined('DEBUG_MODE')) { die(); }

define('MAX_PER_SOURCE', 100);
define('DEFAULT_PER_SOURCE', 20);
define('DEFAULT_SINCE', 'today');

require APP_PATH.'modules/core/functions.php';

/* INPUT */

class Hm_Handler_close_session_early extends Hm_Handler_Module {
    public function process() {
        $this->session->close_early();
    }
}

class Hm_Handler_http_headers extends Hm_Handler_Module {
    public function process() {
        $headers = array();
        if ($this->get('language')) {
            $headers[] = 'Content-Language: '.substr($this->get('language'), 0, 2);
        }
        if ($this->request->tls) {
            $headers[] = 'Strict-Transport-Security: max-age=31536000';
        }
        $headers[] = 'X-XSS-Protection: 1; mode=block';
        $headers[] = 'X-Content-Type-Options: nosniff';
        $headers[] = 'Expires: '.gmdate('D, d M Y H:i:s \G\M\T', strtotime('-1 year'));
        $headers[] = "Content-Security-Policy: default-src 'none'; script-src 'self' 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline';";
        if ($this->request->type == 'AJAX') {
            $headers[] = 'Content-Type: application/json';
        }
        $this->out('http_headers', $headers);
    }
}

class Hm_Handler_process_list_style_setting extends Hm_Handler_Module {
    public function process() {
        list($success, $form) = $this->process_form(array('save_settings', 'list_style'));
        $new_settings = $this->get('new_user_settings', array());
        $settings = $this->get('user_settings', array());

        if ($success) {
            if (in_array($form['list_style'], array('email_style', 'news_style'))) {
                $new_settings['list_style'] = $form['list_style'];
            }
            else {
                $settings['list_style'] = $this->user_config->get('list_style', false);
            }
        }
        else {
            $settings['list_style'] = $this->user_config->get('list_style', false);
        }
        $this->out('new_user_settings', $new_settings, false);
        $this->out('user_settings', $settings, false);
    }
}

class Hm_Handler_process_unread_source_max_setting extends Hm_Handler_Module {
    public function process() {
        list($success, $form) = $this->process_form(array('save_settings', 'unread_per_source'));
        $new_settings = $this->get('new_user_settings', array());
        $settings = $this->get('user_settings', array());

        if ($success) {
            if ($form['unread_per_source'] > MAX_PER_SOURCE || $form['unread_per_source'] < 0) {
                $sources = DEFAULT_PER_SOURCE;
            }
            else {
                $sources = $form['unread_per_source'];
            }
            $new_settings['unread_per_source_setting'] = $sources;
        }
        else {
            $settings['unread_per_source'] = $this->user_config->get('unread_per_source_setting', DEFAULT_PER_SOURCE);
        }
        $this->out('new_user_settings', $new_settings, false);
        $this->out('user_settings', $settings, false);
    }
}

class Hm_Handler_process_all_source_max_setting extends Hm_Handler_Module {
    public function process() {
        list($success, $form) = $this->process_form(array('save_settings', 'all_per_source'));
        $new_settings = $this->get('new_user_settings', array());
        $settings = $this->get('user_settings', array());

        if ($success) {
            if ($form['all_per_source'] > MAX_PER_SOURCE || $form['all_per_source'] < 0) {
                $sources = DEFAULT_PER_SOURCE;
            }
            else {
                $sources = $form['all_per_source'];
            }
            $new_settings['all_per_source_setting'] = $sources;
        }
        else {
            $settings['all_per_source'] = $this->user_config->get('all_per_source_setting', DEFAULT_PER_SOURCE);
        }
        $this->out('new_user_settings', $new_settings, false);
        $this->out('user_settings', $settings, false);
    }
}

class Hm_Handler_process_flagged_source_max_setting extends Hm_Handler_Module {
    public function process() {
        list($success, $form) = $this->process_form(array('save_settings', 'flagged_per_source'));
        $new_settings = $this->get('new_user_settings', array());
        $settings = $this->get('user_settings', array());

        if ($success) {
            if ($form['flagged_per_source'] > MAX_PER_SOURCE || $form['flagged_per_source'] < 0) {
                $sources = DEFAULT_PER_SOURCE;
            }
            else {
                $sources = $form['flagged_per_source'];
            }
            $new_settings['flagged_per_source_setting'] = $sources;
        }
        else {
            $settings['flagged_per_source'] = $this->user_config->get('flagged_per_source_setting', DEFAULT_PER_SOURCE);
        }
        $new_settings = $this->get('new_user_settings', array());
        $settings = $this->get('user_settings', array());
    }
}

class Hm_Handler_process_flagged_since_setting extends Hm_Handler_Module {
    public function process() {
        list($success, $form) = $this->process_form(array('save_settings', 'flagged_since'));
        $new_settings = $this->get('new_user_settings', array());
        $settings = $this->get('user_settings', array());

        if ($success) {
            $new_settings['flagged_since_setting'] = process_since_argument($form['flagged_since'], true);
        }
        else {
            $settings['flagged_since'] = $this->user_config->get('flagged_since_setting', false);
        }
        $this->out('new_user_settings', $new_settings, false);
        $this->out('user_settings', $settings, false);
    }
}

class Hm_Handler_process_all_since_setting extends Hm_Handler_Module {
    public function process() {
        list($success, $form) = $this->process_form(array('save_settings', 'all_since'));
        $new_settings = $this->get('new_user_settings', array());
        $settings = $this->get('user_settings', array());

        if ($success) {
            $new_settings['all_since_setting'] = process_since_argument($form['all_since'], true);
        }
        else {
            $settings['all_since'] = $this->user_config->get('all_since_setting', false);
        }
        $this->out('new_user_settings', $new_settings, false);
        $this->out('user_settings', $settings, false);
    }
}

class Hm_Handler_process_unread_since_setting extends Hm_Handler_Module {
    public function process() {
        list($success, $form) = $this->process_form(array('save_settings', 'unread_since'));
        $new_settings = $this->get('new_user_settings', array());
        $settings = $this->get('user_settings', array());

        if ($success) {
            $new_settings['unread_since_setting'] = process_since_argument($form['unread_since'], true);
        }
        else {
            $settings['unread_since'] = $this->user_config->get('unread_since_setting', false);
        }
        $this->out('new_user_settings', $new_settings, false);
        $this->out('user_settings', $settings, false);
    }
}

class Hm_Handler_process_language_setting extends Hm_Handler_Module {
    public function process() {
        list($success, $form) = $this->process_form(array('save_settings', 'language_setting'));
        $new_settings = $this->get('new_user_settings', array());
        $settings = $this->get('user_settings', array());

        if ($success) {
            $new_settings['language_setting'] = $form['language_setting'];
        }
        else {
            $settings['language'] = $this->user_config->get('language_setting', false);
        }
        $this->out('new_user_settings', $new_settings, false);
        $this->out('user_settings', $settings, false);
    }
}

class Hm_Handler_process_timezone_setting extends Hm_Handler_Module {
    public function process() {
        list($success, $form) = $this->process_form(array('save_settings', 'timezone_setting'));
        $new_settings = $this->get('new_user_settings', array());
        $settings = $this->get('user_settings', array());

        if ($success) {
            $new_settings['timezone_setting'] = $form['timezone_setting'];
        }
        else {
            $settings['timezone'] = $this->user_config->get('timezone_setting', false);
        }
        $this->out('new_user_settings', $new_settings, false);
        $this->out('user_settings', $settings, false);
    }
}

class Hm_Handler_save_user_settings extends Hm_Handler_Module {
    public function process() {
        list($success, $form) = $this->process_form(array('save_settings', 'password'));
        if ($success) {
            if ($new_settings = $this->get('new_user_settings', false)) {
                foreach ($new_settings as $name => $value) {
                    $this->user_config->set($name, $value);
                }
                $user = $this->session->get('username', false);
                $path = $this->config->get('user_settings_dir', false);

                if ($this->get('new_password', false)) {
                    $pass = $this->get('new_password');
                }
                elseif ($this->session->auth($user, $form['password'])) {
                    $pass = $form['password'];
                }
                else {
                    Hm_Msgs::add('ERRIncorrect password, could not save settings to the server');
                    /* TODO: save current settings in session */
                    $pass = false;
                }
                if ($user && $path && $pass) {
                    $this->user_config->save($user, $pass);
                    Hm_Msgs::add('Settings saved');
                    $this->out('reload_folders', true, false);<commit_msg>bug fix in save settings<commit_after><?php

if (!defined('DEBUG_MODE')) { die(); }

define('MAX_PER_SOURCE', 100);
define('DEFAULT_PER_SOURCE', 20);
define('DEFAULT_SINCE', 'today');

require APP_PATH.'modules/core/functions.php';

/* INPUT */

class Hm_Handler_close_session_early extends Hm_Handler_Module {
    public function process() {
        $this->session->close_early();
    }
}

class Hm_Handler_http_headers extends Hm_Handler_Module {
    public function process() {
        $headers = array();
        if ($this->get('language')) {
            $headers[] = 'Content-Language: '.substr($this->get('language'), 0, 2);
        }
        if ($this->request->tls) {
            $headers[] = 'Strict-Transport-Security: max-age=31536000';
        }
        $headers[] = 'X-XSS-Protection: 1; mode=block';
        $headers[] = 'X-Content-Type-Options: nosniff';
        $headers[] = 'Expires: '.gmdate('D, d M Y H:i:s \G\M\T', strtotime('-1 year'));
        $headers[] = "Content-Security-Policy: default-src 'none'; script-src 'self' 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline';";
        if ($this->request->type == 'AJAX') {
            $headers[] = 'Content-Type: application/json';
        }
        $this->out('http_headers', $headers);
    }
}

class Hm_Handler_process_list_style_setting extends Hm_Handler_Module {
    public function process() {
        list($success, $form) = $this->process_form(array('save_settings', 'list_style'));
        $new_settings = $this->get('new_user_settings', array());
        $settings = $this->get('user_settings', array());

        if ($success) {
            if (in_array($form['list_style'], array('email_style', 'news_style'))) {
                $new_settings['list_style'] = $form['list_style'];
            }
            else {
                $settings['list_style'] = $this->user_config->get('list_style', false);
            }
        }
        else {
            $settings['list_style'] = $this->user_config->get('list_style', false);
        }
        $this->out('new_user_settings', $new_settings, false);
        $this->out('user_settings', $settings, false);
    }
}

class Hm_Handler_process_unread_source_max_setting extends Hm_Handler_Module {
    public function process() {
        list($success, $form) = $this->process_form(array('save_settings', 'unread_per_source'));
        $new_settings = $this->get('new_user_settings', array());
        $settings = $this->get('user_settings', array());

        if ($success) {
            if ($form['unread_per_source'] > MAX_PER_SOURCE || $form['unread_per_source'] < 0) {
                $sources = DEFAULT_PER_SOURCE;
            }
            else {
                $sources = $form['unread_per_source'];
            }
            $new_settings['unread_per_source_setting'] = $sources;
        }
        else {
            $settings['unread_per_source'] = $this->user_config->get('unread_per_source_setting', DEFAULT_PER_SOURCE);
        }
        $this->out('new_user_settings', $new_settings, false);
        $this->out('user_settings', $settings, false);
    }
}

class Hm_Handler_process_all_source_max_setting extends Hm_Handler_Module {
    public function process() {
        list($success, $form) = $this->process_form(array('save_settings', 'all_per_source'));
        $new_settings = $this->get('new_user_settings', array());
        $settings = $this->get('user_settings', array());

        if ($success) {
            if ($form['all_per_source'] > MAX_PER_SOURCE || $form['all_per_source'] < 0) {
                $sources = DEFAULT_PER_SOURCE;
            }
            else {
                $sources = $form['all_per_source'];
            }
            $new_settings['all_per_source_setting'] = $sources;
        }
        else {
            $settings['all_per_source'] = $this->user_config->get('all_per_source_setting', DEFAULT_PER_SOURCE);
        }
        $this->out('new_user_settings', $new_settings, false);
        $this->out('user_settings', $settings, false);
    }
}

class Hm_Handler_process_flagged_source_max_setting extends Hm_Handler_Module {
    public function process() {
        list($success, $form) = $this->process_form(array('save_settings', 'flagged_per_source'));
        $new_settings = $this->get('new_user_settings', array());
        $settings = $this->get('user_settings', array());

        if ($success) {
            if ($form['flagged_per_source'] > MAX_PER_SOURCE || $form['flagged_per_source'] < 0) {
                $sources = DEFAULT_PER_SOURCE;
            }
            else {
                $sources = $form['flagged_per_source'];
            }
            $new_settings['flagged_per_source_setting'] = $sources;
        }
        else {
            $settings['flagged_per_source'] = $this->user_config->get('flagged_per_source_setting', DEFAULT_PER_SOURCE);
        }
        $new_settings = $this->get('new_user_settings', array());
        $settings = $this->get('user_settings', array());
    }
}

class Hm_Handler_process_flagged_since_setting extends Hm_Handler_Module {
    public function process() {
        list($success, $form) = $this->process_form(array('save_settings', 'flagged_since'));
        $new_settings = $this->get('new_user_settings', array());
        $settings = $this->get('user_settings', array());

        if ($success) {
            $new_settings['flagged_since_setting'] = process_since_argument($form['flagged_since'], true);
        }
        else {
            $settings['flagged_since'] = $this->user_config->get('flagged_since_setting', false);
        }
        $this->out('new_user_settings', $new_settings, false);
        $this->out('user_settings', $settings, false);
    }
}

class Hm_Handler_process_all_since_setting extends Hm_Handler_Module {
    public function process() {
        list($success, $form) = $this->process_form(array('save_settings', 'all_since'));
        $new_settings = $this->get('new_user_settings', array());
        $settings = $this->get('user_settings', array());

        if ($success) {
            $new_settings['all_since_setting'] = process_since_argument($form['all_since'], true);
        }
        else {
            $settings['all_since'] = $this->user_config->get('all_since_setting', false);
        }
        $this->out('new_user_settings', $new_settings, false);
        $this->out('user_settings', $settings, false);
    }
}

class Hm_Handler_process_unread_since_setting extends Hm_Handler_Module {
    public function process() {
        list($success, $form) = $this->process_form(array('save_settings', 'unread_since'));
        $new_settings = $this->get('new_user_settings', array());
        $settings = $this->get('user_settings', array());

        if ($success) {
            $new_settings['unread_since_setting'] = process_since_argument($form['unread_since'], true);
        }
        else {
            $settings['unread_since'] = $this->user_config->get('unread_since_setting', false);
        }
        $this->out('new_user_settings', $new_settings, false);
        $this->out('user_settings', $settings, false);
    }
}

class Hm_Handler_process_language_setting extends Hm_Handler_Module {
    public function process() {
        list($success, $form) = $this->process_form(array('save_settings', 'language_setting'));
        $new_settings = $this->get('new_user_settings', array());
        $settings = $this->get('user_settings', array());

        if ($success) {
            $new_settings['language_setting'] = $form['language_setting'];
        }
        else {
            $settings['language'] = $this->user_config->get('language_setting', false);
        }
        $this->out('new_user_settings', $new_settings, false);
        $this->out('user_settings', $settings, false);
    }
}

class Hm_Handler_process_timezone_setting extends Hm_Handler_Module {
    public function process() {
        list($success, $form) = $this->process_form(array('save_settings', 'timezone_setting'));
        $new_settings = $this->get('new_user_settings', array());
        $settings = $this->get('user_settings', array());

        if ($success) {
            $new_settings['timezone_setting'] = $form['timezone_setting'];
        }
        else {
            $settings['timezone'] = $this->user_config->get('timezone_setting', false);
        }
        $this->out('new_user_settings', $new_settings, false);
        $this->out('user_settings', $settings, false);
    }
}

class Hm_Handler_save_user_settings extends Hm_Handler_Module {
    public function process() {
        list($success, $form) = $this->process_form(array('save_settings', 'password'));
        if ($success) {
            if ($new_settings = $this->get('new_user_settings', array())) {
                foreach ($new_settings as $name => $value) {
                    $this->user_config->set($name, $value);
                }
                $user = $this->session->get('username', false);
                $path = $this->config->get('user_settings_dir', false);

                if ($this->get('new_password', false)) {
                    $pass = $this->get('new_password');
                }
                elseif ($this->session->auth($user, $form['password'])) {
                    $pass = $form['password'];
                }
                else {
                    Hm_Msgs::add('ERRIncorrect password, could not save settings to the server');
                    /* TODO: save current settings in session */
                    $pass = false;
                }
                if ($user && $path && $pass) {
                    $this->user_config->save($user, $pass);
                    Hm_Msgs::add('Settings saved');
                    $this->out('reload_folders', true, false);
{'fraction_non_alphanumeric': 0.15824175824175823, 'fraction_numerical': 0.0020444671607462305, 'mean_word_length': 2.744688995215311, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 189, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1040318', 'n_tokens_mistral': 5865, 'n_tokens_neox': 5744, 'n_words': 945}