Release diogenes-0.9.22
[diogenes.git] / include / diogenes / diogenes.mime.inc.php
CommitLineData
6855525e
JL
1<?php
2/*
3 * Copyright (C) 2003-2004 Polytechnique.org
4 * http://opensource.polytechnique.org/
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21
22/** Custom implementation of mime_content_type.
23 * It avoids problems encountered with the built-in mime_content_type such as
24 * - CSS files being returned as text/plain
25 */
26function get_mime_type($filename) {
27 // we use the file extension for the basic file types, as file
28 // sometimes gets confused
29
30 $mime_map = array(
31 ".ai" => "application/postscript",
32 ".arj" => "application/arj",
33 ".asf" => "video/x-ms-asf",
34 ".asr" => "video/x-ms-asf",
35 ".asx" => "video/x-ms-asf",
36 ".au" => "audio/basic",
37 ".avi" => "video/x-msvideo",
38 ".bmp" => "image/bmp",
39 ".bz" => "application/x-bzip",
40 ".bz2" => "application/x-bzip2",
41 ".css" => "text/css",
42 ".dcf" => "application/vnd.oma.drm.content",
43 ".deb" => "application/x-deb",
44 ".dm" => "application/vnd.oma.drm.message",
45 ".doc" => "application/msword",
46 ".dot" => "application/msword",
47 ".dvi" => "application/x-dvi",
48 ".eps" => "application/postscript",
49 ".gif" => "image/gif",
50 ".gz" => "application/x-gzip",
51 ".gzip" => "application/x-gzip",
52 ".hqx" => "application/mac-binhex40",
53 ".html" => "text/html",
54 ".htm" => "text/html",
55 ".jad" => "text/vnd.sun.j2me.app-descriptor",
56 ".jar" => "application/java-archive",
57 ".jfif" => "image/jpeg",
58 ".jpe" => "image/jpeg",
59 ".jpeg" => "image/jpeg",
60 ".jpg" => "image/jpeg",
61 ".lyx" => "text/x-lyx",
62 ".midi" => "audio/midi",
63 ".mid" => "audio/midi",
64 ".mp2" => "audio/mpeg",
65 ".mp3" => "audio/mpeg",
66 ".mpg" => "video/mpeg",
67 ".mpe" => "video/mpeg",
68 ".mpeg" => "video/mpeg",
69 ".mov" => "video/quicktime",
70 ".pbm" => "image/x-portable-bitmap",
71 ".pdf" => "application/pdf",
72 ".png" => "image/x-png",
73 ".pnm" => "image/x-portable-anymap",
74 ".ppt" => "application/vnd.ms-powerpoint",
75 ".pps" => "application/vnd.ms-powerpoint",
76 ".ps" => "application/postscript",
77 ".qt" => "video/quicktime",
78 ".ra" => "audio/x-realaudio",
79 ".ram" => "audio/x-pn-realaudio",
80 ".rm" => "audio/x-pn-realaudio",
81 ".rtf" => "application/rtf",
82 ".snd" => "audio/basic",
83 ".tar" => "application/x-tar",
84 ".tex" => "application/x-tex",
85 ".texi" => "application/x-texinfo",
86 ".texinfo" => "application/x-texinfo",
87 ".tgz" => "application/x-compressed",
88 ".txt" => "text/plain",
89 ".wav" => "audio/x-wav",
90 ".wml" => "text/vnd.wap.wml",
91 ".xls" => "application/vnd.ms-excel",
92 ".xml" => "text/xml",
93 ".xpm" => "image/x-xpixmap",
94 ".z" => "application/x-compressed",
95 ".zip" => "application/zip",
96 );
97
98 if (empty($mime_type)) {
99 $ext = strtolower(strrchr(basename($filename), "."));
100 if (isset($mime_map[$ext])) {
101 $mime_type = $mime_map[$ext];
102 }
103 }
104
105 // try to use 'file' to determine mimetype
106 if (empty($mime_type)) {
107 $fp = popen("file -i '$filename' 2>/dev/null", "r");
108 $reply = fgets($fp);
109 pclose($fp);
110
111 // the reply begins with the requested filename
112 if (!strncmp($reply, "$filename: ", strlen($filename)+2)) {
113 $reply = substr($reply, strlen($filename)+2);
114 // followed by the mime type (maybe including options)
115 if (ereg("^([[:alnum:]_-]+/[[:alnum:]_-]+);?.*", $reply, $matches)) {
116 $mime_type = $matches[1];
117 }
118 }
119 }
120
121 // if all else fails, return application/octet-stream
122 if (empty($mime_type)) {
123 $mime_type = "application/octet-stream";
124 }
125
126 return $mime_type;
127}
128
129
130/** Returns the boundary of a MIME multipart content.
131 */
132function get_mime_boundary($filename)
133{
134 $fp = fopen($filename, "rb");
135 $boundary = "";
136 if ($fp && (fscanf($fp, "--%s\r\n", $boundary) == 1) && strlen($boundary))
137 {
138 $expect = "--$boundary--\r\n";
139 fseek($fp, - strlen($expect), SEEK_END);
140 $got = fread($fp, strlen($expect));
141 if ($got != $expect) {
142 $boundary = "";
143 }
144 }
145 fclose($fp);
146 return $boundary;
147}
148
149
150/** Determine whether a given MIME type is multipart.
151 */
152function is_mime_multipart($mimetype)
153{
154 return (preg_match('/^application\/vnd\.oma\.drm\.message$/', $mimetype));
155}
156
157?>