| 926 | | if ( $wp_file_owner !== false && $wp_file_owner === $temp_file_owner ) { |
| 927 | | // WordPress is creating files as the same owner as the WordPress files, |
| 928 | | // this means it's safe to modify & create new files via PHP. |
| 929 | | $method = 'direct'; |
| 930 | | $GLOBALS['_wp_filesystem_direct_method'] = 'file_owner'; |
| 931 | | } elseif ( $allow_relaxed_file_ownership ) { |
| 932 | | // The $context directory is writable, and $allow_relaxed_file_ownership is set, this means we can modify files |
| 933 | | // safely in this directory. This mode doesn't create new files, only alter existing ones. |
| 934 | | $method = 'direct'; |
| 935 | | $GLOBALS['_wp_filesystem_direct_method'] = 'relaxed_ownership'; |
| | 921 | $temp_file_name = $context . 'temp-write-test-' . time(); |
| | 922 | $temp_handle = @fopen( $temp_file_name, 'w' ); |
| | 923 | if ( $temp_handle ) { |
| | 924 | |
| | 925 | // Attempt to determine the file owner of the WordPress files, and that of newly created files |
| | 926 | $wp_file_owner = $temp_file_owner = false; |
| | 927 | if ( function_exists( 'fileowner' ) ) { |
| | 928 | $wp_file_owner = @fileowner( __FILE__ ); |
| | 929 | $temp_file_owner = @fileowner( $temp_file_name ); |
| | 930 | } |
| | 931 | |
| | 932 | if ( $wp_file_owner !== false && $wp_file_owner === $temp_file_owner ) { |
| | 933 | // WordPress is creating files as the same owner as the WordPress files, |
| | 934 | // this means it's safe to modify & create new files via PHP. |
| | 935 | $method = 'direct'; |
| | 936 | $GLOBALS['_wp_filesystem_direct_method'] = 'file_owner'; |
| | 937 | } elseif ( $allow_relaxed_file_ownership ) { |
| | 938 | // The $context directory is writable, and $allow_relaxed_file_ownership is set, this means we can modify files |
| | 939 | // safely in this directory. This mode doesn't create new files, only alter existing ones. |
| | 940 | $method = 'direct'; |
| | 941 | $GLOBALS['_wp_filesystem_direct_method'] = 'relaxed_ownership'; |
| | 942 | } |
| | 943 | |
| | 944 | @fclose( $temp_handle ); |
| | 945 | @unlink( $temp_file_name ); |
| 938 | | @fclose($temp_handle); |
| 939 | | @unlink($temp_file_name); |
| | 949 | if ( ! $method && isset($args['connection_type']) && 'ssh' == $args['connection_type'] && extension_loaded('ssh2') && function_exists('stream_get_contents') ) $method = 'ssh2'; |
| | 950 | if ( ! $method && extension_loaded('ftp') ) $method = 'ftpext'; |
| | 951 | if ( ! $method && ( extension_loaded('sockets') || function_exists('fsockopen') ) ) $method = 'ftpsockets'; //Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread |
| | 952 | |
| | 953 | /** |
| | 954 | * Filter the filesystem method to use. |
| | 955 | * |
| | 956 | * @since 2.6.0 |
| | 957 | * |
| | 958 | * @param string $method Filesystem method to return. |
| | 959 | * @param array $args An array of connection details for the method. |
| | 960 | * @param string $context Full path to the directory that is tested for being writable. |
| | 961 | * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable. |
| | 962 | */ |
| | 963 | $results[$hash] = apply_filters( 'filesystem_method', $method, $args, $context, $allow_relaxed_file_ownership ); |
| | 964 | } |
| | 965 | |
| | 966 | return $results[$hash]; |
| | 967 | } |
| | 968 | |
| | 969 | /** |
| | 970 | * Get the filesystem credentials needed to connect to the site |
| | 971 | * |
| | 972 | * @param string $form_post the URL to post the form to |
| | 973 | * @param string $type the chosen Filesystem method in use |
| | 974 | * @param boolean $error if the current request has failed to connect |
| | 975 | * @param string $context The directory which is needed access to, |
| | 976 | * The write-test will be performed on this |
| | 977 | * directory by get_filesystem_method() |
| | 978 | * @param array $extra_fields Extra POST fields which should be checked for to be included in the post. |
| | 979 | * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable. |
| | 980 | * @return mixed Credentials that are found, not guaranteed to contain all information |
| | 981 | */ |
| | 982 | function get_filesystem_credentials($form_post, $type = '', $error = false, $context = false, $extra_fields = null, $allow_relaxed_file_ownership = false) { |
| | 983 | |
| | 984 | /** This filter is documented in wp-includes/file.php */ |
| | 985 | $credentials = apply_filters( 'request_filesystem_credentials', '', $form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership ); |
| | 986 | if ( '' !== $credentials ) { |
| | 987 | return $credentials; |
| | 988 | } |
| | 989 | |
| | 990 | if ( empty($type) ) { |
| | 991 | $type = get_filesystem_method( array(), $context, $allow_relaxed_file_ownership ); |
| | 992 | } |
| | 993 | |
| | 994 | if ( 'direct' == $type ) { |
| | 995 | return true; |
| | 996 | } |
| | 997 | |
| | 998 | $credentials = get_option('ftp_credentials', array( 'hostname' => '', 'username' => '')); |
| | 999 | |
| | 1000 | // 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) |
| | 1001 | $credentials['hostname'] = defined('FTP_HOST') ? FTP_HOST : (!empty($_POST['hostname']) ? wp_unslash( $_POST['hostname'] ) : $credentials['hostname']); |
| | 1002 | $credentials['username'] = defined('FTP_USER') ? FTP_USER : (!empty($_POST['username']) ? wp_unslash( $_POST['username'] ) : $credentials['username']); |
| | 1003 | $credentials['password'] = defined('FTP_PASS') ? FTP_PASS : (!empty($_POST['password']) ? wp_unslash( $_POST['password'] ) : ''); |
| | 1004 | |
| | 1005 | // Check to see if we are setting the public/private keys for ssh |
| | 1006 | $credentials['public_key'] = defined('FTP_PUBKEY') ? FTP_PUBKEY : (!empty($_POST['public_key']) ? wp_unslash( $_POST['public_key'] ) : ''); |
| | 1007 | $credentials['private_key'] = defined('FTP_PRIKEY') ? FTP_PRIKEY : (!empty($_POST['private_key']) ? wp_unslash( $_POST['private_key'] ) : ''); |
| | 1008 | |
| | 1009 | // Sanitize the hostname, Some people might pass in odd-data: |
| | 1010 | $credentials['hostname'] = preg_replace('|\w+://|', '', $credentials['hostname']); //Strip any schemes off |
| | 1011 | |
| | 1012 | if ( strpos($credentials['hostname'], ':') ) { |
| | 1013 | list( $credentials['hostname'], $credentials['port'] ) = explode(':', $credentials['hostname'], 2); |
| | 1014 | if ( ! is_numeric($credentials['port']) ) |
| | 1015 | unset($credentials['port']); |
| | 1016 | } else { |
| | 1017 | unset($credentials['port']); |
| | 1018 | } |
| | 1019 | |
| | 1020 | if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' == FS_METHOD ) ) { |
| | 1021 | $credentials['connection_type'] = 'ssh'; |
| | 1022 | } elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' == $type ) { //Only the FTP Extension understands SSL |
| | 1023 | $credentials['connection_type'] = 'ftps'; |
| | 1024 | } elseif ( ! empty( $_POST['connection_type'] ) ) { |
| | 1025 | $credentials['connection_type'] = wp_unslash( $_POST['connection_type'] ); |
| | 1026 | } elseif ( ! isset( $credentials['connection_type'] ) ) { //All else fails (And it's not defaulted to something else saved), Default to FTP |
| | 1027 | $credentials['connection_type'] = 'ftp'; |
| | 1028 | } |
| | 1029 | |
| | 1030 | return $credentials; |
| | 1031 | } |
| | 1032 | |
| | 1033 | /** |
| | 1034 | * @param array $credentials Credentials to check required parameters |
| | 1035 | * @param string $type Type of connection to check credentials |
| | 1036 | * against (direct, ftps, ftp, ssh, etc.) |
| | 1037 | * @param string $context The directory which is needed access to, |
| | 1038 | * the write-test will be performed on this |
| | 1039 | * directory by get_filesystem_method() |
| | 1040 | * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable. |
| | 1041 | * |
| | 1042 | * @return bool True when all required parameters have been set for supplied type, False when anything is missing |
| | 1043 | */ |
| | 1044 | function usable_filesystem_credentials($credentials, $type = '', $context = null, $allow_relaxed_file_ownership = false) { |
| | 1045 | if ( empty($type) ) { |
| | 1046 | $type = get_filesystem_method( array(), $context, $allow_relaxed_file_ownership ); |
| | 1047 | } |
| | 1048 | |
| | 1049 | // Direct connection does not need any credentials |
| | 1050 | if ( 'direct' == $type ) { |
| | 1051 | return true; |
| | 1052 | } |
| | 1053 | |
| | 1054 | // We need something to check against |
| | 1055 | if ( empty( $credentials ) ) { |
| | 1056 | return false; |
| | 1057 | } |
| | 1058 | |
| | 1059 | // All the checks required an array for parameters; exit if we got something else |
| | 1060 | if ( ! is_array( $credentials ) ) { |
| | 1061 | return false; |
| | 1062 | } |
| | 1063 | |
| | 1064 | // SSH needs public and private key |
| | 1065 | if ( isset($credentials['connection_type']) && 'ssh' == $credentials['connection_type'] ) { |
| | 1066 | if ( ! isset( $credentials['public_key'], $credentials['private_key'] ) ) { |
| | 1067 | return false; |
| 1020 | | // 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) |
| 1021 | | $credentials['hostname'] = defined('FTP_HOST') ? FTP_HOST : (!empty($_POST['hostname']) ? wp_unslash( $_POST['hostname'] ) : $credentials['hostname']); |
| 1022 | | $credentials['username'] = defined('FTP_USER') ? FTP_USER : (!empty($_POST['username']) ? wp_unslash( $_POST['username'] ) : $credentials['username']); |
| 1023 | | $credentials['password'] = defined('FTP_PASS') ? FTP_PASS : (!empty($_POST['password']) ? wp_unslash( $_POST['password'] ) : ''); |
| 1024 | | |
| 1025 | | // Check to see if we are setting the public/private keys for ssh |
| 1026 | | $credentials['public_key'] = defined('FTP_PUBKEY') ? FTP_PUBKEY : (!empty($_POST['public_key']) ? wp_unslash( $_POST['public_key'] ) : ''); |
| 1027 | | $credentials['private_key'] = defined('FTP_PRIKEY') ? FTP_PRIKEY : (!empty($_POST['private_key']) ? wp_unslash( $_POST['private_key'] ) : ''); |
| 1028 | | |
| 1029 | | // Sanitize the hostname, Some people might pass in odd-data: |
| 1030 | | $credentials['hostname'] = preg_replace('|\w+://|', '', $credentials['hostname']); //Strip any schemes off |
| 1031 | | |
| 1032 | | if ( strpos($credentials['hostname'], ':') ) { |
| 1033 | | list( $credentials['hostname'], $credentials['port'] ) = explode(':', $credentials['hostname'], 2); |
| 1034 | | if ( ! is_numeric($credentials['port']) ) |
| 1035 | | unset($credentials['port']); |
| 1036 | | } else { |
| 1037 | | unset($credentials['port']); |
| 1038 | | } |
| 1039 | | |
| 1040 | | if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' == FS_METHOD ) ) { |
| 1041 | | $credentials['connection_type'] = 'ssh'; |
| 1042 | | } elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' == $type ) { //Only the FTP Extension understands SSL |
| 1043 | | $credentials['connection_type'] = 'ftps'; |
| 1044 | | } elseif ( ! empty( $_POST['connection_type'] ) ) { |
| 1045 | | $credentials['connection_type'] = wp_unslash( $_POST['connection_type'] ); |
| 1046 | | } elseif ( ! isset( $credentials['connection_type'] ) ) { //All else fails (And it's not defaulted to something else saved), Default to FTP |
| 1047 | | $credentials['connection_type'] = 'ftp'; |
| 1048 | | } |
| 1049 | | if ( ! $error && |
| 1050 | | ( |
| 1051 | | ( !empty($credentials['password']) && !empty($credentials['username']) && !empty($credentials['hostname']) ) || |
| 1052 | | ( 'ssh' == $credentials['connection_type'] && !empty($credentials['public_key']) && !empty($credentials['private_key']) ) |
| 1053 | | ) ) { |
| | 1155 | |
| | 1156 | // Display the credentials form to the user |
| | 1157 | request_filesystem_credentials_form( $form_post, $credentials, $type, $error, $context, $extra_fields ); |
| | 1158 | |
| | 1159 | // Nothing usable has been found yet. |
| | 1160 | return false; |
| | 1161 | } |
| | 1162 | |
| | 1163 | /** |
| | 1164 | * Write the form needed to input credentials needed to connect for the specified type |
| | 1165 | * |
| | 1166 | * @param string $form_post the URL to post the form to |
| | 1167 | * @param $credentials |
| | 1168 | * @param string $type the chosen Filesystem method in use |
| | 1169 | * @param boolean $error if the current request has failed to connect |
| | 1170 | * @param string $context The directory which is needed access to, The write-test will be performed on this directory by get_filesystem_method() |
| | 1171 | * @param array $extra_fields Extra POST fields which should be checked for to be included in the post. |
| | 1172 | * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable. |
| | 1173 | */ |
| | 1174 | function request_filesystem_credentials_form($form_post, $credentials, $type = '', $error = false, $context = null, $extra_fields = null, $allow_relaxed_file_ownership = false) { |
| | 1175 | if ( empty( $type ) ) { |
| | 1176 | $type = get_filesystem_method( array(), $context, $allow_relaxed_file_ownership ); |
| | 1177 | } |
| | 1178 | |
| | 1179 | if ( ! is_array($credentials)) { |
| | 1180 | $credentials = array(); |
| | 1181 | } |
| | 1182 | |