/var/www/hkosl.com/aga/wp-content/plugins/contact-form-7/modules/recaptcha.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
<?php

add_action
'wpcf7_init''wpcf7_recaptcha_register_service'10);

function 
wpcf7_recaptcha_register_service() {
    
$integration WPCF7_Integration::get_instance();

    
$integration->add_category'captcha',
        
__'CAPTCHA''contact-form-7' )
    );

    
$integration->add_service'recaptcha',
        
WPCF7_RECAPTCHA::get_instance()
    );
}

add_action'wp_enqueue_scripts''wpcf7_recaptcha_enqueue_scripts'10);

function 
wpcf7_recaptcha_enqueue_scripts() {
    
$service WPCF7_RECAPTCHA::get_instance();

    if ( ! 
$service->is_active() ) {
        return;
    }

    
$url add_query_arg(
        array(
            
'render' => $service->get_sitekey(),
        ),
        
'https://www.google.com/recaptcha/api.js'
    
);

    
wp_enqueue_script'google-recaptcha'$url, array(), '3.0'true );
}

add_filter'wpcf7_form_hidden_fields',
    
'wpcf7_recaptcha_add_hidden_fields'100);

function 
wpcf7_recaptcha_add_hidden_fields$fields ) {
    
$service WPCF7_RECAPTCHA::get_instance();

    if ( ! 
$service->is_active() ) {
        return 
$fields;
    }

    return 
array_merge$fields, array(
        
'g-recaptcha-response' => '',
    ) );
}

add_action'wp_footer''wpcf7_recaptcha_onload_script'40);

function 
wpcf7_recaptcha_onload_script() {
    
$service WPCF7_RECAPTCHA::get_instance();

    if ( ! 
$service->is_active() ) {
        return;
    }

    if ( ! 
wp_script_is'google-recaptcha''done' ) ) {
        return;
    }

    
$actions apply_filters'wpcf7_recaptcha_actions',
        array(
            
'homepage' => 'homepage',
            
'contactform' => 'contactform',
        )
    );

?>
<script type="text/javascript">
( function( grecaptcha, sitekey, actions ) {

    var wpcf7recaptcha = {

        execute: function( action ) {
            grecaptcha.execute(
                sitekey,
                { action: action }
            ).then( function( token ) {
                var forms = document.getElementsByTagName( 'form' );

                for ( var i = 0; i < forms.length; i++ ) {
                    var fields = forms[ i ].getElementsByTagName( 'input' );

                    for ( var j = 0; j < fields.length; j++ ) {
                        var field = fields[ j ];

                        if ( 'g-recaptcha-response' === field.getAttribute( 'name' ) ) {
                            field.setAttribute( 'value', token );
                            break;
                        }
                    }
                }
            } );
        },

        executeOnHomepage: function() {
            wpcf7recaptcha.execute( actions[ 'homepage' ] );
        },

        executeOnContactform: function() {
            wpcf7recaptcha.execute( actions[ 'contactform' ] );
        },

    };

    grecaptcha.ready(
        wpcf7recaptcha.executeOnHomepage
    );

    document.addEventListener( 'change',
        wpcf7recaptcha.executeOnContactform, false
    );

    document.addEventListener( 'wpcf7submit',
        wpcf7recaptcha.executeOnHomepage, false
    );

} )(
    grecaptcha,
    '<?php echo esc_js$service->get_sitekey() ); ?>',
    <?php echo json_encode$actions ), "\n"?>
);
</script>
<?php
}

add_filter'wpcf7_spam''wpcf7_recaptcha_verify_response'9);

function 
wpcf7_recaptcha_verify_response$spam ) {
    if ( 
$spam ) {
        return 
$spam;
    }

    
$service WPCF7_RECAPTCHA::get_instance();

    if ( ! 
$service->is_active() ) {
        return 
$spam;
    }

    
$submission WPCF7_Submission::get_instance();

    
$token = isset( $_POST['g-recaptcha-response'] )
        ? 
trim$_POST['g-recaptcha-response'] ) : '';

    if ( 
$service->verify$token ) ) { // Human
        
$spam false;
    } else { 
// Bot
        
$spam true;

        
$submission->add_spam_log( array(
            
'agent' => 'recaptcha',
            
'reason' => sprintf(
                
__'reCAPTCHA score (%1$.2f) is lower than the threshold (%2$.2f).''contact-form-7' ),
                
$service->get_last_score(),
                
$service->get_threshold()
            ),
        ) );
    }

    return 
$spam;
}

add_action'wpcf7_init''wpcf7_recaptcha_add_form_tag_recaptcha'10);

function 
wpcf7_recaptcha_add_form_tag_recaptcha() {
    
$service WPCF7_RECAPTCHA::get_instance();

    if ( ! 
$service->is_active() ) {
        return;
    }

    
wpcf7_add_form_tag'recaptcha',
        
'__return_empty_string'// no output
        
array( 'display-block' => true )
    );
}

add_action'wpcf7_upgrade''wpcf7_upgrade_recaptcha_v2_v3'10);

function 
wpcf7_upgrade_recaptcha_v2_v3$new_ver$old_ver ) {
    if ( 
version_compare'5.1-dev'$old_ver'<=' ) ) {
        return;
    }

    
$service WPCF7_RECAPTCHA::get_instance();

    if ( ! 
$service->is_active() ) {
        return;
    }

    
// Maybe v2 keys are used now. Warning necessary.
    
WPCF7::update_option'recaptcha_v2_v3_warning'true );
    
WPCF7::update_option'recaptcha'null );
}

add_action'wpcf7_admin_menu''wpcf7_admin_init_recaptcha_v2_v3'10);

function 
wpcf7_admin_init_recaptcha_v2_v3() {
    if ( ! 
WPCF7::get_option'recaptcha_v2_v3_warning' ) ) {
        return;
    }

    
add_filter'wpcf7_admin_menu_change_notice',
        
'wpcf7_admin_menu_change_notice_recaptcha_v2_v3'10);

    
add_action'wpcf7_admin_warnings',
        
'wpcf7_admin_warnings_recaptcha_v2_v3'5);
}

function 
wpcf7_admin_menu_change_notice_recaptcha_v2_v3$counts ) {
    
$counts['wpcf7-integration'] += 1;
    return 
$counts;
}

function 
wpcf7_admin_warnings_recaptcha_v2_v3$page$action$object ) {
    if ( 
'wpcf7-integration' !== $page ) {
        return;
    }

    
$message sprintf(
        
esc_html__"API keys for reCAPTCHA v3 are different from those for v2; keys for v2 don&#8217;t work with the v3 API. You need to register your sites again to get new keys for v3. For details, see %s."'contact-form-7' ) ),
        
wpcf7_link(
            
__'https://contactform7.com/recaptcha/''contact-form-7' ),
            
__'reCAPTCHA (v3)''contact-form-7' )
        )
    );

    echo 
sprintf(
        
'<div class="notice notice-warning"><p>%s</p></div>',
        
$message
    
);
}

if ( ! 
class_exists'WPCF7_Service' ) ) {
    return;
}

class 
WPCF7_RECAPTCHA extends WPCF7_Service {

    private static 
$instance;
    private 
$sitekeys;
    private 
$last_score;

    public static function 
get_instance() {
        if ( empty( 
self::$instance ) ) {
            
self::$instance = new self;
        }

        return 
self::$instance;
    }

    private function 
__construct() {
        
$this->sitekeys WPCF7::get_option'recaptcha' );
    }

    public function 
get_title() {
        return 
__'reCAPTCHA''contact-form-7' );
    }

    public function 
is_active() {
        
$sitekey $this->get_sitekey();
        
$secret $this->get_secret$sitekey );
        return 
$sitekey && $secret;
    }

    public function 
get_categories() {
        return array( 
'captcha' );
    }

    public function 
icon() {
    }

    public function 
link() {
        echo 
wpcf7_link(
            
'https://www.google.com/recaptcha/intro/index.html',
            
'google.com/recaptcha'
        
);
    }

    public function 
get_sitekey() {
        if ( empty( 
$this->sitekeys )
        or ! 
is_array$this->sitekeys ) ) {
            return 
false;
        }

        
$sitekeys array_keys$this->sitekeys );

        return 
$sitekeys[0];
    }

    public function 
get_secret$sitekey ) {
        
$sitekeys = (array) $this->sitekeys;

        if ( isset( 
$sitekeys[$sitekey] ) ) {
            return 
$sitekeys[$sitekey];
        } else {
            return 
false;
        }
    }

    protected function 
log$url$request$response ) {
        
wpcf7_log_remote_request$url$request$response );
    }

    public function 
verify$token ) {
        
$is_human false;

        if ( empty( 
$token ) or ! $this->is_active() ) {
            return 
$is_human;
        }

        
$endpoint 'https://www.google.com/recaptcha/api/siteverify';

        
$sitekey $this->get_sitekey();
        
$secret $this->get_secret$sitekey );

        
$request = array(
            
'body' => array(
                
'secret' => $secret,
                
'response' => $token,
            ),
        );

        
$response wp_remote_postesc_url_raw$endpoint ), $request );

        if ( 
200 != wp_remote_retrieve_response_code$response ) ) {
            if ( 
WP_DEBUG ) {
                
$this->log$endpoint$request$response );
            }

            return 
$is_human;
        }

        
$response_body wp_remote_retrieve_body$response );
        
$response_body json_decode$response_bodytrue );

        
$this->last_score $score = isset( $response_body['score'] )
            ? 
$response_body['score']
            : 
0;

        
$threshold $this->get_threshold();
        
$is_human $threshold $score;

        
$is_human apply_filters'wpcf7_recaptcha_verify_response',
            
$is_human$response_body );

        if ( 
$submission WPCF7_Submission::get_instance() ) {
            
$submission->recaptcha = array(
                
'version' => '3.0',
                
'threshold' => $threshold,
                
'response' => $response_body,
            );
        }

        return 
$is_human;
    }

    public function 
get_threshold() {
        return 
apply_filters'wpcf7_recaptcha_threshold'0.50 );
    }

    public function 
get_last_score() {
        return 
$this->last_score;
    }

    protected function 
menu_page_url$args '' ) {
        
$args wp_parse_args$args, array() );

        
$url menu_page_url'wpcf7-integration'false );
        
$url add_query_arg( array( 'service' => 'recaptcha' ), $url );

        if ( ! empty( 
$args) ) {
            
$url add_query_arg$args$url );
        }

        return 
$url;
    }

    protected function 
save_data() {
        
WPCF7::update_option'recaptcha'$this->sitekeys );
    }

    protected function 
reset_data() {
        
$this->sitekeys null;
        
$this->save_data();
    }

    public function 
load$action '' ) {
        if ( 
'setup' == $action and 'POST' == $_SERVER['REQUEST_METHOD'] ) {
            
check_admin_referer'wpcf7-recaptcha-setup' );

            if ( ! empty( 
$_POST['reset'] ) ) {
                
$this->reset_data();
                
$redirect_to $this->menu_page_url'action=setup' );
            } else {
                
$sitekey = isset( $_POST['sitekey'] ) ? trim$_POST['sitekey'] ) : '';
                
$secret = isset( $_POST['secret'] ) ? trim$_POST['secret'] ) : '';

                if ( 
$sitekey and $secret ) {
                    
$this->sitekeys = array( $sitekey => $secret );
                    
$this->save_data();

                    
$redirect_to $this->menu_page_url( array(
                        
'message' => 'success',
                    ) );
                } else {
                    
$redirect_to $this->menu_page_url( array(
                        
'action' => 'setup',
                        
'message' => 'invalid',
                    ) );
                }
            }

            if ( 
WPCF7::get_option'recaptcha_v2_v3_warning' ) ) {
                
WPCF7::update_option'recaptcha_v2_v3_warning'false );
            }

            
wp_safe_redirect$redirect_to );
            exit();
        }
    }

    public function 
admin_notice$message '' ) {
        if ( 
'invalid' == $message ) {
            echo 
sprintf(
                
'<div class="error notice notice-error is-dismissible"><p><strong>%1$s</strong>: %2$s</p></div>',
                
esc_html__"ERROR"'contact-form-7' ) ),
                
esc_html__"Invalid key values."'contact-form-7' ) ) );
        }

        if ( 
'success' == $message ) {
            echo 
sprintf'<div class="updated notice notice-success is-dismissible"><p>%s</p></div>',
                
esc_html__'Settings saved.''contact-form-7' ) ) );
        }
    }

    public function 
display$action '' ) {
        echo 
'<p>' sprintf(
            
esc_html__'reCAPTCHA protects you against spam and other types of automated abuse. With Contact Form 7&#8217;s reCAPTCHA integration module, you can block abusive form submissions by spam bots. For details, see %s.''contact-form-7' ) ),
            
wpcf7_link(
                
__'https://contactform7.com/recaptcha/''contact-form-7' ),
                
__'reCAPTCHA (v3)''contact-form-7' )
            )
        ) . 
'</p>';

        if ( 
$this->is_active() or 'setup' == $action ) {
            
$this->display_setup();
        } else {
            echo 
sprintf(
                
'<p><a href="%1$s" class="button">%2$s</a></p>',
                
esc_url$this->menu_page_url'action=setup' ) ),
                
esc_html__'Setup Integration''contact-form-7' ) )
            );
        }
    }

    private function 
display_setup() {
        
$sitekey $this->is_active() ? $this->get_sitekey() : '';
        
$secret $this->is_active() ? $this->get_secret$sitekey ) : '';

?>
<form method="post" action="<?php echo esc_url$this->menu_page_url'action=setup' ) ); ?>">
<?php wp_nonce_field'wpcf7-recaptcha-setup' ); ?>
<table class="form-table">
<tbody>
<tr>
    <th scope="row"><label for="sitekey"><?php echo esc_html__'Site Key''contact-form-7' ) ); ?></label></th>
    <td><?php
        
if ( $this->is_active() ) {
            echo 
esc_html$sitekey );
            echo 
sprintf(
                
'<input type="hidden" value="%1$s" id="sitekey" name="sitekey" />',
                
esc_attr$sitekey )
            );
        } else {
            echo 
sprintf(
                
'<input type="text" aria-required="true" value="%1$s" id="sitekey" name="sitekey" class="regular-text code" />',
                
esc_attr$sitekey )
            );
        }
    
?></td>
</tr>
<tr>
    <th scope="row"><label for="secret"><?php echo esc_html__'Secret Key''contact-form-7' ) ); ?></label></th>
    <td><?php
        
if ( $this->is_active() ) {
            echo 
esc_htmlwpcf7_mask_password$secret ) );
            echo 
sprintf(
                
'<input type="hidden" value="%1$s" id="secret" name="secret" />',
                
esc_attr$secret )
            );
        } else {
            echo 
sprintf(
                
'<input type="text" aria-required="true" value="%1$s" id="secret" name="secret" class="regular-text code" />',
                
esc_attr$secret )
            );
        }
    
?></td>
</tr>
</tbody>
</table>
<?php
        
if ( $this->is_active() ) {
            
submit_button(
                
_x'Remove Keys''API keys''contact-form-7' ),
                
'small''reset'
            
);
        } else {
            
submit_button__'Save Changes''contact-form-7' ) );
        }
?>
</form>
<?php
    
}
}