public/wordpress/wp-content/plugins/backupbuddy/classes/restore.php
/**
	 *	_migrateWpConfigGruntwork()
	 *
	 *	Migrates and updates the wp-config.php file contents as needed.
	 *
	 *	@return			true|string			True on success. On false returns the new wp-config file content.
	 */
_migrateWpConfigGruntwork()
Migrates and updates the wp-config.php file contents as needed.

@return                 true|string                     True on success. On false returns the new wp-config file content.
_migrateWpConfigGruntwork () Migrates and updates the wp - config . php file contents as needed . @return true|string True on success . On false returns the new wp - config file content .
function _migrateWpConfigGruntwork() {
		pb_backupbuddy::status( 'message', 'Starting migration of wp-config.php file...' );
		pb_backupbuddy::flush();
		$configFile = ABSPATH . 'wp-config.php';
		pb_backupbuddy::status( 'details', 'Config file: `' . $configFile . '`.' );
		if ( file_exists( $configFile ) ) {
			$updated_home_url = false;
			$wp_config = array();
			$lines = file( $configFile );
			$patterns = array();
			$replacements = array();
			$pattern[0] = '/define\([\s]*(\'|")WP_SITEURL(\'|"),[\s]*(\'|")(.)*(\'|")[\s]*\);/i';
			$replace[0] = "define( 'WP_SITEURL', '" . trim( $this->_state['siteurl'], '/' ) . "' );";
			pb_backupbuddy::status( 'details', 'wp-config.php: Setting WP_SITEURL (if applicable) to `' . trim( $this->_state['siteurl'], '/' ) . '`.' );
			$pattern[1] = '/define\([\s]*(\'|")WP_HOME(\'|"),[\s]*(\'|")(.)*(\'|")[\s]*\);/i';
			$replace[1] = "define( 'WP_HOME', '" . trim( $this->_state['homeurl'], '/' ) . "' );";
			pb_backupbuddy::status( 'details', 'wp-config.php: Setting WP_HOME (if applicable) to `' . trim( $this->_state['homeurl'], '/' ) . '`.' );
			$pattern[2] = '/define\([\s]*(\'|")DB_NAME(\'|"),[\s]*(\'|")(.)*(\'|")[\s]*\);/i';
			$replace[2] = "define( 'DB_NAME', '" . $this->_state['databaseSettings']['database'] . "' );";
			$pattern[3] = '/define\([\s]*(\'|")DB_USER(\'|"),[\s]*(\'|")(.)*(\'|")[\s]*\);/i';
			$replace[3] = "define( 'DB_USER', '" . $this->_state['databaseSettings']['username'] . "' );";
			$pattern[4] = '/define\([\s]*(\'|")DB_PASSWORD(\'|"),[\s]*(\'|")(.)*(\'|")[\s]*\);/i';
			$replace[4] = "define( 'DB_PASSWORD', '" . $this->_preg_escape_back( $this->_state['databaseSettings']['password'] ) . "' );";
			$pattern[5] = '/define\([\s]*(\'|")DB_HOST(\'|"),[\s]*(\'|")(.)*(\'|")[\s]*\);/i';
			$replace[5] = "define( 'DB_HOST', '" . $this->_state['databaseSettings']['server'] . "' );";
			if ( isset( pb_backupbuddy::$options['domain'] ) && ( pb_backupbuddy::$options['domain'] != '' ) ) {
				$pattern[6] = '/define\([\s]*(\'|")DOMAIN_CURRENT_SITE(\'|"),[\s]*(\'|")(.)*(\'|")[\s]*\);/i';
				$replace[6] = "define( 'DOMAIN_CURRENT_SITE', '" . $this->_state['defaultDomain'] . "' );";
				pb_backupbuddy::status( 'details', 'wp-config.php: Setting DOMAIN_CURRENT_SITE (if applicable) to `' . $this->_state['databaseSettings']['defaultDomain'] . '`.' );
			} else {
				pb_backupbuddy::status( 'details', 'wp-config.php did not update DOMAIN_CURRENT_SITE as it was blank.' );
			}
			$pattern[7] = '/\$table_prefix[\s]*=[\s]*(\'|")(.)*(\'|");/i';
			$replace[7] = '$table_prefix = \'' . $this->_state['databaseSettings']['prefix'] . '\';';
			if ( isset( $this->_state['dat']['wp_content_url'] ) ) {
				$new_content_url = str_replace( trim( $this->_state['dat']['siteurl'], '\\/' ), trim( $this->_state['siteurl'], '/' ), $this->_state['dat']['wp_content_url'] );
				$pattern[8] = '/define\([\s]*(\'|")WP_CONTENT_URL(\'|"),[\s]*(\'|")(.)*(\'|")[\s]*\);/i';
				$replace[8] = "define( 'WP_CONTENT_URL', '" . $new_content_url . "' );";
				pb_backupbuddy::status( 'details', 'wp-config.php: Setting WP_CONTENT_URL (if applicable) to `' . $new_content_url . '`.' );
			}
			if ( isset( $this->_state['dat']['wp_content_dir'] ) ) {
				$new_content_dir = str_replace( $this->_state['dat']['abspath'], ABSPATH, $this->_state['dat']['wp_content_dir'] );
				$pattern[9] = '/define\([\s]*(\'|")WP_CONTENT_DIR(\'|"),[\s]*(\'|")(.)*(\'|")[\s]*\);/i';
				$replace[9] = "define( 'WP_CONTENT_DIR', '" . $new_content_dir . "' );";
				pb_backupbuddy::status( 'details', 'wp-config.php: Setting WP_CONTENT_DIR (if applicable) to `' . $new_content_dir . '`.' );
			}
			$lines = preg_replace( $pattern, $replace, $lines );
			if ( ! is_writable( $configFile ) ) {
				pb_backupbuddy::status( 'warning', 'Warning #28572: wp-config.php shows to be unwritable. Attempting to override permissions temporarily.' );
				$oldPerms = ( fileperms( $configFile ) & 0777 );
				@chmod( $configFile, 0644 ); 
			}
			if ( false === ( file_put_contents( $configFile, $lines ) ) ) {
				pb_backupbuddy::alert( 'ERROR #84928: Unable to save changes to wp-config.php. Verify this file has proper write permissions. You may need to manually edit it.', true, '9020' );
				return implode( "\n", $lines );
			}
			if ( isset( $oldPerms ) ) {
				@chmod( $configFile, $oldPerms );
			}
			unset( $lines );
		} else {
			pb_backupbuddy::status( 'details', 'Warning: wp-config.php file not found.' );
		}
		pb_backupbuddy::status( 'message', 'Migration of wp-config.php complete.' );
		return true;
	}
function _migrateWpConfigGruntwork ( ) { pb_backupbuddy :: status ( 'message' , 'Starting migration of wp-config.php file...' ) ; pb_backupbuddy :: flush ( ) ; $ configFile = ABSPATH . 'wp-config.php' ; pb_backupbuddy :: status ( 'details' , 'Config file: `' . $ configFile . '`.' ) ; if ( file_exists ( $ configFile ) ) { $ updated_home_url = false ; $ wp_config = array ( ) ; $ lines = file ( $ configFile ) ; $ patterns = array ( ) ; $ replacements = array ( ) ; $ pattern [ 0 ] = '/define\([\s]*(\'|")WP_SITEURL(\'|"),[\s]*(\'|")(.)*(\'|")[\s]*\);/i' ; $ replace [ 0 ] = " define( 'WP_SITEURL', ' " . trim ( $ this -> _state [ 'siteurl' ] , '/' ) . " ' ); " ; pb_backupbuddy :: status ( 'details' , 'wp-config.php: Setting WP_SITEURL (if applicable) to `' . trim ( $ this -> _state [ 'siteurl' ] , '/' ) . '`.' ) ; $ pattern [ 1 ] = '/define\([\s]*(\'|")WP_HOME(\'|"),[\s]*(\'|")(.)*(\'|")[\s]*\);/i' ; $ replace [ 1 ] = " define( 'WP_HOME', ' " . trim ( $ this -> _state [ 'homeurl' ] , '/' ) . " ' ); " ; pb_backupbuddy :: status ( 'details' , 'wp-config.php: Setting WP_HOME (if applicable) to `' . trim ( $ this -> _state [ 'homeurl' ] , '/' ) . '`.' ) ; $ pattern [ 2 ] = '/define\([\s]*(\'|")DB_NAME(\'|"),[\s]*(\'|")(.)*(\'|")[\s]*\);/i' ; $ replace [ 2 ] = " define( 'DB_NAME', ' " . $ this -> _state [ 'databaseSettings' ] [ 'database' ] . " ' ); " ; $ pattern [ 3 ] = '/define\([\s]*(\'|")DB_USER(\'|"),[\s]*(\'|")(.)*(\'|")[\s]*\);/i' ; $ replace [ 3 ] = " define( 'DB_USER', ' " . $ this -> _state [ 'databaseSettings' ] [ 'username' ] . " ' ); " ; $ pattern [ 4 ] = '/define\([\s]*(\'|")DB_PASSWORD(\'|"),[\s]*(\'|")(.)*(\'|")[\s]*\);/i' ; $ replace [ 4 ] = " define( 'DB_PASSWORD', ' " . $ this -> _preg_escape_back ( $ this -> _state [ 'databaseSettings' ] [ 'password' ] ) . " ' ); " ; $ pattern [ 5 ] = '/define\([\s]*(\'|")DB_HOST(\'|"),[\s]*(\'|")(.)*(\'|")[\s]*\);/i' ; $ replace [ 5 ] = " define( 'DB_HOST', ' " . $ this -> _state [ 'databaseSettings' ] [ 'server' ] . " ' ); " ; if ( isset ( pb_backupbuddy :: $ options [ 'domain' ] ) && ( pb_backupbuddy :: $ options [ 'domain' ] != '' ) ) { $ pattern [ 6 ] = '/define\([\s]*(\'|")DOMAIN_CURRENT_SITE(\'|"),[\s]*(\'|")(.)*(\'|")[\s]*\);/i' ; $ replace [ 6 ] = " define( 'DOMAIN_CURRENT_SITE', ' " . $ this -> _state [ 'defaultDomain' ] . " ' ); " ; pb_backupbuddy :: status ( 'details' , 'wp-config.php: Setting DOMAIN_CURRENT_SITE (if applicable) to `' . $ this -> _state [ 'databaseSettings' ] [ 'defaultDomain' ] . '`.' ) ; } else { pb_backupbuddy :: status ( 'details' , 'wp-config.php did not update DOMAIN_CURRENT_SITE as it was blank.' ) ; } $ pattern [ 7 ] = '/\$table_prefix[\s]*=[\s]*(\'|")(.)*(\'|");/i' ; $ replace [ 7 ] = '$table_prefix = \'' . $ this -> _state [ 'databaseSettings' ] [ 'prefix' ] . '\';' ; if ( isset ( $ this -> _state [ 'dat' ] [ 'wp_content_url' ] ) ) { $ new_content_url = str_replace ( trim ( $ this -> _state [ 'dat' ] [ 'siteurl' ] , '\\/' ) , trim ( $ this -> _state [ 'siteurl' ] , '/' ) , $ this -> _state [ 'dat' ] [ 'wp_content_url' ] ) ; $ pattern [ 8 ] = '/define\([\s]*(\'|")WP_CONTENT_URL(\'|"),[\s]*(\'|")(.)*(\'|")[\s]*\);/i' ; $ replace [ 8 ] = " define( 'WP_CONTENT_URL', ' " . $ new_content_url . " ' ); " ; pb_backupbuddy :: status ( 'details' , 'wp-config.php: Setting WP_CONTENT_URL (if applicable) to `' . $ new_content_url . '`.' ) ; } if ( isset ( $ this -> _state [ 'dat' ] [ 'wp_content_dir' ] ) ) { $ new_content_dir = str_replace ( $ this -> _state [ 'dat' ] [ 'abspath' ] , ABSPATH , $ this -> _state [ 'dat' ] [ 'wp_content_dir' ] ) ; $ pattern [ 9 ] = '/define\([\s]*(\'|")WP_CONTENT_DIR(\'|"),[\s]*(\'|")(.)*(\'|")[\s]*\);/i' ; $ replace [ 9 ] = " define( 'WP_CONTENT_DIR', ' " . $ new_content_dir . " ' ); " ; pb_backupbuddy :: status ( 'details' , 'wp-config.php: Setting WP_CONTENT_DIR (if applicable) to `' . $ new_content_dir . '`.' ) ; } $ lines = preg_replace ( $ pattern , $ replace , $ lines ) ; if ( ! is_writable ( $ configFile ) ) { pb_backupbuddy :: status ( 'warning' , 'Warning #28572: wp-config.php shows to be unwritable. Attempting to override permissions temporarily.' ) ; $ oldPerms = ( fileperms ( $ configFile ) & 0777 ) ; @ chmod ( $ configFile , 0644 ) ; } if ( false === ( file_put_contents ( $ configFile , $ lines ) ) ) { pb_backupbuddy :: alert ( 'ERROR #84928: Unable to save changes to wp-config.php. Verify this file has proper write permissions. You may need to manually edit it.' , true , '9020' ) ; return implode ( " \n " , $ lines ) ; } if ( isset ( $ oldPerms ) ) { @ chmod ( $ configFile , $ oldPerms ) ; } unset ( $ lines ) ; } else { pb_backupbuddy :: status ( 'details' , 'Warning: wp-config.php file not found.' ) ; } pb_backupbuddy :: status ( 'message' , 'Migration of wp-config.php complete.' ) ; return true ; }
_migrateWpConfigGruntwork()
      Migrates and updates the wp-config.php file contents as needed.
_migrateWpConfigGruntwork () Migrates and updates the wp - config . php file contents as needed .
// Useful REGEX site: http://gskinner.com/RegExr/ /*
			Update WP_SITEURL, WP_HOME if they exist.
			Update database DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST.
			RegExp: /define\([\s]*('|")WP_SITEURL('|"),[\s]*('|")(.)*('|")[\s]*\);/gi
			pattern: define\([\s]*('|")WP_SITEURL('|"),[\s]*('|")(.)*('|")[\s]*\);
			*/ // If multisite, update domain. /*
			Update table prefix.
			RegExp: /\$table_prefix[\s]*=[\s]*('|")(.)*('|");/gi
			pattern: \$table_prefix[\s]*=[\s]*('|")(.)*('|");
			*/ // Perform the actual replacement. // Check that we can write to this file. // Try to make writable. // Write changes to config file. // Restore prior permissions if applicable. //pb_backupbuddy::alert( 'Note: wp-config.php file not found. This is normal for a database only backup.' );