Send.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Wishlist\Controller\Index;
  8. use Magento\Framework\App\Action;
  9. use Magento\Framework\App\Config\ScopeConfigInterface;
  10. use Magento\Framework\App\ResponseInterface;
  11. use Magento\Framework\Exception\NotFoundException;
  12. use Magento\Framework\Session\Generic as WishlistSession;
  13. use Magento\Store\Model\StoreManagerInterface;
  14. use Magento\Framework\Controller\ResultFactory;
  15. use Magento\Framework\View\Result\Layout as ResultLayout;
  16. use Magento\Captcha\Helper\Data as CaptchaHelper;
  17. use Magento\Captcha\Observer\CaptchaStringResolver;
  18. use Magento\Framework\Controller\Result\Redirect;
  19. use Magento\Framework\Controller\ResultInterface;
  20. use Magento\Framework\App\ObjectManager;
  21. use Magento\Captcha\Model\DefaultModel as CaptchaModel;
  22. use Magento\Framework\Exception\LocalizedException;
  23. use Magento\Customer\Model\Customer;
  24. /**
  25. * Class Send
  26. *
  27. * @package Magento\Wishlist\Controller\Index
  28. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  29. */
  30. class Send extends \Magento\Wishlist\Controller\AbstractIndex implements Action\HttpPostActionInterface
  31. {
  32. /**
  33. * @var \Magento\Customer\Helper\View
  34. */
  35. protected $_customerHelperView;
  36. /**
  37. * @var \Magento\Framework\Translate\Inline\StateInterface
  38. */
  39. protected $inlineTranslation;
  40. /**
  41. * @var \Magento\Framework\Mail\Template\TransportBuilder
  42. */
  43. protected $_transportBuilder;
  44. /**
  45. * @var \Magento\Wishlist\Model\Config
  46. */
  47. protected $_wishlistConfig;
  48. /**
  49. * @var \Magento\Wishlist\Controller\WishlistProviderInterface
  50. */
  51. protected $wishlistProvider;
  52. /**
  53. * @var \Magento\Customer\Model\Session
  54. */
  55. protected $_customerSession;
  56. /**
  57. * @var \Magento\Framework\Data\Form\FormKey\Validator
  58. */
  59. protected $_formKeyValidator;
  60. /**
  61. * @var WishlistSession
  62. */
  63. protected $wishlistSession;
  64. /**
  65. * @var ScopeConfigInterface
  66. */
  67. protected $scopeConfig;
  68. /**
  69. * @var StoreManagerInterface
  70. */
  71. protected $storeManager;
  72. /**
  73. * @var CaptchaHelper
  74. */
  75. private $captchaHelper;
  76. /**
  77. * @var CaptchaStringResolver
  78. */
  79. private $captchaStringResolver;
  80. /**
  81. * @param Action\Context $context
  82. * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
  83. * @param \Magento\Customer\Model\Session $customerSession
  84. * @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
  85. * @param \Magento\Wishlist\Model\Config $wishlistConfig
  86. * @param \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
  87. * @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
  88. * @param \Magento\Customer\Helper\View $customerHelperView
  89. * @param WishlistSession $wishlistSession
  90. * @param ScopeConfigInterface $scopeConfig
  91. * @param StoreManagerInterface $storeManager
  92. * @param CaptchaHelper|null $captchaHelper
  93. * @param CaptchaStringResolver|null $captchaStringResolver
  94. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  95. */
  96. public function __construct(
  97. Action\Context $context,
  98. \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
  99. \Magento\Customer\Model\Session $customerSession,
  100. \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider,
  101. \Magento\Wishlist\Model\Config $wishlistConfig,
  102. \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
  103. \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
  104. \Magento\Customer\Helper\View $customerHelperView,
  105. WishlistSession $wishlistSession,
  106. ScopeConfigInterface $scopeConfig,
  107. StoreManagerInterface $storeManager,
  108. ?CaptchaHelper $captchaHelper = null,
  109. ?CaptchaStringResolver $captchaStringResolver = null
  110. ) {
  111. $this->_formKeyValidator = $formKeyValidator;
  112. $this->_customerSession = $customerSession;
  113. $this->wishlistProvider = $wishlistProvider;
  114. $this->_wishlistConfig = $wishlistConfig;
  115. $this->_transportBuilder = $transportBuilder;
  116. $this->inlineTranslation = $inlineTranslation;
  117. $this->_customerHelperView = $customerHelperView;
  118. $this->wishlistSession = $wishlistSession;
  119. $this->scopeConfig = $scopeConfig;
  120. $this->storeManager = $storeManager;
  121. $this->captchaHelper = $captchaHelper ?: ObjectManager::getInstance()->get(CaptchaHelper::class);
  122. $this->captchaStringResolver = $captchaStringResolver ?
  123. : ObjectManager::getInstance()->get(CaptchaStringResolver::class);
  124. parent::__construct($context);
  125. }
  126. /**
  127. * Share wishlist
  128. *
  129. * @return \Magento\Framework\Controller\Result\Redirect
  130. * @throws NotFoundException
  131. * @throws \Zend_Validate_Exception
  132. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  133. * @SuppressWarnings(PHPMD.NPathComplexity)
  134. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  135. */
  136. public function execute()
  137. {
  138. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  139. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  140. $captchaForName = 'share_wishlist_form';
  141. /** @var CaptchaModel $captchaModel */
  142. $captchaModel = $this->captchaHelper->getCaptcha($captchaForName);
  143. if (!$this->_formKeyValidator->validate($this->getRequest())) {
  144. $resultRedirect->setPath('*/*/');
  145. return $resultRedirect;
  146. }
  147. $isCorrectCaptcha = $this->validateCaptcha($captchaModel, $captchaForName);
  148. $this->logCaptchaAttempt($captchaModel);
  149. if (!$isCorrectCaptcha) {
  150. $this->messageManager->addErrorMessage(__('Incorrect CAPTCHA'));
  151. $resultRedirect->setPath('*/*/share');
  152. return $resultRedirect;
  153. }
  154. $wishlist = $this->wishlistProvider->getWishlist();
  155. if (!$wishlist) {
  156. throw new NotFoundException(__('Page not found.'));
  157. }
  158. $sharingLimit = $this->_wishlistConfig->getSharingEmailLimit();
  159. $textLimit = $this->_wishlistConfig->getSharingTextLimit();
  160. $emailsLeft = $sharingLimit - $wishlist->getShared();
  161. $emails = $this->getRequest()->getPost('emails');
  162. $emails = empty($emails) ? $emails : explode(',', $emails);
  163. $error = false;
  164. $message = (string)$this->getRequest()->getPost('message');
  165. if (strlen($message) > $textLimit) {
  166. $error = __('Message length must not exceed %1 symbols', $textLimit);
  167. } else {
  168. $message = nl2br(htmlspecialchars($message));
  169. if (empty($emails)) {
  170. $error = __('Please enter an email address.');
  171. } else {
  172. if (count($emails) > $emailsLeft) {
  173. $error = __('This wish list can be shared %1 more times.', $emailsLeft);
  174. } else {
  175. foreach ($emails as $index => $email) {
  176. $email = trim($email);
  177. if (!\Zend_Validate::is($email, \Magento\Framework\Validator\EmailAddress::class)) {
  178. $error = __('Please enter a valid email address.');
  179. break;
  180. }
  181. $emails[$index] = $email;
  182. }
  183. }
  184. }
  185. }
  186. if ($error) {
  187. $this->messageManager->addError($error);
  188. $this->wishlistSession->setSharingForm($this->getRequest()->getPostValue());
  189. $resultRedirect->setPath('*/*/share');
  190. return $resultRedirect;
  191. }
  192. /** @var \Magento\Framework\View\Result\Layout $resultLayout */
  193. $resultLayout = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT);
  194. $this->addLayoutHandles($resultLayout);
  195. $this->inlineTranslation->suspend();
  196. $sent = 0;
  197. try {
  198. $customer = $this->_customerSession->getCustomerDataObject();
  199. $customerName = $this->_customerHelperView->getCustomerName($customer);
  200. $message .= $this->getRssLink($wishlist->getId(), $resultLayout);
  201. $emails = array_unique($emails);
  202. $sharingCode = $wishlist->getSharingCode();
  203. try {
  204. foreach ($emails as $email) {
  205. $transport = $this->_transportBuilder->setTemplateIdentifier(
  206. $this->scopeConfig->getValue(
  207. 'wishlist/email/email_template',
  208. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  209. )
  210. )->setTemplateOptions(
  211. [
  212. 'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
  213. 'store' => $this->storeManager->getStore()->getStoreId(),
  214. ]
  215. )->setTemplateVars(
  216. [
  217. 'customer' => $customer,
  218. 'customerName' => $customerName,
  219. 'salable' => $wishlist->isSalable() ? 'yes' : '',
  220. 'items' => $this->getWishlistItems($resultLayout),
  221. 'viewOnSiteLink' => $this->_url->getUrl('*/shared/index', ['code' => $sharingCode]),
  222. 'message' => $message,
  223. 'store' => $this->storeManager->getStore(),
  224. ]
  225. )->setFrom(
  226. $this->scopeConfig->getValue(
  227. 'wishlist/email/email_identity',
  228. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  229. )
  230. )->addTo(
  231. $email
  232. )->getTransport();
  233. $transport->sendMessage();
  234. $sent++;
  235. }
  236. } catch (\Exception $e) {
  237. $wishlist->setShared($wishlist->getShared() + $sent);
  238. $wishlist->save();
  239. throw $e;
  240. }
  241. $wishlist->setShared($wishlist->getShared() + $sent);
  242. $wishlist->save();
  243. $this->inlineTranslation->resume();
  244. $this->_eventManager->dispatch('wishlist_share', ['wishlist' => $wishlist]);
  245. $this->messageManager->addSuccess(__('Your wish list has been shared.'));
  246. $resultRedirect->setPath('*/*', ['wishlist_id' => $wishlist->getId()]);
  247. return $resultRedirect;
  248. } catch (\Exception $e) {
  249. $this->inlineTranslation->resume();
  250. $this->messageManager->addError($e->getMessage());
  251. $this->wishlistSession->setSharingForm($this->getRequest()->getPostValue());
  252. $resultRedirect->setPath('*/*/share');
  253. return $resultRedirect;
  254. }
  255. }
  256. /**
  257. * Prepare to load additional email blocks
  258. *
  259. * Add 'wishlist_email_rss' layout handle.
  260. * Add 'wishlist_email_items' layout handle.
  261. *
  262. * @param \Magento\Framework\View\Result\Layout $resultLayout
  263. * @return void
  264. */
  265. protected function addLayoutHandles(ResultLayout $resultLayout)
  266. {
  267. if ($this->getRequest()->getParam('rss_url')) {
  268. $resultLayout->addHandle('wishlist_email_rss');
  269. }
  270. $resultLayout->addHandle('wishlist_email_items');
  271. }
  272. /**
  273. * Retrieve RSS link content (html)
  274. *
  275. * @param int $wishlistId
  276. * @param \Magento\Framework\View\Result\Layout $resultLayout
  277. * @return mixed
  278. */
  279. protected function getRssLink($wishlistId, ResultLayout $resultLayout)
  280. {
  281. if ($this->getRequest()->getParam('rss_url')) {
  282. return $resultLayout->getLayout()
  283. ->getBlock('wishlist.email.rss')
  284. ->setWishlistId($wishlistId)
  285. ->toHtml();
  286. }
  287. }
  288. /**
  289. * Retrieve wishlist items content (html)
  290. *
  291. * @param \Magento\Framework\View\Result\Layout $resultLayout
  292. * @return string
  293. */
  294. protected function getWishlistItems(ResultLayout $resultLayout)
  295. {
  296. return $resultLayout->getLayout()
  297. ->getBlock('wishlist.email.items')
  298. ->toHtml();
  299. }
  300. /**
  301. * Log customer action attempts
  302. *
  303. * @param CaptchaModel $captchaModel
  304. * @return void
  305. */
  306. private function logCaptchaAttempt(CaptchaModel $captchaModel): void
  307. {
  308. /** @var Customer $customer */
  309. $customer = $this->_customerSession->getCustomer();
  310. $email = '';
  311. if ($customer->getId()) {
  312. $email = $customer->getEmail();
  313. }
  314. $captchaModel->logAttempt($email);
  315. }
  316. /**
  317. * Captcha validate logic
  318. *
  319. * @param CaptchaModel $captchaModel
  320. * @param string $captchaFormName
  321. * @return bool
  322. */
  323. private function validateCaptcha(CaptchaModel $captchaModel, string $captchaFormName) : bool
  324. {
  325. if ($captchaModel->isRequired()) {
  326. $word = $this->captchaStringResolver->resolve(
  327. $this->getRequest(),
  328. $captchaFormName
  329. );
  330. if (!$captchaModel->isCorrect($word)) {
  331. return false;
  332. }
  333. }
  334. return true;
  335. }
  336. }