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
git-svn-id: https://develop.svn.wordpress.org/trunk@25304 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
d92f3ab536
commit
2f40784d97
|
@ -59,12 +59,20 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base {
|
||||||
* @param int $mode (optional) The file permissions as octal number, usually 0644.
|
* @param int $mode (optional) The file permissions as octal number, usually 0644.
|
||||||
* @return bool False upon failure.
|
* @return bool False upon failure.
|
||||||
*/
|
*/
|
||||||
function put_contents($file, $contents, $mode = false ) {
|
function put_contents( $file, $contents, $mode = false ) {
|
||||||
if ( ! ($fp = @fopen($file, 'w')) )
|
$fp = @fopen( $file, 'wb' );
|
||||||
|
if ( ! $fp )
|
||||||
return false;
|
return false;
|
||||||
@fwrite($fp, $contents);
|
|
||||||
@fclose($fp);
|
$bytes_written = fwrite( $fp, $contents );
|
||||||
$this->chmod($file, $mode);
|
|
||||||
|
fclose( $fp );
|
||||||
|
|
||||||
|
if ( false === $bytes_written || $bytes_written != strlen( $contents ) )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
$this->chmod( $file, $mode );
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -88,17 +88,14 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_contents($file, $type = '', $resumepos = 0 ) {
|
function get_contents( $file ) {
|
||||||
if ( empty($type) )
|
|
||||||
$type = FTP_BINARY;
|
|
||||||
|
|
||||||
$tempfile = wp_tempnam($file);
|
$tempfile = wp_tempnam($file);
|
||||||
$temp = fopen($tempfile, 'w+');
|
$temp = fopen($tempfile, 'w+');
|
||||||
|
|
||||||
if ( ! $temp )
|
if ( ! $temp )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if ( ! @ftp_fget($this->link, $temp, $file, $type, $resumepos) )
|
if ( ! @ftp_fget($this->link, $temp, $file, FTP_BINARY ) )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
fseek($temp, 0); //Skip back to the start of the file being written to
|
fseek($temp, 0); //Skip back to the start of the file being written to
|
||||||
|
@ -117,15 +114,20 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base {
|
||||||
|
|
||||||
function put_contents($file, $contents, $mode = false ) {
|
function put_contents($file, $contents, $mode = false ) {
|
||||||
$tempfile = wp_tempnam($file);
|
$tempfile = wp_tempnam($file);
|
||||||
$temp = fopen($tempfile, 'w+');
|
$temp = fopen( $tempfile, 'wb+' );
|
||||||
if ( ! $temp )
|
if ( ! $temp )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
fwrite($temp, $contents);
|
$bytes_written = fwrite( $temp, $contents );
|
||||||
fseek($temp, 0); //Skip back to the start of the file being written to
|
if ( false === $bytes_written || $bytes_written != strlen( $contents ) ) {
|
||||||
|
fclose( $temp );
|
||||||
|
unlink( $tempfile );
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
|
fseek( $temp, 0 ); // Skip back to the start of the file being written to
|
||||||
$ret = @ftp_fput($this->link, $file, $temp, $type);
|
|
||||||
|
$ret = @ftp_fput( $this->link, $file, $temp, FTP_BINARY );
|
||||||
|
|
||||||
fclose($temp);
|
fclose($temp);
|
||||||
unlink($tempfile);
|
unlink($tempfile);
|
||||||
|
@ -187,7 +189,7 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base {
|
||||||
if ( ! $overwrite && $this->exists($destination) )
|
if ( ! $overwrite && $this->exists($destination) )
|
||||||
return false;
|
return false;
|
||||||
$content = $this->get_contents($source);
|
$content = $this->get_contents($source);
|
||||||
if ( false === $content)
|
if ( false === $content )
|
||||||
return false;
|
return false;
|
||||||
return $this->put_contents($destination, $content, $mode);
|
return $this->put_contents($destination, $content, $mode);
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,20 +75,16 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->ftp->SetType(FTP_AUTOASCII);
|
$this->ftp->SetType( FTP_BINARY );
|
||||||
$this->ftp->Passive(true);
|
$this->ftp->Passive( true );
|
||||||
$this->ftp->setTimeout(FS_TIMEOUT);
|
$this->ftp->setTimeout( FS_TIMEOUT );
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_contents($file, $type = '', $resumepos = 0) {
|
function get_contents( $file ) {
|
||||||
if ( ! $this->exists($file) )
|
if ( ! $this->exists($file) )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if ( empty($type) )
|
|
||||||
$type = FTP_AUTOASCII;
|
|
||||||
$this->ftp->SetType($type);
|
|
||||||
|
|
||||||
$temp = wp_tempnam( $file );
|
$temp = wp_tempnam( $file );
|
||||||
|
|
||||||
if ( ! $temphandle = fopen($temp, 'w+') )
|
if ( ! $temphandle = fopen($temp, 'w+') )
|
||||||
|
@ -122,11 +118,14 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
fwrite($temphandle, $contents);
|
$bytes_written = fwrite( $temphandle, $contents );
|
||||||
fseek($temphandle, 0); //Skip back to the start of the file being written to
|
if ( false === $bytes_written || $bytes_written != strlen( $contents ) ) {
|
||||||
|
fclose( $temphandle );
|
||||||
|
unlink( $temp );
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
|
fseek( $temphandle, 0 ); // Skip back to the start of the file being written to
|
||||||
$this->ftp->SetType($type);
|
|
||||||
|
|
||||||
$ret = $this->ftp->fput($file, $temphandle);
|
$ret = $this->ftp->fput($file, $temphandle);
|
||||||
|
|
||||||
|
|
|
@ -150,7 +150,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_contents($file, $type = '', $resumepos = 0 ) {
|
function get_contents( $file ) {
|
||||||
$file = ltrim($file, '/');
|
$file = ltrim($file, '/');
|
||||||
return file_get_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file);
|
return file_get_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file);
|
||||||
}
|
}
|
||||||
|
@ -164,9 +164,12 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base {
|
||||||
$file = ltrim($file, '/');
|
$file = ltrim($file, '/');
|
||||||
$ret = file_put_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file, $contents);
|
$ret = file_put_contents('ssh2.sftp://' . $this->sftp_link . '/' . $file, $contents);
|
||||||
|
|
||||||
|
if ( $ret !== strlen( $contents ) )
|
||||||
|
return false;
|
||||||
|
|
||||||
$this->chmod($file, $mode);
|
$this->chmod($file, $mode);
|
||||||
|
|
||||||
return false !== $ret;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function cwd() {
|
function cwd() {
|
||||||
|
|
Loading…
Reference in New Issue