- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/class-wp-filesystem-ftpsockets.php
r41161 r42343 21 21 22 22 /** 23 *24 23 * @param array $opt 25 24 */ 26 public function __construct( $opt = '' ) {25 public function __construct( $opt = '' ) { 27 26 $this->method = 'ftpsockets'; 28 27 $this->errors = new WP_Error(); … … 34 33 $this->ftp = new ftp(); 35 34 36 if ( empty( $opt['port']) )35 if ( empty( $opt['port'] ) ) { 37 36 $this->options['port'] = 21; 38 else37 } else { 39 38 $this->options['port'] = (int) $opt['port']; 40 41 if ( empty($opt['hostname']) ) 42 $this->errors->add('empty_hostname', __('FTP hostname is required')); 43 else 39 } 40 41 if ( empty( $opt['hostname'] ) ) { 42 $this->errors->add( 'empty_hostname', __( 'FTP hostname is required' ) ); 43 } else { 44 44 $this->options['hostname'] = $opt['hostname']; 45 } 45 46 46 47 // Check if the options provided are OK. 47 if ( empty ($opt['username']) )48 $this->errors->add( 'empty_username', __('FTP username is required'));49 else48 if ( empty( $opt['username'] ) ) { 49 $this->errors->add( 'empty_username', __( 'FTP username is required' ) ); 50 } else { 50 51 $this->options['username'] = $opt['username']; 51 52 if ( empty ($opt['password']) ) 53 $this->errors->add('empty_password', __('FTP password is required')); 54 else 52 } 53 54 if ( empty( $opt['password'] ) ) { 55 $this->errors->add( 'empty_password', __( 'FTP password is required' ) ); 56 } else { 55 57 $this->options['password'] = $opt['password']; 56 }57 58 /** 59 *58 } 59 } 60 61 /** 60 62 * @return bool 61 63 */ 62 64 public function connect() { 63 if ( ! $this->ftp ) 64 return false; 65 66 $this->ftp->setTimeout(FS_CONNECT_TIMEOUT); 65 if ( ! $this->ftp ) { 66 return false; 67 } 68 69 $this->ftp->setTimeout( FS_CONNECT_TIMEOUT ); 67 70 68 71 if ( ! $this->ftp->SetServer( $this->options['hostname'], $this->options['port'] ) ) { 69 $this->errors->add( 'connect', 72 $this->errors->add( 73 'connect', 70 74 /* translators: %s: hostname:port */ 71 sprintf( __( 'Failed to connect to FTP Server %s' ), 75 sprintf( 76 __( 'Failed to connect to FTP Server %s' ), 72 77 $this->options['hostname'] . ':' . $this->options['port'] 73 78 ) … … 77 82 78 83 if ( ! $this->ftp->connect() ) { 79 $this->errors->add( 'connect', 84 $this->errors->add( 85 'connect', 80 86 /* translators: %s: hostname:port */ 81 sprintf( __( 'Failed to connect to FTP Server %s' ), 87 sprintf( 88 __( 'Failed to connect to FTP Server %s' ), 82 89 $this->options['hostname'] . ':' . $this->options['port'] 83 90 ) … … 87 94 88 95 if ( ! $this->ftp->login( $this->options['username'], $this->options['password'] ) ) { 89 $this->errors->add( 'auth', 96 $this->errors->add( 97 'auth', 90 98 /* translators: %s: username */ 91 sprintf( __( 'Username/Password incorrect for %s' ), 99 sprintf( 100 __( 'Username/Password incorrect for %s' ), 92 101 $this->options['username'] 93 102 ) … … 112 121 */ 113 122 public function get_contents( $file ) { 114 if ( ! $this->exists($file) ) 115 return false; 123 if ( ! $this->exists( $file ) ) { 124 return false; 125 } 116 126 117 127 $temp = wp_tempnam( $file ); … … 124 134 mbstring_binary_safe_encoding(); 125 135 126 if ( ! $this->ftp->fget( $temphandle, $file) ) {127 fclose( $temphandle);128 unlink( $temp);136 if ( ! $this->ftp->fget( $temphandle, $file ) ) { 137 fclose( $temphandle ); 138 unlink( $temp ); 129 139 130 140 reset_mbstring_encoding(); … … 138 148 $contents = ''; 139 149 140 while ( ! feof($temphandle) ) 141 $contents .= fread($temphandle, 8192); 142 143 fclose($temphandle); 144 unlink($temp); 150 while ( ! feof( $temphandle ) ) { 151 $contents .= fread( $temphandle, 8192 ); 152 } 153 154 fclose( $temphandle ); 155 unlink( $temp ); 145 156 return $contents; 146 157 } 147 158 148 159 /** 149 *150 160 * @param string $file 151 161 * @return array 152 162 */ 153 public function get_contents_array($file) { 154 return explode("\n", $this->get_contents($file) ); 155 } 156 157 /** 158 * 163 public function get_contents_array( $file ) { 164 return explode( "\n", $this->get_contents( $file ) ); 165 } 166 167 /** 159 168 * @param string $file 160 169 * @param string $contents … … 162 171 * @return bool 163 172 */ 164 public function put_contents( $file, $contents, $mode = false ) {173 public function put_contents( $file, $contents, $mode = false ) { 165 174 $temp = wp_tempnam( $file ); 166 if ( ! $temphandle = @fopen( $temp, 'w+') ) {167 unlink( $temp);175 if ( ! $temphandle = @fopen( $temp, 'w+' ) ) { 176 unlink( $temp ); 168 177 return false; 169 178 } … … 184 193 fseek( $temphandle, 0 ); // Skip back to the start of the file being written to 185 194 186 $ret = $this->ftp->fput( $file, $temphandle);195 $ret = $this->ftp->fput( $file, $temphandle ); 187 196 188 197 reset_mbstring_encoding(); 189 198 190 fclose( $temphandle);191 unlink( $temp);192 193 $this->chmod( $file, $mode);199 fclose( $temphandle ); 200 unlink( $temp ); 201 202 $this->chmod( $file, $mode ); 194 203 195 204 return $ret; … … 197 206 198 207 /** 199 *200 208 * @return string 201 209 */ 202 210 public function cwd() { 203 211 $cwd = $this->ftp->pwd(); 204 if ( $cwd ) 205 $cwd = trailingslashit($cwd); 212 if ( $cwd ) { 213 $cwd = trailingslashit( $cwd ); 214 } 206 215 return $cwd; 207 216 } 208 217 209 218 /** 210 * 211 * @param string $file 212 * @return bool 213 */ 214 public function chdir($file) { 215 return $this->ftp->chdir($file); 216 } 217 218 /** 219 * 219 * @param string $file 220 * @return bool 221 */ 222 public function chdir( $file ) { 223 return $this->ftp->chdir( $file ); 224 } 225 226 /** 220 227 * @param string $file 221 228 * @param int|bool $mode … … 223 230 * @return bool 224 231 */ 225 public function chmod( $file, $mode = false, $recursive = false ) {232 public function chmod( $file, $mode = false, $recursive = false ) { 226 233 if ( ! $mode ) { 227 if ( $this->is_file( $file) )234 if ( $this->is_file( $file ) ) { 228 235 $mode = FS_CHMOD_FILE; 229 elseif ( $this->is_dir($file) )236 } elseif ( $this->is_dir( $file ) ) { 230 237 $mode = FS_CHMOD_DIR; 231 else238 } else { 232 239 return false; 240 } 233 241 } 234 242 235 243 // chmod any sub-objects if recursive. 236 if ( $recursive && $this->is_dir($file) ) { 237 $filelist = $this->dirlist($file); 238 foreach ( (array)$filelist as $filename => $filemeta ) 239 $this->chmod($file . '/' . $filename, $mode, $recursive); 244 if ( $recursive && $this->is_dir( $file ) ) { 245 $filelist = $this->dirlist( $file ); 246 foreach ( (array) $filelist as $filename => $filemeta ) { 247 $this->chmod( $file . '/' . $filename, $mode, $recursive ); 248 } 240 249 } 241 250 242 251 // chmod the file or directory 243 return $this->ftp->chmod($file, $mode); 244 } 245 246 /** 247 * 252 return $this->ftp->chmod( $file, $mode ); 253 } 254 255 /** 248 256 * @param string $file 249 257 * @return string 250 258 */ 251 public function owner($file) { 252 $dir = $this->dirlist($file); 253 return $dir[$file]['owner']; 254 } 255 256 /** 257 * 259 public function owner( $file ) { 260 $dir = $this->dirlist( $file ); 261 return $dir[ $file ]['owner']; 262 } 263 264 /** 258 265 * @param string $file 259 266 * @return string 260 267 */ 261 public function getchmod($file) { 262 $dir = $this->dirlist($file); 263 return $dir[$file]['permsn']; 264 } 265 266 /** 267 * 268 public function getchmod( $file ) { 269 $dir = $this->dirlist( $file ); 270 return $dir[ $file ]['permsn']; 271 } 272 273 /** 268 274 * @param string $file 269 275 * @return string 270 276 */ 271 public function group($file) { 272 $dir = $this->dirlist($file); 273 return $dir[$file]['group']; 274 } 275 276 /** 277 * 277 public function group( $file ) { 278 $dir = $this->dirlist( $file ); 279 return $dir[ $file ]['group']; 280 } 281 282 /** 278 283 * @param string $source 279 284 * @param string $destination … … 282 287 * @return bool 283 288 */ 284 public function copy($source, $destination, $overwrite = false, $mode = false) { 285 if ( ! $overwrite && $this->exists($destination) ) 286 return false; 287 288 $content = $this->get_contents($source); 289 if ( false === $content ) 290 return false; 291 292 return $this->put_contents($destination, $content, $mode); 293 } 294 295 /** 296 * 289 public function copy( $source, $destination, $overwrite = false, $mode = false ) { 290 if ( ! $overwrite && $this->exists( $destination ) ) { 291 return false; 292 } 293 294 $content = $this->get_contents( $source ); 295 if ( false === $content ) { 296 return false; 297 } 298 299 return $this->put_contents( $destination, $content, $mode ); 300 } 301 302 /** 297 303 * @param string $source 298 304 * @param string $destination … … 300 306 * @return bool 301 307 */ 302 public function move($source, $destination, $overwrite = false ) { 303 return $this->ftp->rename($source, $destination); 304 } 305 306 /** 307 * 308 public function move( $source, $destination, $overwrite = false ) { 309 return $this->ftp->rename( $source, $destination ); 310 } 311 312 /** 308 313 * @param string $file 309 314 * @param bool $recursive … … 311 316 * @return bool 312 317 */ 313 public function delete($file, $recursive = false, $type = false) { 314 if ( empty($file) ) 315 return false; 316 if ( 'f' == $type || $this->is_file($file) ) 317 return $this->ftp->delete($file); 318 if ( !$recursive ) 319 return $this->ftp->rmdir($file); 320 321 return $this->ftp->mdel($file); 322 } 323 324 /** 325 * 318 public function delete( $file, $recursive = false, $type = false ) { 319 if ( empty( $file ) ) { 320 return false; 321 } 322 if ( 'f' == $type || $this->is_file( $file ) ) { 323 return $this->ftp->delete( $file ); 324 } 325 if ( ! $recursive ) { 326 return $this->ftp->rmdir( $file ); 327 } 328 329 return $this->ftp->mdel( $file ); 330 } 331 332 /** 326 333 * @param string $file 327 334 * @return bool … … 334 341 } 335 342 336 return ! empty( $list ); //empty list = no file, so invert.343 return ! empty( $list ); //empty list = no file, so invert. 337 344 // Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server. 338 345 } 339 346 340 347 /** 341 * 342 * @ param string $file343 * @return bool344 */345 public function is_file($file) {346 if ( $this->is_dir($file) )347 return false;348 if ( $this->exists( $file) )348 * @param string $file 349 * @return bool 350 */ 351 public function is_file( $file ) { 352 if ( $this->is_dir( $file ) ) { 353 return false; 354 } 355 if ( $this->exists( $file ) ) { 349 356 return true; 357 } 350 358 return false; 351 359 } 352 360 353 361 /** 354 *355 362 * @param string $path 356 363 * @return bool 357 364 */ 358 public function is_dir( $path) {365 public function is_dir( $path ) { 359 366 $cwd = $this->cwd(); 360 if ( $this->chdir( $path) ) {361 $this->chdir( $cwd);367 if ( $this->chdir( $path ) ) { 368 $this->chdir( $cwd ); 362 369 return true; 363 370 } … … 366 373 367 374 /** 368 * 369 * @param string $file 370 * @return bool 371 */ 372 public function is_readable($file) { 375 * @param string $file 376 * @return bool 377 */ 378 public function is_readable( $file ) { 373 379 return true; 374 380 } 375 381 376 382 /** 377 * 378 * @param string $file 379 * @return bool 380 */ 381 public function is_writable($file) { 383 * @param string $file 384 * @return bool 385 */ 386 public function is_writable( $file ) { 382 387 return true; 383 388 } 384 389 385 390 /** 386 * 387 * @param string $file 388 * @return bool 389 */ 390 public function atime($file) { 391 * @param string $file 392 * @return bool 393 */ 394 public function atime( $file ) { 391 395 return false; 392 396 } 393 397 394 398 /** 395 *396 399 * @param string $file 397 400 * @return int 398 401 */ 399 public function mtime( $file) {400 return $this->ftp->mdtm( $file);402 public function mtime( $file ) { 403 return $this->ftp->mdtm( $file ); 401 404 } 402 405 … … 405 408 * @return int 406 409 */ 407 public function size($file) { 408 return $this->ftp->filesize($file); 409 } 410 411 /** 412 * 410 public function size( $file ) { 411 return $this->ftp->filesize( $file ); 412 } 413 414 /** 413 415 * @param string $file 414 416 * @param int $time … … 416 418 * @return bool 417 419 */ 418 public function touch( $file, $time = 0, $atime = 0 ) {420 public function touch( $file, $time = 0, $atime = 0 ) { 419 421 return false; 420 422 } 421 423 422 424 /** 423 *424 425 * @param string $path 425 426 * @param mixed $chmod … … 428 429 * @return bool 429 430 */ 430 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false ) { 431 $path = untrailingslashit($path); 432 if ( empty($path) ) 433 return false; 434 435 if ( ! $this->ftp->mkdir($path) ) 436 return false; 437 if ( ! $chmod ) 431 public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) { 432 $path = untrailingslashit( $path ); 433 if ( empty( $path ) ) { 434 return false; 435 } 436 437 if ( ! $this->ftp->mkdir( $path ) ) { 438 return false; 439 } 440 if ( ! $chmod ) { 438 441 $chmod = FS_CHMOD_DIR; 439 $this->chmod($path, $chmod); 442 } 443 $this->chmod( $path, $chmod ); 440 444 return true; 441 445 } 442 446 443 447 /** 444 *445 448 * @param string $path 446 449 * @param bool $recursive 447 450 * @return bool 448 451 */ 449 public function rmdir($path, $recursive = false ) { 450 return $this->delete($path, $recursive); 451 } 452 453 /** 454 * 452 public function rmdir( $path, $recursive = false ) { 453 return $this->delete( $path, $recursive ); 454 } 455 456 /** 455 457 * @param string $path 456 458 * @param bool $include_hidden … … 458 460 * @return bool|array 459 461 */ 460 public function dirlist( $path = '.', $include_hidden = true, $recursive = false ) {461 if ( $this->is_file( $path) ) {462 $limit_file = basename( $path);463 $path = dirname($path) . '/';462 public function dirlist( $path = '.', $include_hidden = true, $recursive = false ) { 463 if ( $this->is_file( $path ) ) { 464 $limit_file = basename( $path ); 465 $path = dirname( $path ) . '/'; 464 466 } else { 465 467 $limit_file = false; … … 468 470 mbstring_binary_safe_encoding(); 469 471 470 $list = $this->ftp->dirlist( $path);472 $list = $this->ftp->dirlist( $path ); 471 473 if ( empty( $list ) && ! $this->exists( $path ) ) { 472 474 … … 479 481 foreach ( $list as $struc ) { 480 482 481 if ( '.' == $struc['name'] || '..' == $struc['name'] ) 483 if ( '.' == $struc['name'] || '..' == $struc['name'] ) { 482 484 continue; 483 484 if ( ! $include_hidden && '.' == $struc['name'][0] ) 485 } 486 487 if ( ! $include_hidden && '.' == $struc['name'][0] ) { 485 488 continue; 486 487 if ( $limit_file && $struc['name'] != $limit_file ) 489 } 490 491 if ( $limit_file && $struc['name'] != $limit_file ) { 488 492 continue; 493 } 489 494 490 495 if ( 'd' == $struc['type'] ) { 491 if ( $recursive ) 492 $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive);493 else496 if ( $recursive ) { 497 $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive ); 498 } else { 494 499 $struc['files'] = array(); 500 } 495 501 } 496 502 497 503 // Replace symlinks formatted as "source -> target" with just the source name 498 if ( $struc['islink'] ) 504 if ( $struc['islink'] ) { 499 505 $struc['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $struc['name'] ); 506 } 500 507 501 508 // Add the Octal representation of the file permissions
Note: See TracChangeset
for help on using the changeset viewer.