[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Field/ -> CaptchaField.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2011 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\Field;
  11  
  12  use Joomla\CMS\Captcha\Captcha;
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Form\FormField;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('JPATH_PLATFORM') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * Captcha field.
  22   *
  23   * @since  2.5
  24   */
  25  class CaptchaField extends FormField
  26  {
  27      /**
  28       * The field type.
  29       *
  30       * @var    string
  31       * @since  2.5
  32       */
  33      protected $type = 'Captcha';
  34  
  35      /**
  36       * The captcha base instance of our type.
  37       *
  38       * @var Captcha
  39       */
  40      protected $_captcha;
  41  
  42      /**
  43       * Method to get certain otherwise inaccessible properties from the form field object.
  44       *
  45       * @param   string  $name  The property name for which to get the value.
  46       *
  47       * @return  mixed  The property value or null.
  48       *
  49       * @since   3.2
  50       */
  51      public function __get($name)
  52      {
  53          switch ($name) {
  54              case 'plugin':
  55              case 'namespace':
  56                  return $this->$name;
  57          }
  58  
  59          return parent::__get($name);
  60      }
  61  
  62      /**
  63       * Method to set certain otherwise inaccessible properties of the form field object.
  64       *
  65       * @param   string  $name   The property name for which to set the value.
  66       * @param   mixed   $value  The value of the property.
  67       *
  68       * @return  void
  69       *
  70       * @since   3.2
  71       */
  72      public function __set($name, $value)
  73      {
  74          switch ($name) {
  75              case 'plugin':
  76              case 'namespace':
  77                  $this->$name = (string) $value;
  78                  break;
  79  
  80              default:
  81                  parent::__set($name, $value);
  82          }
  83      }
  84  
  85      /**
  86       * Method to attach a Form object to the field.
  87       *
  88       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
  89       * @param   mixed              $value    The form field value to validate.
  90       * @param   string             $group    The field name group control value. This acts as an array container for the field.
  91       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
  92       *                                       full field name would end up being "bar[foo]".
  93       *
  94       * @return  boolean  True on success.
  95       *
  96       * @since   2.5
  97       */
  98      public function setup(\SimpleXMLElement $element, $value, $group = null)
  99      {
 100          $result = parent::setup($element, $value, $group);
 101  
 102          $app = Factory::getApplication();
 103  
 104          $default = $app->get('captcha');
 105  
 106          if ($app->isClient('site')) {
 107              $default = $app->getParams()->get('captcha', $default);
 108          }
 109  
 110          $plugin = $this->element['plugin'] ?
 111              (string) $this->element['plugin'] :
 112              $default;
 113  
 114          $this->plugin = $plugin;
 115  
 116          if ($plugin === 0 || $plugin === '0' || $plugin === '' || $plugin === null) {
 117              $this->hidden = true;
 118  
 119              return false;
 120          } else {
 121              // Force field to be required. There's no reason to have a captcha if it is not required.
 122              // Obs: Don't put required="required" in the xml file, you just need to have validate="captcha"
 123              $this->required = true;
 124  
 125              if (strpos($this->class, 'required') === false) {
 126                  $this->class .= ' required';
 127              }
 128          }
 129  
 130          $this->namespace = $this->element['namespace'] ? (string) $this->element['namespace'] : $this->form->getName();
 131  
 132          try {
 133              // Get an instance of the captcha class that we are using
 134              $this->_captcha = Captcha::getInstance($this->plugin, array('namespace' => $this->namespace));
 135  
 136              /**
 137               * Give the captcha instance a possibility to react on the setup-process,
 138               * e.g. by altering the XML structure of the field, for example hiding the label
 139               * when using invisible captchas.
 140               */
 141              $this->_captcha->setupField($this, $element);
 142          } catch (\RuntimeException $e) {
 143              $this->_captcha = null;
 144              Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
 145  
 146              return false;
 147          }
 148  
 149          return $result;
 150      }
 151  
 152      /**
 153       * Method to get the field input.
 154       *
 155       * @return  string  The field input.
 156       *
 157       * @since   2.5
 158       */
 159      protected function getInput()
 160      {
 161          if ($this->hidden || $this->_captcha == null) {
 162              return '';
 163          }
 164  
 165          try {
 166              return $this->_captcha->display($this->name, $this->id, $this->class);
 167          } catch (\RuntimeException $e) {
 168              Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
 169          }
 170  
 171          return '';
 172      }
 173  }


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