Skip to content

Instantly share code, notes, and snippets.

@ibejohn818
Last active June 8, 2017 15:58
Show Gist options
  • Save ibejohn818/477b5aa939445e7f558a224e33ed79c7 to your computer and use it in GitHub Desktop.
Save ibejohn818/477b5aa939445e7f558a224e33ed79c7 to your computer and use it in GitHub Desktop.
Media Temple Wordpress PHP Utils
/***
* (mt) Media Temple
* PHP Tools for Wordpress migrations
*
*
*/
class MT_WP_TOOLS
{
private static $_wpDBConfig = false;
public static function wp_db_config($wp_config_path)
{
$conf = file_get_contents($wp_config_path);
$regex = preg_match('/(define\()([\'|\"])(DB_USER)([\'|\"])(.*)(\);)/',$conf,$db_user);
if(!$regex || $db_user[6] != ');') {
throw new Exception("UNABLE TO PARSE DB_USER");
}
$regex = preg_match('/(define\()([\'|\"])(DB_HOST)([\'|\"])(.*)(\);)/',$conf,$db_host);
if(!$regex || $db_host[6] != ');') {
throw new Exception("UNABLE TO PARSE DB_HOST");
}
$regex = preg_match('/(define\()([\'|\"])(DB_PASSWORD)([\'|\"])(.*)(\);)/',$conf,$db_pass);
if(!$regex || $db_pass[6] != ');') {
throw new Exception("UNABLE TO PARSE DB_PASS");
}
$regex = preg_match('/(define\()([\'|\"])(DB_NAME)([\'|\"])(.*)(\);)/',$conf,$db_name);
if(!$regex || $db_name[6] != ');') {
throw new Exception("UNABLE TO PARSE DB_NAME");
}
eval("{$db_user[0]}");
eval("{$db_pass[0]}");
eval("{$db_host[0]}");
eval("{$db_name[0]}");
return [
'dbname'=>DB_NAME,
'dbhost'=>DB_HOST,
'dbpass'=>DB_PASSWORD,
'dbuser'=>DB_USER
];
}
public static function mysqli_obj($wp_config_path)
{
$conf = static::wp_db_config($wp_config_path);
return new Mysqli($conf['dbhost'],$conf['dbuser'],$conf['dbpass'],$conf['dbname']);
}
public static function get_wp_config_path_from_env()
{
$site_path = static::get_sitepath_from_env();
$config = "{$site_path}/wp-config.php";
if(!file_exists($config)) {
throw new Exception("{$config} DOES NOT EXISTS");
}
return $config;
}
public static function get_sitepath_from_env()
{
if(!isset($_SERVER)) {
throw new Exception("\$_SERVER NOT DEFINED!");
}
if(!isset($_SERVER['HOME'])) {
throw new Exception("\$_SERVER['HOME'] NOT DEFINED!");
}
if(!isset($_SERVER['USER'])) {
throw new Exception("\$_SERVER['USER'] NOT DEFINED!");
}
$site_path = "{$_SERVER['HOME']}/{$_SERVER['USER']}";
if(!is_dir($site_path)) {
throw new Exception("{$site_path} DOES NOT EXIST!");
}
return $site_path;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment