-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeptacomBackendCaptcha.php
151 lines (133 loc) · 4.95 KB
/
HeptacomBackendCaptcha.php
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
namespace HeptacomBackendCaptcha;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Enlight_Event_EventArgs;
use Enlight_Controller_Action;
use Enlight_Controller_Request_Request;
use Enlight_Controller_Plugins_ViewRenderer_Bootstrap;
use Shopware\Components\Logger;
use Shopware\Components\Plugin;
use Shopware\Components\Plugin\Context\InstallContext;
use Shopware\Components\Plugin\Context\UpdateContext;
use Shopware\Components\Plugin\Context\ActivateContext;
use Shopware\Components\Plugin\Context\DeactivateContext;
use HeptacomBackendCaptcha\Service\CaptchaService;
class HeptacomBackendCaptcha extends Plugin
{
/**
* @param UpdateContext $context
*/
public function update(UpdateContext $context)
{
$context->scheduleClearCache(InstallContext::CACHE_LIST_FRONTEND);
}
/**
* @param ActivateContext $context
*/
public function activate(ActivateContext $context)
{
$context->scheduleClearCache(InstallContext::CACHE_LIST_FRONTEND);
}
/**
* @param DeactivateContext $context
*/
public function deactivate(DeactivateContext $context)
{
$context->scheduleClearCache(InstallContext::CACHE_LIST_FRONTEND);
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Enlight_Plugins_ViewRenderer_PreRender' => 'onViewRendererPreRender',
'Enlight_Controller_Action_PostDispatchSecure_Backend' => 'onBackendPostDispatch',
'Enlight_Controller_Action_Backend_Login_Login' => 'onBackendLoginLoginAction',
];
}
/**
* @param Enlight_Event_EventArgs $args
* @return bool|null
*/
public function onBackendLoginLoginAction(Enlight_Event_EventArgs $args)
{
/** @var Enlight_Controller_Action $controller */
$controller = $args->get('subject');
$request = $controller->Request();
try {
/** @var CaptchaService $captchaService */
$captchaService = $this->container->get('heptacom_backend_captcha.service.captcha_service');
}
catch (ServiceNotFoundException $exception) {
/**@var Logger $pluginLogger */
$pluginLogger = $this->container->get('PluginLogger');
$pluginLogger->error($exception->getMessage());
return null;
}
if ($captchaService->hasKeys()) {
$gRecaptchaResponse = $request->get('g-recaptcha-response');
$remoteip = $request->getClientIp();
if (!$captchaService->evaluate($gRecaptchaResponse, $remoteip)) {
return false;
}
}
return null;
}
/**
* @param Enlight_Event_EventArgs $args
*/
public function onBackendPostDispatch(Enlight_Event_EventArgs $args)
{
/** @var Enlight_Controller_Action $controller */
$controller = $args->get('subject');
$view = $controller->View();
try {
/** @var CaptchaService $captchaService */
$captchaService = $this->container->get('heptacom_backend_captcha.service.captcha_service');
if ($captchaService->hasKeys()) {
$view->assign('heptacomBackendCaptcha', [
'visibility' => (bool) Shopware()->Config()->getByNamespace('HeptacomBackendCaptcha', 'visibility'),
'sitekey' => $captchaService->getSitekey(),
]);
$view->addTemplateDir(implode(DIRECTORY_SEPARATOR, [$this->getPath(), 'Resources', 'views']));
$view->extendsTemplate(implode(DIRECTORY_SEPARATOR, [
'backend',
'plugins',
'heptacom_backend_captcha',
'header.tpl'
]));
$view->extendsTemplate(implode(DIRECTORY_SEPARATOR, [
'backend',
'plugins',
'heptacom_backend_captcha',
'view',
'main',
'form.js'
]));
}
}
catch (ServiceNotFoundException $exception) {
/**@var Logger $pluginLogger */
$pluginLogger = $this->container->get('PluginLogger');
$pluginLogger->error($exception->getMessage());
}
}
/**
* @param Enlight_Event_EventArgs $args
*/
public function onViewRendererPreRender(Enlight_Event_EventArgs $args)
{
/** @var Enlight_Controller_Plugins_ViewRenderer_Bootstrap $subject */
$subject = $args->get('subject');
$view = $subject->Action()->View();
/** @var Enlight_Controller_Request_Request $request */
$request = $args->get('request');
$module = $request->getModuleName();
$controller = strtolower(trim($request->getControllerName()));
if ($module == 'backend'
&& $controller == 'index') {
$view->Engine()->clearAllCache();
}
}
}