Completed
Push — master ( 1b75d6...4ac9e4 )
by Iurii
01:52
created

Recaptcha::info()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
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
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 "construct.controller"
30
     * @param \gplcart\core\controllers\frontend\Controller $object
31
     */
32
    public function hookConstructControllerFrontend($object)
33
    {
34
        $settings = $this->config->module('recaptcha');
35
36
        if (empty($settings['key']) || empty($settings['secret'])) {
37
            return null;
38
        }
39
40
        $vars = array('recaptcha_key' => $settings['key']);
41
        $html = $object->render('recaptcha|recaptcha', $vars);
42
        $object->setData('captcha', $html);
43
44
        if (!$object->isPosted('g-recaptcha-response')) {
45
            return null;
46
        }
47
48
        /* @var $curl \gplcart\core\helpers\Curl */
49
        $curl = $this->getInstance('gplcart\\core\\helpers\\Curl');
50
51
        $fields = array(
52
            'remoteip' => $object->ip(),
53
            'secret' => $settings['secret'],
54
            'response' => $object->getPosted('g-recaptcha-response')
55
        );
56
57
        $url = 'https://www.google.com/recaptcha/api/siteverify';
58
        $response = json_decode($curl->post($url, array('fields' => $fields)));
59
60
        if (empty($response->success)) {
61
            $object->setError('recaptcha', 'You are spammer!');
62
        }
63
    }
64
65
    /**
66
     * Implements hook "route.list"
67
     * @param array $routes
68
     */
69
    public function hookRouteList(array &$routes)
70
    {
71
        // Module settings page
72
        $routes['admin/module/settings/recaptcha'] = array(
73
            'access' => 'module_edit',
74
            'handlers' => array(
75
                'controller' => array('gplcart\\modules\\recaptcha\\controllers\\Settings', 'editSettings')
76
            )
77
        );
78
    }
79
80
}
81