Skip to content
Closed
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
INSERT INTO `#__extensions` (`package_id`, `name`, `type`, `element`, `folder`, `client_id`, `enabled`, `access`, `protected`, `locked`, `manifest_cache`, `params`, `custom_data`, `ordering`, `state`) VALUES
(0, 'plg_captcha_math', 'plugin', 'math', 'captcha', 0, 0, 1, 0, 1, '', '{}', '', 1, 0);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
INSERT INTO "#__extensions" ("package_id", "name", "type", "element", "folder", "client_id", "enabled", "access", "protected", "locked", "manifest_cache", "params", "custom_data", "ordering", "state") VALUES
(0, 'plg_captcha_math', 'plugin', 'math', 'captcha', 0, 0, 1, 0, 1, '', '{}', '', 1, 0);
11 changes: 11 additions & 0 deletions administrator/language/en-GB/plg_captcha_math.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
; Joomla! Project
; (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
; License GNU General Public License version 2 or later; see LICENSE.txt
; Note : All ini files need to be saved as UTF-8

PLG_CAPTCHA_MATH="Captcha - Math"
PLG_CAPTCHA_MATH_XML_DESCRIPTION="A simple Captcha plugin that uses Math."

PLG_CAPTCHA_MATH_EMPTY_STORE="Captcha Math solution not found, please make sure you did not submit the same form twice."
PLG_CAPTCHA_MATH_ENTER_SOLUTION="Enter solution (all or remaining digits) for"
PLG_CAPTCHA_MATH_WRONG_SOLUTION="Incorrect math solution for the Captcha."
7 changes: 7 additions & 0 deletions administrator/language/en-GB/plg_captcha_math.sys.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
; Joomla! Project
; (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
; License GNU General Public License version 2 or later; see LICENSE.txt
; Note : All ini files need to be saved as UTF-8

PLG_CAPTCHA_MATH="Captcha - Math"
PLG_CAPTCHA_MATH_XML_DESCRIPTION="A simple Captcha plugin that uses Math."
1 change: 1 addition & 0 deletions installation/sql/mysql/base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ INSERT INTO `#__extensions` (`package_id`, `name`, `type`, `element`, `folder`,
(0, 'plg_behaviour_compat', 'plugin', 'compat', 'behaviour', 0, 1, 1, 0, 1, '', '{"classes_aliases":"1","es5_assets":"1"}', '', 1, 0),
(0, 'plg_behaviour_taggable', 'plugin', 'taggable', 'behaviour', 0, 1, 1, 0, 1, '', '{}', '', 2, 0),
(0, 'plg_behaviour_versionable', 'plugin', 'versionable', 'behaviour', 0, 1, 1, 0, 1, '', '{}', '', 3, 0),
(0, 'plg_captcha_math', 'plugin', 'math', 'captcha', 0, 0, 1, 0, 1, '', '{}', '', 1, 0),
(0, 'plg_content_confirmconsent', 'plugin', 'confirmconsent', 'content', 0, 0, 1, 0, 1, '', '{}', '', 1, 0),
(0, 'plg_content_contact', 'plugin', 'contact', 'content', 0, 1, 1, 0, 1, '', '', '', 2, 0),
(0, 'plg_content_emailcloak', 'plugin', 'emailcloak', 'content', 0, 1, 1, 0, 1, '', '{"mode":"1"}', '', 3, 0),
Expand Down
1 change: 1 addition & 0 deletions installation/sql/postgresql/base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ INSERT INTO "#__extensions" ("package_id", "name", "type", "element", "folder",
(0, 'plg_behaviour_compat', 'plugin', 'compat', 'behaviour', 0, 1, 1, 0, 1, '', '{"classes_aliases":"1","es5_assets":"1"}', '', 1, 0),
(0, 'plg_behaviour_taggable', 'plugin', 'taggable', 'behaviour', 0, 1, 1, 0, 1, '', '{}', '', 2, 0),
(0, 'plg_behaviour_versionable', 'plugin', 'versionable', 'behaviour', 0, 1, 1, 0, 1, '', '{}', '', 3, 0),
(0, 'plg_captcha_math', 'plugin', 'math', 'captcha', 0, 0, 1, 0, 1, '', '{}', '', 1, 0),
(0, 'plg_content_confirmconsent', 'plugin', 'confirmconsent', 'content', 0, 0, 1, 0, 1, '', '{}', '', 1, 0),
(0, 'plg_content_contact', 'plugin', 'contact', 'content', 0, 1, 1, 0, 1, '', '', '', 2, 0),
(0, 'plg_content_emailcloak', 'plugin', 'emailcloak', 'content', 0, 1, 1, 0, 1, '', '{"mode":"1"}', '', 3, 0),
Expand Down
52 changes: 52 additions & 0 deletions layouts/plugins/captcha/math/mathcaptcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* @package Joomla.Plugin
* @subpackage Captcha.math
*
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

use Joomla\CMS\Document\Document;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Layout\FileLayout;

extract($displayData);

/**
* Layout variables
* -----------------
* @var string $name Name of the input field.
* @var array $attributes List of input attributes
* @var string $formula Formula
* @var integer $inputIdx Active Input index
* @var Document $document Document instance
* @var FileLayout $this Context
*/

$id = $attributes['id'] ?? '';
$class = str_replace(' required', '', ($attributes['class'] ?? ''));

$letters = 'abcdefghijklmnopqrstuvwxyz';
$classes = [
substr(str_shuffle($letters), 0, 2) . bin2hex(random_bytes(rand(2, 7))),
substr(str_shuffle($letters), 0, 2) . bin2hex(random_bytes(rand(2, 7))),
substr(str_shuffle($letters), 0, 2) . bin2hex(random_bytes(rand(2, 7))),
];

$styles = '';
foreach ($classes as $i => $c) {
$styles .= '.' . $c . '{display:' . ($i !== $inputIdx ? 'none' : 'block') . '}';
}

$document->getWebAssetManager()->addInlineStyle($styles, ['name' => 'inline.plg_captcha_math'])
?>
<div class="input-group mb-3">
<span class="input-group-text"><?php echo Text::_('PLG_CAPTCHA_MATH_ENTER_SOLUTION') ?> <?php echo $this->escape($formula); ?></span>
<input type="text" value="" size="4" name="<?php echo $name; ?>[]" id="<?php echo $id; ?>" class="form-control <?php echo $classes[0] . ' ' . $class; ?>"/>
<input type="text" value="" size="4" name="<?php echo $name; ?>[]" id="<?php echo $id; ?>1" class="form-control <?php echo $classes[1] . ' ' . $class; ?>"/>
<input type="text" value="" size="4" name="<?php echo $name; ?>[]" id="<?php echo $id; ?>2" class="form-control <?php echo $classes[2] . ' ' . $class; ?>"/>
</div>
5 changes: 4 additions & 1 deletion libraries/src/Captcha/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function display($name, $id, $class = '')
/**
* Checks if the answer is correct.
*
* @param string $code The answer.
* @param mixed $code The answer.
*
* @return bool Whether the provided answer was correct
*
Expand All @@ -203,6 +203,9 @@ public function display($name, $id, $class = '')
public function checkAnswer($code)
{
if ($this->provider) {
if (!is_scalar($code)) {
$code = json_encode($code);
}
return $this->provider->checkAnswer($code);
}

Expand Down
22 changes: 22 additions & 0 deletions plugins/captcha/math/math.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<extension type="plugin" group="captcha" method="upgrade">
<name>PLG_CAPTCHA_MATH</name>
<version>5.1.0</version>
<creationDate>2024-02</creationDate>
<author>Joomla! Project</author>
<authorEmail>[email protected]</authorEmail>
<authorUrl>www.joomla.org</authorUrl>
<copyright>(C) 2024 Open Source Matters, Inc.</copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<description>PLG_CAPTCHA_MATH_XML_DESCRIPTION</description>
<namespace path="src">Joomla\Plugin\Captcha\Math</namespace>
<files>
<folder plugin="math">services</folder>
<folder>src</folder>
</files>
<languages>
<language tag="en-GB">language/en-GB/plg_captcha_math.ini</language>
<language tag="en-GB">language/en-GB/plg_captcha_math.sys.ini</language>
</languages>
<config/>
</extension>
46 changes: 46 additions & 0 deletions plugins/captcha/math/services/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/**
* @package Joomla.Plugin
* @subpackage Captcha.math
*
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

\defined('_JEXEC') or die;

use Joomla\CMS\Extension\PluginInterface;
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;
use Joomla\Plugin\Captcha\Math\Extension\Math;

return new class () implements ServiceProviderInterface {
/**
* Registers the service provider with a DI container.
*
* @param Container $container The DI container.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function register(Container $container)
{
$container->set(
PluginInterface::class,
function (Container $container) {
$plugin = new Math(
$container->get(DispatcherInterface::class),
(array) PluginHelper::getPlugin('captcha', 'math')
);
$plugin->setApplication(Factory::getApplication());

return $plugin;
}
);
}
};
57 changes: 57 additions & 0 deletions plugins/captcha/math/src/Extension/Math.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* @package Joomla.Plugin
* @subpackage Captcha.math
*
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Plugin\Captcha\Math\Extension;

use Joomla\CMS\Event\Captcha\CaptchaSetupEvent;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Event\SubscriberInterface;
use Joomla\Plugin\Captcha\Math\Provider\MathCaptchaProvider;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
* Math captcha Plugin
*
* @since __DEPLOY_VERSION__
*/
final class Math extends CMSPlugin implements SubscriberInterface
{
/**
* Returns an array of events this plugin will listen to.
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
public static function getSubscribedEvents(): array
{
return [
'onCaptchaSetup' => 'onCaptchaSetup',
];
}

/**
* Register Captcha instance
*
* @param CaptchaSetupEvent $event
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function onCaptchaSetup(CaptchaSetupEvent $event)
{
$this->loadLanguage();
$event->getCaptchaRegistry()->add(new MathCaptchaProvider($this->getApplication()));
}
}
Loading