1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package reCAPTCHA |
5
|
|
|
* @author Iurii Makukh <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2017, Iurii Makukh <[email protected]> |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\recaptcha; |
11
|
|
|
|
12
|
|
|
use Exception; |
13
|
|
|
use gplcart\core\Container; |
14
|
|
|
use gplcart\core\controllers\frontend\Controller; |
15
|
|
|
use gplcart\core\Module; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Main class for reCAPTCHA module |
19
|
|
|
*/ |
20
|
|
|
class Main |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Module class instance |
25
|
|
|
* @var \gplcart\core\Module $module |
26
|
|
|
*/ |
27
|
|
|
protected $module; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Module $module |
31
|
|
|
*/ |
32
|
|
|
public function __construct(Module $module) |
33
|
|
|
{ |
34
|
|
|
$this->module = $module; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Implements hook "construct.controller" |
39
|
|
|
* @param Controller $controller |
40
|
|
|
*/ |
41
|
|
|
public function hookConstructControllerFrontend(Controller $controller) |
42
|
|
|
{ |
43
|
|
|
$this->setRecaptcha($controller); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Implements hook "route.list" |
48
|
|
|
* @param array $routes |
49
|
|
|
*/ |
50
|
|
|
public function hookRouteList(array &$routes) |
51
|
|
|
{ |
52
|
|
|
$routes['admin/module/settings/recaptcha'] = array( |
53
|
|
|
'access' => 'module_edit', |
54
|
|
|
'handlers' => array( |
55
|
|
|
'controller' => array('gplcart\\modules\\recaptcha\\controllers\\Settings', 'editSettings') |
56
|
|
|
) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns rendered rECAPTCHA widget |
62
|
|
|
* @param Controller $controller |
63
|
|
|
* @param array $settings |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function getWidget(Controller $controller, array $settings) |
67
|
|
|
{ |
68
|
|
|
return $controller->render('recaptcha|recaptcha', array('recaptcha_key' => $settings['key'])); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Post query to Recaptcha service |
73
|
|
|
* @param Controller $controller |
74
|
|
|
* @param array $settings |
75
|
|
|
* @return mixed|null |
76
|
|
|
*/ |
77
|
|
|
public function request(Controller $controller, array $settings) |
78
|
|
|
{ |
79
|
|
|
$options = array( |
80
|
|
|
'method' => 'POST', |
81
|
|
|
'data' => array( |
82
|
|
|
'secret' => $settings['secret'], |
83
|
|
|
'remoteip' => $controller->getIp(), |
84
|
|
|
'response' => $controller->getPosted('g-recaptcha-response', '', true, 'string') |
85
|
|
|
), |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$url = 'https://www.google.com/recaptcha/api/siteverify'; |
89
|
|
|
|
90
|
|
|
try { |
91
|
|
|
$response = $this->getHttpModel()->request($url, $options); |
92
|
|
|
return json_decode($response['data']); |
93
|
|
|
} catch (Exception $ex) { |
94
|
|
|
trigger_error($ex->getMessage()); |
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Process reCAPTCHA response |
101
|
|
|
* @param Controller $controller |
102
|
|
|
* @param array $settings |
103
|
|
|
* @return null|bool |
104
|
|
|
*/ |
105
|
|
|
public function process(Controller $controller, array $settings) |
106
|
|
|
{ |
107
|
|
|
if ($controller->isPosted('g-recaptcha-response')) { |
108
|
|
|
$response = $this->request($controller, $settings); |
109
|
|
|
return !empty($response->success); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Render and add CAPTCHA |
117
|
|
|
* @param Controller $controller |
118
|
|
|
*/ |
119
|
|
|
protected function setRecaptcha(Controller $controller) |
120
|
|
|
{ |
121
|
|
|
if (!$controller->isInternalRoute()) { |
122
|
|
|
$settings = $this->module->getSettings('recaptcha'); |
123
|
|
|
if (!empty($settings['key']) && !empty($settings['secret'])) { |
124
|
|
|
$controller->setData('_captcha', $this->getWidget($controller, $settings)); |
125
|
|
|
$result = $this->process($controller, $settings); |
126
|
|
|
if (isset($result) && empty($result)) { |
127
|
|
|
$controller->setError('_captcha', 'Spam submission'); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns Http model class instance |
135
|
|
|
* @return \gplcart\core\models\Http |
136
|
|
|
*/ |
137
|
|
|
protected function getHttpModel() |
138
|
|
|
{ |
139
|
|
|
/** @var \gplcart\core\models\Http $instance */ |
140
|
|
|
$instance = Container::get('gplcart\\core\\models\\Http'); |
141
|
|
|
return $instance; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|