![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/magento/module-contact/Controller/Index/ |
<?php /** * * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace Magento\Contact\Controller\Index; use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface; use Magento\Contact\Model\ConfigInterface; use Magento\Contact\Model\MailInterface; use Magento\Framework\App\Action\Context; use Magento\Framework\App\Request\DataPersistorInterface; use Magento\Framework\Controller\Result\Redirect; use Magento\Framework\Exception\LocalizedException; use Psr\Log\LoggerInterface; use Magento\Framework\App\ObjectManager; use Magento\Framework\DataObject; class Post extends \Magento\Contact\Controller\Index implements HttpPostActionInterface { /** * @var DataPersistorInterface */ private $dataPersistor; /** * @var Context */ private $context; /** * @var MailInterface */ private $mail; /** * @var LoggerInterface */ private $logger; /** * @param Context $context * @param ConfigInterface $contactsConfig * @param MailInterface $mail * @param DataPersistorInterface $dataPersistor * @param LoggerInterface $logger */ public function __construct( Context $context, ConfigInterface $contactsConfig, MailInterface $mail, DataPersistorInterface $dataPersistor, LoggerInterface $logger = null ) { parent::__construct($context, $contactsConfig); $this->context = $context; $this->mail = $mail; $this->dataPersistor = $dataPersistor; $this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class); } /** * Post user question * * @return Redirect */ public function execute() { if (!$this->getRequest()->isPost()) { return $this->resultRedirectFactory->create()->setPath('*/*/'); } try { $this->sendEmail($this->validatedParams()); $this->messageManager->addSuccessMessage( __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.') ); $this->dataPersistor->clear('contact_us'); } catch (LocalizedException $e) { $this->messageManager->addErrorMessage($e->getMessage()); $this->dataPersistor->set('contact_us', $this->getRequest()->getParams()); } catch (\Exception $e) { $this->logger->critical($e); $this->messageManager->addErrorMessage( __('An error occurred while processing your form. Please try again later.') ); $this->dataPersistor->set('contact_us', $this->getRequest()->getParams()); } return $this->resultRedirectFactory->create()->setPath('contact/index'); } /** * Method to send email. * * @param array $post Post data from contact form * * @return void */ private function sendEmail($post) { $this->mail->send( $post['email'], ['data' => new DataObject($post)] ); } /** * Method to validated params. * * @return array * @throws \Exception */ private function validatedParams() { $request = $this->getRequest(); if (trim($request->getParam('name', '')) === '') { throw new LocalizedException(__('Enter the Name and try again.')); } if (trim($request->getParam('comment', '')) === '') { throw new LocalizedException(__('Enter the comment and try again.')); } if (\strpos($request->getParam('email', ''), '@') === false) { throw new LocalizedException(__('The email address is invalid. Verify the email address and try again.')); } if (trim($request->getParam('hideit', '')) !== '') { // phpcs:ignore Magento2.Exceptions.DirectThrow throw new \Exception(); } return $request->getParams(); } }