diff -r 3d4e9c994f10 -r a86126ab1dd4 wp/wp-admin/includes/class-wp-filesystem-ftpext.php --- a/wp/wp-admin/includes/class-wp-filesystem-ftpext.php Tue Oct 22 16:11:46 2019 +0200 +++ b/wp/wp-admin/includes/class-wp-filesystem-ftpext.php Tue Dec 15 13:49:49 2020 +0100 @@ -38,8 +38,7 @@ return; } - // This Class uses the timeout on a per-connection basis, Others use it on a per-action basis. - + // This class uses the timeout on a per-connection basis, others use it on a per-action basis. if ( ! defined( 'FS_TIMEOUT' ) ) { define( 'FS_TIMEOUT', 240 ); } @@ -70,7 +69,8 @@ } $this->options['ssl'] = false; - if ( isset( $opt['connection_type'] ) && 'ftps' == $opt['connection_type'] ) { + + if ( isset( $opt['connection_type'] ) && 'ftps' === $opt['connection_type'] ) { $this->options['ssl'] = true; } } @@ -92,29 +92,32 @@ if ( ! $this->link ) { $this->errors->add( 'connect', - /* translators: %s: hostname:port */ sprintf( + /* translators: %s: hostname:port */ __( 'Failed to connect to FTP Server %s' ), $this->options['hostname'] . ':' . $this->options['port'] ) ); + return false; } if ( ! @ftp_login( $this->link, $this->options['username'], $this->options['password'] ) ) { $this->errors->add( 'auth', - /* translators: %s: username */ sprintf( + /* translators: %s: Username. */ __( 'Username/Password incorrect for %s' ), $this->options['username'] ) ); + return false; } - // Set the Connection to use Passive FTP - @ftp_pasv( $this->link, true ); + // Set the connection to use Passive FTP. + ftp_pasv( $this->link, true ); + if ( @ftp_get_option( $this->link, FTP_TIMEOUT_SEC ) < FS_TIMEOUT ) { @ftp_set_option( $this->link, FTP_TIMEOUT_SEC, FS_TIMEOUT ); } @@ -132,29 +135,30 @@ * or if the file couldn't be retrieved. */ public function get_contents( $file ) { - $tempfile = wp_tempnam( $file ); - $temp = fopen( $tempfile, 'w+' ); + $tempfile = wp_tempnam( $file ); + $temphandle = fopen( $tempfile, 'w+' ); - if ( ! $temp ) { + if ( ! $temphandle ) { unlink( $tempfile ); return false; } - if ( ! @ftp_fget( $this->link, $temp, $file, FTP_BINARY ) ) { - fclose( $temp ); + if ( ! ftp_fget( $this->link, $temphandle, $file, FTP_BINARY ) ) { + fclose( $temphandle ); unlink( $tempfile ); return false; } - fseek( $temp, 0 ); // Skip back to the start of the file being written to + fseek( $temphandle, 0 ); // Skip back to the start of the file being written to. $contents = ''; - while ( ! feof( $temp ) ) { - $contents .= fread( $temp, 8 * KB_IN_BYTES ); + while ( ! feof( $temphandle ) ) { + $contents .= fread( $temphandle, 8 * KB_IN_BYTES ); } - fclose( $temp ); + fclose( $temphandle ); unlink( $tempfile ); + return $contents; } @@ -182,10 +186,10 @@ * @return bool True on success, false on failure. */ public function put_contents( $file, $contents, $mode = false ) { - $tempfile = wp_tempnam( $file ); - $temp = fopen( $tempfile, 'wb+' ); + $tempfile = wp_tempnam( $file ); + $temphandle = fopen( $tempfile, 'wb+' ); - if ( ! $temp ) { + if ( ! $temphandle ) { unlink( $tempfile ); return false; } @@ -193,21 +197,21 @@ mbstring_binary_safe_encoding(); $data_length = strlen( $contents ); - $bytes_written = fwrite( $temp, $contents ); + $bytes_written = fwrite( $temphandle, $contents ); reset_mbstring_encoding(); if ( $data_length !== $bytes_written ) { - fclose( $temp ); + fclose( $temphandle ); unlink( $tempfile ); return false; } - fseek( $temp, 0 ); // Skip back to the start of the file being written to + fseek( $temphandle, 0 ); // Skip back to the start of the file being written to. - $ret = @ftp_fput( $this->link, $file, $temp, FTP_BINARY ); + $ret = ftp_fput( $this->link, $file, $temphandle, FTP_BINARY ); - fclose( $temp ); + fclose( $temphandle ); unlink( $tempfile ); $this->chmod( $file, $mode ); @@ -223,10 +227,12 @@ * @return string|false The current working directory on success, false on failure. */ public function cwd() { - $cwd = @ftp_pwd( $this->link ); + $cwd = ftp_pwd( $this->link ); + if ( $cwd ) { $cwd = trailingslashit( $cwd ); } + return $cwd; } @@ -250,7 +256,7 @@ * @param string $file Path to the file. * @param int|false $mode Optional. The permissions as octal number, usually 0644 for files, * 0755 for directories. Default false. - * @param bool $recursive Optional. If set to true, changes file group recursively. + * @param bool $recursive Optional. If set to true, changes file permissions recursively. * Default false. * @return bool True on success, false on failure. */ @@ -268,16 +274,18 @@ // chmod any sub-objects if recursive. if ( $recursive && $this->is_dir( $file ) ) { $filelist = $this->dirlist( $file ); + foreach ( (array) $filelist as $filename => $filemeta ) { $this->chmod( $file . '/' . $filename, $mode, $recursive ); } } - // chmod the file or directory + // chmod the file or directory. if ( ! function_exists( 'ftp_chmod' ) ) { - return (bool) @ftp_site( $this->link, sprintf( 'CHMOD %o %s', $mode, $file ) ); + return (bool) ftp_site( $this->link, sprintf( 'CHMOD %o %s', $mode, $file ) ); } - return (bool) @ftp_chmod( $this->link, $mode, $file ); + + return (bool) ftp_chmod( $this->link, $mode, $file ); } /** @@ -290,6 +298,7 @@ */ public function owner( $file ) { $dir = $this->dirlist( $file ); + return $dir[ $file ]['owner']; } @@ -303,6 +312,7 @@ */ public function getchmod( $file ) { $dir = $this->dirlist( $file ); + return $dir[ $file ]['permsn']; } @@ -316,6 +326,7 @@ */ public function group( $file ) { $dir = $this->dirlist( $file ); + return $dir[ $file ]['group']; } @@ -336,10 +347,13 @@ if ( ! $overwrite && $this->exists( $destination ) ) { return false; } + $content = $this->get_contents( $source ); + if ( false === $content ) { return false; } + return $this->put_contents( $destination, $content, $mode ); } @@ -364,7 +378,7 @@ * @since 2.5.0 * * @param string $file Path to the file or directory. - * @param bool $recursive Optional. If set to true, changes file group recursively. + * @param bool $recursive Optional. If set to true, deletes files and folders recursively. * Default false. * @param string|false $type Type of resource. 'f' for file, 'd' for directory. * Default false. @@ -374,20 +388,24 @@ if ( empty( $file ) ) { return false; } - if ( 'f' == $type || $this->is_file( $file ) ) { - return @ftp_delete( $this->link, $file ); + + if ( 'f' === $type || $this->is_file( $file ) ) { + return ftp_delete( $this->link, $file ); } + if ( ! $recursive ) { - return @ftp_rmdir( $this->link, $file ); + return ftp_rmdir( $this->link, $file ); } $filelist = $this->dirlist( trailingslashit( $file ) ); + if ( ! empty( $filelist ) ) { foreach ( $filelist as $delete_file ) { $this->delete( trailingslashit( $file ) . $delete_file['name'], $recursive, $delete_file['type'] ); } } - return @ftp_rmdir( $this->link, $file ); + + return ftp_rmdir( $this->link, $file ); } /** @@ -399,13 +417,13 @@ * @return bool Whether $file exists or not. */ public function exists( $file ) { - $list = @ftp_nlist( $this->link, $file ); + $list = ftp_nlist( $this->link, $file ); if ( empty( $list ) && $this->is_dir( $file ) ) { return true; // File is an empty directory. } - return ! empty( $list ); //empty list = no file, so invert. + return ! empty( $list ); // Empty list = no file, so invert. } /** @@ -431,10 +449,12 @@ public function is_dir( $path ) { $cwd = $this->cwd(); $result = @ftp_chdir( $this->link, trailingslashit( $path ) ); + if ( $result && $path == $this->cwd() || $this->cwd() != $cwd ) { @ftp_chdir( $this->link, $cwd ); return true; } + return false; } @@ -532,14 +552,17 @@ */ public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) { $path = untrailingslashit( $path ); + if ( empty( $path ) ) { return false; } - if ( ! @ftp_mkdir( $this->link, $path ) ) { + if ( ! ftp_mkdir( $this->link, $path ) ) { return false; } + $this->chmod( $path, $chmod ); + return true; } @@ -558,83 +581,99 @@ } /** - * @staticvar bool $is_windows * @param string $line * @return array */ public function parselisting( $line ) { static $is_windows = null; + if ( is_null( $is_windows ) ) { $is_windows = stripos( ftp_systype( $this->link ), 'win' ) !== false; } if ( $is_windows && preg_match( '/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|