Ticket #7690: 7690.6.diff
| File 7690.6.diff, 15.8 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 … … 19 44 var $debugtest = true; // this is my var that will output the text when debuggin this class 20 45 21 46 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; 23 55 var $errors = array(); 24 56 var $options = array(); 25 57 26 var $permission = null;58 var $permission = 0644; 27 59 28 60 var $filetypes = array( 29 61 'php'=>FTP_ASCII, … … 71 103 $this->options['username'] = $opt['username']; 72 104 73 105 if ( empty ($opt['password']) ) 74 $this->errors->add('empty_password', __('SSH password is required'));106 $this->errors->add('empty_password', __('SSH2 password is required')); 75 107 else 76 108 $this->options['password'] = $opt['password']; 77 109 … … 95 127 } 96 128 97 129 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__\";"))) { 100 132 $this->errors->add('command', sprintf(__('Unable to preform command: %s'), $command)); 101 133 } else { 102 134 stream_set_blocking( $stream, true ); 103 135 $time_start = time(); 104 $data = "";136 $data = null; 105 137 while( true ) { 138 if (strpos($data,"__COMMAND_FINISHED__") !== false){ 139 break; // the command has finshed! 140 } 106 141 if( (time()-$time_start) > $this->timeout ){ 107 142 $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; 109 145 } 110 146 while( $buf = fread( $stream, strlen($stream) ) ){ 111 147 $data .= $buf; 112 148 } 113 149 } 114 150 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"); 117 155 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"); 120 158 return false; 121 159 } else { 122 $this->debug("Data : " . print_r($data, true));160 $this->debug("Data Only."); 123 161 return $data; 124 162 } 125 163 } 164 return false; 126 165 } 127 166 128 167 function debug($text) 129 168 { 130 169 if ($this->debugtest) 131 170 { 132 echo $text . "<br/>";171 echo "<br/>" . $text . "<br/>"; 133 172 } 134 173 } 135 174 136 175 function setDefaultPermissions($perm) { 176 $this->debug("setDefaultPermissions();"); 137 177 $this->permission = $perm; 138 178 } 139 179 140 function get_contents($file, $type = '', $resumepos = 0 ){ 180 function get_contents($file, $type = '', $resumepos = 0 ) { 181 $this->debug("get_contents();"); 141 182 if( empty($type) ){ 142 183 $extension = substr(strrchr($file, "."), 1); 143 184 $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII; … … 157 198 } 158 199 159 200 function get_contents_array($file) { 201 $this->debug("get_contents_array();"); 160 202 return explode("\n", $this->get_contents($file)); 161 203 } 162 204 163 205 function put_contents($file, $contents, $type = '' ) { 206 $this->debug("put_contents(".$file.",contents,".$type.");"); 164 207 if( empty($type) ) { 165 208 $extension = substr(strrchr($file, "."), 1); 166 209 $type = isset($this->filetypes[ $extension ]) ? $this->filetypes[ $extension ] : FTP_ASCII; 167 210 } 168 211 $temp = tmpfile(); 169 if ( ! $temp ) 212 if ( ! $temp ) { 170 213 return false; 214 } 171 215 fwrite($temp, $contents); 172 216 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); 174 219 fclose($temp); 175 220 return $ret; 176 221 } 177 222 178 223 function cwd() { 224 // $this->debug("cwd();"); 179 225 $cwd = $this->run_command($this->link, "pwd"); 180 226 if( $cwd ) 181 227 $cwd = trailingslashit($cwd); … … 183 229 } 184 230 185 231 function chdir($dir) { 232 // $this->debug("chdir();"); 186 233 if ($this->run_command($this->link, "cd " . $dir, true)) { 187 234 return true; 188 235 } … … 190 237 } 191 238 192 239 function chgrp($file, $group, $recursive = false ) { 240 $this->debug("chgrp();"); 193 241 return false; 194 242 } 195 243 196 244 function chmod($file, $mode = false, $recursive = false) { 245 $this->debug("chmod();"); 197 246 if( ! $mode ) 198 247 $mode = $this->permission; 199 248 if( ! $mode ) … … 212 261 } 213 262 214 263 function chown($file, $owner, $recursive = false ) { 264 $this->debug("chown();"); 215 265 return false; 216 266 } 217 267 218 268 function owner($file) { 269 $this->debug("owner();"); 219 270 $dir = $this->dirlist($file); 220 271 return $dir[$file]['owner']; 221 272 } 222 273 223 274 function getchmod($file) { 275 $this->debug("getchmod();"); 224 276 $dir = $this->dirlist($file); 225 277 return $dir[$file]['permsn']; 226 278 } 227 279 228 280 function group($file) { 281 $this->debug("group();"); 229 282 $dir = $this->dirlist($file); 230 283 return $dir[$file]['group']; 231 284 } 232 285 233 286 function copy($source, $destination, $overwrite = false ) { 287 $this->debug("copy();"); 234 288 if( ! $overwrite && $this->exists($destination) ) 235 289 return false; 236 290 $content = $this->get_contents($source); … … 240 294 } 241 295 242 296 function move($source, $destination, $overwrite = false) { 297 $this->debug("move();"); 243 298 return @ssh2_sftp_rename($this->link, $source, $destination); 244 299 } 245 300 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); 254 310 } 255 return @ssh2_sftp_rmdir($this->link, $file);256 311 } 257 312 258 313 function exists($file) { 314 $this->debug("exists();"); 259 315 $list = $this->run_command($this->link, sprintf('ls -la %s', $file)); 260 316 if( ! $list ) 261 317 return false; … … 263 319 } 264 320 265 321 function is_file($file) { 266 return $this->is_dir($file) ? false : true;322 return $this->is_dir($file) ? true : false; 267 323 } 268 324 269 325 function is_dir($path) { 270 326 $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); 275 330 return true; 276 331 } 277 332 return false; 278 333 } 279 334 280 335 function is_readable($file) { 336 $this->debug("is_readable();"); 281 337 //Get dir list, Check if the file is writable by the current user?? 282 338 return true; 283 339 } 284 340 285 341 function is_writable($file) { 342 $this->debug("is_writable();"); 286 343 //Get dir list, Check if the file is writable by the current user?? 287 344 return true; 288 345 } 289 346 290 347 function atime($file) { 348 $this->debug("atime();"); 291 349 return false; 292 350 } 293 351 294 352 function mtime($file) { 353 $this->debug("mtime();"); 295 354 return; // i have to look up to see if there is a way in SSH2 to look the modifed date 296 355 // return ftp_mdtm($this->link, $file); 297 356 } 298 357 299 358 function size($file) { 359 $this->debug("size();"); 300 360 return; // i have to look up to see if there is a way in SSH2 to get the file size 301 361 // return ftp_size($this->link, $file); 302 362 } 303 363 304 364 function touch($file, $time = 0, $atime = 0) { 365 $this->debug("touch();"); 305 366 return false; 306 367 } 307 368 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."); 310 377 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 } 318 382 } 319 383 320 384 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 } 321 390 if( ! $recursive ) 322 391 return @ssh2_sftp_rmdir($this->link, $path); 323 392 … … 328 397 } 329 398 330 399 function parselisting($line) { 400 $this->debug("parselisting();"); 331 401 $is_windows = ($this->OS_remote == FTP_OS_Windows); 332 402 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)) { 333 403 $b = array(); … … 390 460 } 391 461 392 462 function dirlist($path = '.', $incdot = false, $recursive = false) { 463 $this->debug("dirlist();"); 393 464 if( $this->is_file($path) ) { 394 465 $limitFile = basename($path); 395 466 $path = dirname($path) . '/'; … … 442 513 return $ret; 443 514 } 444 515 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); 448 519 } 449 520 } 450 521 451 ?> 452 No newline at end of file 522 ?> -
wp-admin/includes/file.php
389 389 $to = trailingslashit($to); 390 390 $path = explode('/', $to); 391 391 $tmppath = ''; 392 392 393 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 396 395 } 397 396 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 398 402 foreach ($archive_files as $file) { 399 403 $path = explode('/', $file['filename']); 400 404 $tmppath = ''; 401 402 405 // Loop through each of the items and check that the folder exists. 403 406 for ( $j = 0; $j < count($path) - 1; $j++ ) { 404 407 $tmppath .= $path[$j] . '/'; 405 408 if ( ! $fs->is_dir($to . $tmppath) ) 409 if ( $fs->method == "ssh2" ) { $tmppath = untrailingslashit($tmppath); } // ssh can not take the trailing slash 406 410 if ( !$fs->mkdir($to . $tmppath, 0755) ) 407 411 return new WP_Error('mkdir_failed', __('Could not create directory'), $to . $tmppath); 408 412 } 409 413 410 414 // We've made sure the folders are there, so let's extract the file now: 411 415 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. 413 417 return new WP_Error('copy_failed', __('Could not copy file'), $to . $file['filename']); 414 418 $fs->chmod($to . $file['filename'], 0644); 415 419 } 416 420 } 417 421 418 422 return true; 419 423 } 420 424 … … 480 484 unlink($temp_file); 481 485 } 482 486 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'; 487 488 488 489 if ( ! $method && extension_loaded('ftp') ) $method = 'ftpext'; 489 490 if ( ! $method && ( extension_loaded('sockets') || function_exists('fsockopen') ) ) $method = 'ftpsockets'; //Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread … … 551 552 <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> 552 553 </tr> 553 554 <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"> 554 559 <th scope="row"><?php _e('Connection Type') ?></th> 555 560 <td> 556 561 <fieldset><legend class="hidden"><?php _e('Connection Type') ?> </legend> -
wp-admin/includes/update.php
238 238 return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message()); 239 239 240 240 $working_dir = $content_dir . 'upgrade/core'; 241 242 241 // Clean up working directory 243 if ( $wp_filesystem->is_dir($working_dir) ) 242 if ( $wp_filesystem->is_dir($working_dir) ) { 244 243 $wp_filesystem->delete($working_dir, true); 244 } 245 245 246 apply_filters('update_feedback', __('Unpacking the update'));246 apply_filters('update_feedback', __('Unpacking the core update')); 247 247 // 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 250 250 // Once extracted, delete the package 251 251 unlink($download_file); 252 252 253 253 if ( is_wp_error($result) ) { 254 $wp_filesystem->debug("Error with the unzip."); 254 255 $wp_filesystem->delete($working_dir, true); 256 exit; // having problem with the delete also... 255 257 return $result; 256 258 } 257 259 258 260 // Copy update-core.php from the new version into place. 259 261 if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) { 260 262 $wp_filesystem->delete($working_dir, true);