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