Completed
Push — master ( cb0e93...c6ae02 )
by Iurii
01:08
created

Recaptcha::getSocketClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 Recaptcha
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
     * @raram array $settings
78
     * @return null|bool
79
     */
80
    protected function processRecaptcha($controller, $settings)
81
    {
82
        if ($controller->isPosted('g-recaptcha-response')) {
83
            $response = $this->queryRecaptcha($controller, $settings);
84
            if (empty($response->success)) {
85
                $controller->setError('recaptcha', $controller->text('You are spammer!'));
86
                return false;
87
            }
88
            return true;
89
        }
90
91
        return null;
92
    }
93
94
    /**
95
     * Post query to Recaptcha service
96
     * @param \gplcart\core\controllers\frontend\Controller $controller
97
     * @param array $settings
98
     * @return object|null
99
     */
100
    protected function queryRecaptcha($controller, array $settings)
101
    {
102
        $options = array(
103
            'method' => 'POST',
104
            'data' => array(
105
                'secret' => $settings['secret'],
106
                'remoteip' => $controller->getIp(),
107
                'response' => $controller->getPosted('g-recaptcha-response', '', true, 'string')
108
            ),
109
        );
110
111
        $url = 'https://www.google.com/recaptcha/api/siteverify';
112
113
        try {
114
            $response = $this->getSocketClient()->request($url, $options);
115
            return json_decode($response['data']);
116
        } catch (\Exception $ex) {
117
            trigger_error($ex->getMessage());
118
            return null;
119
        }
120
    }
121
122
    /**
123
     * Returns Socket client helper class instance
124
     * @return \gplcart\core\helpers\SocketClient
125
     */
126
    protected function getSocketClient()
127
    {
128
        return Container::get('gplcart\\core\\helpers\\SocketClient');
129
    }
130
131
}
132