| 3208 | * Detect MP4 Content Type |
| 3209 | * |
| 3210 | * The MP4 file extension is intended for video content, but might be |
| 3211 | * mistakenly applied to audio files as well. This will attempt to |
| 3212 | * determine an MP4 file's content and rename if needed. |
| 3213 | * |
| 3214 | * @since 4.9 |
| 3215 | * |
| 3216 | * @see https://core.trac.wordpress.org/ticket/40921 |
| 3217 | * |
| 3218 | * @param array $wp_check_filetype_and_ext File data array. |
| 3219 | * @param string $file Full path to file. |
| 3220 | * @param string $filename The name of the file. |
| 3221 | * @param array $mimes Allowed MIME types. |
| 3222 | * @return array File data. |
| 3223 | */ |
| 3224 | function wp_check_mp4_filetype_and_ext( $checked, $file, $filename, $mimes = null ) { |
| 3225 | if ( 'mp4' === $checked['ext'] ) { |
| 3226 | // Load ID3 parser. |
| 3227 | if ( ! defined( 'GETID3_TEMP_DIR' ) ) { |
| 3228 | define( 'GETID3_TEMP_DIR', get_temp_dir() ); |
| 3229 | } |
| 3230 | |
| 3231 | if ( ! class_exists( 'getID3', false ) ) { |
| 3232 | require( ABSPATH . WPINC . '/ID3/getid3.php' ); |
| 3233 | } |
| 3234 | |
| 3235 | $id3 = new getID3(); |
| 3236 | $data = $id3->analyze( $file ); |
| 3237 | |
| 3238 | // This is audio if the MIME says so. |
| 3239 | if ( |
| 3240 | isset( $data['mime_type'] ) && |
| 3241 | ( 0 === strpos( $data['mime_type'], 'audio/' ) ) |
| 3242 | ) { |
| 3243 | $checked['ext'] = 'm4a'; |
| 3244 | $checked['type'] = 'audio/mpeg'; |
| 3245 | } |
| 3246 | |
| 3247 | // The ftype might suggest its audioness. |
| 3248 | elseif ( |
| 3249 | isset( $data['quicktime']['ftype']['signature'] ) && |
| 3250 | ( 'M4A' === $data['quicktime']['ftype']['signature'] ) |
| 3251 | ) { |
| 3252 | $checked['ext'] = 'm4a'; |
| 3253 | $checked['type'] = 'audio/mpeg'; |
| 3254 | } |
| 3255 | |
| 3256 | // This might also be audio if there is no video resolution. |
| 3257 | elseif ( |
| 3258 | isset( $data['video']['resolution_x'] ) && |
| 3259 | ! $data['video']['resolution_x'] && |
| 3260 | isset( $data['video']['resolution_y'] ) && |
| 3261 | ! $data['video']['resolution_y'] |
| 3262 | ) { |
| 3263 | $checked['ext'] = 'm4a'; |
| 3264 | $checked['type'] = 'audio/mpeg'; |
| 3265 | } |
| 3266 | |
| 3267 | // Update the filename. |
| 3268 | if ( 'm4a' === $checked['ext'] ) { |
| 3269 | $checked['proper_filename'] = substr( $filename, 0, -3 ) . 'm4a'; |
| 3270 | } |
| 3271 | } |
| 3272 | |
| 3273 | return $checked; |
| 3274 | } |
| 3275 | add_filter( 'wp_check_filetype_and_ext', 'wp_check_mp4_filetype_and_ext', 100, 4 ); |
| 3276 | |
| 3277 | /** |
| 3278 | * Detect OGG Content Type |
| 3279 | * |
| 3280 | * The OGG file extension is intended for audio content, but might be |
| 3281 | * mistakenly applied to video files as well. This will attempt to |
| 3282 | * determine an OGG file's content and rename if needed. |
| 3283 | * |
| 3284 | * @since 4.9 |
| 3285 | * |
| 3286 | * @see https://core.trac.wordpress.org/ticket/40921 |
| 3287 | * |
| 3288 | * @param array $wp_check_filetype_and_ext File data array. |
| 3289 | * @param string $file Full path to file. |
| 3290 | * @param string $filename The name of the file. |
| 3291 | * @param array $mimes Allowed MIME types. |
| 3292 | * @return array File data. |
| 3293 | */ |
| 3294 | function wp_check_ogg_filetype_and_ext( $checked, $file, $filename, $mimes = null ) { |
| 3295 | if ( 'ogg' === $checked['ext'] ) { |
| 3296 | // Load ID3 parser. |
| 3297 | if ( ! defined( 'GETID3_TEMP_DIR' ) ) { |
| 3298 | define( 'GETID3_TEMP_DIR', get_temp_dir() ); |
| 3299 | } |
| 3300 | |
| 3301 | if ( ! class_exists( 'getID3', false ) ) { |
| 3302 | require( ABSPATH . WPINC . '/ID3/getid3.php' ); |
| 3303 | } |
| 3304 | |
| 3305 | $id3 = new getID3(); |
| 3306 | $data = $id3->analyze( $file ); |
| 3307 | |
| 3308 | // This is video if the MIME says so. |
| 3309 | if ( |
| 3310 | isset( $data['mime_type'] ) && |
| 3311 | ( 0 === strpos( $data['mime_type'], 'video/' ) ) |
| 3312 | ) { |
| 3313 | $checked['ext'] = 'ogv'; |
| 3314 | $checked['type'] = 'video/ogg'; |
| 3315 | } |
| 3316 | |
| 3317 | // This is video if there is a resolution. |
| 3318 | elseif ( |
| 3319 | ( isset( $data['video']['resolution_x'] ) && $data['video']['resolution_x'] > 0 ) || |
| 3320 | ( isset( $data['video']['resolution_y'] ) && $data['video']['resolution_y'] > 0 ) |
| 3321 | ) { |
| 3322 | $checked['ext'] = 'ogv'; |
| 3323 | $checked['type'] = 'video/ogg'; |
| 3324 | } |
| 3325 | |
| 3326 | // Update the filename. |
| 3327 | if ( 'ogv' === $checked['ext'] ) { |
| 3328 | $checked['proper_filename'] = substr( $filename, 0, -3 ) . 'ogv'; |
| 3329 | } |
| 3330 | } |
| 3331 | |
| 3332 | return $checked; |
| 3333 | } |
| 3334 | add_filter( 'wp_check_filetype_and_ext', 'wp_check_ogg_filetype_and_ext', 100, 4 ); |
| 3335 | |
| 3336 | /** |