-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathIndex.php
More file actions
122 lines (117 loc) · 4.45 KB
/
Index.php
File metadata and controls
122 lines (117 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* mc-magento2 Magento Component
*
* @category Ebizmarts
* @package mc-magento2
* @author Ebizmarts Team <info@ebizmarts.com>
* @copyright Ebizmarts (http://ebizmarts.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @date: 5/23/17 3:36 PM
* @file: Index.php
*/
namespace Ebizmarts\MailChimp\Controller\WebHook;
use Magento\Framework\App\Action\Action;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\CsrfAwareActionInterface;
use Magento\Framework\App\Request\InvalidRequestException;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\ResultInterface;
class Index extends Action implements CsrfAwareActionInterface
{
const WEBHOOK__PATH = 'mailchimp/webhook/index';
/**
* @var ResultFactory
*/
protected $_resultFactory;
/**
* @var \Ebizmarts\MailChimp\Helper\Data
*/
protected $_helper;
/**
* @var \Ebizmarts\MailChimp\Model\MailChimpWebhookRequestFactory
*/
protected $_chimpWebhookRequestFactory;
private $_remoteAddress;
/**
* Index constructor.
* @param Context $context
* @param \Ebizmarts\MailChimp\Helper\Data $helper
* @param \Ebizmarts\MailChimp\Model\MailChimpWebhookRequestFactory $chimpWebhookRequestFactory
* @param \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress
*/
public function __construct(
Context $context,
\Ebizmarts\MailChimp\Helper\Data $helper,
\Ebizmarts\MailChimp\Model\MailChimpWebhookRequestFactory $chimpWebhookRequestFactory,
\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress
) {
parent::__construct($context);
$this->_resultFactory = $context->getResultFactory();
$this->_helper = $helper;
$this->_chimpWebhookRequestFactory = $chimpWebhookRequestFactory;
$this->_remoteAddress = $remoteAddress;
}
/**
* @inheritDoc
*/
public function createCsrfValidationException(
RequestInterface $request
): ?InvalidRequestException {
return null;
}
/**
* @inheritDoc
*/
public function validateForCsrf(RequestInterface $request): ?bool
{
return true;
}
public function execute()
{
$requestKey = $this->getRequest()->getParam('wkey');
/**
* @var ResultInterface $result
*/
$result = $this->_resultFactory->create(ResultFactory::TYPE_RAW);
$result->setContents('');
if (!$requestKey) {
$this->_helper->log('No wkey parameter from ip: '.$this->_remoteAddress->getRemoteAddress());
$result->setHttpResponseCode(403);
return $result;
}
$key = $this->_helper->getWebhooksKey();
if ($key!=$requestKey) {
$this->_helper->log('wkey parameter is invalid from ip: '.$this->_remoteAddress->getRemoteAddress());
$result->setHttpResponseCode(403);
return $result;
}
if ($this->getRequest()->getPost('type')) {
$request = $this->getRequest()->getPost();
if ($this->_helper->getConfigValue(\Ebizmarts\MailChimp\Helper\Data::XML_PATH_WEBHOOK_ACTIVE) ||
$request['type']==\Ebizmarts\MailChimp\Cron\Webhook::TYPE_SUBSCRIBE) {
try {
$chimpRequest = $this->_chimpWebhookRequestFactory->create();
$chimpRequest->setType($request['type']);
$chimpRequest->setFiredAt($request['fired_at']);
$chimpRequest->setDataRequest($this->_helper->serialize($request['data']));
$chimpRequest->setProcessed(false);
$chimpRequest->getResource()->save($chimpRequest);
$result->setHttpResponseCode(200);
} catch(\Exception $e) {
$this->_helper->log($e->getMessage());
$this->_helper->log($request['data']);
$result->setHttpResponseCode(403);
}
} else {
$this->_helper->log("The two way is off");
}
} else {
$this->_helper->log('An empty request comes from ip: '.$this->_remoteAddress->getRemoteAddress());
$result->setHttpResponseCode(200);
}
return $result;
}
}