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 gplcart\core\Module; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Main class for reCAPTCHA module |
16
|
|
|
*/ |
17
|
|
|
class Recaptcha |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Module class instance |
22
|
|
|
* @var \gplcart\core\Module $module |
23
|
|
|
*/ |
24
|
|
|
protected $module; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param Module $module |
28
|
|
|
*/ |
29
|
|
|
public function __construct(Module $module) |
30
|
|
|
{ |
31
|
|
|
$this->module = $module; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Implements hook "construct.controller" |
36
|
|
|
* @param \gplcart\core\controllers\frontend\Controller $controller |
37
|
|
|
*/ |
38
|
|
|
public function hookConstructControllerFrontend($controller) |
39
|
|
|
{ |
40
|
|
|
$this->setRecaptcha($controller); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Implements hook "route.list" |
45
|
|
|
* @param array $routes |
46
|
|
|
*/ |
47
|
|
|
public function hookRouteList(array &$routes) |
48
|
|
|
{ |
49
|
|
|
$routes['admin/module/settings/recaptcha'] = array( |
50
|
|
|
'access' => 'module_edit', |
51
|
|
|
'handlers' => array( |
52
|
|
|
'controller' => array('gplcart\\modules\\recaptcha\\controllers\\Settings', 'editSettings') |
53
|
|
|
) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Render and add CAPTCHA |
59
|
|
|
* @param \gplcart\core\controllers\frontend\Controller $controller |
60
|
|
|
*/ |
61
|
|
|
protected function setRecaptcha($controller) |
62
|
|
|
{ |
63
|
|
|
if (!$controller->isInternalRoute()) { |
64
|
|
|
$settings = $this->module->getSettings('recaptcha'); |
65
|
|
|
if (!empty($settings['key']) && !empty($settings['secret'])) { |
66
|
|
|
$html = $controller->render('recaptcha|recaptcha', array('recaptcha_key' => $settings['key'])); |
67
|
|
|
$controller->setData('_captcha', $html); |
68
|
|
|
$this->processRecaptcha($controller, $settings); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Process reCAPTCHA's response |
75
|
|
|
* @param \gplcart\core\controllers\frontend\Controller $controller |
76
|
|
|
* @raram array $settings |
77
|
|
|
* @return null|bool |
78
|
|
|
*/ |
79
|
|
|
protected function processRecaptcha($controller, $settings) |
80
|
|
|
{ |
81
|
|
|
if ($controller->isPosted('g-recaptcha-response')) { |
82
|
|
|
$response = $this->queryRecaptcha($controller, $settings); |
83
|
|
|
if (empty($response->success)) { |
84
|
|
|
$controller->setError('recaptcha', $controller->text('You are spammer!')); |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Post query to Recaptcha service |
95
|
|
|
* @param \gplcart\core\controllers\frontend\Controller $controller |
96
|
|
|
* @param array $settings |
97
|
|
|
* @return object|null |
98
|
|
|
*/ |
99
|
|
|
protected function queryRecaptcha($controller, array $settings) |
100
|
|
|
{ |
101
|
|
|
$options = array( |
102
|
|
|
'method' => 'POST', |
103
|
|
|
'data' => array( |
104
|
|
|
'secret' => $settings['secret'], |
105
|
|
|
'remoteip' => $controller->getIp(), |
106
|
|
|
'response' => $controller->getPosted('g-recaptcha-response', '', true, 'string') |
107
|
|
|
), |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
$url = 'https://www.google.com/recaptcha/api/siteverify'; |
111
|
|
|
|
112
|
|
|
try { |
113
|
|
|
$response = $controller->httpRequest($url, $options); |
114
|
|
|
return json_decode($response['data']); |
115
|
|
|
} catch (\Exception $ex) { |
116
|
|
|
trigger_error($ex->getMessage()); |
117
|
|
|
return null; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|