From 88aa17980a02dcf4f8cb151c29c5e3ed0a94a9ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20V=C3=ADger?= Date: Tue, 21 Apr 2026 23:23:27 +0200 Subject: [PATCH] support ENV vars for all input fields: - ADMINER_DEFAULT_DRIVER - ADMINER_DEFAULT_SERVER - ADMINER_DEFAULT_USERNAME - ADMINER_DEFAULT_PASSWORD - ADMINER_DEFAULT_DB --- 4/fastcgi/index.php | 33 ++++++++++++++++++++++++++++++++- 4/index.php | 33 ++++++++++++++++++++++++++++++++- 5/fastcgi/index.php | 41 ++++++++++++++++++++++++++++++----------- 5/index.php | 41 ++++++++++++++++++++++++++++++----------- 4 files changed, 124 insertions(+), 24 deletions(-) diff --git a/4/fastcgi/index.php b/4/fastcgi/index.php index a48629f..386ea6d 100644 --- a/4/fastcgi/index.php +++ b/4/fastcgi/index.php @@ -10,7 +10,38 @@ function _callParent($function, $args) { $return = \Adminer::loginForm(); $form = ob_get_clean(); - echo str_replace('name="auth[server]" value="" title="hostname[:port]"', 'name="auth[server]" value="'.($_ENV['ADMINER_DEFAULT_SERVER'] ?: 'db').'" title="hostname[:port]"', $form); + if (!empty($_ENV['ADMINER_DEFAULT_DRIVER'])) { + $form = \preg_replace( + '#', $_ENV['ADMINER_DEFAULT_DRIVER']), + $form, + ); + } + + if (!empty($_ENV['ADMINER_DEFAULT_PASSWORD'])) { + $form = \preg_replace( + '#name="auth\[password\]" (value="([^"]*)" )?#', + \sprintf('name="auth[password]" value="%s" ', $_ENV['ADMINER_DEFAULT_PASSWORD']), + $form, + ); + } + + $form = \preg_replace_callback( + '/name="auth\[(server|username|db)\]" (id="[^"]*" )?(autofocus )?value="([^"]*)"/', + static function (array $matches): string { + $defaultValues = ['server' => 'db']; + return \sprintf( + 'name="auth[%s]" %s%svalue="%s"', + $matches[1], + $matches[2] ?: '', + $matches[3] ?: '', + $_ENV['ADMINER_DEFAULT_' . \strtoupper($matches[1])] ?? ($matches[4] ?: ($defaultValues[$matches[1]] ?? '')) + ); + }, + $form, + ); + + echo $form; return $return; } diff --git a/4/index.php b/4/index.php index a48629f..386ea6d 100644 --- a/4/index.php +++ b/4/index.php @@ -10,7 +10,38 @@ function _callParent($function, $args) { $return = \Adminer::loginForm(); $form = ob_get_clean(); - echo str_replace('name="auth[server]" value="" title="hostname[:port]"', 'name="auth[server]" value="'.($_ENV['ADMINER_DEFAULT_SERVER'] ?: 'db').'" title="hostname[:port]"', $form); + if (!empty($_ENV['ADMINER_DEFAULT_DRIVER'])) { + $form = \preg_replace( + '#', $_ENV['ADMINER_DEFAULT_DRIVER']), + $form, + ); + } + + if (!empty($_ENV['ADMINER_DEFAULT_PASSWORD'])) { + $form = \preg_replace( + '#name="auth\[password\]" (value="([^"]*)" )?#', + \sprintf('name="auth[password]" value="%s" ', $_ENV['ADMINER_DEFAULT_PASSWORD']), + $form, + ); + } + + $form = \preg_replace_callback( + '/name="auth\[(server|username|db)\]" (id="[^"]*" )?(autofocus )?value="([^"]*)"/', + static function (array $matches): string { + $defaultValues = ['server' => 'db']; + return \sprintf( + 'name="auth[%s]" %s%svalue="%s"', + $matches[1], + $matches[2] ?: '', + $matches[3] ?: '', + $_ENV['ADMINER_DEFAULT_' . \strtoupper($matches[1])] ?? ($matches[4] ?: ($defaultValues[$matches[1]] ?? '')) + ); + }, + $form, + ); + + echo $form; return $return; } diff --git a/5/fastcgi/index.php b/5/fastcgi/index.php index 934da37..d9e873f 100644 --- a/5/fastcgi/index.php +++ b/5/fastcgi/index.php @@ -2,9 +2,9 @@ namespace docker { function adminer_object() { /** - * Prefills the “Server” field with the ADMINER_DEFAULT_SERVER environment variable. + * Prefills login fields with the corresponding ADMINER_DEFAULT_* environment variables. */ - final class DefaultServerPlugin extends \Adminer\Plugin { + final class EnvLoginFormFieldsPlugin extends \Adminer\Plugin { public function __construct( private \Adminer\Adminer $adminer ) { } @@ -12,14 +12,33 @@ public function __construct( public function loginFormField(...$args): string { return (function (...$args): string { $field = $this->loginFormField(...$args); - + + if (!empty($_ENV['ADMINER_DEFAULT_DRIVER']) && \str_contains($field, '#', + \sprintf('', $_ENV['ADMINER_DEFAULT_DRIVER']), + $field, + ); + } + + if (!empty($_ENV['ADMINER_DEFAULT_PASSWORD']) && \str_contains($field, 'auth[password]')) { + return \preg_replace( + '/name="auth\[password\]" (value="([^"]*)" )?/', + \sprintf('name="auth[password]" value="%s" ', $_ENV['ADMINER_DEFAULT_PASSWORD']), + $field, + ); + } + return \preg_replace_callback( - '/name="auth\[server\]" value="" title="(?:[^"]+)"/', + '/name="auth\[(server|username|db)\]" (id="[^"]*" )?(autofocus )?value="([^"]*)"/', static function (array $matches): string { - return \str_replace( - 'value=""', - \sprintf('value="%s"', ($_ENV['ADMINER_DEFAULT_SERVER'] ?: 'db')), - $matches[0], + $defaultValues = ['server' => 'db']; + return \sprintf( + 'name="auth[%s]" %s%svalue="%s"', + $matches[1], + $matches[2] ?: '', + $matches[3] ?: '', + $_ENV['ADMINER_DEFAULT_' . \strtoupper($matches[1])] ?? ($matches[4] ?: ($defaultValues[$matches[1]] ?? '')) ); }, $field, @@ -38,9 +57,9 @@ static function (array $matches): string { (function () { $last = &$this->hooks['loginFormField'][\array_key_last($this->hooks['loginFormField'])]; if ($last instanceof \Adminer\Adminer) { - $defaultServerPlugin = new DefaultServerPlugin($last); - $this->plugins[] = $defaultServerPlugin; - $last = $defaultServerPlugin; + $envLoginFormFieldsPlugin = new EnvLoginFormFieldsPlugin($last); + $this->plugins[] = $envLoginFormFieldsPlugin; + $last = $envLoginFormFieldsPlugin; } })->call($adminer); diff --git a/5/index.php b/5/index.php index 934da37..d9e873f 100644 --- a/5/index.php +++ b/5/index.php @@ -2,9 +2,9 @@ namespace docker { function adminer_object() { /** - * Prefills the “Server” field with the ADMINER_DEFAULT_SERVER environment variable. + * Prefills login fields with the corresponding ADMINER_DEFAULT_* environment variables. */ - final class DefaultServerPlugin extends \Adminer\Plugin { + final class EnvLoginFormFieldsPlugin extends \Adminer\Plugin { public function __construct( private \Adminer\Adminer $adminer ) { } @@ -12,14 +12,33 @@ public function __construct( public function loginFormField(...$args): string { return (function (...$args): string { $field = $this->loginFormField(...$args); - + + if (!empty($_ENV['ADMINER_DEFAULT_DRIVER']) && \str_contains($field, '#', + \sprintf('', $_ENV['ADMINER_DEFAULT_DRIVER']), + $field, + ); + } + + if (!empty($_ENV['ADMINER_DEFAULT_PASSWORD']) && \str_contains($field, 'auth[password]')) { + return \preg_replace( + '/name="auth\[password\]" (value="([^"]*)" )?/', + \sprintf('name="auth[password]" value="%s" ', $_ENV['ADMINER_DEFAULT_PASSWORD']), + $field, + ); + } + return \preg_replace_callback( - '/name="auth\[server\]" value="" title="(?:[^"]+)"/', + '/name="auth\[(server|username|db)\]" (id="[^"]*" )?(autofocus )?value="([^"]*)"/', static function (array $matches): string { - return \str_replace( - 'value=""', - \sprintf('value="%s"', ($_ENV['ADMINER_DEFAULT_SERVER'] ?: 'db')), - $matches[0], + $defaultValues = ['server' => 'db']; + return \sprintf( + 'name="auth[%s]" %s%svalue="%s"', + $matches[1], + $matches[2] ?: '', + $matches[3] ?: '', + $_ENV['ADMINER_DEFAULT_' . \strtoupper($matches[1])] ?? ($matches[4] ?: ($defaultValues[$matches[1]] ?? '')) ); }, $field, @@ -38,9 +57,9 @@ static function (array $matches): string { (function () { $last = &$this->hooks['loginFormField'][\array_key_last($this->hooks['loginFormField'])]; if ($last instanceof \Adminer\Adminer) { - $defaultServerPlugin = new DefaultServerPlugin($last); - $this->plugins[] = $defaultServerPlugin; - $last = $defaultServerPlugin; + $envLoginFormFieldsPlugin = new EnvLoginFormFieldsPlugin($last); + $this->plugins[] = $envLoginFormFieldsPlugin; + $last = $envLoginFormFieldsPlugin; } })->call($adminer);