From d4f4f4232c8115b5560e0d2d6bc3ab90350ffd40 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 19 Jun 2020 10:34:26 +0000 Subject: [PATCH] Coding Standards: Add some space around control structures in `WP_Filesystem_*` classes for consistency and better readability. Additionally, synchronize `$tempfile` and `$temphandle` variable names in `WP_Filesystem_FTPext` and `WP_Filesystem_ftpsockets`. See #49542. git-svn-id: https://develop.svn.wordpress.org/trunk@48089 602fd350-edb4-49c9-b593-d223f7449a82 --- .../includes/class-wp-filesystem-base.php | 17 ++++ .../includes/class-wp-filesystem-direct.php | 38 +++++++++ .../includes/class-wp-filesystem-ftpext.php | 79 +++++++++++++++---- .../class-wp-filesystem-ftpsockets.php | 47 ++++++++--- .../includes/class-wp-filesystem-ssh2.php | 39 +++++++++ 5 files changed, 193 insertions(+), 27 deletions(-) diff --git a/src/wp-admin/includes/class-wp-filesystem-base.php b/src/wp-admin/includes/class-wp-filesystem-base.php index 7d96de0676..170a2444f4 100644 --- a/src/wp-admin/includes/class-wp-filesystem-base.php +++ b/src/wp-admin/includes/class-wp-filesystem-base.php @@ -55,11 +55,13 @@ class WP_Filesystem_Base { */ public function abspath() { $folder = $this->find_folder( ABSPATH ); + // Perhaps the FTP folder is rooted at the WordPress install. // Check for wp-includes folder in root. Could have some false positives, but rare. if ( ! $folder && $this->is_dir( '/' . WPINC ) ) { $folder = '/'; } + return $folder; } @@ -188,6 +190,7 @@ class WP_Filesystem_Base { if ( ! defined( $constant ) ) { continue; } + if ( $folder === $dir ) { return trailingslashit( constant( $constant ) ); } @@ -198,18 +201,21 @@ class WP_Filesystem_Base { if ( ! defined( $constant ) ) { continue; } + if ( 0 === stripos( $folder, $dir ) ) { // $folder starts with $dir. $potential_folder = preg_replace( '#^' . preg_quote( $dir, '#' ) . '/#i', trailingslashit( constant( $constant ) ), $folder ); $potential_folder = trailingslashit( $potential_folder ); if ( $this->is_dir( $potential_folder ) ) { $this->cache[ $folder ] = $potential_folder; + return $potential_folder; } } } } elseif ( 'direct' === $this->method ) { $folder = str_replace( '\\', '/', $folder ); // Windows path sanitisation. + return trailingslashit( $folder ); } @@ -223,12 +229,16 @@ class WP_Filesystem_Base { if ( $this->exists( $folder ) ) { // Folder exists at that absolute path. $folder = trailingslashit( $folder ); $this->cache[ $folder ] = $folder; + return $folder; } + $return = $this->search_for_folder( $folder ); + if ( $return ) { $this->cache[ $folder ] = $return; } + return $return; } @@ -279,6 +289,7 @@ class WP_Filesystem_Base { // Let's try that folder: $newdir = trailingslashit( path_join( $base, $key ) ); + if ( $this->verbose ) { /* translators: %s: Directory name. */ printf( "\n" . __( 'Changing to %s' ) . "
\n", $newdir ); @@ -287,6 +298,7 @@ class WP_Filesystem_Base { // Only search for the remaining path tokens in the directory, not the full path again. $newfolder = implode( '/', array_slice( $folder_parts, $index + 1 ) ); $ret = $this->search_for_folder( $newfolder, $newdir, $loop ); + if ( $ret ) { return $ret; } @@ -300,6 +312,7 @@ class WP_Filesystem_Base { /* translators: %s: Directory name. */ printf( "\n" . __( 'Found %s' ) . "
\n", $base . $last_path ); } + return trailingslashit( $base . $last_path ); } @@ -329,6 +342,7 @@ class WP_Filesystem_Base { */ public function gethchmod( $file ) { $perms = intval( $this->getchmod( $file ), 8 ); + if ( ( $perms & 0xC000 ) == 0xC000 ) { // Socket. $info = 's'; } elseif ( ( $perms & 0xA000 ) == 0xA000 ) { // Symbolic Link. @@ -367,6 +381,7 @@ class WP_Filesystem_Base { $info .= ( ( $perms & 0x0001 ) ? ( ( $perms & 0x0200 ) ? 't' : 'x' ) : ( ( $perms & 0x0200 ) ? 'T' : '-' ) ); + return $info; } @@ -402,6 +417,7 @@ class WP_Filesystem_Base { for ( $i = 0, $c = count( $attarray ); $i < $c; $i++ ) { $key = array_search( $attarray[ $i ], $legal, true ); + if ( $key ) { $realmode .= $legal[ $key ]; } @@ -420,6 +436,7 @@ class WP_Filesystem_Base { $newmode .= $mode[1] + $mode[2] + $mode[3]; $newmode .= $mode[4] + $mode[5] + $mode[6]; $newmode .= $mode[7] + $mode[8] + $mode[9]; + return $newmode; } diff --git a/src/wp-admin/includes/class-wp-filesystem-direct.php b/src/wp-admin/includes/class-wp-filesystem-direct.php index 17ccad8d5d..3881f70afc 100644 --- a/src/wp-admin/includes/class-wp-filesystem-direct.php +++ b/src/wp-admin/includes/class-wp-filesystem-direct.php @@ -64,6 +64,7 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { */ public function put_contents( $file, $contents, $mode = false ) { $fp = @fopen( $file, 'wb' ); + if ( ! $fp ) { return false; } @@ -125,15 +126,19 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { if ( ! $this->exists( $file ) ) { return false; } + if ( ! $recursive ) { return chgrp( $file, $group ); } + if ( ! $this->is_dir( $file ) ) { return chgrp( $file, $group ); } + // Is a directory, and we want recursive. $file = trailingslashit( $file ); $filelist = $this->dirlist( $file ); + foreach ( $filelist as $filename ) { $this->chgrp( $file . $filename, $group, $recursive ); } @@ -167,9 +172,11 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { if ( ! $recursive || ! $this->is_dir( $file ) ) { return chmod( $file, $mode ); } + // Is a directory, and we want recursive. $file = trailingslashit( $file ); $filelist = $this->dirlist( $file ); + foreach ( (array) $filelist as $filename => $filemeta ) { $this->chmod( $file . $filename, $mode, $recursive ); } @@ -192,17 +199,22 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { if ( ! $this->exists( $file ) ) { return false; } + if ( ! $recursive ) { return chown( $file, $owner ); } + if ( ! $this->is_dir( $file ) ) { return chown( $file, $owner ); } + // Is a directory, and we want recursive. $filelist = $this->dirlist( $file ); + foreach ( $filelist as $filename ) { $this->chown( $file . '/' . $filename, $owner, $recursive ); } + return true; } @@ -216,16 +228,21 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { */ public function owner( $file ) { $owneruid = @fileowner( $file ); + if ( ! $owneruid ) { return false; } + if ( ! function_exists( 'posix_getpwuid' ) ) { return $owneruid; } + $ownerarray = posix_getpwuid( $owneruid ); + if ( ! $ownerarray ) { return false; } + return $ownerarray['name']; } @@ -253,16 +270,21 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { */ public function group( $file ) { $gid = @filegroup( $file ); + if ( ! $gid ) { return false; } + if ( ! function_exists( 'posix_getgrgid' ) ) { return $gid; } + $grouparray = posix_getgrgid( $gid ); + if ( ! $grouparray ) { return false; } + return $grouparray['name']; } @@ -285,9 +307,11 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { } $rtval = copy( $source, $destination ); + if ( $mode ) { $this->chmod( $destination, $mode ); } + return $rtval; } @@ -314,6 +338,7 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { if ( $this->copy( $source, $destination, $overwrite ) && $this->exists( $destination ) ) { $this->delete( $source ); + return true; } else { return false; @@ -337,11 +362,13 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem. return false; } + $file = str_replace( '\\', '/', $file ); // For Win32, occasional problems deleting files otherwise. if ( 'f' === $type || $this->is_file( $file ) ) { return @unlink( $file ); } + if ( ! $recursive && $this->is_dir( $file ) ) { return @rmdir( $file ); } @@ -351,6 +378,7 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { $filelist = $this->dirlist( $file, true ); $retval = true; + if ( is_array( $filelist ) ) { foreach ( $filelist as $filename => $fileinfo ) { if ( ! $this->delete( $file . $filename, $recursive, $fileinfo['type'] ) ) { @@ -480,9 +508,11 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { if ( 0 == $time ) { $time = time(); } + if ( 0 == $atime ) { $atime = time(); } + return touch( $file, $time, $atime ); } @@ -503,6 +533,7 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) { // Safe mode fails with a trailing slash under certain PHP versions. $path = untrailingslashit( $path ); + if ( empty( $path ) ) { return false; } @@ -514,13 +545,17 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { if ( ! @mkdir( $path ) ) { return false; } + $this->chmod( $path, $chmod ); + if ( $chown ) { $this->chown( $path, $chown ); } + if ( $chgrp ) { $this->chgrp( $path, $chgrp ); } + return true; } @@ -576,6 +611,7 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { } $dir = dir( $path ); + if ( ! $dir ) { return false; } @@ -619,8 +655,10 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { $ret[ $struc['name'] ] = $struc; } + $dir->close(); unset( $dir ); + return $ret; } } diff --git a/src/wp-admin/includes/class-wp-filesystem-ftpext.php b/src/wp-admin/includes/class-wp-filesystem-ftpext.php index b1a65fc524..d165b2bc3e 100644 --- a/src/wp-admin/includes/class-wp-filesystem-ftpext.php +++ b/src/wp-admin/includes/class-wp-filesystem-ftpext.php @@ -69,6 +69,7 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { } $this->options['ssl'] = false; + if ( isset( $opt['connection_type'] ) && 'ftps' === $opt['connection_type'] ) { $this->options['ssl'] = true; } @@ -97,6 +98,7 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { $this->options['hostname'] . ':' . $this->options['port'] ) ); + return false; } @@ -109,11 +111,13 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { $this->options['username'] ) ); + return false; } // 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 ); } @@ -131,29 +135,30 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { * 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( $temphangle ); unlink( $tempfile ); + return $contents; } @@ -181,10 +186,10 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { * @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; } @@ -192,21 +197,21 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { 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,9 +228,11 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { */ public function cwd() { $cwd = ftp_pwd( $this->link ); + if ( $cwd ) { $cwd = trailingslashit( $cwd ); } + return $cwd; } @@ -267,6 +274,7 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { // 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 ); } @@ -276,6 +284,7 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { if ( ! function_exists( 'ftp_chmod' ) ) { return (bool) ftp_site( $this->link, sprintf( 'CHMOD %o %s', $mode, $file ) ); } + return (bool) ftp_chmod( $this->link, $mode, $file ); } @@ -289,6 +298,7 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { */ public function owner( $file ) { $dir = $this->dirlist( $file ); + return $dir[ $file ]['owner']; } @@ -302,6 +312,7 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { */ public function getchmod( $file ) { $dir = $this->dirlist( $file ); + return $dir[ $file ]['permsn']; } @@ -315,6 +326,7 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { */ public function group( $file ) { $dir = $this->dirlist( $file ); + return $dir[ $file ]['group']; } @@ -335,10 +347,13 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { if ( ! $overwrite && $this->exists( $destination ) ) { return false; } + $content = $this->get_contents( $source ); + if ( false === $content ) { return false; } + return $this->put_contents( $destination, $content, $mode ); } @@ -373,19 +388,23 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { if ( empty( $file ) ) { return false; } + if ( 'f' === $type || $this->is_file( $file ) ) { return ftp_delete( $this->link, $file ); } + if ( ! $recursive ) { 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 ); } @@ -430,10 +449,12 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { 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; } @@ -531,6 +552,7 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { */ public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) { $path = untrailingslashit( $path ); + if ( empty( $path ) ) { return false; } @@ -538,7 +560,9 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { if ( ! ftp_mkdir( $this->link, $path ) ) { return false; } + $this->chmod( $path, $chmod ); + return true; } @@ -563,23 +587,28 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { */ 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; // 4-digit year fix. } + $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]; @@ -591,15 +620,19 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { $b['name'] = $lucifer[8]; } 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'] ) { @@ -607,20 +640,24 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { } 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 ( 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]; @@ -630,6 +667,7 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { $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]; } @@ -678,10 +716,13 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { } $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 ); + @ftp_chdir( $this->link, $pwd ); if ( empty( $list ) ) { // Empty array = non-existent folder (real folder will show . at least). @@ -689,8 +730,10 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { } $dirlist = array(); + foreach ( $list as $k => $v ) { $entry = $this->parselisting( $v ); + if ( empty( $entry ) ) { continue; } @@ -711,6 +754,7 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { } $ret = array(); + foreach ( (array) $dirlist as $struc ) { if ( 'd' === $struc['type'] ) { if ( $recursive ) { @@ -722,6 +766,7 @@ class WP_Filesystem_FTPext extends WP_Filesystem_Base { $ret[ $struc['name'] ] = $struc; } + return $ret; } diff --git a/src/wp-admin/includes/class-wp-filesystem-ftpsockets.php b/src/wp-admin/includes/class-wp-filesystem-ftpsockets.php index c200372788..83619bfd35 100644 --- a/src/wp-admin/includes/class-wp-filesystem-ftpsockets.php +++ b/src/wp-admin/includes/class-wp-filesystem-ftpsockets.php @@ -36,6 +36,7 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { if ( ! include_once ABSPATH . 'wp-admin/includes/class-ftp.php' ) { return; } + $this->ftp = new ftp(); if ( empty( $opt['port'] ) ) { @@ -87,6 +88,7 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { $this->options['hostname'] . ':' . $this->options['port'] ) ); + return false; } @@ -99,6 +101,7 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { $this->options['hostname'] . ':' . $this->options['port'] ) ); + return false; } @@ -111,12 +114,14 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { $this->options['username'] ) ); + return false; } $this->ftp->SetType( FTP_BINARY ); $this->ftp->Passive( true ); $this->ftp->setTimeout( FS_TIMEOUT ); + return true; } @@ -134,11 +139,11 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { return false; } - $temp = wp_tempnam( $file ); + $tempfile = wp_tempnam( $file ); + $temphandle = fopen( $tempfile, 'w+' ); - $temphandle = fopen( $temp, 'w+' ); if ( ! $temphandle ) { - unlink( $temp ); + unlink( $tempfile ); return false; } @@ -146,7 +151,7 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { if ( ! $this->ftp->fget( $temphandle, $file ) ) { fclose( $temphandle ); - unlink( $temp ); + unlink( $tempfile ); reset_mbstring_encoding(); @@ -163,7 +168,8 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { } fclose( $temphandle ); - unlink( $temp ); + unlink( $tempfile ); + return $contents; } @@ -191,10 +197,11 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { * @return bool True on success, false on failure. */ public function put_contents( $file, $contents, $mode = false ) { - $temp = wp_tempnam( $file ); - $temphandle = @fopen( $temp, 'w+' ); + $tempfile = wp_tempnam( $file ); + $temphandle = @fopen( $tempfile, 'w+' ); + if ( ! $temphandle ) { - unlink( $temp ); + unlink( $tempfile ); return false; } @@ -202,9 +209,10 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { mbstring_binary_safe_encoding(); $bytes_written = fwrite( $temphandle, $contents ); + if ( false === $bytes_written || strlen( $contents ) != $bytes_written ) { fclose( $temphandle ); - unlink( $temp ); + unlink( $tempfile ); reset_mbstring_encoding(); @@ -218,7 +226,7 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { reset_mbstring_encoding(); fclose( $temphandle ); - unlink( $temp ); + unlink( $tempfile ); $this->chmod( $file, $mode ); @@ -234,9 +242,11 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { */ public function cwd() { $cwd = $this->ftp->pwd(); + if ( $cwd ) { $cwd = trailingslashit( $cwd ); } + return $cwd; } @@ -278,6 +288,7 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { // 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 ); } @@ -297,6 +308,7 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { */ public function owner( $file ) { $dir = $this->dirlist( $file ); + return $dir[ $file ]['owner']; } @@ -310,6 +322,7 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { */ public function getchmod( $file ) { $dir = $this->dirlist( $file ); + return $dir[ $file ]['permsn']; } @@ -323,6 +336,7 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { */ public function group( $file ) { $dir = $this->dirlist( $file ); + return $dir[ $file ]['group']; } @@ -345,6 +359,7 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { } $content = $this->get_contents( $source ); + if ( false === $content ) { return false; } @@ -383,9 +398,11 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { if ( empty( $file ) ) { return false; } + if ( 'f' === $type || $this->is_file( $file ) ) { return $this->ftp->delete( $file ); } + if ( ! $recursive ) { return $this->ftp->rmdir( $file ); } @@ -424,9 +441,11 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { if ( $this->is_dir( $file ) ) { return false; } + if ( $this->exists( $file ) ) { return true; } + return false; } @@ -440,10 +459,12 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { */ public function is_dir( $path ) { $cwd = $this->cwd(); + if ( $this->chdir( $path ) ) { $this->chdir( $cwd ); return true; } + return false; } @@ -541,6 +562,7 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { */ public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) { $path = untrailingslashit( $path ); + if ( empty( $path ) ) { return false; } @@ -548,10 +570,13 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { if ( ! $this->ftp->mkdir( $path ) ) { return false; } + if ( ! $chmod ) { $chmod = FS_CHMOD_DIR; } + $this->chmod( $path, $chmod ); + return true; } @@ -605,6 +630,7 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { mbstring_binary_safe_encoding(); $list = $this->ftp->dirlist( $path ); + if ( empty( $list ) && ! $this->exists( $path ) ) { reset_mbstring_encoding(); @@ -613,6 +639,7 @@ class WP_Filesystem_ftpsockets extends WP_Filesystem_Base { } $ret = array(); + foreach ( $list as $struc ) { if ( '.' === $struc['name'] || '..' === $struc['name'] ) { diff --git a/src/wp-admin/includes/class-wp-filesystem-ssh2.php b/src/wp-admin/includes/class-wp-filesystem-ssh2.php index 94853a650b..7ee9f9a78f 100644 --- a/src/wp-admin/includes/class-wp-filesystem-ssh2.php +++ b/src/wp-admin/includes/class-wp-filesystem-ssh2.php @@ -132,6 +132,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { $this->options['hostname'] . ':' . $this->options['port'] ) ); + return false; } @@ -145,6 +146,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { $this->options['username'] ) ); + return false; } } else { @@ -157,11 +159,13 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { $this->options['username'] ) ); + return false; } } $this->sftp_link = ssh2_sftp( $this->link ); + if ( ! $this->sftp_link ) { $this->errors->add( 'connect', @@ -171,6 +175,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { $this->options['hostname'] . ':' . $this->options['port'] ) ); + return false; } @@ -194,6 +199,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { if ( '/' === $path ) { $path = '/./'; } + return 'ssh2.sftp://' . $this->sftp_link . '/' . ltrim( $path, '/' ); } @@ -211,6 +217,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { } $stream = ssh2_exec( $this->link, $command ); + if ( ! $stream ) { $this->errors->add( 'command', @@ -232,6 +239,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { return $data; } } + return false; } @@ -292,9 +300,11 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { */ public function cwd() { $cwd = ssh2_sftp_realpath( $this->sftp_link, '.' ); + if ( $cwd ) { $cwd = trailingslashit( trim( $cwd ) ); } + return $cwd; } @@ -325,9 +335,11 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { if ( ! $this->exists( $file ) ) { return false; } + if ( ! $recursive || ! $this->is_dir( $file ) ) { return $this->run_command( sprintf( 'chgrp %s %s', escapeshellarg( $group ), escapeshellarg( $file ) ), true ); } + return $this->run_command( sprintf( 'chgrp -R %s %s', escapeshellarg( $group ), escapeshellarg( $file ) ), true ); } @@ -361,6 +373,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { if ( ! $recursive || ! $this->is_dir( $file ) ) { return $this->run_command( sprintf( 'chmod %o %s', $mode, escapeshellarg( $file ) ), true ); } + return $this->run_command( sprintf( 'chmod -R %o %s', $mode, escapeshellarg( $file ) ), true ); } @@ -379,9 +392,11 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { if ( ! $this->exists( $file ) ) { return false; } + if ( ! $recursive || ! $this->is_dir( $file ) ) { return $this->run_command( sprintf( 'chown %s %s', escapeshellarg( $owner ), escapeshellarg( $file ) ), true ); } + return $this->run_command( sprintf( 'chown -R %s %s', escapeshellarg( $owner ), escapeshellarg( $file ) ), true ); } @@ -395,16 +410,21 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { */ public function owner( $file ) { $owneruid = @fileowner( $this->sftp_path( $file ) ); + if ( ! $owneruid ) { return false; } + if ( ! function_exists( 'posix_getpwuid' ) ) { return $owneruid; } + $ownerarray = posix_getpwuid( $owneruid ); + if ( ! $ownerarray ) { return false; } + return $ownerarray['name']; } @@ -430,16 +450,21 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { */ public function group( $file ) { $gid = @filegroup( $this->sftp_path( $file ) ); + if ( ! $gid ) { return false; } + if ( ! function_exists( 'posix_getgrgid' ) ) { return $gid; } + $grouparray = posix_getgrgid( $gid ); + if ( ! $grouparray ) { return false; } + return $grouparray['name']; } @@ -460,10 +485,13 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { if ( ! $overwrite && $this->exists( $destination ) ) { return false; } + $content = $this->get_contents( $source ); + if ( false === $content ) { return false; } + return $this->put_contents( $destination, $content, $mode ); } @@ -508,15 +536,19 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { if ( 'f' === $type || $this->is_file( $file ) ) { return ssh2_sftp_unlink( $this->sftp_link, $file ); } + if ( ! $recursive ) { return ssh2_sftp_rmdir( $this->sftp_link, $file ); } + $filelist = $this->dirlist( $file ); + if ( is_array( $filelist ) ) { foreach ( $filelist as $filename => $fileinfo ) { $this->delete( $file . '/' . $filename, $recursive, $fileinfo['type'] ); } } + return ssh2_sftp_rmdir( $this->sftp_link, $file ); } @@ -650,6 +682,7 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { */ public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) { $path = untrailingslashit( $path ); + if ( empty( $path ) ) { return false; } @@ -657,15 +690,19 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { if ( ! $chmod ) { $chmod = FS_CHMOD_DIR; } + if ( ! ssh2_sftp_mkdir( $this->sftp_link, $path, $chmod, true ) ) { return false; } + if ( $chown ) { $this->chown( $path, $chown ); } + if ( $chgrp ) { $this->chgrp( $path, $chgrp ); } + return true; } @@ -764,8 +801,10 @@ class WP_Filesystem_SSH2 extends WP_Filesystem_Base { $ret[ $struc['name'] ] = $struc; } + $dir->close(); unset( $dir ); + return $ret; } }