CoreFiles by mySites.guru
POWCaptcha.php93 lines, 2,239 bytes, from Joomla_6.1.2-Stable-Full_Package.zip
Raw History Compare with 6.1.1
md5
a489de946bc0a69480e78e542d99a291
sha1
0451f0c4e47a84856d4ca2f808495c88ecd5055e
sha256
e603b1f8a1ebb43af68b207aaee392ab6f641439b68cba560646f94ee62f77f9
<?php

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

namespace Joomla\Plugin\Captcha\POWCaptcha\Extension;

use Joomla\CMS\Event\Captcha\CaptchaSetupEvent;
use Joomla\CMS\Event\Plugin\AjaxEvent;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Session\Session;
use Joomla\Event\SubscriberInterface;
use Joomla\Plugin\Captcha\POWCaptcha\Provider\POWCaptchaProvider;

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

/**
 * Proof of work captcha Plugin
 * Based on the ALTCHA captcha library
 *
 * @since 6.1.0
 */
final class POWCaptcha extends CMSPlugin implements SubscriberInterface
{
    /**
     * Load the language file on instantiation.
     *
     * @var    boolean
     * @since 6.1.0
     */
    protected $autoloadLanguage = true;

    public static function getSubscribedEvents(): array
    {
        return [
            'onAjaxPowcaptcha' => 'handleAjaxRequest',
            'onCaptchaSetup'   => 'setupCaptcha',
        ];
    }

    /**
     * Register Captcha instance
     *
     * @param CaptchaSetupEvent $event
     *
     * @return void
     */
    public function setupCaptcha(CaptchaSetupEvent $event)
    {
        $event->getCaptchaRegistry()->add($this->getProvider());
    }

    /**
     * Handles the ajax request triggered by altcha to fetch the challenge code
     *
     * @param AjaxEvent $event
     */
    public function handleAjaxRequest(AjaxEvent $event)
    {
        // CRSF Token check
        if (!Session::checkToken('get')) {
            $event->updateEventResult(json_encode([]));

            return;
        }

        $event->updateEventResult(
            json_encode(
                $this->getProvider()->getChallenge()
            )
        );
    }

    /**
     * Returns the actual captcha provider instance
     *
     * @return POWCaptchaProvider
     */
    protected function getProvider(): POWCaptchaProvider
    {
        return new POWCaptchaProvider(
            $this->params,
            $this->getApplication()
        );
    }
}

The same content is in 23 releases, Joomla 6.1.0-alpha2 to 6.2.0-beta2. See them all, or every version of this file.