| 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 ); |
| | 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 ( 'ssh' == $credentials['connection_type'] ) { |
| | 1066 | return ( ! empty( $credentials['public_key'] ) && ! empty( $credentials['private_key'] ) ); |
| | 1067 | } |
| | 1068 | |
| | 1069 | // Other connection methods need hostname, password and username |
| | 1070 | return ( ! empty( $credentials['password'] ) && ! empty( $credentials['username'] ) && ! empty( $credentials['hostname'] ) ); |
| | 1071 | } |
| | 1072 | |
| | 1073 | /** |
| 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 | | ) ) { |
| | 1147 | |
| | 1148 | // Display the credentials form to the user |
| | 1149 | request_filesystem_credentials_form( $form_post, $credentials, $type, $error, $context, $extra_fields ); |
| | 1150 | |
| | 1151 | // Nothing usable has been found yet. |
| | 1152 | return false; |
| | 1153 | } |
| | 1154 | |
| | 1155 | /** |
| | 1156 | * Write the form needed to input credentials needed to connect for the specified type |
| | 1157 | * |
| | 1158 | * @param string $form_post the URL to post the form to |
| | 1159 | * @param $credentials |
| | 1160 | * @param string $type the chosen Filesystem method in use |
| | 1161 | * @param boolean $error if the current request has failed to connect |
| | 1162 | * @param string $context The directory which is needed access to, The write-test will be performed on this directory by get_filesystem_method() |
| | 1163 | * @param array $extra_fields Extra POST fields which should be checked for to be included in the post. |
| | 1164 | * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable. |
| | 1165 | */ |
| | 1166 | function request_filesystem_credentials_form($form_post, $credentials, $type = '', $error = false, $context = null, $extra_fields = null, $allow_relaxed_file_ownership = false) { |
| | 1167 | if ( empty( $type ) ) { |
| | 1168 | $type = get_filesystem_method( array(), $context, $allow_relaxed_file_ownership ); |
| | 1169 | } |
| | 1170 | |
| | 1171 | if ( ! is_array($credentials)) { |
| | 1172 | $credentials = array(); |
| | 1173 | } |
| | 1174 | |