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