Make WordPress Core

Changeset 8990


Ignore:
Timestamp:
09/26/2008 06:53:57 AM (17 years ago)
Author:
westi
Message:

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

Location:
trunk/wp-admin/includes
Files:
3 edited

Legend:

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

    r8645 r8990  
    167167        return $newmode;
    168168    }
     169
     170    /**
     171    * Determines if the string provided contains binary characters.
     172    *
     173    * @since 2.7
     174    * @package WordPress
     175    * @subpackage WP_Filesystem
     176    *
     177    * @param string $text String to test against
     178    *
     179    */
     180    function is_binary( $text ) {
     181        return (bool) preg_match('|[^\x20-\x7E]|', $text); //chr(32)..chr(127)
     182    }
    169183}
    170184
  • trunk/wp-admin/includes/class-wp-filesystem-ftpext.php

    r8811 r8990  
    2323    var $permission = null;
    2424
    25     var $filetypes = array(
    26                             'php'=>FTP_ASCII,
    27                             'css'=>FTP_ASCII,
    28                             'txt'=>FTP_ASCII,
    29                             'js'=>FTP_ASCII,
    30                             'html'=>FTP_ASCII,
    31                             'htm'=>FTP_ASCII,
    32                             'xml'=>FTP_ASCII,
    33 
    34                             'jpg'=>FTP_BINARY,
    35                             'png'=>FTP_BINARY,
    36                             'gif'=>FTP_BINARY,
    37                             'bmp'=>FTP_BINARY
    38                             );
    39 
    4025    function WP_Filesystem_FTPext($opt='') {
    4126        $this->method = 'ftpext';
     
    10489
    10590    function get_contents($file, $type = '', $resumepos = 0 ){
    106         if( empty($type) ){
    107             $extension = substr(strrchr($file, "."), 1);
    108             $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII;
    109         }
     91        if( empty($type) )
     92            $type = FTP_BINARY;
     93
    11094        $temp = tmpfile();
    11195        if ( ! $temp )
    11296            return false;
     97
    11398        if( ! @ftp_fget($this->link, $temp, $file, $type, $resumepos) )
    11499            return false;
     100
    115101        fseek($temp, 0); //Skip back to the start of the file being written to
    116102        $contents = '';
    117         while (!feof($temp)) {
     103
     104        while ( ! feof($temp) )
    118105            $contents .= fread($temp, 8192);
    119         }
     106
    120107        fclose($temp);
    121108        return $contents;
     
    125112    }
    126113    function put_contents($file, $contents, $type = '' ) {
    127         if( empty($type) ) {
    128             $extension = substr(strrchr($file, "."), 1);
    129             $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII;
    130         }
     114        if( empty($type) )
     115            $type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
     116
    131117        $temp = tmpfile();
    132118        if ( ! $temp )
    133119            return false;
     120
    134121        fwrite($temp, $contents);
    135122        fseek($temp, 0); //Skip back to the start of the file being written to
     123
    136124        $ret = @ftp_fput($this->link, $file, $temp, $type);
     125
    137126        fclose($temp);
    138127        return $ret;
  • trunk/wp-admin/includes/class-wp-filesystem-ftpsockets.php

    r8645 r8990  
    2323    var $permission = null;
    2424
    25     var $filetypes = array(
    26                             'php' => FTP_ASCII,
    27                             'css' => FTP_ASCII,
    28                             'txt' => FTP_ASCII,
    29                             'js'  => FTP_ASCII,
    30                             'html'=> FTP_ASCII,
    31                             'htm' => FTP_ASCII,
    32                             'xml' => FTP_ASCII,
    33 
    34                             'jpg' => FTP_BINARY,
    35                             'png' => FTP_BINARY,
    36                             'gif' => FTP_BINARY,
    37                             'bmp' => FTP_BINARY
    38                             );
    39 
    4025    function WP_Filesystem_ftpsockets($opt = '') {
    4126        $this->method = 'ftpsockets';
     
    10691            return false;
    10792
    108         if( empty($type) ){
    109             $extension = substr(strrchr($file, '.'), 1);
    110             $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_AUTOASCII;
    111         }
     93        if( empty($type) )
     94            $type = FTP_AUTOASCII;
    11295        $this->ftp->SetType($type);
     96
    11397        $temp = wp_tempnam( $file );
     98
    11499        if ( ! $temphandle = fopen($temp, 'w+') )
    115100            return false;
     101
    116102        if ( ! $this->ftp->fget($temphandle, $file) ) {
    117103            fclose($temphandle);
     
    119105            return ''; //Blank document, File does exist, Its just blank.
    120106        }
     107
    121108        fseek($temphandle, 0); //Skip back to the start of the file being written to
    122109        $contents = '';
     110
    123111        while ( ! feof($temphandle) )
    124112            $contents .= fread($temphandle, 8192);
     113
    125114        fclose($temphandle);
    126115        unlink($temp);
     
    133122
    134123    function put_contents($file, $contents, $type = '' ) {
    135         if( empty($type) ){
    136             $extension = substr(strrchr($file, '.'), 1);
    137             $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_AUTOASCII;
    138         }
     124        if( empty($type) )
     125            $type = $this->is_binary($contents) ? FTP_BINARY : FTP_ASCII;
     126
    139127        $this->ftp->SetType($type);
    140128
     
    144132            return false;
    145133        }
     134
    146135        fwrite($temphandle, $contents);
    147136        fseek($temphandle, 0); //Skip back to the start of the file being written to
     137
    148138        $ret = $this->ftp->fput($file, $temphandle);
     139
    149140        fclose($temphandle);
    150141        unlink($temp);
Note: See TracChangeset for help on using the changeset viewer.