Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/tutor-pro/addons/auth/classes/Recaptcha.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + /**
3 + * Spam protection by Google reCAPTCHA
4 + *
5 + * @package TutorPro\Auth
6 + * @author Themeum <support@themeum.com>
7 + * @link https://themeum.com
8 + * @since 2.1.9
9 + */
10 +
11 + namespace TutorPro\Auth;
12 +
13 + /**
14 + * Recaptcha Class.
15 + *
16 + * @since 2.1.9
17 + */
18 + class Recaptcha {
19 + const VERSION_V2 = 'v2';
20 + const VERSION_V3 = 'v3';
21 + const ERROR_CODE = 'tutor_recaptcha_error';
22 +
23 + /**
24 + * Load reCAPTCHA form based on version and site key.
25 + *
26 + * @since 2.1.9
27 + *
28 + * @param string $version reCAPTCHA version.
29 + * @param string $site_key site key.
30 + *
31 + * @return void
32 + */
33 + public static function form_content( $version, $site_key ) {
34 + if ( empty( $site_key ) ) {
35 + return;
36 + }
37 +
38 + /**
39 + * reCAPTCHA v2 form content
40 + */
41 + if ( self::VERSION_V2 === $version ) {
42 + ?>
43 + <script src='https://www.google.com/recaptcha/api.js' async defer></script>
44 + <div class="g-recaptcha"
45 + style="margin-bottom: 15px;transform: scale(0.89);transform-origin:0 0;"
46 + data-sitekey="<?php echo esc_attr( $site_key ); ?>"></div>
47 + <?php
48 + }
49 +
50 + /**
51 + * reCAPTCHA v3 form content
52 + */
53 + if ( self::VERSION_V3 === $version ) {
54 + ?>
55 + <script src="https://www.google.com/recaptcha/api.js?render=<?php echo esc_attr( $site_key ); ?>"></script>
56 +
57 + <input type="hidden" id="recaptcha_token" name="recaptcha_token">
58 + <script>
59 + grecaptcha.ready(function() {
60 + grecaptcha.execute('<?php echo esc_attr( $site_key ); ?>', {action: 'form_submit'}).then(function(token) {
61 + document.getElementById('recaptcha_token').value = token;
62 + });
63 + });
64 + </script>
65 + <?php
66 + }
67 + }
68 +
69 + /**
70 + * Verify reCAPTCHA response.
71 + *
72 + * @since 2.1.9
73 + *
74 + * @param string $response captcha response.
75 + * @param string $secret_key secret key.
76 + *
77 + * @return void|\WP_Error
78 + */
79 + public static function verify( $response, $secret_key ) {
80 + $url = 'https://www.google.com/recaptcha/api/siteverify';
81 +
82 + $data = array(
83 + 'secret' => $secret_key,
84 + 'response' => $response,
85 + );
86 +
87 + $options = array(
88 + 'http' => array(
89 + 'header' => 'Content-type: application/x-www-form-urlencoded',
90 + 'method' => 'POST',
91 + 'content' => http_build_query( $data ),
92 + ),
93 + );
94 +
95 + $context = stream_context_create( $options );
96 + $result = file_get_contents( $url, false, $context );
97 +
98 + if ( $result === false ) {
99 + return new \WP_Error( self::ERROR_CODE, __( 'Something went wrong', 'tutor-pro' ) );
100 + }
101 +
102 + $result = json_decode( $result );
103 + if ( ! $result->success ) {
104 + return new \WP_Error( self::ERROR_CODE, __( 'Spam request catched by Google reCAPTCHA', 'tutor-pro' ) );
105 + }
106 + }
107 + }
108 +