[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Captcha/ -> Captcha.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\Captcha;
  11  
  12  use Joomla\CMS\Factory;
  13  use Joomla\CMS\Filter\InputFilter;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\Plugin\CMSPlugin;
  16  use Joomla\CMS\Plugin\PluginHelper;
  17  use Joomla\Event\DispatcherAwareInterface;
  18  use Joomla\Event\DispatcherAwareTrait;
  19  use Joomla\Event\DispatcherInterface;
  20  use Joomla\Registry\Registry;
  21  
  22  // phpcs:disable PSR1.Files.SideEffects
  23  \defined('JPATH_PLATFORM') or die;
  24  // phpcs:enable PSR1.Files.SideEffects
  25  
  26  /**
  27   * Joomla! Captcha base object
  28   *
  29   * @abstract
  30   * @since  2.5
  31   */
  32  class Captcha implements DispatcherAwareInterface
  33  {
  34      use DispatcherAwareTrait;
  35  
  36      /**
  37       * Captcha Plugin object
  38       *
  39       * @var    CMSPlugin
  40       * @since  2.5
  41       */
  42      private $captcha;
  43  
  44      /**
  45       * Editor Plugin name
  46       *
  47       * @var    string
  48       * @since  2.5
  49       */
  50      private $name;
  51  
  52      /**
  53       * Array of instances of this class.
  54       *
  55       * @var    Captcha[]
  56       * @since  2.5
  57       */
  58      private static $instances = array();
  59  
  60      /**
  61       * Class constructor.
  62       *
  63       * @param   string  $captcha  The plugin to use.
  64       * @param   array   $options  Associative array of options.
  65       *
  66       * @since   2.5
  67       * @throws  \RuntimeException
  68       */
  69      public function __construct($captcha, $options)
  70      {
  71          $this->name = $captcha;
  72  
  73          if (!empty($options['dispatcher']) && $options['dispatcher'] instanceof DispatcherInterface) {
  74              $this->setDispatcher($options['dispatcher']);
  75          } else {
  76              $this->setDispatcher(Factory::getApplication()->getDispatcher());
  77          }
  78  
  79          $this->_load($options);
  80      }
  81  
  82      /**
  83       * Returns the global Captcha object, only creating it
  84       * if it doesn't already exist.
  85       *
  86       * @param   string  $captcha  The plugin to use.
  87       * @param   array   $options  Associative array of options.
  88       *
  89       * @return  Captcha|null  Instance of this class.
  90       *
  91       * @since   2.5
  92       * @throws  \RuntimeException
  93       */
  94      public static function getInstance($captcha, array $options = array())
  95      {
  96          $signature = md5(serialize(array($captcha, $options)));
  97  
  98          if (empty(self::$instances[$signature])) {
  99              self::$instances[$signature] = new Captcha($captcha, $options);
 100          }
 101  
 102          return self::$instances[$signature];
 103      }
 104  
 105      /**
 106       * Fire the onInit event to initialise the captcha plugin.
 107       *
 108       * @param   string  $id  The id of the field.
 109       *
 110       * @return  boolean  True on success
 111       *
 112       * @since   2.5
 113       * @throws  \RuntimeException
 114       */
 115      public function initialise($id)
 116      {
 117          $arg = ['id' => $id];
 118  
 119          $this->update('onInit', $arg);
 120  
 121          return true;
 122      }
 123  
 124      /**
 125       * Get the HTML for the captcha.
 126       *
 127       * @param   string  $name   The control name.
 128       * @param   string  $id     The id for the control.
 129       * @param   string  $class  Value for the HTML class attribute
 130       *
 131       * @return  string  The return value of the function "onDisplay" of the selected Plugin.
 132       *
 133       * @since   2.5
 134       * @throws  \RuntimeException
 135       */
 136      public function display($name, $id, $class = '')
 137      {
 138          // Check if captcha is already loaded.
 139          if ($this->captcha === null) {
 140              return '';
 141          }
 142  
 143          // Initialise the Captcha.
 144          if (!$this->initialise($id)) {
 145              return '';
 146          }
 147  
 148          $arg = [
 149              'name'  => $name,
 150              'id'    => $id ?: $name,
 151              'class' => $class,
 152          ];
 153  
 154          $result = $this->update('onDisplay', $arg);
 155  
 156          return $result;
 157      }
 158  
 159      /**
 160       * Checks if the answer is correct.
 161       *
 162       * @param   string  $code  The answer.
 163       *
 164       * @return  bool    Whether the provided answer was correct
 165       *
 166       * @since   2.5
 167       * @throws  \RuntimeException
 168       */
 169      public function checkAnswer($code)
 170      {
 171          // Check if captcha is already loaded
 172          if ($this->captcha === null) {
 173              return false;
 174          }
 175  
 176          $arg = ['code'  => $code];
 177  
 178          $result = $this->update('onCheckAnswer', $arg);
 179  
 180          return $result;
 181      }
 182  
 183      /**
 184       * Method to react on the setup of a captcha field. Gives the possibility
 185       * to change the field and/or the XML element for the field.
 186       *
 187       * @param   \Joomla\CMS\Form\Field\CaptchaField  $field    Captcha field instance
 188       * @param   \SimpleXMLElement                    $element  XML form definition
 189       *
 190       * @return void
 191       */
 192      public function setupField(\Joomla\CMS\Form\Field\CaptchaField $field, \SimpleXMLElement $element)
 193      {
 194          if ($this->captcha === null) {
 195              return;
 196          }
 197  
 198          $arg = [
 199              'field' => $field,
 200              'element' => $element,
 201          ];
 202  
 203          $result = $this->update('onSetupField', $arg);
 204  
 205          return $result;
 206      }
 207  
 208      /**
 209       * Method to call the captcha callback if it exist.
 210       *
 211       * @param   string  $name   Callback name
 212       * @param   array   &$args  Arguments
 213       *
 214       * @return  mixed
 215       *
 216       * @since   4.0.0
 217       */
 218      private function update($name, &$args)
 219      {
 220          if (method_exists($this->captcha, $name)) {
 221              return call_user_func_array(array($this->captcha, $name), array_values($args));
 222          }
 223  
 224          return null;
 225      }
 226  
 227      /**
 228       * Load the Captcha plugin.
 229       *
 230       * @param   array  $options  Associative array of options.
 231       *
 232       * @return  void
 233       *
 234       * @since   2.5
 235       * @throws  \RuntimeException
 236       */
 237      private function _load(array $options = array())
 238      {
 239          // Build the path to the needed captcha plugin
 240          $name = InputFilter::getInstance()->clean($this->name, 'cmd');
 241          $path = JPATH_PLUGINS . '/captcha/' . $name . '/' . $name . '.php';
 242  
 243          if (!is_file($path)) {
 244              throw new \RuntimeException(Text::sprintf('JLIB_CAPTCHA_ERROR_PLUGIN_NOT_FOUND', $name));
 245          }
 246  
 247          // Require plugin file
 248          require_once $path;
 249  
 250          // Get the plugin
 251          $plugin = PluginHelper::getPlugin('captcha', $this->name);
 252  
 253          if (!$plugin) {
 254              throw new \RuntimeException(Text::sprintf('JLIB_CAPTCHA_ERROR_PLUGIN_NOT_FOUND', $name));
 255          }
 256  
 257          // Check for already loaded params
 258          if (!($plugin->params instanceof Registry)) {
 259              $params = new Registry($plugin->params);
 260              $plugin->params = $params;
 261          }
 262  
 263          // Build captcha plugin classname
 264          $name = 'PlgCaptcha' . $this->name;
 265          $dispatcher     = $this->getDispatcher();
 266          $this->captcha = new $name($dispatcher, (array) $plugin, $options);
 267      }
 268  }


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