<?php
class ControllerCommonBanIP extends Controller {

    public function index() {

        if(isset($_COOKIE['ban_ip']) AND $this->getIgnored()){
                return $this->forward('common/ban_ip/info');
        }else{
                $this->load->model('catalog/customer_ban_ip');
                $ip = getenv('REMOTE_ADDR');
                $result = $this->model_catalog_customer_ban_ip->getCustomerBanIp($ip);
              if($result AND $this->getIgnored()){
                    setcookie('ban_ip', $ip);
                    return $this->forward('common/ban_ip/info');
                }
        }

    }

    private function getIgnored(){
        if(isset($this->request->get['route'])){
            switch ($this->request->get['route']){
                case 'api/category/categoryall':        return false; break;
                case 'api/loginapi/login':              return false; break;
                case 'api/configuracoes/infologin':     return false; break;
                case 'api/configuracoes/menulateral':   return false; break;
                case 'api/customer/logincustomer':      return false; break;
                default:
                    return true;
            }
        }else{
            return true;
        }
    }

	public function info() {
        $this->language->load('common/ban_ip');

        $this->data['heading_title'] = $this->document->getTitle();

        $this->data['message'] = $this->language->get('text_message');
		$this->data['duvidas'] = $this->language->get('text_duvidas');

		$this->data['email']   = $this->config->get('safety_limiter_status')? $this->config->get('safety_limiter_mail') : $this->config->get('config_email');

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/ban_ip.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/common/ban_ip.tpl';
        } else {
            $this->template = 'default/template/common/ban_ip.tpl';
        }

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

		$this->response->setOutput($this->render());

    }

}
