wp/wp-admin/includes/class-wp-filesystem-ftpsockets.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
--- a/wp/wp-admin/includes/class-wp-filesystem-ftpsockets.php	Tue Oct 22 16:11:46 2019 +0200
+++ b/wp/wp-admin/includes/class-wp-filesystem-ftpsockets.php	Tue Dec 15 13:49:49 2020 +0100
@@ -33,9 +33,10 @@
 		$this->errors = new WP_Error();
 
 		// Check if possible to use ftp functions.
-		if ( ! @include_once( ABSPATH . 'wp-admin/includes/class-ftp.php' ) ) {
+		if ( ! include_once ABSPATH . 'wp-admin/includes/class-ftp.php' ) {
 			return;
 		}
+
 		$this->ftp = new ftp();
 
 		if ( empty( $opt['port'] ) ) {
@@ -81,42 +82,46 @@
 		if ( ! $this->ftp->SetServer( $this->options['hostname'], $this->options['port'] ) ) {
 			$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 ( ! $this->ftp->connect() ) {
 			$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 ( ! $this->ftp->login( $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;
 		}
 
 		$this->ftp->SetType( FTP_BINARY );
 		$this->ftp->Passive( true );
 		$this->ftp->setTimeout( FS_TIMEOUT );
+
 		return true;
 	}
 
@@ -134,10 +139,11 @@
 			return false;
 		}
 
-		$temp = wp_tempnam( $file );
+		$tempfile   = wp_tempnam( $file );
+		$temphandle = fopen( $tempfile, 'w+' );
 
-		if ( ! $temphandle = fopen( $temp, 'w+' ) ) {
-			unlink( $temp );
+		if ( ! $temphandle ) {
+			unlink( $tempfile );
 			return false;
 		}
 
@@ -145,16 +151,16 @@
 
 		if ( ! $this->ftp->fget( $temphandle, $file ) ) {
 			fclose( $temphandle );
-			unlink( $temp );
+			unlink( $tempfile );
 
 			reset_mbstring_encoding();
 
-			return ''; // Blank document, File does exist, It's just blank.
+			return ''; // Blank document. File does exist, it's just blank.
 		}
 
 		reset_mbstring_encoding();
 
-		fseek( $temphandle, 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( $temphandle ) ) {
@@ -162,7 +168,8 @@
 		}
 
 		fclose( $temphandle );
-		unlink( $temp );
+		unlink( $tempfile );
+
 		return $contents;
 	}
 
@@ -190,33 +197,36 @@
 	 * @return bool True on success, false on failure.
 	 */
 	public function put_contents( $file, $contents, $mode = false ) {
-		$temp = wp_tempnam( $file );
-		if ( ! $temphandle = @fopen( $temp, 'w+' ) ) {
-			unlink( $temp );
+		$tempfile   = wp_tempnam( $file );
+		$temphandle = @fopen( $tempfile, 'w+' );
+
+		if ( ! $temphandle ) {
+			unlink( $tempfile );
 			return false;
 		}
 
-		// The FTP class uses string functions internally during file download/upload
+		// The FTP class uses string functions internally during file download/upload.
 		mbstring_binary_safe_encoding();
 
 		$bytes_written = fwrite( $temphandle, $contents );
-		if ( false === $bytes_written || $bytes_written != strlen( $contents ) ) {
+
+		if ( false === $bytes_written || strlen( $contents ) != $bytes_written ) {
 			fclose( $temphandle );
-			unlink( $temp );
+			unlink( $tempfile );
 
 			reset_mbstring_encoding();
 
 			return false;
 		}
 
-		fseek( $temphandle, 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 = $this->ftp->fput( $file, $temphandle );
 
 		reset_mbstring_encoding();
 
 		fclose( $temphandle );
-		unlink( $temp );
+		unlink( $tempfile );
 
 		$this->chmod( $file, $mode );
 
@@ -232,9 +242,11 @@
 	 */
 	public function cwd() {
 		$cwd = $this->ftp->pwd();
+
 		if ( $cwd ) {
 			$cwd = trailingslashit( $cwd );
 		}
+
 		return $cwd;
 	}
 
@@ -258,7 +270,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.
 	 */
@@ -276,12 +288,13 @@
 		// 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.
 		return $this->ftp->chmod( $file, $mode );
 	}
 
@@ -295,6 +308,7 @@
 	 */
 	public function owner( $file ) {
 		$dir = $this->dirlist( $file );
+
 		return $dir[ $file ]['owner'];
 	}
 
@@ -308,6 +322,7 @@
 	 */
 	public function getchmod( $file ) {
 		$dir = $this->dirlist( $file );
+
 		return $dir[ $file ]['permsn'];
 	}
 
@@ -321,6 +336,7 @@
 	 */
 	public function group( $file ) {
 		$dir = $this->dirlist( $file );
+
 		return $dir[ $file ]['group'];
 	}
 
@@ -343,6 +359,7 @@
 		}
 
 		$content = $this->get_contents( $source );
+
 		if ( false === $content ) {
 			return false;
 		}
@@ -371,7 +388,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.
@@ -381,9 +398,11 @@
 		if ( empty( $file ) ) {
 			return false;
 		}
-		if ( 'f' == $type || $this->is_file( $file ) ) {
+
+		if ( 'f' === $type || $this->is_file( $file ) ) {
 			return $this->ftp->delete( $file );
 		}
+
 		if ( ! $recursive ) {
 			return $this->ftp->rmdir( $file );
 		}
@@ -406,7 +425,7 @@
 			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.
 		// Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server.
 	}
 
@@ -422,9 +441,11 @@
 		if ( $this->is_dir( $file ) ) {
 			return false;
 		}
+
 		if ( $this->exists( $file ) ) {
 			return true;
 		}
+
 		return false;
 	}
 
@@ -438,10 +459,12 @@
 	 */
 	public function is_dir( $path ) {
 		$cwd = $this->cwd();
+
 		if ( $this->chdir( $path ) ) {
 			$this->chdir( $cwd );
 			return true;
 		}
+
 		return false;
 	}
 
@@ -539,6 +562,7 @@
 	 */
 	public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) {
 		$path = untrailingslashit( $path );
+
 		if ( empty( $path ) ) {
 			return false;
 		}
@@ -546,10 +570,13 @@
 		if ( ! $this->ftp->mkdir( $path ) ) {
 			return false;
 		}
+
 		if ( ! $chmod ) {
 			$chmod = FS_CHMOD_DIR;
 		}
+
 		$this->chmod( $path, $chmod );
+
 		return true;
 	}
 
@@ -603,6 +630,7 @@
 		mbstring_binary_safe_encoding();
 
 		$list = $this->ftp->dirlist( $path );
+
 		if ( empty( $list ) && ! $this->exists( $path ) ) {
 
 			reset_mbstring_encoding();
@@ -611,13 +639,14 @@
 		}
 
 		$ret = array();
+
 		foreach ( $list as $struc ) {
 
-			if ( '.' == $struc['name'] || '..' == $struc['name'] ) {
+			if ( '.' === $struc['name'] || '..' === $struc['name'] ) {
 				continue;
 			}
 
-			if ( ! $include_hidden && '.' == $struc['name'][0] ) {
+			if ( ! $include_hidden && '.' === $struc['name'][0] ) {
 				continue;
 			}
 
@@ -625,7 +654,7 @@
 				continue;
 			}
 
-			if ( 'd' == $struc['type'] ) {
+			if ( 'd' === $struc['type'] ) {
 				if ( $recursive ) {
 					$struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive );
 				} else {
@@ -633,12 +662,12 @@
 				}
 			}
 
-			// Replace symlinks formatted as "source -> target" with just the source name
+			// Replace symlinks formatted as "source -> target" with just the source name.
 			if ( $struc['islink'] ) {
 				$struc['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $struc['name'] );
 			}
 
-			// Add the Octal representation of the file permissions
+			// Add the octal representation of the file permissions.
 			$struc['permsn'] = $this->getnumchmodfromh( $struc['perms'] );
 
 			$ret[ $struc['name'] ] = $struc;