Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
50 changes: 50 additions & 0 deletions plugins/task/sitestatus/services/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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 __DEPLOY_VERSION__
*/
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;
}
);
}
};
6 changes: 3 additions & 3 deletions plugins/task/sitestatus/sitestatus.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
<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>
<filename plugin="sitestatus">sitestatus.php</filename>
<folder>language</folder>
<folder>forms</folder>
<folder plugin="sitestatus">services</folder>
<folder>src</folder>
</files>
<languages>
<language tag="en-GB">language/en-GB/plg_task_sitestatus.ini</language>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,29 @@
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Plugin\Task\SiteStatus\Extension;

// Restrict direct access
defined('_JEXEC') or die;

use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Filesystem\Path;
use Joomla\CMS\Language\Text;
use Exception;
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;
use Joomla\Utilities\ArrayHelper;

/**
* 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
*/
class PlgTaskSitestatus extends CMSPlugin implements SubscriberInterface
final class SiteStatus extends CMSPlugin implements SubscriberInterface
{
use TaskPluginTrait;

Expand All @@ -54,14 +55,6 @@ class PlgTaskSitestatus extends CMSPlugin implements SubscriberInterface

];

/**
* The application object.
*
* @var CMSApplication
* @since 4.1.0
*/
protected $app;

/**
* Autoload the language file.
*
Expand All @@ -85,6 +78,40 @@ public static function getSubscribedEvents(): array
];
}

/**
* The old config
*
* @var string
* @since __DEPLOY_VERSION__
*/
private $oldConfig;

/**
* The config file
*
* @var string
* @since __DEPLOY_VERSION__
*/
private $configFile;

/**
* Constructor.
*
* @param DispatcherInterface $dispatcher The dispatcher
* @param array $config An optional associative array of configuration settings
* @param string $oldConfig The old config
* @param string $configFile The config
*
* @since __DEPLOY_VERSION__
*/
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
*
Expand All @@ -102,7 +129,7 @@ public function alterSiteStatus(ExecuteTaskEvent $event): void

$this->startRoutine($event);

$config = ArrayHelper::fromObject(new JConfig);
$config = $this->oldConfig;

$toggle = self::TASKS_MAP[$event->getRoutineId()]['toggle'];
$oldStatus = $config['offline'] ? 'offline' : 'online';
Expand All @@ -113,13 +140,12 @@ public function alterSiteStatus(ExecuteTaskEvent $event): void
}
else
{
$offline = self::TASKS_MAP[$event->getRoutineId()]['offline'];
$config['offline'] = $offline;
$config['offline'] = self::TASKS_MAP[$event->getRoutineId()]['offline'];
}

$newStatus = $config['offline'] ? 'offline' : 'online';
$exit = $this->writeConfigFile(new Registry($config));
$this->logTask(Text::sprintf('PLG_TASK_SITE_STATUS_TASK_LOG_SITE_STATUS', $oldStatus, $newStatus));
$this->logTask($this->translate('PLG_TASK_SITE_STATUS_TASK_LOG_SITE_STATUS', $oldStatus, $newStatus));

$this->endRoutine($event, $exit);
}
Expand All @@ -137,20 +163,23 @@ public function alterSiteStatus(ExecuteTaskEvent $event): void
private function writeConfigFile(Registry $config): int
{
// Set the configuration file path.
$file = JPATH_CONFIGURATION . '/configuration.php';
$file = $this->configFile;

// Attempt to make the file writeable.
if (Path::isOwner($file) && !Path::setPermissions($file))
if (file_exists($file) && Path::isOwner($file) && !Path::setPermissions($file))
{
$this->logTask(Text::_('PLG_TASK_SITE_STATUS_ERROR_CONFIGURATION_PHP_NOTWRITABLE'), 'notice');
$this->logTask($this->translate('PLG_TASK_SITE_STATUS_ERROR_CONFIGURATION_PHP_NOTWRITABLE'), 'notice');
}

// Attempt to write the configuration file as a PHP class named JConfig.
$configuration = $config->toString('PHP', array('class' => 'JConfig', 'closingtag' => false));

if (!File::write($file, $configuration))
try
{
// Attempt to write the configuration file as a PHP class named JConfig.
$configuration = $config->toString('PHP', array('class' => 'JConfig', 'closingtag' => false));
File::write($file, $configuration);
}
catch (Exception $e)
{
$this->logTask(Text::_('PLG_TASK_SITE_STATUS_ERROR_WRITE_FAILED'), 'error');
$this->logTask($this->translate('PLG_TASK_SITE_STATUS_ERROR_WRITE_FAILED'), 'error');

return Status::KNOCKOUT;
}
Expand All @@ -164,7 +193,7 @@ private function writeConfigFile(Registry $config): int
// Attempt to make the file un-writeable.
if (Path::isOwner($file) && !Path::setPermissions($file, '0444'))
{
$this->logTask(Text::_('PLG_TASK_SITE_STATUS_ERROR_CONFIGURATION_PHP_NOTUNWRITABLE'), 'notice');
$this->logTask($this->translate('PLG_TASK_SITE_STATUS_ERROR_CONFIGURATION_PHP_NOTUNWRITABLE'), 'notice');
}

return Status::OK;
Expand Down
Loading