Skip to content

Commit

Permalink
FIX: Fixes #92 by using correct IP address prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
phptek committed Jan 16, 2024
1 parent a0c1640 commit 4b0ad0f
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/Log/SentryLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Class: SentryLogger.
*
* @author Russell Michell 2017-2021 <russ@theruss.com>
* @author Russell Michell 2017-2024 <russ@theruss.com>
* @package phptek/sentry
*/

Expand All @@ -14,15 +14,12 @@
use Sentry\Frame;
use Sentry\Client;
use SilverStripe\Control\Director;
use SilverStripe\Control\Middleware\TrustedProxyMiddleware;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\Backtrace;
use SilverStripe\Security\Member;
use SilverStripe\Security\Security;
use SilverStripe\Core\Config\Configurable;
use PhpTek\Sentry\Adaptor\SentryAdaptor;
use SilverStripe\Control\Controller;
use SilverStripe\Core\Environment;

/**
* The SentryLogger class is a bridge between {@link SentryAdaptor} and
Expand All @@ -32,6 +29,11 @@ class SentryLogger
{
use Configurable;

/**
* @var string
*/
public const DEFAULT_IP = '0.0.0.0';

/**
* @var SentryAdaptor
*/
Expand Down Expand Up @@ -252,22 +254,23 @@ public static function get_app_info(): string
* Returns the client IP address which originated this request.
* Lifted and modified from SilverStripe 3's SS_HTTPRequest.
*
* @return string
* @return mixed null|string
*/
public static function get_ip(): string
public static function get_ip(): ?string
{
if (Controller::has_curr()) {
$controller = Controller::curr();

if ($request = $controller->getRequest()) {
return $request->getIP() ?? '';
return $request->getIP();
}
}

if (isset($_SERVER['REMOTE_ADDR'])) {
return $_SERVER['REMOTE_ADDR'];
}

return '';
return null;
}

/**
Expand All @@ -284,7 +287,7 @@ public static function user_data(Member $member = null): array
}

return [
'ip_address' => self::get_ip() ?: self::SLW_NOOP,
'ip_address' => self::get_ip() ?? self::DEFAULT_IP,
'id' => $member ? $member->getField('ID') : self::SLW_NOOP,
'email' => $member ? $member->getField('Email') : self::SLW_NOOP,
];
Expand Down

0 comments on commit 4b0ad0f

Please sign in to comment.