<?php

class ControllerCommonLogin extends Controller
{
    private $error = array();

    public function index()
    {
        $this->language->load('common/login');

        $this->document->setTitle($this->language->get('heading_title'));

        if ($this->user->isLogged() && isset($this->request->get['token']) && ($this->request->get['token'] == $this->session->data['token'])) {
            $this->redirect($this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'));
        }

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
            $this->session->data['token'] = md5(mt_rand());

            if (isset($this->request->post['redirect'])) {
                $this->redirect($this->request->post['redirect'] . '&token=' . $this->session->data['token']);
            } else {
                $this->redirect($this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'));
            }
        }

        $this->data['heading_title'] = $this->language->get('heading_title');
        $this->data['text_login'] = $this->language->get('text_login');
        $this->data['text_forgotten'] = $this->language->get('text_forgotten');

        $this->data['entry_username'] = $this->language->get('entry_username');
        $this->data['entry_password'] = $this->language->get('entry_password');
        $this->data['entry_captcha_placeholder'] = $this->language->get('entry_captcha_placeholder');

        $this->data['button_login'] = $this->language->get('button_login');

        if ((isset($this->session->data['token']) && !isset($this->request->get['token'])) || ((isset($this->request->get['token']) && (isset($this->session->data['token']) && ($this->request->get['token'] != $this->session->data['token']))))) {
            $this->error['warning'] = $this->language->get('error_token');
        }

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } elseif (isset($this->request->get['code'])) {
            $this->data['error_warning'] = $this->language->get('error_code');
        } else {
            $this->data['error_warning'] = '';
        }

        if (isset($this->error['captcha'])) {
            $this->data['error_captcha'] = $this->error['captcha'];
        } else {
            $this->data['error_captcha'] = '';
        }

        if (isset($this->error['google_captcha'])) {
            $this->data['error_google_captcha'] = $this->error['google_captcha'];
        } else {
            $this->data['error_google_captcha'] = '';
        }

        $this->data['action'] = $this->url->link('common/login', '', 'SSL');

        if (isset($this->request->post['username'])) {
            $this->data['username'] = $this->request->post['username'];
        } else {
            $this->data['username'] = '';
        }

        if (isset($this->request->post['password'])) {
            $this->data['password'] = $this->request->post['password'];
        } else {
            $this->data['password'] = '';
        }

        if (isset($this->request->post['captcha'])) {
            $this->data['captcha'] = $this->request->post['captcha'];
        } else {
            $this->data['captcha'] = '';
        }

        if (isset($this->request->get['route'])) {
            $route = $this->request->get['route'];

            unset($this->request->get['route']);

            if (isset($this->request->get['token'])) {
                unset($this->request->get['token']);
            }

            $url = '';

            if ($this->request->get) {
                $url .= http_build_query($this->request->get);
            }

            $this->data['redirect'] = $this->url->link($route, $url, 'SSL');
        } else {
            $this->data['redirect'] = '';
        }

        $this->data['forgotten'] = $this->url->link('common/forgotten', '', 'SSL');

        $this->data['site_key'] = $this->config->get('google_recaptcha_site_key');
        $this->data['google_captcha'] =
            ($this->data['site_key'] && $this->config->get('google_recaptcha_secret_key') && $this->config->get('wg24_google_recaptcha_status'))
            && (isset($this->session->data['admin_login_trys']) && $this->session->data['admin_login_trys'] > 0);
        $this->data['default_captcha'] = !$this->data['google_captcha'] && (isset($this->session->data['admin_login_trys']) && $this->session->data['admin_login_trys'] > 0);

        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render('common/login'));
    }

    protected function validate()
    {
        if ($this->config->get('google_recaptcha_site_key') && $this->config->get('google_recaptcha_secret_key') && $this->config->get('wg24_google_recaptcha_status')
            && (isset($this->session->data['admin_login_trys']) && $this->session->data['admin_login_trys'] > 0)) {
            $recaptcha = $this->ftrgeralregistry->validateGoogleCaptha($this->config->get('google_recaptcha_secret_key'), isset($this->request->post['g-recaptcha-response']) ? $this->request->post['g-recaptcha-response'] : '', $this->request->server['REMOTE_ADDR']);

            if (!$recaptcha["success"]) {
                $this->error['google_captcha'] = 'Erro de validação no reCaptcha! Efetue a validação para continuar';
            }
        }
        if ((!$this->config->get('google_recaptcha_site_key') || !$this->config->get('google_recaptcha_secret_key') || !$this->config->get('wg24_google_recaptcha_status'))
        && (isset($this->session->data['admin_login_trys']) && $this->session->data['admin_login_trys'] > 0)) {
            if (empty($this->session->data['captcha']) || ($this->session->data['captcha'] != $this->request->post['captcha'])) {
                $this->error['captcha'] = 'O código de verificação não coincide com a imagem!';
            }
        }

        // apenas se validou o captcha tenta validar login
        if (!$this->error) {
            if (isset($this->request->post['username']) && isset($this->request->post['password']) && !$this->user->login($this->request->post['username'], $this->request->post['password'])) {
                $this->error['warning'] = $this->language->get('error_login');
            }
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }

    public function captcha()
    {
        $this->load->library('captcha');

        $captcha = new Captcha();
        $this->session->data['captcha'] = $captcha->getCode();

        $captcha->showImage();
    }
}
