Completed
Push — master ( 3bcf62...b4a99d )
by Iurii
01:06
created

Recaptcha::hookConstructControllerFrontend()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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