Completed
Push — master ( 8a7a85...89e44e )
by Iurii
01:13
created

Main::getHttpModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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