Created
October 17, 2014 02:53
-
-
Save fabrizim/1239d95174cac4ce75a5 to your computer and use it in GitHub Desktop.
Update passwords after MySQL upgrade
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env php | |
<?php | |
echo "MySQL root Password: "; | |
system('stty -echo'); | |
$root_password = trim(fgets(STDIN)); | |
system('stty echo'); | |
// add a new line since the users CR didn't echo | |
echo "\n"; | |
$dbh = new PDO('mysql:host=localhost', 'root', $root_password); | |
$files = array(); | |
foreach( array('configuration.php','wp-config.php','wp/wp-config.php', 'site/wp-config.php') as $file){ | |
$files = array_merge( $files, glob('/home/*/public_html/'.$file) ); | |
$files = array_merge( $files, glob('/home/*/vhosts/*/'.$file) ); | |
} | |
foreach( $files as $i => $file ){ | |
$creds = get_creds( $file ); | |
echo $file."\n"; | |
update_password( $creds, $dbh ); | |
} | |
function get_creds( $file ) | |
{ | |
$content = file_get_contents( $file ); | |
switch( basename( $file ) ){ | |
case 'configuration.php': | |
preg_match('/(\$password\s*=\s*[\'"])(.*?)([\'"])/i', $content, $matches); | |
$password = $matches[2]; | |
preg_match('/(\$user\s*=\s*[\'"])(.*?)([\'"])/i', $content, $matches); | |
$username = $matches[2]; | |
if( !$password && !$username ){ | |
preg_match('/(\$mosConfig_password\s*=\s*[\'"])(.*?)([\'"])/i', $content, $matches); | |
$password = $matches[2]; | |
preg_match('/(\$mosConfig_user\s*=\s*[\'"])(.*?)([\'"])/i', $content, $matches); | |
$username = $matches[2]; | |
} | |
break; | |
case 'wp-config.php': | |
preg_match('/\'DB_PASSWORD\'\s*?,\s*?\'(.*?)\'/m', $content, $matches); | |
$password = $matches[1]; | |
preg_match('/\'DB_USER\'\s*?,\s*?\'(.*?)\'/m', $content, $matches); | |
$username = $matches[1]; | |
break; | |
} | |
return array('username'=>$username, 'password'=>$password); | |
} | |
function test_creds( $creds ){ | |
return !!mysql_connect('localhost', $creds['username'], $creds['password']); | |
} | |
function update_password( $creds, $dbh ) | |
{ | |
$user = $creds['username']; | |
$pass = $creds['password']; | |
$dbh->exec("SET PASSWORD FOR '$user'@'localhost' = PASSWORD('$pass')"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment