<?php
class ControllerInformationLess extends Controller {
    private $error = array(); 
	    
    public function index() {
        $this->language->load('information/less');
        
        
        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate())
        {
            $this->load->model('module/less');
            
            $text = "<b>{$this->language->get('email_name')}:</b> {$_POST['less_name']}<br /><br />
                    <b>{$this->language->get('email_product')}:</b> {$_POST['less_product']}<br /><br />
                    <b>{$this->language->get('email_price')}:</b> {$_POST['less_price']}<br /><br />
		    <b>{$this->language->get('email_url')}:</b> {$_POST['less_url']}<br /><br />
                    <b>{$this->language->get('email_phone')}:</b> {$_POST['less_phone']}<br /><br />
                    <b>{$this->language->get('email_email')}:</b> {$_POST['less_email']}";
            
            $mail = new Mail();
            $mail->protocol  = $this->config->get('config_mail_protocol');
            $mail->parameter = $this->config->get('config_mail_parameter');
            $mail->hostname  = $this->config->get('config_smtp_host');
            $mail->username  = $this->config->get('config_smtp_username');
            $mail->password  = $this->config->get('config_smtp_password');
            $mail->port      = $this->config->get('config_smtp_port');
            $mail->timeout   = $this->config->get('config_smtp_timeout');				
            $mail->setTo($this->model_module_less->getMail());
            $mail->setFrom($this->config->get('config_email'));
            $mail->setSender($this->language->get('info_sender'));
            $mail->setSubject(html_entity_decode($_POST['name'], ENT_QUOTES, 'UTF-8'));
            $mail->setText(strip_tags($text));
            $mail->send();
    	
            $this->response->setOutput($this->language->get('info_output'));
        }
        else
        {
            header("HTTP/1.0 404 Not Found");
            echo '<ul>';
            foreach($this->error as $item)
            {
                echo '<li>' . $item . '</li>';
            }
             echo '</ul>';
        }	
    }
	
    private function validate() {
        if ( (!isset($this->request->post['less_name'])) || (utf8_strlen($this->request->post['less_name']) < 3) || (utf8_strlen($this->request->post['less_name']) > 50))
        {
            $this->error['name'] = $this->language->get('error_name');
        }
        
        if ( (!isset($this->request->post['less_product'])) || (utf8_strlen($this->request->post['less_product']) < 3) || (utf8_strlen($this->request->post['less_product']) > 100))
        {
            $this->error['product'] = $this->language->get('error_product');
        }
        
        if ( (!isset($this->request->post['less_price'])) || (utf8_strlen($this->request->post['less_price']) < 1) || (utf8_strlen($this->request->post['less_price']) > 100))
        {
            $this->error['price'] = $this->language->get('error_price');
        }

        if ( (!isset($this->request->post['less_url'])) || (utf8_strlen($this->request->post['less_url']) < 3) || (utf8_strlen($this->request->post['less_url']) > 255))
        {
            $this->error['url'] = $this->language->get('error_url');
        }

        if( (!isset($this->request->post['less_phone'])) || preg_match('|[^0-9()-\s+]+|i',$this->request->post['less_phone']) || 
                (utf8_strlen(preg_replace('/[^0-9]?/i', "", $this->request->post['less_phone'])) < 5) )
        {
            $this->error['phone'] = $this->language->get('error_phone');
        } 

        if ( (!isset($this->request->post['less_email'])) || !preg_match('/^[^\@]+@.*\.[a-z]{2,6}$/i', $this->request->post['less_email']))
        {
            $this->error['email'] = $this->language->get('error_email');
        }

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

