Make WordPress Core

Changeset 25304


Ignore:
Timestamp:
09/09/2013 02:42:52 AM (13 years ago)
Author:
dd32
Message:

WP_Filesystem: Ensure that all files are read/written correctly by verifying the return values from fwrite() and using FTP_BINARY mode (ASCII converts line endings as per the spec). See #25237

Location:
trunk/src/wp-admin/includes
Files:
4 edited

Legend:

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

    r23191 r25304  
    6060         * @return bool False upon failure.
    6161         */
    62         function put_contents($file, $contents, $mode = false ) {
    63                 if ( ! ($fp = @fopen($file, 'w')) )
    64                         return false;
    65                 @fwrite($fp, $contents);
    66                 @fclose($fp);
    67                 $this->chmod($file, $mode);
     62        function put_contents( $file, $contents, $mode = false ) {
     63                $fp = @fopen( $file, 'wb' );
     64                if ( ! $fp )
     65                        return false;
     66
     67                $bytes_written = fwrite( $fp, $contents );
     68
     69                fclose( $fp );
     70
     71                if ( false === $bytes_written || $bytes_written != strlen( $contents ) )
     72                        return false;
     73
     74                $this->chmod( $file, $mode );
     75
    6876                return true;
    6977        }
  • trunk/src/wp-admin/includes/class-wp-filesystem-ftpext.php

    r21223 r25304  
    8989        }
    9090
    91         function get_contents($file, $type = '', $resumepos = 0 ) {
    92                 if ( empty($type) )
    93                         $type = FTP_BINARY;
    94 
     91        function get_contents( $file ) {
    9592                $tempfile = wp_tempnam($file);
    9693                $temp = fopen($tempfile, 'w+');
    … …  
    9996                        return false;
    10097
    101                 if ( ! @ftp_fget($this->link, $temp, $file, $type, $resumepos) )
     98                if ( ! @ftp_fget($this->link, $temp, $file, FTP_BINARY ) )
    10299                        return false;
    103100
    … …  
    118115        function put_contents($file, $contents, $mode = false ) {
    119116                $tempfile = wp_tempnam($file);
    120                 $temp = fopen($tempfile, 'w+');
     117                $temp = fopen( $tempfile, 'wb+' );
    121118                if ( ! $temp )
    122119                        return false;
    123120
    124                 fwrite($temp, $contents);
    125                 fseek($temp, 0); //Skip back to the start of the file being written to
    126 
    127                 $type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
    128                 $ret = @ftp_fput($this->link, $file, $temp, $type);
     121                $bytes_written = fwrite( $temp, $contents );
     122                if ( false === $bytes_written || $bytes_written != strlen( $contents ) ) {
     123                        fclose( $temp );
     124                        unlink( $tempfile );
     125                        return false;
     126                }
     127
     128                fseek( $temp, 0 ); // Skip back to the start of the file being written to
     129
     130                $ret = @ftp_fput( $this->link, $file, $temp, FTP_BINARY );
    129131
    130132                fclose($temp);
    … …  
    188190                        return false;
    189191                $content = $this->get_contents($source);
    190                 if ( false === $content)
     192                if ( false === $content )
    191193                        return false;
    192194                return $this->put_contents($destination, $content, $mode);
  • trunk/src/wp-admin/includes/class-wp-filesystem-ftpsockets.php

    r25274 r25304  
    7676                }
    7777
    78                 $this->ftp->SetType(FTP_AUTOASCII);
    79                 $this->ftp->Passive(true);
    80                 $this->ftp->setTimeout(FS_TIMEOUT);
     78                $this->ftp->SetType( FTP_BINARY );
     79                $this->ftp->Passive( true );
     80                $this->ftp->setTimeout( FS_TIMEOUT );
    8181                return true;
    8282        }
    8383
    84         function get_contents($file, $type = '', $resumepos = 0) {
     84        function get_contents( $file ) {
    8585                if ( ! $this->exists($file) )
    8686                        return false;
    87 
    88                 if ( empty($type) )
    89                         $type = FTP_AUTOASCII;
    90                 $this->ftp->SetType($type);
    9187
    9288                $temp = wp_tempnam( $file );
    … …  
    123119                }
    124120
    125                 fwrite($temphandle, $contents);
    126                 fseek($temphandle, 0); //Skip back to the start of the file being written to
    127 
    128                 $type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
    129                 $this->ftp->SetType($type);
     121                $bytes_written = fwrite( $temphandle, $contents );
     122                if ( false === $bytes_written || $bytes_written != strlen( $contents ) ) {
     123                        fclose( $temphandle );
     124                        unlink( $temp );
     125                        return false;
     126                }
     127
     128                fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
    130129
    131130                $ret = $this->ftp->fput($file, $temphandle);
  • trunk/src/wp-admin/includes/class-wp-filesystem-ssh2.php

    r24626 r25304  
    151151        }
    152152
    153         function get_contents($file, $type = '', $resumepos = 0 ) {
     153        function get_contents( $file ) {
    154154                $file = ltrim($file, '/');
    155155                return file_get_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file);
    … …  
    165165                $ret = file_put_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file, $contents);
    166166
     167                if ( $ret !== strlen( $contents ) )
     168                        return false;
     169
    167170                $this->chmod($file, $mode);
    168171
    169                 return false !== $ret;
     172                return true;
    170173        }
    171174
Note: See TracChangeset for help on using the changeset viewer.