Example #1
0
 public function contact()
 {
     //get security
     $security = Security::getSecurity(Security::TYPE_FORM);
     $crsf = $security->getProtection('form1', Form::PROTECTION_CSRF);
     $captcha = $security->getProtection('form1', Form::PROTECTION_CAPTCHA);
     //create new and add to ajax data
     $crsf->create();
     $this->addAjaxDatas('token', $crsf->get());
     $error = false;
     //check security
     if (!$crsf->check(Http::getPost('token'))) {
         $error = true;
     }
     if (!$captcha->check(Http::getPost('captcha'))) {
         $this->addError($this->language->getVar('validate_security'), 'captcha');
         $error = true;
     }
     // send mail
     if ($error) {
         $this->notifyError($this->language->getVar('validate_error'));
     } else {
         //send mail
         SwiftMailer::getInstance();
         $mail = \Swift_Message::newInstance();
         $mail->setFrom(array(ADMIN_EMAIL => $this->language->getVar('site_name')));
         $mail->setTo(CONTACT_EMAIL);
         $mail->setSubject($this->language->getVar('site_name') . ' demande de contact');
         $contents = new MailContents($this->tpl->getPath() . 'mails' . DS . 'contact.tpl.php');
         $contents->addVar('message', nl2br(Http::getPost('message')))->addVar('name', Http::getPost('name'))->addVar('email', Http::getPost('email'))->addVar('subject', Http::getPost('subject'));
         $mail->addPart($contents->getMailContents(), 'text/html');
         $transport = defined('SMTP_SERVER') && !is_null(SMTP_SERVER) && SMTP_SERVER != '' ? \Swift_SmtpTransport::newInstance(SMTP_SERVER, 25) : \Swift_MailTransport::newInstance();
         $mailer = \Swift_Mailer::newInstance($transport);
         $mailer->send($mail);
         $this->notifySuccess($this->language->getVar('validate_success'));
     }
     //set in session
     $crsf->set();
 }
 public function display()
 {
     if ($this->hasErrors()) {
         $this->tpl->setVar('errors', $this->getErrors());
     }
     if ($this->tpl->post === null) {
         $this->tpl->setVar('post', Http::getPost(), false, true);
     }
     if ($this->tpl->query === null) {
         $this->tpl->setVar('query', Http::getQuery(), false, true);
     }
     if ($this->tpl->cookie === null) {
         $this->tpl->setVar('cookie', Http::getCookie(), false, true);
     }
     $this->tpl->setVar('notifyInformation', $this->session->get('notifyInformation'), false, true);
     $this->tpl->setVar('notifyError', $this->session->get('notifyError'), false, true);
     $this->tpl->setVar('notifySuccess', $this->session->get('notifySuccess'), false, true);
     if ($this->_isAjax) {
         if ($this->hasErrors()) {
             $this->addAjaxDatas('errors', $this->getErrors());
         }
         if ($this->_ajaxAutoAddDatas['post'] && !array_key_exists('post', $this->_ajaxDatas)) {
             $this->addAjaxDatas('post', Http::getPost());
         }
         if ($this->_ajaxAutoAddDatas['query'] && !array_key_exists('query', $this->_ajaxDatas)) {
             $this->addAjaxDatas('query', Http::getQuery());
         }
         if ($this->_ajaxAutoAddDatas['cookie'] && !array_key_exists('cookie', $this->_ajaxDatas)) {
             $this->addAjaxDatas('cookie', Http::getCookie());
         }
         if ($this->_ajaxAutoAddDatas['content'] && !array_key_exists('content', $this->_ajaxDatas)) {
             $this->addAjaxDatas('content', $this->tpl->getContent());
         }
         if (!array_key_exists('notifyInformation', $this->_ajaxDatas)) {
             $this->addAjaxDatas('notifyInformation', $this->session->get('notifyInformation'));
         }
         if (!array_key_exists('notifyError', $this->_ajaxDatas)) {
             $this->addAjaxDatas('notifyError', $this->session->get('notifyError'));
         }
         if (!array_key_exists('notifySuccess', $this->_ajaxDatas)) {
             $this->addAjaxDatas('notifySuccess', $this->session->get('notifySuccess'));
         }
         // No cache
         if (!$this->_ajaxDatasCache) {
             Header::sentHeader('Cache-Control', 'no-cache, must-revalidate');
             Header::sentHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT');
         }
         switch ($this->_ajaxDatasType) {
             case self::HTML:
                 Header::sentHeader('Content-type', 'text/html');
                 foreach ($this->_ajaxDatas as $data) {
                     echo $data;
                 }
                 break;
             case self::XML:
                 Header::sentHeader('Content-type', 'text/xml');
                 foreach ($this->_ajaxDatas as $data) {
                     echo $data;
                 }
                 break;
             case self::JSON:
                 Header::sentHeader('Content-type', 'application/json');
                 echo json_encode((object) $this->_ajaxDatas, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
                 break;
             default:
                 throw new \Exception('Ajax datas type not valid');
         }
     } else {
         //display
         $this->tpl->display();
         $this->log->debug('Display template file : "' . $this->tpl->getFile() . '"', 'router');
     }
     // Delete stored messages
     $this->session->delete('notifyInformation', true);
     $this->session->delete('notifyError', true);
     $this->session->delete('notifySuccess', true);
 }