Make WordPress Core


Ignore:
Timestamp:
06/19/2020 10:34:26 AM (5 years ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Add some space around control structures in WP_Filesystem_* classes for consistency and better readability.

Additionally, synchronize $tempfile and $temphandle variable names in WP_Filesystem_FTPext and WP_Filesystem_ftpsockets.

See #49542.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-filesystem-ftpext.php

    r47808 r48089  
    7070
    7171        $this->options['ssl'] = false;
     72
    7273        if ( isset( $opt['connection_type'] ) && 'ftps' === $opt['connection_type'] ) {
    7374            $this->options['ssl'] = true;
     
    9899                )
    99100            );
     101
    100102            return false;
    101103        }
     
    110112                )
    111113            );
     114
    112115            return false;
    113116        }
     
    115118        // Set the connection to use Passive FTP.
    116119        ftp_pasv( $this->link, true );
     120
    117121        if ( @ftp_get_option( $this->link, FTP_TIMEOUT_SEC ) < FS_TIMEOUT ) {
    118122            @ftp_set_option( $this->link, FTP_TIMEOUT_SEC, FS_TIMEOUT );
     
    132136     */
    133137    public function get_contents( $file ) {
    134         $tempfile = wp_tempnam( $file );
    135         $temp    = fopen( $tempfile, 'w+' );
    136 
    137         if ( ! $temp ) {
     138        $tempfile   = wp_tempnam( $file );
     139        $temphandle = fopen( $tempfile, 'w+' );
     140
     141        if ( ! $temphandle ) {
    138142            unlink( $tempfile );
    139143            return false;
    140144        }
    141145
    142         if ( ! ftp_fget( $this->link, $temp, $file, FTP_BINARY ) ) {
    143             fclose( $temp );
     146        if ( ! ftp_fget( $this->link, $temphandle, $file, FTP_BINARY ) ) {
     147            fclose( $temphandle );
    144148            unlink( $tempfile );
    145149            return false;
    146150        }
    147151
    148         fseek( $temp, 0 ); // Skip back to the start of the file being written to.
     152        fseek( $temphandle, 0 ); // Skip back to the start of the file being written to.
    149153        $contents = '';
    150154
    151         while ( ! feof( $temp ) ) {
    152             $contents .= fread( $temp, 8 * KB_IN_BYTES );
    153         }
    154 
    155         fclose( $temp );
     155        while ( ! feof( $temphandle ) ) {
     156            $contents .= fread( $temphandle, 8 * KB_IN_BYTES );
     157        }
     158
     159        fclose( $temphangle );
    156160        unlink( $tempfile );
     161
    157162        return $contents;
    158163    }
     
    182187     */
    183188    public function put_contents( $file, $contents, $mode = false ) {
    184         $tempfile = wp_tempnam( $file );
    185         $temp    = fopen( $tempfile, 'wb+' );
    186 
    187         if ( ! $temp ) {
     189        $tempfile   = wp_tempnam( $file );
     190        $temphandle = fopen( $tempfile, 'wb+' );
     191
     192        if ( ! $temphandle ) {
    188193            unlink( $tempfile );
    189194            return false;
     
    193198
    194199        $data_length   = strlen( $contents );
    195         $bytes_written = fwrite( $temp, $contents );
     200        $bytes_written = fwrite( $temphandle, $contents );
    196201
    197202        reset_mbstring_encoding();
    198203
    199204        if ( $data_length !== $bytes_written ) {
    200             fclose( $temp );
     205            fclose( $temphandle );
    201206            unlink( $tempfile );
    202207            return false;
    203208        }
    204209
    205         fseek( $temp, 0 ); // Skip back to the start of the file being written to.
    206 
    207         $ret = ftp_fput( $this->link, $file, $temp, FTP_BINARY );
    208 
    209         fclose( $temp );
     210        fseek( $temphandle, 0 ); // Skip back to the start of the file being written to.
     211
     212        $ret = ftp_fput( $this->link, $file, $temphandle, FTP_BINARY );
     213
     214        fclose( $temphandle );
    210215        unlink( $tempfile );
    211216
     
    224229    public function cwd() {
    225230        $cwd = ftp_pwd( $this->link );
     231
    226232        if ( $cwd ) {
    227233            $cwd = trailingslashit( $cwd );
    228234        }
     235
    229236        return $cwd;
    230237    }
     
    268275        if ( $recursive && $this->is_dir( $file ) ) {
    269276            $filelist = $this->dirlist( $file );
     277
    270278            foreach ( (array) $filelist as $filename => $filemeta ) {
    271279                $this->chmod( $file . '/' . $filename, $mode, $recursive );
     
    277285            return (bool) ftp_site( $this->link, sprintf( 'CHMOD %o %s', $mode, $file ) );
    278286        }
     287
    279288        return (bool) ftp_chmod( $this->link, $mode, $file );
    280289    }
     
    290299    public function owner( $file ) {
    291300        $dir = $this->dirlist( $file );
     301
    292302        return $dir[ $file ]['owner'];
    293303    }
     
    303313    public function getchmod( $file ) {
    304314        $dir = $this->dirlist( $file );
     315
    305316        return $dir[ $file ]['permsn'];
    306317    }
     
    316327    public function group( $file ) {
    317328        $dir = $this->dirlist( $file );
     329
    318330        return $dir[ $file ]['group'];
    319331    }
     
    336348            return false;
    337349        }
     350
    338351        $content = $this->get_contents( $source );
     352
    339353        if ( false === $content ) {
    340354            return false;
    341355        }
     356
    342357        return $this->put_contents( $destination, $content, $mode );
    343358    }
     
    374389            return false;
    375390        }
     391
    376392        if ( 'f' === $type || $this->is_file( $file ) ) {
    377393            return ftp_delete( $this->link, $file );
    378394        }
     395
    379396        if ( ! $recursive ) {
    380397            return ftp_rmdir( $this->link, $file );
     
    382399
    383400        $filelist = $this->dirlist( trailingslashit( $file ) );
     401
    384402        if ( ! empty( $filelist ) ) {
    385403            foreach ( $filelist as $delete_file ) {
     
    387405            }
    388406        }
     407
    389408        return ftp_rmdir( $this->link, $file );
    390409    }
     
    431450        $cwd    = $this->cwd();
    432451        $result = @ftp_chdir( $this->link, trailingslashit( $path ) );
     452
    433453        if ( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
    434454            @ftp_chdir( $this->link, $cwd );
    435455            return true;
    436456        }
     457
    437458        return false;
    438459    }
     
    532553    public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) {
    533554        $path = untrailingslashit( $path );
     555
    534556        if ( empty( $path ) ) {
    535557            return false;
     
    539561            return false;
    540562        }
     563
    541564        $this->chmod( $path, $chmod );
     565
    542566        return true;
    543567    }
     
    564588    public function parselisting( $line ) {
    565589        static $is_windows = null;
     590
    566591        if ( is_null( $is_windows ) ) {
    567592            $is_windows = stripos( ftp_systype( $this->link ), 'win' ) !== false;
     
    570595        if ( $is_windows && preg_match( '/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)/', $line, $lucifer ) ) {
    571596            $b = array();
     597
    572598            if ( $lucifer[3] < 70 ) {
    573599                $lucifer[3] += 2000;
     
    575601                $lucifer[3] += 1900; // 4-digit year fix.
    576602            }
     603
    577604            $b['isdir'] = ( '<DIR>' === $lucifer[7] );
     605
    578606            if ( $b['isdir'] ) {
    579607                $b['type'] = 'd';
     
    581609                $b['type'] = 'f';
    582610            }
     611
    583612            $b['size']   = $lucifer[7];
    584613            $b['month']  = $lucifer[1];
     
    592621        } elseif ( ! $is_windows ) {
    593622            $lucifer = preg_split( '/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY );
     623
    594624            if ( $lucifer ) {
    595625                // echo $line."\n";
    596626                $lcount = count( $lucifer );
     627
    597628                if ( $lcount < 8 ) {
    598629                    return '';
    599630                }
     631
    600632                $b           = array();
    601633                $b['isdir']  = 'd' === $lucifer[0][0];
    602634                $b['islink'] = 'l' === $lucifer[0][0];
     635
    603636                if ( $b['isdir'] ) {
    604637                    $b['type'] = 'd';
     
    608641                    $b['type'] = 'f';
    609642                }
     643
    610644                $b['perms']  = $lucifer[0];
    611645                $b['permsn'] = $this->getnumchmodfromh( $b['perms'] );
     
    614648                $b['group']  = $lucifer[3];
    615649                $b['size']   = $lucifer[4];
     650
    616651                if ( 8 == $lcount ) {
    617652                    sscanf( $lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day'] );
    618653                    sscanf( $lucifer[6], '%d:%d', $b['hour'], $b['minute'] );
     654
    619655                    $b['time'] = mktime( $b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year'] );
    620656                    $b['name'] = $lucifer[7];
     
    622658                    $b['month'] = $lucifer[5];
    623659                    $b['day']   = $lucifer[6];
     660
    624661                    if ( preg_match( '/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2 ) ) {
    625662                        $b['year']   = gmdate( 'Y' );
     
    631668                        $b['minute'] = 0;
    632669                    }
     670
    633671                    $b['time'] = strtotime( sprintf( '%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute'] ) );
    634672                    $b['name'] = $lucifer[8];
     
    679717
    680718        $pwd = ftp_pwd( $this->link );
     719
    681720        if ( ! @ftp_chdir( $this->link, $path ) ) { // Can't change to folder = folder doesn't exist.
    682721            return false;
    683722        }
     723
    684724        $list = ftp_rawlist( $this->link, '-a', false );
     725
    685726        @ftp_chdir( $this->link, $pwd );
    686727
     
    690731
    691732        $dirlist = array();
     733
    692734        foreach ( $list as $k => $v ) {
    693735            $entry = $this->parselisting( $v );
     736
    694737            if ( empty( $entry ) ) {
    695738                continue;
     
    712755
    713756        $ret = array();
     757
    714758        foreach ( (array) $dirlist as $struc ) {
    715759            if ( 'd' === $struc['type'] ) {
     
    723767            $ret[ $struc['name'] ] = $struc;
    724768        }
     769
    725770        return $ret;
    726771    }
Note: See TracChangeset for help on using the changeset viewer.