Skip to content

Commit

Permalink
Coding Standards: Add some space around control structures in `WP_Fil…
Browse files Browse the repository at this point in the history
…esystem_*` 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
  • Loading branch information
SergeyBiryukov committed Jun 19, 2020
1 parent 13614f7 commit d4f4f42
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 27 deletions.
17 changes: 17 additions & 0 deletions src/wp-admin/includes/class-wp-filesystem-base.php
Expand Up @@ -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;
}

Expand Down Expand Up @@ -188,6 +190,7 @@ public function find_folder( $folder ) {
if ( ! defined( $constant ) ) {
continue;
}

if ( $folder === $dir ) {
return trailingslashit( constant( $constant ) );
}
Expand All @@ -198,18 +201,21 @@ public function find_folder( $folder ) {
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 );
}

Expand All @@ -223,12 +229,16 @@ public function find_folder( $folder ) {
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;
}

Expand Down Expand Up @@ -279,6 +289,7 @@ public function search_for_folder( $folder, $base = '.', $loop = false ) {

// Let's try that folder:
$newdir = trailingslashit( path_join( $base, $key ) );

if ( $this->verbose ) {
/* translators: %s: Directory name. */
printf( "\n" . __( 'Changing to %s' ) . "<br/>\n", $newdir );
Expand All @@ -287,6 +298,7 @@ public function search_for_folder( $folder, $base = '.', $loop = false ) {
// 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;
}
Expand All @@ -300,6 +312,7 @@ public function search_for_folder( $folder, $base = '.', $loop = false ) {
/* translators: %s: Directory name. */
printf( "\n" . __( 'Found %s' ) . "<br/>\n", $base . $last_path );
}

return trailingslashit( $base . $last_path );
}

Expand Down Expand Up @@ -329,6 +342,7 @@ public function search_for_folder( $folder, $base = '.', $loop = false ) {
*/
public function gethchmod( $file ) {
$perms = intval( $this->getchmod( $file ), 8 );

if ( ( $perms & 0xC000 ) == 0xC000 ) { // Socket.
$info = 's';
} elseif ( ( $perms & 0xA000 ) == 0xA000 ) { // Symbolic Link.
Expand Down Expand Up @@ -367,6 +381,7 @@ public function gethchmod( $file ) {
$info .= ( ( $perms & 0x0001 ) ?
( ( $perms & 0x0200 ) ? 't' : 'x' ) :
( ( $perms & 0x0200 ) ? 'T' : '-' ) );

return $info;
}

Expand Down Expand Up @@ -402,6 +417,7 @@ public function getnumchmodfromh( $mode ) {

for ( $i = 0, $c = count( $attarray ); $i < $c; $i++ ) {
$key = array_search( $attarray[ $i ], $legal, true );

if ( $key ) {
$realmode .= $legal[ $key ];
}
Expand All @@ -420,6 +436,7 @@ public function getnumchmodfromh( $mode ) {
$newmode .= $mode[1] + $mode[2] + $mode[3];
$newmode .= $mode[4] + $mode[5] + $mode[6];
$newmode .= $mode[7] + $mode[8] + $mode[9];

return $newmode;
}

Expand Down
38 changes: 38 additions & 0 deletions src/wp-admin/includes/class-wp-filesystem-direct.php
Expand Up @@ -64,6 +64,7 @@ public function get_contents_array( $file ) {
*/
public function put_contents( $file, $contents, $mode = false ) {
$fp = @fopen( $file, 'wb' );

if ( ! $fp ) {
return false;
}
Expand Down Expand Up @@ -125,15 +126,19 @@ public function chgrp( $file, $group, $recursive = false ) {
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 );
}
Expand Down Expand Up @@ -167,9 +172,11 @@ public function chmod( $file, $mode = false, $recursive = false ) {
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 );
}
Expand All @@ -192,17 +199,22 @@ public function chown( $file, $owner, $recursive = false ) {
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;
}

Expand All @@ -216,16 +228,21 @@ public function chown( $file, $owner, $recursive = false ) {
*/
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'];
}

Expand Down Expand Up @@ -253,16 +270,21 @@ public function getchmod( $file ) {
*/
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'];
}

Expand All @@ -285,9 +307,11 @@ public function copy( $source, $destination, $overwrite = false, $mode = false )
}

$rtval = copy( $source, $destination );

if ( $mode ) {
$this->chmod( $destination, $mode );
}

return $rtval;
}

Expand All @@ -314,6 +338,7 @@ public function move( $source, $destination, $overwrite = false ) {

if ( $this->copy( $source, $destination, $overwrite ) && $this->exists( $destination ) ) {
$this->delete( $source );

return true;
} else {
return false;
Expand All @@ -337,11 +362,13 @@ public function delete( $file, $recursive = false, $type = false ) {
// 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 );
}
Expand All @@ -351,6 +378,7 @@ public function delete( $file, $recursive = false, $type = false ) {
$filelist = $this->dirlist( $file, true );

$retval = true;

if ( is_array( $filelist ) ) {
foreach ( $filelist as $filename => $fileinfo ) {
if ( ! $this->delete( $file . $filename, $recursive, $fileinfo['type'] ) ) {
Expand Down Expand Up @@ -480,9 +508,11 @@ public function touch( $file, $time = 0, $atime = 0 ) {
if ( 0 == $time ) {
$time = time();
}

if ( 0 == $atime ) {
$atime = time();
}

return touch( $file, $time, $atime );
}

Expand All @@ -503,6 +533,7 @@ public function touch( $file, $time = 0, $atime = 0 ) {
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;
}
Expand All @@ -514,13 +545,17 @@ public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) {
if ( ! @mkdir( $path ) ) {
return false;
}

$this->chmod( $path, $chmod );

if ( $chown ) {
$this->chown( $path, $chown );
}

if ( $chgrp ) {
$this->chgrp( $path, $chgrp );
}

return true;
}

Expand Down Expand Up @@ -576,6 +611,7 @@ public function dirlist( $path, $include_hidden = true, $recursive = false ) {
}

$dir = dir( $path );

if ( ! $dir ) {
return false;
}
Expand Down Expand Up @@ -619,8 +655,10 @@ public function dirlist( $path, $include_hidden = true, $recursive = false ) {

$ret[ $struc['name'] ] = $struc;
}

$dir->close();
unset( $dir );

return $ret;
}
}

0 comments on commit d4f4f42

Please sign in to comment.