Ticket #7690: 7690.5.diff
| File 7690.5.diff, 8.0 KB (added by , 18 years ago) |
|---|
-
wp-admin/includes/class-wp-filesystem-ssh2.php
9 9 /** 10 10 * WordPress Filesystem Class for implementing SSH2. 11 11 * 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 * 12 37 * @since 2.7 13 38 * @package WordPress 14 39 * @subpackage Filesystem … … 23 48 var $errors = array(); 24 49 var $options = array(); 25 50 26 var $permission = null;51 var $permission = 0644; 27 52 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_BINARY41 );42 43 53 function WP_Filesystem_SSH2($opt='') { 44 54 $this->method = 'ssh2'; 45 55 $this->errors = new WP_Error(); … … 71 81 $this->options['username'] = $opt['username']; 72 82 73 83 if ( empty ($opt['password']) ) 74 $this->errors->add('empty_password', __('SSH password is required'));84 $this->errors->add('empty_password', __('SSH2 password is required')); 75 85 else 76 86 $this->options['password'] = $opt['password']; 77 87 … … 96 106 97 107 function run_command($link, $command, $returnbool = false) { 98 108 $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 ); 103 113 $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 ) { 107 117 $this->errors->add('command', sprintf(__('Connection to the server has timeout after %s seconds.'), $this->timeout)); 108 118 break; 109 119 } 110 while( $buf = fread( $stream, strlen($stream) ) ){ 111 $data .= $buf; 112 } 120 while( $buf = fread( $stream, strlen($stream) ) ) 121 $data .= $buf; 113 122 } 114 123 fclose($stream); 115 if ( ($returnbool) && ($data)) {124 if ( $returnbool && $data ) { 116 125 $this->debug("Data: " . print_r($data, true) . " Returning: True"); 117 126 return true; 118 } elseif ( ($returnbool) && (!$data)) {127 } elseif ( $returnbool && ! $data ) { 119 128 $this->debug("Data: " . print_r($data, true) . " Returning: False"); 120 129 return false; 121 130 } else { … … 134 143 } 135 144 136 145 function setDefaultPermissions($perm) { 137 $this->permission = $perm; 146 if ( $perm ) 147 $this->permission = $perm; 138 148 } 139 149 140 150 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) ) ); 147 152 return false; 148 if( ! @ssh2_scp_recv($this->link, $temp, $file) )153 if( ! ssh2_scp_recv($this->link, $file, $tempfile) ) 149 154 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); 156 157 return $contents; 157 158 } 158 159 … … 161 162 } 162 163 163 164 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'); 169 167 if ( ! $temp ) 170 168 return false; 171 169 fwrite($temp, $contents); 172 fseek($temp, 0); //Skip back to the start of the file being written to173 $ret = @ssh2_scp_send($this->link, $file, $temp, $type);174 170 fclose($temp); 171 $ret = ssh2_scp_send($this->link, $tempfile, $file, $this->permission); 172 unlink($tempfile); 175 173 return $ret; 176 174 } 177 175 178 176 function cwd() { 179 $cwd = $this->run_command($this->link, "pwd");177 $cwd = $this->run_command($this->link, 'pwd'); 180 178 if( $cwd ) 181 179 $cwd = trailingslashit($cwd); 182 180 return $cwd; 183 181 } 184 182 185 183 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) ); 190 185 } 191 186 192 187 function chgrp($file, $group, $recursive = false ) { … … 243 238 return @ssh2_sftp_rename($this->link, $source, $destination); 244 239 } 245 240 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") 247 243 if ( $this->is_file($file) ) 248 return @ssh2_sftp_unlink($this->link, $file);244 return ! $this->run_command($this->link, 'rm ' . $file, true); 249 245 if ( !$recursive ) 250 return @ssh2_sftp_rmdir($this->link,$file);246 return ! $this->run_command($this->link, 'rmdir ' . $file); 251 247 $filelist = $this->dirlist($file); 252 248 foreach ((array) $filelist as $filename => $fileinfo) { 253 249 $this->delete($file . '/' . $filename, $recursive); 254 250 } 255 return @ssh2_sftp_rmdir($this->link, $file);251 return ! $this->run_command($this->link, 'rmdir ' . $file, true); 256 252 } 257 253 258 254 function exists($file) { … … 270 266 $cwd = $this->cwd(); 271 267 $result = $this->run_command($this->link, sprintf('cd %s', $path), true); 272 268 if( $result && $path == $this->cwd() || $this->cwd() != $cwd ) { 273 // @todo: use ssh2_exec 274 @ftp_chdir($this->link, $cwd); 269 $this->chdir($cwd); 275 270 return true; 276 271 } 277 272 return false; … … 306 301 } 307 302 308 303 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 310 305 return false; 311 306 if( $chmod ) 312 307 $this->chmod($path, $chmod); … … 448 443 } 449 444 } 450 445 451 ?> 452 No newline at end of file 446 ?> -
wp-admin/includes/file.php
480 480 unlink($temp_file); 481 481 } 482 482 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'; 487 484 488 485 if ( ! $method && extension_loaded('ftp') ) $method = 'ftpext'; 489 486 if ( ! $method && ( extension_loaded('sockets') || function_exists('fsockopen') ) ) $method = 'ftpsockets'; //Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread