‪TYPO3CMS  10.4
IpAnonymizationTask.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
23 
35 {
36 
40  public ‪$numberOfDays = 180;
41 
45  public ‪$mask = 2;
46 
50  public ‪$table = '';
51 
58  public function ‪execute()
59  {
60  $configuration = ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][self::class]['options']['tables'][‪$this->table] ?? [];
61  if (empty($configuration)) {
62  throw new \RuntimeException(self::class . ' misconfiguration: ' . $this->table . ' does not exist in configuration', 1524502548);
63  }
64  $this->‪handleTable($this->table, $configuration);
65  return true;
66  }
67 
76  protected function ‪handleTable(‪$table, array $configuration)
77  {
78  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable(‪$table);
79  $queryBuilder = $connection->createQueryBuilder();
80  $queryBuilder->getRestrictions()->removeAll();
81 
82  if (empty($configuration['dateField'])) {
83  throw new \RuntimeException(self::class . ' misconfiguration: "dateField" must be defined for table ' . ‪$table, 1524502549);
84  }
85  if (empty($configuration['ipField'])) {
86  throw new \RuntimeException(self::class . ' misconfiguration: "ipField" must be defined for table ' . ‪$table, 1524502666);
87  }
88  $deleteTimestamp = strtotime('-' . $this->numberOfDays . 'days');
89  if ($deleteTimestamp === false) {
90  throw new \RuntimeException(self::class . ' misconfiguration: number of days could not be calculated for table ' . ‪$table, 1524526354);
91  }
92  if ($this->mask === 2) {
93  $notLikeMaskPattern = '%.0.0';
94  } else {
95  $notLikeMaskPattern = '%.0';
96  }
97  try {
98  $result = $queryBuilder
99  ->select('uid', $configuration['ipField'])
100  ->where(
101  $queryBuilder->expr()->lt(
102  $configuration['dateField'],
103  $queryBuilder->createNamedParameter($deleteTimestamp, \PDO::PARAM_INT)
104  ),
105  $queryBuilder->expr()->neq(
106  $configuration['ipField'],
107  $queryBuilder->createNamedParameter('', \PDO::PARAM_STR)
108  ),
109  $queryBuilder->expr()->isNotNull($configuration['ipField']),
110  $queryBuilder->expr()->notLike(
111  $configuration['ipField'],
112  $queryBuilder->createNamedParameter($notLikeMaskPattern, \PDO::PARAM_STR)
113  ),
114  $queryBuilder->expr()->notLike(
115  $configuration['ipField'],
116  $queryBuilder->createNamedParameter('%::', \PDO::PARAM_STR)
117  )
118  )
119  ->from(‪$table)
120  ->execute();
121 
122  while ($row = $result->fetch()) {
123  $ip = (string)$row[$configuration['ipField']];
124 
125  $connection->update(
126  ‪$table,
127  [
128  $configuration['ipField'] => ‪IpAnonymizationUtility::anonymizeIp($ip, (int)$this->mask)
129  ],
130  [
131  'uid' => $row['uid']
132  ]
133  );
134  }
135  } catch (\Exception $e) {
136  throw new \RuntimeException(self::class . ' failed for table ' . $this->table . ' with error: ' . $e->getMessage(), 1524502550);
137  }
138  return true;
139  }
140 
146  public function ‪getAdditionalInformation()
147  {
148  return sprintf($this->‪getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.ipAnonymization.additionalInformationTable'), $this->table, $this->numberOfDays);
149  }
150 }
‪TYPO3\CMS\Scheduler\Task\IpAnonymizationTask\execute
‪bool execute()
Definition: IpAnonymizationTask.php:55
‪TYPO3\CMS\Scheduler\Task\IpAnonymizationTask
Definition: IpAnonymizationTask.php:35
‪TYPO3\CMS\Scheduler\Task
Definition: AbstractTask.php:16
‪TYPO3\CMS\Scheduler\Task\IpAnonymizationTask\$mask
‪int $mask
Definition: IpAnonymizationTask.php:43
‪TYPO3\CMS\Scheduler\Task\IpAnonymizationTask\getAdditionalInformation
‪string getAdditionalInformation()
Definition: IpAnonymizationTask.php:143
‪TYPO3\CMS\Core\Utility\IpAnonymizationUtility
Definition: IpAnonymizationUtility.php:26
‪TYPO3\CMS\Scheduler\Task\IpAnonymizationTask\$numberOfDays
‪int $numberOfDays
Definition: IpAnonymizationTask.php:39
‪TYPO3\CMS\Scheduler\Task\AbstractTask\getLanguageService
‪LanguageService null getLanguageService()
Definition: AbstractTask.php:605
‪TYPO3\CMS\Scheduler\Task\AbstractTask
Definition: AbstractTask.php:35
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Scheduler\Task\IpAnonymizationTask\handleTable
‪bool handleTable($table, array $configuration)
Definition: IpAnonymizationTask.php:73
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\IpAnonymizationUtility\anonymizeIp
‪static string anonymizeIp(string $address, int $mask=null)
Definition: IpAnonymizationUtility.php:62
‪TYPO3\CMS\Scheduler\Task\IpAnonymizationTask\$table
‪string $table
Definition: IpAnonymizationTask.php:47