Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change: refactor captcha implementation #9595

Merged
merged 3 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libraries/kunena/src/Layout/KunenaBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class KunenaBase extends KunenaLayoutBase
public $userhasvoted;
public $show;
public $show_title;
public $captchaDisplay;
public $body;

/**
Expand Down
64 changes: 43 additions & 21 deletions src/libraries/kunena/src/Template/KunenaTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
use Exception;
use Joomla\CMS\Document\Document;
use Joomla\CMS\Factory;
use Joomla\Filesystem\Folder;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Uri\Uri;
use Joomla\Event\Event;
use Joomla\Filesystem\Folder;
use Joomla\Registry\Registry;
use Kunena\Forum\Libraries\Config\KunenaConfig;
use Kunena\Forum\Libraries\Error\KunenaError;
Expand Down Expand Up @@ -447,13 +448,13 @@ public static function getInstance($name = null)
}

if (class_exists($classname)) {
self::$_instances [$name] = new $classname($templatename);
self::$_instances[$name] = new $classname($templatename);
} else {
self::$_instances [$name] = new KunenaTemplate($templatename);
self::$_instances[$name] = new KunenaTemplate($templatename);
}
}

return self::$_instances [$name];
return self::$_instances[$name];
}

/**
Expand Down Expand Up @@ -543,8 +544,8 @@ public function loadLanguage(): void

foreach (array_reverse($this->default) as $template) {
$lang->load('kunena_tmpl_' . $template, JPATH_SITE)
|| $lang->load('kunena_tmpl_' . $template, KPATH_SITE)
|| $lang->load('kunena_tmpl_' . $template, KPATH_SITE . '/template/' . $template);
|| $lang->load('kunena_tmpl_' . $template, KPATH_SITE)
|| $lang->load('kunena_tmpl_' . $template, KPATH_SITE . '/template/' . $template);
}
}

Expand Down Expand Up @@ -583,8 +584,10 @@ public function getUserClasses(): array
public function getButton(string $link, string $name, string $scope, string $type, $id = null): string
{
$types = ['communication' => 'comm', 'user' => 'user', 'moderation' => 'mod'];
$names = ['unsubscribe' => 'subscribe', 'unfavorite' => 'favorite', 'unsticky' => 'sticky', 'unlock' => 'lock', 'create' => 'newtopic',
'quickReply' => 'reply', 'quote' => 'kquote', 'edit' => 'kedit', ];
$names = [
'unsubscribe' => 'subscribe', 'unfavorite' => 'favorite', 'unsticky' => 'sticky', 'unlock' => 'lock', 'create' => 'newtopic',
'quickReply' => 'reply', 'quote' => 'kquote', 'edit' => 'kedit',
];

$text = Text::_("COM_KUNENA_BUTTON_{$scope}_{$name}");
$title = Text::_("COM_KUNENA_BUTTON_{$scope}_{$name}_LONG");
Expand Down Expand Up @@ -873,9 +876,9 @@ public function createCacheDir()
// Compile CSS from SCSS files.
$ktemplate = KunenaFactory::getTemplate();
$ktemplate->compileScss("assets/scss/{$this->name}.scss", 'kunena.css');

$filenamescss = JPATH_SITE . "/components/com_kunena/template/{$this->name}/assets/scss/custom.scss";

if (file_exists($filenamescss) && 0 != fileSize($filenamescss)) {
$this->compileScss('assets/scss/custom.scss', 'kunena-custom.css');
$this->addStyleSheet('kunena-custom.css');
Expand Down Expand Up @@ -1781,22 +1784,41 @@ public function tooltips(bool $class = false): string
*
* @throws \Exception
* @since Kunena 6.0
* @deprecated Kunena 6.3 Use getCaptcha() instead
*/
public function recaptcha(bool $topic_ids = false): string
{
if (PluginHelper::isEnabled('captcha')) {
PluginHelper::importPlugin('captcha');
return $this->getCaptcha();
}

if ($topic_ids) {
PluginHelper::importPlugin('captcha');
Factory::getApplication()->triggerEvent('onInit', ['dynamic_recaptcha_' . $topic_ids]);
$display = Factory::getApplication()->triggerEvent('onDisplay', [null, 'dynamic_recaptcha_' . $topic_ids, 'controls g-recaptcha']);
} else {
Factory::getApplication()->triggerEvent('onInit', ['dynamic_recaptcha_1']);
$display = Factory::getApplication()->triggerEvent('onDisplay', [null, 'dynamic_recaptcha_1', 'controls g-recaptcha']);
}
/**
* Function to get the Global Configured Captcha
*
* @return string
*
* @throws \Exception
* @since Kunena 6.3
*/
public function getCaptcha(): string
{
$app = Factory::getApplication();
$captcha = $app->get('captcha', '0');

return $display[0];
if ($captcha && PluginHelper::isEnabled('captcha', $captcha)) {
PluginHelper::importPlugin('captcha', $captcha);
// $captchaInitEvent = new Event('onInit', ['kunena_captcha_' . (int) $topic_ids]);
// $captchaDisplayEvent = new Event('onDisplay', [null, 'kunena_captcha_' . $topic_ids, 'required']);
$captchaInitEvent = new Event('onInit', []);
$captchaDisplayEvent = new Event('onDisplay', []);

$app->getDispatcher()->dispatch('onInit', $captchaInitEvent);
$app->getDispatcher()->dispatch('onDisplay', $captchaDisplayEvent);

$result = $captchaDisplayEvent->getArgument('result') ?: [];

if (is_array($result) && count($result) >= 1) {
return $result[0];
}
}

return '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,27 @@ class TopicFormCreateDisplay extends KunenaControllerDisplay
public $allowedExtensions;

public $action;

public $selectcatlist;

public $selected;

public $captchaEnabled;

public $topicIcons;

public $message;

public $topic;

public $category;

public $ktemplate;

public $me;

public $captchaDisplay;

/**
* @var string
* @since Kunena 6.0
Expand Down Expand Up @@ -172,7 +175,7 @@ protected function before()
}

if ($this->topic->isAuthorised('create') && $this->me->canDoCaptcha()) {
$this->captchaDisplay = $this->ktemplate->recaptcha();
$this->captchaDisplay = $this->ktemplate->getCaptcha();
$this->captchaEnabled = true;
} else {
$this->captchaEnabled = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

use Exception;
use Joomla\CMS\Document\HtmlDocument;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Registry\Registry;
Expand Down Expand Up @@ -86,6 +85,8 @@ class TopicFormReplyDisplay extends KunenaControllerDisplay

public $topic;

public $captchaDisplay;

/**
* Prepare topic reply form.
*
Expand Down Expand Up @@ -141,7 +142,7 @@ protected function before()
$this->category = $this->topic->getCategory();

if ($parent->isAuthorised('reply') && $this->me->canDoCaptcha()) {
$this->captchaDisplay = KunenaTemplate::getInstance()->recaptcha();
$this->captchaDisplay = KunenaTemplate::getInstance()->getCaptcha();
$this->captchaEnabled = true;
} else {
$this->captchaEnabled = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ class TopicItemMessageDisplay extends KunenaControllerDisplay

public $thankyou_delete;

public $captchaDisplay;

/**
* Prepare displaying message.
*
Expand Down Expand Up @@ -137,7 +139,7 @@ protected function before()
$this->captchaEnabled = false;

if ($this->message->isAuthorised('reply') && $this->me->canDoCaptcha() && $this->config->quickReply) {
$this->captchaDisplay = KunenaTemplate::getInstance()->recaptcha();
$this->captchaDisplay = KunenaTemplate::getInstance()->getCaptcha();
$this->captchaEnabled = true;
} else {
$this->captchaEnabled = false;
Expand Down Expand Up @@ -185,8 +187,8 @@ protected function before()
$this->reportMessageLink = HTMLHelper::_(
'link',
'index.php?option=com_kunena&view=topic&layout=report&catid='
. \intval($this->category->id) . '&id=' . \intval($this->message->thread)
. '&mesid=' . \intval($this->message->id),
. \intval($this->category->id) . '&id=' . \intval($this->message->thread)
. '&mesid=' . \intval($this->message->id),
Text::_('COM_KUNENA_REPORT'),
Text::_('COM_KUNENA_REPORT')
);
Expand Down
24 changes: 9 additions & 15 deletions src/site/src/Controllers/TopicController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Joomla\CMS\Session\Session;
use Joomla\CMS\Uri\Uri;
use Joomla\Database\Exception\ExecutionFailureException;
use Joomla\Event\Event;
use Joomla\Registry\Registry;
use Joomla\Utilities\ArrayHelper;
use Kunena\Forum\Libraries\Access\KunenaAccess;
Expand Down Expand Up @@ -641,24 +642,17 @@ public function post()
}

if ($this->me->canDoCaptcha()) {
if (PluginHelper::isEnabled('captcha')) {
$plugin = PluginHelper::getPlugin('captcha');
$params = new Registry($plugin[0]->params);
$app = Factory::getApplication();
$captcha = $app->get('captcha', '0');

$captcha_pubkey = $params->get('public_key');
$captcha_privkey = $params->get('private_key');
if ($captcha && PluginHelper::isEnabled('captcha', $captcha)) {
$captchaCheckEvent = new Event('onCheckAnswer', []);
$app->getDispatcher()->dispatch('onCheckAnswer', $captchaCheckEvent);

if (!empty($captcha_pubkey) && !empty($captcha_privkey)) {
PluginHelper::importPlugin('captcha');
$result = $captchaCheckEvent->getArgument('result') ?: [];

$captcha_response = $this->app->input->getString('g-recaptcha-response');

if (!empty($captcha_response)) {
// For ReCaptcha API 2.0
$res = $this->app->triggerEvent('onCheckAnswer', [$this->app->input->getString('g-recaptcha-response')]);
}

if (!$res[0]) {
if (is_array($result) && count($result) >= 1) {
if (!$result[0]) {
$this->setRedirectBack();

return;
Expand Down
Loading
Loading