|
9 | 9 | class GoogleRecaptcha
|
10 | 10 | {
|
11 | 11 |
|
12 |
| - public static function show($site_key,$after_field_id='Form_ContactForm_Comment', $debug='no_debug', $extra_class="mt-4 mb-4", $please_tick_msg="Please tick the I'm not robot checkbox") { |
13 |
| - $debug_alert = ($debug=='no_debug') ? 'false' : 'true'; |
14 |
| - $str = <<<EOF |
15 |
| - <!-- Start of the Google Recaptcha code --> |
16 |
| - <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> |
17 |
| -
|
18 |
| - <script src='https://www.google.com/recaptcha/api.js'></script> |
19 |
| - <script> |
20 |
| - |
21 |
| - // Display google recaptcha |
22 |
| - $('#$after_field_id').parent().after('<div class="g-recaptcha $extra_class" data-sitekey="$site_key"></div> '); |
23 |
| - |
24 |
| - // Validate google recaptcha before submit the form |
25 |
| - $('#$after_field_id').closest('form').submit(function(e) { |
26 |
| - var response = grecaptcha.getResponse(); |
27 |
| - //recaptcha failed validation |
28 |
| - if(response.length == 0) { |
29 |
| - alert("$please_tick_msg"); |
30 |
| - return $debug_alert; |
31 |
| - } |
32 |
| - return true; |
33 |
| - }); |
34 |
| - |
35 |
| - </script> |
36 |
| - <!-- End of the Google Recaptcha code --> |
37 |
| -EOF; |
38 |
| - return $str; |
39 |
| - } |
40 |
| - |
41 | 12 | /**
|
42 | 13 | * verify function
|
43 | 14 | *
|
44 | 15 | * @param [string] $secret_key
|
45 | 16 | * @param [string] $break_msg, if set, pop up as an javascript alert and exit
|
46 | 17 | * @return true or false
|
47 | 18 | */
|
48 |
| - public static function verify($secret_key, $break_msg = null) { |
| 19 | + public static function verify($secret_key, $break_msg = null) |
| 20 | + { |
49 | 21 | $valid = false;
|
50 |
| - if ( isset($_POST['g-recaptcha-response']) && strlen($_POST['g-recaptcha-response'])>20 ) { |
| 22 | + if (isset($_POST['g-recaptcha-response']) && strlen($_POST['g-recaptcha-response']) > 20) { |
51 | 23 | $valid = Self::result($secret_key, $_POST['g-recaptcha-response']);
|
52 | 24 | }
|
53 |
| - |
54 |
| - if ( !$valid && $break_msg){ |
55 |
| - if ( strlen($break_msg) < 10 ){ |
| 25 | + |
| 26 | + if (!$valid && $break_msg) { |
| 27 | + if (strlen($break_msg) < 10) { |
56 | 28 | $break_msg = "Google Recaptcha Validation Failed!!";
|
57 | 29 | }
|
58 | 30 | echo '<script>alert("' . $break_msg . '");history.back();</script>';
|
59 | 31 | exit();
|
60 |
| - } |
61 |
| - return $valid; |
| 32 | + } |
| 33 | + return $valid; |
62 | 34 | }
|
63 | 35 |
|
64 | 36 | /**
|
@@ -103,6 +75,101 @@ public static function result($secret_key, $g_recaptcha_response)
|
103 | 75 | } else {
|
104 | 76 | return true;
|
105 | 77 | }
|
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * show recaptcha function without jQuery |
| 82 | + * |
| 83 | + * @param string $site_key |
| 84 | + * @param string $after_field_id |
| 85 | + * @param string $debug |
| 86 | + * @param string $extra_class |
| 87 | + * @param string $please_tick_msg |
| 88 | + * @return void |
| 89 | + */ |
| 90 | + public static function show($site_key, $after_field_id = 'Form_ContactForm_Comment', $debug = 'no_debug', $extra_class = "mt-4 mb-4", $please_tick_msg = "Please tick the I'm not robot checkbox") |
| 91 | + { |
| 92 | + $debug_alert = ($debug == 'no_debug') ? 'false' : 'true'; |
| 93 | + $str = <<<EOF |
| 94 | + <!-- Start of the Google Recaptcha code --> |
| 95 | + |
| 96 | + <script src='https://www.google.com/recaptcha/api.js'></script> |
| 97 | + <script> |
| 98 | + |
| 99 | + // Display google recaptcha |
| 100 | + var alexEL = document.getElementById('$after_field_id'); |
| 101 | + alexEL.parentNode.insertAdjacentHTML('afterend', '<div class="g-recaptcha $extra_class" data-sitekey="$site_key"></div>'); |
| 102 | +
|
| 103 | + function alexFindClosestNode(el, selector) { |
| 104 | + const matchesSelector = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector; |
106 | 105 |
|
107 |
| - } |
| 106 | + while (el) { |
| 107 | + if (matchesSelector.call(el, selector)) { |
| 108 | + return el; |
| 109 | + } else { |
| 110 | + el = el.parentElement; |
| 111 | + } |
| 112 | + } |
| 113 | + return null; |
| 114 | + } |
| 115 | +
|
| 116 | + // Validate google recaptcha before submit the form |
| 117 | +
|
| 118 | + alexFindClosestNode(alexEL,'form').addEventListener('submit',function(e){ |
| 119 | + var response = grecaptcha.getResponse(); |
| 120 | + //recaptcha failed validation |
| 121 | + if(response.length == 0) { |
| 122 | + alert("$please_tick_msg"); |
| 123 | + e.preventDefault(); |
| 124 | + return $debug_alert; |
| 125 | + } |
| 126 | + return true; |
| 127 | + }); |
| 128 | + |
| 129 | + </script> |
| 130 | + <!-- End of the Google Recaptcha code --> |
| 131 | +EOF; |
| 132 | + return $str; |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + /** |
| 137 | + * jqueryShow function |
| 138 | + * |
| 139 | + * @param string $site_key |
| 140 | + * @param string $after_field_id |
| 141 | + * @param string $debug |
| 142 | + * @param string $extra_class |
| 143 | + * @param string $please_tick_msg |
| 144 | + * @return void |
| 145 | + */ |
| 146 | + public static function jqueryShow($site_key, $after_field_id = 'Form_ContactForm_Comment', $debug = 'no_debug', $extra_class = "mt-4 mb-4", $please_tick_msg = "Please tick the I'm not robot checkbox") |
| 147 | + { |
| 148 | + $debug_alert = ($debug == 'no_debug') ? 'false' : 'true'; |
| 149 | + $str = <<<EOF |
| 150 | + <!-- Start of the Google Recaptcha code --> |
| 151 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> |
| 152 | +
|
| 153 | + <script src='https://www.google.com/recaptcha/api.js'></script> |
| 154 | + <script> |
| 155 | + |
| 156 | + // Display google recaptcha |
| 157 | + $('#$after_field_id').parent().after('<div class="g-recaptcha $extra_class" data-sitekey="$site_key"></div> '); |
| 158 | + |
| 159 | + // Validate google recaptcha before submit the form |
| 160 | + $('#$after_field_id').closest('form').submit(function(e) { |
| 161 | + var response = grecaptcha.getResponse(); |
| 162 | + //recaptcha failed validation |
| 163 | + if(response.length == 0) { |
| 164 | + alert("$please_tick_msg"); |
| 165 | + return $debug_alert; |
| 166 | + } |
| 167 | + return true; |
| 168 | + }); |
| 169 | + |
| 170 | + </script> |
| 171 | + <!-- End of the Google Recaptcha code --> |
| 172 | +EOF; |
| 173 | + return $str; |
| 174 | + } |
108 | 175 | }
|
0 commit comments