first commit

This commit is contained in:
2025-06-17 11:53:18 +02:00
commit 9f0f7ba12b
8804 changed files with 1369176 additions and 0 deletions

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<form>
<fields name="params">
<fieldset name="task_params">
<field
name="consentexpiration"
type="integer"
label="PLG_TASK_PRIVACYCONSENT_CONSENTEXPIRATIONDAYS_LABEL"
description="PLG_TASK_PRIVACYCONSENT_CONSENTEXPIRATIONDAYS_DESC"
first="0"
last="720"
step="30"
default="360"
filter="int"
validate="number"
/>
<field
name="remind"
type="integer"
label="PLG_TASK_PRIVACYCONSENT_REMINDBEFORE_LABEL"
description="PLG_TASK_PRIVACYCONSENT_REMINDBEFORE_DESC"
first="0"
last="120"
step="1"
default="30"
filter="int"
validate="number"
/>
</fieldset>
</fields>
</form>

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<extension type="plugin" group="task" method="upgrade">
<name>plg_task_privacyconsent</name>
<author>Joomla! Project</author>
<creationDate>2023-07</creationDate>
<copyright>(C) 2023 Open Source Matters, Inc.</copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<authorEmail>admin@joomla.org</authorEmail>
<authorUrl>www.joomla.org</authorUrl>
<version>5.0.0</version>
<description>PLG_TASK_PRIVACYCONSENT_XML_DESCRIPTION</description>
<namespace path="src">Joomla\Plugin\Task\PrivacyConsent</namespace>
<files>
<folder>forms</folder>
<folder plugin="privacyconsent">services</folder>
<folder>src</folder>
</files>
<languages>
<language tag="en-GB">language/en-GB/plg_task_privacyconsent.ini</language>
<language tag="en-GB">language/en-GB/plg_task_privacyconsent.sys.ini</language>
</languages>
</extension>

View File

@ -0,0 +1,50 @@
<?php
/**
* @package Joomla.Plugin
* @subpackage Task.PrivacyConsent
*
* @copyright (C) 2023 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\CMS\User\UserFactoryInterface;
use Joomla\Database\DatabaseInterface;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;
use Joomla\Plugin\Task\PrivacyConsent\Extension\PrivacyConsent;
return new class () implements ServiceProviderInterface {
/**
* Registers the service provider with a DI container.
*
* @param Container $container The DI container.
*
* @return void
*
* @since 5.0.0
*/
public function register(Container $container)
{
$container->set(
PluginInterface::class,
function (Container $container) {
$plugin = new PrivacyConsent(
$container->get(DispatcherInterface::class),
(array) PluginHelper::getPlugin('task', 'privacyconsent')
);
$plugin->setApplication(Factory::getApplication());
$plugin->setDatabase($container->get(DatabaseInterface::class));
$plugin->setUserFactory($container->get(UserFactoryInterface::class));
return $plugin;
}
);
}
};

View File

@ -0,0 +1,261 @@
<?php
/**
* @package Joomla.Plugin
* @subpackage Task.PrivacyConsent
*
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Plugin\Task\PrivacyConsent\Extension;
use Joomla\CMS\Application\ApplicationHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Mail\Exception\MailDisabledException;
use Joomla\CMS\Mail\MailTemplate;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\User\UserFactoryAwareTrait;
use Joomla\CMS\User\UserHelper;
use Joomla\Component\Scheduler\Administrator\Event\ExecuteTaskEvent;
use Joomla\Component\Scheduler\Administrator\Task\Status;
use Joomla\Component\Scheduler\Administrator\Task\Task;
use Joomla\Component\Scheduler\Administrator\Traits\TaskPluginTrait;
use Joomla\Database\DatabaseAwareTrait;
use Joomla\Database\ParameterType;
use Joomla\Event\SubscriberInterface;
use PHPMailer\PHPMailer\Exception as phpmailerException;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* A task plugin. Offers 2 task routines Invalidate Expired Consents and Remind Expired Consents
* {@see ExecuteTaskEvent}.
*
* @since 5.0.0
*/
final class PrivacyConsent extends CMSPlugin implements SubscriberInterface
{
use DatabaseAwareTrait;
use TaskPluginTrait;
use UserFactoryAwareTrait;
/**
* @var string[]
* @since 5.0.0
*/
private const TASKS_MAP = [
'privacy.consent' => [
'langConstPrefix' => 'PLG_TASK_PRIVACYCONSENT_INVALIDATE',
'method' => 'privacyConsents',
'form' => 'privacyconsentForm',
],
];
/**
* @var boolean
*
* @since 5.0.0
*/
protected $autoloadLanguage = true;
/**
* @inheritDoc
*
* @return string[]
*
* @since 5.0.0
*/
public static function getSubscribedEvents(): array
{
return [
'onTaskOptionsList' => 'advertiseRoutines',
'onExecuteTask' => 'standardRoutineHandler',
'onContentPrepareForm' => 'enhanceTaskItemForm',
];
}
/**
* Method to send the remind for privacy consents renew.
*
* @param ExecuteTaskEvent $event The `onExecuteTask` event.
*
* @return integer The routine exit code.
*
* @since 5.0.0
* @throws \Exception
*/
private function privacyConsents(ExecuteTaskEvent $event): int
{
// Load the parameters.
$expire = (int) $event->getArgument('params')->consentexpiration ?? 365;
$remind = (int) $event->getArgument('params')->remind ?? 30;
if (
$this->invalidateExpiredConsents($expire) === Status::OK
&& $this->remindExpiringConsents($expire, $remind) === Status::OK
) {
return Status::OK;
}
return Status::KNOCKOUT;
}
/**
* Method to send the remind for privacy consents renew.
*
* @param integer $expire
* @param integer $remind
*
* @return integer The routine exit code.
*
* @since 5.0.0
* @throws \Exception
*/
private function remindExpiringConsents($expire, $remind): int
{
$now = Factory::getDate()->toSql();
$period = '-' . ($expire - $remind);
$db = $this->getDatabase();
$query = $db->getQuery(true);
$query->select($db->quoteName(['r.id', 'r.user_id', 'u.email']))
->from($db->quoteName('#__privacy_consents', 'r'))
->join('LEFT', $db->quoteName('#__users', 'u'), $db->quoteName('u.id') . ' = ' . $db->quoteName('r.user_id'))
->where($db->quoteName('subject') . ' = ' . $db->quote('PLG_SYSTEM_PRIVACYCONSENT_SUBJECT'))
->where($db->quoteName('remind') . ' = 0')
->where($query->dateAdd($db->quote($now), $period, 'DAY') . ' > ' . $db->quoteName('created'));
try {
$users = $db->setQuery($query)->loadObjectList();
} catch (\RuntimeException $exception) {
return Status::KNOCKOUT;
}
// Do not process further if no expired consents found
if (empty($users)) {
return Status::OK;
}
$app = $this->getApplication();
$linkMode = $app->get('force_ssl', 0) == 2 ? Route::TLS_FORCE : Route::TLS_IGNORE;
foreach ($users as $user) {
$token = ApplicationHelper::getHash(UserHelper::genRandomPassword());
$hashedToken = UserHelper::hashPassword($token);
// The mail
try {
$templateData = [
'sitename' => $app->get('sitename'),
'url' => Uri::root(),
'tokenurl' => Route::link('site', 'index.php?option=com_privacy&view=remind&remind_token=' . $token, false, $linkMode, true),
'formurl' => Route::link('site', 'index.php?option=com_privacy&view=remind', false, $linkMode, true),
'token' => $token,
];
$mailer = new MailTemplate('plg_task_privacyconsent.request.reminder', $app->getLanguage()->getTag());
$mailer->addTemplateData($templateData);
$mailer->addRecipient($user->email);
$mailResult = $mailer->send();
if ($mailResult === false) {
return Status::KNOCKOUT;
}
$userId = (int) $user->id;
// Update the privacy_consents item to not send the reminder again
$query->clear()
->update($db->quoteName('#__privacy_consents'))
->set($db->quoteName('remind') . ' = 1')
->set($db->quoteName('token') . ' = :token')
->where($db->quoteName('id') . ' = :userid')
->bind(':token', $hashedToken)
->bind(':userid', $userId, ParameterType::INTEGER);
$db->setQuery($query);
try {
$db->execute();
} catch (\RuntimeException $e) {
return Status::KNOCKOUT;
}
} catch (MailDisabledException | phpmailerException $exception) {
return Status::KNOCKOUT;
}
}
$this->logTask('Remind end');
return Status::OK;
}
/**
* Method to delete the expired privacy consents.
*
* @param integer $expire
*
* @return integer The routine exit code.
*
* @since 5.0.0
* @throws \Exception
*/
private function invalidateExpiredConsents($expire): int
{
$now = Factory::getDate()->toSql();
$period = '-' . $expire;
$db = $this->getDatabase();
$query = $db->getQuery(true);
$query->select($db->quoteName(['id', 'user_id']))
->from($db->quoteName('#__privacy_consents'))
->where($query->dateAdd($db->quote($now), $period, 'DAY') . ' > ' . $db->quoteName('created'))
->where($db->quoteName('subject') . ' = ' . $db->quote('PLG_SYSTEM_PRIVACYCONSENT_SUBJECT'))
->where($db->quoteName('state') . ' = 1');
$db->setQuery($query);
try {
$users = $db->loadObjectList();
} catch (\RuntimeException $e) {
return Status::KNOCKOUT;
}
// Do not process further if no expired consents found
if (empty($users)) {
return Status::OK;
}
// Push a notification to the site's super users
/** @var MessageModel $messageModel */
$messageModel = $this->getApplication()->bootComponent('com_messages')->getMVCFactory()->createModel('Message', 'Administrator');
foreach ($users as $user) {
$userId = (int) $user->id;
$query = $db->getQuery(true)
->update($db->quoteName('#__privacy_consents'))
->set($db->quoteName('state') . ' = 0')
->where($db->quoteName('id') . ' = :userid')
->bind(':userid', $userId, ParameterType::INTEGER);
$db->setQuery($query);
try {
$db->execute();
} catch (\RuntimeException $e) {
return Status::KNOCKOUT;
}
$messageModel->notifySuperUsers(
Text::_('PLG_TASK_PRIVACYCONSENT_NOTIFICATION_USER_PRIVACY_EXPIRED_SUBJECT'),
Text::sprintf('PLG_TASK_PRIVACYCONSENT_NOTIFICATION_USER_PRIVACY_EXPIRED_MESSAGE', $this->getUserFactory()->loadUserById($user->user_id)->username)
);
}
return Status::OK;
}
}