Skip to content

Commit 3025402

Browse files
authored
Merge pull request #8936 from jdarwood007/3.0/mysqlNTOA
[3.0] Implement MySQL native INET6_ATON
2 parents 6fca102 + 5417f68 commit 3025402

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

Sources/Db/APIs/MySQL.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -766,13 +766,11 @@ public function error_insert(array $error_array): void
766766
$this->connection,
767767
'INSERT INTO ' . $this->prefix . 'log_errors
768768
(id_member, log_time, ip, url, message, session, error_type, file, line, backtrace)
769-
VALUES( ?, ?, unhex(?), ?, ?, ?, ?, ?, ?, ?)',
769+
VALUES( ?, ?, INET6_ATON(?), ?, ?, ?, ?, ?, ?, ?)',
770770
);
771771
}
772772

773-
if (filter_var($error_array['ip'], FILTER_VALIDATE_IP) !== false) {
774-
$error_array['ip'] = bin2hex(inet_pton($error_array['ip']));
775-
} else {
773+
if (filter_var($error_array['ip'], FILTER_VALIDATE_IP) === false) {
776774
$error_array['ip'] = null;
777775
}
778776

@@ -2808,8 +2806,7 @@ protected function replacement__callback(array $matches, array $db_values, objec
28082806
$this->error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
28092807
}
28102808

2811-
// We don't use the native support of mysql > 5.6.2
2812-
return \sprintf('unhex(\'%1$s\')', $ip->toHex());
2809+
return \sprintf('INET6_ATON(\'%1$s\')', $ip);
28132810

28142811
case 'array_inet':
28152812
if (\is_array($replacement)) {
@@ -2828,7 +2825,7 @@ protected function replacement__callback(array $matches, array $db_values, objec
28282825
$this->error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__);
28292826
}
28302827

2831-
$replacement[$key] = \sprintf('unhex(\'%1$s\')', $ip->toHex());
2828+
$replacement[$key] = \sprintf('INET6_ATON(\'%1$s\')', $ip);
28322829
}
28332830

28342831
return implode(', ', $replacement);

Sources/Maintenance/Migration/v2_1/Ipv6BanItem.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ public function execute(): bool
8585
WHERE ip_low1 > 0',
8686
);
8787
} else {
88+
// Note, We now support MySQL 8+, which means we could use INET6_ATON.
89+
// The upgrade logic was built this way and should remain the same.
90+
// If changed, a full upgrade from 2.0 to 3.0 would need to be tested.
8891
$this->quote(
8992
'UPDATE IGNORE {db_prefix}ban_items
9093
SET ip_low =

0 commit comments

Comments
 (0)