first commit
This commit is contained in:
49
plugins/task/sitestatus/services/provider.php
Normal file
49
plugins/task/sitestatus/services/provider.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Plugin
|
||||
* @subpackage Task.SiteStatus
|
||||
*
|
||||
* @copyright (C) 2022 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\Task\SiteStatus\Extension\SiteStatus;
|
||||
use Joomla\Utilities\ArrayHelper;
|
||||
|
||||
return new class () implements ServiceProviderInterface {
|
||||
/**
|
||||
* Registers the service provider with a DI container.
|
||||
*
|
||||
* @param Container $container The DI container.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 4.2.0
|
||||
*/
|
||||
public function register(Container $container)
|
||||
{
|
||||
$container->set(
|
||||
PluginInterface::class,
|
||||
function (Container $container) {
|
||||
$plugin = new SiteStatus(
|
||||
$container->get(DispatcherInterface::class),
|
||||
(array) PluginHelper::getPlugin('task', 'sitestatus'),
|
||||
ArrayHelper::fromObject(new JConfig()),
|
||||
JPATH_CONFIGURATION . '/configuration.php'
|
||||
);
|
||||
$plugin->setApplication(Factory::getApplication());
|
||||
|
||||
return $plugin;
|
||||
}
|
||||
);
|
||||
}
|
||||
};
|
||||
21
plugins/task/sitestatus/sitestatus.xml
Normal file
21
plugins/task/sitestatus/sitestatus.xml
Normal file
@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<extension type="plugin" group="task" method="upgrade">
|
||||
<name>plg_task_site_status</name>
|
||||
<author>Joomla! Project</author>
|
||||
<creationDate>2021-08</creationDate>
|
||||
<copyright>(C) 2021 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>4.1</version>
|
||||
<description>PLG_TASK_SITE_STATUS_XML_DESCRIPTION</description>
|
||||
<namespace path="src">Joomla\Plugin\Task\SiteStatus</namespace>
|
||||
<files>
|
||||
<folder plugin="sitestatus">services</folder>
|
||||
<folder>src</folder>
|
||||
</files>
|
||||
<languages>
|
||||
<language tag="en-GB">language/en-GB/plg_task_sitestatus.ini</language>
|
||||
<language tag="en-GB">language/en-GB/plg_task_sitestatus.sys.ini</language>
|
||||
</languages>
|
||||
</extension>
|
||||
192
plugins/task/sitestatus/src/Extension/SiteStatus.php
Normal file
192
plugins/task/sitestatus/src/Extension/SiteStatus.php
Normal file
@ -0,0 +1,192 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Plugins
|
||||
* @subpackage Task.SiteStatus
|
||||
*
|
||||
* @copyright (C) 2021 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Plugin\Task\SiteStatus\Extension;
|
||||
|
||||
use Joomla\CMS\Plugin\CMSPlugin;
|
||||
use Joomla\Component\Scheduler\Administrator\Event\ExecuteTaskEvent;
|
||||
use Joomla\Component\Scheduler\Administrator\Task\Status;
|
||||
use Joomla\Component\Scheduler\Administrator\Traits\TaskPluginTrait;
|
||||
use Joomla\Event\DispatcherInterface;
|
||||
use Joomla\Event\SubscriberInterface;
|
||||
use Joomla\Filesystem\File;
|
||||
use Joomla\Filesystem\Path;
|
||||
use Joomla\Registry\Registry;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Task plugin with routines to change the offline status of the site. These routines can be used to control planned
|
||||
* maintenance periods and related operations.
|
||||
*
|
||||
* @since 4.1.0
|
||||
*/
|
||||
final class SiteStatus extends CMSPlugin implements SubscriberInterface
|
||||
{
|
||||
use TaskPluginTrait;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
* @since 4.1.0
|
||||
*/
|
||||
protected const TASKS_MAP = [
|
||||
'plg_task_toggle_offline' => [
|
||||
'langConstPrefix' => 'PLG_TASK_SITE_STATUS',
|
||||
'toggle' => true,
|
||||
],
|
||||
'plg_task_toggle_offline_set_online' => [
|
||||
'langConstPrefix' => 'PLG_TASK_SITE_STATUS_SET_ONLINE',
|
||||
'toggle' => false,
|
||||
'offline' => false,
|
||||
],
|
||||
'plg_task_toggle_offline_set_offline' => [
|
||||
'langConstPrefix' => 'PLG_TASK_SITE_STATUS_SET_OFFLINE',
|
||||
'toggle' => false,
|
||||
'offline' => true,
|
||||
],
|
||||
|
||||
];
|
||||
|
||||
/**
|
||||
* Autoload the language file.
|
||||
*
|
||||
* @var boolean
|
||||
* @since 4.1.0
|
||||
*/
|
||||
protected $autoloadLanguage = true;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*
|
||||
* @return string[]
|
||||
*
|
||||
* @since 4.1.0
|
||||
*/
|
||||
public static function getSubscribedEvents(): array
|
||||
{
|
||||
return [
|
||||
'onTaskOptionsList' => 'advertiseRoutines',
|
||||
'onExecuteTask' => 'alterSiteStatus',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* The old config
|
||||
*
|
||||
* @var array
|
||||
* @since 4.2.0
|
||||
*/
|
||||
private $oldConfig;
|
||||
|
||||
/**
|
||||
* The config file
|
||||
*
|
||||
* @var string
|
||||
* @since 4.2.0
|
||||
*/
|
||||
private $configFile;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param DispatcherInterface $dispatcher The dispatcher
|
||||
* @param array $config An optional associative array of configuration settings
|
||||
* @param array $oldConfig The old config
|
||||
* @param string $configFile The config
|
||||
*
|
||||
* @since 4.2.0
|
||||
*/
|
||||
public function __construct(DispatcherInterface $dispatcher, array $config, array $oldConfig, string $configFile)
|
||||
{
|
||||
parent::__construct($dispatcher, $config);
|
||||
|
||||
$this->oldConfig = $oldConfig;
|
||||
$this->configFile = $configFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ExecuteTaskEvent $event The onExecuteTask event
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 4.1.0
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function alterSiteStatus(ExecuteTaskEvent $event): void
|
||||
{
|
||||
if (!\array_key_exists($event->getRoutineId(), self::TASKS_MAP)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->startRoutine($event);
|
||||
|
||||
$config = $this->oldConfig;
|
||||
|
||||
$toggle = self::TASKS_MAP[$event->getRoutineId()]['toggle'];
|
||||
$oldStatus = $config['offline'] ? 'offline' : 'online';
|
||||
|
||||
if ($toggle) {
|
||||
$config['offline'] = !$config['offline'];
|
||||
} else {
|
||||
$config['offline'] = self::TASKS_MAP[$event->getRoutineId()]['offline'];
|
||||
}
|
||||
|
||||
$newStatus = $config['offline'] ? 'offline' : 'online';
|
||||
$exit = $this->writeConfigFile(new Registry($config));
|
||||
$this->logTask(sprintf($this->getApplication()->getLanguage()->_('PLG_TASK_SITE_STATUS_TASK_LOG_SITE_STATUS'), $oldStatus, $newStatus));
|
||||
|
||||
$this->endRoutine($event, $exit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to write the configuration to a file.
|
||||
*
|
||||
* @param Registry $config A Registry object containing all global config data.
|
||||
*
|
||||
* @return integer The task exit code
|
||||
*
|
||||
* @since 4.1.0
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function writeConfigFile(Registry $config): int
|
||||
{
|
||||
// Set the configuration file path.
|
||||
$file = $this->configFile;
|
||||
|
||||
// Attempt to make the file writeable.
|
||||
if (file_exists($file) && Path::isOwner($file) && !Path::setPermissions($file)) {
|
||||
$this->logTask($this->getApplication()->getLanguage()->_('PLG_TASK_SITE_STATUS_ERROR_CONFIGURATION_PHP_NOTWRITABLE'), 'notice');
|
||||
}
|
||||
|
||||
try {
|
||||
// Attempt to write the configuration file as a PHP class named JConfig.
|
||||
$configuration = $config->toString('PHP', ['class' => 'JConfig', 'closingtag' => false]);
|
||||
File::write($file, $configuration);
|
||||
} catch (\Exception $e) {
|
||||
$this->logTask($this->getApplication()->getLanguage()->_('PLG_TASK_SITE_STATUS_ERROR_WRITE_FAILED'), 'error');
|
||||
|
||||
return Status::KNOCKOUT;
|
||||
}
|
||||
|
||||
// Invalidates the cached configuration file
|
||||
if (\function_exists('opcache_invalidate')) {
|
||||
opcache_invalidate($file);
|
||||
}
|
||||
|
||||
// Attempt to make the file un-writeable.
|
||||
if (Path::isOwner($file) && !Path::setPermissions($file, '0444')) {
|
||||
$this->logTask($this->getApplication()->getLanguage()->_('PLG_TASK_SITE_STATUS_ERROR_CONFIGURATION_PHP_NOTUNWRITABLE'), 'notice');
|
||||
}
|
||||
|
||||
return Status::OK;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user