Passed
Push — master ( 59abca...fa3542 )
by Mihail
05:32
created

Extend/Core/Captcha/Recaptcha.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Extend\Core\Captcha;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Helper\FileSystem\File;
7
use Ffcms\Core\Helper\Url;
0 ignored issues
show
The type Ffcms\Core\Helper\Url was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Ffcms\Core\Interfaces\iCaptcha;
9
use Symfony\Component\HttpFoundation\Request;
10
11
class Recaptcha implements iCaptcha
12
{
13
    private static $siteKey;
14
    private static $secret;
15
16
    /**
17
     * Set site key (public key) and secret (private key) on init
18
     * @param string $siteKey
19
     * @param string $secret
20
     */
21
    public function __construct($siteKey, $secret)
22
    {
23
        self::$siteKey = $siteKey;
24
        self::$secret = $secret;
25
    }
26
27
    /**
28
     * Check is captcha provide 'full-based' output realisation
29
     * @return bool
30
     */
31
    public function isFull()
32
    {
33
        return true;
34
    }
35
36
    /**
37
     * Get captcha image link(isFull():=false) or builded JS code(isFull():=true)
38
     * @return string
39
     */
40
    public function get()
41
    {
42
        // build google captcha ;)
43
        $html = '<div class="g-recaptcha" data-sitekey="' . self::$siteKey . '"></div>
44
            <script type="text/javascript"
45
                    src="https://www.google.com/recaptcha/api.js?hl=' . App::$Request->getLanguage() . '">
46
            </script>';
47
        return $html;
48
    }
49
50
    /**
51
     * Validate input data from captcha
52
     * @param string|null $data
53
     * @return bool
54
     */
55
    public static function validate($data = null)
56
    {
57
        // nevertheless what we got in our model, recaptcha is suck and don't allow to change response field name
58
        $data = App::$Request->get('g-recaptcha-response');
59
60
        // make validation
61
        $request = Request::create('https://www.google.com/recaptcha/api/siteverify', 'GET', [
62
            'secret' => self::$secret,
63
            'response' => $data,
64
            'remoteip' => App::$Request->getClientIp()
65
        ]);
66
67
        // make request and parse response
68
        $url = $request->getSchemeAndHttpHost() . $request->getRequestUri();
69
        $response = File::getFromUrl($url);
70
        $object = json_decode($response);
71
72
        return $object->success;
73
    }
74
}
75