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 extends Module |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Constructor |
22
|
|
|
*/ |
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
parent::__construct(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Implements hook "module.install.before" |
30
|
|
|
*/ |
31
|
|
|
public function hookModuleInstallBefore(&$result) |
32
|
|
|
{ |
33
|
|
|
if (!function_exists('curl_init')) { |
34
|
|
|
$result = 'CURL library is not enabled'; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Implements hook "construct.controller" |
40
|
|
|
* @param \gplcart\core\controllers\frontend\Controller $object |
41
|
|
|
*/ |
42
|
|
|
public function hookConstructControllerFrontend($object) |
43
|
|
|
{ |
44
|
|
|
$settings = $this->config->module('recaptcha'); |
45
|
|
|
|
46
|
|
|
if (empty($settings['key']) || empty($settings['secret'])) { |
47
|
|
|
return null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$html = $object->render('recaptcha|recaptcha', array('recaptcha_key' => $settings['key'])); |
51
|
|
|
$object->setData('_captcha', $html); |
52
|
|
|
|
53
|
|
|
if (!$object->isPosted('g-recaptcha-response')) { |
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/* @var $curl \gplcart\core\helpers\Curl */ |
58
|
|
|
$curl = $this->getInstance('gplcart\\core\\helpers\\Curl'); |
59
|
|
|
|
60
|
|
|
/* @var $request \gplcart\core\helpers\Request */ |
61
|
|
|
$request = $this->getInstance('gplcart\\core\\helpers\\Request'); |
62
|
|
|
|
63
|
|
|
$fields = array( |
64
|
|
|
'remoteip' => $request->ip(), |
65
|
|
|
'secret' => $settings['secret'], |
66
|
|
|
'response' => $object->getPosted('g-recaptcha-response', '', true, 'string') |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$url = 'https://www.google.com/recaptcha/api/siteverify'; |
70
|
|
|
|
71
|
|
|
try { |
72
|
|
|
$response = json_decode($curl->post($url, array('fields' => $fields))); |
73
|
|
|
} catch (\Exception $ex) { |
74
|
|
|
$object->setError('recaptcha', $ex->getMessage()); |
75
|
|
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (empty($response->success)) { |
79
|
|
|
$object->setError('recaptcha', 'You are spammer!'); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Implements hook "route.list" |
85
|
|
|
* @param array $routes |
86
|
|
|
*/ |
87
|
|
|
public function hookRouteList(array &$routes) |
88
|
|
|
{ |
89
|
|
|
// Module settings page |
90
|
|
|
$routes['admin/module/settings/recaptcha'] = array( |
91
|
|
|
'access' => 'module_edit', |
92
|
|
|
'handlers' => array( |
93
|
|
|
'controller' => array('gplcart\\modules\\recaptcha\\controllers\\Settings', 'editSettings') |
94
|
|
|
) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|