| 3427 | /** |
| 3428 | * Detect MP4 Content Type |
| 3429 | * |
| 3430 | * The MP4 file extension is intended for video content, but might be |
| 3431 | * mistakenly applied to audio files as well. This will attempt to |
| 3432 | * determine an MP4 file's content and rename if needed. |
| 3433 | * |
| 3434 | * @since 4.9 |
| 3435 | * |
| 3436 | * @see https://core.trac.wordpress.org/ticket/40921 |
| 3437 | * |
| 3438 | * @param array $wp_check_filetype_and_ext File data array. |
| 3439 | * @param string $file Full path to file. |
| 3440 | * @param string $filename The name of the file. |
| 3441 | * @param array $mimes Allowed MIME types. |
| 3442 | * @return array File data. |
| 3443 | */ |
| 3444 | function wp_check_mp4_filetype_and_ext( $checked, $file, $filename, $mimes = null ) { |
| 3445 | if ( 'mp4' === $checked['ext'] ) { |
| 3446 | // Load ID3 parser. |
| 3447 | if ( ! defined( 'GETID3_TEMP_DIR' ) ) { |
| 3448 | define( 'GETID3_TEMP_DIR', get_temp_dir() ); |
| 3449 | } |
| 3450 | |
| 3451 | if ( ! class_exists( 'getID3', false ) ) { |
| 3452 | require( ABSPATH . WPINC . '/ID3/getid3.php' ); |
| 3453 | } |
| 3454 | |
| 3455 | $id3 = new getID3(); |
| 3456 | $data = $id3->analyze( $file ); |
| 3457 | |
| 3458 | // This is audio if the MIME says so. |
| 3459 | if ( |
| 3460 | isset( $data['mime_type'] ) && |
| 3461 | ( 0 === strpos( $data['mime_type'], 'audio/' ) ) |
| 3462 | ) { |
| 3463 | $checked['ext'] = 'm4a'; |
| 3464 | $checked['type'] = 'audio/mpeg'; |
| 3465 | } |
| 3466 | |
| 3467 | // The ftype might suggest its audioness. |
| 3468 | elseif ( |
| 3469 | isset( $data['quicktime']['ftype']['signature'] ) && |
| 3470 | ( 'M4A' === $data['quicktime']['ftype']['signature'] ) |
| 3471 | ) { |
| 3472 | $checked['ext'] = 'm4a'; |
| 3473 | $checked['type'] = 'audio/mpeg'; |
| 3474 | } |
| 3475 | |
| 3476 | // This might also be audio if there is no video resolution. |
| 3477 | elseif ( |
| 3478 | isset( $data['video']['resolution_x'] ) && |
| 3479 | ! $data['video']['resolution_x'] && |
| 3480 | isset( $data['video']['resolution_y'] ) && |
| 3481 | ! $data['video']['resolution_y'] |
| 3482 | ) { |
| 3483 | $checked['ext'] = 'm4a'; |
| 3484 | $checked['type'] = 'audio/mpeg'; |
| 3485 | } |
| 3486 | |
| 3487 | // Update the filename. |
| 3488 | if ( 'm4a' === $checked['ext'] ) { |
| 3489 | $checked['proper_filename'] = substr( $filename, 0, -3 ) . 'm4a'; |
| 3490 | } |
| 3491 | } |
| 3492 | |
| 3493 | return $checked; |
| 3494 | } |
| 3495 | add_filter( 'wp_check_filetype_and_ext', 'wp_check_mp4_filetype_and_ext', 100, 4 ); |
| 3496 | |
| 3497 | /** |
| 3498 | * Detect OGG Content Type |
| 3499 | * |
| 3500 | * The OGG file extension is intended for audio content, but might be |
| 3501 | * mistakenly applied to video files as well. This will attempt to |
| 3502 | * determine an OGG file's content and rename if needed. |
| 3503 | * |
| 3504 | * @since 4.9 |
| 3505 | * |
| 3506 | * @see https://core.trac.wordpress.org/ticket/40921 |
| 3507 | * |
| 3508 | * @param array $wp_check_filetype_and_ext File data array. |
| 3509 | * @param string $file Full path to file. |
| 3510 | * @param string $filename The name of the file. |
| 3511 | * @param array $mimes Allowed MIME types. |
| 3512 | * @return array File data. |
| 3513 | */ |
| 3514 | function wp_check_ogg_filetype_and_ext( $checked, $file, $filename, $mimes = null ) { |
| 3515 | if ( 'ogg' === $checked['ext'] ) { |
| 3516 | // Load ID3 parser. |
| 3517 | if ( ! defined( 'GETID3_TEMP_DIR' ) ) { |
| 3518 | define( 'GETID3_TEMP_DIR', get_temp_dir() ); |
| 3519 | } |
| 3520 | |
| 3521 | if ( ! class_exists( 'getID3', false ) ) { |
| 3522 | require( ABSPATH . WPINC . '/ID3/getid3.php' ); |
| 3523 | } |
| 3524 | |
| 3525 | $id3 = new getID3(); |
| 3526 | $data = $id3->analyze( $file ); |
| 3527 | |
| 3528 | // This is video if the MIME says so. |
| 3529 | if ( |
| 3530 | isset( $data['mime_type'] ) && |
| 3531 | ( 0 === strpos( $data['mime_type'], 'video/' ) ) |
| 3532 | ) { |
| 3533 | $checked['ext'] = 'ogv'; |
| 3534 | $checked['type'] = 'video/ogg'; |
| 3535 | } |
| 3536 | |
| 3537 | // This is video if there is a resolution. |
| 3538 | elseif ( |
| 3539 | ( isset( $data['video']['resolution_x'] ) && $data['video']['resolution_x'] > 0 ) || |
| 3540 | ( isset( $data['video']['resolution_y'] ) && $data['video']['resolution_y'] > 0 ) |
| 3541 | ) { |
| 3542 | $checked['ext'] = 'ogv'; |
| 3543 | $checked['type'] = 'video/ogg'; |
| 3544 | } |
| 3545 | |
| 3546 | // Update the filename. |
| 3547 | if ( 'ogv' === $checked['ext'] ) { |
| 3548 | $checked['proper_filename'] = substr( $filename, 0, -3 ) . 'ogv'; |
| 3549 | } |
| 3550 | } |
| 3551 | |
| 3552 | return $checked; |
| 3553 | } |
| 3554 | add_filter( 'wp_check_filetype_and_ext', 'wp_check_ogg_filetype_and_ext', 100, 4 ); |
| 3555 | |