diff --git a/public/auth.php b/public/auth.php index 328e7473..aeef377f 100644 --- a/public/auth.php +++ b/public/auth.php @@ -169,7 +169,7 @@ switch ($authMode) { break; } - $ipAddress = IPAddress::remote()->getString(); + $ipAddress = remote_address(); $emailSent = Database::prepare(' SELECT COUNT(`verification_code`) > 0 FROM `msz_users_password_resets` @@ -235,7 +235,7 @@ MSG; $authLoginError = ''; while ($isSubmission) { - $ipAddress = IPAddress::remote()->getString(); + $ipAddress = remote_address(); if (!isset($authUsername, $authPassword)) { $authLoginError = "You didn't fill all the forms!"; @@ -343,7 +343,7 @@ MSG; $authUsername, $authPassword, $authEmail, - IPAddress::remote()->getString() + remote_address() ); if ($createUser < 1) { diff --git a/public/forum/posting.php b/public/forum/posting.php index dcc1e6d3..f01d423c 100644 --- a/public/forum/posting.php +++ b/public/forum/posting.php @@ -1,6 +1,5 @@ getUserId(), - IPAddress::remote()->getString(), + remote_address(), $postText, MSZ_PARSER_BBCODE ); diff --git a/src/Net/IPAddress.php b/src/Net/IPAddress.php index 58ed8f8f..0d2de356 100644 --- a/src/Net/IPAddress.php +++ b/src/Net/IPAddress.php @@ -141,7 +141,7 @@ final class IPAddress public static function remote(string $fallbackAddress = self::FALLBACK_ADDRESS): IPAddress { try { - return self::fromString($_SERVER['REMOTE_ADDR'] ?? $fallbackAddress); + return self::fromString(remote_address($fallbackAddress)); } catch (InvalidArgumentException $ex) { return self::fromString($fallbackAddress); } diff --git a/src/Users/user.php b/src/Users/user.php index 0251b5d5..45f2cf4d 100644 --- a/src/Users/user.php +++ b/src/Users/user.php @@ -77,7 +77,7 @@ function user_bump_last_active(int $userId, string $ipAddress = null): void `last_ip` = INET6_ATON(:last_ip) WHERE `user_id` = :user_id '); - $bumpUserLast->bindValue('last_ip', $ipAddress ?? $_SERVER['REMOTE_ADDR'] ?? '::1'); + $bumpUserLast->bindValue('last_ip', $ipAddress ?? remote_address()); $bumpUserLast->bindValue('user_id', $userId); $bumpUserLast->execute(); } diff --git a/src/audit_log.php b/src/audit_log.php index 9a7974a4..a8a2c62c 100644 --- a/src/audit_log.php +++ b/src/audit_log.php @@ -6,9 +6,9 @@ function audit_log( string $action, int $userId = 0, array $params = [], - IPAddress $ipAddress = null + ?string $ipAddress = null ): void { - $ipAddress = $ipAddress ?? IPAddress::remote(); + $ipAddress = $ipAddress ?? remote_address(); for ($i = 0; $i < count($params); $i++) { if (preg_match('#^(-?[0-9]+)$#', $params[$i])) { @@ -20,13 +20,13 @@ function audit_log( INSERT INTO `msz_audit_log` (`log_action`, `user_id`, `log_params`, `log_ip`, `log_country`) VALUES - (:action, :user, :params, :ip, :country) + (:action, :user, :params, INET6_ATON(:ip), :country) '); $addLog->bindValue('action', $action); $addLog->bindValue('user', $userId < 1 ? null : $userId); $addLog->bindValue('params', json_encode($params)); - $addLog->bindValue('ip', $ipAddress->getRaw()); - $addLog->bindValue('country', $ipAddress->getCountryCode()); + $addLog->bindValue('ip', $ipAddress); + $addLog->bindValue('country', get_country_code($ipAddress)); $addLog->execute(); } diff --git a/utility.php b/utility.php index 0a31cfa0..f5fd1260 100644 --- a/utility.php +++ b/utility.php @@ -29,6 +29,11 @@ function array_apply(array $array, callable $func): array return $array; } +function remote_address(string $fallback = '::1'): string +{ + return $_SERVER['REMOTE_ADDR'] ?? $fallback; +} + function set_cookie_m(string $name, string $value, int $expires): void { setcookie(