TYPO3 CMS  TYPO3_7-6
IpAnonymizationTask.php
Go to the documentation of this file.
1 <?php
2 
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 
20 
31 {
32 
36  public $numberOfDays = 180;
37 
41  public $mask = 2;
42 
46  public $table = '';
47 
54  public function execute()
55  {
56  $configuration = isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][self::class]['options']['tables'][$this->table])
57  ? $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][self::class]['options']['tables'][$this->table]
58  : [];
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  $database = $this->getDatabase();
77  if (empty($configuration['dateField'])) {
78  throw new \RuntimeException(self::class . ' misconfiguration: "dateField" must be defined for table ' . $table, 1524502549);
79  }
80  if (empty($configuration['ipField'])) {
81  throw new \RuntimeException(self::class . ' misconfiguration: "ipField" must be defined for table ' . $table, 1524502666);
82  }
83  $deleteTimestamp = strtotime('-' . $this->numberOfDays . 'days');
84  if ($deleteTimestamp === false) {
85  throw new \RuntimeException(self::class . ' misconfiguration: number of days could not be calculated for table ' . $table, 1524526354);
86  }
87  $where = $configuration['dateField'] . ' < ' . (int)$deleteTimestamp
88  . ' AND ' . $configuration['ipField'] . ' <> \'\''
89  . ' AND ' . $configuration['ipField'] . ' is not null'
90  . ' AND ' . $configuration['ipField'] . ' not like \'%.0.0\''
91  . ' AND ' . $configuration['ipField'] . ' not like \'%::\'';
92  $res = $database->exec_SELECTquery('uid,' . $configuration['ipField'], $table, $where);
93  if (!$res) {
94  throw new \RuntimeException(self::class . ' failed for table ' . $this->table . ' with error: ' . $database->sql_error(), 1524502550);
95  }
96  while ($row = $database->sql_fetch_assoc($res)) {
97  $ip = (string)$row[$configuration['ipField']];
98  $database->exec_UPDATEquery($table, 'uid = ' . (int)$row['uid'], [
99  $configuration['ipField'] => IpAnonymizationUtility::anonymizeIp($ip, $this->mask)
100  ]);
101  }
102  $database->sql_free_result($res);
103  return true;
104  }
105 
111  public function getAdditionalInformation()
112  {
113  return sprintf($GLOBALS['LANG']->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.ipAnonymization.additionalInformationTable'), $this->table, $this->numberOfDays);
114  }
115 
119  private function getDatabase()
120  {
121  return $GLOBALS['TYPO3_DB'];
122  }
123 }
$database
Definition: server.php:40
static static static anonymizeIp($address, $mask=null)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']