Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

filelib - Added support for looking up icons via MIME type as well as…

… via extension
  • Loading branch information...
commit 3ce73b147f1f52f3b39a844e62393536c3812d80 1 parent d5e50c2
sam_marshall authored
Showing with 43 additions and 2 deletions.
  1. +43 −2 lib/filelib.php
View
45 lib/filelib.php
@@ -2,8 +2,14 @@
define('BYTESERVING_BOUNDARY', 's1k2o3d4a5k6s7'); //unique string constant
-function mimeinfo($element, $filename) {
- $mimeinfo = array (
+/**
+ * @return List of information about file types based on extensions.
+ * Associative array of extension (lower-case) to associative array
+ * from 'element name' to data. Current element names are 'type' and 'icon'.
+ * Unknown types should use the 'xxx' entry which includes defaults.
+ */
+function get_mimetypes_array() {
+ return array (
'xxx' => array ('type'=>'document/unknown', 'icon'=>'unknown.gif'),
'3gp' => array ('type'=>'video/quicktime', 'icon'=>'video.gif'),
'ai' => array ('type'=>'application/postscript', 'icon'=>'image.gif'),
@@ -128,6 +134,20 @@ function mimeinfo($element, $filename) {
'xsl' => array ('type'=>'text/xml', 'icon'=>'xml.gif'),
'zip' => array ('type'=>'application/zip', 'icon'=>'zip.gif')
);
+}
+
+/**
+ * Obtains information about a filetype based on its extension. Will
+ * use a default if no information is present about that particular
+ * extension.
+ * @param string $element Desired information (usually 'icon'
+ * for icon filename or 'type' for MIME type)
+ * @param string $filename Filename we're looking up
+ * @return string Requested piece of information from array
+ */
+function mimeinfo($element, $filename) {
+ static $mimeinfo;
+ $mimeinfo=get_mimetypes_array();
if (eregi('\.([a-z0-9]+)$', $filename, $match)) {
if (isset($mimeinfo[strtolower($match[1])][$element])) {
@@ -140,6 +160,27 @@ function mimeinfo($element, $filename) {
}
}
+/**
+ * Obtains information about a filetype based on the MIME type rather than
+ * the other way around.
+ * @param string $element Desired information (usually 'icon')
+ * @param string $mimetype MIME type we're looking up
+ * @return string Requested piece of information from array
+ */
+function mimeinfo_from_type($element, $mimetype) {
+ static $mimeinfo;
+ $mimeinfo=get_mimetypes_array();
+
+ foreach($mimeinfo as $values) {
+ if($values['type']==$mimetype) {
+ if(isset($values[$element])) {
+ return $values[$element];
+ }
+ break;
+ }
+ }
+ return $mimeinfo['xxx'][$element]; // Default
+}
/**
* @PARAM $filter int 0=no filtering, 1=all files, 2=html files only

0 comments on commit 3ce73b1

Please sign in to comment.
Something went wrong with that request. Please try again.