Completed
Push — master ( f50c69...a38316 )
by Iurii
01:42
created

Recaptcha::hookConstructControllerFrontend()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.439
c 0
b 0
f 0
cc 6
eloc 22
nc 5
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
     * Constructor
22
     */
23
    public function __construct()
24
    {
25
        parent::__construct();
26
    }
27
28
    /**
29
     * Implements hook "module.install.before"
30
     */
31
    public function hookModuleInstallBefore(&$result)
32
    {
33
        if (!function_exists('curl_init')) {
34
            $result = 'CURL library is not enabled';
35
        }
36
    }
37
38
    /**
39
     * Implements hook "construct.controller"
40
     * @param \gplcart\core\controllers\frontend\Controller $object
41
     */
42
    public function hookConstructControllerFrontend($object)
43
    {
44
        $settings = $this->config->module('recaptcha');
45
46
        if (empty($settings['key']) || empty($settings['secret'])) {
47
            return null;
48
        }
49
50
        $html = $object->render('recaptcha|recaptcha', array('recaptcha_key' => $settings['key']));
51
        $object->setData('_captcha', $html);
52
53
        if (!$object->isPosted('g-recaptcha-response')) {
54
            return null;
55
        }
56
57
        /* @var $curl \gplcart\core\helpers\Curl */
58
        $curl = $this->getInstance('gplcart\\core\\helpers\\Curl');
59
60
        /* @var $request \gplcart\core\helpers\Request */
61
        $request = $this->getInstance('gplcart\\core\\helpers\\Request');
62
63
        $fields = array(
64
            'remoteip' => $request->ip(),
65
            'secret' => $settings['secret'],
66
            'response' => $object->getPosted('g-recaptcha-response', '', true, 'string')
67
        );
68
69
        $url = 'https://www.google.com/recaptcha/api/siteverify';
70
71
        try {
72
            $response = json_decode($curl->post($url, array('fields' => $fields)));
73
        } catch (\Exception $ex) {
74
            $object->setError('recaptcha', $ex->getMessage());
75
            return null;
76
        }
77
78
        if (empty($response->success)) {
79
            $object->setError('recaptcha', 'You are spammer!');
80
        }
81
    }
82
83
    /**
84
     * Implements hook "route.list"
85
     * @param array $routes
86
     */
87
    public function hookRouteList(array &$routes)
88
    {
89
        // Module settings page
90
        $routes['admin/module/settings/recaptcha'] = array(
91
            'access' => 'module_edit',
92
            'handlers' => array(
93
                'controller' => array('gplcart\\modules\\recaptcha\\controllers\\Settings', 'editSettings')
94
            )
95
        );
96
    }
97
98
}
99