@@ -22,15 +22,15 @@ public function __construct(Router $router, Responder $responder, array $propert
22
22
$ this ->reflection = $ reflection ;
23
23
}
24
24
25
- private function callHandler ($ record , string $ operation , ReflectedTable $ table ) /*: object */
25
+ private function callHandler (ServerRequestInterface $ request , $ record , string $ operation , ReflectedTable $ table ) /*: object */
26
26
{
27
27
$ context = (array ) $ record ;
28
28
$ columnNames = $ this ->getProperty ('columns ' , '' );
29
29
if ($ columnNames ) {
30
30
foreach (explode (', ' , $ columnNames ) as $ columnName ) {
31
31
if ($ table ->hasColumn ($ columnName )) {
32
32
if ($ operation == 'create ' ) {
33
- $ context [$ columnName ] = $ _SERVER [ ' REMOTE_ADDR ' ] ;
33
+ $ context [$ columnName ] = $ this -> getIpAddress ( $ request ) ;
34
34
} else {
35
35
unset($ context [$ columnName ]);
36
36
}
@@ -40,6 +40,18 @@ private function callHandler($record, string $operation, ReflectedTable $table)
40
40
return (object ) $ context ;
41
41
}
42
42
43
+ private function getIpAddress (ServerRequestInterface $ request ): string
44
+ {
45
+ $ reverseProxy = $ this ->getProperty ('reverseProxy ' , '' );
46
+ if ($ reverseProxy ) {
47
+ $ ipAddress = array_pop ($ request ->getHeader ('X-Forwarded-For ' ));
48
+ } else {
49
+ $ serverParams = $ request ->getServerParams ();
50
+ $ ipAddress = $ serverParams ['REMOTE_ADDR ' ] ?? '127.0.0.1 ' ;
51
+ }
52
+ return $ ipAddress ;
53
+ }
54
+
43
55
public function process (ServerRequestInterface $ request , RequestHandlerInterface $ next ): ResponseInterface
44
56
{
45
57
$ operation = RequestUtils::getOperation ($ request );
@@ -53,10 +65,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
53
65
$ table = $ this ->reflection ->getTable ($ tableName );
54
66
if (is_array ($ record )) {
55
67
foreach ($ record as &$ r ) {
56
- $ r = $ this ->callHandler ($ r , $ operation , $ table );
68
+ $ r = $ this ->callHandler ($ request , $ r , $ operation , $ table );
57
69
}
58
70
} else {
59
- $ record = $ this ->callHandler ($ record , $ operation , $ table );
71
+ $ record = $ this ->callHandler ($ request , $ record , $ operation , $ table );
60
72
}
61
73
$ request = $ request ->withParsedBody ($ record );
62
74
}
0 commit comments