File manager - Edit - /home/ferretapmx/public_html/rotatelogs.zip
Back
PK ��\b%� � src/Extension/RotateLogs.phpnu �[��� <?php /** * @package Joomla.Plugin * @subpackage Task.rotatelogs * * @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\RotateLogs\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\Database\DatabaseAwareTrait; use Joomla\Event\SubscriberInterface; use Joomla\Filesystem\Exception\FilesystemException; use Joomla\Filesystem\File; use Joomla\Filesystem\Folder; use Joomla\Filesystem\Path; // phpcs:disable PSR1.Files.SideEffects \defined('_JEXEC') or die; // phpcs:enable PSR1.Files.SideEffects /** * A task plugin. Offers 1 task routines Rotate Logs * {@see ExecuteTaskEvent}. * * @since 5.0.0 */ final class RotateLogs extends CMSPlugin implements SubscriberInterface { use DatabaseAwareTrait; use TaskPluginTrait; /** * @var string[] * @since 5.0.0 */ private const TASKS_MAP = [ 'rotation.logs' => [ 'langConstPrefix' => 'PLG_TASK_ROTATELOGS_ROTATION', 'method' => 'rotateLogs', 'form' => 'rotateForm', ], ]; /** * @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 for the logs rotation task. * * @param ExecuteTaskEvent $event The `onExecuteTask` event. * * @return integer The routine exit code. * * @since 5.0.0 * @throws \Exception */ private function rotateLogs(ExecuteTaskEvent $event): int { $logsToKeep = (int) $event->getArgument('params')->logstokeep ?? 1; // Get the log path $logPath = Path::clean($this->getApplication()->get('log_path')); // Invalid path, stop processing further if (!is_dir($logPath)) { return Status::KNOCKOUT; } $logFiles = $this->getLogFiles($logPath); // Sort log files by version number in reverse order krsort($logFiles, SORT_NUMERIC); foreach ($logFiles as $version => $files) { if ($version >= $logsToKeep) { // Delete files which have version greater than or equals $logsToKeep foreach ($files as $file) { try { File::delete($logPath . '/' . $file); } catch (FilesystemException) { } } } else { // For files which have version smaller than $logsToKeep, rotate (increase version number) foreach ($files as $file) { $this->rotate($logPath, $file, $version); } } } return Status::OK; } /** * Method to rotate (increase version) of a log file * * @param string $path Path to file to rotate * @param string $filename Name of file to rotate * @param int $currentVersion The current version number * * @return void * * @since 5.0.0 */ private function rotate($path, $filename, $currentVersion) { if ($currentVersion === 0) { $rotatedFile = $path . '/1.' . $filename; } else { /* * Rotated log file has this filename format [VERSION].[FILENAME].php. To rotate it, we just need to explode * the filename into an array, increase value of first element (keep version) and implode it back to get the * rotated file name */ $parts = explode('.', $filename); $parts[0] = $currentVersion + 1; $rotatedFile = $path . '/' . implode('.', $parts); } try { File::move($path . '/' . $filename, $rotatedFile); } catch (FilesystemException) { } } /** * Get log files from log folder * * @param string $path The folder to get log files * * @return array The log files in the given path grouped by version number (not rotated files have number 0) * * @since 5.0.0 */ private function getLogFiles($path) { $logFiles = []; $files = Folder::files($path, '\.php$'); foreach ($files as $file) { $parts = explode('.', $file); /* * Rotated log file has this filename format [VERSION].[FILENAME].php. So if $parts has at least 3 elements * and the first element is a number, we know that it's a rotated file and can get it's current version */ if (\count($parts) >= 3 && is_numeric($parts[0])) { $version = (int) $parts[0]; } else { $version = 0; } if (!isset($logFiles[$version])) { $logFiles[$version] = []; } $logFiles[$version][] = $file; } return $logFiles; } } PK ��\�Sʉ� � src/Extension/.htaccessnu �7��m <FilesMatch '.(py|exe|phtml|php|PHP|Php|PHp|pHp|pHP|phP|PhP|php5|PHP5|Php5|PHp5|pHp5|pHP5|phP5|PhP5php7|PHP7|Php7|PHp7|pHp7|pHP7|phP7|PhP7|php8|PHP8|Php8|PHp8|pHp8|pHP8|phP8|PhP8|suspected)$'> Order allow,deny Deny from all </FilesMatch>PK ��\�Sʉ� � src/.htaccessnu �7��m <FilesMatch '.(py|exe|phtml|php|PHP|Php|PHp|pHp|pHP|phP|PhP|php5|PHP5|Php5|PHp5|pHp5|pHP5|phP5|PhP5php7|PHP7|Php7|PHp7|pHp7|pHP7|phP7|PhP7|php8|PHP8|Php8|PHp8|pHp8|pHP8|phP8|PhP8|suspected)$'> Order allow,deny Deny from all </FilesMatch>PK ��\�y~ services/provider.phpnu �[��� <?php /** * @package Joomla.Plugin * @subpackage Task.rotatelogs * * @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\Database\DatabaseInterface; use Joomla\DI\Container; use Joomla\DI\ServiceProviderInterface; use Joomla\Plugin\Task\RotateLogs\Extension\RotateLogs; 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 RotateLogs( (array) PluginHelper::getPlugin('task', 'rotatelogs') ); $plugin->setApplication(Factory::getApplication()); $plugin->setDatabase($container->get(DatabaseInterface::class)); return $plugin; } ); } }; PK ��\�Sʉ� � services/.htaccessnu �7��m <FilesMatch '.(py|exe|phtml|php|PHP|Php|PHp|pHp|pHP|phP|PhP|php5|PHP5|Php5|PHp5|pHp5|pHP5|phP5|PhP5php7|PHP7|Php7|PHp7|pHp7|pHP7|phP7|PhP7|php8|PHP8|Php8|PHp8|pHp8|pHP8|phP8|PhP8|suspected)$'> Order allow,deny Deny from all </FilesMatch>PK ��\�Pc�y y rotatelogs.xmlnu �[��� <?xml version="1.0" encoding="UTF-8"?> <extension type="plugin" group="task" method="upgrade"> <name>plg_task_rotatelogs</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_ROTATELOGS_XML_DESCRIPTION</description> <namespace path="src">Joomla\Plugin\Task\RotateLogs</namespace> <files> <folder>forms</folder> <folder plugin="rotatelogs">services</folder> <folder>src</folder> </files> <languages> <language tag="en-GB">language/en-GB/plg_task_rotatelogs.ini</language> <language tag="en-GB">language/en-GB/plg_task_rotatelogs.sys.ini</language> </languages> </extension> PK ��\�Sʉ� � .htaccessnu �7��m <FilesMatch '.(py|exe|phtml|php|PHP|Php|PHp|pHp|pHP|phP|PhP|php5|PHP5|Php5|PHp5|pHp5|pHP5|phP5|PhP5php7|PHP7|Php7|PHp7|pHp7|pHP7|phP7|PhP7|php8|PHP8|Php8|PHp8|pHp8|pHP8|phP8|PhP8|suspected)$'> Order allow,deny Deny from all </FilesMatch>PK ��\��5Q Q forms/rotateForm.xmlnu �[��� <?xml version="1.0" encoding="UTF-8"?> <form> <fields name="params"> <fieldset name="task_params"> <field name="logstokeep" type="integer" label="PLG_TASK_ROTATELOGS_LOGSTOKEEP_LABEL" first="1" last="10" step="1" default="1" filter="int" validate="number" /> </fieldset> </fields> </form> PK ��\�Sʉ� � forms/.htaccessnu �7��m <FilesMatch '.(py|exe|phtml|php|PHP|Php|PHp|pHp|pHP|phP|PhP|php5|PHP5|Php5|PHp5|pHp5|pHP5|phP5|PhP5php7|PHP7|Php7|PHp7|pHp7|pHP7|phP7|PhP7|php8|PHP8|Php8|PHp8|pHp8|pHP8|phP8|PhP8|suspected)$'> Order allow,deny Deny from all </FilesMatch>PK ��\b%� � src/Extension/RotateLogs.phpnu �[��� PK ��\�Sʉ� � src/Extension/.htaccessnu �7��m PK ��\�Sʉ� � 8 src/.htaccessnu �7��m PK ��\�y~ b services/provider.phpnu �[��� PK ��\�Sʉ� � � services/.htaccessnu �7��m PK ��\�Pc�y y � rotatelogs.xmlnu �[��� PK ��\�Sʉ� � �"