<?php

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

/**
 * Description of login
 *
 * @author Igor Ratsun
 */
class Login extends MX_Controller {

    public function __construct() {
        // Get observers
        $this->load->config('ciext/template');
        $this->load->library('ciext/observers/login/login_observer');
        $this->load->library('ciext/observers/login/last_logged_observer');

        // Add observers to subjet
        $this->load->library('ciext/observers/login/login_subject');
        $this->login_subject->attach($this->login_observer);
        $this->login_subject->attach($this->last_logged_observer);
    }

    public function index() {
        // Browser check
        if ($this->config->item('check_browser') == TRUE) {
            $this->load->library('ciext/browser');
            $this->browser->check_browser();
        }

        $this->lang->load('ciext/admin');
        $this->load->helper('form');

        $this->load->library('ciext/ciext');
        $this->lang->load('ciext/login');

        $id_user = $this->rt_user->id_user;
        if (!empty($id_user)) {
            redirect(base_url($this->config->item('admin_page')));
        }

        $content = array_merge(array(
            'error_msg' => '',
            'form_open' => form_open($this->config->item('admin_page') . '/login'),
            'form_close' => form_close(),
            'login' => $this->input->post('login'),
                ), $this->lang->language);

        if (!empty($_POST)) {
            $hash = $this->session->userdata('form_hash');
            $id_user = 0;
            if ($hash == $this->input->post('hash')) {
                $id_user = $this->rt_user->login($this->input->post('login', TRUE), $this->input->post('password'));
            }

            // Notify
            $this->login_subject->is_logged = ($id_user !== 0) ? TRUE : FALSE;
            $this->login_subject->user_name = $this->input->post('login', TRUE);
            $this->login_subject->notify();

            if ($id_user == 0) {
                $content['error_msg'] = '<div class="alert alert-error"><span>' . $this->lang->line('msg_incorrect_pass') . '</span></div>';
            } else {
                // Set user cookie if needed
                if (!empty($_POST['remember'])) {
                    $this->rt_user->set_user_cookie();
                }

                $redirect_page = (strpos($this->session->userdata('http_referer'), base_url()) !== FALSE) ?
                        $this->session->userdata('http_referer') :
                        base_url($this->config->item('admin_page'));

                redirect($redirect_page);
            }
        }

        $content['hash'] = md5(md5(mktime()) . $this->config->item('encryption_key'));
        $this->session->set_userdata('form_hash', $content['hash']);

        // Set referer
        if (!empty($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != base_url() . 'admin/install/finish') {
            $this->session->set_userdata('http_referer', $_SERVER['HTTP_REFERER']);
        }
        
        $content['admin_footer'] = $this->config->item('admin_footer');

        $this->parser->parse('ciext/login', $content);
    }

}
