[FEATURE] Add scheduler task to anonymize IP addresses of tables
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Task / IpAnonymizationAdditionalFieldProvider.php
1 <?php
2
3 namespace TYPO3\CMS\Scheduler\Task;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Lang\LanguageService;
19 use TYPO3\CMS\Scheduler\AdditionalFieldProviderInterface;
20
21 /**
22 * Additional BE fields for ip address anonymization task.
23 */
24 class IpAnonymizationAdditionalFieldProvider implements AdditionalFieldProviderInterface
25 {
26 /**
27 * @var array Default number of days by table
28 */
29 protected $defaultNumberOfDays = [];
30
31 /**
32 * Add additional fields
33 *
34 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
35 * @param AbstractTask|null $task When editing, reference to the current task. NULL when adding.
36 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
37 * @return array Array containing all the information pertaining to the additional fields
38 */
39 public function getAdditionalFields(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
40 {
41 $additionalFields = [];
42 $additionalFields['task_ipAnonymization_table'] = $this->getTableAdditionalField($taskInfo, $task, $parentObject);
43 $additionalFields['task_ipAnonymization_numberOfDays'] = $this->getNumberOfDaysAdditionalField($taskInfo, $task, $parentObject);
44 $additionalFields['task_ipAnonymization_mask'] = $this->getMaskAdditionalField($taskInfo, $task, $parentObject);
45 return $additionalFields;
46 }
47
48 /**
49 * Add a select field of available tables.
50 *
51 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
52 * @param AbstractTask|IpAnonymizationTask|null $task When editing, reference to the current task. NULL when adding.
53 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
54 * @return array Array containing all the information pertaining to the additional fields
55 */
56 protected function getTableAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
57 {
58 $tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][IpAnonymizationTask::class]['options']['tables'];
59 $options = [];
60 // Add an empty option on top if an existing task is configured
61 // with a table that can not be found in configuration anymore
62 if ($parentObject->CMD === 'edit' && !array_key_exists($task->table, $tableConfiguration)) {
63 $options[] = '<option value="" selected="selected"></option>';
64 }
65 foreach ($tableConfiguration as $tableName => $configuration) {
66 if ($parentObject->CMD === 'add' && empty($options)) {
67 // Select first table by default if adding a new task
68 $options[] = '<option value="' . $tableName . '" selected="selected">' . $tableName . '</option>';
69 } elseif ($task->table === $tableName) {
70 // Select currently selected table
71 $options[] = '<option value="' . $tableName . '" selected="selected">' . $tableName . '</option>';
72 } else {
73 $options[] = '<option value="' . $tableName . '">' . $tableName . '</option>';
74 }
75 }
76 $fieldName = 'tx_scheduler[scheduler_ipAnonymization_table]';
77 $fieldId = 'task_ipAnonymization_table';
78 $fieldHtml = [];
79 // Add table drop down html
80 $fieldHtml[] = '<select class="form-control" name="' . $fieldName . '" id="' . $fieldId . '">' . implode(LF, $options) . '</select>';
81 // Add js array for default 'number of days' values
82 $fieldHtml[] = '<script type="text/javascript">/*<![CDATA[*/<!--';
83 $fieldHtml[] = 'var defaultNumberOfDays = ' . json_encode($this->defaultNumberOfDays) . ';';
84 $fieldHtml[] = '// -->/*]]>*/</script>';
85 $fieldConfiguration = [
86 'code' => implode(LF, $fieldHtml),
87 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.ipAnonymization.table',
88 'cshKey' => '_MOD_system_txschedulerM1',
89 'cshLabel' => $fieldId
90 ];
91 return $fieldConfiguration;
92 }
93
94 /**
95 * Add an input field to get the number of days.
96 *
97 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
98 * @param AbstractTask|IpAnonymizationTask|null $task When editing, reference to the current task. NULL when adding.
99 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
100 * @return array Array containing all the information pertaining to the additional fields
101 */
102 protected function getNumberOfDaysAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
103 {
104 $fieldId = 'scheduler_ipAnonymization_numberOfDays';
105 // Initialize selected fields
106 $disabled = '';
107 if (empty($taskInfo[$fieldId])) {
108 if ($parentObject->CMD === 'add') {
109 // In case of new task, set to 180 days
110 $taskInfo[$fieldId] = 180;
111 } elseif ($parentObject->CMD === 'edit') {
112 // In case of editing the task, set to currently selected value
113 $taskInfo[$fieldId] = $task->numberOfDays;
114 if ($task->numberOfDays === 0 && !isset($this->defaultNumberOfDays[$task->table])) {
115 $disabled = ' disabled="disabled"';
116 }
117 }
118 }
119 $fieldName = 'tx_scheduler[' . $fieldId . ']';
120 $fieldHtml = '<input class="form-control" type="text" ' . 'name="' . $fieldName . '" ' . 'id="' . $fieldId . '" ' . $disabled . 'value="' . (int)$taskInfo['scheduler_ipAnonymization_numberOfDays'] . '" ' . 'size="4">';
121 $fieldConfiguration = [
122 'code' => $fieldHtml,
123 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.ipAnonymization.numberOfDays',
124 'cshKey' => '_MOD_system_txschedulerM1',
125 'cshLabel' => $fieldId
126 ];
127 return $fieldConfiguration;
128 }
129
130 /**
131 * Add an input field to get the mask.
132 *
133 * @param array $taskInfo Reference to the array containing the info used in the add/edit form
134 * @param AbstractTask|IpAnonymizationTask|null $task When editing, reference to the current task. NULL when adding.
135 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
136 * @return array Array containing all the information pertaining to the additional fields
137 */
138 protected function getMaskAdditionalField(array &$taskInfo, $task, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
139 {
140 $fieldId = 'scheduler_ipAnonymization_mask';
141 // Initialize selected fields
142 if (empty($taskInfo[$fieldId])) {
143 if ($parentObject->CMD === 'add') {
144 // In case of new task, set to 180 days
145 $taskInfo[$fieldId] = 2;
146 } elseif ($parentObject->CMD === 'edit') {
147 // In case of editing the task, set to currently selected value
148 $taskInfo[$fieldId] = $task->mask;
149 }
150 }
151 $fieldName = 'tx_scheduler[' . $fieldId . ']';
152
153 $fieldHtml = '';
154 foreach ([1, 2] as $mask) {
155 $selected = (int)$taskInfo[$fieldId] === $mask ? ' selected' : '';
156 $fieldHtml .= '<option value="' . $mask . '"' . $selected . '>'
157 . htmlspecialchars($this->getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.ipAnonymization.mask.' . $mask))
158 . '</option>';
159 }
160
161 $fieldHtml = '<select class="form-control" name="' . $fieldName . '" ' . 'id="' . $fieldId . '">' . $fieldHtml . '</select>';
162
163 $fieldConfiguration = [
164 'code' => $fieldHtml,
165 'label' => 'LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.ipAnonymization.mask',
166 'cshKey' => '_MOD_system_txschedulerM1',
167 'cshLabel' => $fieldId
168 ];
169 return $fieldConfiguration;
170 }
171
172 /**
173 * Validate additional fields
174 *
175 * @param array $submittedData Reference to the array containing the data submitted by the user
176 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
177 * @return bool True if validation was ok (or selected class is not relevant), false otherwise
178 */
179 public function validateAdditionalFields(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
180 {
181 $validData = $this->validateTableAdditionalField($submittedData, $parentObject);
182 $validData &= $this->validateNumberOfDaysAdditionalField($submittedData, $parentObject);
183 return $validData;
184 }
185
186 /**
187 * Checks given table for existence in configuration array
188 *
189 * @param array $submittedData Reference to the array containing the data submitted by the user
190 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
191 * @return bool True if table exists in configuration, false otherwise
192 */
193 public function validateTableAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
194 {
195 $validData = false;
196 $tableConfiguration = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][IpAnonymizationTask::class]['options']['tables'];
197 if (!isset($submittedData['scheduler_ipAnonymization_table'])) {
198 $validData = true;
199 } elseif (array_key_exists($submittedData['scheduler_ipAnonymization_table'], $tableConfiguration)) {
200 $validData = true;
201 }
202 return $validData;
203 }
204
205 /**
206 * Checks if given number of days is a positive integer
207 *
208 * @param array $submittedData Reference to the array containing the data submitted by the user
209 * @param \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject Reference to the calling object (Scheduler's BE module)
210 * @return bool True if validation was ok (or selected class is not relevant), false otherwise
211 */
212 public function validateNumberOfDaysAdditionalField(array &$submittedData, \TYPO3\CMS\Scheduler\Controller\SchedulerModuleController $parentObject)
213 {
214 $validData = false;
215 if (!isset($submittedData['scheduler_ipAnonymization_numberOfDays'])) {
216 $validData = true;
217 } elseif ((int)$submittedData['scheduler_ipAnonymization_numberOfDays'] >= 0) {
218 $validData = true;
219 } else {
220 // Issue error message
221 $parentObject->addMessage($this->getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:msg.invalidNumberOfDays'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
222 }
223 return $validData;
224 }
225
226 /**
227 * Save additional field in task
228 *
229 * @param array $submittedData Contains data submitted by the user
230 * @param \TYPO3\CMS\Scheduler\Task\AbstractTask $task Reference to the current task object
231 */
232 public function saveAdditionalFields(array $submittedData, \TYPO3\CMS\Scheduler\Task\AbstractTask $task)
233 {
234 $task->table = $submittedData['scheduler_ipAnonymization_table'];
235 $task->mask = $submittedData['scheduler_ipAnonymization_mask'];
236 $task->numberOfDays = (int)$submittedData['scheduler_ipAnonymization_numberOfDays'];
237 }
238
239 /**
240 * Returns an instance of LanguageService
241 *
242 * @return LanguageService
243 */
244 protected function getLanguageService(): LanguageService
245 {
246 return $GLOBALS['LANG'];
247 }
248 }