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'; |
| 920 | if ( ! $method ) { |
| 921 | |
| 922 | $temp_file_name = $context . 'temp-write-test-' . time(); |
| 923 | $temp_handle = @fopen( $temp_file_name, 'w' ); |
| 924 | if ( $temp_handle ) { |
| 925 | |
| 926 | // Attempt to determine the file owner of the WordPress files, and that of newly created files |
| 927 | $wp_file_owner = $temp_file_owner = false; |
| 928 | if ( function_exists( 'fileowner' ) ) { |
| 929 | $wp_file_owner = @fileowner( __FILE__ ); |
| 930 | $temp_file_owner = @fileowner( $temp_file_name ); |
| 931 | } |
| 932 | |
| 933 | if ( $wp_file_owner !== false && $wp_file_owner === $temp_file_owner ) { |
| 934 | // WordPress is creating files as the same owner as the WordPress files, |
| 935 | // this means it's safe to modify & create new files via PHP. |
| 936 | $method = 'direct'; |
| 937 | $GLOBALS['_wp_filesystem_direct_method'] = 'file_owner'; |
| 938 | } elseif ( $allow_relaxed_file_ownership ) { |
| 939 | // The $context directory is writable, and $allow_relaxed_file_ownership is set, this means we can modify files |
| 940 | // safely in this directory. This mode doesn't create new files, only alter existing ones. |
| 941 | $method = 'direct'; |
| 942 | $GLOBALS['_wp_filesystem_direct_method'] = 'relaxed_ownership'; |
| 943 | } |
| 944 | |
| 945 | @fclose( $temp_handle ); |
| 946 | @unlink( $temp_file_name ); |
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 | | ) ) { |
| 1053 | |
| 1054 | return $credentials; |
| 1055 | } |
| 1056 | |
| 1057 | /** |
| 1058 | * @param array $credentials Credentials to check required parameters |
| 1059 | * @param string $type Type of connection to check credentials against (direct, ftps, ftp, ssh, etc.) |
| 1060 | * @param string $context The directory which is needed access to, The write-test will be performed on this directory by get_filesystem_method() |
| 1061 | * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable. |
| 1062 | * |
| 1063 | * @return bool True when all required parameters have been set for supplied type, False when anything is missing |
| 1064 | */ |
| 1065 | function usable_filesystem_credentials($credentials, $type = '', $context = null, $allow_relaxed_file_ownership = false) { |
| 1066 | if ( empty($type) ) { |
| 1067 | $type = get_filesystem_method( array(), $context, $allow_relaxed_file_ownership ); |
| 1068 | } |
| 1069 | |
| 1070 | // Direct connection does not need any credentials |
| 1071 | if ( 'direct' == $type ) { |
| 1072 | return true; |
| 1073 | } |
| 1074 | |
| 1075 | // We need something to check against |
| 1076 | if ( empty( $credentials ) ) { |
| 1077 | return false; |
| 1078 | } |
| 1079 | |
| 1080 | // All the checks required an array for parameters; exit if we got something else |
| 1081 | if ( ! is_array( $credentials ) ) { |
| 1082 | return false; |
| 1083 | } |
| 1084 | |
| 1085 | // SSH needs public and private key |
| 1086 | if ( 'ssh' == $credentials['connection_type'] ) { |
| 1087 | return ( ! empty( $credentials['public_key'] ) && ! empty( $credentials['private_key'] ) ); |
| 1088 | } |
| 1089 | |
| 1090 | // Other connection methods need hostname, password and username |
| 1091 | return ( ! empty( $credentials['password'] ) && ! empty( $credentials['username'] ) && ! empty( $credentials['hostname'] ) ); |
| 1092 | } |
| 1093 | |
| 1094 | /** |
| 1095 | * Displays a form to the user to request for their FTP/SSH details in order |
| 1096 | * to connect to the filesystem. |
| 1097 | * |
| 1098 | * All chosen/entered details are saved, Excluding the Password. |
| 1099 | * |
| 1100 | * Hostnames may be in the form of hostname:portnumber (eg: wordpress.org:2467) |
| 1101 | * to specify an alternate FTP/SSH port. |
| 1102 | * |
| 1103 | * Plugins may override this form by returning true|false via the |
| 1104 | * {@see 'request_filesystem_credentials'} filter. |
| 1105 | * |
| 1106 | * @since 2.5. |
| 1107 | * |
| 1108 | * @todo Properly mark optional arguments as such |
| 1109 | * |
| 1110 | * @param string $form_post the URL to post the form to |
| 1111 | * @param string $type the chosen Filesystem method in use |
| 1112 | * @param boolean $error if the current request has failed to connect |
| 1113 | * @param string $context The directory which is needed access to, The write-test will be performed on this directory by get_filesystem_method() |
| 1114 | * @param array $extra_fields Extra POST fields which should be checked for to be included in the post. |
| 1115 | * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable. |
| 1116 | * @return boolean False on failure. True on success. |
| 1117 | */ |
| 1118 | function request_filesystem_credentials($form_post, $type = '', $error = false, $context = false, $extra_fields = null, $allow_relaxed_file_ownership = false ) { |
| 1119 | /** |
| 1120 | * Filter the filesystem credentials form output. |
| 1121 | * |
| 1122 | * Returning anything other than an empty string will effectively short-circuit |
| 1123 | * output of the filesystem credentials form, returning that value instead. |
| 1124 | * |
| 1125 | * @since 2.5.0 |
| 1126 | * |
| 1127 | * @param mixed $output Form output to return instead. Default empty. |
| 1128 | * @param string $form_post URL to POST the form to. |
| 1129 | * @param string $type Chosen type of filesystem. |
| 1130 | * @param bool $error Whether the current request has failed to connect. |
| 1131 | * Default false. |
| 1132 | * @param string $context Full path to the directory that is tested for |
| 1133 | * being writable. |
| 1134 | * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable. |
| 1135 | * @param array $extra_fields Extra POST fields. |
| 1136 | */ |
| 1137 | $credentials = apply_filters( 'request_filesystem_credentials', '', $form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership ); |
| 1138 | if ( ! empty( $credentials ) ) { |
| 1139 | return $credentials; |
| 1140 | } |
| 1141 | |
| 1142 | if ( empty( $type ) ) { |
| 1143 | $type = get_filesystem_method( array(), $context, $allow_relaxed_file_ownership ); |
| 1144 | } |
| 1145 | |
| 1146 | if ( 'direct' == $type ) { |
| 1147 | return true; |
| 1148 | } |
| 1149 | |
| 1150 | $credentials = get_filesystem_credentials( $form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership ); |
| 1151 | |
| 1152 | if ( ! $error && usable_filesystem_credentials( $credentials, $type ) ) { |
| 1153 | |
| 1168 | |
| 1169 | // Display the credentials form to the user |
| 1170 | request_filesystem_credentials_form( $form_post, $credentials, $type, $error, $context, $extra_fields ); |
| 1171 | |
| 1172 | // Nothing usable has been found yet. |
| 1173 | return false; |
| 1174 | } |
| 1175 | |
| 1176 | /** |
| 1177 | * Write the form needed to input credentials needed to connect for the specified type |
| 1178 | * |
| 1179 | * @param string $form_post the URL to post the form to |
| 1180 | * @param $credentials |
| 1181 | * @param string $type the chosen Filesystem method in use |
| 1182 | * @param boolean $error if the current request has failed to connect |
| 1183 | * @param string $context The directory which is needed access to, The write-test will be performed on this directory by get_filesystem_method() |
| 1184 | * @param array $extra_fields Extra POST fields which should be checked for to be included in the post. |
| 1185 | * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable. |
| 1186 | */ |
| 1187 | function request_filesystem_credentials_form($form_post, $credentials, $type = '', $error = false, $context = null, $extra_fields = null, $allow_relaxed_file_ownership = false) { |
| 1188 | if ( empty( $type ) ) { |
| 1189 | $type = get_filesystem_method( array(), $context, $allow_relaxed_file_ownership ); |
| 1190 | } |
| 1191 | |
| 1192 | if ( ! is_array($credentials)) { |
| 1193 | $credentials = array(); |
| 1194 | } |
| 1195 | |
1100 | | ?> |
1101 | | <script type="text/javascript"> |
1102 | | <!-- |
1103 | | jQuery(function($){ |
1104 | | jQuery("#ssh").click(function () { |
1105 | | jQuery("#ssh_keys").show(); |
1106 | | }); |
1107 | | jQuery("#ftp, #ftps").click(function () { |
1108 | | jQuery("#ssh_keys").hide(); |
1109 | | }); |
1110 | | jQuery('form input[value=""]:first').focus(); |
1111 | | }); |
1112 | | --> |
1113 | | </script> |
1114 | | <form action="<?php echo esc_url( $form_post ) ?>" method="post"> |
1115 | | <div> |
1116 | | <h3><?php _e('Connection Information') ?></h3> |
1117 | | <p><?php |
1118 | | $label_user = __('Username'); |
1119 | | $label_pass = __('Password'); |
1120 | | _e('To perform the requested action, WordPress needs to access your web server.'); |
1121 | | echo ' '; |
1122 | | if ( ( isset( $types['ftp'] ) || isset( $types['ftps'] ) ) ) { |
1123 | | if ( isset( $types['ssh'] ) ) { |
1124 | | _e('Please enter your FTP or SSH credentials to proceed.'); |
1125 | | $label_user = __('FTP/SSH Username'); |
1126 | | $label_pass = __('FTP/SSH Password'); |
1127 | | } else { |
1128 | | _e('Please enter your FTP credentials to proceed.'); |
1129 | | $label_user = __('FTP Username'); |
1130 | | $label_pass = __('FTP Password'); |
1131 | | } |
1132 | | echo ' '; |
1133 | | } |
1134 | | _e('If you do not remember your credentials, you should contact your web host.'); |
1135 | | ?></p> |
1136 | | <table class="form-table"> |
1137 | | <tr> |
1138 | | <th scope="row"><label for="hostname"><?php _e('Hostname') ?></label></th> |
1139 | | <td><input name="hostname" type="text" id="hostname" value="<?php echo esc_attr($hostname); if ( !empty($port) ) echo ":$port"; ?>"<?php disabled( defined('FTP_HOST') ); ?> size="40" /></td> |
1140 | | </tr> |
| 1235 | ?> |
| 1236 | <script type="text/javascript"> |
| 1237 | <!-- |
| 1238 | jQuery(function($){ |
| 1239 | jQuery("#ssh").click(function () { |
| 1240 | jQuery("#ssh_keys").show(); |
| 1241 | }); |
| 1242 | jQuery("#ftp, #ftps").click(function () { |
| 1243 | jQuery("#ssh_keys").hide(); |
| 1244 | }); |
| 1245 | jQuery('form input[value=""]:first').focus(); |
| 1246 | }); |
| 1247 | --> |
| 1248 | </script> |
| 1249 | <form action="<?php echo esc_url( $form_post ) ?>" method="post"> |
| 1250 | <div> |
| 1251 | <h3><?php _e('Connection Information') ?></h3> |
| 1252 | <p><?php |
| 1253 | $label_user = __('Username'); |
| 1254 | $label_pass = __('Password'); |
| 1255 | _e('To perform the requested action, WordPress needs to access your web server.'); |
| 1256 | echo ' '; |
| 1257 | if ( ( isset( $types['ftp'] ) || isset( $types['ftps'] ) ) ) { |
| 1258 | if ( isset( $types['ssh'] ) ) { |
| 1259 | _e('Please enter your FTP or SSH credentials to proceed.'); |
| 1260 | $label_user = __('FTP/SSH Username'); |
| 1261 | $label_pass = __('FTP/SSH Password'); |
| 1262 | } else { |
| 1263 | _e('Please enter your FTP credentials to proceed.'); |
| 1264 | $label_user = __('FTP Username'); |
| 1265 | $label_pass = __('FTP Password'); |
| 1266 | } |
| 1267 | echo ' '; |
| 1268 | } |
| 1269 | _e('If you do not remember your credentials, you should contact your web host.'); |
| 1270 | ?></p> |
| 1271 | <table class="form-table"> |
| 1272 | <tr> |
| 1273 | <th scope="row"><label for="hostname"><?php _e('Hostname') ?></label></th> |
| 1274 | <td><input name="hostname" type="text" id="hostname" value="<?php echo esc_attr($hostname); if ( !empty($port) ) echo ":$port"; ?>"<?php disabled( defined('FTP_HOST') ); ?> size="40" /></td> |
| 1275 | </tr> |
1153 | | <?php if ( isset($types['ssh']) ) : ?> |
1154 | | <tr id="ssh_keys" style="<?php if ( 'ssh' != $connection_type ) echo 'display:none' ?>"> |
1155 | | <th scope="row"><?php _e('Authentication Keys') ?> |
1156 | | <div class="key-labels textright"> |
1157 | | <label for="public_key"><?php _e('Public Key:') ?></label ><br /> |
1158 | | <label for="private_key"><?php _e('Private Key:') ?></label> |
1159 | | </div></th> |
1160 | | <td><br /><input name="public_key" type="text" id="public_key" value="<?php echo esc_attr($public_key) ?>"<?php disabled( defined('FTP_PUBKEY') ); ?> size="40" /> |
1161 | | <br /><input name="private_key" type="text" id="private_key" value="<?php echo esc_attr($private_key) ?>"<?php disabled( defined('FTP_PRIKEY') ); ?> size="40" /> |
1162 | | <div><?php _e('Enter the location on the server where the keys are located. If a passphrase is needed, enter that in the password field above.') ?></div></td> |
1163 | | </tr> |
1164 | | <?php endif; ?> |
| 1288 | <?php if ( isset($types['ssh']) ) : ?> |
| 1289 | <tr id="ssh_keys" style="<?php if ( 'ssh' != $connection_type ) echo 'display:none' ?>"> |
| 1290 | <th scope="row"><?php _e('Authentication Keys') ?> |
| 1291 | <div class="key-labels textright"> |
| 1292 | <label for="public_key"><?php _e('Public Key:') ?></label ><br /> |
| 1293 | <label for="private_key"><?php _e('Private Key:') ?></label> |
| 1294 | </div></th> |
| 1295 | <td><br /><input name="public_key" type="text" id="public_key" value="<?php echo esc_attr($public_key) ?>"<?php disabled( defined('FTP_PUBKEY') ); ?> size="40" /> |
| 1296 | <br /><input name="private_key" type="text" id="private_key" value="<?php echo esc_attr($private_key) ?>"<?php disabled( defined('FTP_PRIKEY') ); ?> size="40" /> |
| 1297 | <div><?php _e('Enter the location on the server where the keys are located. If a passphrase is needed, enter that in the password field above.') ?></div></td> |
| 1298 | </tr> |
| 1299 | <?php endif; ?> |
1166 | | <tr> |
1167 | | <th scope="row"><?php _e('Connection Type') ?></th> |
1168 | | <td> |
1169 | | <fieldset><legend class="screen-reader-text"><span><?php _e('Connection Type') ?></span></legend> |
1170 | | <?php |
1171 | | $disabled = disabled( (defined('FTP_SSL') && FTP_SSL) || (defined('FTP_SSH') && FTP_SSH), true, false ); |
1172 | | foreach ( $types as $name => $text ) : ?> |
1173 | | <label for="<?php echo esc_attr($name) ?>"> |
1174 | | <input type="radio" name="connection_type" id="<?php echo esc_attr($name) ?>" value="<?php echo esc_attr($name) ?>"<?php checked($name, $connection_type); echo $disabled; ?> /> |
1175 | | <?php echo $text ?> |
1176 | | </label> |
1177 | | <?php endforeach; ?> |
1178 | | </fieldset> |
1179 | | </td> |
1180 | | </tr> |
1181 | | </table> |
| 1301 | <tr> |
| 1302 | <th scope="row"><?php _e('Connection Type') ?></th> |
| 1303 | <td> |
| 1304 | <fieldset><legend class="screen-reader-text"><span><?php _e('Connection Type') ?></span></legend> |
| 1305 | <?php |
| 1306 | $disabled = disabled( (defined('FTP_SSL') && FTP_SSL) || (defined('FTP_SSH') && FTP_SSH), true, false ); |
| 1307 | foreach ( $types as $name => $text ) : ?> |
| 1308 | <label for="<?php echo esc_attr($name) ?>"> |
| 1309 | <input type="radio" name="connection_type" id="<?php echo esc_attr($name) ?>" value="<?php echo esc_attr($name) ?>"<?php checked($name, $connection_type); echo $disabled; ?> /> |
| 1310 | <?php echo $text ?> |
| 1311 | </label> |
| 1312 | <?php endforeach; ?> |
| 1313 | </fieldset> |
| 1314 | </td> |
| 1315 | </tr> |
| 1316 | </table> |