8
8
9
9
use Magento \Framework \App \Action ;
10
10
use Magento \Framework \App \Config \ScopeConfigInterface ;
11
+ use Magento \Framework \App \ResponseInterface ;
11
12
use Magento \Framework \Exception \NotFoundException ;
12
13
use Magento \Framework \Session \Generic as WishlistSession ;
13
14
use Magento \Store \Model \StoreManagerInterface ;
14
15
use Magento \Framework \Controller \ResultFactory ;
15
16
use Magento \Framework \View \Result \Layout as ResultLayout ;
17
+ use Magento \Captcha \Helper \Data as CaptchaHelper ;
18
+ use Magento \Captcha \Observer \CaptchaStringResolver ;
19
+ use Magento \Framework \Controller \Result \Redirect ;
20
+ use Magento \Framework \Controller \ResultInterface ;
21
+ use Magento \Framework \App \ObjectManager ;
22
+ use Magento \Captcha \Model \DefaultModel as CaptchaModel ;
23
+ use Magento \Framework \Exception \LocalizedException ;
24
+ use Magento \Customer \Model \Customer ;
16
25
17
26
/**
18
27
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -69,6 +78,16 @@ class Send extends \Magento\Wishlist\Controller\AbstractIndex
69
78
*/
70
79
protected $ storeManager ;
71
80
81
+ /**
82
+ * @var CaptchaHelper
83
+ */
84
+ private $ captchaHelper ;
85
+
86
+ /**
87
+ * @var CaptchaStringResolver
88
+ */
89
+ private $ captchaStringResolver ;
90
+
72
91
/**
73
92
* @param Action\Context $context
74
93
* @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
@@ -81,6 +100,8 @@ class Send extends \Magento\Wishlist\Controller\AbstractIndex
81
100
* @param WishlistSession $wishlistSession
82
101
* @param ScopeConfigInterface $scopeConfig
83
102
* @param StoreManagerInterface $storeManager
103
+ * @param CaptchaHelper|null $captchaHelper
104
+ * @param CaptchaStringResolver|null $captchaStringResolver
84
105
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
85
106
*/
86
107
public function __construct (
@@ -94,7 +115,9 @@ public function __construct(
94
115
\Magento \Customer \Helper \View $ customerHelperView ,
95
116
WishlistSession $ wishlistSession ,
96
117
ScopeConfigInterface $ scopeConfig ,
97
- StoreManagerInterface $ storeManager
118
+ StoreManagerInterface $ storeManager ,
119
+ ?CaptchaHelper $ captchaHelper = null ,
120
+ ?CaptchaStringResolver $ captchaStringResolver = null
98
121
) {
99
122
$ this ->_formKeyValidator = $ formKeyValidator ;
100
123
$ this ->_customerSession = $ customerSession ;
@@ -106,6 +129,10 @@ public function __construct(
106
129
$ this ->wishlistSession = $ wishlistSession ;
107
130
$ this ->scopeConfig = $ scopeConfig ;
108
131
$ this ->storeManager = $ storeManager ;
132
+ $ this ->captchaHelper = $ captchaHelper ?: ObjectManager::getInstance ()->get (CaptchaHelper::class);
133
+ $ this ->captchaStringResolver = $ captchaStringResolver ?
134
+ : ObjectManager::getInstance ()->get (CaptchaStringResolver::class);
135
+
109
136
parent ::__construct ($ context );
110
137
}
111
138
@@ -114,6 +141,7 @@ public function __construct(
114
141
*
115
142
* @return \Magento\Framework\Controller\Result\Redirect
116
143
* @throws NotFoundException
144
+ * @throws \Zend_Validate_Exception
117
145
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
118
146
* @SuppressWarnings(PHPMD.NPathComplexity)
119
147
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -122,11 +150,25 @@ public function execute()
122
150
{
123
151
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
124
152
$ resultRedirect = $ this ->resultFactory ->create (ResultFactory::TYPE_REDIRECT );
153
+ $ captchaForName = 'share_wishlist_form ' ;
154
+ /** @var CaptchaModel $captchaModel */
155
+ $ captchaModel = $ this ->captchaHelper ->getCaptcha ($ captchaForName );
156
+
125
157
if (!$ this ->_formKeyValidator ->validate ($ this ->getRequest ())) {
126
158
$ resultRedirect ->setPath ('*/*/ ' );
127
159
return $ resultRedirect ;
128
160
}
129
161
162
+ $ isCorrectCaptcha = $ this ->validateCaptcha ($ captchaModel , $ captchaForName );
163
+
164
+ $ this ->logCaptchaAttempt ($ captchaModel );
165
+
166
+ if (!$ isCorrectCaptcha ) {
167
+ $ this ->messageManager ->addErrorMessage (__ ('Incorrect CAPTCHA ' ));
168
+ $ resultRedirect ->setPath ('*/*/share ' );
169
+ return $ resultRedirect ;
170
+ }
171
+
130
172
$ wishlist = $ this ->wishlistProvider ->getWishlist ();
131
173
if (!$ wishlist ) {
132
174
throw new NotFoundException (__ ('Page not found. ' ));
@@ -288,4 +330,43 @@ protected function getWishlistItems(ResultLayout $resultLayout)
288
330
->getBlock ('wishlist.email.items ' )
289
331
->toHtml ();
290
332
}
333
+
334
+ /**
335
+ * Log customer action attempts
336
+ * @param CaptchaModel $captchaModel
337
+ * @return void
338
+ */
339
+ private function logCaptchaAttempt (CaptchaModel $ captchaModel )
340
+ {
341
+ /** @var Customer $customer */
342
+ $ customer = $ this ->_customerSession ->getCustomer ();
343
+ $ email = '' ;
344
+
345
+ if ($ customer ->getId ()) {
346
+ $ email = $ customer ->getEmail ();
347
+ }
348
+
349
+ $ captchaModel ->logAttempt ($ email );
350
+ }
351
+
352
+ /**
353
+ * @param CaptchaModel $captchaModel
354
+ * @param string $captchaFormName
355
+ * @return bool
356
+ */
357
+ private function validateCaptcha (CaptchaModel $ captchaModel , string $ captchaFormName ) : bool
358
+ {
359
+ if ($ captchaModel ->isRequired ()) {
360
+ $ word = $ this ->captchaStringResolver ->resolve (
361
+ $ this ->getRequest (),
362
+ $ captchaFormName
363
+ );
364
+
365
+ if (!$ captchaModel ->isCorrect ($ word )) {
366
+ return false ;
367
+ }
368
+ }
369
+
370
+ return true ;
371
+ }
291
372
}
0 commit comments