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