Make WordPress Core

Ticket #21913: readymadeweb-filetype.patch

File readymadeweb-filetype.patch, 3.5 KB (added by ReadyMadeWeb, 12 years ago)

Patch to WordPress Importer

Line 
1diff -r rmw-wordpress-importer-0.6.2/languages/wordpress-importer.pot wordpress-importer/languages/wordpress-importer.pot
25c5
3< "Project-Id-Version: WordPress Importer 0.6\n"
4---
5> "Project-Id-Version: WordPress Importer 0.5\n"
6diff -r rmw-wordpress-importer-0.6.2/wordpress-importer.php wordpress-importer/wordpress-importer.php
7841,845c841
8<               // Note that we don't do this in the case that the original upload didn't
9<               // have a valid extension, but we were able to determine one by looking at
10<               // the relevant HTTP headers (as denoted by the
11<               // 'force_extension_in_replacement' flag)
12<               if ( preg_match( '!^image/!', $info['type'] ) && empty($upload['force_extension_in_replacement'])) {
13---
14>               if ( preg_match( '!^image/!', $info['type'] ) ) {
15866d861
16<
17870,889d864
18<               // Some systems, like moveable-type / typepad, advertise files w/o
19<               // extension information attached.      We can get around this limitation
20<               // by grabbing information about the file from the webserver
21<               $file_type = wp_check_filetype($file_name);
22<
23<               // If there is a situation where we're adding a file extension
24<               // onto an upload that didn't originally have one, we
25<               // can tell the remapper to not strip off the extension/
26<               $force_extension_in_replacement = FALSE;
27<
28<               if ( empty( $file_type['ext'] ) ) {
29<
30<                       $retured_file_info = $this->fetch_attachment_info_from_server( $url );
31<
32<                       if ( $retured_file_info ) {
33<                               $file_name = $retured_file_info['filename'];
34<                               $force_extension_in_replacement = TRUE;
35<                       }
36<               }
37<
38935,936d909
39<               if ($force_extension_in_replacement)
40<                       $upload['force_extension_in_replacement'] = TRUE;
411111,1165d1083
42<
43<       /**
44<        * Attempts to grab information about a file from a URL.  This
45<        * call just requests the header information from the server (the
46<        * body of the resource isn't fetched) so the call is relativly lite.
47<        *
48<        * @param string $url
49<        *   A valid url to request mime info from
50<        *
51<        * @return array|bool
52<        *   If the server didn't return a valid header, FALSE is returned.  Otherwise,
53<        *   an array with the following keys is returned:
54<        *     'ext' (string):  The extension of the file, if avaiable
55<        *     'type' (string): The advertised mime type of the resource, if available
56<        *     'filename' (string):
57<        *                      The suggested name of the file, if available
58<        */
59<       function fetch_attachment_info_from_server($url) {
60<
61<               $results = array(
62<                       'ext' => '',
63<                       'type' => '',
64<                       'filename' => '',
65<               );
66<
67<               $curl = curl_init();
68<               curl_setopt( $curl, CURLOPT_URL, $url );
69<               curl_setopt( $curl, CURLOPT_HEADER, TRUE );
70<               curl_setopt( $curl, CURLOPT_NOBODY, TRUE );
71<               curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE );
72<               $header_info = curl_exec( $curl );
73<               curl_close( $curl );
74<
75<               if ( ! $header_info) {
76<                       return FALSE;
77<               }
78<
79<               $file_type_pattern = '/Content-Type:\s?([^\s]+)/i';
80<               $file_type_matches = array();
81<               if ( preg_match( $file_type_pattern, $header_info, $file_type_matches ) ) {
82<                       $results['type'] = trim( $file_type_matches[1] );
83<               }
84<
85<               $file_name_pattern = '/filename=([^\s]+)/i';
86<               $file_name_matches = array();
87<               if ( preg_match( $file_name_pattern, $header_info, $file_name_matches ) ) {
88<                       $results['filename'] = trim( $file_name_matches[1] );
89<
90<                       if ( ( $index = strripos( $results['filename'], '.' ) ) !== FALSE ) {
91<                               $results['ext'] = substr( $results['filename'], $index + 1 );
92<                       }
93<               }
94<
95<               return $results;
96<       }