Completed
Push — master ( 831f0c...c56d4b )
by Iurii
01:16
created

Main::setRecaptcha()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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