Completed
Push — master ( 4f523e...829189 )
by Michael
13s
created

XoopsCaptchaRecaptcha2::verify()   C

Complexity

Conditions 8
Paths 12

Size

Total Lines 38
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 28
nc 12
nop 1
dl 0
loc 38
rs 5.3846
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 31 and the first side effect is on line 26.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
use Xmf\Request;
13
use Xmf\IPAddress;
14
15
/**
16
 * CAPTCHA for Recaptcha mode
17
 *
18
 * @package     class
19
 * @subpackage  CAPTCHA
20
 * @author      Grégory Mage
21
 * @copyright   2016 XOOPS Project (http://xoops.org)
22
 * @license     GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
23
 * @link        http://xoops.org
24
 */
25
26
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
27
28
/**
29
 * Class XoopsCaptchaRecaptcha2
30
 */
31
class XoopsCaptchaRecaptcha2 extends XoopsCaptchaMethod
32
{
33
    /**
34
     * XoopsCaptchaRecaptcha2::isActive()
35
     *
36
     * @return bool
37
     */
38
    public function isActive()
39
    {
40
        return true;
41
    }
42
43
    /**
44
     * XoopsCaptchaRecaptcha2::render()
45
     *
46
     * @return string
47
     */
48
    public function render()
49
    {
50
        $form = '<script src="https://www.google.com/recaptcha/api.js"></script>';
51
        $form .= '<div class="form-group"><div class="g-recaptcha" data-sitekey="'
52
            . $this->config['website_key'] . '"></div></div>';
53
        return $form;
54
    }
55
56
    /**
57
     * XoopsCaptchaRecaptcha2::verify()
58
     *
59
     * @param string|null $sessionName unused for recaptcha
60
     *
61
     * @return bool
62
     */
63
    public function verify($sessionName = null)
64
    {
65
        $isValid = false;
66
        $recaptchaResponse = Request::getString('g-recaptcha-response', '');
67
        $recaptchaVerifyURL = 'https://www.google.com/recaptcha/api/siteverify?secret=' . $this->config['secret_key']
68
            . '&response=' .  $recaptchaResponse . '&remoteip=' . IPAddress::fromRequest()->asReadable();
69
        $usedCurl = false;
70
        if (function_exists('curl_init') && false !== ($curlHandle  = curl_init())) {
71
            curl_setopt($curlHandle, CURLOPT_URL, $recaptchaVerifyURL);
72
            curl_setopt($curlHandle, CURLOPT_FAILONERROR, true);
73
            curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
74
            curl_setopt($curlHandle, CURLOPT_CONNECTTIMEOUT, 5);
75
            $curlReturn = curl_exec($curlHandle);
76
            if (false === $curlReturn) {
77
                trigger_error(curl_error($curlHandle));
78
            } else {
79
                $usedCurl = true;
80
                $recaptchaCheck = json_decode($curlReturn, true);
81
            }
82
            curl_close($curlHandle);
83
        }
84
        if (false === $usedCurl) {
85
            $recaptchaCheck = file_get_contents($recaptchaVerifyURL);
86
            $recaptchaCheck = json_decode($recaptchaCheck, true);
87
        }
88
        if (isset($recaptchaCheck['success']) && $recaptchaCheck['success'] === true) {
89
            $isValid = true;
90
        } else {
91
            /** @var \XoopsCaptcha $captchaInstance */
92
            $captchaInstance = \XoopsCaptcha::getInstance();
93
            /** @var array $recaptchaCheck */
94
            foreach ($recaptchaCheck['error-codes'] as $msg) {
0 ignored issues
show
Bug introduced by
The variable $recaptchaCheck does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
95
                $captchaInstance->message[] = $msg;
96
            }
97
        }
98
99
        return $isValid;
100
    }
101
}
102