Skip to content
Open
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
33 changes: 32 additions & 1 deletion 4/fastcgi/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'#<select name=["\']auth\[driver\]["\'].*</select>#',
\sprintf('<input name="auth[driver]" value="%s">', $_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;
}
Expand Down
33 changes: 32 additions & 1 deletion 4/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'#<select name=["\']auth\[driver\]["\'].*</select>#',
\sprintf('<input name="auth[driver]" value="%s">', $_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;
}
Expand Down
41 changes: 30 additions & 11 deletions 5/fastcgi/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,43 @@
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
) { }

public function loginFormField(...$args): string {
return (function (...$args): string {
$field = $this->loginFormField(...$args);


if (!empty($_ENV['ADMINER_DEFAULT_DRIVER']) && \str_contains($field, '<select')) {
return \preg_replace(
'#<select name=["\']auth\[driver\]["\'].*</select>#',
\sprintf('<input name="auth[driver]" value="%s">', $_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,
Expand All @@ -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);

Expand Down
41 changes: 30 additions & 11 deletions 5/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,43 @@
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
) { }

public function loginFormField(...$args): string {
return (function (...$args): string {
$field = $this->loginFormField(...$args);


if (!empty($_ENV['ADMINER_DEFAULT_DRIVER']) && \str_contains($field, '<select')) {
return \preg_replace(
'#<select name=["\']auth\[driver\]["\'].*</select>#',
\sprintf('<input name="auth[driver]" value="%s">', $_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,
Expand All @@ -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);

Expand Down