diff --git a/class-MimeTypes.php b/class-MimeTypes.php index 0e6cb67..7599425 100644 --- a/class-MimeTypes.php +++ b/class-MimeTypes.php @@ -1,279 +1,276 @@ . /** * Handle MIME and file types (extensions). * * @use MAGIC_MIME_FILE, functions.php */ class MimeTypes { /** * [ * 'category-1' => [ * 'mime' => ['filetype1', 'filetype2'], * ... * ], * ... * ] * * @var array */ private $mimeTypes; function __construct( $defaults = true ) { if( $defaults ) { $this->loadDefaults(); } } /** * Get the singleton instance * * @return self */ public static function instance() { static $me = false; if( ! $me ) { $me = new self(); } return $me; } /** * * @param string $category Category e.g. 'audio' * @param mixed $mimeTypes Array of MIME => file types. * E.g.: * [ * 'image/png' => 'png', * 'image/jpeg' => array['jpg', 'jpeg'], * ... * ] */ public function registerMimetypes($category, $mimeTypes) { // Register category if not exists if( ! isset( $this->mimeTypes[ $category ] ) ) { $this->mimeTypes[ $category ] = []; } foreach($mimeTypes as $mime => $filetypes) { force_array( $filetypes ); if( ! isset( $this->mimeTypes[ $category ][ $mime ] ) ) { $this->mimeTypes[ $category ][ $mime ] = $filetypes; continue; // No need next lines. } $this->mimeTypes[ $category ][ $mime ] = array_merge( $this->mimeTypes[ $category ][ $mime ], $filetypes ); } } /** * Load default MIME types */ private function loadDefaults() { $this->registerMimetypes('image', [ 'image/jpeg' => ['jpg', 'jpeg'], 'image/png' => 'png', 'image/gif' => 'gif' ] ); $this->registerMimetypes('audio', [ 'audio/x-flac' => 'flac', 'audio/ogg' => 'ogg', 'audio/vorbis' => 'ogg', 'audio/vorbis-config' => 'ogg', 'audio/mpeg' => 'mp3', 'audio/MPA' => 'mp4', 'audio/mpa-robust' => 'mp4' ] ); $this->registerMimetypes('video', [ 'video/mp4' => 'mp4', 'application/ogg' => 'ogg' ] ); $this->registerMimetypes('document', [ 'application/pdf' => 'pdf', 'application/x-pdf' => 'pdf', 'application/x-bzpdf' => 'pdf', 'application/x-gzpdf' => 'pdf', 'application/vnd.oasis.opendocument.text' => 'odt', 'application/vnd.oasis.opendocument.presentation' => 'odp', 'application/vnd.oasis.opendocument.spreadsheet' => 'ods', 'application/vnd.oasis.opendocument.graphics' => 'odg', 'application/msword' => 'doc', 'application/vnd.ms-excel' => 'xls', 'application/vnd.ms-powerpoint' => 'ppt', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => ['docx', 'xlsx', 'pptx'] ] ); } /** * Get an array of MIME of the selected category. If category is NULL, all the MIME are returned. * * @param array|string|null $category Category e.g. 'audio' * @return array Array of MIME. */ public function getMimetypes($categories = null) { $allMimeTypes = []; if( $categories === null ) { // All MIME types foreach($this->mimeTypes as $mimeTypes) { foreach($mimeTypes as $mime => $filetypes) { $allMimeTypes[] = $mime; } } } else { force_array( $categories ); // MIME types from selected categories foreach($categories as $category) { if( ! isset( $this->mimeTypes[ $category ] ) ) { self::errorUnknownCategory( $category ); continue; } foreach($this->mimeTypes[ $category ] as $mime => $filetypes) { $allMimeTypes[] = $mime; } } if( count($categories) > 0 ) { $allMimeTypes = array_unique( $allMimeTypes ); } } return $allMimeTypes; } /** * Check if a MIME belongs to a certain category. * * @param string $mime_value E.g.: 'audio/vorbis'. * @param mixed $category E.g.: 'document' * @return bool */ public function isMimetypeInCategory($mimetype, $category = null ) { return in_array($mimetype, $this->getMimetypes($category), true); } /** * Get the file types of a MIME. * * @param string|null $category * If NULL it search in all the categories. * @param string|null $mimetype The MIME type. * @return mixed FALSE if the MIME is not registered or an array of file types. */ public function getFiletypes($categories = null, $mimetype = null) { $all_types = []; if( $categories === null ) { // Search in all the categories. foreach($this->mimeTypes as $mimeTypes) { foreach($mimeTypes as $mime => $types) { if($mimetype === null || $mime === $mimetype) { $all_types = array_merge($all_types, $types); } } } } else { force_array( $categories ); // Search *that* category if exists foreach($categories as $category) { if( ! isset( $this->mimeTypes[ $category ] ) ) { self::errorUnknownCategory($category); continue; } foreach($this->mimeTypes[ $category ] as $mime => $types) { if($mimetype === null || $mime === $mimetype) { $all_types = array_merge($all_types, $types); } } } } return array_unique( $all_types ); } /** * Check (and get) the known file extension. * * @param string $filename The file name. * @param array|string|null $categories MIME categories. * @param string|null $mimetype The MIME type. * @return string|false FALSE or the file extension e.g.: 'png' */ public function getFileExtensionFromExpectations($filename, $categories = null, $mimetype = null) { // Have to insert an arg to enable this feature? // ASD.JPEG => asd.jpeg // @TODO $filename = strtolower($filename); $expected_filetypes = $this->getFiletypes($categories, $mimetype); foreach($expected_filetypes as $filetype) { $dotted = ".$filetype"; $strlen = strlen($dotted); if( substr( $filename, -$strlen, $strlen) === $dotted ) { return $filetype; } } return false; } /** * Get the MIME type from a file. * * @param string $filepath The file path. * @param bool $pure * true for 'image/png; something'; * false for 'image/png'. * @return string|false */ public static function fileMimetype( $filepath, $pure = false ) { $finfo = finfo_open( FILEINFO_MIME, MAGIC_MIME_FILE ); if( ! $finfo ) { return error( sprintf( 'error opening the fileinfo database expected in %s', MAGIC_MIME_FILE ) ); } $mime = finfo_file( $finfo, $filepath ); if( ! $mime ) { return error( sprintf( "can't detect the mime of the file %s", $filepath ) ); } if( ! $pure ) { $mime = explode(';', $mime, 2); // Split "; charset" $mime = $mime[0]; } return $mime; } private static function errorUnknownCategory($category) { - error( sprintf( - 'the MIME category %s is not registered"), - $category - ) ); + error( "the MIME category $category is not registered" ); } }