Make WordPress Core

Changeset 50700


Ignore:
Timestamp:
04/12/2021 06:17:37 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Use strict comparison in wp-admin/includes/file.php.

Includes minor code layout fixes for better readability.

See #52627.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/file.php

    r50505 r50700  
    147147
    148148    $dir = @opendir( $folder );
     149
    149150    if ( $dir ) {
    150151        while ( ( $file = readdir( $dir ) ) !== false ) {
     
    370371        return new WP_Error( 'missing_file' );
    371372    }
    372     $file = $args['file'];
    373     if ( 0 !== validate_file( $file ) ) {
     373
     374    if ( 0 !== validate_file( $args['file'] ) ) {
    374375        return new WP_Error( 'bad_file' );
    375376    }
     
    378379        return new WP_Error( 'missing_content' );
    379380    }
    380     $content = $args['newcontent'];
    381381
    382382    if ( ! isset( $args['nonce'] ) ) {
    383383        return new WP_Error( 'missing_nonce' );
    384384    }
     385
     386    $file    = $args['file'];
     387    $content = $args['newcontent'];
    385388
    386389    $plugin    = null;
    387390    $theme     = null;
    388391    $real_file = null;
     392
    389393    if ( ! empty( $args['plugin'] ) ) {
    390394        $plugin = $args['plugin'];
     
    418422    } elseif ( ! empty( $args['theme'] ) ) {
    419423        $stylesheet = $args['theme'];
     424
    420425        if ( 0 !== validate_file( $stylesheet ) ) {
    421426            return new WP_Error( 'bad_theme_path' );
     
    495500
    496501    $f = fopen( $real_file, 'w+' );
     502
    497503    if ( false === $f ) {
    498504        return new WP_Error( 'file_not_writable' );
     
    501507    $written = fwrite( $f, $content );
    502508    fclose( $f );
     509
    503510    if ( false === $written ) {
    504511        return new WP_Error( 'unable_to_write', __( 'Unable to write to file.' ) );
     
    576583
    577584        $result = null;
     585
    578586        if ( false === $scrape_result_position ) {
    579587            $result = $loopback_request_failure;
     
    610618
    611619        if ( true !== $result ) {
    612 
    613620            // Roll-back file change.
    614621            file_put_contents( $real_file, $previous_content );
     
    621628                unset( $result['message'] );
    622629            }
     630
    623631            return new WP_Error( 'php_error', $message, $result );
    624632        }
     
    672680
    673681    $fp = @fopen( $temp_filename, 'x' );
     682
    674683    if ( ! $fp && is_writable( $dir ) && file_exists( $temp_filename ) ) {
    675684        return wp_tempnam( $filename, $dir );
    676685    }
     686
    677687    if ( $fp ) {
    678688        fclose( $fp );
     
    847857
    848858    // A correct form post will pass this test.
    849     if ( $test_form && ( ! isset( $_POST['action'] ) || ( $_POST['action'] != $action ) ) ) {
     859    if ( $test_form && ( ! isset( $_POST['action'] ) || $_POST['action'] !== $action ) ) {
    850860        return call_user_func_array( $upload_error_handler, array( &$file, __( 'Invalid form submission.' ) ) );
    851861    }
     862
    852863    // A successful upload will pass this test. It makes no sense to override this one.
    853864    if ( isset( $file['error'] ) && $file['error'] > 0 ) {
     
    875886            );
    876887        }
     888
    877889        return call_user_func_array( $upload_error_handler, array( &$file, $error_msg ) );
    878890    }
     
    889901            $file['name'] = $proper_filename;
    890902        }
     903
    891904        if ( ( ! $type || ! $ext ) && ! current_user_can( 'unfiltered_upload' ) ) {
    892905            return call_user_func_array( $upload_error_handler, array( &$file, __( 'Sorry, this file type is not permitted for security reasons.' ) ) );
    893906        }
     907
    894908        if ( ! $type ) {
    895909            $type = $file['type'];
     
    944958                $error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];
    945959            }
     960
    946961            return $upload_error_handler(
    947962                $file,
     
    10791094    $tmpfname = wp_tempnam( $url_filename );
    10801095    if ( ! $tmpfname ) {
    1081         return new WP_Error( 'http_no_file', __( 'Could not create Temporary file.' ) );
     1096        return new WP_Error( 'http_no_file', __( 'Could not create temporary file.' ) );
    10821097    }
    10831098
     
    10981113    $response_code = wp_remote_retrieve_response_code( $response );
    10991114
    1100     if ( 200 != $response_code ) {
     1115    if ( 200 !== $response_code ) {
    11011116        $data = array(
    11021117            'code' => $response_code,
     
    11051120        // Retrieve a sample of the response body for debugging purposes.
    11061121        $tmpf = fopen( $tmpfname, 'rb' );
     1122
    11071123        if ( $tmpf ) {
    11081124            /**
     
    11161132             */
    11171133            $response_size = apply_filters( 'download_url_error_max_body_size', KB_IN_BYTES );
    1118             $data['body']  = fread( $tmpf, $response_size );
     1134
     1135            $data['body'] = fread( $tmpf, $response_size );
    11191136            fclose( $tmpf );
    11201137        }
    11211138
    11221139        unlink( $tmpfname );
     1140
    11231141        return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ), $data );
    11241142    }
    11251143
    11261144    $content_md5 = wp_remote_retrieve_header( $response, 'content-md5' );
     1145
    11271146    if ( $content_md5 ) {
    11281147        $md5_check = verify_file_md5( $tmpfname, $content_md5 );
     1148
    11291149        if ( is_wp_error( $md5_check ) ) {
    11301150            unlink( $tmpfname );
     
    11421162         * @param string[] $hostnames List of hostnames.
    11431163         */
    1144         $signed_hostnames       = apply_filters( 'wp_signature_hosts', array( 'wordpress.org', 'downloads.wordpress.org', 's.w.org' ) );
     1164        $signed_hostnames = apply_filters( 'wp_signature_hosts', array( 'wordpress.org', 'downloads.wordpress.org', 's.w.org' ) );
     1165
    11451166        $signature_verification = in_array( parse_url( $url, PHP_URL_HOST ), $signed_hostnames, true );
    11461167    }
     
    11491170    if ( $signature_verification ) {
    11501171        $signature = wp_remote_retrieve_header( $response, 'x-content-signature' );
     1172
    11511173        if ( ! $signature ) {
    11521174            // Retrieve signatures from a file if the header wasn't included.
     
    12261248 */
    12271249function verify_file_md5( $filename, $expected_md5 ) {
    1228     if ( 32 == strlen( $expected_md5 ) ) {
     1250    if ( 32 === strlen( $expected_md5 ) ) {
    12291251        $expected_raw_md5 = pack( 'H*', $expected_md5 );
    1230     } elseif ( 24 == strlen( $expected_md5 ) ) {
     1252    } elseif ( 24 === strlen( $expected_md5 ) ) {
    12311253        $expected_raw_md5 = base64_decode( $expected_md5 );
    12321254    } else {
     
    12881310        // Sodium_Compat isn't compatible with PHP 7.2.0~7.2.2 due to a bug in the PHP Opcache extension, bail early as it'll fail.
    12891311        // https://bugs.php.net/bug.php?id=75938
    1290 
    12911312        return new WP_Error(
    12921313            'signature_verification_unsupported',
     
    13021323            )
    13031324        );
    1304 
    13051325    }
    13061326
     
    15371557
    15381558    $zopen = $z->open( $file, ZIPARCHIVE::CHECKCONS );
     1559
    15391560    if ( true !== $zopen ) {
    15401561        return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), array( 'ziparchive_error' => $zopen ) );
     
    15451566    for ( $i = 0; $i < $z->numFiles; $i++ ) {
    15461567        $info = $z->statIndex( $i );
     1568
    15471569        if ( ! $info ) {
    15481570            return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) );
     
    15781600    if ( wp_doing_cron() ) {
    15791601        $available_space = @disk_free_space( WP_CONTENT_DIR );
     1602
    15801603        if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space ) {
    1581             return new WP_Error( 'disk_full_unzip_file', __( 'Could not copy files. You may have run out of disk space.' ), compact( 'uncompressed_size', 'available_space' ) );
     1604            return new WP_Error(
     1605                'disk_full_unzip_file',
     1606                __( 'Could not copy files. You may have run out of disk space.' ),
     1607                compact( 'uncompressed_size', 'available_space' )
     1608            );
    15821609        }
    15831610    }
    15841611
    15851612    $needed_dirs = array_unique( $needed_dirs );
     1613
    15861614    foreach ( $needed_dirs as $dir ) {
    15871615        // Check the parent folders of the folders all exist within the creation array.
    1588         if ( untrailingslashit( $to ) == $dir ) { // Skip over the working directory, we know this exists (or will exist).
     1616        if ( untrailingslashit( $to ) === $dir ) { // Skip over the working directory, we know this exists (or will exist).
    15891617            continue;
    15901618        }
     1619
    15911620        if ( strpos( $dir, $to ) === false ) { // If the directory is not within the working directory, skip it.
    15921621            continue;
     
    15941623
    15951624        $parent_folder = dirname( $dir );
    1596         while ( ! empty( $parent_folder ) && untrailingslashit( $to ) != $parent_folder && ! in_array( $parent_folder, $needed_dirs, true ) ) {
     1625
     1626        while ( ! empty( $parent_folder )
     1627            && untrailingslashit( $to ) !== $parent_folder
     1628            && ! in_array( $parent_folder, $needed_dirs, true )
     1629        ) {
    15971630            $needed_dirs[] = $parent_folder;
    15981631            $parent_folder = dirname( $parent_folder );
    15991632        }
    16001633    }
     1634
    16011635    asort( $needed_dirs );
    16021636
     
    16121646    for ( $i = 0; $i < $z->numFiles; $i++ ) {
    16131647        $info = $z->statIndex( $i );
     1648
    16141649        if ( ! $info ) {
    16151650            return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) );
     
    16301665
    16311666        $contents = $z->getFromIndex( $i );
     1667
    16321668        if ( false === $contents ) {
    16331669            return new WP_Error( 'extract_failed_ziparchive', __( 'Could not extract file from archive.' ), $info['name'] );
     
    17051741    if ( wp_doing_cron() ) {
    17061742        $available_space = @disk_free_space( WP_CONTENT_DIR );
     1743
    17071744        if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space ) {
    1708             return new WP_Error( 'disk_full_unzip_file', __( 'Could not copy files. You may have run out of disk space.' ), compact( 'uncompressed_size', 'available_space' ) );
     1745            return new WP_Error(
     1746                'disk_full_unzip_file',
     1747                __( 'Could not copy files. You may have run out of disk space.' ),
     1748                compact( 'uncompressed_size', 'available_space' )
     1749            );
    17091750        }
    17101751    }
    17111752
    17121753    $needed_dirs = array_unique( $needed_dirs );
     1754
    17131755    foreach ( $needed_dirs as $dir ) {
    17141756        // Check the parent folders of the folders all exist within the creation array.
    1715         if ( untrailingslashit( $to ) == $dir ) { // Skip over the working directory, we know this exists (or will exist).
     1757        if ( untrailingslashit( $to ) === $dir ) { // Skip over the working directory, we know this exists (or will exist).
    17161758            continue;
    17171759        }
     1760
    17181761        if ( strpos( $dir, $to ) === false ) { // If the directory is not within the working directory, skip it.
    17191762            continue;
     
    17211764
    17221765        $parent_folder = dirname( $dir );
    1723         while ( ! empty( $parent_folder ) && untrailingslashit( $to ) != $parent_folder && ! in_array( $parent_folder, $needed_dirs, true ) ) {
     1766
     1767        while ( ! empty( $parent_folder )
     1768            && untrailingslashit( $to ) !== $parent_folder
     1769            && ! in_array( $parent_folder, $needed_dirs, true )
     1770        ) {
    17241771            $needed_dirs[] = $parent_folder;
    17251772            $parent_folder = dirname( $parent_folder );
    17261773        }
    17271774    }
     1775
    17281776    asort( $needed_dirs );
    17291777
     
    17561804        }
    17571805    }
     1806
    17581807    return true;
    17591808}
     
    17951844                // If copy failed, chmod file to 0644 and try again.
    17961845                $wp_filesystem->chmod( $to . $filename, FS_CHMOD_FILE );
     1846
    17971847                if ( ! $wp_filesystem->copy( $from . $filename, $to . $filename, true, FS_CHMOD_FILE ) ) {
    17981848                    return new WP_Error( 'copy_failed_copy_dir', __( 'Could not copy file.' ), $to . $filename );
     
    18101860            // Generate the $sub_skip_list for the subdirectory as a sub-set of the existing $skip_list.
    18111861            $sub_skip_list = array();
     1862
    18121863            foreach ( $skip_list as $skip_item ) {
    18131864                if ( 0 === strpos( $skip_item, $filename . '/' ) ) {
     
    18171868
    18181869            $result = copy_dir( $from . $filename, $to . $filename, $sub_skip_list );
     1870
    18191871            if ( is_wp_error( $result ) ) {
    18201872                return $result;
     
    19482000
    19492001    // If the directory doesn't exist (wp-content/languages) then use the parent directory as we'll create it.
    1950     if ( WP_LANG_DIR == $context && ! is_dir( $context ) ) {
     2002    if ( WP_LANG_DIR === $context && ! is_dir( $context ) ) {
    19512003        $context = dirname( $context );
    19522004    }
     
    20692121     */
    20702122    $req_cred = apply_filters( 'request_filesystem_credentials', '', $form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership );
     2123
    20712124    if ( '' !== $req_cred ) {
    20722125        return $req_cred;
     
    21372190        $credentials['connection_type'] = 'ftp';
    21382191    }
     2192
    21392193    if ( ! $error
    2140         && ( ( ! empty( $credentials['password'] ) && ! empty( $credentials['username'] ) && ! empty( $credentials['hostname'] ) )
    2141             || ( 'ssh' === $credentials['connection_type'] && ! empty( $credentials['public_key'] ) && ! empty( $credentials['private_key'] ) )
     2194        && ( ! empty( $credentials['hostname'] ) && ! empty( $credentials['username'] ) && ! empty( $credentials['password'] )
     2195            || 'ssh' === $credentials['connection_type'] && ! empty( $credentials['public_key'] ) && ! empty( $credentials['private_key'] )
    21422196        )
    21432197    ) {
     
    21482202        }
    21492203
    2150         unset( $stored_credentials['password'], $stored_credentials['port'], $stored_credentials['private_key'], $stored_credentials['public_key'] );
     2204        unset(
     2205            $stored_credentials['password'],
     2206            $stored_credentials['port'],
     2207            $stored_credentials['private_key'],
     2208            $stored_credentials['public_key']
     2209        );
    21512210
    21522211        if ( ! wp_installing() ) {
     
    21562215        return $credentials;
    21572216    }
     2217
    21582218    $hostname        = isset( $credentials['hostname'] ) ? $credentials['hostname'] : '';
    21592219    $username        = isset( $credentials['username'] ) ? $credentials['username'] : '';
     
    21962256     */
    21972257    $types = apply_filters( 'fs_ftp_connection_types', $types, $credentials, $type, $error, $context );
    2198 
    21992258    ?>
    22002259<form action="<?php echo esc_url( $form_post ); ?>" method="post">
Note: See TracChangeset for help on using the changeset viewer.