Rev 1050 | Rev 1116 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
702 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
1086 | daniel-mar | 5 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
702 | daniel-mar | 6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
8 | * you may not use this file except in compliance with the License. |
||
9 | * You may obtain a copy of the License at |
||
10 | * |
||
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | * |
||
13 | * Unless required by applicable law or agreed to in writing, software |
||
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | * See the License for the specific language governing permissions and |
||
17 | * limitations under the License. |
||
18 | */ |
||
19 | |||
1050 | daniel-mar | 20 | namespace ViaThinkSoft\OIDplus; |
702 | daniel-mar | 21 | |
1086 | daniel-mar | 22 | // phpcs:disable PSR1.Files.SideEffects |
23 | \defined('INSIDE_OIDPLUS') or die; |
||
24 | // phpcs:enable PSR1.Files.SideEffects |
||
25 | |||
702 | daniel-mar | 26 | class OIDplusCaptchaPluginRecaptcha extends OIDplusCaptchaPlugin { |
27 | |||
1016 | daniel-mar | 28 | /*public*/ const RECAPTCHA_V2_CHECKBOX = 1; |
29 | /*public*/ const RECAPTCHA_V2_INVISIBLE = 2; |
||
30 | /*public*/ const RECAPTCHA_V3 = 3; |
||
31 | |||
702 | daniel-mar | 32 | public static function id(): string { |
1016 | daniel-mar | 33 | return 'reCAPTCHA'; // TODO: Now it is called "reCAPTCHA" |
702 | daniel-mar | 34 | } |
35 | |||
1016 | daniel-mar | 36 | public function isVisible(): bool { |
37 | return OIDplus::baseConfig()->getValue('RECAPTCHA_VERSION', self::RECAPTCHA_V2_CHECKBOX) == self::RECAPTCHA_V2_CHECKBOX; |
||
709 | daniel-mar | 38 | } |
39 | |||
702 | daniel-mar | 40 | public function captchaGenerate($header_text=null, $footer_text=null) { |
1016 | daniel-mar | 41 | return '<noscript>'. |
702 | daniel-mar | 42 | '<p><font color="red">'._L('You need to enable JavaScript to solve the CAPTCHA.').'</font></p>'. |
43 | '</noscript>'. |
||
1016 | daniel-mar | 44 | (!$this->isVisible() || !$header_text ? '' : '<p>'.$header_text.'</p>'). |
45 | '<div id="recaptcha"></div>'. |
||
46 | '<input type="hidden" id="oidplus-recaptcha-response" name="oidplus-recaptcha-response">'. |
||
47 | '<script>'. |
||
48 | // ' $("form").submit(function(e){'. |
||
49 | // // TODO: The form must not be submitted before recaptchaFinished() is called! |
||
50 | // ' event.preventDefault();'. |
||
51 | // ' });'. |
||
52 | ' var recaptchaLoaded = function() {'. |
||
53 | ' console.log("reCAPTCHA ready");'. |
||
54 | ' grecaptcha.render("recaptcha", {'. |
||
55 | ' "sitekey": "'.OIDplus::baseConfig()->getValue('RECAPTCHA_PUBLIC', '').'",'. |
||
56 | ($this->isVisible() ? '' : |
||
57 | ' "size": "invisible",'). |
||
58 | ' "callback": function (token) {'. // TODO: also 'expired-callback' and 'error-callback' |
||
59 | ' console.log("reCAPTCHA solved");'. |
||
60 | ' document.getElementById("oidplus-recaptcha-response").value = token;'. |
||
61 | ' }'. |
||
62 | ' });'. |
||
63 | ($this->isVisible() ? '' : |
||
64 | ' grecaptcha.execute();'). |
||
65 | ' };'. |
||
66 | ' var oidplus_captcha_response = function() {'. |
||
67 | ' return document.getElementById("oidplus-recaptcha-response").value;'. |
||
68 | ' };'. |
||
69 | ' var oidplus_captcha_reset = function() {'. |
||
70 | ' grecaptcha.reset();'. |
||
71 | ($this->isVisible() ? '' : |
||
72 | ' grecaptcha.execute();'). |
||
73 | ' };'. |
||
74 | '</script>'. |
||
75 | '<script src="https://www.google.com/recaptcha/api.js?onload=recaptchaLoaded&render=explicit" async defer></script>'. |
||
76 | (!$this->isVisible() || !$footer_text ? '' : '<p>'.$footer_text.'</p>'); |
||
702 | daniel-mar | 77 | } |
78 | |||
79 | public function captchaVerify($params, $fieldname=null) { |
||
1001 | daniel-mar | 80 | $secret=OIDplus::baseConfig()->getValue('RECAPTCHA_PRIVATE', ''); |
81 | |||
1016 | daniel-mar | 82 | if (is_null($fieldname)) $fieldname = 'oidplus-recaptcha-response'; // no individual AJAX field name (created by oidplus_captcha_response()) means that it is a plain POST event (e.g. by oobe.php) |
702 | daniel-mar | 83 | _CheckParamExists($params, $fieldname); |
84 | $response=$params[$fieldname]; |
||
1016 | daniel-mar | 85 | |
86 | $verify=url_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.urlencode($secret).'&response='.urlencode($response).'&remoteip='.urlencode($_SERVER['REMOTE_ADDR'])); |
||
702 | daniel-mar | 87 | if (!$verify) { |
1016 | daniel-mar | 88 | throw new OIDplusException(_L('CAPTCHA not successfully verified').' (Web request failed)'); |
702 | daniel-mar | 89 | } |
1001 | daniel-mar | 90 | $captcha_success=@json_decode($verify); |
1016 | daniel-mar | 91 | $SCORE_THRESHOLD = 0.5; // TODO: Make Score configurable (only V3) |
92 | if (!$captcha_success) { |
||
1022 | daniel-mar | 93 | throw new OIDplusException(_L('CAPTCHA not successfully verified').' ('._L('JSON Decode failed').')'); |
702 | daniel-mar | 94 | } |
1016 | daniel-mar | 95 | if ($captcha_success->success==false) { |
1022 | daniel-mar | 96 | throw new OIDplusException(_L('CAPTCHA not successfully verified').' ('._L('Failed').')'); |
1016 | daniel-mar | 97 | } |
98 | if (isset($captcha_success->score) && ($captcha_success->score < $SCORE_THRESHOLD)) { |
||
1022 | daniel-mar | 99 | throw new OIDplusException(_L('CAPTCHA not successfully verified').' ('._L('Score %1 too low', $captcha_success->score).')'); |
1016 | daniel-mar | 100 | } |
702 | daniel-mar | 101 | } |
102 | |||
103 | public static function setupHTML(): string { |
||
104 | return '<div id="CAPTCHAPLUGIN_PARAMS_RECAPTCHA">'. |
||
105 | '<p>(<a href="https://developers.google.com/recaptcha/intro" target="_blank">'._L('more information and obtain key').'</a>)</p>'. |
||
1016 | daniel-mar | 106 | '<p>'._L('reCAPTCHA Version').'<br><select id="recaptcha_version">'. |
1050 | daniel-mar | 107 | // Note: JavaScript will add "\ViaThinkSoft\OIDplus\OIDplusCaptchaPluginRecaptcha::" in front of the name |
108 | ' <option name="RECAPTCHA_V2_CHECKBOX">reCAPTCHA V2 Checkbox</option>'. |
||
109 | ' <option name="RECAPTCHA_V2_INVISIBLE">reCAPTCHA V2 Invisible</option>'. |
||
110 | ' <option name="RECAPTCHA_V3">reCAPTCHA V3</option>'. |
||
1016 | daniel-mar | 111 | '</select></p>'. |
702 | daniel-mar | 112 | '<p>'._L('reCAPTCHA Public key').'<br><input id="recaptcha_public" type="text" onkeypress="rebuild()" onkeyup="rebuild()"> <span id="recaptcha_public_warn"></span></p>'. |
113 | '<p>'._L('reCAPTCHA Private key').'<br><input id="recaptcha_private" type="text" onkeypress="rebuild()" onkeyup="rebuild()"> <span id="recaptcha_private_warn"></span></p>'. |
||
114 | '</div>'; |
||
115 | } |
||
116 | |||
1001 | daniel-mar | 117 | function httpHeaderCheck(&$http_headers) { |
118 | $http_headers["Content-Security-Policy"]["script-src"][] = "https://www.google.com/"; |
||
119 | $http_headers["Content-Security-Policy"]["script-src"][] = "https://www.gstatic.com/"; |
||
1015 | daniel-mar | 120 | $http_headers["Content-Security-Policy"]["img-src"][] = "https://www.google.com/"; |
121 | $http_headers["Content-Security-Policy"]["img-src"][] = "https://www.gstatic.com/"; |
||
122 | $http_headers["Content-Security-Policy"]["frame-src"][] = "https://www.google.com/"; |
||
123 | $http_headers["Content-Security-Policy"]["frame-src"][] = "https://www.gstatic.com/"; |
||
1001 | daniel-mar | 124 | } |
125 | |||
702 | daniel-mar | 126 | } |