public/wordpress/wp-content/plugins/backupbuddy/classes/restore.php
/**
	 *	migrateHtaccess()
	 *
	 *	Migrates .htaccess.bb_temp file if it exists.
	 *
	 *	@return		boolean		False only if file is unwritable. True if write success; true if file does not even exist.
	 *
	 */
migrateHtaccess()
Migrates .htaccess.bb_temp file if it exists.

@return         boolean         False only if file is unwritable. True if write success; true if file does not even exist.
migrateHtaccess () Migrates . htaccess . bb_temp file if it exists . @return boolean False only if file is unwritable . True if write success ; true if file does not even exist .
function migrateHtaccess() {
		$htaccessFile = ABSPATH . '.htaccess.bb_temp';
		if ( ! file_exists( $htaccessFile ) ) {
			pb_backupbuddy::status( 'message', 'No `' . basename( $htaccessFile ) . '` file found. Creating basic default .htaccess.bb_temp file (to be later renamed to .htaccess).' );
			$htaccess_contents =
				"# BEGIN WordPress\n
				<IfModule mod_rewrite.c>\n
				RewriteEngine On\n
				RewriteBase /\n
				RewriteRule ^index\\.php$ - [L]\n
				RewriteCond %{REQUEST_FILENAME} !-f\n
				RewriteCond %{REQUEST_FILENAME} !-d\n
				RewriteRule . /index.php [L]\n
				</IfModule>\n
				# END WordPress\n";
			file_put_contents( $htaccessFile, $htaccess_contents );
			unset( $htaccess_contents );
		}
		pb_backupbuddy::status( 'message', 'Migrating `' . basename( $htaccessFile ) . '` file...' );
		$oldurl = strtolower( $this->_state['dat']['siteurl'] );
		$oldurl = str_replace( '/', '\\', $oldurl );
		$oldurl = str_replace( 'http:\\', '', $oldurl );
		$oldurl = trim( $oldurl, '\\' );
		$oldurl = explode( '\\', $oldurl );
		$oldurl[0] = '';
		$old_path = implode( '/', $oldurl );
		$newurl = strtolower( $this->_state['siteurl'] );
		$newurl = str_replace( '/', '\\', $newurl );
		$newurl = str_replace( 'http:\\', '', $newurl );
		$newurl = trim( $newurl, '\\' );
		$newurl = explode( '\\', $newurl );
		$newurl[0] = '';
		pb_backupbuddy::status( 'message', 'Checking `' . basename( $htaccessFile ) . '` file.' );
		if ( $newurl !== $oldurl ) {
			pb_backupbuddy::status( 'message', 'URL directory has changed. Updating from `' . implode( '/', $oldurl ) . '` to `' . implode( '/', $newurl ) . '`.' );
		}
		$rewrite_lines = array();
		$got_rewrite = false;
		$rewrite_path = implode( '/', $newurl );
		$file_array = file( $htaccessFile );
		$htaccessNeedsUpdating = false;
		foreach ( (array)$file_array as $line_number => $line) {
			if ( $got_rewrite == true ) { 
				if ( strstr( $line, 'END WordPress' ) ) { 
					$got_rewrite = false;
					$rewrite_lines[] =  $line; 
				} else {
					if ( $newurl !== $oldurl ) {
						if ( strstr( $line, 'RewriteBase' ) ) { 
							$rewrite_lines[] = 'RewriteBase ' . $rewrite_path . '/' . "\n";
							$htaccessNeedsUpdating = true;
						} elseif ( strstr( $line, 'RewriteRule' ) ) { 
							if ( strstr( $line, '^index\.php$' ) ) { 
								$rewrite_lines[] = $line;
								pb_backupbuddy::status( 'details', '.htaccess ^index\.php$ detected. Leaving as is.' );
							} elseif ( ! strstr( $line, 'RewriteRule . ') ) {
								$new_line = str_replace( $old_path, $rewrite_path, $line );
								$rewrite_lines[] = $new_line;
								$htaccessNeedsUpdating = true;
								if ( $new_line != $line ) {
									pb_backupbuddy::status( 'message', '.htaccess line changed from `' . trim( $line ) . '` to `' . trim( $new_line ) . '`.' );
								}
							} else { 
								$rewrite_lines[] = 'RewriteRule . ' . $rewrite_path . '/index.php' . "\n";
								$htaccessNeedsUpdating = true;
							}
						} else { 
							$rewrite_lines[] =  $line; 
							if ( false !== strstr( $line, 'RewriteRule . ') ) { , warn user potentially if path may need changed.
								if ( $old_path !== $rewrite_path ) {
									pb_backupbuddy::status( 'warning', 'User-defined RewriteRule found and WordPress path has changed so this rule MAY need manually updated by you to function properly.  Line: "' . $line . '".' );
								}
							}
						}
					}
				}
			} else { 
				if ( strstr( $line, 'BEGIN WordPress' ) ) {
					$got_rewrite = true; 
				}
				if ( strstr( $line, 'AddHandler' ) ) { 
					$message = 'An AddHandler directive has been found in your .htaccess file. This could result in WordPress and PHP not running properly if configured improperly, especially when migrating to a new server. If you encounter problems such as an Internal Server Error or Error 500, try removing this line from your .htaccess file.  Line: "' . $line . '".';
					pb_backupbuddy::status( 'warning', $message );
					pb_backupbuddy::alert( $message );
				} else {
					$rewrite_lines[] =  $line; 
				}
			}
		} 
		if ( true === $htaccessNeedsUpdating ) {
			if ( file_exists( $htaccessFile ) && ( ! is_writable( $htaccessFile ) ) ) {
				pb_backupbuddy::status( 'warning', 'Warning #28573: Temp `' . basename( $htaccessFile ) . '` file shows to be unwritable. Attempting to override permissions temporarily.' );
				$oldPerms = ( fileperms( $htaccessFile ) & 0777 );
				@chmod( $htaccessFile, 0644 ); 
				if ( ! is_writable( $htaccessFile ) ) {
					pb_backupbuddy::status( 'error', 'Error #9020: Unable to write to `' . basename( $htaccessFile ) . '` file. Verify permissions.' );
					pb_backupbuddy::alert( 'Warning: Unable to write to temporary .htaccess file. Verify this file has proper write permissions. You may receive 404 Not Found errors on your site if this is not corrected. To fix after migration completes: Log in to your WordPress admin and select Settings: Permalinks from the left menu then save. To manually update, copy/paste the following into your .htaccess file: <textarea>' . implode( $rewrite_lines ) . '</textarea>', '9020' );
					return false;
				}
			}
			$handling = fopen( $htaccessFile, 'w');
			fwrite( $handling, implode( $rewrite_lines ) );
			fclose( $handling );
			unset( $handling );
			if ( isset( $oldPerms ) ) {
				@chmod( $htaccessFile, $oldPerms );
			}
			pb_backupbuddy::status( 'message', 'Migrated `' . basename( $htaccessFile ) . '` file. It will be renamed back to `.htaccess` on the final step.' );
		} else {
			pb_backupbuddy::status( 'message', 'No changes needed for `' . basename( $htaccessFile ) . '` file.' );
		}
		return true;
	}
function migrateHtaccess ( ) { $ htaccessFile = ABSPATH . '.htaccess.bb_temp' ; if ( ! file_exists ( $ htaccessFile ) ) { pb_backupbuddy :: status ( 'message' , 'No `' . basename ( $ htaccessFile ) . '` file found. Creating basic default .htaccess.bb_temp file (to be later renamed to .htaccess).' ) ; $ htaccess_contents = " # BEGIN WordPress \n 
				<IfModule mod_rewrite.c> \n 
				RewriteEngine On \n 
				RewriteBase / \n 
				RewriteRule ^index \\ .php$ - [L] \n 
				RewriteCond %{REQUEST_FILENAME} !-f \n 
				RewriteCond %{REQUEST_FILENAME} !-d \n 
				RewriteRule . /index.php [L] \n 
				</IfModule> \n 
				# END WordPress \n " ; file_put_contents ( $ htaccessFile , $ htaccess_contents ) ; unset ( $ htaccess_contents ) ; } pb_backupbuddy :: status ( 'message' , 'Migrating `' . basename ( $ htaccessFile ) . '` file...' ) ; $ oldurl = strtolower ( $ this -> _state [ 'dat' ] [ 'siteurl' ] ) ; $ oldurl = str_replace ( '/' , '\\' , $ oldurl ) ; $ oldurl = str_replace ( 'http:\\' , '' , $ oldurl ) ; $ oldurl = trim ( $ oldurl , '\\' ) ; $ oldurl = explode ( '\\' , $ oldurl ) ; $ oldurl [ 0 ] = '' ; $ old_path = implode ( '/' , $ oldurl ) ; $ newurl = strtolower ( $ this -> _state [ 'siteurl' ] ) ; $ newurl = str_replace ( '/' , '\\' , $ newurl ) ; $ newurl = str_replace ( 'http:\\' , '' , $ newurl ) ; $ newurl = trim ( $ newurl , '\\' ) ; $ newurl = explode ( '\\' , $ newurl ) ; $ newurl [ 0 ] = '' ; pb_backupbuddy :: status ( 'message' , 'Checking `' . basename ( $ htaccessFile ) . '` file.' ) ; if ( $ newurl !== $ oldurl ) { pb_backupbuddy :: status ( 'message' , 'URL directory has changed. Updating from `' . implode ( '/' , $ oldurl ) . '` to `' . implode ( '/' , $ newurl ) . '`.' ) ; } $ rewrite_lines = array ( ) ; $ got_rewrite = false ; $ rewrite_path = implode ( '/' , $ newurl ) ; $ file_array = file ( $ htaccessFile ) ; $ htaccessNeedsUpdating = false ; foreach ( ( array ) $ file_array as $ line_number => $ line ) { if ( $ got_rewrite == true ) { if ( strstr ( $ line , 'END WordPress' ) ) { $ got_rewrite = false ; $ rewrite_lines [ ] = $ line ; } else { if ( $ newurl !== $ oldurl ) { if ( strstr ( $ line , 'RewriteBase' ) ) { $ rewrite_lines [ ] = 'RewriteBase ' . $ rewrite_path . '/' . " \n " ; $ htaccessNeedsUpdating = true ; } elseif ( strstr ( $ line , 'RewriteRule' ) ) { if ( strstr ( $ line , '^index\.php$' ) ) { $ rewrite_lines [ ] = $ line ; pb_backupbuddy :: status ( 'details' , '.htaccess ^index\.php$ detected. Leaving as is.' ) ; } elseif ( ! strstr ( $ line , 'RewriteRule . ' ) ) { $ new_line = str_replace ( $ old_path , $ rewrite_path , $ line ) ; $ rewrite_lines [ ] = $ new_line ; $ htaccessNeedsUpdating = true ; if ( $ new_line != $ line ) { pb_backupbuddy :: status ( 'message' , '.htaccess line changed from `' . trim ( $ line ) . '` to `' . trim ( $ new_line ) . '`.' ) ; } } else { $ rewrite_lines [ ] = 'RewriteRule . ' . $ rewrite_path . '/index.php' . " \n " ; $ htaccessNeedsUpdating = true ; } } else { $ rewrite_lines [ ] = $ line ; if ( false !== strstr ( $ line , 'RewriteRule . ' ) ) { if ( $ old_path !== $ rewrite_path ) { pb_backupbuddy :: status ( 'warning' , 'User-defined RewriteRule found and WordPress path has changed so this rule MAY need manually updated by you to function properly.  Line: "' . $ line . '".' ) ; } } } } } } else { if ( strstr ( $ line , 'BEGIN WordPress' ) ) { $ got_rewrite = true ; } if ( strstr ( $ line , 'AddHandler' ) ) { $ message = 'An AddHandler directive has been found in your .htaccess file. This could result in WordPress and PHP not running properly if configured improperly, especially when migrating to a new server. If you encounter problems such as an Internal Server Error or Error 500, try removing this line from your .htaccess file.  Line: "' . $ line . '".' ; pb_backupbuddy :: status ( 'warning' , $ message ) ; pb_backupbuddy :: alert ( $ message ) ; } else { $ rewrite_lines [ ] = $ line ; } } } if ( true === $ htaccessNeedsUpdating ) { if ( file_exists ( $ htaccessFile ) && ( ! is_writable ( $ htaccessFile ) ) ) { pb_backupbuddy :: status ( 'warning' , 'Warning #28573: Temp `' . basename ( $ htaccessFile ) . '` file shows to be unwritable. Attempting to override permissions temporarily.' ) ; $ oldPerms = ( fileperms ( $ htaccessFile ) & 0777 ) ; @ chmod ( $ htaccessFile , 0644 ) ; if ( ! is_writable ( $ htaccessFile ) ) { pb_backupbuddy :: status ( 'error' , 'Error #9020: Unable to write to `' . basename ( $ htaccessFile ) . '` file. Verify permissions.' ) ; pb_backupbuddy :: alert ( 'Warning: Unable to write to temporary .htaccess file. Verify this file has proper write permissions. You may receive 404 Not Found errors on your site if this is not corrected. To fix after migration completes: Log in to your WordPress admin and select Settings: Permalinks from the left menu then save. To manually update, copy/paste the following into your .htaccess file: <textarea>' . implode ( $ rewrite_lines ) . '</textarea>' , '9020' ) ; return false ; } } $ handling = fopen ( $ htaccessFile , 'w' ) ; fwrite ( $ handling , implode ( $ rewrite_lines ) ) ; fclose ( $ handling ) ; unset ( $ handling ) ; if ( isset ( $ oldPerms ) ) { @ chmod ( $ htaccessFile , $ oldPerms ) ; } pb_backupbuddy :: status ( 'message' , 'Migrated `' . basename ( $ htaccessFile ) . '` file. It will be renamed back to `.htaccess` on the final step.' ) ; } else { pb_backupbuddy :: status ( 'message' , 'No changes needed for `' . basename ( $ htaccessFile ) . '` file.' ) ; } return true ; }
migrateHtaccess()
      Migrates .htaccess.bb_temp file if it exists.
migrateHtaccess () Migrates . htaccess . bb_temp file if it exists .
// If no .htaccess.bb_temp file exists then create a basic default one then migrate that as needed. @since 2.2.32. // Default .htaccess file. // Loop through .htaccess lines, updating as needed. // In a WordPress section. // End of a WordPress block so stop replacing. // Captures end of WordPress block. // RewriteBase // RewriteRule // Handle new strange rewriterule. Leave as is. // Handle what is probably a user generated rule - better detection needed. // Normal spot. // User custom rewriterule we did not update. // Captures everything inside WordPress block we arent modifying. // RewriteRule, warn user potentially if path may need changed. // Outside a WordPress section. // Beginning of a WordPress block so start replacing. // Has AddHandler. Skip keeping this line -- if importbuddy is running now then we do not need this most likely. // Captures everything outside of WordPress block (except addhandler stuff). // end foreach. // If the URL (domain and/or URL subdirectory ) has changed, then need to update .htaccess.bb_temp file. // Check that we can write to this file (if it already exists). // Try to make writable. // Check if still not writable... // Restore prior permissions if applicable.