wp/lib/blobfolio/wp/bm/mime.php
public static function check_real_filetype($file, $filename=null, $mimes= null) {
		// Default filename.
		if (empty($filename)) {
			$filename = basename($file);
		}

		// Default MIMEs.
		if (empty($mimes)) {
			$mimes = get_allowed_mime_types();
		}

		// Run a name-based check first.
		$checked = wp_check_filetype($filename, $mimes);

		// Only dig deeper if we can.
		if (
			false !== $checked['ext'] &&
			false !== $checked['type'] &&
			@file_exists($file) &&
			@filesize($file) > 0
		) {
			$real_mime = false;

			try {
				// Fall back to fileinfo, if available.
				if (
					false === $real_mime &&
					extension_loaded('fileinfo') &&
					defined('FILEINFO_MIME_TYPE')
				) {
					$finfo = finfo_open(FILEINFO_MIME_TYPE);
					$real_mime = finfo_file($finfo, $file);
					finfo_close($finfo);

					// Account for inconsistent return values.
					if (!is_string($real_mime) || !$real_mime) {
						$real_mime = false;
					}
					else {
						$real_mime = strtolower(sanitize_mime_type($real_mime));
					}
				}
			} catch (\Throwable $e) {
				$real_mime = false;
			} catch (\Exception $e) {
				$real_mime = false;
			}

			// We might need to override fileinfo.
			if ((false !== $real_mime) && ($real_mime !== $checked['type'])) {
				// SVGs are often misidentified if they are missing the
				// leading XML tag and/or DOCTYPE declarations.
				if ('image/svg+xml' === $checked['type']) {
					$tmp = @file_get_contents($file);
					if (
						is_string($tmp) &&
						preg_match('/\s*<svg/iu', $tmp)
					) {
						$real_mime = 'image/svg+xml';
					}
				}

				// JSON doesn't have much magic for fileinfo to work
				// with, but we can assume if it parses, it's JSON.
				elseif (
					function_exists('json_decode') &&
					static::check_alias($checked['ext'], 'application/json')
				) {
					$tmp = @file_get_contents($file);
					if (is_string($tmp) && $tmp) {
						$tmp2 = @json_decode($tmp, true);
						if (null !== $tmp2) {
							$real_mime = $checked['type'];
						}
					}
				}

				// It is easy to confuse XML with HTML, usually because
				// a document is missing a leading XML tag.
				elseif (
					('text/html' === $real_mime) &&
					static::check_alias($checked['ext'], 'text/xml')
				) {
					$real_mime = $checked['type'];
				}
			}

			// Evaluate our real MIME.
			if ((false !== $real_mime) && ($real_mime !== $checked['type'])) {
				if (!static::check_alias($checked['ext'], $real_mime)) {
					// Maybe this type belongs to another allowed extension.
					if (false !== $result = static::check_allowed_aliases($real_mime, $mimes)) {
						$checked['ext'] = $result['ext'];
						$checked['type'] = $result['type'];
					}
					// Otherwise reject the results.
					else {
						$checked['ext'] = false;
						$checked['type'] = false;
					}
				}
			}
		}// End content-based type checking.

		// Filter.
		return apply_filters(
			'blobmimes_check_real_filetype',
			$checked,
			$file,
			$filename,
			$mimes
		);
	}
public static function check_real_filetype($file, $filename=null, $mimes= null) {
		// Default filename.
		if (empty($filename)) {
			$filename = basename($file);
		}

		// Default MIMEs.
		if (empty($mimes)) {
			$mimes = get_allowed_mime_types();
		}

		// Run a name-based check first.
		$checked = wp_check_filetype($filename, $mimes);

		// Only dig deeper if we can.
		if (
			false !== $checked['ext'] &&
			false !== $checked['type'] &&
			@file_exists($file) &&
			@filesize($file) > 0
		) {
			$real_mime = false;

			try {
				// Fall back to fileinfo, if available.
				if (
					false === $real_mime &&
					extension_loaded('fileinfo') &&
					defined('FILEINFO_MIME_TYPE')
				) {
					$finfo = finfo_open(FILEINFO_MIME_TYPE);
					$real_mime = finfo_file($finfo, $file);
					finfo_close($finfo);

					// Account for inconsistent return values.
					if (!is_string($real_mime) || !$real_mime) {
						$real_mime = false;
					}
					else {
						$real_mime = strtolower(sanitize_mime_type($real_mime));
					}
				}
			} catch (\Throwable $e) {
				$real_mime = false;
			} catch (\Exception $e) {
				$real_mime = false;
			}

			// We might need to override fileinfo.
			if ((false !== $real_mime) && ($real_mime !== $checked['type'])) {
				// SVGs are often misidentified if they are missing the
				// leading XML tag and/or DOCTYPE declarations.
				if ('image/svg+xml' === $checked['type']) {
					$tmp = @file_get_contents($file);
					if (
						is_string($tmp) &&
						preg_match('/\s*<svg/iu', $tmp)
					) {
						$real_mime = 'image/svg+xml';
					}
				}

				// JSON doesn't have much magic for fileinfo to work
				// with, but we can assume if it parses, it's JSON.
				elseif (
					function_exists('json_decode') &&
					static::check_alias($checked['ext'], 'application/json')
				) {
					$tmp = @file_get_contents($file);
					if (is_string($tmp) && $tmp) {
						$tmp2 = @json_decode($tmp, true);
						if (null !== $tmp2) {
							$real_mime = $checked['type'];
						}
					}
				}

				// It is easy to confuse XML with HTML, usually because
				// a document is missing a leading XML tag.
				elseif (
					('text/html' === $real_mime) &&
					static::check_alias($checked['ext'], 'text/xml')
				) {
					$real_mime = $checked['type'];
				}
			}

			// Evaluate our real MIME.
			if ((false !== $real_mime) && ($real_mime !== $checked['type'])) {
				if (!static::check_alias($checked['ext'], $real_mime)) {
					// Maybe this type belongs to another allowed extension.
					if (false !== $result = static::check_allowed_aliases($real_mime, $mimes)) {
						$checked['ext'] = $result['ext'];
						$checked['type'] = $result['type'];
					}
					// Otherwise reject the results.
					else {
						$checked['ext'] = false;
						$checked['type'] = false;
					}
				}
			}
		}// End content-based type checking.

		// Filter.
		return apply_filters(
			'blobmimes_check_real_filetype',
			$checked,
			$file,
			$filename,
			$mimes
		);
	}
Retrieve the "real" file type from the file.

This extends `wp_check_filetype()` to additionally
consider content-based indicators of a file's
true type.

The content-based type will override the name-based
type if available and included in the $mimes list.

A false response will be set if the extension is
not allowed, or if a "real MIME" was found and
that MIME is not allowed.

@see wp_check_filetype()
@see wp_check_filetype_and_ext()

@param string $file Full path to the file.
@param string $filename The name of the file (may differ from $file due to $file being in a tmp directory).
@param array  $mimes Optional. Key is the file extension with value as the mime type.
@return array Values with extension first and mime type.