Skip to content

Commit 105792a

Browse files
committed
use PSR-7 for IP address middleware
1 parent 8198c22 commit 105792a

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

api.include.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7981,15 +7981,15 @@ public function __construct(Router $router, Responder $responder, array $propert
79817981
$this->reflection = $reflection;
79827982
}
79837983

7984-
private function callHandler($record, string $operation, ReflectedTable $table) /*: object */
7984+
private function callHandler(ServerRequestInterface $request, $record, string $operation, ReflectedTable $table) /*: object */
79857985
{
79867986
$context = (array) $record;
79877987
$columnNames = $this->getProperty('columns', '');
79887988
if ($columnNames) {
79897989
foreach (explode(',', $columnNames) as $columnName) {
79907990
if ($table->hasColumn($columnName)) {
79917991
if ($operation == 'create') {
7992-
$context[$columnName] = $_SERVER['REMOTE_ADDR'];
7992+
$context[$columnName] = $this->getIpAddress($request);
79937993
} else {
79947994
unset($context[$columnName]);
79957995
}
@@ -7999,6 +7999,18 @@ private function callHandler($record, string $operation, ReflectedTable $table)
79997999
return (object) $context;
80008000
}
80018001

8002+
private function getIpAddress(ServerRequestInterface $request): string
8003+
{
8004+
$reverseProxy = $this->getProperty('reverseProxy', '');
8005+
if ($reverseProxy) {
8006+
$ipAddress = array_pop($request->getHeader('X-Forwarded-For'));
8007+
} else {
8008+
$serverParams = $request->getServerParams();
8009+
$ipAddress = $serverParams['REMOTE_ADDR'] ?? '127.0.0.1';
8010+
}
8011+
return $ipAddress;
8012+
}
8013+
80028014
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
80038015
{
80048016
$operation = RequestUtils::getOperation($request);
@@ -8012,10 +8024,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
80128024
$table = $this->reflection->getTable($tableName);
80138025
if (is_array($record)) {
80148026
foreach ($record as &$r) {
8015-
$r = $this->callHandler($r, $operation, $table);
8027+
$r = $this->callHandler($request, $r, $operation, $table);
80168028
}
80178029
} else {
8018-
$record = $this->callHandler($record, $operation, $table);
8030+
$record = $this->callHandler($request, $record, $operation, $table);
80198031
}
80208032
$request = $request->withParsedBody($record);
80218033
}

api.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7981,15 +7981,15 @@ public function __construct(Router $router, Responder $responder, array $propert
79817981
$this->reflection = $reflection;
79827982
}
79837983

7984-
private function callHandler($record, string $operation, ReflectedTable $table) /*: object */
7984+
private function callHandler(ServerRequestInterface $request, $record, string $operation, ReflectedTable $table) /*: object */
79857985
{
79867986
$context = (array) $record;
79877987
$columnNames = $this->getProperty('columns', '');
79887988
if ($columnNames) {
79897989
foreach (explode(',', $columnNames) as $columnName) {
79907990
if ($table->hasColumn($columnName)) {
79917991
if ($operation == 'create') {
7992-
$context[$columnName] = $_SERVER['REMOTE_ADDR'];
7992+
$context[$columnName] = $this->getIpAddress($request);
79937993
} else {
79947994
unset($context[$columnName]);
79957995
}
@@ -7999,6 +7999,18 @@ private function callHandler($record, string $operation, ReflectedTable $table)
79997999
return (object) $context;
80008000
}
80018001

8002+
private function getIpAddress(ServerRequestInterface $request): string
8003+
{
8004+
$reverseProxy = $this->getProperty('reverseProxy', '');
8005+
if ($reverseProxy) {
8006+
$ipAddress = array_pop($request->getHeader('X-Forwarded-For'));
8007+
} else {
8008+
$serverParams = $request->getServerParams();
8009+
$ipAddress = $serverParams['REMOTE_ADDR'] ?? '127.0.0.1';
8010+
}
8011+
return $ipAddress;
8012+
}
8013+
80028014
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
80038015
{
80048016
$operation = RequestUtils::getOperation($request);
@@ -8012,10 +8024,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
80128024
$table = $this->reflection->getTable($tableName);
80138025
if (is_array($record)) {
80148026
foreach ($record as &$r) {
8015-
$r = $this->callHandler($r, $operation, $table);
8027+
$r = $this->callHandler($request, $r, $operation, $table);
80168028
}
80178029
} else {
8018-
$record = $this->callHandler($record, $operation, $table);
8030+
$record = $this->callHandler($request, $record, $operation, $table);
80198031
}
80208032
$request = $request->withParsedBody($record);
80218033
}

0 commit comments

Comments
 (0)