Skip to content
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 58 additions & 25 deletions admin/class-boldgrid-backup-admin-restore-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,36 +237,69 @@ public function prepare_restore() {
* @return bool True if permissions were able to be updated successfully.
*/
public function set_writable_permissions( $archive_filepath ) {
global $wp_filesystem;

$zip = new ZipArchive();

if ( $zip->open( $archive_filepath ) ) {
for ( $i = 0; $i < $zip->numFiles; $i++ ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName
$data = $zip->statIndex( $i );

if ( empty( $data['name'] ) ) {
continue;
if ( class_exists( 'ZipArchive' ) ) {
$zip = new ZipArchive();
if ( $zip->open( $archive_filepath ) ) {
for ( $i = 0; $i < $zip->numFiles; $i++ ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName
$data = $zip->statIndex( $i );

if ( empty( $data['name'] ) ) {
continue;
}

if ( $this->change_file_permissions( $data['name'] ) ) {
continue;
} else {
return false;
}
}

$full_path = ABSPATH . $data['name'];

// If the file does not exists, no need to check its permissions.
if ( ! $wp_filesystem->exists( $full_path ) ) {
continue;
}

if ( ! $wp_filesystem->chmod( $full_path ) ) {
$this->errors[] = sprintf(
// translators: 1 The path to a file that cannot be restored due to file permissions.
__( 'Permission denied. Unable to restore the following file: %1$s', 'boldgrid-backup' ),
$full_path
);
return false;
}
} elseif ( class_exists( 'PclZip' ) ) {
$zip = new PclZip( $archive_filepath );
if ( $zip->listContent() ) {
foreach ( $zip->listContent() as $file ) {

if ( empty( $file['filename'] ) ) {
continue;
}

if ( $this->change_file_permissions( $file['filename'] ) ) {
continue;
} else {
return false;
}
}
}
}
return true;
}

/**
* Updates file permission per file
*
* This takes an individual filepath derived from the lists of files
* in an archive, and uses wp_filesystem to set the correct permissions
*
* @since SINCEVERSION
* @param string $file_name name of file to be changed.
* @return bool True if permissions were able to be updated successfully.
*/
public function change_file_permissions( $file_name ) {
global $wp_filesystem;
$full_path = ABSPATH . $file_name;

// If the file does not exists, no need to check its permissions.
if ( ! $wp_filesystem->exists( $full_path ) ) {
return true;
}
if ( ! $wp_filesystem->chmod( $full_path ) ) {
$this->errors[] = sprintf(
// translators: 1 The path to a file that cannot be restored due to file permissions.
__( 'Permission denied. Unable to restore the following file: %1$s', 'boldgrid-backup' ),
$full_path
);
return false;
}
return true;
}

Expand Down