Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Lib.FileMime.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Assosiate icon with mime type
8  *
9  * @author Anakeen
10  * @version $Id: Lib.FileMime.php,v 1.9 2008/05/06 08:20:43 marc Exp $
11  * @package FDL
12  * @subpackage CORE
13  */
14 /**
15  */
16 
17 function getIconMimeArray()
18 {
19  return array(
20  "application/msword" => "mime-wordprocessing",
21  "application/vnd.openxmlformats-officedocument.wordprocessingml.document" => "mime-wordprocessing",
22  "application/ogg" => "audio-x-generic",
23  "application/pdf" => "mime-pdf",
24  "application/postscript" => "mime-wordprocessing",
25  "application/rtf" => "mime-wordprocessing",
26  "application/vnd.lotus-1-2-3" => "mime-spreadsheet",
27  "application/vnd.ms-excel" => "mime-spreadsheet",
28  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" => "mime-spreadsheet",
29  "application/vnd.ms-powerpoint" => "mime-presentation",
30  "application/vnd.oasis.opendocument.presentation" => "mime-presentation",
31  "application/vnd.openxmlformats-officedocument.presentationml.presentation" => "mime-presentation",
32  "application/vnd.oasis.opendocument.spreadsheet" => "mime-spreadsheet",
33  "application/vnd.oasis.opendocument.text" => "mime-wordprocessing",
34  "application/vnd.oasis.opendocument.graphics" => "mime-vectorgfx",
35  "application/vnd.stardivision.calc" => "mime-spreadsheet",
36  "application/vnd.stardivision.impress" => "mime-presentation",
37  "application/vnd.stardivision.writer" => "mime-wordprocessing",
38  "application/vnd.sun.xml.calc" => "mime-spreadsheet",
39  "application/vnd.sun.xml.impress" => "mime-presentation",
40  "application/vnd.sun.xml.writer" => "mime-wordprocessing",
41  "application/vnd.sun.xml.writer.global" => "mime-wordprocessing",
42  "application/vnd.sun.xml.draw" => "mime-vectorgfx",
43  "application/wordperfect" => "mime-wordprocessing",
44  "application/x-7z-compressed" => "mime-tgz",
45  "application/x-abiword" => "mime-wordprocessing",
46  "application/x-applix-spreadsheet" => "mime-spreadsheet",
47  "application/x-applix-word" => "mime-wordprocessing",
48  "application/x-archive" => "mime-tar",
49  "application/x-arj" => "mime-tgz",
50  "application/x-bzip-compressed-tar" => "mime-tgz",
51  "application/x-bzip" => "mime-tgz",
52  "application/x-compressed-tar" => "mime-tgz",
53  "application/x-compress" => "mime-tgz",
54  "application/x-cpio-compressed" => "mime-tgz",
55  "application/x-cpio" => "mime-tgz",
56  "application/x-deb" => "mime-tgz",
57  "application/x-dvi" => "mime-wordprocessing",
58  "application/x-executable" => "application/x-executable",
59  "application/x-font-afm" => "font-x-generic",
60  "application/x-font-bdf" => "font-x-generic",
61  "application/x-font-linux-psf" => "font-x-generic",
62  "application/x-font-pcf" => "font-x-generic",
63  "application/x-font-sunos-news" => "font-x-generic",
64  "application/x-font-ttf" => "font-x-generic",
65  "application/x-gnumeric" => "mime-spreadsheet",
66  "application/x-gzip" => "mime-tgz",
67  "application/xhtml+xml" => "text-html",
68  "application/x-killustrator" => "mime-vectorgfx",
69  "application/x-kpresenter" => "mime-presentation",
70  "application/x-kspread" => "mime-spreadsheet",
71  "application/x-kword" => "mime-wordprocessing",
72  "application/x-ms-dos-executable" => "application/x-executable",
73  "application/x-perl" => "mime-shellscript",
74  "application/x-php" => "text-html",
75  "application/x-python-bytecode" => "mime-shellscript",
76  "application/x-rar" => "mime-tgz",
77  "application/x-rpm" => "mime-rpm",
78  "application/x-shellscript" => "mime-shellscript",
79  "application/x-shockwave-flash" => "video-x-generic",
80  "application/x-tar" => "mime-tar",
81  "application/x-tarz" => "mime-tgz",
82  "application/x-tex" => "mime-tex",
83  "application/x-zip" => "mime-zip",
84  "application/zip" => "mime-zip",
85  "application" => "mime-zip",
86  "audio" => "mime-sound",
87  "image" => "mime-image2",
88  "video" => "mime-video",
89  "image/jpeg" => "mime-image",
90  "text/html" => "mime-html",
91  "text/xml" => "mime-xml",
92  "text/x-c++" => "mime-source_cpp",
93  "text/x-c" => "mime-source_c",
94  "text/x-c-header" => "mime-source_h",
95  "text/x-php" => "mime-source_php",
96  "text" => "mime-txt",
97  "text/vnd.wap.wml" => "mime-html",
98  "text/x-csh" => "mime-shellscript",
99  "text/x-python" => "mime-shellscript",
100  "text/x-sh" => "mime-shellscript",
101  "text/x-vcalendar" => "mime-vcalendar",
102  "text/x-vcard" => "mime-address-book",
103  "text/x-zsh" => "mime-shellscript",
104  "text/x-makefile" => "mime-make",
105  "x-font-afm" => "font-x-generic"
106  );
107 }
108 
109 function getIconMimeFile($sysmime)
110 {
111  $icon = "";
112  if ($sysmime) {
113  $tmime = explode(";", $sysmime);
114  $mime = trim($tmime[0]);
115  $mimeIcon = getIconMimeArray();
116  if (isset($mimeIcon[$mime])) {
117  $icon = $mimeIcon[$mime] . ".png";
118  } else {
119  $p = strpos($mime, '/');
120  $mime = substr($mime, 0, $p);
121  if (isset($mimeIcon[$mime])) {
122  $icon = $mimeIcon[$mime] . ".png";
123  }
124  }
125  }
126  return $icon;
127 }
128 /**
129  * return system file mime
130  * @param string $f filename
131  * @param string $fn basename of file (can be different of real path)
132  * return string mime like text/html
133  */
134 function getSysMimeFile($f, $fn = "")
135 {
136  if (!file_exists($f)) return false;
137 
138  $ret = getMimeFile($fn, 'sys');
139  if ($ret !== false) {
140  return $ret;
141  }
142 
143  $sys = trim(shell_exec(sprintf("file --mime -b %s", escapeshellarg($f))));
144  $txt = getTextMimeFile($f);
145  $sys = strtok($sys, " ;\n\t"); // only first part
146  // correct errors of file function
147  if (preg_match('/Makefile/', $fn)) return 'text/x-makefile';
148  if (preg_match('/ASCII C\+\+/', $txt)) {
149  if (preg_match('/\.h$/', $fn)) return 'text/x-c-header';
150  return 'text/x-c++';
151  }
152  if (preg_match('/PHP script/', $txt)) return 'text/x-php';
153  // open office archive
154  if (preg_match('/zip$/', $sys) || (preg_match('/octet-stream$/', $sys))) {
155 
156  if (preg_match('/OpenOffice/', $txt)) {
157  if (preg_match('/\.sxw$/', $fn)) return 'application/vnd.sun.xml.writer';
158  if (preg_match('/\.sxc$/', $fn)) return 'application/vnd.sun.xml.calc';
159  if (preg_match('/\.sxi$/', $fn)) return 'application/vnd.sun.xml.impress';
160  if (preg_match('/\.sxd$/', $fn)) return 'application/vnd.sun.xml.draw';
161  if (preg_match('/\.sxg$/', $fn)) return 'application/vnd.sun.xml.writer.global';
162  return 'application/vnd.sun.xml.writer';
163  }
164  if (preg_match('/OpenDocument/', $txt)) {
165  if (preg_match('/\.odp$/', $fn)) return 'application/vnd.oasis.opendocument.presentation';
166  if (preg_match('/\.odt$/', $fn)) return 'application/vnd.oasis.opendocument.text';
167  if (preg_match('/\.ods$/', $fn)) return 'application/vnd.oasis.opendocument.spreadsheet';
168  if (preg_match('/\.odg$/', $fn)) return 'application/vnd.oasis.opendocument.graphics';
169  return 'application/vnd.oasis.opendocument.text';
170  }
171 
172  if (preg_match('/\.sxw$/', $fn)) return 'application/vnd.sun.xml.writer';
173  if (preg_match('/\.sxc$/', $fn)) return 'application/vnd.sun.xml.calc';
174  if (preg_match('/\.sxi$/', $fn)) return 'application/vnd.sun.xml.impress';
175  if (preg_match('/\.sxd$/', $fn)) return 'application/vnd.sun.xml.draw';
176  if (preg_match('/\.sxg$/', $fn)) return 'application/vnd.sun.xml.writer.global';
177  if (preg_match('/\.odp$/', $fn)) return 'application/vnd.oasis.opendocument.presentation';
178  if (preg_match('/\.odt$/', $fn)) return 'application/vnd.oasis.opendocument.text';
179  if (preg_match('/\.ods$/', $fn)) return 'application/vnd.oasis.opendocument.spreadsheet';
180  if (preg_match('/\.odg$/', $fn)) return 'application/vnd.oasis.opendocument.graphics';
181  }
182  if (!$sys) {
183  // try with text only
184  if ($txt) {
185  if (preg_match('/^XML/', $txt)) return 'text/xml';
186  }
187 
188  if (preg_match('/\.xls$/', $fn)) return 'application/vnd.ms-excel';
189  if (preg_match('/\.doc$/', $fn)) return 'application/msword';
190  if (preg_match('/\.ppt$/', $fn)) return 'application/vnd.ms-powerpoint';
191  }
192  if ($sys == 'application/msword') { // for old sys mime info
193  if (preg_match('/\.xls$/', $fn)) return 'application/vnd.ms-excel';
194  if (preg_match('/\.ppt$/', $fn)) return 'application/vnd.ms-powerpoint';
195  }
196 
197  return strtok($sys, " ;\n\t");
198 }
199 function getTextMimeFile($f, $fn = '')
200 {
201  $ret = getMimeFile($fn, 'text');
202  if ($ret !== false) {
203  return $ret;
204  }
205 
206  $txt = trim(shell_exec(sprintf("file -b %s", escapeshellarg($f))));
207  if ($txt == 'data') {
208  if (preg_match('/\.ods$/', $f)) $txt = 'OpenDocument Spreadsheet';
209  else if (preg_match('/\.odt$/', $f)) $txt = 'OpenDocument Text';
210  else if (preg_match('/\.odp$/', $f)) $txt = 'OpenDocument Presentation';
211  else if (preg_match('/\.odg$/', $f)) $txt = 'OpenDocument Drawing';
212  }
213  if (!$txt) return " ";
214  return $txt;
215 }
216 /**
217  * get current extension from system mime
218  * @return string (empty string if no extension found)
219  */
220 function getExtension($smime)
221 {
222  include_once ("MimeExtension.php");
223  $exts = getExtensions();
224  if (!empty($exts[$smime])) {
225  return strtok($exts[$smime], " \n\t");
226  }
227  return "";
228 }
229 /**
230  * get extension from file name
231  */
233 {
234  $ext = false;
235  if ($filename && strrpos($filename, '.') > 0) $ext = substr($filename, strrpos($filename, '.') + 1);
236  return $ext;
237 }
238 /**
239  * get MIME type/text from mime.conf and mime-user.conf files
240  */
241 function getMimeFile($filename, $type = 'sys')
242 {
243  $conf_user = loadUserMimeConf();
244  $conf_global = loadMimeConf();
245 
246  $conf = array_merge($conf_user, $conf_global);
247 
248  foreach ($conf as $rule) {
249  $ext = $rule['ext'];
250  if (preg_match("/\.\Q$ext\E$/i", $filename)) {
251  return $rule[$type];
252  }
253  }
254 
255  return false;
256 }
257 /**
258  * load mime-user.conf XML file into PHP array
259  */
261 {
262  include_once ('WHAT/Lib.Prefix.php');
263 
264  $rules = array();
265 
266  $conf_file = DEFAULT_PUBDIR . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "mime-user.conf";
267  if (!file_exists($conf_file) || !is_readable($conf_file)) {
268  return $rules;
269  }
270 
271  $xml = simplexml_load_file($conf_file);
272  if ($xml === false) {
273  error_log(__FUNCTION__ . " " . sprintf("Could not load user MIME config '%s'.", $conf_file));
274  return $rules;
275  }
276 
277  foreach ($xml->mime as $mimeNode) {
278  $rule = array();
279  foreach (array(
280  'ext',
281  'sys',
282  'text'
283  ) as $attrName) {
284  $rule[$attrName] = (string)$mimeNode[$attrName];
285  }
286  array_push($rules, $rule);
287  }
288 
289  return $rules;
290 }
291 /**
292  * load mime.conf XML file into PHP array
293  */
294 function loadMimeConf()
295 {
296  include_once ('WHAT/Lib.Prefix.php');
297 
298  $rules = array();
299 
300  $conf_file = DEFAULT_PUBDIR . DIRECTORY_SEPARATOR . "config" . DIRECTORY_SEPARATOR . "mime.conf";
301  $xml = simplexml_load_file($conf_file);
302  if ($xml === false) {
303  error_log(__FUNCTION__ . " " . sprintf("Could not load MIME config '%s'.", $conf_file));
304  return $rules;
305  }
306 
307  foreach ($xml->mime as $mimeNode) {
308  $rule = array();
309  foreach (array(
310  'ext',
311  'sys',
312  'text'
313  ) as $attrName) {
314  $rule[$attrName] = (string)$mimeNode[$attrName];
315  }
316  array_push($rules, $rule);
317  }
318 
319  return $rules;
320 }
321 /**
322  * get number of pages from pdf file
323  * @return int
324  */
326 {
327  $nbpages = 0;
328  if (file_exists($file)) {
329  $nbpages = intval(trim(shell_exec(sprintf('grep -c "/Type[[:space:]]*/Page>" %s', escapeshellarg($file)))));
330  }
331  return $nbpages;
332 }
333 ?>
getTextMimeFile($f, $fn= '')
getIconMimeArray()
loadUserMimeConf()
$ret
$filename
getSysMimeFile($f, $fn="")
$file
getMimeFile($filename, $type= 'sys')
getPdfNumberOfPages($file)
getIconMimeFile($sysmime)
getExtensions()
const DEFAULT_PUBDIR
Definition: Lib.Prefix.php:28
getFileExtension($filename)
loadMimeConf()
getExtension($smime)
← centre documentaire © anakeen