public function receiveformAction()
 {
     if ($this->getRequest()->isPost()) {
         $xmlHttpRequest = $this->_request->isXmlHttpRequest();
         $formParams = $this->getRequest()->getParams();
         $sessionHelper = Zend_Controller_Action_HelperBroker::getStaticHelper('Session');
         if (!empty($formParams)) {
             $websiteConfig = Zend_Controller_Action_HelperBroker::getExistingHelper('config')->getConfig();
             $formMapper = Application_Model_Mappers_FormMapper::getInstance();
             // get the form details
             $form = $formMapper->findByName($formParams['formName']);
             $useCaptcha = $form->getCaptcha();
             //hidden input validation
             $formName = $form->getName();
             $formId = $form->getId();
             if (!isset($formParams[md5($formName . $formId)]) || $formParams[md5($formName . $formId)] != '') {
                 if ($xmlHttpRequest) {
                     $this->_helper->response->success($form->getMessageSuccess());
                 }
                 $this->_redirect($formParams['formUrl']);
             }
             unset($formParams[md5($formName . $formId)]);
             //validating recaptcha
             if ($useCaptcha == 1) {
                 if (!empty($websiteConfig) && !empty($websiteConfig[Tools_System_Tools::RECAPTCHA_PUBLIC_KEY]) && !empty($websiteConfig[Tools_System_Tools::RECAPTCHA_PRIVATE_KEY]) && isset($formParams['recaptcha_challenge_field']) || isset($formParams['captcha'])) {
                     if (isset($formParams['recaptcha_challenge_field']) && isset($formParams['recaptcha_response_field'])) {
                         if ($formParams['recaptcha_response_field'] == '') {
                             if ($xmlHttpRequest) {
                                 $this->_helper->response->fail($this->_helper->language->translate('You\'ve entered an incorrect security text. Please try again.'));
                             }
                             $sessionHelper->toasterFormError = $this->_helper->language->translate('You\'ve entered an incorrect security text. Please try again.');
                             $this->_redirect($formParams['formUrl']);
                         }
                         $recaptcha = new Zend_Service_ReCaptcha($websiteConfig[Tools_System_Tools::RECAPTCHA_PUBLIC_KEY], $websiteConfig[Tools_System_Tools::RECAPTCHA_PRIVATE_KEY]);
                         $result = $recaptcha->verify($formParams['recaptcha_challenge_field'], $formParams['recaptcha_response_field']);
                         if (!$result->isValid()) {
                             if ($xmlHttpRequest) {
                                 $this->_helper->response->fail($this->_helper->language->translate('You\'ve entered an incorrect security text. Please try again.'));
                             }
                             $sessionHelper->toasterFormError = $this->_helper->language->translate('You\'ve entered an incorrect security text. Please try again.');
                             $this->_redirect($formParams['formUrl']);
                         }
                         unset($formParams['recaptcha_challenge_field']);
                         unset($formParams['recaptcha_response_field']);
                     } else {
                         //validating captcha
                         if (!$this->_validateCaptcha(strtolower($formParams['captcha']), $formParams['captchaId'])) {
                             if ($xmlHttpRequest) {
                                 $this->_helper->response->fail($this->_helper->language->translate('You\'ve entered an incorrect security text. Please try again.'));
                             }
                             $sessionHelper->toasterFormError = $this->_helper->language->translate('You\'ve entered an incorrect security text. Please try again.');
                             $this->_redirect($formParams['formUrl']);
                         }
                     }
                 } else {
                     if ($xmlHttpRequest) {
                         $this->_helper->response->fail($this->_helper->language->translate('You\'ve entered an incorrect security text. Please try again.'));
                     }
                     $sessionHelper->toasterFormError = $this->_helper->language->translate('You\'ve entered an incorrect security text. Please try again.');
                     $this->_redirect($formParams['formUrl']);
                 }
             }
             $sessionHelper->formName = $formParams['formName'];
             $sessionHelper->formPageId = $formParams['formPageId'];
             unset($formParams['formPageId']);
             unset($formParams['submit']);
             if (isset($formParams['conversionPageUrl'])) {
                 $conversionPageUrl = $formParams['conversionPageUrl'];
                 unset($formParams['conversionPageUrl']);
             }
             $attachment = array();
             if (!$xmlHttpRequest) {
                 //Adding attachments to email
                 $websitePathTemp = $this->_helper->website->getPath() . $this->_helper->website->getTmp();
                 $uploader = new Zend_File_Transfer_Adapter_Http();
                 $uploader->setDestination($websitePathTemp);
                 $uploader->addValidator('Extension', false, self::ATTACHMENTS_FILE_TYPES);
                 //Adding Size limitation
                 $uploader->addValidator('Size', false, $formParams['uploadLimitSize'] * 1024 * 1024);
                 //Adding mime types validation
                 $uploader->addValidator('MimeType', true, array('application/pdf', 'application/xml', 'application/zip', 'text/csv', 'text/plain', 'image/png', 'image/jpeg', 'image/gif', 'image/bmp', 'application/msword', 'application/vnd.ms-excel'));
                 $files = $uploader->getFileInfo();
                 foreach ($files as $file => $fileInfo) {
                     if ($fileInfo['name'] != '') {
                         if ($uploader->isValid($file)) {
                             $uploader->receive($file);
                             $at = new Zend_Mime_Part(file_get_contents($uploader->getFileName($file)));
                             $at->type = $uploader->getMimeType($file);
                             $at->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
                             $at->encoding = Zend_Mime::ENCODING_BASE64;
                             $at->filename = $fileInfo['name'];
                             $attachment[] = $at;
                             unset($at);
                             Tools_Filesystem_Tools::deleteFile($this->_helper->website->getPath() . $this->_helper->website->getTmp() . $fileInfo['name']);
                         } else {
                             $validationErrors = $uploader->getErrors();
                             $errorMessage = '';
                             foreach ($validationErrors as $errorType) {
                                 if ($errorType == 'fileMimeTypeFalse') {
                                     $errorMessage .= 'Invalid file format type. ';
                                 }
                                 if ($errorType == 'fileSizeTooBig') {
                                     $errorMessage .= $this->_helper->language->translate('Maximum size upload') . ' ' . $formParams['uploadLimitSize'] . 'mb.';
                                 }
                                 if ($errorType == 'fileExtensionFalse') {
                                     $errorMessage .= 'File extension not valid. ';
                                 }
                             }
                             $sessionHelper->toasterFormError = $this->_helper->language->translate($errorMessage);
                             $this->_redirect($formParams['formUrl']);
                         }
                     }
                 }
             }
             unset($formParams['uploadLimitSize']);
             // sending mails
             $sysMailWatchdog = new Tools_Mail_SystemMailWatchdog(array('trigger' => Tools_Mail_SystemMailWatchdog::TRIGGER_FORMSENT, 'data' => $formParams, 'attachment' => $attachment));
             $mailWatchdog = new Tools_Mail_Watchdog(array('trigger' => Tools_Mail_SystemMailWatchdog::TRIGGER_FORMSENT, 'data' => $formParams, 'attachment' => $attachment));
             $mailWatchdog->notify($form);
             $mailsSent = $sysMailWatchdog->notify($form);
             if ($mailsSent) {
                 $form->notifyObservers();
                 if ($xmlHttpRequest) {
                     $this->_helper->response->success($form->getMessageSuccess());
                 }
                 //redirect to conversion page
                 if ($conversionPageUrl) {
                     $this->_redirect($conversionPageUrl);
                 }
                 $sessionHelper->toasterFormSuccess = $form->getMessageSuccess();
                 $this->_redirect($formParams['formUrl']);
             }
             if ($xmlHttpRequest) {
                 $this->_helper->response->fail($form->getMessageError());
             }
             $sessionHelper->toasterFormError = $form->getMessageError();
             $this->_redirect($formParams['formUrl']);
         }
     }
 }
Example #2
0
 protected function _load()
 {
     if (!is_array($this->_options) || empty($this->_options) || !isset($this->_options[0]) || !$this->_options[0] || preg_match('~^\\s*$~', $this->_options[0])) {
         throw new Exceptions_SeotoasterException($this->_translator->translate('You should provide a form name.'));
     }
     if (strtolower($this->_options[0]) == 'conversioncode') {
         return $this->_conversionCode($this->_options);
     }
     $sessionHelper = Zend_Controller_Action_HelperBroker::getStaticHelper('Session');
     $useCaptcha = isset($this->_options[1]) && $this->_options[1] == 'captcha' ? true : false;
     $useRecaptcha = isset($this->_options[1]) && $this->_options[1] == 'recaptcha' ? true : false;
     $uploadLimitSize = is_numeric(end($this->_options)) ? end($this->_options) : self::UPLOAD_LIMIT_SIZE;
     $formMapper = Application_Model_Mappers_FormMapper::getInstance();
     $pageMapper = Application_Model_Mappers_PageMapper::getInstance();
     $form = $formMapper->findByName($this->_options[0]);
     $pageHelper = new Helpers_Action_Page();
     $pageHelper->init();
     if ($useCaptcha || $useRecaptcha) {
         if ($form != null) {
             $form->setCaptcha(1);
             $formMapper->save($form);
         }
         if ($useRecaptcha) {
             $recaptchaTheme = 'red';
             $recaptchaWidgetId = uniqid('recaptcha_widget_');
             if (isset($this->_options[2])) {
                 $recaptchaTheme = $this->_options[2];
                 if ($recaptchaTheme == 'custom') {
                     $this->_view->customRecaptcha = true;
                 }
             }
             $this->_view->recaptchaWidgetId = $recaptchaWidgetId;
             $this->_view->addScriptPath($this->_websiteHelper->getPath() . 'seotoaster_core/application/views/scripts/backend/form/');
             $this->_view->recaptchaCode = Tools_System_Tools::generateRecaptcha($recaptchaTheme, $recaptchaWidgetId);
         }
         if ($useCaptcha) {
             $this->_view->captchaId = Tools_System_Tools::generateCaptcha();
         }
     }
     if (isset($sessionHelper->toasterFormError)) {
         $this->_view->toasterFormError = $sessionHelper->toasterFormError;
         unset($sessionHelper->toasterFormError);
     }
     if (isset($sessionHelper->toasterFormSuccess)) {
         $this->_view->toasterFormSuccess = $sessionHelper->toasterFormSuccess;
         unset($sessionHelper->toasterFormSuccess);
     }
     $trackingConversionUrl = 'form-' . $this->_options[0] . '-thank-you';
     $trackingConversionUrl = $pageHelper->filterUrl($trackingConversionUrl);
     $trackingPageExist = $pageMapper->findByUrl($trackingConversionUrl);
     if ($trackingPageExist instanceof Application_Model_Models_Page) {
         $this->_view->trackingConversionUrl = $trackingConversionUrl;
     }
     $this->_view->useRecaptcha = $useRecaptcha;
     $this->_view->useCaptcha = $useCaptcha;
     $this->_view->form = Application_Model_Mappers_FormMapper::getInstance()->findByName($this->_options[0]);
     $this->_view->allowMidification = Tools_Security_Acl::isAllowed(Tools_Security_Acl::RESOURCE_ADMINPANEL);
     $this->_view->formName = $this->_options[0];
     $this->_view->uploadLimitSize = $uploadLimitSize;
     $filter = new Zend_Filter_Alnum();
     $this->_view->formId = $filter->filter($this->_options[0]);
     $this->_view->pageId = $this->_toasterOptions['id'];
     $this->_view->websiteTmp = $this->_websiteHelper->getTmp();
     $this->_view->formUrl = $this->_toasterOptions['url'];
     return $this->_view->render('form.phtml');
 }