Make WordPress Core

Ticket #7690: 7690.6.diff

File 7690.6.diff, 15.8 KB (added by ShaneF, 18 years ago)

see note above

  • 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
     
    1944        var $debugtest = true;  //      this is my var that will output the text when debuggin this class
    2045       
    2146        var $link;
    22         var $timeout = 5;
     47        /*
     48         * This is the timeout value for ssh results to comeback.
     49         * Slower servers might need this incressed, but this number otherwise should not change.
     50         *
     51         * @parm $timeout int
     52         *
     53         */
     54        var $timeout = 15;
    2355        var $errors = array();
    2456        var $options = array();
    2557
    26         var $permission = null;
     58        var $permission = 0644;
    2759
    2860        var $filetypes = array(
    2961                                                        'php'=>FTP_ASCII,
     
    71103                        $this->options['username'] = $opt['username'];
    72104
    73105                if ( empty ($opt['password']) )
    74                         $this->errors->add('empty_password', __('SSH password is required'));
     106                        $this->errors->add('empty_password', __('SSH2 password is required'));
    75107                else
    76108                        $this->options['password'] = $opt['password'];
    77109
     
    95127        }
    96128       
    97129        function run_command($link, $command, $returnbool = false) {
    98                 $this->debug("run_command(".$command.");");
    99                 if(!($stream = @ssh2_exec( $link, $command ))) {
     130                //$this->debug("run_command(".$command.",".$returnbool.");");
     131                if(!($stream = @ssh2_exec( $link, $command . "; echo \"__COMMAND_FINISHED__\";"))) {
    100132            $this->errors->add('command', sprintf(__('Unable to preform command: %s'), $command));
    101133        } else {
    102134            stream_set_blocking( $stream, true );
    103135                        $time_start = time();
    104             $data = "";
     136            $data = null;
    105137                        while( true ) {
     138                            if (strpos($data,"__COMMAND_FINISHED__") !== false){
     139                                break;  //      the command has finshed!
     140                            }
    106141                            if( (time()-$time_start) > $this->timeout ){
    107142                                $this->errors->add('command', sprintf(__('Connection to the server has timeout after %s seconds.'), $this->timeout));
    108                                 break;
     143                                        unset($this->link);     //      close connection
     144                                return false;
    109145                            }
    110146                    while( $buf = fread( $stream, strlen($stream) ) ){
    111147                        $data .= $buf;
    112148                    }
    113149                        }
    114150            fclose($stream);
    115             if (($returnbool) && ($data)) {
    116                 $this->debug("Data: " . print_r($data, true) . " Returning: True");
     151                        $data = str_replace("__COMMAND_FINISHED__", "", $data);
     152                        //$this->debug("run_command(".$command."); --> \$data = " . $data);
     153            if (($returnbool) && ( (int) $data )) {
     154                $this->debug("Data. Returning: True");
    117155                return true;
    118             } elseif (($returnbool) && (!$data)) {
    119                 $this->debug("Data: " . print_r($data, true) . " Returning: False");
     156            } elseif (($returnbool) && (! (int) $data )) {
     157                $this->debug("Data. Returning: False");
    120158                return false;
    121159            } else {
    122                 $this->debug("Data: " . print_r($data, true));
     160                $this->debug("Data Only.");
    123161                return $data;
    124162            }
    125163        }
     164                return false;
    126165        }
    127166
    128167        function debug($text)
    129168        {
    130169                if ($this->debugtest)
    131170                {
    132                         echo $text . "<br/>";
     171                        echo "<br/>" . $text . "<br/>";
    133172                }
    134173        }
    135174
    136175        function setDefaultPermissions($perm) {
     176                $this->debug("setDefaultPermissions();");
    137177                $this->permission = $perm;
    138178        }
    139179
    140         function get_contents($file, $type = '', $resumepos = 0 ){
     180        function get_contents($file, $type = '', $resumepos = 0 ) {
     181                $this->debug("get_contents();");
    141182                if( empty($type) ){
    142183                        $extension = substr(strrchr($file, "."), 1);
    143184                        $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII;
     
    157198        }
    158199       
    159200        function get_contents_array($file) {
     201                $this->debug("get_contents_array();");
    160202                return explode("\n", $this->get_contents($file));
    161203        }
    162204       
    163205        function put_contents($file, $contents, $type = '' ) {
     206                $this->debug("put_contents(".$file.",contents,".$type.");");
    164207                if( empty($type) ) {
    165208                        $extension = substr(strrchr($file, "."), 1);
    166209                        $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII;
    167210                }
    168211                $temp = tmpfile();
    169                 if ( ! $temp )
     212                if ( ! $temp ) {
    170213                        return false;
     214                }
    171215                fwrite($temp, $contents);
    172216                fseek($temp, 0); //Skip back to the start of the file being written to
    173                 $ret = @ssh2_scp_send($this->link, $file, $temp, $type);
     217                // @todo: fix me
     218                $ret = @ssh2_scp_send($this->link, $temp, $file, 0644);
    174219                fclose($temp);
    175220                return $ret;
    176221        }
    177222       
    178223        function cwd() {
     224//              $this->debug("cwd();");
    179225                $cwd = $this->run_command($this->link, "pwd");
    180226                if( $cwd )
    181227                        $cwd = trailingslashit($cwd);
     
    183229        }
    184230       
    185231        function chdir($dir) {
     232//              $this->debug("chdir();");
    186233                if ($this->run_command($this->link, "cd " . $dir, true)) {
    187234                        return true;
    188235                }
     
    190237        }
    191238       
    192239        function chgrp($file, $group, $recursive = false ) {
     240                $this->debug("chgrp();");
    193241                return false;
    194242        }
    195243       
    196244        function chmod($file, $mode = false, $recursive = false) {
     245                $this->debug("chmod();");
    197246                if( ! $mode )
    198247                        $mode = $this->permission;
    199248                if( ! $mode )
     
    212261        }
    213262       
    214263        function chown($file, $owner, $recursive = false ) {
     264                $this->debug("chown();");
    215265                return false;
    216266        }
    217267       
    218268        function owner($file) {
     269                $this->debug("owner();");
    219270                $dir = $this->dirlist($file);
    220271                return $dir[$file]['owner'];
    221272        }
    222273       
    223274        function getchmod($file) {
     275                $this->debug("getchmod();");
    224276                $dir = $this->dirlist($file);
    225277                return $dir[$file]['permsn'];
    226278        }
    227279       
    228280        function group($file) {
     281                $this->debug("group();");
    229282                $dir = $this->dirlist($file);
    230283                return $dir[$file]['group'];
    231284        }
    232285       
    233286        function copy($source, $destination, $overwrite = false ) {
     287                $this->debug("copy();");
    234288                if( ! $overwrite && $this->exists($destination) )
    235289                        return false;
    236290                $content = $this->get_contents($source);
     
    240294        }
    241295       
    242296        function move($source, $destination, $overwrite = false) {
     297                $this->debug("move();");
    243298                return @ssh2_sftp_rename($this->link, $source, $destination);
    244299        }
    245300
    246         function delete($file, $recursive=false) {
    247                 if ( $this->is_file($file) )
    248                         return @ssh2_sftp_unlink($this->link, $file);
    249                 if ( !$recursive )
    250                         return @ssh2_sftp_rmdir($this->link, $file);
    251                 $filelist = $this->dirlist($file);
    252                 foreach ((array) $filelist as $filename => $fileinfo) {
    253                         $this->delete($file . '/' . $filename, $recursive);
     301        function delete($path, $recursive = false) {
     302                $this->debug("delete(".$path.");");
     303                $sftp = ssh2_sftp($this->link);
     304                if ($recursive) {
     305                        $this->debug("Delete Directory: " . $path);
     306                        return @ssh2_sftp_rmdir($sftp, $path);
     307                } else {
     308                        $this->debug("Delete File " . $path);
     309                        return @ssh2_sftp_unlink($sftp, $path);
    254310                }
    255                 return @ssh2_sftp_rmdir($this->link, $file);
    256311        }
    257312
    258313        function exists($file) {
     314                $this->debug("exists();");
    259315                $list = $this->run_command($this->link, sprintf('ls -la %s', $file));
    260316                if( ! $list )
    261317                        return false;
     
    263319        }
    264320       
    265321        function is_file($file) {
    266                 return $this->is_dir($file) ? false : true;
     322                return $this->is_dir($file) ? true : false;
    267323        }
    268324       
    269325        function is_dir($path) {
    270326                $cwd = $this->cwd();
    271                 $result = $this->run_command($this->link, sprintf('cd %s', $path), true);
    272                 if( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
    273                         // @todo: use ssh2_exec
    274                         @ftp_chdir($this->link, $cwd);
     327                $result = $this->run_command($this->link, "cd " . $path, true);
     328                if ( $result && $path == $this->cwd() || $this->cwd() != $cwd ) {
     329                        $this->run_command($this->link, sprintf('cd %s', $path), true);
    275330                        return true;
    276331                }
    277332                return false;
    278333        }
    279334       
    280335        function is_readable($file) {
     336                $this->debug("is_readable();");
    281337                //Get dir list, Check if the file is writable by the current user??
    282338                return true;
    283339        }
    284340       
    285341        function is_writable($file) {
     342                $this->debug("is_writable();");
    286343                //Get dir list, Check if the file is writable by the current user??
    287344                return true;
    288345        }
    289346       
    290347        function atime($file) {
     348                $this->debug("atime();");
    291349                return false;
    292350        }
    293351       
    294352        function mtime($file) {
     353                $this->debug("mtime();");
    295354                return; //      i have to look up to see if there is a way in SSH2 to look the modifed date
    296355                //      return ftp_mdtm($this->link, $file);
    297356        }
    298357       
    299358        function size($file) {
     359                $this->debug("size();");
    300360                return; //      i have to look up to see if there is a way in SSH2 to get the file size
    301361                //      return ftp_size($this->link, $file);
    302362        }
    303363       
    304364        function touch($file, $time = 0, $atime = 0) {
     365                $this->debug("touch();");
    305366                return false;
    306367        }
    307368       
    308         function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
    309                 if( !@ssh2_sftp_mkdir($this->link, $path) )
     369        function mkdir($path, $octal = 0755) {
     370                $this->debug("mkdir(".$path.",".$octal.");");
     371                $sftp = ssh2_sftp($this->link);
     372                if (!function_exists('ssh2_sftp_mkdir')) {
     373                        $this->errors->add('function_needed_mkdir', sprintf(__('Your SSH2 Module does not support ssh2_sftp_mkdir')));
     374                }
     375                if( !@ssh2_sftp_mkdir($sftp, $path, $octal, true) && !is_dir($path . "/") ) {
     376                        $this->debug("Directory not created.");
    310377                        return false;
    311                 if( $chmod )
    312                         $this->chmod($path, $chmod);
    313                 if( $chown )
    314                         $this->chown($path, $chown);
    315                 if( $chgrp )
    316                         $this->chgrp($path, $chgrp);
    317                 return true;
     378                } else {
     379                        $this->debug("Directory created.");
     380                        return true;
     381                }
    318382        }
    319383       
    320384        function rmdir($path, $recursive = false) {
     385                $this->debug("rmdir(".$path.",".$recursive."); --> Note: Recursive does not work like MKDIR");
     386                $sftp = ssh2_sftp($this->link);
     387                if (!function_exists('ssh2_sftp_rmdir')) {
     388                        $this->errors->add('function_needed_rmdir', sprintf(__('Your SSH2 Module does not support ssh2_sftp_rmdir')));
     389                }
    321390                if( ! $recursive )
    322391                        return @ssh2_sftp_rmdir($this->link, $path);
    323392
     
    328397        }
    329398
    330399        function parselisting($line) {
     400        $this->debug("parselisting();");
    331401                $is_windows = ($this->OS_remote == FTP_OS_Windows);
    332402                if ($is_windows && preg_match("/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)/", $line, $lucifer)) {
    333403                        $b = array();
     
    390460        }
    391461
    392462        function dirlist($path = '.', $incdot = false, $recursive = false) {
     463                $this->debug("dirlist();");
    393464                if( $this->is_file($path) ) {
    394465                        $limitFile = basename($path);
    395466                        $path = dirname($path) . '/';
     
    442513                return $ret;
    443514        }
    444515
    445         function __destruct(){
    446                 if( $this->link )
    447                         unset($this->link);
     516        function __destruct() {
     517                if ( ( $this->link ) || ( $this->linksftp ) )
     518                        unset($this->link, $this->linksftp);
    448519        }
    449520}
    450521
    451 ?>
    452  No newline at end of file
     522?>
  • wp-admin/includes/file.php

     
    389389        $to = trailingslashit($to);
    390390        $path = explode('/', $to);
    391391        $tmppath = '';
     392       
    392393        for ( $j = 0; $j < count($path) - 1; $j++ ) {
    393                 $tmppath .= $path[$j] . '/';
    394                 if ( ! $fs->is_dir($tmppath) )
    395                         $fs->mkdir($tmppath, 0755);
     394                $tmppath .= $path[$j] . '/';    // for ssh, you can enter the whole directory stucture and it will create the folders as needed. unsure about ftp/ftps
    396395        }
    397396
     397        if ( ! $fs->is_dir($tmppath) ) {
     398                if ( $fs->method == "ssh2" ) { $tmppath = untrailingslashit($tmppath); }        //      ssh can not take the trailing slash
     399                $fs->mkdir($tmppath, 0755);
     400        }
     401
    398402        foreach ($archive_files as $file) {
    399403                $path = explode('/', $file['filename']);
    400404                $tmppath = '';
    401 
    402405                // Loop through each of the items and check that the folder exists.
    403406                for ( $j = 0; $j < count($path) - 1; $j++ ) {
    404407                        $tmppath .= $path[$j] . '/';
    405408                        if ( ! $fs->is_dir($to . $tmppath) )
     409                                if ( $fs->method == "ssh2" ) { $tmppath = untrailingslashit($tmppath); }        //      ssh can not take the trailing slash
    406410                                if ( !$fs->mkdir($to . $tmppath, 0755) )
    407411                                        return new WP_Error('mkdir_failed', __('Could not create directory'), $to . $tmppath);
    408412                }
    409413
    410414                // We've made sure the folders are there, so let's extract the file now:
    411415                if ( ! $file['folder'] ) {
    412                         if ( !$fs->put_contents( $to . $file['filename'], $file['content']) )
     416                        if ( !$fs->put_contents( $to . $file['filename'], $file['content']) )   //      for ssh, this is where it is currenly failing.
    413417                                return new WP_Error('copy_failed', __('Could not copy file'), $to . $file['filename']);
    414418                        $fs->chmod($to . $file['filename'], 0644);
    415419                }
    416420        }
    417 
     421       
    418422        return true;
    419423}
    420424
     
    480484                unlink($temp_file);
    481485        }
    482486
    483         if ( isset($args['connection_type']) && 'ssh' == $args['connection_type'] ) {
    484                 $method = 'SSH2';
    485                 return apply_filters('filesystem_method', $method);
    486         }
     487        if ( ! $method && isset($args['connection_type']) && 'ssh' == $args['connection_type'] && extension_loaded('ssh2') ) $method = 'ssh2';
    487488
    488489        if ( ! $method && extension_loaded('ftp') ) $method = 'ftpext';
    489490        if ( ! $method && ( extension_loaded('sockets') || function_exists('fsockopen') ) ) $method = 'ftpsockets'; //Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread
     
    551552<td><input name="password" type="password" id="password" value=""<?php if( defined('FTP_PASS') ) echo ' disabled="disabled"' ?> size="40" /><?php if( defined('FTP_PASS') && !empty($password) ) echo '<em>'.__('(Password not shown)').'</em>'; ?></td>
    552553</tr>
    553554<tr valign="top">
     555<th scope="row"><label for="port"><?php _e('Port') ?></label></th>
     556<td><input name="port" type="port" id="port" value="21"<?php if( defined('FTP_PORT') ) echo ' disabled="disabled"' ?> size="5" /></td>
     557</tr>
     558<tr valign="top">
    554559<th scope="row"><?php _e('Connection Type') ?></th>
    555560<td>
    556561<fieldset><legend class="hidden"><?php _e('Connection Type') ?> </legend>
  • wp-admin/includes/update.php

     
    238238                return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
    239239
    240240        $working_dir = $content_dir . 'upgrade/core';
    241 
    242241        // Clean up working directory
    243         if ( $wp_filesystem->is_dir($working_dir) )
     242        if ( $wp_filesystem->is_dir($working_dir) ) {
    244243                $wp_filesystem->delete($working_dir, true);
     244        }
    245245
    246         apply_filters('update_feedback', __('Unpacking the update'));
     246        apply_filters('update_feedback', __('Unpacking the core update'));
    247247        // Unzip package to working directory
    248         $result = unzip_file($download_file, $working_dir);
    249 
     248        // @note: It fails here -- Shane
     249        $result = unzip_file($download_file, $working_dir);     //      this is the main problem
    250250        // Once extracted, delete the package
    251251        unlink($download_file);
    252 
     252       
    253253        if ( is_wp_error($result) ) {
     254                $wp_filesystem->debug("Error with the unzip.");
    254255                $wp_filesystem->delete($working_dir, true);
     256                exit;   // having problem with the delete also...
    255257                return $result;
    256258        }
    257 
     259       
    258260        // Copy update-core.php from the new version into place.
    259261        if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) {
    260262                $wp_filesystem->delete($working_dir, true);