[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Rule/ -> CaptchaRule.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Form\Rule;
  11  
  12  use Joomla\CMS\Captcha\Captcha;
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Form\Form;
  15  use Joomla\CMS\Form\FormRule;
  16  use Joomla\Registry\Registry;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('JPATH_PLATFORM') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * Form Rule class for the Joomla Framework.
  24   *
  25   * @since  2.5
  26   */
  27  class CaptchaRule extends FormRule
  28  {
  29      /**
  30       * Method to test if the Captcha is correct.
  31       *
  32       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
  33       * @param   mixed              $value    The form field value to validate.
  34       * @param   string             $group    The field name group control value. This acts as an array container for the field.
  35       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
  36       *                                       full field name would end up being "bar[foo]".
  37       * @param   Registry           $input    An optional Registry object with the entire data set to validate against the entire form.
  38       * @param   Form               $form     The form object for which the field is being tested.
  39       *
  40       * @return  boolean  True if the value is valid, false otherwise.
  41       *
  42       * @since   2.5
  43       */
  44      public function test(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
  45      {
  46          $app    = Factory::getApplication();
  47          $plugin = $app->get('captcha');
  48  
  49          if ($app->isClient('site')) {
  50              $plugin = $app->getParams()->get('captcha', $plugin);
  51          }
  52  
  53          $namespace = $element['namespace'] ?: $form->getName();
  54  
  55          // Use 0 for none
  56          if ($plugin === 0 || $plugin === '0') {
  57              return true;
  58          }
  59  
  60          try {
  61              $captcha = Captcha::getInstance((string) $plugin, array('namespace' => (string) $namespace));
  62  
  63              return $captcha->checkAnswer($value);
  64          } catch (\RuntimeException $e) {
  65              $app->enqueueMessage($e->getMessage(), 'error');
  66          }
  67  
  68          return false;
  69      }
  70  }


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