<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class action extends MX_Controller
{

    protected $redirect = '';

    public function __construct()
    {
        parent::__construct();

        // CSRF check
        if ($this->input->post($this->security->get_csrf_token_name())) {
            $this->security->csrf_verify();
        }

        $this->redirect = (!empty($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : '/';
    }

    public function login()
    {
        if ($this->input->post('login')) {
            $id_user = $this->rt_user->login($this->input->post('login'), $this->input->post('password'));
            if (empty($id_user)) {
                $this->session->set_flashdata('login_widget_dialog_status', 'true');
                $this->session->set_userdata('login_widget_dialog_login', $this->input->post('login', TRUE));
            }
        }

        redirect($this->redirect);
    }

    public function logout()
    {
        $this->rt_user->logout();
        redirect('/');
    }

    public function forgot_password()
    {
        if ($this->input->post('email')) {
            $this->session->set_flashdata('forgot_password_message', TRUE);
            $this->load->model('user/user_model_site');
            $id_user = $this->user_model_site->getIdUserByEmail($this->input->post('email'));
            if (!empty($id_user)) {
                $this->user_model_site->sendRecoveryEmail($id_user);
            } else {
                $this->session->set_flashdata('forgot_password_error', TRUE);
            }
            $this->session->set_flashdata('forgot_password_email', $this->input->post('email'));
        }

        redirect($this->redirect);
    }

    public function loggin_by_hash($hash = array())
    {
        $this->load->model('user/user_model_site');

        $data = explode('_', $hash);

        if (empty($data[0]) || empty($hash) || $hash != $this->user_model_site->makeHash($data[0])) {

            show_404();
        }

        $this->session->set_userdata('id_user', $data[0]);

        redirect(base_url());
    }

    public function registration_confirm($hash)
    {
        $this->load->model('user/user_model_site');

        $data = explode('_', $hash);

        if (empty($data[0]) || empty($hash) || $hash != $this->user_model_site->makeHash($data[0])) {

            show_404();
        }

        $this->load->model('user/user_register_model');

        if ($this->user_register_model->confirm_registration((int) $data[0])) {
            $this->session->set_flashdata('registration_confirm', TRUE);
            redirect($this->config->item('registration_page'));
        }

        show_404();
    }

    public function change_user_info()
    {
        $id_user = $this->rt_user->id_user;
        if (empty($id_user)) {

            show_404();
        }

        if ($this->input->post()) {
            $this->load->model('user/user_register_model');
            $this->user_register_model->update_current_user();
        }

        redirect($this->redirect);
    }

}
