- md5
- 002e4015235b92bca2e19d9e11a1d9ff
- sha1
- da44237fc4798a5f97e513ac57e81645f4434a08
- sha256
- 09cfa82b85e8724561f80b1467de256002cd46919423f6fb65fbf2ea123e3b78
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Captcha;
defined('JPATH_PLATFORM') or die;
use Joomla\Event\DispatcherAwareInterface;
use Joomla\Event\DispatcherAwareTrait;
use Joomla\Event\Event;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Registry\Registry;
/**
* Joomla! Captcha base object
*
* @abstract
* @package Joomla.Libraries
* @subpackage Captcha
* @since 2.5
*/
class Captcha implements DispatcherAwareInterface
{
use DispatcherAwareTrait;
/**
* Captcha Plugin object
*
* @var CMSPlugin
* @since 2.5
*/
private $_captcha;
/**
* Editor Plugin name
*
* @var string
* @since 2.5
*/
private $_name;
/**
* Array of instances of this class.
*
* @var Captcha[]
* @since 2.5
*/
private static $_instances = array();
/**
* Class constructor.
*
* @param string $captcha The editor to use.
* @param array $options Associative array of options.
*
* @since 2.5
*/
public function __construct($captcha, $options)
{
$this->_name = $captcha;
$this->setDispatcher(\JFactory::getApplication()->getDispatcher());
$this->_load($options);
}
/**
* Returns the global Captcha object, only creating it
* if it doesn't already exist.
*
* @param string $captcha The plugin to use.
* @param array $options Associative array of options.
*
* @return Captcha|null Instance of this class.
*
* @since 2.5
*/
public static function getInstance($captcha, array $options = array())
{
$signature = md5(serialize(array($captcha, $options)));
if (empty(self::$_instances[$signature]))
{
try
{
self::$_instances[$signature] = new static($captcha, $options);
}
catch (\RuntimeException $e)
{
\JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
return;
}
}
return self::$_instances[$signature];
}
/**
* Fire the onInit event to initialise the captcha plugin.
*
* @param string $id The id of the field.
*
* @return boolean True on success
*
* @since 2.5
*/
public function initialise($id)
{
$event = new Event(
'onInit',
['id' => $id]
);
try
{
$this->getDispatcher()->dispatch('onInit', $event);
}
catch (\Exception $e)
{
\JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
return false;
}
return true;
}
/**
* Get the HTML for the captcha.
*
* @param string $name The control name.
* @param string $id The id for the control.
* @param string $class Value for the HTML class attribute
*
* @return string The return value of the function "onDisplay" of the selected Plugin.
*
* @since 2.5
*/
public function display($name, $id, $class = '')
{
// Check if captcha is already loaded.
if ($this->_captcha === null)
{
return '';
}
// Initialise the Captcha.
if (!$this->initialise($id))
{
return '';
}
$event = new Event(
'onDisplay',
[
'name' => $name,
'id' => $id ?: $name,
'class' => $class ? 'class="' . $class . '"' : '',
]
);
$result = $this->getDispatcher()->dispatch('onInit', $event);
// TODO REFACTOR ME! This is Ye Olde Way of returning plugin results192
return $result['result'][0];
}
/**
* Checks if the answer is correct.
*
* @param string $code The answer.
*
* @return bool The return value of the function "onCheckAnswer" of the selected Plugin.
*
* @since 2.5
*/
public function checkAnswer($code)
{
// Check if captcha is already loaded
if ($this->_captcha === null)
{
return false;
}
$event = new Event(
'onCheckAnswer',
['code' => $code]
);
$result = $this->getDispatcher()->dispatch('onCheckAnswer', $event);
// TODO REFACTOR ME! This is Ye Olde Way of returning plugin results
return $result['result'][0];
}
/**
* Load the Captcha plugin.
*
* @param array $options Associative array of options.
*
* @return void
*
* @since 2.5
* @throws \RuntimeException
*/
private function _load(array $options = array())
{
// Build the path to the needed captcha plugin
$name = \JFilterInput::getInstance()->clean($this->_name, 'cmd');
$path = JPATH_PLUGINS . '/captcha/' . $name . '/' . $name . '.php';
if (!is_file($path))
{
throw new \RuntimeException(\JText::sprintf('JLIB_CAPTCHA_ERROR_PLUGIN_NOT_FOUND', $name));
}
// Require plugin file
require_once $path;
// Get the plugin
$plugin = PluginHelper::getPlugin('captcha', $this->_name);
if (!$plugin)
{
throw new \RuntimeException(\JText::sprintf('JLIB_CAPTCHA_ERROR_PLUGIN_NOT_FOUND', $name));
}
// Check for already loaded params
if (!($plugin->params instanceof Registry))
{
$params = new Registry($plugin->params);
$plugin->params = $params;
}
// Build captcha plugin classname
$name = 'PlgCaptcha' . $this->_name;
$dispatcher = $this->getDispatcher();
$this->_captcha = new $name($dispatcher, (array) $plugin, $options);
}
}
No other release has this exact content. See every version of this file.