Skip to content

Commit

Permalink
Updated SQL data for IP anonymization
Browse files Browse the repository at this point in the history
  • Loading branch information
mystralkk committed Jun 3, 2021
1 parent 6ca4a9a commit 353d00b
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 1 deletion.
5 changes: 5 additions & 0 deletions public_html/admin/install/classes/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,11 @@ protected function doDatabaseUpgrades($currentGlVersion, $checkForMessage = fals
break;

case '2.2.1sr1':
require_once $_CONF['path'] . 'sql/updates/' . $_DB_dbms . '_2.2.1_to_2.2.2.php';
$this->updateDB($_SQL, $progress);
update_ConfValuesFor222();
update_TablesContainingIPAddresses222();

$currentGlVersion = '2.2.2';
break;

Expand Down
3 changes: 2 additions & 1 deletion public_html/admin/install/config-install.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// | |
// | Initial configuration setup. |
// +---------------------------------------------------------------------------+
// | Copyright (C) 2007-2019 by the following authors: |
// | Copyright (C) 2007-2021 by the following authors: |
// | |
// | Authors: Aaron Blankstein - kantai AT gmail DOT com |
// +---------------------------------------------------------------------------+
Expand Down Expand Up @@ -70,6 +70,7 @@ function install_config(ConfigInterface $c)
$c->add('about_cookies_link','','text',0,0,NULL,2040,TRUE, $me, 0);
$c->add('terms_of_use_link','','text',0,0,NULL,2050,TRUE, $me, 0);
$c->add('privacy_policy_link','','text',0,0,NULL,2060,TRUE, $me, 0);
$c->add('ip_anonymization',\Geeklog\IP::POLICY_NEVER_ANONYMIZE,'text',0,0,NULL,2070,TRUE, $me, 0);

$c->add('tab_mail', NULL, 'tab', 0, 1, NULL, 0, TRUE, $me, 1);
$c->add('fs_mail', NULL, 'fieldset', 0, 1, NULL, 0, TRUE, $me, 1);
Expand Down
9 changes: 9 additions & 0 deletions public_html/lib-common.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@
$_PAGE_TIMER = new timerobject();
$_PAGE_TIMER->startTimer();

// Initialize IP class
if (!defined('GL_INSTALL_ACTIVE')) {
\Geeklog\IP::init($_TABLES['ip_addresses'], $_CONF['ip_anonymization']);
}

/**
* This provides optional URL rewriting functionality.
*
Expand Down Expand Up @@ -9309,6 +9314,10 @@ function COM_handleEval($code, $type = 1, $embeddedPHP = false)
if (($_VARS['last_scheduled_run'] + $_CONF['cron_schedule_interval']) <= time()) {
DB_query("UPDATE {$_TABLES['vars']} SET value=UNIX_TIMESTAMP() WHERE name='last_scheduled_run'");
PLG_runScheduledTask();

if (!defined('GL_INSTALL_ACTIVE')) {
\Geeklog\IP::updateIPAddressesTable();
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions sql/mysql_tableanddata.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@
) ENGINE=MyISAM
";

$_SQL[] = "
CREATE TABLE {$_TABLES['ip_addresses']} (
seq INT NOT NULL AUTO_INCREMENT,
ipaddress VARCHAR(39) NOT NULL DEFAULT '0.0.0.0',
created_at INT NOT NULL DEFAULT 0,
is_anonymized INT NOT NULL default 0,
PRIMARY KEY (seq)
) ENGINE=MyISAM
";

$_SQL[] ="
CREATE TABLE {$_TABLES['language_items']} (
id INT(11) NOT NULL AUTO_INCREMENT,
Expand Down
10 changes: 10 additions & 0 deletions sql/pgsql_tableanddata.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@
CREATE UNIQUE INDEX {$_TABLES['groups']}_grp_name ON {$_TABLES['groups']}(grp_name);
";

$_SQL[] = "
CREATE TABLE {$_TABLES['ip_addresses']} (
seq SERIAL,
ipaddress VARCHAR(39) NOT NULL DEFAULT '0.0.0.0',
created_at INT NOT NULL DEFAULT 0,
is_anonymized INT NOT NULL default 0,
PRIMARY KEY (seq)
)
";

$_SQL[] = "
CREATE TABLE {$_TABLES['language_items']} (
id SERIAL NOT NULL,
Expand Down
94 changes: 94 additions & 0 deletions sql/updates/mysql_2.2.1_to_2.2.2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

// *************************************
// New 'ip_addresses' table
$_SQL[] = "
CREATE TABLE {$_TABLES['ip_addresses']} (
seq INT NOT NULL AUTO_INCREMENT,
ipaddress VARCHAR(39) NOT NULL DEFAULT '0.0.0.0',
created_at INT NOT NULL DEFAULT 0,
is_anonymized INT NOT NULL default 0,
PRIMARY KEY (seq)
) ENGINE=MyISAM
";

/**
* Add/Edit/Delete config options for new version
*/
function update_ConfValuesFor222()
{
global $_CONF, $_TABLES;

require_once $_CONF['path_system'] . 'classes/config.class.php';

$c = config::get_instance();
$me = 'Core';

// Add IP anonymization policy
$c->add('ip_anonymization', \Geeklog\IP::POLICY_NEVER_ANONYMIZE, 'text', 0, 0, null, 2070, true, $me, 0);

return true;
}

/**
* Move IP addresses to the new 'ip_addresses' table
*
* @return bool
*/
function update_TablesContainingIPAddresses222()
{
global $_TABLES;

$data = [
'comments' => ['cid', 'ipaddress'],
'commentsumissions' => ['cid', 'ipaddress'],
'likes' => ['lid', 'ipaddress'],
'sessions' => ['sess_id', 'remote_ip'],
'speedlimit' => ['id', 'ipaddress'],
'trackback' => ['cid', 'ipaddress'],
];

foreach ($data as $table => $pair) {
$primaryKeyColumn = $pair[0];
$ipColumn = $pair[1];

// Add 'seq' column
DB_query("ALTER TABLE $_TABLES[$table] ADD COLUMN seq INT NOT NULL DEFAULT 0");

// Collect primary ke values and IP addresses
$result = DB_query("SELECT $primaryKeyColumn, $ipColumn FROM $_TABLES[$table]");
$rows = [];

while (($A = DB_fetchArray($result, false)) != false) {
$rows[] = $A;
}

\Geeklog\IP::init($_TABLES['ip_addresses'], \Geeklog\IP::POLICY_NEVER_ANONYMIZE);

foreach ($rows as $row) {
$primaryKeyValue = $row[$primaryKeyColumn];
$ipAddress = $row[$ipColumn];

// Move IP addresses to 'ip_addresses' table
$seq = \Geeklog\IP::getSeq($ipAddress);

// Update 'seq' column
if ($table === 'sessions') {
DB_query("UPDATE $_TABLES[$table] SET seq = $seq WHERE $primaryKeyColumn = $primaryKeyValue");
} else {
$primaryKeyValue = DB_escapeString($primaryKeyValue);
DB_query("UPDATE $_TABLES[$table] SET seq = $seq WHERE $primaryKeyColumn = '$primaryKeyValue'");
}
}

// Drop key related to IP addresses
if ($table === 'speedlimit') {
DB_query("ALTER TABLE $_TABLES[$table] DROP KEY type_ipaddress");
}

// Drop column 'ipaddress'
DB_query("ALTER TABLE $_TABLES[$table] DROP COLUMN $ipColumn");
}

return true;
}
93 changes: 93 additions & 0 deletions sql/updates/pgsql_2.2.1_to_2.2.2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

// *************************************
// New 'ip_addresses' table
$_SQL[] = "
CREATE TABLE {$_TABLES['ip_addresses']} (
seq SERIAL,
ipaddress VARCHAR(39) NOT NULL DEFAULT '0.0.0.0',
created_at INT NOT NULL DEFAULT 0,
is_anonymized INT NOT NULL default 0,
PRIMARY KEY (seq)
";

/**
* Add/Edit/Delete config options for new version
*/
function update_ConfValuesFor222()
{
global $_CONF, $_TABLES;

require_once $_CONF['path_system'] . 'classes/config.class.php';

$c = config::get_instance();
$me = 'Core';

// Add IP anonymization policy
$c->add('ip_anonymization',\Geeklog\IP::POLICY_NEVER_ANONYMIZE,'text',0,0,NULL,2070,TRUE, $me, 0);

return true;
}

/**
* Move IP addresses to the new 'ip_addresses' table
*
* @return bool
*/
function update_TablesContainingIPAddresses222()
{
global $_TABLES;

$data = [
'comments' => ['cid', 'ipaddress'],
'commentsumissions' => ['cid', 'ipaddress'],
'likes' => ['lid', 'ipaddress'],
'sessions' => ['sess_id', 'remote_ip'],
'speedlimit' => ['id', 'ipaddress'],
'trackback' => ['cid', 'ipaddress'],
];

foreach ($data as $table => $pair) {
$primaryKeyColumn = $pair[0];
$ipColumn = $pair[1];

// Add 'seq' column
DB_query("ALTER TABLE $_TABLES[$table] ADD COLUMN seq INT NOT NULL DEFAULT 0");

// Collect primary ke values and IP addresses
$result = DB_query("SELECT $primaryKeyColumn, $ipColumn FROM $_TABLES[$table]");
$rows = [];

while (($A = DB_fetchArray($result, false)) != false) {
$rows[] = $A;
}

\Geeklog\IP::init($_TABLES['ip_addresses'], \Geeklog\IP::POLICY_NEVER_ANONYMIZE);

foreach ($rows as $row) {
$primaryKeyValue = $row[$primaryKeyColumn];
$ipAddress = $row[$ipColumn];

// Move IP addresses to 'ip_addresses' table
$seq = \Geeklog\IP::getSeq($ipAddress);

// Update 'seq' column
if ($table === 'sessions') {
DB_query("UPDATE $_TABLES[$table] SET seq = $seq WHERE $primaryKeyColumn = $primaryKeyValue");
} else {
$primaryKeyValue = DB_escapeString($primaryKeyValue);
DB_query("UPDATE $_TABLES[$table] SET seq = $seq WHERE $primaryKeyColumn = '$primaryKeyValue'");
}
}

// Drop index related to IP addresses
if ($table === 'speedlimit') {
DB_query("ALTER TABLE $_TABLES[$table] DROP INDEX {$_TABLES['speedlimit']}_type_ipaddress");
}

// Drop column 'ipaddress'
DB_query("ALTER TABLE $_TABLES[$table] DROP COLUMN $ipColumn");
}

return true;
}

0 comments on commit 353d00b

Please sign in to comment.