-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
SessionGC.php
136 lines (115 loc) · 3.81 KB
/
SessionGC.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* @package Joomla.Plugins
* @subpackage Task.sessiongc
*
* @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\SessionGC\Extension;
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Session\MetadataManager;
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\Event\DispatcherInterface;
use Joomla\Event\SubscriberInterface;
// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
/**
* A task plugin. Session data purge task.
* {@see ExecuteTaskEvent}.
*
* @since 5.0.0
*/
final class SessionGC extends CMSPlugin implements SubscriberInterface
{
use TaskPluginTrait;
/**
* The meta data manager
*
* @var MetadataManager
*
* @since 4.4.0
*/
private $metadataManager;
/**
* @var string[]
* @since 5.0.0
*/
private const TASKS_MAP = [
'session.gc' => [
'langConstPrefix' => 'PLG_TASK_SESSIONGC',
'method' => 'sessionGC',
'form' => 'sessionGCForm',
],
];
/**
* @var boolean
* @since 5.0.0
*/
protected $autoloadLanguage = true;
/**
* Constructor.
*
* @param DispatcherInterface $dispatcher The dispatcher
* @param array $config An optional associative array of configuration settings
* @param MetadataManager $metadataManager The user factory
*
* @since 4.4.0
*/
public function __construct(DispatcherInterface $dispatcher, array $config, MetadataManager $metadataManager)
{
parent::__construct($dispatcher, $config);
$this->metadataManager = $metadataManager;
}
/**
* @inheritDoc
*
* @return string[]
*
* @since 5.0.0
*/
public static function getSubscribedEvents(): array
{
return [
'onTaskOptionsList' => 'advertiseRoutines',
'onExecuteTask' => 'standardRoutineHandler',
'onContentPrepareForm' => 'enhanceTaskItemForm',
];
}
/**
* @param ExecuteTaskEvent $event The `onExecuteTask` event.
*
* @return integer The routine exit code.
*
* @since 5.0.0
* @throws \Exception
*/
private function sessionGC(ExecuteTaskEvent $event): int
{
$enableGC = (int) $event->getArgument('params')->enable_session_gc ?? 1;
if ($enableGC) {
$probability = (int) $event->getArgument('params')->gc_probability ?? 1;
$divisor = (int) $event->getArgument('params')->gc_divisor ?? 100;
$random = $divisor * lcg_value();
if ($probability > 0 && $random < $probability) {
$this->getApplication()->getSession()->gc();
}
}
$enableMetadata = (int) $event->getArgument('params')->enable_session_metadata_gc ?? 1;
if ($this->getApplication()->get('session_handler', 'none') !== 'database' && $enableMetadata) {
$probability = (int) $event->getArgument('params')->gc_probability ?? 1;
$divisor = (int) $event->getArgument('params')->gc_divisor ?? 100;
$random = $divisor * lcg_value();
if ($probability > 0 && $random < $probability) {
$this->metadataManager->deletePriorTo(time() - $this->getApplication()->getSession()->getExpire());
}
}
$this->logTask('SessionGC end');
return Status::OK;
}
}