[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/plugins/task/sitestatus/src/Extension/ -> SiteStatus.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Plugins
   5   * @subpackage  Task.SiteStatus
   6   *
   7   * @copyright   (C) 2021 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Plugin\Task\SiteStatus\Extension;
  12  
  13  use Exception;
  14  use Joomla\CMS\Plugin\CMSPlugin;
  15  use Joomla\Component\Scheduler\Administrator\Event\ExecuteTaskEvent;
  16  use Joomla\Component\Scheduler\Administrator\Task\Status;
  17  use Joomla\Component\Scheduler\Administrator\Traits\TaskPluginTrait;
  18  use Joomla\Event\DispatcherInterface;
  19  use Joomla\Event\SubscriberInterface;
  20  use Joomla\Filesystem\File;
  21  use Joomla\Filesystem\Path;
  22  use Joomla\Registry\Registry;
  23  
  24  // phpcs:disable PSR1.Files.SideEffects
  25  \defined('_JEXEC') or die;
  26  // phpcs:enable PSR1.Files.SideEffects
  27  
  28  /**
  29   * Task plugin with routines to change the offline status of the site. These routines can be used to control planned
  30   * maintenance periods and related operations.
  31   *
  32   * @since  4.1.0
  33   */
  34  final class SiteStatus extends CMSPlugin implements SubscriberInterface
  35  {
  36      use TaskPluginTrait;
  37  
  38      /**
  39       * @var string[]
  40       * @since 4.1.0
  41       */
  42      protected const TASKS_MAP = [
  43          'plg_task_toggle_offline'             => [
  44              'langConstPrefix' => 'PLG_TASK_SITE_STATUS',
  45              'toggle'          => true,
  46          ],
  47          'plg_task_toggle_offline_set_online'  => [
  48              'langConstPrefix' => 'PLG_TASK_SITE_STATUS_SET_ONLINE',
  49              'toggle'          => false,
  50              'offline'         => false,
  51          ],
  52          'plg_task_toggle_offline_set_offline' => [
  53              'langConstPrefix' => 'PLG_TASK_SITE_STATUS_SET_OFFLINE',
  54              'toggle'          => false,
  55              'offline'         => true,
  56          ],
  57  
  58      ];
  59  
  60      /**
  61       * Autoload the language file.
  62       *
  63       * @var boolean
  64       * @since 4.1.0
  65       */
  66      protected $autoloadLanguage = true;
  67  
  68      /**
  69       * @inheritDoc
  70       *
  71       * @return string[]
  72       *
  73       * @since 4.1.0
  74       */
  75      public static function getSubscribedEvents(): array
  76      {
  77          return [
  78              'onTaskOptionsList' => 'advertiseRoutines',
  79              'onExecuteTask'     => 'alterSiteStatus',
  80          ];
  81      }
  82  
  83      /**
  84       * The old config
  85       *
  86       * @var    array
  87       * @since  4.2.0
  88       */
  89      private $oldConfig;
  90  
  91      /**
  92       * The config file
  93       *
  94       * @var    string
  95       * @since  4.2.0
  96       */
  97      private $configFile;
  98  
  99      /**
 100       * Constructor.
 101       *
 102       * @param   DispatcherInterface  $dispatcher  The dispatcher
 103       * @param   array                $config      An optional associative array of configuration settings
 104       * @param   array                $oldConfig   The old config
 105       * @param   string               $configFile  The config
 106       *
 107       * @since   4.2.0
 108       */
 109      public function __construct(DispatcherInterface $dispatcher, array $config, array $oldConfig, string $configFile)
 110      {
 111          parent::__construct($dispatcher, $config);
 112  
 113          $this->oldConfig  = $oldConfig;
 114          $this->configFile = $configFile;
 115      }
 116  
 117      /**
 118       * @param   ExecuteTaskEvent  $event  The onExecuteTask event
 119       *
 120       * @return void
 121       *
 122       * @since 4.1.0
 123       * @throws Exception
 124       */
 125      public function alterSiteStatus(ExecuteTaskEvent $event): void
 126      {
 127          if (!array_key_exists($event->getRoutineId(), self::TASKS_MAP)) {
 128              return;
 129          }
 130  
 131          $this->startRoutine($event);
 132  
 133          $config = $this->oldConfig;
 134  
 135          $toggle    = self::TASKS_MAP[$event->getRoutineId()]['toggle'];
 136          $oldStatus = $config['offline'] ? 'offline' : 'online';
 137  
 138          if ($toggle) {
 139              $config['offline'] = !$config['offline'];
 140          } else {
 141              $config['offline'] = self::TASKS_MAP[$event->getRoutineId()]['offline'];
 142          }
 143  
 144          $newStatus = $config['offline'] ? 'offline' : 'online';
 145          $exit      = $this->writeConfigFile(new Registry($config));
 146          $this->logTask(sprintf($this->getApplication()->getLanguage()->_('PLG_TASK_SITE_STATUS_TASK_LOG_SITE_STATUS'), $oldStatus, $newStatus));
 147  
 148          $this->endRoutine($event, $exit);
 149      }
 150  
 151      /**
 152       * Method to write the configuration to a file.
 153       *
 154       * @param   Registry  $config  A Registry object containing all global config data.
 155       *
 156       * @return  integer  The task exit code
 157       *
 158       * @since  4.1.0
 159       * @throws Exception
 160       */
 161      private function writeConfigFile(Registry $config): int
 162      {
 163          // Set the configuration file path.
 164          $file = $this->configFile;
 165  
 166          // Attempt to make the file writeable.
 167          if (file_exists($file) && Path::isOwner($file) && !Path::setPermissions($file)) {
 168              $this->logTask($this->getApplication()->getLanguage()->_('PLG_TASK_SITE_STATUS_ERROR_CONFIGURATION_PHP_NOTWRITABLE'), 'notice');
 169          }
 170  
 171          try {
 172              // Attempt to write the configuration file as a PHP class named JConfig.
 173              $configuration = $config->toString('PHP', array('class' => 'JConfig', 'closingtag' => false));
 174              File::write($file, $configuration);
 175          } catch (Exception $e) {
 176              $this->logTask($this->getApplication()->getLanguage()->_('PLG_TASK_SITE_STATUS_ERROR_WRITE_FAILED'), 'error');
 177  
 178              return Status::KNOCKOUT;
 179          }
 180  
 181          // Invalidates the cached configuration file
 182          if (function_exists('opcache_invalidate')) {
 183              opcache_invalidate($file);
 184          }
 185  
 186          // Attempt to make the file un-writeable.
 187          if (Path::isOwner($file) && !Path::setPermissions($file, '0444')) {
 188              $this->logTask($this->getApplication()->getLanguage()->_('PLG_TASK_SITE_STATUS_ERROR_CONFIGURATION_PHP_NOTUNWRITABLE'), 'notice');
 189          }
 190  
 191          return Status::OK;
 192      }
 193  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer