Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * Class to manage the custom filetypes list that is stored in a config variable.
19
 *
20
 * @package core
21
 * @copyright 2014 The Open University
22
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
defined('MOODLE_INTERNAL') || die();
26
 
27
require_once($CFG->libdir . '/filelib.php');
28
 
29
/**
30
 * Class to manage the custom filetypes list that is stored in a config variable.
31
 *
32
 * @copyright 2014 The Open University
33
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
abstract class core_filetypes {
36
    /** @var array Cached MIME types for current request */
37
    protected static $cachedtypes;
38
 
39
    /**
40
     * Gets default MIME types that are included as standard.
41
     *
42
     * Note: Use the function get_mimetypes_array to access this data including
43
     * any customisations the user might have made.
44
     *
45
     * @return array Default (pre-installed) MIME type information
46
     */
47
    protected static function get_default_types() {
48
        return array(
49
            'xxx' => array('type' => 'document/unknown', 'icon' => 'unknown'),
50
            '3gp' => array('type' => 'video/quicktime', 'icon' => 'video', 'groups' => array('video'), 'string' => 'video'),
51
            '7z' => array('type' => 'application/x-7z-compressed', 'icon' => 'archive',
52
                    'groups' => array('archive'), 'string' => 'archive'),
53
            'aac' => array('type' => 'audio/aac', 'icon' => 'audio', 'groups' => array('audio', 'html_audio', 'web_audio'),
54
                    'string' => 'audio'),
55
            'accdb' => array('type' => 'application/msaccess', 'icon' => 'database'),
56
            'ai' => array('type' => 'application/postscript', 'icon' => 'eps', 'groups' => array('image'), 'string' => 'image'),
57
            'aif' => array('type' => 'audio/x-aiff', 'icon' => 'audio', 'groups' => array('audio'), 'string' => 'audio'),
58
            'aiff' => array('type' => 'audio/x-aiff', 'icon' => 'audio', 'groups' => array('audio'), 'string' => 'audio'),
59
            'aifc' => array('type' => 'audio/x-aiff', 'icon' => 'audio', 'groups' => array('audio'), 'string' => 'audio'),
60
            'applescript' => array('type' => 'text/plain', 'icon' => 'text'),
61
            'asc' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
62
            'asm' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
63
            'au' => array('type' => 'audio/au', 'icon' => 'audio', 'groups' => array('audio'), 'string' => 'audio'),
64
            'avi' => array('type' => 'video/x-ms-wm', 'icon' => 'video',
65
                    'groups' => array('video', 'web_video'), 'string' => 'video'),
66
            'bmp' => array('type' => 'image/bmp', 'icon' => 'image', 'groups' => array('image'), 'string' => 'image'),
67
            'c' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
68
            'cct' => array('type' => 'shockwave/director', 'icon' => 'flash'),
69
            'cpp' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
70
            'cs' => array('type' => 'application/x-csh', 'icon' => 'sourcecode'),
71
            'css' => array('type' => 'text/css', 'icon' => 'json', 'groups' => array('web_file')),
72
            'csv' => array('type' => 'text/csv', 'icon' => 'spreadsheet', 'groups' => array('spreadsheet')),
73
            'dv' => array('type' => 'video/x-dv', 'icon' => 'video', 'groups' => array('video'), 'string' => 'video'),
74
            'dmg' => array('type' => 'application/octet-stream', 'icon' => 'unknown'),
75
 
76
            'doc' => array('type' => 'application/msword', 'icon' => 'document', 'groups' => array('document')),
77
            'bdoc' => array('type' => 'application/x-digidoc', 'icon' => 'document', 'groups' => array('archive')),
78
            'cdoc' => array('type' => 'application/x-digidoc', 'icon' => 'document', 'groups' => array('archive')),
79
            'ddoc' => array('type' => 'application/x-digidoc', 'icon' => 'document', 'groups' => array('archive')),
80
            'docx' => array('type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
81
                    'icon' => 'document', 'groups' => array('document')),
82
            'docm' => array('type' => 'application/vnd.ms-word.document.macroEnabled.12', 'icon' => 'document'),
83
            'dotx' => array('type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
84
                    'icon' => 'document'),
85
            'dotm' => array('type' => 'application/vnd.ms-word.template.macroEnabled.12', 'icon' => 'document'),
86
 
87
            'dcr' => array('type' => 'application/x-director', 'icon' => 'flash'),
88
            'dif' => array('type' => 'video/x-dv', 'icon' => 'video', 'groups' => array('video'), 'string' => 'video'),
89
            'dir' => array('type' => 'application/x-director', 'icon' => 'flash'),
90
            'dxr' => array('type' => 'application/x-director', 'icon' => 'flash'),
91
            'eps' => array('type' => 'application/postscript', 'icon' => 'eps'),
92
            'epub' => array('type' => 'application/epub+zip', 'icon' => 'epub', 'groups' => array('document')),
93
            'fdf' => array('type' => 'application/vnd.fdf', 'icon' => 'pdf'),
94
            'flac' => array('type' => 'audio/flac', 'icon' => 'audio', 'groups' => array('audio', 'html_audio', 'web_audio'),
95
                    'string' => 'audio'),
96
            'flv' => array('type' => 'video/x-flv', 'icon' => 'flash',
97
                    'groups' => array('video', 'web_video'), 'string' => 'video'),
98
            'f4v' => array('type' => 'video/mp4', 'icon' => 'flash', 'groups' => array('video', 'web_video'), 'string' => 'video'),
99
            'fmp4' => array('type' => 'video/mp4', 'icon' => 'video', 'groups' => array('html_video', 'video', 'web_video'),
100
                    'string' => 'video'),
101
            'gallery' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
102
            'galleryitem' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
103
            'gallerycollection' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
104
            'gdraw' => array('type' => 'application/vnd.google-apps.drawing', 'icon' => 'image', 'groups' => array('image')),
105
            'gdoc' => array('type' => 'application/vnd.google-apps.document', 'icon' => 'document', 'groups' => array('document')),
106
            'gsheet' => array('type' => 'application/vnd.google-apps.spreadsheet', 'icon' => 'spreadsheet',
107
                    'groups' => array('spreadsheet')),
108
            'gslides' => array('type' => 'application/vnd.google-apps.presentation', 'icon' => 'powerpoint',
109
                    'groups' => array('presentation')),
110
            'gif' => array('type' => 'image/gif', 'icon' => 'gif', 'groups' => array('image', 'web_image', 'optimised_image'),
111
                'string' => 'image'),
112
            'gtar' => array('type' => 'application/x-gtar', 'icon' => 'archive',
113
                    'groups' => array('archive'), 'string' => 'archive'),
114
            'tgz' => array('type' => 'application/g-zip', 'icon' => 'archive', 'groups' => array('archive'), 'string' => 'archive'),
115
            'gz' => array('type' => 'application/g-zip', 'icon' => 'archive', 'groups' => array('archive'), 'string' => 'archive'),
116
            'gzip' => array('type' => 'application/g-zip', 'icon' => 'archive',
117
                    'groups' => array('archive'), 'string' => 'archive'),
118
            'h' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
119
            'h5p' => array('type' => 'application/zip.h5p', 'icon' => 'h5p', 'string' => 'archive'),
120
            'hpp' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
121
            'hqx' => array('type' => 'application/mac-binhex40', 'icon' => 'archive',
122
                    'groups' => array('archive'), 'string' => 'archive'),
123
            'htc' => array('type' => 'text/x-component', 'icon' => 'markup'),
124
            'html' => array('type' => 'text/html', 'icon' => 'markup', 'groups' => array('web_file')),
125
            'xhtml' => array('type' => 'application/xhtml+xml', 'icon' => 'markup', 'groups' => array('web_file')),
126
            'htm' => array('type' => 'text/html', 'icon' => 'markup', 'groups' => array('web_file')),
127
            'ico' => array('type' => 'image/vnd.microsoft.icon', 'icon' => 'image',
128
                    'groups' => array('image'), 'string' => 'image'),
129
            'ics' => array('type' => 'text/calendar', 'icon' => 'text'),
130
            'imscc' => array('type' => 'application/zip', 'icon' => 'archive', 'string' => 'archive'),
131
            'isf' => array('type' => 'application/inspiration', 'icon' => 'isf'),
132
            'ist' => array('type' => 'application/inspiration.template', 'icon' => 'isf'),
133
            'java' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
134
            'jar' => array('type' => 'application/java-archive', 'icon' => 'archive'),
135
            'jcb' => array('type' => 'text/xml', 'icon' => 'markup'),
136
            'jcl' => array('type' => 'text/xml', 'icon' => 'markup'),
137
            'jcw' => array('type' => 'text/xml', 'icon' => 'markup'),
138
            'jmt' => array('type' => 'text/xml', 'icon' => 'markup'),
139
            'jmx' => array('type' => 'text/xml', 'icon' => 'markup'),
140
            'jnlp' => array('type' => 'application/x-java-jnlp-file', 'icon' => 'markup'),
141
            'jpe' => array('type' => 'image/jpeg', 'icon' => 'image', 'groups' => array('image', 'web_image', 'optimised_image'),
142
                'string' => 'image'),
143
            'jpeg' => array('type' => 'image/jpeg', 'icon' => 'image', 'groups' => array('image', 'web_image', 'optimised_image'),
144
                'string' => 'image'),
145
            'jpg' => array('type' => 'image/jpeg', 'icon' => 'image', 'groups' => array('image', 'web_image', 'optimised_image'),
146
                'string' => 'image'),
147
            'jqz' => array('type' => 'text/xml', 'icon' => 'markup'),
148
            'js' => array('type' => 'application/x-javascript', 'icon' => 'text', 'groups' => array('web_file')),
149
            'json' => array('type' => 'application/json', 'icon' => 'json'),
150
            'latex' => array('type' => 'application/x-latex', 'icon' => 'text'),
151
            'm' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
152
            'mbz' => array('type' => 'application/vnd.moodle.backup', 'icon' => 'moodle'),
153
            'mdb' => array('type' => 'application/x-msaccess', 'icon' => 'database'),
154
            'mht' => array('type' => 'message/rfc822', 'icon' => 'archive'),
155
            'mhtml' => array('type' => 'message/rfc822', 'icon' => 'archive'),
156
            'mov' => array('type' => 'video/quicktime', 'icon' => 'video',
157
                    'groups' => array('video', 'web_video', 'html_video'), 'string' => 'video'),
158
            'movie' => array('type' => 'video/x-sgi-movie', 'icon' => 'video', 'groups' => array('video'), 'string' => 'video'),
159
            'mw' => array('type' => 'application/maple', 'icon' => 'math'),
160
            'mws' => array('type' => 'application/maple', 'icon' => 'math'),
161
            'm3u' => array('type' => 'audio/x-mpegurl', 'icon' => 'audio', 'groups' => array('audio'), 'string' => 'audio'),
162
            'm3u8' => array('type' => 'application/x-mpegURL', 'icon' => 'video', 'groups' => array('media_source')),
163
            'mp3' => array('type' => 'audio/mp3', 'icon' => 'audio', 'groups' => array('audio', 'html_audio', 'web_audio'),
164
                    'string' => 'audio'),
165
            'mp4' => array('type' => 'video/mp4', 'icon' => 'video', 'groups' => array('html_video', 'video', 'web_video'),
166
                    'string' => 'video'),
167
            'm4v' => array('type' => 'video/mp4', 'icon' => 'video', 'groups' => array('html_video', 'video', 'web_video'),
168
                    'string' => 'video'),
169
            'm4a' => array('type' => 'audio/mp4', 'icon' => 'audio', 'groups' => array('audio', 'html_audio', 'web_audio'),
170
                    'string' => 'audio'),
171
            'mpeg' => array('type' => 'video/mpeg', 'icon' => 'video', 'groups' => array('video', 'web_video'),
172
                    'string' => 'video'),
173
            'mpd' => array('type' => 'application/dash+xml', 'icon' => 'video', 'groups' => array('media_source')),
174
            'mpe' => array('type' => 'video/mpeg', 'icon' => 'video', 'groups' => array('video', 'web_video'),
175
                    'string' => 'video'),
176
            'mpg' => array('type' => 'video/mpeg', 'icon' => 'video', 'groups' => array('video', 'web_video'),
177
                    'string' => 'video'),
178
            'mpr' => array('type' => 'application/vnd.moodle.profiling', 'icon' => 'moodle'),
179
 
180
            'nbk' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
181
            'notebook' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
182
 
183
            'odt' => array('type' => 'application/vnd.oasis.opendocument.text', 'icon' => 'writer', 'groups' => array('document')),
184
            'ott' => array('type' => 'application/vnd.oasis.opendocument.text-template',
185
                    'icon' => 'writer', 'groups' => array('document')),
186
            'oth' => array('type' => 'application/vnd.oasis.opendocument.text-web', 'icon' => 'oth', 'groups' => array('document')),
187
            'odm' => array('type' => 'application/vnd.oasis.opendocument.text-master', 'icon' => 'writer'),
188
            'odg' => array('type' => 'application/vnd.oasis.opendocument.graphics', 'icon' => 'draw'),
189
            'otg' => array('type' => 'application/vnd.oasis.opendocument.graphics-template', 'icon' => 'draw'),
190
            'odp' => array('type' => 'application/vnd.oasis.opendocument.presentation', 'icon' => 'impress',
191
                    'groups' => array('presentation')),
192
            'otp' => array('type' => 'application/vnd.oasis.opendocument.presentation-template', 'icon' => 'impress',
193
                    'groups' => array('presentation')),
194
            'ods' => array('type' => 'application/vnd.oasis.opendocument.spreadsheet',
195
                    'icon' => 'calc', 'groups' => array('spreadsheet')),
196
            'ots' => array('type' => 'application/vnd.oasis.opendocument.spreadsheet-template',
197
                    'icon' => 'calc', 'groups' => array('spreadsheet')),
198
            'odc' => array('type' => 'application/vnd.oasis.opendocument.chart', 'icon' => 'chart'),
199
            'odf' => array('type' => 'application/vnd.oasis.opendocument.formula', 'icon' => 'math'),
200
            'odb' => array('type' => 'application/vnd.oasis.opendocument.database', 'icon' => 'database'),
201
            'odi' => array('type' => 'application/vnd.oasis.opendocument.image', 'icon' => 'draw'),
202
            'oga' => array('type' => 'audio/ogg', 'icon' => 'audio', 'groups' => array('audio', 'html_audio', 'web_audio'),
203
                    'string' => 'audio'),
204
            'ogg' => array('type' => 'audio/ogg', 'icon' => 'audio', 'groups' => array('audio', 'html_audio', 'web_audio'),
205
                    'string' => 'audio'),
206
            'ogv' => array('type' => 'video/ogg', 'icon' => 'video', 'groups' => array('html_video', 'video', 'web_video'),
207
                    'string' => 'video'),
208
 
209
            'pct' => array('type' => 'image/pict', 'icon' => 'image', 'groups' => array('image'), 'string' => 'image'),
210
            'pdf' => array('type' => 'application/pdf', 'icon' => 'pdf', 'groups' => array('document')),
211
            'php' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
212
            'pic' => array('type' => 'image/pict', 'icon' => 'image', 'groups' => array('image'), 'string' => 'image'),
213
            'pict' => array('type' => 'image/pict', 'icon' => 'image', 'groups' => array('image'), 'string' => 'image'),
214
            'png' => array('type' => 'image/png', 'icon' => 'image', 'groups' => array('image', 'web_image', 'optimised_image'),
215
                'string' => 'image'),
216
            'pps' => array('type' => 'application/vnd.ms-powerpoint', 'icon' => 'powerpoint', 'groups' => array('presentation')),
217
            'ppt' => array('type' => 'application/vnd.ms-powerpoint', 'icon' => 'powerpoint', 'groups' => array('presentation')),
218
            'pptx' => array('type' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
219
                    'icon' => 'powerpoint', 'groups' => array('presentation')),
220
            'pptm' => array('type' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12', 'icon' => 'powerpoint',
221
                    'groups' => array('presentation')),
222
            'potx' => array('type' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
223
                    'icon' => 'powerpoint', 'groups' => array('presentation')),
224
            'potm' => array('type' => 'application/vnd.ms-powerpoint.template.macroEnabled.12', 'icon' => 'powerpoint',
225
                    'groups' => array('presentation')),
226
            'ppam' => array('type' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12', 'icon' => 'powerpoint',
227
                    'groups' => array('presentation')),
228
            'ppsx' => array('type' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
229
                    'icon' => 'powerpoint', 'groups' => array('presentation')),
230
            'ppsm' => array('type' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12', 'icon' => 'powerpoint',
231
                    'groups' => array('presentation')),
232
            'ps' => array('type' => 'application/postscript', 'icon' => 'pdf'),
233
            'psd' => array('type' => 'image/vnd.adobe.photoshop', 'icon' => 'psd'),
234
            'pub' => array('type' => 'application/x-mspublisher', 'icon' => 'publisher', 'groups' => array('presentation')),
235
 
236
            'qt' => array('type' => 'video/quicktime', 'icon' => 'video',
237
                    'groups' => array('video', 'web_video'), 'string' => 'video'),
238
            'ra' => array('type' => 'audio/x-realaudio-plugin', 'icon' => 'audio',
239
                    'groups' => array('audio', 'web_audio'), 'string' => 'audio'),
240
            'ram' => array('type' => 'audio/x-pn-realaudio-plugin', 'icon' => 'audio',
241
                    'groups' => array('audio'), 'string' => 'audio'),
242
            'rar' => array('type' => 'application/x-rar-compressed', 'icon' => 'archive',
243
                    'groups' => array('archive'), 'string' => 'archive'),
244
            'rhb' => array('type' => 'text/xml', 'icon' => 'markup'),
245
            'rm' => array('type' => 'audio/x-pn-realaudio-plugin', 'icon' => 'audio',
246
                    'groups' => array('audio'), 'string' => 'audio'),
247
            'rmvb' => array('type' => 'application/vnd.rn-realmedia-vbr', 'icon' => 'video',
248
                    'groups' => array('video'), 'string' => 'video'),
249
            'rtf' => array('type' => 'text/rtf', 'icon' => 'text', 'groups' => array('document')),
250
            'rtx' => array('type' => 'text/richtext', 'icon' => 'text'),
251
            'rv' => array('type' => 'audio/x-pn-realaudio-plugin', 'icon' => 'audio',
252
                    'groups' => array('video'), 'string' => 'video'),
253
            'scss' => array('type' => 'text/x-scss', 'icon' => 'json', 'groups' => array('web_file')),
254
            'sh' => array('type' => 'application/x-sh', 'icon' => 'sourcecode'),
255
            'sit' => array('type' => 'application/x-stuffit', 'icon' => 'archive',
256
                    'groups' => array('archive'), 'string' => 'archive'),
257
            'smi' => array('type' => 'application/smil', 'icon' => 'text'),
258
            'smil' => array('type' => 'application/smil', 'icon' => 'text'),
259
            'sqt' => array('type' => 'text/xml', 'icon' => 'markup'),
260
            'svg' => array('type' => 'image/svg+xml', 'icon' => 'image',
261
                    'groups' => array('image', 'web_image'), 'string' => 'image'),
262
            'svgz' => array('type' => 'image/svg+xml', 'icon' => 'image',
263
                    'groups' => array('image', 'web_image'), 'string' => 'image'),
264
            'swa' => array('type' => 'application/x-director', 'icon' => 'flash'),
265
            'swf' => array('type' => 'application/x-shockwave-flash', 'icon' => 'flash'),
266
            'swfl' => array('type' => 'application/x-shockwave-flash', 'icon' => 'flash'),
267
 
268
            'sxw' => array('type' => 'application/vnd.sun.xml.writer', 'icon' => 'writer'),
269
            'stw' => array('type' => 'application/vnd.sun.xml.writer.template', 'icon' => 'writer'),
270
            'sxc' => array('type' => 'application/vnd.sun.xml.calc', 'icon' => 'calc'),
271
            'stc' => array('type' => 'application/vnd.sun.xml.calc.template', 'icon' => 'calc'),
272
            'sxd' => array('type' => 'application/vnd.sun.xml.draw', 'icon' => 'draw'),
273
            'std' => array('type' => 'application/vnd.sun.xml.draw.template', 'icon' => 'draw'),
274
            'sxi' => array('type' => 'application/vnd.sun.xml.impress', 'icon' => 'impress', 'groups' => array('presentation')),
275
            'sti' => array('type' => 'application/vnd.sun.xml.impress.template', 'icon' => 'impress',
276
                    'groups' => array('presentation')),
277
            'sxg' => array('type' => 'application/vnd.sun.xml.writer.global', 'icon' => 'writer'),
278
            'sxm' => array('type' => 'application/vnd.sun.xml.math', 'icon' => 'math'),
279
 
280
            'tar' => array('type' => 'application/x-tar', 'icon' => 'archive', 'groups' => array('archive'), 'string' => 'archive'),
281
            'tif' => array('type' => 'image/tiff', 'icon' => 'image', 'groups' => array('image'), 'string' => 'image'),
282
            'tiff' => array('type' => 'image/tiff', 'icon' => 'image', 'groups' => array('image'), 'string' => 'image'),
283
            'tex' => array('type' => 'application/x-tex', 'icon' => 'text'),
284
            'texi' => array('type' => 'application/x-texinfo', 'icon' => 'text'),
285
            'texinfo' => array('type' => 'application/x-texinfo', 'icon' => 'text'),
286
            'ts' => array('type' => 'video/MP2T', 'icon' => 'video', 'groups' => array('video', 'web_video'),
287
                    'string' => 'video'),
288
            'tsv' => array('type' => 'text/tab-separated-values', 'icon' => 'text'),
289
            'txt' => array('type' => 'text/plain', 'icon' => 'text', 'defaulticon' => true),
290
            'vtt' => array('type' => 'text/vtt', 'icon' => 'text', 'groups' => array('html_track')),
291
            'wav' => array('type' => 'audio/wav', 'icon' => 'audio', 'groups' => array('audio', 'html_audio', 'web_audio'),
292
                    'string' => 'audio'),
293
            'webm' => array('type' => 'video/webm', 'icon' => 'video', 'groups' => array('html_video', 'video', 'web_video'),
294
                    'string' => 'video'),
1441 ariadna 295
            'webp' => ['type' => 'image/webp', 'icon' => 'image', 'groups' => ['image', 'web_image', 'optimised_image'],
296
                'string' => 'image'],
1 efrain 297
            'wmv' => array('type' => 'video/x-ms-wmv', 'icon' => 'video', 'groups' => array('video'), 'string' => 'video'),
298
            'asf' => array('type' => 'video/x-ms-asf', 'icon' => 'video', 'groups' => array('video'), 'string' => 'video'),
299
            'wma' => array('type' => 'audio/x-ms-wma', 'icon' => 'audio', 'groups' => array('audio'), 'string' => 'audio'),
300
 
301
            'xbk' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
302
            'xdp' => array('type' => 'application/vnd.adobe.xdp+xml', 'icon' => 'pdf'),
303
            'xfd' => array('type' => 'application/vnd.xfdl', 'icon' => 'pdf'),
304
            'xfdf' => array('type' => 'application/vnd.adobe.xfdf', 'icon' => 'pdf'),
305
 
306
            'xls' => array('type' => 'application/vnd.ms-excel', 'icon' => 'spreadsheet', 'groups' => array('spreadsheet')),
307
            'xlsx' => array('type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'icon' => 'spreadsheet',
308
                'groups' => array('spreadsheet')),
309
            'xlsm' => array('type' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
310
                    'icon' => 'spreadsheet', 'groups' => array('spreadsheet')),
311
            'xltx' => array('type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
312
                    'icon' => 'spreadsheet'),
313
            'xltm' => array('type' => 'application/vnd.ms-excel.template.macroEnabled.12', 'icon' => 'spreadsheet'),
314
            'xlsb' => array('type' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', 'icon' => 'spreadsheet'),
315
            'xlam' => array('type' => 'application/vnd.ms-excel.addin.macroEnabled.12', 'icon' => 'spreadsheet'),
316
 
317
            'xml' => array('type' => 'application/xml', 'icon' => 'markup'),
318
            'xsl' => array('type' => 'text/xml', 'icon' => 'markup'),
319
 
320
            'yaml' => array('type' => 'application/yaml', 'icon' => 'markup'),
321
            'yml' => array('type' => 'application/yaml', 'icon' => 'markup'),
322
 
323
            'zip' => array('type' => 'application/zip', 'icon' => 'archive', 'groups' => array('archive'), 'string' => 'archive')
324
        );
325
    }
326
 
327
    /**
328
     * Given a mimetype - return a valid file extension for it.
329
     *
330
     * @param $mimetype string
331
     * @return string|bool False if the mimetype was not known, a string indicating a valid file extension otherwise. It may not
332
     *                     be the only valid file extension - just the first one found.
333
     */
334
    public static function get_file_extension($mimetype) {
335
        $types = self::get_types();
336
        foreach ($types as $extension => $info) {
337
            if ($info['type'] == $mimetype) {
338
                return $extension;
339
            }
340
        }
341
        return false;
342
    }
343
 
344
    /**
345
     * Gets all the current types.
346
     *
347
     * @return array Associative array from extension to array of data about type
348
     */
349
    public static function &get_types() {
350
        // If it was already done in this request, use cache.
351
        if (self::$cachedtypes) {
352
            return self::$cachedtypes;
353
        }
354
 
355
        // Get defaults.
356
        $mimetypes = self::get_default_types();
357
 
358
        // Get custom file types.
359
        $custom = self::get_custom_types();
360
 
361
        // Check value is an array.
362
        if (!is_array($custom)) {
363
            debugging('Invalid $CFG->customfiletypes (not array)', DEBUG_DEVELOPER);
364
            $custom = array();
365
        }
366
 
367
        foreach ($custom as $customentry) {
368
            // Each entry is a stdClass object similar to the array values above.
369
            if (empty($customentry->extension)) {
370
                debugging('Invalid $CFG->customfiletypes entry (extension field required)',
371
                        DEBUG_DEVELOPER);
372
                continue;
373
            }
374
 
375
            // To delete a standard entry, set 'deleted' to true.
376
            if (!empty($customentry->deleted)) {
377
                unset($mimetypes[$customentry->extension]);
378
                continue;
379
            }
380
 
381
            // Check required fields.
382
            if (empty($customentry->type) || empty($customentry->icon)) {
383
                debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
384
                        ' (type and icon fields required)', DEBUG_DEVELOPER);
385
                continue;
386
            }
387
 
388
            // Build result array.
389
            $result = array('type' => $customentry->type, 'icon' => $customentry->icon);
390
            if (!empty($customentry->groups)) {
391
                if (!is_array($customentry->groups)) {
392
                    debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
393
                            ' (groups field not array)', DEBUG_DEVELOPER);
394
                    continue;
395
                }
396
                $result['groups'] = $customentry->groups;
397
            }
398
            if (!empty($customentry->string)) {
399
                if (!is_string($customentry->string)) {
400
                    debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
401
                            ' (string field not string)', DEBUG_DEVELOPER);
402
                    continue;
403
                }
404
                $result['string'] = $customentry->string;
405
            }
406
            if (!empty($customentry->defaulticon)) {
407
                if (!is_bool($customentry->defaulticon)) {
408
                    debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
409
                            ' (defaulticon field not bool)', DEBUG_DEVELOPER);
410
                    continue;
411
                }
412
                $result['defaulticon'] = $customentry->defaulticon;
413
            }
414
            if (!empty($customentry->customdescription)) {
415
                if (!is_string($customentry->customdescription)) {
416
                    debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
417
                            ' (customdescription field not string)', DEBUG_DEVELOPER);
418
                    continue;
419
                }
420
                // As the name suggests, this field is used only for custom entries.
421
                $result['customdescription'] = $customentry->customdescription;
422
            }
423
 
424
            // Track whether it is a custom filetype or a modified existing
425
            // filetype.
426
            if (array_key_exists($customentry->extension, $mimetypes)) {
427
                $result['modified'] = true;
428
            } else {
429
                $result['custom'] = true;
430
            }
431
 
432
            // Add result array to list.
433
            $mimetypes[$customentry->extension] = $result;
434
        }
435
 
436
        self::$cachedtypes = $mimetypes;
437
        return self::$cachedtypes;
438
    }
439
 
440
    /**
441
     * Gets custom types from config variable, after decoding the JSON if required.
442
     *
443
     * @return array Array of custom types (empty array if none)
444
     */
445
    protected static function get_custom_types() {
446
        global $CFG;
447
        if (!empty($CFG->customfiletypes)) {
448
            if (is_array($CFG->customfiletypes)) {
449
                // You can define this as an array in config.php...
450
                return $CFG->customfiletypes;
451
            } else {
452
                // Or as a JSON string in the config table.
453
                return json_decode($CFG->customfiletypes);
454
            }
455
        } else {
456
            return array();
457
        }
458
    }
459
 
460
    /**
461
     * Sets the custom types into config variable, encoding into JSON.
462
     *
463
     * @param array $types Array of custom types
464
     * @throws coding_exception If the custom types are fixed in config.php.
465
     */
466
    protected static function set_custom_types(array $types) {
467
        global $CFG;
468
        // Check the setting hasn't been forced.
469
        if (array_key_exists('customfiletypes', $CFG->config_php_settings)) {
470
            throw new coding_exception('Cannot set custom filetypes because they ' .
471
                    'are defined in config.php');
472
        }
473
        if (empty($types)) {
474
            unset_config('customfiletypes');
475
        } else {
476
            set_config('customfiletypes', json_encode(array_values($types)));
477
        }
478
 
479
        // Clear the cached type list.
480
        self::reset_caches();
481
    }
482
 
483
    /**
484
     * Clears the type cache. This is not needed in normal use as the
485
     * set_custom_types function automatically clears the cache. Intended for
486
     * use in unit tests.
487
     */
488
    public static function reset_caches() {
489
        self::$cachedtypes = null;
490
    }
491
 
492
    /**
493
     * Gets the default types that have been deleted. Returns an array containing
494
     * the defaults of all those types.
495
     *
496
     * @return array Array (same format as get_mimetypes_array)
497
     */
498
    public static function get_deleted_types() {
499
        $defaults = self::get_default_types();
500
        $deleted = array();
501
        foreach (self::get_custom_types() as $customentry) {
502
            if (!empty($customentry->deleted)) {
503
                $deleted[$customentry->extension] = $defaults[$customentry->extension];
504
            }
505
        }
506
        return $deleted;
507
    }
508
 
509
    /**
510
     * Adds a new entry to the list of custom filetypes.
511
     *
512
     * @param string $extension File extension without dot, e.g. 'doc'
513
     * @param string $mimetype MIME type e.g. 'application/msword'
514
     * @param string $coreicon Core icon to use e.g. 'document'
515
     * @param array $groups Array of group strings that this type belongs to
516
     * @param string $corestring Custom lang string name in mimetypes.php
517
     * @param string $customdescription Custom description (plain text/multilang)
518
     * @param bool $defaulticon True if this should be the default icon for the type
519
     * @throws coding_exception If the extension already exists, or otherwise invalid
520
     */
521
    public static function add_type($extension, $mimetype, $coreicon,
522
            array $groups = array(), $corestring = '', $customdescription = '',
523
            $defaulticon = false) {
524
        // Check for blank extensions or incorrectly including the dot.
525
        $extension = (string)$extension;
526
        if ($extension === '' || $extension[0] === '.') {
527
            throw new coding_exception('Invalid extension .' . $extension);
528
        }
529
 
530
        // Check extension not already used.
531
        $mimetypes = get_mimetypes_array();
532
        if (array_key_exists($extension, $mimetypes)) {
533
            throw new coding_exception('Extension ' . $extension . ' already exists');
534
        }
535
 
536
        // For default icon, check there isn't already something with default icon
537
        // set for that MIME type.
538
        if ($defaulticon) {
539
            foreach ($mimetypes as $type) {
540
                if ($type['type'] === $mimetype && !empty($type['defaulticon'])) {
541
                    throw new coding_exception('MIME type ' . $mimetype .
542
                            ' already has a default icon set');
543
                }
544
            }
545
        }
546
 
547
        // Get existing custom filetype list.
548
        $customs = self::get_custom_types();
549
 
550
        // Check if there's a 'deleted' entry for the extension, if so then get
551
        // rid of it.
552
        foreach ($customs as $key => $custom) {
553
            if ($custom->extension === $extension) {
554
                unset($customs[$key]);
555
            }
556
        }
557
 
558
        // Set up config record for new type.
559
        $newtype = self::create_config_record($extension, $mimetype, $coreicon, $groups,
560
                $corestring, $customdescription, $defaulticon);
561
 
562
        // See if there's a default value with this extension.
563
        $needsadding = true;
564
        $defaults = self::get_default_types();
565
        if (array_key_exists($extension, $defaults)) {
566
            // If it has the same values, we don't need to add it.
567
            $defaultvalue = $defaults[$extension];
568
            $modified = (array)$newtype;
569
            unset($modified['extension']);
570
            ksort($defaultvalue);
571
            ksort($modified);
572
            if ($modified === $defaultvalue) {
573
                $needsadding = false;
574
            }
575
        }
576
 
577
        // Add to array and set in config.
578
        if ($needsadding) {
579
            $customs[] = $newtype;
580
        }
581
        self::set_custom_types($customs);
582
    }
583
 
584
    /**
585
     * Updates an entry in the list of filetypes in config.
586
     *
587
     * @param string $extension File extension without dot, e.g. 'doc'
588
     * @param string $newextension New file extension (same if not changing)
589
     * @param string $mimetype MIME type e.g. 'application/msword'
590
     * @param string $coreicon Core icon to use e.g. 'document'
591
     * @param array $groups Array of group strings that this type belongs to
592
     * @param string $corestring Custom lang string name in mimetypes.php
593
     * @param string $customdescription Custom description (plain text/multilang)
594
     * @param bool $defaulticon True if this should be the default icon for the type
595
     * @throws coding_exception If the new extension already exists, or otherwise invalid
596
     */
597
    public static function update_type($extension, $newextension, $mimetype, $coreicon,
598
            array $groups = array(), $corestring = '', $customdescription = '',
599
            $defaulticon = false) {
600
 
601
        // Extension must exist.
602
        $extension = (string)$extension;
603
        $mimetypes = get_mimetypes_array();
604
        if (!array_key_exists($extension, $mimetypes)) {
605
            throw new coding_exception('Extension ' . $extension . ' not found');
606
        }
607
 
608
        // If there's a new extension then this must not exist.
609
        $newextension = (string)$newextension;
610
        if ($newextension !== $extension) {
611
            if ($newextension === '' || $newextension[0] === '.') {
612
                throw new coding_exception('Invalid extension .' . $newextension);
613
            }
614
            if (array_key_exists($newextension, $mimetypes)) {
615
                throw new coding_exception('Extension ' . $newextension . ' already exists');
616
            }
617
        }
618
 
619
        // For default icon, check there isn't already something with default icon
620
        // set for that MIME type (unless it's this).
621
        if ($defaulticon) {
622
            foreach ($mimetypes as $ext => $type) {
623
                if ($ext !== $extension && $type['type'] === $mimetype &&
624
                        !empty($type['defaulticon'])) {
625
                    throw new coding_exception('MIME type ' . $mimetype .
626
                            ' already has a default icon set');
627
                }
628
            }
629
        }
630
 
631
        // Delete the old extension and then add the new one (may be same). This
632
        // will correctly handle cases when a default type is involved.
633
        self::delete_type($extension);
634
        self::add_type($newextension, $mimetype, $coreicon, $groups, $corestring,
635
                $customdescription, $defaulticon);
636
    }
637
 
638
    /**
639
     * Deletes a file type from the config list (or, for a standard one, marks it
640
     * as deleted).
641
     *
642
     * @param string $extension File extension without dot, e.g. 'doc'
643
     * @throws coding_exception If the extension does not exist, or otherwise invalid
644
     */
645
    public static function delete_type($extension) {
646
        // Extension must exist.
647
        $mimetypes = get_mimetypes_array();
648
        if (!array_key_exists($extension, $mimetypes)) {
649
            throw new coding_exception('Extension ' . $extension . ' not found');
650
        }
651
 
652
        // Get existing custom filetype list.
653
        $customs = self::get_custom_types();
654
 
655
        // Remove any entries for this extension.
656
        foreach ($customs as $key => $custom) {
657
            if ($custom->extension === $extension && empty($custom->deleted)) {
658
                unset($customs[$key]);
659
            }
660
        }
661
 
662
        // If it was a standard entry (doesn't have 'custom' set) then add a
663
        // deleted marker.
664
        if (empty($mimetypes[$extension]['custom'])) {
665
            $customs[] = (object)array('extension' => $extension, 'deleted' => true);
666
        }
667
 
668
        // Save and reset cache.
669
        self::set_custom_types($customs);
670
    }
671
 
672
    /**
673
     * Reverts a file type to the default. May only be called on types that have
674
     * default values. This will undelete the type if necessary or set its values.
675
     * If the type is already at default values, does nothing.
676
     *
677
     * @param string $extension File extension without dot, e.g. 'doc'
678
     * @return bool True if anything was changed, false if it was already default
679
     * @throws coding_exception If the extension is not a default type.
680
     */
681
    public static function revert_type_to_default($extension) {
682
        $extension = (string)$extension;
683
 
684
        // Check it actually is a default type.
685
        $defaults = self::get_default_types();
686
        if (!array_key_exists($extension, $defaults)) {
687
            throw new coding_exception('Extension ' . $extension . ' is not a default type');
688
        }
689
 
690
        // Loop through all the custom settings.
691
        $changed = false;
692
        $customs = self::get_custom_types();
693
        foreach ($customs as $key => $customentry) {
694
            if ($customentry->extension === $extension) {
695
                unset($customs[$key]);
696
                $changed = true;
697
            }
698
        }
699
 
700
        // Save changes if any.
701
        if ($changed) {
702
            self::set_custom_types($customs);
703
        }
704
        return $changed;
705
    }
706
 
707
    /**
708
     * Converts function parameters into a record for storing in the JSON value.
709
     *
710
     * @param string $extension File extension without dot, e.g. 'doc'
711
     * @param string $mimetype MIME type e.g. 'application/msword'
712
     * @param string $coreicon Core icon to use e.g. 'document'
713
     * @param array $groups Array of group strings that this type belongs to
714
     * @param string $corestring Custom lang string name in mimetypes.php
715
     * @param string $customdescription Custom description (plain text/multilang)
716
     * @param bool $defaulticon True if this should be the default icon for the type
717
     * @return stdClass Record matching the parameters
718
     */
719
    protected static function create_config_record($extension, $mimetype,
720
            $coreicon, array $groups, $corestring, $customdescription, $defaulticon) {
721
        // Construct new entry.
722
        $newentry = (object)array('extension' => (string)$extension, 'type' => (string)$mimetype,
723
                'icon' => (string)$coreicon);
724
        if ($groups) {
725
            if (!is_array($groups)) {
726
                throw new coding_exception('Groups must be an array');
727
            }
728
            foreach ($groups as $group) {
729
                if (!is_string($group)) {
730
                    throw new coding_exception('Groups must be an array of strings');
731
                }
732
            }
733
            $newentry->groups = $groups;
734
        }
735
        if ($corestring) {
736
            $newentry->string = (string)$corestring;
737
        }
738
        if ($customdescription) {
739
            $newentry->customdescription = (string)$customdescription;
740
        }
741
        if ($defaulticon) {
742
            $newentry->defaulticon = true;
743
        }
744
        return $newentry;
745
    }
746
}