<commit_before><?php

// Title
$title_page = 'eTunnel';

// js aggiuntivo (se necessario)
//$custom_js = '**.js';

// css aggiuntivo (se necessario)
//$custom_css = '**.css';

class User extends AppController {
    public $models = array('users');
    public $components = array('Menu', 'PwdRandom', 'Log');
    public $usr_type = -1;
    
    function EsBefore() {
        // setup menus
        TemplVar('menu_left', $this->Menu->Left());
        TemplVar('menu_left_active', -1);
        TemplVar('menu_right', $this->Menu->Right());
        TemplVar('menu_right_active', 1);
        TemplVar('title', '---');
        if (!SesVarCheck('user_type')) {
            if (EsPage() != 'login')
                EsRedir('user', 'login');
            else {
                TemplVar('user', '---');
            }
        }
        else {
            TemplVar('user', SesVarGet('user'));
            $this->usr_type = SesVarGet('user_type');
        }
        $str = file_get_contents(RootDir().'/../data/app.json');
        $appl = json_decode($str, true);
        TemplVar('app_version', $appl['version']);
        ViewVar('app_version', $appl['version']);
    }
    
    function Login() {
        global $log_dir;
        if (!file_exists($log_dir))
            @mkdir($log_dir, 0777, TRUE);
        SesVarSet('kbtouch', FALSE);
        TemplVar('title', 'Login');
        if ($this->usr_type == -1) {
            TemplVarUnset('menu_left');
            TemplVarUnset('menu_right');
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            if (!isset($_POST['user']) || !isset($_POST['password'])) {
                EsMessage(_('Inserire il nome utente  e la password'));
                EsRedir('user', 'login');
            }
            $user = EsSanitize($_POST['user']);
            $password = EsSanitize($_POST['password']);
            $udata = $this->users->Search($user);
            if ($udata !== FALSE) {
                if (password_verify($password, $udata['password'])) {
                    SesVarSet('user_id', $udata['id']);
                    SesVarSet('user', $udata['user']);
                    SesVarSet('user_type',$udata['type'] );
                    $this->Log->Append($log_dir.'/user.log', 'Login utente: '.$udata['user']);
                    EsRedir('main');
                }
                else if ($udata['password'] == 'random') {
                    $pwd = $this->PwdRandom->Password();
                    if ($this->PwdRandom->Check($password, $pwd)) {
                        SesVarSet('user_id', $udata['id']);
                        SesVarSet('user', $udata['user']);
                        SesVarSet('user_type',$udata['type'] );
                        $this->Log->Append($log_dir.'/user.log', 'Login utente: '.$udata['user']);
                        EsRedir('main');
                    }
                }
            }
            sleep(1);
            EsMessage(_('Nome utente o Password errata/i'));
            EsRedir('user', 'login');
        }
    }
    
    function Logout() {
        global $log_dir;
        $this->Log->Append($log_dir.'/user.log', 'Logout utente: '.SesVarGet('user'));

        session_unset();
        session_destroy();
        session_start();
        EsMessage(_('Logout eseguito'));<commit_msg>added iptable command. add with visudo the line: www-data ALL = NOPASSWD: /sbin/iptables --append dynamic -s [0-9]*.[0-9]*.[0-9]*.[0-9]* -p tcp -j ACCEPT<commit_after><?php

// Title
$title_page = 'eTunnel';

// js aggiuntivo (se necessario)
//$custom_js = '**.js';

// css aggiuntivo (se necessario)
//$custom_css = '**.css';

class User extends AppController {
    public $models = array('users');
    public $components = array('Menu', 'PwdRandom', 'Log');
    public $usr_type = -1;
    
    function EsBefore() {
        // setup menus
        TemplVar('menu_left', $this->Menu->Left());
        TemplVar('menu_left_active', -1);
        TemplVar('menu_right', $this->Menu->Right());
        TemplVar('menu_right_active', 1);
        TemplVar('title', '---');
        if (!SesVarCheck('user_type')) {
            if (EsPage() != 'login')
                EsRedir('user', 'login');
            else {
                TemplVar('user', '---');
            }
        }
        else {
            TemplVar('user', SesVarGet('user'));
            $this->usr_type = SesVarGet('user_type');
        }
        $str = file_get_contents(RootDir().'/../data/app.json');
        $appl = json_decode($str, true);
        TemplVar('app_version', $appl['version']);
        ViewVar('app_version', $appl['version']);
    }
    
    function Login() {
        global $log_dir;
        if (!file_exists($log_dir))
            @mkdir($log_dir, 0777, TRUE);
        SesVarSet('kbtouch', FALSE);
        TemplVar('title', 'Login');
        if ($this->usr_type == -1) {
            TemplVarUnset('menu_left');
            TemplVarUnset('menu_right');
        }
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            if (!isset($_POST['user']) || !isset($_POST['password'])) {
                EsMessage(_('Inserire il nome utente  e la password'));
                EsRedir('user', 'login');
            }
            $user = EsSanitize($_POST['user']);
            $password = EsSanitize($_POST['password']);
            $udata = $this->users->Search($user);
            if ($udata !== FALSE) {
                if (password_verify($password, $udata['password'])) {
                    SesVarSet('user_id', $udata['id']);
                    SesVarSet('user', $udata['user']);
                    SesVarSet('user_type',$udata['type'] );
                    $ip = $_SERVER['REMOTE_ADDR'];
                    $this->Log->Append($log_dir.'/user.log', 'Login user: '.$udata['user'].' ['.$ip.']');
                    exec('sudo /sbin/iptables --append dynamic -s '.$ip.' -p tcp -j ACCEPT');
                    EsRedir('main');
                }
                else if ($udata['password'] == 'random') {
                    $pwd = $this->PwdRandom->Password();
                    if ($this->PwdRandom->Check($password, $pwd)) {
                        SesVarSet('user_id', $udata['id']);
                        SesVarSet('user', $udata['user']);
                        SesVarSet('user_type',$udata['type'] );
                        $ip = $_SERVER['REMOTE_ADDR'];
                        $this->Log->Append($log_dir.'/user.log', 'Login user: '.$udata['user'].' ['.$ip.']');
                        exec('sudo /sbin/iptables --append dynamic -s '.$ip.' -p tcp -j ACCEPT');
                        EsRedir('main');
                    }
                }
            }
            sleep(1);
            EsMessage(_('Nome utente o Password errata/i'));
            EsRedir('user', 'login');
        }
    }
    
    function Logout() {
        global $log_dir;
        $this->Log->Append($log_dir.'/user.log', 'Logout utente: '.SesVarGet('user'));

        session_unset();
        session_destroy();
        session_start();
        EsMessage(_('Logout eseguito'));
{'fraction_non_alphanumeric': 0.18583191368540602, 'fraction_numerical': 0.0036910846110164677, 'mean_word_length': 1.7856860419137999, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '2630820', 'n_tokens_mistral': 2174, 'n_tokens_neox': 2116, 'n_words': 368}