Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check file is writable and it exists before unlink #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
24 changes: 19 additions & 5 deletions class-wxr-importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1778,15 +1778,15 @@ protected function fetch_remote_file( $url, $post ) {

// request failed
if ( is_wp_error( $response ) ) {
unlink( $upload['file'] );
$this->unlink_file( $upload['file'] );
return $response;
}

$code = (int) wp_remote_retrieve_response_code( $response );

// make sure the fetch was successful
if ( $code !== 200 ) {
unlink( $upload['file'] );
$this->unlink_file( $upload['file'] );
return new WP_Error(
'import_file_error',
sprintf(
Expand All @@ -1802,25 +1802,39 @@ protected function fetch_remote_file( $url, $post ) {
$headers = wp_remote_retrieve_headers( $response );

if ( isset( $headers['content-length'] ) && $filesize !== (int) $headers['content-length'] ) {
unlink( $upload['file'] );
$this->unlink_file( $upload['file'] );
return new WP_Error( 'import_file_error', __( 'Remote file is incorrect size', 'wordpress-importer' ) );
}

if ( 0 === $filesize ) {
unlink( $upload['file'] );
$this->unlink_file( $upload['file'] );
return new WP_Error( 'import_file_error', __( 'Zero size file downloaded', 'wordpress-importer' ) );
}

$max_size = (int) $this->max_attachment_size();
if ( ! empty( $max_size ) && $filesize > $max_size ) {
unlink( $upload['file'] );
$this->unlink_file( $upload['file'] );
$message = sprintf( __( 'Remote file is too large, limit is %s', 'wordpress-importer' ), size_format( $max_size ) );
return new WP_Error( 'import_file_error', $message );
}

return $upload;
}

/**
* Delete file from the server if it is writable and it exists
*
* @param $file
*
* @return bool
*/
protected function unlink_file( $file ) {
if ( file_exists( $file['file'] ) && is_writable( $file['file'] ) && unlink( $file['file'] ) ) {
return true;
}
return false;
}

protected function post_process() {
// Time to tackle any left-over bits
if ( ! empty( $this->requires_remapping['post'] ) ) {
Expand Down