Subversion Repositories oidplus

Rev

Rev 709 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
  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.  
  20. if (!defined('INSIDE_OIDPLUS')) die();
  21.  
  22. class OIDplusCaptchaPluginRecaptcha extends OIDplusCaptchaPlugin {
  23.  
  24.         public static function id(): string {
  25.                 return 'ReCAPTCHA';
  26.         }
  27.  
  28.         public function captchaDomHead() {
  29.                 // Here you can add styles and scripts to be included into the HTML <head> part
  30.                 return '<script>
  31.                 function oidplus_captcha_response() {
  32.                         return OIDplusCaptchaPluginRecaptcha.captchaResponse();
  33.                 }
  34.                 function oidplus_captcha_reset() {
  35.                         return OIDplusCaptchaPluginRecaptcha.captchaReset();
  36.                 }
  37.                 </script>
  38.                 <script src="https://www.google.com/recaptcha/api.js"></script>';
  39.         }
  40.  
  41.         public function captchaGenerate($header_text=null, $footer_text=null) {
  42.                 return ($header_text ? '<p>'.$header_text.'</p>' : '') .
  43.                        '<noscript>'.
  44.                        '<p><font color="red">'._L('You need to enable JavaScript to solve the CAPTCHA.').'</font></p>'.
  45.                        '</noscript>'.
  46.                        '<div id="g-recaptcha" class="g-recaptcha" data-sitekey="'.OIDplus::baseConfig()->getValue('RECAPTCHA_PUBLIC', '').'"></div>'.
  47.                        //Don't use jQuery, because we might not have included it (e.g. in oobe.php)
  48.                        //'<script> grecaptcha.render($("#g-recaptcha")[0], { "sitekey" : "'.OIDplus::baseConfig()->getValue('RECAPTCHA_PUBLIC', '').'" }); </script>'.
  49.                        // TODO: oobe.php:formatted:42 Uncaught TypeError: grecaptcha.render is not a function at oobe.php:formatted:42 (but it still works?!)
  50.                        '<script> grecaptcha.render(document.getElementById("g-recaptcha"), { "sitekey" : "'.OIDplus::baseConfig()->getValue('RECAPTCHA_PUBLIC', '').'" }); </script>'.
  51.                        ($footer_text ? '<p>'.$footer_text.'</p>' : '');
  52.         }
  53.  
  54.         public function captchaVerify($params, $fieldname=null) {
  55.                 if (is_null($fieldname)) $fieldname = 'g-recaptcha-response'; // no individual field name (created by oidplus_captcha_response()) means that it is a plain POST event (e.g. by oobe.php)
  56.  
  57.                 $secret=OIDplus::baseConfig()->getValue('RECAPTCHA_PRIVATE', '');
  58.                 _CheckParamExists($params, $fieldname);
  59.                 $response=$params[$fieldname];
  60.                 $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".urlencode($secret)."&response=".urlencode($response));
  61.                 if (!$verify) {
  62.                         throw new OIDplusException(_L('CAPTCHA not successfully verified'));
  63.                 }
  64.                 $captcha_success=json_decode($verify);
  65.                 if (!$captcha_success || ($captcha_success->success==false)) {
  66.                         throw new OIDplusException(_L('CAPTCHA not successfully verified'));
  67.                 }
  68.         }
  69.  
  70.         public static function setupHTML(): string {
  71.                 return '<div id="CAPTCHAPLUGIN_PARAMS_RECAPTCHA">'.
  72.                        '<p>(<a href="https://developers.google.com/recaptcha/intro" target="_blank">'._L('more information and obtain key').'</a>)</p>'.
  73.                        '<p>'._L('reCAPTCHA Public key').'<br><input id="recaptcha_public" type="text" onkeypress="rebuild()" onkeyup="rebuild()"> <span id="recaptcha_public_warn"></span></p>'.
  74.                        '<p>'._L('reCAPTCHA Private key').'<br><input id="recaptcha_private" type="text" onkeypress="rebuild()" onkeyup="rebuild()"> <span id="recaptcha_private_warn"></span></p>'.
  75.                        '</div>';
  76.         }
  77.  
  78. }
  79.