Skip to content

Commit f2e50e9

Browse files
committed
Initial commit
1 parent 8b7f63a commit f2e50e9

File tree

16 files changed

+578
-0
lines changed

16 files changed

+578
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* @category HS
4+
*
5+
* @copyright Copyright (c) 2015 Hungersoft (http://www.hungersoft.com)
6+
* @license http://www.hungersoft.com/license.txt Hungersoft General License
7+
*/
8+
9+
namespace HS\Honeypot\Block\Adminhtml\Form\Field;
10+
11+
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
12+
13+
/**
14+
* Class AdditionalEmail.
15+
*/
16+
class CustomForms extends AbstractFieldArray
17+
{
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
protected function _prepareToRender()
22+
{
23+
$this->addColumn('selector', ['label' => __('Selector'), 'class' => 'required-entry']);
24+
$this->addColumn('action', ['label' => __('Form Action'), 'class' => 'required-entry']);
25+
$this->_addAfter = false;
26+
$this->_addButtonLabel = __('Add Form');
27+
}
28+
}

Helper/Data.php

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
/**
3+
* Copyright 2019 Hungersoft (http://www.hungersoft.com).
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace HS\Honeypot\Helper;
19+
20+
use Magento\Store\Model\ScopeInterface;
21+
use Magento\Framework\App\Helper\AbstractHelper;
22+
use HS\Honeypot\Model\System\Config\Source\Forms;
23+
24+
class Data extends AbstractHelper
25+
{
26+
const CONFIG_ENABLED = 'hs_honeypot/general/enabled';
27+
const CONFIG_FORMS = 'hs_honeypot/general/forms';
28+
const CONFIG_CUSTOM_FORMS = 'hs_honeypot/general/custom_forms';
29+
30+
private $formActionSelectors = [
31+
Forms::TYPE_LOGIN => 'body.customer-account-login #login-form.form.form-login',
32+
Forms::TYPE_CREATE => 'body.customer-account-create #form-validate.form-create-account',
33+
Forms::TYPE_FORGOT => '#form-validate.form.password.forget',
34+
Forms::TYPE_CONTACT => '#contact-form',
35+
Forms::TYPE_CHANGE_PASSWORD => '#form-validate.form.form-edit-account',
36+
Forms::TYPE_PRODUCT_REVIEW => '#review-form',
37+
];
38+
39+
/**
40+
* Currently selected store ID if applicable.
41+
*
42+
* @var int
43+
*/
44+
protected $_storeId = null;
45+
46+
/**
47+
* Get config value by path.
48+
*
49+
* @param string $path
50+
*
51+
* @return mixed
52+
*/
53+
public function getConfigValue($path)
54+
{
55+
return $this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE);
56+
}
57+
58+
/**
59+
* Get config flag by path.
60+
*
61+
* @param string $path
62+
*
63+
* @return bool
64+
*/
65+
public function getConfigFlag($path)
66+
{
67+
return $this->scopeConfig->isSetFlag($path, ScopeInterface::SCOPE_STORE, $this->_storeId);
68+
}
69+
70+
/**
71+
* Return true if active and false otherwise.
72+
*
73+
* @return bool
74+
*/
75+
public function isEnabled()
76+
{
77+
return $this->getConfigFlag(self::CONFIG_ENABLED);
78+
}
79+
80+
/**
81+
* Get selected forms.
82+
*
83+
* @return array
84+
*/
85+
public function getForms($store = null)
86+
{
87+
$forms = $this->scopeConfig->getValue(
88+
self::CONFIG_FORMS,
89+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
90+
$store
91+
);
92+
93+
$forms = explode(',', $forms) ?: [];
94+
if (!is_array($forms)) {
95+
return [trim($forms)];
96+
}
97+
98+
return array_map('trim', $forms);
99+
}
100+
101+
/**
102+
* Get selected forms.
103+
*
104+
* @return array
105+
*/
106+
public function getCustomForms()
107+
{
108+
$data = $this->scopeConfig->getValue(
109+
self::CONFIG_CUSTOM_FORMS,
110+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
111+
);
112+
113+
return json_decode($data, true) ?: [];
114+
}
115+
116+
/**
117+
* Get css selectors for the selected forms.
118+
*
119+
* @return array
120+
*/
121+
public function getSelectedFormSelectors()
122+
{
123+
$forms = $this->getForms();
124+
$finalForms = [];
125+
foreach ($forms as $action) {
126+
if (isset($this->formActionSelectors[$action])) {
127+
$finalForms[] = $this->formActionSelectors[$action];
128+
}
129+
}
130+
131+
return $finalForms;
132+
}
133+
134+
/**
135+
* Get selected forms paths.
136+
*
137+
* @return array
138+
*/
139+
public function getFormSelectors()
140+
{
141+
$forms = $this->getSelectedFormSelectors();
142+
$customForms = array_column($this->getCustomForms(), 'selector');
143+
144+
return array_merge($forms, $customForms);
145+
}
146+
147+
/**
148+
* Get selected form actions.
149+
*
150+
* @return array
151+
*/
152+
public function getFormActions()
153+
{
154+
$forms = $this->getForms();
155+
$customForms = array_column($this->getCustomForms(), 'action');
156+
157+
return array_merge($forms, $customForms);
158+
}
159+
}

Model/System/Config/Source/Forms.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* @category HS
4+
*
5+
* @copyright Copyright (c) 2015 Hungersoft (http://www.hungersoft.com)
6+
* @license http://www.hungersoft.com/license.txt Hungersoft General License
7+
*/
8+
9+
namespace HS\Honeypot\Model\System\Config\Source;
10+
11+
use Magento\Framework\Option\ArrayInterface;
12+
13+
class Forms implements ArrayInterface
14+
{
15+
const TYPE_LOGIN = 'customer_account_loginPost';
16+
const TYPE_CREATE = 'customer_account_createpost';
17+
const TYPE_FORGOT = 'customer_account_forgotpasswordpost';
18+
const TYPE_CONTACT = 'contact_index_post';
19+
const TYPE_CHANGE_PASSWORD = 'customer_account_editPost';
20+
const TYPE_PRODUCT_REVIEW = 'review_product_post';
21+
22+
/**
23+
* @return array
24+
*/
25+
public function toOptionArray()
26+
{
27+
$options = [];
28+
foreach ($this->getOptions() as $value => $label) {
29+
$options[] = [
30+
'value' => $value,
31+
'label' => $label,
32+
];
33+
}
34+
35+
return $options;
36+
}
37+
38+
/**
39+
* @return array
40+
*/
41+
public function getOptions()
42+
{
43+
return [
44+
self::TYPE_LOGIN => __('Login'),
45+
self::TYPE_CREATE => __('Create User'),
46+
self::TYPE_FORGOT => __('Forgot Password'),
47+
self::TYPE_CONTACT => __('Contact Us'),
48+
self::TYPE_CHANGE_PASSWORD => __('Change Password'),
49+
self::TYPE_PRODUCT_REVIEW => __('Product Review'),
50+
];
51+
}
52+
}

Observer/ActionPredispatch.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
/**
3+
* Copyright 2019 Hungersoft (http://www.hungersoft.com).
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace HS\Honeypot\Observer;
19+
20+
use Magento\Framework\App\Request\Http;
21+
use HS\Honeypot\Helper\Data;
22+
use Magento\Framework\App\Action\Action;
23+
use Magento\Framework\App\ActionFlag;
24+
use Magento\Framework\App\Response\RedirectInterface;
25+
use Magento\Framework\App\ResponseInterface;
26+
use Magento\Framework\Message\ManagerInterface;
27+
28+
class ActionPredispatch implements \Magento\Framework\Event\ObserverInterface
29+
{
30+
/**
31+
* @var Data
32+
*/
33+
protected $helper;
34+
35+
/**
36+
* @var Http
37+
*/
38+
protected $request;
39+
40+
/**
41+
* @var ResponseInterface
42+
*/
43+
protected $response;
44+
45+
/**
46+
* @var ActionFlag
47+
*/
48+
private $actionFlag;
49+
50+
/**
51+
* @var ManagerInterface
52+
*/
53+
protected $messageManager;
54+
55+
/**
56+
* @var RedirectInterface
57+
*/
58+
protected $redirect;
59+
60+
/**
61+
* @param Data $helper
62+
* @param Http $request
63+
* @param ActionFlag $actionFlag
64+
* @param ResponseInterface $response
65+
* @param RedirectInterface $redirect
66+
* @param ManagerInterface $messageManager
67+
*/
68+
public function __construct(
69+
Data $helper,
70+
Http $request,
71+
ActionFlag $actionFlag,
72+
ResponseInterface $response,
73+
RedirectInterface $redirect,
74+
ManagerInterface $messageManager
75+
) {
76+
$this->helper = $helper;
77+
$this->request = $request;
78+
$this->redirect = $redirect;
79+
$this->response = $response;
80+
$this->actionFlag = $actionFlag;
81+
$this->messageManager = $messageManager;
82+
}
83+
84+
/**
85+
* Execute observer.
86+
*
87+
* @param \Magento\Framework\Event\Observer $observer
88+
*/
89+
public function execute(
90+
\Magento\Framework\Event\Observer $observer
91+
) {
92+
if (!$this->helper->isEnabled()
93+
|| !in_array($this->request->getFullActionName(), $this->helper->getFormActions())
94+
) {
95+
return $this;
96+
}
97+
98+
if ($this->request->getParam('hs_hid') !== '') {
99+
$this->processError();
100+
}
101+
}
102+
103+
/**
104+
* Process error.
105+
*
106+
* @return array | void
107+
*/
108+
private function processError()
109+
{
110+
$message = __('Unauthorized form submission!');
111+
if ($this->request->isAjax()) {
112+
return [
113+
'success' => false,
114+
'error' => true,
115+
'message' => $message,
116+
];
117+
}
118+
119+
$this->messageManager->getMessages(true);
120+
$this->messageManager->addErrorMessage($message);
121+
$this->actionFlag->set('', Action::FLAG_NO_DISPATCH, true);
122+
$this->response->setRedirect($this->redirect->getRefererUrl());
123+
}
124+
}

0 commit comments

Comments
 (0)