Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/Helpers/NetworkHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* -------------------------------------------------------------------------
* advancedforms plugin for GLPI
* -------------------------------------------------------------------------
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2025 by the advancedforms plugin team.
* @license MIT https://opensource.org/licenses/mit-license.php
* @link https://github.com/pluginsGLPI/advancedforms
* -------------------------------------------------------------------------
*/

namespace GlpiPlugin\Advancedforms\Helpers;

final class NetworkHelper
{
public static function getRemoteIpAddress(): string
{
// Check X-Forwarded-For header first (proxy/load balancer)
$forwarded_for = getenv("HTTP_X_FORWARDED_FOR");
if (is_string($forwarded_for) && ($forwarded_for !== '')) {
// May contain multiple IPs separated by ", " - get the first one
$ips = explode(',', $forwarded_for);
return trim($ips[0]);
}

// Fallback to REMOTE_ADDR
$remote_addr = getenv("REMOTE_ADDR");
if (is_string($remote_addr) && ($remote_addr !== '')) {
return $remote_addr;
}

// Last fallback to $_SERVER
if (isset($_SERVER['REMOTE_ADDR']) && is_string($_SERVER['REMOTE_ADDR'])) {
return $_SERVER['REMOTE_ADDR'];
}

return '';
}
}
4 changes: 2 additions & 2 deletions src/Model/QuestionType/HostnameQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
use Glpi\Form\Question;
use Glpi\Form\QuestionType\AbstractQuestionType;
use Glpi\Form\QuestionType\QuestionTypeCategoryInterface;
use GlpiPlugin\Advancedforms\Helpers\NetworkHelper;
use GlpiPlugin\Advancedforms\Model\Config\ConfigurableItemInterface;
use GlpiPlugin\Advancedforms\Model\Mapper\FormcreatorHostnameTypeMapper;
use Override;
use Toolbox;

/**
* Legacy question type from the formcreator plugin
Expand Down Expand Up @@ -108,7 +108,7 @@ public function renderEndUserTemplate(Question|null $question): string
$twig = TemplateRenderer::getInstance();
return $twig->renderFromStringTemplate($template, [
'question' => $question,
'hostname' => gethostbyaddr(Toolbox::getRemoteIpAddress()),
'hostname' => gethostbyaddr(NetworkHelper::getRemoteIpAddress()),
]);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Model/QuestionType/IpAddressQuestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
use Glpi\Form\Question;
use Glpi\Form\QuestionType\AbstractQuestionType;
use Glpi\Form\QuestionType\QuestionTypeCategoryInterface;
use GlpiPlugin\Advancedforms\Helpers\NetworkHelper;
use GlpiPlugin\Advancedforms\Model\Config\ConfigurableItemInterface;
use GlpiPlugin\Advancedforms\Model\Mapper\FormcreatorIpTypeMapper;
use Override;
use Toolbox;

/**
* Legacy question type from the formcreator plugin
Expand Down Expand Up @@ -108,7 +108,7 @@ public function renderEndUserTemplate(Question|null $question): string
$twig = TemplateRenderer::getInstance();
return $twig->renderFromStringTemplate($template, [
'question' => $question,
'ip' => Toolbox::getRemoteIpAddress(),
'ip' => NetworkHelper::getRemoteIpAddress(),
]);
}

Expand Down
157 changes: 157 additions & 0 deletions tests/Helpers/NetworkHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

/**
* -------------------------------------------------------------------------
* advancedforms plugin for GLPI
* -------------------------------------------------------------------------
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2025 by the advancedforms plugin team.
* @license MIT https://opensource.org/licenses/mit-license.php
* @link https://github.com/pluginsGLPI/advancedforms
* -------------------------------------------------------------------------
*/

namespace GlpiPlugin\Advancedforms\Tests\Helpers;

use GlpiPlugin\Advancedforms\Helpers\NetworkHelper;
use GlpiPlugin\Advancedforms\Tests\AdvancedFormsTestCase;
use PHPUnit\Framework\Attributes\DataProvider;

final class NetworkHelperTest extends AdvancedFormsTestCase
{
public static function getRemoteIpAddressProvider(): \Generator
{
// Test X-Forwarded-For header with single IP (highest priority)
yield 'X-Forwarded-For with single IP' => [
'http_x_forwarded_for' => '192.168.1.100',
'remote_addr_env' => '10.0.0.1',
'remote_addr_server' => '10.0.0.1',
'expected' => '192.168.1.100',
];

// Test X-Forwarded-For header with multiple IPs (should use first one)
yield 'X-Forwarded-For with multiple IPs' => [
'http_x_forwarded_for' => '192.168.1.100, 10.0.0.5, 172.16.0.1',
'remote_addr_env' => '10.0.0.1',
'remote_addr_server' => '10.0.0.1',
'expected' => '192.168.1.100',
];

// Test X-Forwarded-For header with spaces
yield 'X-Forwarded-For with spaces' => [
'http_x_forwarded_for' => ' 192.168.1.200 ',
'remote_addr_env' => '10.0.0.1',
'remote_addr_server' => '10.0.0.1',
'expected' => '192.168.1.200',
];

// Test fallback to REMOTE_ADDR from getenv
yield 'Fallback to REMOTE_ADDR from getenv' => [
'http_x_forwarded_for' => '',
'remote_addr_env' => '123.123.123.123',
'remote_addr_server' => '10.0.0.1',
'expected' => '123.123.123.123',
];

// Test fallback to $_SERVER['REMOTE_ADDR']
yield 'Fallback to $_SERVER REMOTE_ADDR' => [
'http_x_forwarded_for' => '',
'remote_addr_env' => '',
'remote_addr_server' => '99.99.99.99',
'expected' => '99.99.99.99',
];

// Test no IP available at all
yield 'No IP available' => [
'http_x_forwarded_for' => '',
'remote_addr_env' => '',
'remote_addr_server' => null,
'expected' => '',
];

// Test IPv6 address in X-Forwarded-For
yield 'IPv6 in X-Forwarded-For' => [
'http_x_forwarded_for' => '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
'remote_addr_env' => '10.0.0.1',
'remote_addr_server' => '10.0.0.1',
'expected' => '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
];

// Test IPv6 address in REMOTE_ADDR
yield 'IPv6 in REMOTE_ADDR' => [
'http_x_forwarded_for' => '',
'remote_addr_env' => '2001:0db8:85a3::8a2e:0370:7334',
'remote_addr_server' => '10.0.0.1',
'expected' => '2001:0db8:85a3::8a2e:0370:7334',
];

// Test X-Forwarded-For with comma separated IPs without spaces
yield 'X-Forwarded-For comma separated without spaces' => [
'http_x_forwarded_for' => '192.168.1.50,10.0.0.5',
'remote_addr_env' => '10.0.0.1',
'remote_addr_server' => '10.0.0.1',
'expected' => '192.168.1.50',
];
}

#[DataProvider('getRemoteIpAddressProvider')]
public function testGetIPAddress(
string $http_x_forwarded_for,
string $remote_addr_env,
?string $remote_addr_server,
string $expected,
): void {
// Save values
$saveServer = $_SERVER;

// Setup environment
if ($http_x_forwarded_for !== '') {
putenv("HTTP_X_FORWARDED_FOR={$http_x_forwarded_for}");
} else {
putenv("HTTP_X_FORWARDED_FOR=");
}

if ($remote_addr_env !== '') {
putenv("REMOTE_ADDR={$remote_addr_env}");
} else {
putenv("REMOTE_ADDR=");
}

if ($remote_addr_server !== null) {
$_SERVER['REMOTE_ADDR'] = $remote_addr_server;
} else {
unset($_SERVER['REMOTE_ADDR']);
}

// Test
$ip = NetworkHelper::getRemoteIpAddress();
$this->assertEquals($expected, $ip);

// Clean up environment variables
putenv("HTTP_X_FORWARDED_FOR");
putenv("REMOTE_ADDR");

// Restore values
$_SERVER = $saveServer;
}
}