Issues (994)

src/GoogleExt/recaptcha.php (9 issues)

1
<?php
2
3
namespace GoogleExt;
4
5
use Extender\request;
6
use JSON\json;
7
8
class recaptcha
9
{
10
  public $secret = '6LeLW-MUAAAAADaHQWVpUV5CqjNymO0cu_gbL0vv';
11
  public $siteKey = '6LeLW-MUAAAAALgiXAKP0zo2oslXXbCy57CjFcie';
12
  public static $secretKey;
13
  private static $_instance = null;
14
15
  public static function getInstance()
16
  {
17
    if (null === self::$_instance) {
18
      self::$_instance = new self();
19
    }
20
21
    return self::$_instance;
22
  }
23
24
  public function set_secret($key)
25
  {
26
    $this->secret = $key;
27
  }
28
29
  public function setSecret($key)
30
  {
31
    $this->set_secret($key);
32
  }
33
34
  public static function verifyCaptchaOld($callback = null, $error = null)
0 ignored issues
show
The parameter $error is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

34
  public static function verifyCaptchaOld($callback = null, /** @scrutinizer ignore-unused */ $error = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $callback is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

34
  public static function verifyCaptchaOld(/** @scrutinizer ignore-unused */ $callback = null, $error = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
  {
36
    $opt['url'] = 'https://www.google.com/recaptcha/api/siteverify?secret=' . self::getInstance()->secret . '&response=' . $_REQUEST['g-recaptcha-response'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$opt was never initialized. Although not strictly required by PHP, it is generally a good practice to add $opt = array(); before regardless.
Loading history...
37
38
    $req = request::static_request($opt);
39
40
    exit(var_dump($req));
0 ignored issues
show
Are you sure the usage of var_dump($req) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Security Debugging Code introduced by
var_dump($req) looks like debug code. Are you sure you do not want to remove it?
Loading history...
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
41
42
    if (isset($req['response']['body']['success']) && false !== $req['response']['body']['success']) {
0 ignored issues
show
IfNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
43
      if ($req['response']['body']['success']) {
44
        if (is_callable($callback)) {
45
          return call_user_func($callback);
46
        }
47
      }
48
    } elseif (is_callable($error)) {
49
      return call_user_func($error);
50
    } else {
51
      if (isset($req['response']['body'])) {
52
        $req = $req['response']['body'];
53
      }
54
      json::json($req);
55
    }
56
  }
57
58
  public static function verifyCaptcha($callback)
59
  {
60
    if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) {
61
      $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP'];
62
    }
63
    if (preg_match('/^192|^127/s', $_SERVER['REMOTE_ADDR'])) {
64
      //return call_user_func($callback, true);
65
    }
66
    if (!isset($_POST['g-recaptcha-response'])) {
67
      if (ob_get_level()) {
68
        ob_end_clean();
69
      }
70
      \JSON\json::json(['error' => true, 'message' => 'Recaptcha token required']);
71
      exit;
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
72
    }
73
    // Verify captcha
74
    $post_data = http_build_query(
75
      [
76
        'secret' => self::getInstance()->secret,
77
        'response' => $_POST['g-recaptcha-response'],
78
        'remoteip' => $_SERVER['REMOTE_ADDR'],
79
      ]
80
    );
81
    $opts = [
82
      'http' => [
83
        'method' => 'POST',
84
        'header' => 'Content-type: application/x-www-form-urlencoded',
85
        'content' => $post_data,
86
      ],
87
    ];
88
    $context = stream_context_create($opts);
89
    $response = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context);
90
    $result = json_decode($response, true);
91
92
    if (!$result['success']) {
93
      $result['error'] = true;
94
      unset($result['success']);
95
      $result['message'] = 'Captcha not valid, please reload the page or submit the form again';
96
      $result['title'] = 'reCaptcha information';
97
      json::json($result);
98
      exit;
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
99
    } else {
100
      return call_user_func($callback, true);
101
    }
102
  }
103
}
104