1
/* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
* $Id: types.pike,v 1.1.1.1 2006/03/27 12:40:12 exodusd Exp $
20
constant cvs_version="$Id: types.pike,v 1.1.1.1 2006/03/27 12:40:12 exodusd Exp $";
22
inherit "/kernel/module";
23
inherit "/base/xml_parser";
24
inherit "/base/xml_data";
31
//! This module reads some configuration files located in the
32
//! servers config/ directory and stores information about all
33
//! mime types and what sTeam class to create for a specific mime type.
35
#define MIME_FILE _Server->query_config("config-dir")+"mimetypes.txt"
36
#define CLASS_FILE _Server->query_config("config-dir")+"classtypes.txt"
44
private mapping mMime;
45
private mapping mExts;
46
private array asDefaults = ({ "text/plain", "text/html", "text/wiki", "application/msword", "application/vnd.ms-powerpoint", "image/gif", "image/jpeg", "image/png", "application/pdf" });
50
static void init_module()
56
* Register a new class with a filename to create and a mimetype.
57
* This will change the content of the CLASS_FILE.
59
* @param string cl - the class to register
60
* @param string doc_class - the class to clone
61
* @param string mime - the mimetype
62
* @param string desc - the description for this document class.
63
* @author Thomas Bopp (astra@upb.de)
66
register_class(string cl, string doc_class, string mime, string desc)
68
if ( !_SECURITY->access_register_class(0, 0, CALLER) )
70
mMime[cl] = ({ doc_class, mime, desc });
71
Stdio.File f = Stdio.File(CLASS_FILE, "wct");
72
f->write("<classes>\n");
73
foreach(indices(mMime), string ext) {
74
f->write("\t<type>\n\t\t<ext>"+ext+"</ext>\n\t\t<class>"+
75
mMime[ext][DATA_TYPE]+"</class>\n\t\t<desc>"+
76
mMime[ext][DATA_DESC]+"</desc>\n\t</type>\n");
78
f->write("</classes>\n");
83
* query_document_type, returns an array of document type information
84
* depending on the given filename.
86
* @param filename - the filename
87
* @return the document-type of that file (depends only on extension)
89
* @see query_mime_type
92
query_document_type(string filename)
94
string base_type, file_extension;
99
LOG("query_document_type()");
100
if ( !mappingp(mMime) )
107
ploder = filename / "."; /* explode, hmm */
108
if ( !sizeof(ploder) )
109
return ({ "Document", "gen" });
111
file_extension = ploder[sizeof(ploder)-1];
113
for ( i = strlen(file_extension) - 1; i >= 0; i-- )
114
if ( file_extension[i] >= 'A' && file_extension[i] <= 'Z' )
115
file_extension[i] -= ('A' - 'a');
116
file_extension = lower_case(file_extension);
117
LOG("File Extension:" + file_extension);
118
if ( arrayp(mMime[file_extension]) )
120
return ({ mMime[file_extension][DATA_TYPE],
124
return ({ "Document", file_extension });
128
* Returns the documents extensions for a given mimetype.
130
* @param string mtype - the mimetype
131
* @return array of extensions for this mime type with information
133
array query_document_type_mime(string mtype)
135
if ( arrayp(mExts[mtype]) )
136
return ({ mExts[mtype][DATA_TYPE], "" });
138
return ({ "Document", "" });
142
* Return the array of possible extensions for the given mimetype.
144
* @param string mtype - the mimetype
145
* @return array of extensions for mtype
147
array query_mimetype_extensions(string mtype)
149
array extensions = ({ });
150
if ( !arrayp(mExts[mtype]) )
153
foreach( mExts[mtype], mixed ext ) {
154
extensions += ({ ext[DATA_EXT] });
160
register_extensions(string mtype, string desc, array extensions, void|int def)
164
error("Cannot reregister extension !");
165
mExts[mtype] = extensions;
167
foreach(extensions, string ext) {
169
mMime[ext] = ({ "Document", mtype, desc, i, ext });
171
if ( def && search(asDefaults, mtype) == -1 )
172
asDefaults += ({ mtype });
175
array(string) get_default_mimetypes()
177
return copy_value(asDefaults);
181
* Return the mimetype for a given extension.
183
* @param doc - the document extension
184
* @return the mime-type of the document
185
* @author Thomas Bopp
186
* @see query_document_type
189
query_mime_type(string doc)
191
if ( ! stringp( doc ) )
192
return "application/x-unknown-content-type";
196
if ( !mappingp(mMime) )
199
data = mMime[lower_case(doc)];
201
return "application/x-unknown-content-type";
203
return data[DATA_MIME];
207
* Return the mime description for an extension.
209
* @param doc - the document extension
210
* @return the description of the mime-type
211
* @author Thomas Bopp
212
* @see query_mime_type
215
query_mime_desc(string doc)
219
if ( !mappingp(mMime) )
222
data = mMime[lower_case(doc)];
226
return data[DATA_DESC];
232
* @author Thomas Bopp
238
string type, mimeType, baseType, desc;
240
array(string) tokens;
242
int start, i, j, len;
245
buf = Stdio.read_file(CLASS_FILE);
247
ASSERTINFO(stringp(buf),
248
"Initialization error: class file missing ["+CLASS_FILE+"]\n");
250
//lines = buf / "\n";
252
NodeXML n = parse_data(buf);
253
foreach(n->children, NodeXML type) {
254
NodeXML t = type->get_node("ext");
255
mMime[t->data] = ({ type->get_node("class")->data,
256
"application/x-unknown-content-type",
257
type->get_node("desc")->data, 0 });
260
// read the mimetypes.txt
261
buf = Stdio.read_file(MIME_FILE);
262
if ( !stringp(buf) ) {
263
FATAL("Initialization error: mime file missing! ["+MIME_FILE +"]\n");
268
for ( i = sizeof(lines) - 1; i >= 0; i-- ) {
269
len = strlen(lines[i]);
270
if ( len == 0 || lines[i][0] == '#' )
272
j = 0; start = -1; tokens = ({ });
274
if ( lines[i][j] == ' ' || lines[i][j] == '\t' ) {
276
tokens += ({ lines[i][start..j-1] });
280
else if ( start == -1 ) {
286
tokens += ({ lines[i][start..] });
288
if ( sizeof(tokens) > 1 ) {
289
for ( j = sizeof(tokens) - 1; j >= 1; j-- ) {
290
data = mMime[tokens[j]];
292
if ( arrayp(data) ) {
293
// remember the priority of extensions
294
// html htm wiki, then html should have priority (lowest j)
295
mMime[tokens[j]][DATA_MIME] = tokens[0];
296
mMime[tokens[j]][DATA_PRIO] = j;
300
({ "Document", tokens[0], "Generic", j });
305
foreach ( indices(mMime), string ext ) {
306
// mExts mapping holds mimetype:data
307
if ( arrayp(mExts[mMime[ext][DATA_MIME]]) )
308
mExts[mMime[ext][DATA_MIME]] += ({ mMime[ext]+({ ext }) });
310
mExts[mMime[ext][DATA_MIME]] = ({ mMime[ext]+({ ext }) });
313
foreach ( indices(mExts), string mtype ) {
314
if ( sizeof(mExts[mtype]) > 1 ) {
315
array sorter = ({ });
316
foreach(mExts[mtype], mixed ext)
317
sorter += ({ ext[DATA_PRIO] });
318
sort(mExts[mtype], sorter);
324
* Return the whole mapping of saved types. The mapping contains
325
* file extension: informations (array)
327
* @return the mapping with all type definitions
328
* @author Thomas Bopp
334
return copy_value(mMime);
339
* return the class for a doctype (extension of a file).
341
* @param doc - the doctype (or extension)
343
* @author Thomas Bopp (astra@upb.de)
345
string query_document_class(string doc)
351
return CLASS_NAME_DOCUMENT;
352
LOG("class is " + data[DATA_TYPE]);
353
return data[DATA_TYPE];
357
* Return the document class for the given mimetype.
358
* The class can be found in the classes/ Folder of sTeam.
360
* @param string mtype - the mimetype
361
* @return string of class type used
363
string query_document_class_mime(string mtype)
365
if ( arrayp(mExts[mtype]) )
366
return mExts[mtype][0][DATA_TYPE];
368
return CLASS_NAME_DOCUMENT;
372
string get_identifier() { return "types"; }