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]+|