Autodetect binary files for FTP filesystems. Fixes #7568 props DD32.

git-svn-id: http://svn.automattic.com/wordpress/trunk@8990 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
westi 2008-09-26 06:53:57 +00:00
parent 81aebef3b3
commit 121f0754c6
3 changed files with 42 additions and 48 deletions

View File

@ -166,6 +166,20 @@ class WP_Filesystem_Base {
$newmode .= $mode[6] + $mode[7] + $mode[8]; $newmode .= $mode[6] + $mode[7] + $mode[8];
return $newmode; return $newmode;
} }
/**
* Determines if the string provided contains binary characters.
*
* @since 2.7
* @package WordPress
* @subpackage WP_Filesystem
*
* @param string $text String to test against
*
*/
function is_binary( $text ) {
return (bool) preg_match('|[^\x20-\x7E]|', $text); //chr(32)..chr(127)
}
} }
?> ?>

View File

@ -22,21 +22,6 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base {
var $permission = null; var $permission = null;
var $filetypes = array(
'php'=>FTP_ASCII,
'css'=>FTP_ASCII,
'txt'=>FTP_ASCII,
'js'=>FTP_ASCII,
'html'=>FTP_ASCII,
'htm'=>FTP_ASCII,
'xml'=>FTP_ASCII,
'jpg'=>FTP_BINARY,
'png'=>FTP_BINARY,
'gif'=>FTP_BINARY,
'bmp'=>FTP_BINARY
);
function WP_Filesystem_FTPext($opt='') { function WP_Filesystem_FTPext($opt='') {
$this->method = 'ftpext'; $this->method = 'ftpext';
$this->errors = new WP_Error(); $this->errors = new WP_Error();
@ -103,20 +88,22 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base {
} }
function get_contents($file, $type = '', $resumepos = 0 ){ function get_contents($file, $type = '', $resumepos = 0 ){
if( empty($type) ){ if( empty($type) )
$extension = substr(strrchr($file, "."), 1); $type = FTP_BINARY;
$type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII;
}
$temp = tmpfile(); $temp = tmpfile();
if ( ! $temp ) if ( ! $temp )
return false; return false;
if( ! @ftp_fget($this->link, $temp, $file, $type, $resumepos) ) if( ! @ftp_fget($this->link, $temp, $file, $type, $resumepos) )
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
$contents = ''; $contents = '';
while (!feof($temp)) {
while ( ! feof($temp) )
$contents .= fread($temp, 8192); $contents .= fread($temp, 8192);
}
fclose($temp); fclose($temp);
return $contents; return $contents;
} }
@ -124,16 +111,18 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base {
return explode("\n", $this->get_contents($file)); return explode("\n", $this->get_contents($file));
} }
function put_contents($file, $contents, $type = '' ) { function put_contents($file, $contents, $type = '' ) {
if( empty($type) ) { if( empty($type) )
$extension = substr(strrchr($file, "."), 1); $type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
$type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII;
}
$temp = tmpfile(); $temp = tmpfile();
if ( ! $temp ) if ( ! $temp )
return false; return false;
fwrite($temp, $contents); fwrite($temp, $contents);
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
$ret = @ftp_fput($this->link, $file, $temp, $type); $ret = @ftp_fput($this->link, $file, $temp, $type);
fclose($temp); fclose($temp);
return $ret; return $ret;
} }

View File

@ -22,21 +22,6 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
var $permission = null; var $permission = null;
var $filetypes = array(
'php' => FTP_ASCII,
'css' => FTP_ASCII,
'txt' => FTP_ASCII,
'js' => FTP_ASCII,
'html'=> FTP_ASCII,
'htm' => FTP_ASCII,
'xml' => FTP_ASCII,
'jpg' => FTP_BINARY,
'png' => FTP_BINARY,
'gif' => FTP_BINARY,
'bmp' => FTP_BINARY
);
function WP_Filesystem_ftpsockets($opt = '') { function WP_Filesystem_ftpsockets($opt = '') {
$this->method = 'ftpsockets'; $this->method = 'ftpsockets';
$this->errors = new WP_Error(); $this->errors = new WP_Error();
@ -105,23 +90,27 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
if( ! $this->exists($file) ) if( ! $this->exists($file) )
return false; return false;
if( empty($type) ){ if( empty($type) )
$extension = substr(strrchr($file, '.'), 1); $type = FTP_AUTOASCII;
$type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_AUTOASCII;
}
$this->ftp->SetType($type); $this->ftp->SetType($type);
$temp = wp_tempnam( $file ); $temp = wp_tempnam( $file );
if ( ! $temphandle = fopen($temp, 'w+') ) if ( ! $temphandle = fopen($temp, 'w+') )
return false; return false;
if ( ! $this->ftp->fget($temphandle, $file) ) { if ( ! $this->ftp->fget($temphandle, $file) ) {
fclose($temphandle); fclose($temphandle);
unlink($temp); unlink($temp);
return ''; //Blank document, File does exist, Its just blank. return ''; //Blank document, File does exist, Its just blank.
} }
fseek($temphandle, 0); //Skip back to the start of the file being written to fseek($temphandle, 0); //Skip back to the start of the file being written to
$contents = ''; $contents = '';
while ( ! feof($temphandle) ) while ( ! feof($temphandle) )
$contents .= fread($temphandle, 8192); $contents .= fread($temphandle, 8192);
fclose($temphandle); fclose($temphandle);
unlink($temp); unlink($temp);
return $contents; return $contents;
@ -132,10 +121,9 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
} }
function put_contents($file, $contents, $type = '' ) { function put_contents($file, $contents, $type = '' ) {
if( empty($type) ){ if( empty($type) )
$extension = substr(strrchr($file, '.'), 1); $type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
$type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_AUTOASCII;
}
$this->ftp->SetType($type); $this->ftp->SetType($type);
$temp = wp_tempnam( $file ); $temp = wp_tempnam( $file );
@ -143,9 +131,12 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base {
unlink($temp); unlink($temp);
return false; return false;
} }
fwrite($temphandle, $contents); fwrite($temphandle, $contents);
fseek($temphandle, 0); //Skip back to the start of the file being written to fseek($temphandle, 0); //Skip back to the start of the file being written to
$ret = $this->ftp->fput($file, $temphandle); $ret = $this->ftp->fput($file, $temphandle);
fclose($temphandle); fclose($temphandle);
unlink($temp); unlink($temp);
return $ret; return $ret;