diff --git a/class-MimeTypes.php b/class-MimeTypes.php index 72e4a29..fd6b32f 100644 --- a/class-MimeTypes.php +++ b/class-MimeTypes.php @@ -1,278 +1,288 @@ . // fifo system default define_default( 'MAGIC_MIME_FILE', null ); /** * 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. + * 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) { + 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. + * Extract the file extension from a filename (if it's correct) * - * @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' + * @param string $filename The file name to be checked + * @param array|string|null $categories File categories (e.g. 'image'), NULL for every + * @param string|null $mimetype The file mime type (NULL to inherit from the file) + * @return string|false The file extension e.g.: 'png' or FALSE on failure */ - public function getFileExtensionFromExpectations($filename, $categories = null, $mimetype = null) { + public function getFileExtensionFromExpectations( $filename, $categories = null, $mimetype = null ) { + + // normalize the file name + // @TODO: have to insert an arg to enable this feature? + // ASD.JPEG => asd.jpeg + $filename = strtolower( $filename ); - // Have to insert an arg to enable this feature? - // ASD.JPEG => asd.jpeg - // @TODO - $filename = strtolower($filename); + // guess the mime type + if( !$mimetype ) { + $mimetype = self::fileMimetype( $filename ); + } - $expected_filetypes = $this->getFiletypes($categories, $mimetype); - foreach($expected_filetypes as $filetype) { + // compare every known file extensions with the filename + $expected_filetypes = $this->getFiletypes( $categories, $mimetype ); + foreach( $expected_filetypes as $filetype ) { $dotted = ".$filetype"; - $strlen = strlen($dotted); + $strlen = strlen( $dotted ); if( substr( $filename, -$strlen, $strlen) === $dotted ) { return $filetype; } } + return false; } /** * Guess the MIME type from a file * * It requires the existence of the MAGIC_MIME_FILE constant. * - * @param string $filepath The file path. - * @param bool $pure true for 'image/png; something'; or false for 'image/png'. + * @param string $filepath The file path + * @param bool $pure Set to true for 'image/png; something'; or false for 'image/png' (default) * @return string|false */ public static function fileMimetype( $filepath, $pure = false ) { $finfo = finfo_open( FILEINFO_MIME, MAGIC_MIME_FILE ); - if( ! $finfo ) { + if( !$finfo ) { error( sprintf( 'error opening fileinfo database placed in %s', MAGIC_MIME_FILE ) ); return false; } + $mime = finfo_file( $finfo, $filepath ); - if( ! $mime ) { + if( !$mime ) { error( "can't detect MIME of file $filepath" ); return false; } - if( ! $pure ) { + + if( !$pure ) { $mime = explode(';', $mime, 2); // Split "; charset" $mime = $mime[0]; } + return $mime; } private static function errorUnknownCategory($category) { error( "the MIME category $category is not registered" ); } } diff --git a/functions.php b/functions.php index 4db3dd1..ae0c4f9 100644 --- a/functions.php +++ b/functions.php @@ -1,1122 +1,1124 @@ . /** * Check if the user do not want to be tracked * * See the HTTP 'DNT' header. * * @return boolean */ function do_not_track() { return isset( $_SERVER['HTTP_DNT'] ) && $_SERVER['HTTP_DNT'] === '1'; } /* * Configuration helpers * * Shortcuts useful when you declare a configuration file. */ /** * Define a constant if it does not exist * * @param string $name Constant name * @param string $value Constant value */ function define_default( $name, $value ) { defined( $name ) or define( $name, $value ); } /* * Template helpers * * Shortcuts useful when you build a page */ /** * Return a ' selected="selected"' HTML attribute under some circumstances * * @param mixed $helper If this is the only arg, return * @param mixed $current If this matches $helper, return * @param bool $force If this is true, return */ function selected( $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) { return html_attribute_when_matching( 'selected', 'selected', $helper, $current, $force); } /** * Return a ' checked="checked"' HTML attribute under some circumstances * * This is useful for the HTML tag. * * @param mixed $helper If this is the only arg, return * @param mixed $current If this matches $helper, return * @param bool $force If this is true, return */ function checked( $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) { return html_attribute_when_matching( 'checked', 'checked', $helper, $current, $force); } /** * Return a ' disabled="disabled"' HTML attribute under some circumstances * * @param mixed $helper If this is the only arg, return * @param mixed $current If this matches $helper, return * @param bool $force If this is true, return */ function disabled( $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) { return html_attribute_when_matching( 'disabled', 'disabled', $helper, $current, $force ); } /** * Return an HTML attribute under some circumstances * * @param string $attribute HTML attribute name e.g. 'disabled' * @param string $value HTML attribute value e.g. * @param mixed $helper If this is the only arg, return * @param mixed $current If this matches $helper, return * @param bool $force If this is true, return */ function html_attribute_when_matching( $attribute, $value, $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) { if( $helper === $current || $helper && PHP_INT_MAX === $current || $force ) { return HTML::property( $attribute, $value ); } return ''; } /** * Get an HTML attribute ' value="$v"' * * The value will be sanitized. * * @param string $v * @return string */ function value( $v ) { return HTML::property( 'value', $v ); } /** * Shortcut for htmlentities() * * Return an HTML-sanitized untrusted string to be safe from XSS. * * @param string $s e.g. 'Hello