| 870 | // Some systems, like moveable-type / typepad, advertise files w/o |
| 871 | // extension information attached. We can get around this limitation |
| 872 | // by grabbing information about the file from the webserver |
| 873 | $file_type = wp_check_filetype($file_name); |
| 874 | |
| 875 | // If there is a situation where we're adding a file extension |
| 876 | // onto an upload that didn't originally have one, we |
| 877 | // can tell the remapper to not strip off the extension/ |
| 878 | $force_extension_in_replacement = false; |
| 879 | |
| 880 | if ( empty( $file_type['ext'] ) ) { |
| 881 | |
| 882 | $retured_file_info = $this->fetch_attachment_info_from_server( $url ); |
| 883 | |
| 884 | if ( $retured_file_info ) { |
| 885 | $file_name = $retured_file_info['filename']; |
| 886 | $force_extension_in_replacement = true; |
| 887 | } |
| 888 | } |
| 889 | |
| 1111 | |
| 1112 | /** |
| 1113 | * Attempts to grab information about a file from a URL. This |
| 1114 | * call just requests the header information from the server (the |
| 1115 | * body of the resource isn't fetched) so the call is relativly lite. |
| 1116 | * |
| 1117 | * @param string $url |
| 1118 | * A valid url to request mime info from |
| 1119 | * |
| 1120 | * @return array|bool |
| 1121 | * If the server didn't return a valid header, false is returned. Otherwise, |
| 1122 | * an array with the following keys is returned: |
| 1123 | * 'ext' (string): The extension of the file, if avaiable |
| 1124 | * 'type' (string): The advertised mime type of the resource, if available |
| 1125 | * 'filename' (string): |
| 1126 | * The suggested name of the file, if available |
| 1127 | */ |
| 1128 | function fetch_attachment_info_from_server($url) { |
| 1129 | |
| 1130 | $results = array( |
| 1131 | 'ext' => '', |
| 1132 | 'type' => '', |
| 1133 | 'filename' => '', |
| 1134 | ); |
| 1135 | |
| 1136 | $header_info = wp_remote_head( $url ); |
| 1137 | |
| 1138 | if ( is_wp_error( $header_info ) ) { |
| 1139 | return false; |
| 1140 | } |
| 1141 | |
| 1142 | $content_type = wp_remote_retrieve_header( $header_info, 'content-type' ); |
| 1143 | if ( $content_type ) { |
| 1144 | $results['type'] = trim( $content_type ); |
| 1145 | } |
| 1146 | |
| 1147 | $display_disposition = wp_remote_retrieve_header( $header_info, 'content-disposition' ); |
| 1148 | $file_name_pattern = '/filename=([^\s]+)/i'; |
| 1149 | $file_name_matches = array(); |
| 1150 | if ( preg_match( $file_name_pattern, $display_disposition, $file_name_matches ) ) { |
| 1151 | $results['filename'] = trim( $file_name_matches[1] ); |
| 1152 | |
| 1153 | if ( ( $index = strripos( $results['filename'], '.' ) ) !== false ) { |
| 1154 | $results['ext'] = substr( $results['filename'], $index + 1 ); |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | return $results; |
| 1159 | } |