/AdminerAutoLoginForm.php Secret
-
-
Save codebymikey/518d60237143424ca6073dcec23d6bd2 to your computer and use it in GitHub Desktop.
| <?php | |
| /** | |
| * Adminer plugin for filling and/or auto-submitting the login form. | |
| * | |
| * This class allows you to pre-fill the Adminer login form fields | |
| * with predefined credentials, and optionally auto-submit | |
| * the form to perform an automatic login. | |
| * | |
| * Typical usage: instantiate the class with login parameters and call loginForm(). | |
| * | |
| * @author Paul BOREL <paul.borel@gmail.com> | |
| */ | |
| class AdminerAutoLoginForm { | |
| /** | |
| * @var array Associative array with login parameters: system, server, name, pass, database. | |
| */ | |
| private $params; | |
| /** | |
| * @var bool Indicates whether the form should be automatically submitted after filling. | |
| */ | |
| private $autoSubmit; | |
| /** | |
| * Initializes the plugin to fill (and optionally auto-submit) the Adminer login form. | |
| * | |
| * The $params keys can be: | |
| * - system : (string) database driver type. Possible values: | |
| * - "server" (MySQL) | |
| * - "sqlite" (SQLite3) | |
| * - "sqlite2" (SQLite2) | |
| * - "pgsql" (PostgreSQL) | |
| * - "oracle" (Oracle) | |
| * - "mssql" (MS SQL) | |
| * - "firebird" (Firebird alpha) | |
| * - "simpledb" (SimpleDB) | |
| * - "mongo" (MongoDB) | |
| * - "elastic" (Elasticsearch) | |
| * - server : (string) SQL server address/name, default "" | |
| * - name : (string) SQL username, default "" | |
| * - pass : (string) password, default "" | |
| * - database : (string) database name, default "" | |
| * | |
| * @param array $params Associative array of login parameters. | |
| * @param bool $autoSubmit Whether to automatically submit the form after filling it (default true). | |
| */ | |
| public function __construct(array $params = [], $autoSubmit = null) { | |
| $defaults = [ | |
| 'system' => $_ENV['ADMINER_DEFAULT_SYSTEM'] ?? 'server', | |
| 'server' => $_ENV['ADMINER_DEFAULT_SERVER'] ?? '', | |
| 'name' => $_ENV['ADMINER_DEFAULT_NAME'] ?? '', | |
| 'pass' => $_ENV['ADMINER_DEFAULT_PASS'] ?? '', | |
| 'database' => $_ENV['ADMINER_DEFAULT_DATABASE'] ?? '', | |
| ]; | |
| $autoSubmit = $autoSubmit ?? (($_ENV['ADMINER_AUTOLOGIN_AUTOSUBMIT'] ?? 'false') === 'true'); | |
| $this->params = array_merge($defaults, $params); | |
| $this->autoSubmit = $autoSubmit; | |
| } | |
| function name() { | |
| return null; | |
| } | |
| /** | |
| * Outputs JavaScript code to pre-fill and/or auto-submit the Adminer login form. | |
| * | |
| * The script is output only if the URL parameters do not already contain | |
| * the login information (driver, username, db). | |
| * | |
| * @return null | |
| */ | |
| public function loginForm() { | |
| $empty = empty($_GET[Adminer\DRIVER] ?? '') && | |
| empty($_GET['username'] ?? '') && | |
| empty($_GET['db'] ?? ''); | |
| if ( | |
| $empty || ( | |
| ($_GET[Adminer\DRIVER] ?? '' === $this->params['server']) | |
| ) | |
| ) { | |
| $fields = [ | |
| 'auth[server]' => $this->params['server'], | |
| 'auth[username]' => $this->params['name'], | |
| 'auth[password]' => $this->params['pass'], | |
| 'auth[db]' => $this->params['database'] | |
| ]; | |
| ?> | |
| <script<?= Adminer\nonce(); ?>> | |
| document.addEventListener("DOMContentLoaded", function() { | |
| // Select the driver in the dropdown | |
| var dr = document.querySelector('option[value="<?= htmlspecialchars($this->params['system']) ?>"]'); | |
| if(dr) dr.selected = true; | |
| // Auto-fill the form fields | |
| <?php foreach($fields as $name => $value): if($value): ?> | |
| var el = document.querySelector('input[name="<?= htmlspecialchars($name) ?>"]'); | |
| if(el && el.value.trim() === "") el.value = "<?= addslashes($value) ?>"; | |
| <?php endif; endforeach; ?> | |
| // Automatically submit the form if enabled | |
| <?php if($empty && $this->autoSubmit): ?> | |
| var btn = document.querySelector('input[type="submit"]'); | |
| if(btn) btn.click(); | |
| <?php endif; ?> | |
| }); | |
| </script> | |
| <?php | |
| } | |
| return null; | |
| } | |
| } |
This version of the plugin is designed to work with the adminer docker image - entirely via environment variables and ADMINER_PLUGINS without needing to mount the /var/www/html/plugins-enabled/autologin-form.php file path since the image automatically does that for you.
I think you might still need to mount /var/www/html/plugins/autologin-form.php pointing to this file though.
I think the convention is to keep the plugin with just the class definition (so other plugins can extend it as needed), then have a separate file that instantiates/enable or disable it without deleting the actual plugin.
I couldn't get it working with the ADMINER_PLUGINS environment variable and the mount, but I now notice a difference between /plugins and /plugins-enabled: that might have been the reason it didn't work for me. I probably mixed them up.
Just for completeness' sake: I now have the plugin file (with return new AdminerAutoLoginForm(); at the end) in some directory that I mount as /plugins-enabled, without specifying ADMINER_PLUGINS:
volumes:
- ../dir-with-the-php-file/:/var/www/html/plugins-enabled
When including this file via my docker-compose.yml, I needed to add
return new AdminerAutoLoginForm();at the end of the file, otherwise the plugin would not work.