Skip to content

Instantly share code, notes, and snippets.

@bjorn-ali-goransson
Last active August 23, 2019 16:10
Show Gist options
  • Save bjorn-ali-goransson/51d141f48accefcb45fbc7bb058e18bc to your computer and use it in GitHub Desktop.
Save bjorn-ali-goransson/51d141f48accefcb45fbc7bb058e18bc to your computer and use it in GitHub Desktop.
Automatic login to Adminer using Wordpress DB credentials (wp-config.php)
<?php
function define_find( $filename = 'wp-config.php' ) { // https://github.com/interconnectit/Search-Replace-DB/blob/master/index.php#L726
if ( $filename == 'wp-config.php' ) {
$filename = dirname( __FILE__ ) . '/' . basename( $filename );
// look up one directory if config file doesn't exist in current directory
if ( ! file_exists( $filename ) )
$filename = dirname( __FILE__ ) . '/../' . basename( $filename );
}
if ( file_exists( $filename ) && is_file( $filename ) && is_readable( $filename ) ) {
$file = @fopen( $filename, 'r' );
$file_content = fread( $file, filesize( $filename ) );
@fclose( $file );
}
preg_match_all( '/define\s*?\(\s*?([\'"])(DB_NAME|DB_USER|DB_PASSWORD|DB_HOST|DB_CHARSET|DB_COLLATE)\1\s*?,\s*?([\'"])([^\3]*?)\3\s*?\)\s*?;/si', $file_content, $defines );
if ( ( isset( $defines[ 2 ] ) && ! empty( $defines[ 2 ] ) ) && ( isset( $defines[ 4 ] ) && ! empty( $defines[ 4 ] ) ) ) {
foreach( $defines[ 2 ] as $key => $define ) {
switch( $define ) {
case 'DB_NAME':
$name = $defines[ 4 ][ $key ];
break;
case 'DB_USER':
$user = $defines[ 4 ][ $key ];
break;
case 'DB_PASSWORD':
$pass = $defines[ 4 ][ $key ];
break;
case 'DB_HOST':
$host = $defines[ 4 ][ $key ];
break;
case 'DB_CHARSET':
$char = $defines[ 4 ][ $key ];
break;
case 'DB_COLLATE':
$coll = $defines[ 4 ][ $key ];
break;
}
}
}
return array(
'host' => $host,
'name' => $name,
'user' => $user,
'pass' => $pass,
'char' => $char,
'coll' => $coll
);
}
$creds = define_find();
?>
<form action='adminer.php' method='post' name='login'>
<input name='auth[driver]' value="server">
<input name="auth[server]" value="<?= $creds['host'] ?>">
<input name="auth[username]" value="<?= $creds['user'] ?>">
<input name="auth[password]" value="<?= $creds['pass'] ? $creds['pass'] : 'any_password' ?>">
<input name="auth[db]" value="<?= $creds['name'] ?>">
</form>
<script>
window.onload = function(){
document.forms['login'].submit();
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment