+/**
+ * Determines which method to use for reading, writing, modifying, or deleting
+ * files on the filesystem.
+ *
+ * The priority of the transports are: Direct, SSH2, FTP PHP Extension, FTP Sockets
+ * (Via Sockets class, or `fsockopen()`). Valid values for these are: 'direct', 'ssh2',
+ * 'ftpext' or 'ftpsockets'.
+ *
+ * The return value can be overridden by defining the `FS_METHOD` constant in `wp-config.php`,
+ * or filtering via {@see 'filesystem_method'}.
+ *
+ * @link https://codex.wordpress.org/Editing_wp-config.php#WordPress_Upgrade_Constants
+ *
+ * Plugins may define a custom transport handler, See WP_Filesystem().
+ *
+ * @since 2.5.0
+ *
+ * @global callable $_wp_filesystem_direct_method
+ *
+ * @param array $args Optional. Connection details. Default empty array.
+ * @param string $context Optional. Full path to the directory that is tested
+ * for being writable. Default false.
+ * @param bool $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable.
+ * Default false.
+ * @return string The transport to use, see description for valid return values.
+ */
+function get_filesystem_method( $args = array(), $context = false, $allow_relaxed_file_ownership = false ) {
+ $method = defined('FS_METHOD') ? FS_METHOD : false; // Please ensure that this is either 'direct', 'ssh2', 'ftpext' or 'ftpsockets'
+
+ if ( ! $context ) {
+ $context = WP_CONTENT_DIR;
+ }
+
+ // If the directory doesn't exist (wp-content/languages) then use the parent directory as we'll create it.
+ if ( WP_LANG_DIR == $context && ! is_dir( $context ) ) {
+ $context = dirname( $context );
+ }
+
+ $context = trailingslashit( $context );
+
+ if ( ! $method ) {
+
+ $temp_file_name = $context . 'temp-write-test-' . time();
+ $temp_handle = @fopen($temp_file_name, 'w');
+ if ( $temp_handle ) {
+
+ // Attempt to determine the file owner of the WordPress files, and that of newly created files
+ $wp_file_owner = $temp_file_owner = false;
+ if ( function_exists('fileowner') ) {
+ $wp_file_owner = @fileowner( __FILE__ );
+ $temp_file_owner = @fileowner( $temp_file_name );
+ }
+
+ if ( $wp_file_owner !== false && $wp_file_owner === $temp_file_owner ) {
+ // WordPress is creating files as the same owner as the WordPress files,
+ // this means it's safe to modify & create new files via PHP.
+ $method = 'direct';
+ $GLOBALS['_wp_filesystem_direct_method'] = 'file_owner';
+ } elseif ( $allow_relaxed_file_ownership ) {
+ // The $context directory is writable, and $allow_relaxed_file_ownership is set, this means we can modify files
+ // safely in this directory. This mode doesn't create new files, only alter existing ones.
+ $method = 'direct';
+ $GLOBALS['_wp_filesystem_direct_method'] = 'relaxed_ownership';
+ }
+
+ @fclose($temp_handle);
+ @unlink($temp_file_name);
+ }
+ }
+
+ if ( ! $method && isset($args['connection_type']) && 'ssh' == $args['connection_type'] && extension_loaded('ssh2') && function_exists('stream_get_contents') ) $method = 'ssh2';
+ if ( ! $method && extension_loaded('ftp') ) $method = 'ftpext';
+ if ( ! $method && ( extension_loaded('sockets') || function_exists('fsockopen') ) ) $method = 'ftpsockets'; //Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread
+
+ /**
+ * Filter the filesystem method to use.
+ *
+ * @since 2.6.0
+ *
+ * @param string $method Filesystem method to return.
+ * @param array $args An array of connection details for the method.
+ * @param string $context Full path to the directory that is tested for being writable.
+ * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable.
+ */
+ return apply_filters( 'filesystem_method', $method, $args, $context, $allow_relaxed_file_ownership );
+}
+
+/**
+ * Displays a form to the user to request for their FTP/SSH details in order
+ * to connect to the filesystem.
+ *
+ * All chosen/entered details are saved, Excluding the Password.
+ *
+ * Hostnames may be in the form of hostname:portnumber (eg: wordpress.org:2467)
+ * to specify an alternate FTP/SSH port.
+ *
+ * Plugins may override this form by returning true|false via the
+ * {@see 'request_filesystem_credentials'} filter.
+ *
+ * @since 2.5.
+ *
+ * @todo Properly mark optional arguments as such
+ *
+ * @global string $pagenow
+ *
+ * @param string $form_post the URL to post the form to
+ * @param string $type the chosen Filesystem method in use
+ * @param bool $error if the current request has failed to connect
+ * @param string $context The directory which is needed access to, The write-test will be performed on this directory by get_filesystem_method()
+ * @param array $extra_fields Extra POST fields which should be checked for to be included in the post.
+ * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable.
+ * @return bool False on failure. True on success.
+ */
+function request_filesystem_credentials($form_post, $type = '', $error = false, $context = false, $extra_fields = null, $allow_relaxed_file_ownership = false ) {
+ global $pagenow;
+
+ /**
+ * Filter the filesystem credentials form output.
+ *
+ * Returning anything other than an empty string will effectively short-circuit
+ * output of the filesystem credentials form, returning that value instead.
+ *
+ * @since 2.5.0
+ *
+ * @param mixed $output Form output to return instead. Default empty.
+ * @param string $form_post URL to POST the form to.
+ * @param string $type Chosen type of filesystem.
+ * @param bool $error Whether the current request has failed to connect.
+ * Default false.
+ * @param string $context Full path to the directory that is tested for
+ * being writable.
+ * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable.
+ * @param array $extra_fields Extra POST fields.
+ */
+ $req_cred = apply_filters( 'request_filesystem_credentials', '', $form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership );
+ if ( '' !== $req_cred )
+ return $req_cred;
+
+ if ( empty($type) ) {
+ $type = get_filesystem_method( array(), $context, $allow_relaxed_file_ownership );
+ }
+
+ if ( 'direct' == $type )
+ return true;
+
+ if ( is_null( $extra_fields ) )
+ $extra_fields = array( 'version', 'locale' );
+
+ $credentials = get_option('ftp_credentials', array( 'hostname' => '', 'username' => ''));
+
+ // If defined, set it to that, Else, If POST'd, set it to that, If not, Set it to whatever it previously was(saved details in option)
+ $credentials['hostname'] = defined('FTP_HOST') ? FTP_HOST : (!empty($_POST['hostname']) ? wp_unslash( $_POST['hostname'] ) : $credentials['hostname']);
+ $credentials['username'] = defined('FTP_USER') ? FTP_USER : (!empty($_POST['username']) ? wp_unslash( $_POST['username'] ) : $credentials['username']);
+ $credentials['password'] = defined('FTP_PASS') ? FTP_PASS : (!empty($_POST['password']) ? wp_unslash( $_POST['password'] ) : '');
+
+ // Check to see if we are setting the public/private keys for ssh
+ $credentials['public_key'] = defined('FTP_PUBKEY') ? FTP_PUBKEY : (!empty($_POST['public_key']) ? wp_unslash( $_POST['public_key'] ) : '');
+ $credentials['private_key'] = defined('FTP_PRIKEY') ? FTP_PRIKEY : (!empty($_POST['private_key']) ? wp_unslash( $_POST['private_key'] ) : '');