public/wordpress/wp-content/plugins/backupbuddy/classes/core.php
/* importbuddy()
	 *
	 * IMPORTANT: If outputting to browser (no output file) must die() after outputting content if using AJAX. Do not output to browser anything after this function in this case.
	 * IMPORTANT: _ALWAYS_ returns FALSE if no importbuddy pass hash is passed AND no pass hash is set in the Settings page.
	 * If $output_file is blank then importbuddy will either be returned or returned to the brwoser as a downloaded file based on 3rd parameter.
	 * If $output_file is defined then returns true on success writing file, else false.
	 *
	 * Use $importbuddy_pass_hash = null to leave #PASSWORD# placeholder. Eg for use with Live.
	 */
importbuddy()
IMPORTANT: If outputting to browser (no output file) must die() after outputting content if using AJAX. Do not output to browser anything after this function in this case.
IMPORTANT: _ALWAYS_ returns FALSE if no importbuddy pass hash is passed AND no pass hash is set in the Settings page.
If $output_file is blank then importbuddy will either be returned or returned to the brwoser as a downloaded file based on 3rd parameter.
If $output_file is defined then returns true on success writing file, else false.

Use $importbuddy_pass_hash = null to leave #PASSWORD# placeholder. Eg for use with Live.
importbuddy () IMPORTANT : If outputting to browser ( no output file ) must die () after outputting content if using AJAX . Do not output to browser anything after this function in this case . IMPORTANT : _ALWAYS_ returns FALSE if no importbuddy pass hash is passed AND no pass hash is set in the Settings page . If $output_file is blank then importbuddy will either be returned or returned to the brwoser as a downloaded file based on 3rd parameter . If $output_file is defined then returns true on success writing file else false . Use $importbuddy_pass_hash = null to leave #PASSWORD# placeholder . Eg for use with Live .
public static function importbuddy( $output_file = '', $importbuddy_pass_hash = '', $return_not_echo = false ) {
		pb_backupbuddy::set_greedy_script_limits(); 
		if ( null !== $importbuddy_pass_hash ) {
			if ( $importbuddy_pass_hash == '' ) {
				if ( !isset( pb_backupbuddy::$options ) ) {
					pb_backupbuddy::load();
				}
				$importbuddy_pass_hash = pb_backupbuddy::$options['importbuddy_pass_hash'];
			}
			if ( $importbuddy_pass_hash == '' ) {
				$message = 'Warning #9032 - You have not set an ImportBuddy password on the BackupBuddy Settings page. A copy of the importbuddy.php file needed to restore your backup is included in Full backup zip files for convenience. Since a password is not a random one has been applied to this importbuddy.php so you will need to use the "Forgot Password" option if using it to restore.';
				pb_backupbuddy::status( 'warning', $message );
				$importbuddy_pass_hash = pb_backupbuddy::random_string( 45 ); 
			}
		}
		pb_backupbuddy::status( 'details', 'Loading importbuddy core file into memory.' );
		$output = file_get_contents( pb_backupbuddy::plugin_path() . '/_importbuddy/_importbuddy.php' );
		if ( ( null !== $importbuddy_pass_hash ) && ( $importbuddy_pass_hash != '' ) ) {
			$output = preg_replace('/#PASSWORD#/', $importbuddy_pass_hash, $output, 1 ); 
		}
		$version_string = pb_backupbuddy::settings( 'version' ) . ' (downloaded ' . date( DATE_W3C ) . ')';
		if ( defined( 'BACKUPBUDDY_DEV' ) && ( true === BACKUPBUDDY_DEV ) ) {
			if ( @file_exists( pb_backupbuddy::plugin_path() . '/.git/logs/HEAD' ) ) {
				$commit_log = escapeshellarg( pb_backupbuddy::plugin_path() . '/.git/logs/HEAD' );
				$commit_line = str_replace( '\'', '`', exec( "tail -n 1 {$commit_log}" ) );
				$version_string .= ' <span style="font-size: 8px;">[DEV: ' . $commit_line . ']</span>';
			}
		}
		$output = preg_replace('/#VERSION#/', $version_string, $output, 2 ); 
		$_packdata = array( 
			'_importbuddy/importbuddy'							=>		'importbuddy',
			'classes/_migrate_database.php'						=>		'importbuddy/classes/_migrate_database.php',
			'classes/core.php'									=>		'importbuddy/classes/core.php',
			'classes/import.php'								=>		'importbuddy/classes/import.php',
			'classes/restore.php'								=>		'importbuddy/classes/restore.php',
			'classes/_restoreFiles.php'							=>		'importbuddy/classes/_restoreFiles.php',
			'classes/remote_api.php'								=>		'importbuddy/classes/remote_api.php',
			'js/jquery.leanModal.min.js'							=>		'importbuddy/js/jquery.leanModal.min.js',
			'css/animate.css'									=>		'importbuddy/css/animate.css',
			'images/working.gif'								=>		'importbuddy/images/working.gif',
			'images/bullet_go.png'								=>		'importbuddy/images/bullet_go.png',
			'images/favicon.png'								=>		'importbuddy/images/favicon.png',
			'images/sort_down.png'								=>		'importbuddy/images/sort_down.png',
			'images/icon_menu_32x32.png'							=>		'importbuddy/images/icon_menu_32x32.png',
			'lib/dbreplace'									=>		'importbuddy/lib/dbreplace',
			'lib/dbimport'										=>		'importbuddy/lib/dbimport',
			'lib/commandbuddy'									=>		'importbuddy/lib/commandbuddy',
			'lib/zipbuddy'										=>		'importbuddy/lib/zipbuddy',
			'lib/mysqlbuddy'									=>		'importbuddy/lib/mysqlbuddy',
			'lib/textreplacebuddy'								=>		'importbuddy/lib/textreplacebuddy',
			'lib/cpanel'										=>		'importbuddy/lib/cpanel',
			'pluginbuddy'										=>		'importbuddy/pluginbuddy',
			'controllers/pages/server_info'						=>		'importbuddy/controllers/pages/server_info',
			'controllers/pages/server_tools.php'					=>		'importbuddy/controllers/pages/server_tools.php',
			'destinations/stash2/class.itx_helper2.php'				=>		'importbuddy/lib/stash2/class.itx_helper2.php',
			'destinations/stash2/class.itcred.php'					=>		'importbuddy/lib/stash2/class.itcred.php',
			'destinations/stash2/class-phpass.php'					=>		'importbuddy/lib/stash2/class-phpass.php',
			'destinations/_s3lib/aws-sdk/lib/requestcore'			=>		'importbuddy/lib/requestcore',
		);
		pb_backupbuddy::status( 'details', 'Loading each file into memory for writing master importbuddy file.' );
		$output .= "\n<?php /*\n###PACKDATA,BEGIN\n";
		foreach( $_packdata as $pack_source => $pack_destination ) {
			$pack_source = '/' . $pack_source;
			if ( is_dir( pb_backupbuddy::plugin_path() . $pack_source ) ) {
				$files = pb_backupbuddy::$filesystem->deepscandir( pb_backupbuddy::plugin_path() . $pack_source );
			} else {
				$files = array( pb_backupbuddy::plugin_path() . $pack_source );
			}
			foreach( $files as $file ) {
				if ( is_file( $file ) ) {
					$source = str_replace( pb_backupbuddy::plugin_path(), '', $file );
					$destination = $pack_destination . substr( $source, strlen( $pack_source ) );
					$output .= "###PACKDATA,FILE_START,{$source},{$destination}\n";
					$output .= base64_encode( file_get_contents( $file ) );
					$output .= "\n";
					$output .= "###PACKDATA,FILE_END,{$source},{$destination}\n";
				}
			}
		}
		$output .= "###PACKDATA,END\n*/";
		$output .= "\n\n\n\n\n\n\n\n\n\n";
		if ( true === $return_not_echo ) {
			return $output;
		}
		if ( $output_file == '' ) { 
			header( 'Content-Description: File Transfer' );
			header( 'Content-Type: text/plain; name=importbuddy.php' );
			header( 'Content-Disposition: attachment; filename=importbuddy.php' );
			header( 'Expires: 0' );
			header( 'Content-Length: ' . strlen( $output ) );
			pb_backupbuddy::flush();
			echo $output;
			pb_backupbuddy::flush();
		} else { 
			pb_backupbuddy::status( 'details', 'Writing importbuddy master file to disk.' );
			if ( false === file_put_contents( $output_file, $output ) ) {
				pb_backupbuddy::status( 'error', 'Error #483894: Unable to write to file `' . $output_file . '`.' );
				return false;
			} else {
				return true;
			}
		}
	}
public static function importbuddy ( $ output_file = '' , $ importbuddy_pass_hash = '' , $ return_not_echo = false ) { pb_backupbuddy :: set_greedy_script_limits ( ) ; if ( null !== $ importbuddy_pass_hash ) { if ( $ importbuddy_pass_hash == '' ) { if ( ! isset ( pb_backupbuddy :: $ options ) ) { pb_backupbuddy :: load ( ) ; } $ importbuddy_pass_hash = pb_backupbuddy :: $ options [ 'importbuddy_pass_hash' ] ; } if ( $ importbuddy_pass_hash == '' ) { $ message = 'Warning #9032 - You have not set an ImportBuddy password on the BackupBuddy Settings page. A copy of the importbuddy.php file needed to restore your backup is included in Full backup zip files for convenience. Since a password is not a random one has been applied to this importbuddy.php so you will need to use the "Forgot Password" option if using it to restore.' ; pb_backupbuddy :: status ( 'warning' , $ message ) ; $ importbuddy_pass_hash = pb_backupbuddy :: random_string ( 45 ) ; } } pb_backupbuddy :: status ( 'details' , 'Loading importbuddy core file into memory.' ) ; $ output = file_get_contents ( pb_backupbuddy :: plugin_path ( ) . '/_importbuddy/_importbuddy.php' ) ; if ( ( null !== $ importbuddy_pass_hash ) && ( $ importbuddy_pass_hash != '' ) ) { $ output = preg_replace ( '/#PASSWORD#/' , $ importbuddy_pass_hash , $ output , 1 ) ; } $ version_string = pb_backupbuddy :: settings ( 'version' ) . ' (downloaded ' . date ( DATE_W3C ) . ')' ; if ( defined ( 'BACKUPBUDDY_DEV' ) && ( true === BACKUPBUDDY_DEV ) ) { if ( @ file_exists ( pb_backupbuddy :: plugin_path ( ) . '/.git/logs/HEAD' ) ) { $ commit_log = escapeshellarg ( pb_backupbuddy :: plugin_path ( ) . '/.git/logs/HEAD' ) ; $ commit_line = str_replace ( '\'' , '`' , exec ( " tail -n 1  { $ commit_log } " ) ) ; $ version_string .= ' <span style="font-size: 8px;">[DEV: ' . $ commit_line . ']</span>' ; } } $ output = preg_replace ( '/#VERSION#/' , $ version_string , $ output , 2 ) ; $ _packdata = array ( '_importbuddy/importbuddy' => 'importbuddy' , 'classes/_migrate_database.php' => 'importbuddy/classes/_migrate_database.php' , 'classes/core.php' => 'importbuddy/classes/core.php' , 'classes/import.php' => 'importbuddy/classes/import.php' , 'classes/restore.php' => 'importbuddy/classes/restore.php' , 'classes/_restoreFiles.php' => 'importbuddy/classes/_restoreFiles.php' , 'classes/remote_api.php' => 'importbuddy/classes/remote_api.php' , 'js/jquery.leanModal.min.js' => 'importbuddy/js/jquery.leanModal.min.js' , 'css/animate.css' => 'importbuddy/css/animate.css' , 'images/working.gif' => 'importbuddy/images/working.gif' , 'images/bullet_go.png' => 'importbuddy/images/bullet_go.png' , 'images/favicon.png' => 'importbuddy/images/favicon.png' , 'images/sort_down.png' => 'importbuddy/images/sort_down.png' , 'images/icon_menu_32x32.png' => 'importbuddy/images/icon_menu_32x32.png' , 'lib/dbreplace' => 'importbuddy/lib/dbreplace' , 'lib/dbimport' => 'importbuddy/lib/dbimport' , 'lib/commandbuddy' => 'importbuddy/lib/commandbuddy' , 'lib/zipbuddy' => 'importbuddy/lib/zipbuddy' , 'lib/mysqlbuddy' => 'importbuddy/lib/mysqlbuddy' , 'lib/textreplacebuddy' => 'importbuddy/lib/textreplacebuddy' , 'lib/cpanel' => 'importbuddy/lib/cpanel' , 'pluginbuddy' => 'importbuddy/pluginbuddy' , 'controllers/pages/server_info' => 'importbuddy/controllers/pages/server_info' , 'controllers/pages/server_tools.php' => 'importbuddy/controllers/pages/server_tools.php' , 'destinations/stash2/class.itx_helper2.php' => 'importbuddy/lib/stash2/class.itx_helper2.php' , 'destinations/stash2/class.itcred.php' => 'importbuddy/lib/stash2/class.itcred.php' , 'destinations/stash2/class-phpass.php' => 'importbuddy/lib/stash2/class-phpass.php' , 'destinations/_s3lib/aws-sdk/lib/requestcore' => 'importbuddy/lib/requestcore' , ) ; pb_backupbuddy :: status ( 'details' , 'Loading each file into memory for writing master importbuddy file.' ) ; $ output .= " \n <?php /* \n ###PACKDATA,BEGIN \n " ; foreach ( $ _packdata as $ pack_source => $ pack_destination ) { $ pack_source = '/' . $ pack_source ; if ( is_dir ( pb_backupbuddy :: plugin_path ( ) . $ pack_source ) ) { $ files = pb_backupbuddy :: $ filesystem -> deepscandir ( pb_backupbuddy :: plugin_path ( ) . $ pack_source ) ; } else { $ files = array ( pb_backupbuddy :: plugin_path ( ) . $ pack_source ) ; } foreach ( $ files as $ file ) { if ( is_file ( $ file ) ) { $ source = str_replace ( pb_backupbuddy :: plugin_path ( ) , '' , $ file ) ; $ destination = $ pack_destination . substr ( $ source , strlen ( $ pack_source ) ) ; $ output .= " ###PACKDATA,FILE_START, { $ source } , { $ destination } \n " ; $ output .= base64_encode ( file_get_contents ( $ file ) ) ; $ output .= " \n " ; $ output .= " ###PACKDATA,FILE_END, { $ source } , { $ destination } \n " ; } } } $ output .= " ###PACKDATA,END \n */ " ; $ output .= " \n \n \n \n \n \n \n \n \n \n " ; if ( true === $ return_not_echo ) { return $ output ; } if ( $ output_file == '' ) { header ( 'Content-Description: File Transfer' ) ; header ( 'Content-Type: text/plain; name=importbuddy.php' ) ; header ( 'Content-Disposition: attachment; filename=importbuddy.php' ) ; header ( 'Expires: 0' ) ; header ( 'Content-Length: ' . strlen ( $ output ) ) ; pb_backupbuddy :: flush ( ) ; echo $ output ; pb_backupbuddy :: flush ( ) ; } else { pb_backupbuddy :: status ( 'details' , 'Writing importbuddy master file to disk.' ) ; if ( false === file_put_contents ( $ output_file , $ output ) ) { pb_backupbuddy :: status ( 'error' , 'Error #483894: Unable to write to file `' . $ output_file . '`.' ) ; return false ; } else { return true ; } } }
importbuddy()
 IMPORTANT: If outputting to browser (no output file) must die() after outputting content if using AJAX.
importbuddy () IMPORTANT : If outputting to browser ( no output file ) must die () after outputting content if using AJAX .
// Some people run out of PHP memory. // Random long hash for dummy password. // Only replaces first instance. // If on DEV system (.git dir exists) then append some details on current. // Only replaces first TWO instances. // PACK IMPORTBUDDY // NO TRAILING OR PRECEEDING SLASHES! // Stash //'destinations/stash2/init.php'						=>		'importbuddy/lib/stash2/init.php', // No file so output to browser. // BE SURE TO die() AFTER THIS AND NOT OUTPUT TO BROWSER! // Write to file.