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