[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/plugins/captcha/recaptcha_invisible/ -> recaptcha_invisible.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Plugin
   5   * @subpackage  Captcha
   6   *
   7   * @copyright   (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9  
  10   * @phpcs:disable Squiz.Classes.ValidClassName.NotCamelCaps
  11  
  12   * @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
  13   */
  14  
  15  use Joomla\CMS\Captcha\Google\HttpBridgePostRequestMethod;
  16  use Joomla\CMS\Factory;
  17  use Joomla\CMS\Language\Text;
  18  use Joomla\CMS\Plugin\CMSPlugin;
  19  use Joomla\Utilities\IpHelper;
  20  
  21  // phpcs:disable PSR1.Files.SideEffects
  22  \defined('_JEXEC') or die;
  23  // phpcs:enable PSR1.Files.SideEffects
  24  
  25  /**
  26   * Invisible reCAPTCHA Plugin.
  27   *
  28   * @since  3.9.0
  29   */
  30  class PlgCaptchaRecaptcha_Invisible extends CMSPlugin
  31  {
  32      /**
  33       * Load the language file on instantiation.
  34       *
  35       * @var    boolean
  36       * @since  3.9.0
  37       */
  38      protected $autoloadLanguage = true;
  39  
  40      /**
  41       * Application object.
  42       *
  43       * @var    \Joomla\CMS\Application\CMSApplication
  44       * @since  4.0.0
  45       */
  46      protected $app;
  47  
  48      /**
  49       * Reports the privacy related capabilities for this plugin to site administrators.
  50       *
  51       * @return  array
  52       *
  53       * @since   3.9.0
  54       */
  55      public function onPrivacyCollectAdminCapabilities()
  56      {
  57          $this->loadLanguage();
  58  
  59          return array(
  60              Text::_('PLG_CAPTCHA_RECAPTCHA_INVISIBLE') => array(
  61                  Text::_('PLG_RECAPTCHA_INVISIBLE_PRIVACY_CAPABILITY_IP_ADDRESS'),
  62              ),
  63          );
  64      }
  65  
  66      /**
  67       * Initialise the captcha
  68       *
  69       * @param   string  $id  The id of the field.
  70       *
  71       * @return  boolean True on success, false otherwise
  72       *
  73       * @since   3.9.0
  74       * @throws  \RuntimeException
  75       */
  76      public function onInit($id = 'dynamic_recaptcha_invisible_1')
  77      {
  78          $pubkey = $this->params->get('public_key', '');
  79  
  80          if ($pubkey === '') {
  81              throw new \RuntimeException(Text::_('PLG_RECAPTCHA_INVISIBLE_ERROR_NO_PUBLIC_KEY'));
  82          }
  83  
  84          $apiSrc = 'https://www.google.com/recaptcha/api.js?onload=JoomlainitReCaptchaInvisible&render=explicit&hl='
  85              . Factory::getLanguage()->getTag();
  86  
  87          // Load assets, the callback should be first
  88          $this->app->getDocument()->getWebAssetManager()
  89              ->registerAndUseScript('plg_captcha_recaptchainvisible', 'plg_captcha_recaptcha_invisible/recaptcha.min.js', [], ['defer' => true])
  90              ->registerAndUseScript('plg_captcha_recaptchainvisible.api', $apiSrc, [], ['defer' => true], ['plg_captcha_recaptchainvisible'])
  91              ->registerAndUseStyle('plg_captcha_recaptchainvisible', 'plg_captcha_recaptcha_invisible/recaptcha_invisible.css');
  92  
  93          return true;
  94      }
  95  
  96      /**
  97       * Gets the challenge HTML
  98       *
  99       * @param   string  $name   The name of the field. Not Used.
 100       * @param   string  $id     The id of the field.
 101       * @param   string  $class  The class of the field.
 102       *
 103       * @return  string  The HTML to be embedded in the form.
 104       *
 105       * @since  3.9.0
 106       */
 107      public function onDisplay($name = null, $id = 'dynamic_recaptcha_invisible_1', $class = '')
 108      {
 109          $dom = new \DOMDocument('1.0', 'UTF-8');
 110          $ele = $dom->createElement('div');
 111          $ele->setAttribute('id', $id);
 112          $ele->setAttribute('class', ((trim($class) == '') ? 'g-recaptcha' : ($class . ' g-recaptcha')));
 113          $ele->setAttribute('data-sitekey', $this->params->get('public_key', ''));
 114          $ele->setAttribute('data-badge', $this->params->get('badge', 'bottomright'));
 115          $ele->setAttribute('data-size', 'invisible');
 116          $ele->setAttribute('data-tabindex', $this->params->get('tabindex', '0'));
 117          $ele->setAttribute('data-callback', $this->params->get('callback', ''));
 118          $ele->setAttribute('data-expired-callback', $this->params->get('expired_callback', ''));
 119          $ele->setAttribute('data-error-callback', $this->params->get('error_callback', ''));
 120          $dom->appendChild($ele);
 121  
 122          return $dom->saveHTML($ele);
 123      }
 124  
 125      /**
 126       * Calls an HTTP POST function to verify if the user's guess was correct
 127       *
 128       * @param   string  $code  Answer provided by user. Not needed for the Recaptcha implementation
 129       *
 130       * @return  boolean  True if the answer is correct, false otherwise
 131       *
 132       * @since   3.9.0
 133       * @throws  \RuntimeException
 134       */
 135      public function onCheckAnswer($code = null)
 136      {
 137          $input      = Factory::getApplication()->input;
 138          $privatekey = $this->params->get('private_key');
 139          $remoteip   = IpHelper::getIp();
 140  
 141          $response  = $input->get('g-recaptcha-response', '', 'string');
 142  
 143          // Check for Private Key
 144          if (empty($privatekey)) {
 145              throw new \RuntimeException(Text::_('PLG_RECAPTCHA_INVISIBLE_ERROR_NO_PRIVATE_KEY'));
 146          }
 147  
 148          // Check for IP
 149          if (empty($remoteip)) {
 150              throw new \RuntimeException(Text::_('PLG_RECAPTCHA_INVISIBLE_ERROR_NO_IP'));
 151          }
 152  
 153          // Discard spam submissions
 154          if (trim($response) == '') {
 155              throw new \RuntimeException(Text::_('PLG_RECAPTCHA_INVISIBLE_ERROR_EMPTY_SOLUTION'));
 156          }
 157  
 158          return $this->getResponse($privatekey, $remoteip, $response);
 159      }
 160  
 161      /**
 162       * Method to react on the setup of a captcha field. Gives the possibility
 163       * to change the field and/or the XML element for the field.
 164       *
 165       * @param   \Joomla\CMS\Form\Field\CaptchaField  $field    Captcha field instance
 166       * @param   \SimpleXMLElement                    $element  XML form definition
 167       *
 168       * @return void
 169       *
 170       * @since 3.9.0
 171       */
 172      public function onSetupField(\Joomla\CMS\Form\Field\CaptchaField $field, \SimpleXMLElement $element)
 173      {
 174          // Hide the label for the invisible recaptcha type
 175          $element['hiddenLabel'] = 'true';
 176      }
 177  
 178      /**
 179       * Get the reCaptcha response.
 180       *
 181       * @param   string  $privatekey  The private key for authentication.
 182       * @param   string  $remoteip    The remote IP of the visitor.
 183       * @param   string  $response    The response received from Google.
 184       *
 185       * @return  boolean  True if response is good | False if response is bad.
 186       *
 187       * @since   3.9.0
 188       * @throws  \RuntimeException
 189       */
 190      private function getResponse($privatekey, $remoteip, $response)
 191      {
 192          $reCaptcha = new \ReCaptcha\ReCaptcha($privatekey, new HttpBridgePostRequestMethod());
 193          $response = $reCaptcha->verify($response, $remoteip);
 194  
 195          if (!$response->isSuccess()) {
 196              foreach ($response->getErrorCodes() as $error) {
 197                  throw new \RuntimeException($error);
 198              }
 199  
 200              return false;
 201          }
 202  
 203          return true;
 204      }
 205  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer