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]+|) +(.+)/', $line, $lucifer ) ) { $b = array(); + if ( $lucifer[3] < 70 ) { $lucifer[3] += 2000; } else { - $lucifer[3] += 1900; // 4digit year fix + $lucifer[3] += 1900; // 4-digit year fix. } - $b['isdir'] = ( $lucifer[7] == '' ); + + $b['isdir'] = ( '' === $lucifer[7] ); + if ( $b['isdir'] ) { $b['type'] = 'd'; } else { $b['type'] = 'f'; } + $b['size'] = $lucifer[7]; $b['month'] = $lucifer[1]; $b['day'] = $lucifer[2]; $b['year'] = $lucifer[3]; $b['hour'] = $lucifer[4]; $b['minute'] = $lucifer[5]; - $b['time'] = @mktime( $lucifer[4] + ( strcasecmp( $lucifer[6], 'PM' ) == 0 ? 12 : 0 ), $lucifer[5], 0, $lucifer[1], $lucifer[2], $lucifer[3] ); + $b['time'] = mktime( $lucifer[4] + ( strcasecmp( $lucifer[6], 'PM' ) == 0 ? 12 : 0 ), $lucifer[5], 0, $lucifer[1], $lucifer[2], $lucifer[3] ); $b['am/pm'] = $lucifer[6]; $b['name'] = $lucifer[8]; - } elseif ( ! $is_windows && $lucifer = preg_split( '/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY ) ) { - //echo $line."\n"; - $lcount = count( $lucifer ); - if ( $lcount < 8 ) { - return ''; - } - $b = array(); - $b['isdir'] = $lucifer[0]{0} === 'd'; - $b['islink'] = $lucifer[0]{0} === 'l'; - if ( $b['isdir'] ) { - $b['type'] = 'd'; - } elseif ( $b['islink'] ) { - $b['type'] = 'l'; - } else { - $b['type'] = 'f'; - } - $b['perms'] = $lucifer[0]; - $b['permsn'] = $this->getnumchmodfromh( $b['perms'] ); - $b['number'] = $lucifer[1]; - $b['owner'] = $lucifer[2]; - $b['group'] = $lucifer[3]; - $b['size'] = $lucifer[4]; - if ( $lcount == 8 ) { - sscanf( $lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day'] ); - sscanf( $lucifer[6], '%d:%d', $b['hour'], $b['minute'] ); - $b['time'] = @mktime( $b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year'] ); - $b['name'] = $lucifer[7]; - } else { - $b['month'] = $lucifer[5]; - $b['day'] = $lucifer[6]; - if ( preg_match( '/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2 ) ) { - $b['year'] = date( 'Y' ); - $b['hour'] = $l2[1]; - $b['minute'] = $l2[2]; + } elseif ( ! $is_windows ) { + $lucifer = preg_split( '/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY ); + + if ( $lucifer ) { + // echo $line."\n"; + $lcount = count( $lucifer ); + + if ( $lcount < 8 ) { + return ''; + } + + $b = array(); + $b['isdir'] = 'd' === $lucifer[0][0]; + $b['islink'] = 'l' === $lucifer[0][0]; + + if ( $b['isdir'] ) { + $b['type'] = 'd'; + } elseif ( $b['islink'] ) { + $b['type'] = 'l'; } else { - $b['year'] = $lucifer[7]; - $b['hour'] = 0; - $b['minute'] = 0; + $b['type'] = 'f'; } - $b['time'] = strtotime( sprintf( '%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute'] ) ); - $b['name'] = $lucifer[8]; + + $b['perms'] = $lucifer[0]; + $b['permsn'] = $this->getnumchmodfromh( $b['perms'] ); + $b['number'] = $lucifer[1]; + $b['owner'] = $lucifer[2]; + $b['group'] = $lucifer[3]; + $b['size'] = $lucifer[4]; + + if ( 8 == $lcount ) { + sscanf( $lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day'] ); + sscanf( $lucifer[6], '%d:%d', $b['hour'], $b['minute'] ); + + $b['time'] = mktime( $b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year'] ); + $b['name'] = $lucifer[7]; + } else { + $b['month'] = $lucifer[5]; + $b['day'] = $lucifer[6]; + + if ( preg_match( '/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2 ) ) { + $b['year'] = gmdate( 'Y' ); + $b['hour'] = $l2[1]; + $b['minute'] = $l2[2]; + } else { + $b['year'] = $lucifer[7]; + $b['hour'] = 0; + $b['minute'] = 0; + } + + $b['time'] = strtotime( sprintf( '%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute'] ) ); + $b['name'] = $lucifer[8]; + } } } - // Replace symlinks formatted as "source -> target" with just the source name + // Replace symlinks formatted as "source -> target" with just the source name. if ( isset( $b['islink'] ) && $b['islink'] ) { $b['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $b['name'] ); } @@ -675,11 +714,14 @@ $limit_file = false; } - $pwd = @ftp_pwd( $this->link ); + $pwd = ftp_pwd( $this->link ); + if ( ! @ftp_chdir( $this->link, $path ) ) { // Can't change to folder = folder doesn't exist. return false; } - $list = @ftp_rawlist( $this->link, '-a', false ); + + $list = ftp_rawlist( $this->link, '-a', false ); + @ftp_chdir( $this->link, $pwd ); if ( empty( $list ) ) { // Empty array = non-existent folder (real folder will show . at least). @@ -687,17 +729,19 @@ } $dirlist = array(); + foreach ( $list as $k => $v ) { $entry = $this->parselisting( $v ); + if ( empty( $entry ) ) { continue; } - if ( '.' == $entry['name'] || '..' == $entry['name'] ) { + if ( '.' === $entry['name'] || '..' === $entry['name'] ) { continue; } - if ( ! $include_hidden && '.' == $entry['name'][0] ) { + if ( ! $include_hidden && '.' === $entry['name'][0] ) { continue; } @@ -709,8 +753,9 @@ } $ret = array(); + foreach ( (array) $dirlist as $struc ) { - if ( 'd' == $struc['type'] ) { + if ( 'd' === $struc['type'] ) { if ( $recursive ) { $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive ); } else { @@ -720,6 +765,7 @@ $ret[ $struc['name'] ] = $struc; } + return $ret; }