cfeb642dbe24da0a4baf0f63905607fea0477bda
[pear] / File / MimeType.php
1 <?php
2 /*
3 $y = new File_MimeType();
4 //var_dump($y->fromExt('doc'));
5
6 $y = new File_MimeType();
7 var_dump($y->fromFilename('fred.doc'));
8
9
10 $y = new File_MimeType();
11 //var_dump($y->toExt('application/x-pdf'));
12
13 require_once '../Pman/DataObjects/Documents.php';
14
15 $x = new Pman_DataObjects_Documents();
16 $ar = $x->mimeTypeMap();
17
18 foreach($ar as $ext=>$mt) {
19     $calc = $y->fromExt($ext);
20     if ($calc != $mt) {
21         echo "array( '$mt', array('$ext')),<BR>";
22     }
23 }
24 */
25
26 $GLOBALS['File_MimeType'] = array();
27
28 class File_MimeType
29 {
30     function __construct()
31     {
32         
33         $this->load();
34         
35     }
36     function load()
37     {
38         if (!empty($GLOBALS['File_MimeType'] )) {
39             return;
40         }
41         $mtl = array();//&$GLOBALS['File_MimeType'];
42         $f = '/etc/mime.types';
43         if (!@file_exists($f)) { // open_basedir..
44             $f= dirname(__FILE__).'/mime.types';
45         }
46         $ar = file($f);
47         foreach($ar as $l) {
48             $l = strtolower( trim($l) );
49             if (!strlen($l) || $l[0] == '#') {
50                 continue;
51             }
52             $ex = preg_split('/\s+/', $l);
53             if ((count($ex) == 1)) {
54                 continue;
55             }
56             $mt = array_shift($ex);
57             
58             $mtl[] = array($mt, $ex);
59             
60         }
61         
62         $GLOBALS['File_MimeType'] = array_merge(
63             array(
64                 // override!!!!
65                 array( 'application/octet-stream', array('dat')),
66                 array( 'application/dwg', array('dwg')), // << this is wrong
67                 //array( 'application/vnd.dwg', array('dwg')), // this is prefered?
68                 array( 'application/dxf', array('dxf')),
69                 
70                 array( 'application/mswordapplication', array('doc')),
71                 array( 'application/vnd.openxmlformats-officedocument.presentationml.template', array('potx')),
72                 array( 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', array('ppsx')),
73                 array( 'application/vnd.openxmlformats-officedocument.presentationml.presentation', array('pptx')),
74                 array( 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', array('xlsx')),
75                 array( 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', array('xltx')),
76                 array( 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', array('docx')),
77                 array( 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', array('dotx')),
78                 array( 'application/voicexml+xml', array('vxml')),
79                 array( 'application/x-futuresplash', array('spl')),
80                 array( 'application/x-netcdf', array('cdf')),
81                 array( 'application/x-tex', array('tex')),
82                 array( 'application/xslt+xml', array('xslt')),
83                 array( 'application/xml-dtd', array('dtd')),
84                 
85                 
86                 
87                 array( 'audio/x-mpegurl', array('m3u')),
88                 array( 'audio/mpeg', array('mp3')),
89                 array( 'application/vnd.rn-realmedia', array('rm')),
90                 array( 'image/bmp', array('bmp')),
91                 array( 'image/cgm', array('cgm')),
92                 
93                 array( 'image/x-icon', array('ico')),
94                 array( 'image/vnd.microsoft.icon', array('ico')),
95                 
96                 array( 'image/svg', array('svg')),
97                 
98                 array( 'text/calendar', array('ifb')),
99                 array( 'text/csv', array('csv')),
100                 array( 'text/xml', array('xml')),
101                 array( 'text/sgml', array('sgml')),
102                 array( 'text/sgml', array('sgm')),
103                 // bjs is a Pman json file..
104                 array( 'text/plain', array('txt','asc', 'text','pot','brf', 'bjs')),
105                 
106                 array( 'video/vnd.mpegurl', array('m4u')),
107                 array( 'video/x-flv', array('flv')),
108                 array( 'video/ogg', array('ogv')),
109                 
110                 
111             
112             
113             
114             ), 
115             
116             $mtl);
117         
118         // fixes...
119         
120         
121         ///echo '<PRE>'; var_dump($mtl);
122     }
123     
124     function fromExt($ext) 
125     {
126         //echo "LOOKUP" ; var_dump($ext);
127         $mtl = $GLOBALS['File_MimeType'];
128         $ext = strtolower($ext);
129         foreach($mtl as $mtd) {
130             if (in_array($ext, $mtd[1])) {
131                 return $mtd[0];
132             }
133         }
134         return 'application/octet-stream';
135     }
136     
137     function fromFilename($fn)
138     {
139         $ar = explode('.', $fn);
140         return $this->fromExt(array_pop($ar));
141     }
142     
143     function toExt($mtype) 
144     {
145         $mtype = strtolower($mtype);
146         switch($mtype) {
147             case 'image/jpg':
148                 $mtype = 'image/jpeg';
149                 break;
150             // any others.?
151         }
152         
153         $mtl = $GLOBALS['File_MimeType'];
154          
155         foreach($mtl as $mtd) {
156             if ($mtd[0] == $mtype) {
157                 return $mtd[1][0];
158             }
159         }
160         
161         return '';
162     }
163     
164     
165 }
166
167