Make WordPress Core

Ticket #7690: 7690.5.diff

File 7690.5.diff, 8.0 KB (added by DD32, 18 years ago)

See Note in comments

  • wp-admin/includes/class-wp-filesystem-ssh2.php

     
    99/**
    1010 * WordPress Filesystem Class for implementing SSH2.
    1111 *
     12 * To use this class you must follow these steps for PHP 5.2.6+
     13 *
     14 * @contrib http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/ - Installation Notes
     15 *
     16 * Complie libssh2 (Note: Only 0.14 is officaly working with PHP 5.2.6+ right now.)
     17 *
     18 * cd /usr/src
     19 * wget http://surfnet.dl.sourceforge.net/sourceforge/libssh2/libssh2-0.14.tar.gz
     20 * tar -zxvf libssh2-0.14.tar.gz
     21 * cd libssh2-0.14/
     22 * ./configure
     23 * make all install
     24 *
     25 * Note: No not leave the directory yet!
     26 *
     27 * Enter: pecl install -f ssh2
     28 *
     29 * Copy the ssh.so file it creates to your PHP Module Directory.
     30 * Open up your PHP.INI file and look for where extensions are placed.
     31 * Add in your PHP.ini file: extension=ssh2.so
     32 *
     33 * Restart Apache!
     34 * Check phpinfo() streams to confirm that: ssh2.shell, ssh2.exec, ssh2.tunnel, ssh2.scp, ssh2.sftp  exist.
     35 *
     36 *
    1237 * @since 2.7
    1338 * @package WordPress
    1439 * @subpackage Filesystem
     
    2348        var $errors = array();
    2449        var $options = array();
    2550
    26         var $permission = null;
     51        var $permission = 0644;
    2752
    28         var $filetypes = array(
    29                                                         'php'=>FTP_ASCII,
    30                                                         'css'=>FTP_ASCII,
    31                                                         'txt'=>FTP_ASCII,
    32                                                         'js'=>FTP_ASCII,
    33                                                         'html'=>FTP_ASCII,
    34                                                         'htm'=>FTP_ASCII,
    35                                                         'xml'=>FTP_ASCII,
    36 
    37                                                         'jpg'=>FTP_BINARY,
    38                                                         'png'=>FTP_BINARY,
    39                                                         'gif'=>FTP_BINARY,
    40                                                         'bmp'=>FTP_BINARY
    41                                                         );
    42 
    4353        function WP_Filesystem_SSH2($opt='') {
    4454                $this->method = 'ssh2';
    4555                $this->errors = new WP_Error();
     
    7181                        $this->options['username'] = $opt['username'];
    7282
    7383                if ( empty ($opt['password']) )
    74                         $this->errors->add('empty_password', __('SSH password is required'));
     84                        $this->errors->add('empty_password', __('SSH2 password is required'));
    7585                else
    7686                        $this->options['password'] = $opt['password'];
    7787
     
    96106       
    97107        function run_command($link, $command, $returnbool = false) {
    98108                $this->debug("run_command(".$command.");");
    99                 if(!($stream = @ssh2_exec( $link, $command ))) {
    100             $this->errors->add('command', sprintf(__('Unable to preform command: %s'), $command));
    101         } else {
    102             stream_set_blocking( $stream, true );
     109                if( ! ($stream = @ssh2_exec( $link, $command )) ) {
     110                        $this->errors->add('command', sprintf(__('Unable to preform command: %s'), $command));
     111                } else {
     112                        stream_set_blocking( $stream, true );
    103113                        $time_start = time();
    104             $data = "";
    105                         while( true ) {
    106                             if( (time()-$time_start) > $this->timeout ){
     114                        $data = '';
     115                        while ( true ) {
     116                            if( (time()-$time_start) > $this->timeout ) {
    107117                                $this->errors->add('command', sprintf(__('Connection to the server has timeout after %s seconds.'), $this->timeout));
    108118                                break;
    109119                            }
    110                     while( $buf = fread( $stream, strlen($stream) ) ){
    111                         $data .= $buf;
    112                     }
     120                                while( $buf = fread( $stream, strlen($stream) ) )
     121                                        $data .= $buf;
    113122                        }
    114123            fclose($stream);
    115             if (($returnbool) && ($data)) {
     124            if ( $returnbool && $data ) {
    116125                $this->debug("Data: " . print_r($data, true) . " Returning: True");
    117126                return true;
    118             } elseif (($returnbool) && (!$data)) {
     127            } elseif ( $returnbool && ! $data ) {
    119128                $this->debug("Data: " . print_r($data, true) . " Returning: False");
    120129                return false;
    121130            } else {
     
    134143        }
    135144
    136145        function setDefaultPermissions($perm) {
    137                 $this->permission = $perm;
     146                if ( $perm )
     147                        $this->permission = $perm;
    138148        }
    139149
    140150        function get_contents($file, $type = '', $resumepos = 0 ){
    141                 if( empty($type) ){
    142                         $extension = substr(strrchr($file, "."), 1);
    143                         $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII;
    144                 }
    145                 $temp = tmpfile();
    146                 if ( ! $temp )
     151                if ( ! $tempfile = wp_tempnam( basename($file) ) );
    147152                        return false;
    148                 if( ! @ssh2_scp_recv($this->link, $temp, $file) )
     153                if( ! ssh2_scp_recv($this->link, $file, $tempfile) )
    149154                        return false;
    150                 fseek($temp, 0); //Skip back to the start of the file being written to
    151                 $contents = '';
    152                 while (!feof($temp)) {
    153                         $contents .= fread($temp, 8192);
    154                 }
    155                 fclose($temp);
     155                $contents = file_get_contents($tempfile);
     156                unlink($tempfile);
    156157                return $contents;
    157158        }
    158159       
     
    161162        }
    162163       
    163164        function put_contents($file, $contents, $type = '' ) {
    164                 if( empty($type) ) {
    165                         $extension = substr(strrchr($file, "."), 1);
    166                         $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII;
    167                 }
    168                 $temp = tmpfile();
     165                $tempfile = wp_tempnam( basename($file) );
     166                $temp = fopen($tempfile, 'w');
    169167                if ( ! $temp )
    170168                        return false;
    171169                fwrite($temp, $contents);
    172                 fseek($temp, 0); //Skip back to the start of the file being written to
    173                 $ret = @ssh2_scp_send($this->link, $file, $temp, $type);
    174170                fclose($temp);
     171                $ret = ssh2_scp_send($this->link, $tempfile, $file, $this->permission);
     172                unlink($tempfile);
    175173                return $ret;
    176174        }
    177175       
    178176        function cwd() {
    179                 $cwd = $this->run_command($this->link, "pwd");
     177                $cwd = $this->run_command($this->link, 'pwd');
    180178                if( $cwd )
    181179                        $cwd = trailingslashit($cwd);
    182180                return $cwd;
    183181        }
    184182       
    185183        function chdir($dir) {
    186                 if ($this->run_command($this->link, "cd " . $dir, true)) {
    187                         return true;
    188                 }
    189                 return false;
     184                return ( $this->run_command($this->link, 'cd ' . $dir, true) );
    190185        }
    191186       
    192187        function chgrp($file, $group, $recursive = false ) {
     
    243238                return @ssh2_sftp_rename($this->link, $source, $destination);
    244239        }
    245240
    246         function delete($file, $recursive=false) {
     241        function delete($file, $recursive = false) {
     242                //Note: Inverse boolean here, Empty string("false") returned on true,string error on failure ("true")
    247243                if ( $this->is_file($file) )
    248                         return @ssh2_sftp_unlink($this->link, $file);
     244                        return ! $this->run_command($this->link, 'rm ' . $file, true);
    249245                if ( !$recursive )
    250                         return @ssh2_sftp_rmdir($this->link, $file);
     246                         return ! $this->run_command($this->link, 'rmdir ' . $file);
    251247                $filelist = $this->dirlist($file);
    252248                foreach ((array) $filelist as $filename => $fileinfo) {
    253249                        $this->delete($file . '/' . $filename, $recursive);
    254250                }
    255                 return @ssh2_sftp_rmdir($this->link, $file);
     251                return ! $this->run_command($this->link, 'rmdir ' . $file, true);
    256252        }
    257253
    258254        function exists($file) {
     
    270266                $cwd = $this->cwd();
    271267                $result = $this->run_command($this->link, sprintf('cd %s', $path), true);
    272268                if( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
    273                         // @todo: use ssh2_exec
    274                         @ftp_chdir($this->link, $cwd);
     269                        $this->chdir($cwd);
    275270                        return true;
    276271                }
    277272                return false;
     
    306301        }
    307302       
    308303        function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
    309                 if( !@ssh2_sftp_mkdir($this->link, $path) )
     304                if( $this->run_command($this->link, 'mkdir ' . $path, true) ) //true on failure
    310305                        return false;
    311306                if( $chmod )
    312307                        $this->chmod($path, $chmod);
     
    448443        }
    449444}
    450445
    451 ?>
    452  No newline at end of file
     446?>
  • wp-admin/includes/file.php

     
    480480                unlink($temp_file);
    481481        }
    482482
    483         if ( isset($args['connection_type']) && 'ssh' == $args['connection_type'] ) {
    484                 $method = 'SSH2';
    485                 return apply_filters('filesystem_method', $method);
    486         }
     483        if ( ! $method && isset($args['connection_type']) && 'ssh' == $args['connection_type'] && extension_loaded('ssh2') ) $method = 'ssh2';
    487484
    488485        if ( ! $method && extension_loaded('ftp') ) $method = 'ftpext';
    489486        if ( ! $method && ( extension_loaded('sockets') || function_exists('fsockopen') ) ) $method = 'ftpsockets'; //Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread