From 8c8debca4e5cbbb4457277aa67ddc204fca01ade Mon Sep 17 00:00:00 2001 From: Herb v/d Dool Date: Tue, 14 Feb 2023 14:43:13 -0500 Subject: [PATCH] dev/core#4127 move ipAddress to userSystem --- CRM/Utils/System.php | 8 +------- CRM/Utils/System/Backdrop.php | 10 ++++++++++ CRM/Utils/System/Base.php | 10 ++++++++++ CRM/Utils/System/Drupal.php | 10 ++++++++++ CRM/Utils/System/Drupal8.php | 7 +++++++ 5 files changed, 38 insertions(+), 7 deletions(-) diff --git a/CRM/Utils/System.php b/CRM/Utils/System.php index 10c0672734..83652a6fb7 100644 --- a/CRM/Utils/System.php +++ b/CRM/Utils/System.php @@ -1285,14 +1285,8 @@ class CRM_Utils_System { * IP address of logged in user. */ public static function ipAddress($strictIPV4 = TRUE) { - $address = $_SERVER['REMOTE_ADDR'] ?? NULL; - $config = CRM_Core_Config::singleton(); - if ($config->userSystem->is_drupal && function_exists('ip_address')) { - // drupal function handles the server being behind a proxy securely. We still have legacy ipn methods - // that reach this point without bootstrapping hence the check that the fn exists - $address = ip_address(); - } + $address = $config->userSystem->ipAddress(); // hack for safari if ($address == '::1') { diff --git a/CRM/Utils/System/Backdrop.php b/CRM/Utils/System/Backdrop.php index 1742a479c9..2b60e638e7 100644 --- a/CRM/Utils/System/Backdrop.php +++ b/CRM/Utils/System/Backdrop.php @@ -1186,4 +1186,14 @@ AND u.status = 1 } } + /** + * @inheritdoc + */ + public function ipAddress():?string { + // Backdrop function handles the server being behind a proxy securely. We + // still have legacy ipn methods that reach this point without bootstrapping + // hence the check that the fn exists. + return function_exists('ip_address') ? ip_address() : ($_SERVER['REMOTE_ADDR'] ?? NULL); + } + } diff --git a/CRM/Utils/System/Base.php b/CRM/Utils/System/Base.php index 0b88f721b2..3a69bc69e6 100644 --- a/CRM/Utils/System/Base.php +++ b/CRM/Utils/System/Base.php @@ -1142,4 +1142,14 @@ abstract class CRM_Utils_System_Base { return FALSE; } + /** + * Get the client's IP address. + * + * @return string + * IP address + */ + public function ipAddress():?string { + return $_SERVER['REMOTE_ADDR'] ?? NULL; + } + } diff --git a/CRM/Utils/System/Drupal.php b/CRM/Utils/System/Drupal.php index 9371875577..a645c8b722 100644 --- a/CRM/Utils/System/Drupal.php +++ b/CRM/Utils/System/Drupal.php @@ -921,4 +921,14 @@ AND u.status = 1 } } + /** + * @inheritdoc + */ + public function ipAddress():?string { + // Drupal function handles the server being behind a proxy securely. We + // still have legacy ipn methods that reach this point without bootstrapping + // hence the check that the fn exists. + return function_exists('ip_address') ? ip_address() : ($_SERVER['REMOTE_ADDR'] ?? NULL); + } + } diff --git a/CRM/Utils/System/Drupal8.php b/CRM/Utils/System/Drupal8.php index d5a6034015..c4d06a5a2c 100644 --- a/CRM/Utils/System/Drupal8.php +++ b/CRM/Utils/System/Drupal8.php @@ -925,4 +925,11 @@ class CRM_Utils_System_Drupal8 extends CRM_Utils_System_DrupalBase { return NULL; } + /** + * @inheritdoc + */ + public function ipAddress():?string { + return class_exists('Drupal') ? \Drupal::request()->getClientIp() : ($_SERVER['REMOTE_ADDR'] ?? NULL); + } + } -- 2.25.1