GITLAB

Snippets

Sign in
  • Sign in

Google ReCaptcha for Prestashop
Add new snippet


#74 by 72f60816e60779e1cff35b00d383cb94?s=40&d=identicon doekia
← discover snippets
header.tpl Buy Me a Coffee at ko-fi.com
raw
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
<!-- Copyright (c)2017-2018 (d)oekia Enter-Solutions GPL -->
                <!-- Google ReCaptcha on contact form -->
                <script>
                        (function(){
                        var googlecaptchasitekey = 'XXXXXXX-the-public-site-key-XXXXXXXXX'; /* TODO: tune this https://www.google.com/recaptcha/admin */
                        var trigger = function(){
                                setTimeout(function(){
                                    $('div.g-recaptcha').remove();
                                    var $forms = $('form.contact-form-box,form#sendOrderMessage');
                                    if ($forms.length > 0){
                                            var captcha = $('<div class="g-recaptcha" data-sitekey="'+ googlecaptchasitekey + '">');
                                            var $submit = $forms.find('#submitMessage,.button[name=submitMessage]');
                                            $submit.before(captcha);
                                            $submit.click(function(event){
                                                    $gresponse = $forms.find('[name=g-recaptcha-response]');
                                                    if ($gresponse.length == 0 || $gresponse.val().length == 0) {
                                                            event.preventDefault();
                                                            event.stopPropagation();
                                                            return false;
                                                    }
                                            });
                                            try {
                                                    window.grecaptcha.render(captcha[0]);
                                            } catch(e){};
                                    }
                                },1000);
                        };
                        $(document).ready(trigger);
                        $(document).bind('ajaxComplete', trigger);
                        })();
                </script>
                {if !isset($language_code)}{assign var="language_code" value=$lang_iso}{/if}
                <script src='https://www.google.com/recaptcha/api.js?hl={$language_code}'></script>
                <script>
                    if ( window.history.replaceState ) {
                        window.history.replaceState( null, null, window.location.href );
                    }
                </script>

                {*
                      TODO: cut/paste into index.php (first after comments)
                      if (isset($_REQUEST['submitMessage'])){
                                if (empty($_REQUEST['g-recaptcha-response'])){
                                        sleep(25);
                                        die('not human');
                                }
                                $opts = array('http' =>
                                                array(
                                                        'method'  => 'POST',
                                                        'header'  => 'Content-type: application/x-www-form-urlencoded',
                                                        'content' => http_build_query( $post =
                                                                array(
                                                                        'secret' => 'XXXXXXX-the-secret-key-XXXXXXXXXX', /* TODO: tune this https://www.google.com/recaptcha/admin */
                                                                        'response' => $_REQUEST['g-recaptcha-response'],
                                                                )
                                                        ),
                                                ),
                                );
                                if (in_array(ini_get('allow_url_fopen'), array('On', 'on', '1'))) {
	                                $stream = stream_context_create($opts);
                                	$captcha = @json_decode(file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $stream), true);
                                } elseif (function_exists('curl_init')) {
					$curl = curl_init();
					curl_setopt_array($curl, array(
						CURLOPT_RETURNTRANSFER => 1,
						CURLOPT_URL => 'https://www.google.com/recaptcha/api/siteverify',
						CURLOPT_POST => 1,
						CURLOPT_POSTFIELDS => $post,
						)
					);
					$captcha = @json_decode(curl_exec($curl),true);
					curl_close($curl);
				} else {
					// buggy hoster !
					$captcha = array('error' => 'buggy hoster');
				}
                                if (empty($captcha) || empty($captcha['success']) || !$captcha['success']){
                                        if(!empty($_SERVER['HTTP_REFERER']) && ($h = parse_url($_SERVER['HTTP_REFERER'])) && $h['host'] == $_SERVER['HTTP_HOST']) {
                                            sleep(5);
                                            die('<p>Captcha Invalide</p><a href="'.$_SERVER['HTTP_REFERER'].'"><button>Retour au formulaire</button></a>');
                                        }

                                        sleep(25);
                                        die('not human'.PHP_EOL/*.print_r($captcha,1)*/);
                                }
                        }
                *}
                <!-- /Google ReCaptcha -->