MDL-45170 files: check other draftfile areas when processing
[moodle.git] / lib / classes / filetypes.php
blob4a84e55815cba3690c4c03d08645eb9cedd314cb
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.
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/>.
17 /**
18 * Class to manage the custom filetypes list that is stored in a config variable.
20 * @package core
21 * @copyright 2014 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->libdir . '/filelib.php');
29 /**
30 * Class to manage the custom filetypes list that is stored in a config variable.
32 * @copyright 2014 The Open University
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 abstract class core_filetypes {
36 /** @var array Cached MIME types for current request */
37 protected static $cachedtypes;
39 /**
40 * Gets default MIME types that are included as standard.
42 * Note: Use the function get_mimetypes_array to access this data including
43 * any customisations the user might have made.
45 * @return array Default (pre-installed) MIME type information
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' => 'quicktime', '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' => 'base'),
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' => 'avi',
65 'groups' => array('video', 'web_video'), 'string' => 'video'),
66 'bmp' => array('type' => 'image/bmp', 'icon' => 'bmp', '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' => 'text', 'groups' => array('web_file')),
72 'csv' => array('type' => 'text/csv', 'icon' => 'spreadsheet', 'groups' => array('spreadsheet')),
73 'dv' => array('type' => 'video/x-dv', 'icon' => 'quicktime', 'groups' => array('video'), 'string' => 'video'),
74 'dmg' => array('type' => 'application/octet-stream', 'icon' => 'unknown'),
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'),
87 'dcr' => array('type' => 'application/x-director', 'icon' => 'flash'),
88 'dif' => array('type' => 'video/x-dv', 'icon' => 'quicktime', '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'),
100 'gallery' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
101 'galleryitem' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
102 'gallerycollection' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
103 'gdraw' => array('type' => 'application/vnd.google-apps.drawing', 'icon' => 'image', 'groups' => array('image')),
104 'gdoc' => array('type' => 'application/vnd.google-apps.document', 'icon' => 'document', 'groups' => array('document')),
105 'gsheet' => array('type' => 'application/vnd.google-apps.spreadsheet', 'icon' => 'spreadsheet',
106 'groups' => array('spreadsheet')),
107 'gslides' => array('type' => 'application/vnd.google-apps.presentation', 'icon' => 'powerpoint',
108 'groups' => array('presentation')),
109 'gif' => array('type' => 'image/gif', 'icon' => 'gif', 'groups' => array('image', 'web_image'), 'string' => 'image'),
110 'gtar' => array('type' => 'application/x-gtar', 'icon' => 'archive',
111 'groups' => array('archive'), 'string' => 'archive'),
112 'tgz' => array('type' => 'application/g-zip', 'icon' => 'archive', 'groups' => array('archive'), 'string' => 'archive'),
113 'gz' => array('type' => 'application/g-zip', 'icon' => 'archive', 'groups' => array('archive'), 'string' => 'archive'),
114 'gzip' => array('type' => 'application/g-zip', 'icon' => 'archive',
115 'groups' => array('archive'), 'string' => 'archive'),
116 'h' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
117 'hpp' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
118 'hqx' => array('type' => 'application/mac-binhex40', 'icon' => 'archive',
119 'groups' => array('archive'), 'string' => 'archive'),
120 'htc' => array('type' => 'text/x-component', 'icon' => 'markup'),
121 'html' => array('type' => 'text/html', 'icon' => 'html', 'groups' => array('web_file')),
122 'xhtml' => array('type' => 'application/xhtml+xml', 'icon' => 'html', 'groups' => array('web_file')),
123 'htm' => array('type' => 'text/html', 'icon' => 'html', 'groups' => array('web_file')),
124 'ico' => array('type' => 'image/vnd.microsoft.icon', 'icon' => 'image',
125 'groups' => array('image'), 'string' => 'image'),
126 'ics' => array('type' => 'text/calendar', 'icon' => 'text'),
127 'isf' => array('type' => 'application/inspiration', 'icon' => 'isf'),
128 'ist' => array('type' => 'application/inspiration.template', 'icon' => 'isf'),
129 'java' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
130 'jar' => array('type' => 'application/java-archive', 'icon' => 'archive'),
131 'jcb' => array('type' => 'text/xml', 'icon' => 'markup'),
132 'jcl' => array('type' => 'text/xml', 'icon' => 'markup'),
133 'jcw' => array('type' => 'text/xml', 'icon' => 'markup'),
134 'jmt' => array('type' => 'text/xml', 'icon' => 'markup'),
135 'jmx' => array('type' => 'text/xml', 'icon' => 'markup'),
136 'jnlp' => array('type' => 'application/x-java-jnlp-file', 'icon' => 'markup'),
137 'jpe' => array('type' => 'image/jpeg', 'icon' => 'jpeg', 'groups' => array('image', 'web_image'), 'string' => 'image'),
138 'jpeg' => array('type' => 'image/jpeg', 'icon' => 'jpeg', 'groups' => array('image', 'web_image'), 'string' => 'image'),
139 'jpg' => array('type' => 'image/jpeg', 'icon' => 'jpeg', 'groups' => array('image', 'web_image'), 'string' => 'image'),
140 'jqz' => array('type' => 'text/xml', 'icon' => 'markup'),
141 'js' => array('type' => 'application/x-javascript', 'icon' => 'text', 'groups' => array('web_file')),
142 'json' => array('type' => 'application/json', 'icon' => 'text'),
143 'latex' => array('type' => 'application/x-latex', 'icon' => 'text'),
144 'm' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
145 'mbz' => array('type' => 'application/vnd.moodle.backup', 'icon' => 'moodle'),
146 'mdb' => array('type' => 'application/x-msaccess', 'icon' => 'base'),
147 'mht' => array('type' => 'message/rfc822', 'icon' => 'archive'),
148 'mhtml' => array('type' => 'message/rfc822', 'icon' => 'archive'),
149 'mov' => array('type' => 'video/quicktime', 'icon' => 'quicktime',
150 'groups' => array('video', 'web_video', 'html_video'), 'string' => 'video'),
151 'movie' => array('type' => 'video/x-sgi-movie', 'icon' => 'quicktime', 'groups' => array('video'), 'string' => 'video'),
152 'mw' => array('type' => 'application/maple', 'icon' => 'math'),
153 'mws' => array('type' => 'application/maple', 'icon' => 'math'),
154 'm3u' => array('type' => 'audio/x-mpegurl', 'icon' => 'mp3', 'groups' => array('audio'), 'string' => 'audio'),
155 'mp3' => array('type' => 'audio/mp3', 'icon' => 'mp3', 'groups' => array('audio', 'html_audio', 'web_audio'),
156 'string' => 'audio'),
157 'mp4' => array('type' => 'video/mp4', 'icon' => 'mpeg', 'groups' => array('html_video', 'video', 'web_video'),
158 'string' => 'video'),
159 'm4v' => array('type' => 'video/mp4', 'icon' => 'mpeg', 'groups' => array('html_video', 'video', 'web_video'),
160 'string' => 'video'),
161 'm4a' => array('type' => 'audio/mp4', 'icon' => 'mp3', 'groups' => array('audio', 'html_audio', 'web_audio'),
162 'string' => 'audio'),
163 'mpeg' => array('type' => 'video/mpeg', 'icon' => 'mpeg', 'groups' => array('video', 'web_video'),
164 'string' => 'video'),
165 'mpe' => array('type' => 'video/mpeg', 'icon' => 'mpeg', 'groups' => array('video', 'web_video'),
166 'string' => 'video'),
167 'mpg' => array('type' => 'video/mpeg', 'icon' => 'mpeg', 'groups' => array('video', 'web_video'),
168 'string' => 'video'),
169 'mpr' => array('type' => 'application/vnd.moodle.profiling', 'icon' => 'moodle'),
171 'nbk' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
172 'notebook' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
174 'odt' => array('type' => 'application/vnd.oasis.opendocument.text', 'icon' => 'writer', 'groups' => array('document')),
175 'ott' => array('type' => 'application/vnd.oasis.opendocument.text-template',
176 'icon' => 'writer', 'groups' => array('document')),
177 'oth' => array('type' => 'application/vnd.oasis.opendocument.text-web', 'icon' => 'oth', 'groups' => array('document')),
178 'odm' => array('type' => 'application/vnd.oasis.opendocument.text-master', 'icon' => 'writer'),
179 'odg' => array('type' => 'application/vnd.oasis.opendocument.graphics', 'icon' => 'draw'),
180 'otg' => array('type' => 'application/vnd.oasis.opendocument.graphics-template', 'icon' => 'draw'),
181 'odp' => array('type' => 'application/vnd.oasis.opendocument.presentation', 'icon' => 'impress',
182 'groups' => array('presentation')),
183 'otp' => array('type' => 'application/vnd.oasis.opendocument.presentation-template', 'icon' => 'impress',
184 'groups' => array('presentation')),
185 'ods' => array('type' => 'application/vnd.oasis.opendocument.spreadsheet',
186 'icon' => 'calc', 'groups' => array('spreadsheet')),
187 'ots' => array('type' => 'application/vnd.oasis.opendocument.spreadsheet-template',
188 'icon' => 'calc', 'groups' => array('spreadsheet')),
189 'odc' => array('type' => 'application/vnd.oasis.opendocument.chart', 'icon' => 'chart'),
190 'odf' => array('type' => 'application/vnd.oasis.opendocument.formula', 'icon' => 'math'),
191 'odb' => array('type' => 'application/vnd.oasis.opendocument.database', 'icon' => 'base'),
192 'odi' => array('type' => 'application/vnd.oasis.opendocument.image', 'icon' => 'draw'),
193 'oga' => array('type' => 'audio/ogg', 'icon' => 'audio', 'groups' => array('audio', 'html_audio', 'web_audio'),
194 'string' => 'audio'),
195 'ogg' => array('type' => 'audio/ogg', 'icon' => 'audio', 'groups' => array('audio', 'html_audio', 'web_audio'),
196 'string' => 'audio'),
197 'ogv' => array('type' => 'video/ogg', 'icon' => 'video', 'groups' => array('html_video', 'video', 'web_video'),
198 'string' => 'video'),
200 'pct' => array('type' => 'image/pict', 'icon' => 'image', 'groups' => array('image'), 'string' => 'image'),
201 'pdf' => array('type' => 'application/pdf', 'icon' => 'pdf', 'groups' => array('document')),
202 'php' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
203 'pic' => array('type' => 'image/pict', 'icon' => 'image', 'groups' => array('image'), 'string' => 'image'),
204 'pict' => array('type' => 'image/pict', 'icon' => 'image', 'groups' => array('image'), 'string' => 'image'),
205 'png' => array('type' => 'image/png', 'icon' => 'png', 'groups' => array('image', 'web_image'), 'string' => 'image'),
206 'pps' => array('type' => 'application/vnd.ms-powerpoint', 'icon' => 'powerpoint', 'groups' => array('presentation')),
207 'ppt' => array('type' => 'application/vnd.ms-powerpoint', 'icon' => 'powerpoint', 'groups' => array('presentation')),
208 'pptx' => array('type' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
209 'icon' => 'powerpoint', 'groups' => array('presentation')),
210 'pptm' => array('type' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12', 'icon' => 'powerpoint',
211 'groups' => array('presentation')),
212 'potx' => array('type' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
213 'icon' => 'powerpoint', 'groups' => array('presentation')),
214 'potm' => array('type' => 'application/vnd.ms-powerpoint.template.macroEnabled.12', 'icon' => 'powerpoint',
215 'groups' => array('presentation')),
216 'ppam' => array('type' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12', 'icon' => 'powerpoint',
217 'groups' => array('presentation')),
218 'ppsx' => array('type' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
219 'icon' => 'powerpoint', 'groups' => array('presentation')),
220 'ppsm' => array('type' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12', 'icon' => 'powerpoint',
221 'groups' => array('presentation')),
222 'ps' => array('type' => 'application/postscript', 'icon' => 'pdf'),
223 'pub' => array('type' => 'application/x-mspublisher', 'icon' => 'publisher', 'groups' => array('presentation')),
225 'qt' => array('type' => 'video/quicktime', 'icon' => 'quicktime',
226 'groups' => array('video', 'web_video'), 'string' => 'video'),
227 'ra' => array('type' => 'audio/x-realaudio-plugin', 'icon' => 'audio',
228 'groups' => array('audio', 'web_audio'), 'string' => 'audio'),
229 'ram' => array('type' => 'audio/x-pn-realaudio-plugin', 'icon' => 'audio',
230 'groups' => array('audio'), 'string' => 'audio'),
231 'rar' => array('type' => 'application/x-rar-compressed', 'icon' => 'archive',
232 'groups' => array('archive'), 'string' => 'archive'),
233 'rhb' => array('type' => 'text/xml', 'icon' => 'markup'),
234 'rm' => array('type' => 'audio/x-pn-realaudio-plugin', 'icon' => 'audio',
235 'groups' => array('audio'), 'string' => 'audio'),
236 'rmvb' => array('type' => 'application/vnd.rn-realmedia-vbr', 'icon' => 'video',
237 'groups' => array('video'), 'string' => 'video'),
238 'rtf' => array('type' => 'text/rtf', 'icon' => 'text', 'groups' => array('document')),
239 'rtx' => array('type' => 'text/richtext', 'icon' => 'text'),
240 'rv' => array('type' => 'audio/x-pn-realaudio-plugin', 'icon' => 'audio',
241 'groups' => array('video'), 'string' => 'video'),
242 'scss' => array('type' => 'text/x-scss', 'icon' => 'text', 'groups' => array('web_file')),
243 'sh' => array('type' => 'application/x-sh', 'icon' => 'sourcecode'),
244 'sit' => array('type' => 'application/x-stuffit', 'icon' => 'archive',
245 'groups' => array('archive'), 'string' => 'archive'),
246 'smi' => array('type' => 'application/smil', 'icon' => 'text'),
247 'smil' => array('type' => 'application/smil', 'icon' => 'text'),
248 'sqt' => array('type' => 'text/xml', 'icon' => 'markup'),
249 'svg' => array('type' => 'image/svg+xml', 'icon' => 'image',
250 'groups' => array('image', 'web_image'), 'string' => 'image'),
251 'svgz' => array('type' => 'image/svg+xml', 'icon' => 'image',
252 'groups' => array('image', 'web_image'), 'string' => 'image'),
253 'swa' => array('type' => 'application/x-director', 'icon' => 'flash'),
254 'swf' => array('type' => 'application/x-shockwave-flash', 'icon' => 'flash', 'groups' => array('video', 'web_video')),
255 'swfl' => array('type' => 'application/x-shockwave-flash', 'icon' => 'flash', 'groups' => array('video', 'web_video')),
257 'sxw' => array('type' => 'application/vnd.sun.xml.writer', 'icon' => 'writer'),
258 'stw' => array('type' => 'application/vnd.sun.xml.writer.template', 'icon' => 'writer'),
259 'sxc' => array('type' => 'application/vnd.sun.xml.calc', 'icon' => 'calc'),
260 'stc' => array('type' => 'application/vnd.sun.xml.calc.template', 'icon' => 'calc'),
261 'sxd' => array('type' => 'application/vnd.sun.xml.draw', 'icon' => 'draw'),
262 'std' => array('type' => 'application/vnd.sun.xml.draw.template', 'icon' => 'draw'),
263 'sxi' => array('type' => 'application/vnd.sun.xml.impress', 'icon' => 'impress', 'groups' => array('presentation')),
264 'sti' => array('type' => 'application/vnd.sun.xml.impress.template', 'icon' => 'impress',
265 'groups' => array('presentation')),
266 'sxg' => array('type' => 'application/vnd.sun.xml.writer.global', 'icon' => 'writer'),
267 'sxm' => array('type' => 'application/vnd.sun.xml.math', 'icon' => 'math'),
269 'tar' => array('type' => 'application/x-tar', 'icon' => 'archive', 'groups' => array('archive'), 'string' => 'archive'),
270 'tif' => array('type' => 'image/tiff', 'icon' => 'tiff', 'groups' => array('image'), 'string' => 'image'),
271 'tiff' => array('type' => 'image/tiff', 'icon' => 'tiff', 'groups' => array('image'), 'string' => 'image'),
272 'tex' => array('type' => 'application/x-tex', 'icon' => 'text'),
273 'texi' => array('type' => 'application/x-texinfo', 'icon' => 'text'),
274 'texinfo' => array('type' => 'application/x-texinfo', 'icon' => 'text'),
275 'tsv' => array('type' => 'text/tab-separated-values', 'icon' => 'text'),
276 'txt' => array('type' => 'text/plain', 'icon' => 'text', 'defaulticon' => true),
277 'vtt' => array('type' => 'text/vtt', 'icon' => 'text', 'groups' => array('html_track')),
278 'wav' => array('type' => 'audio/wav', 'icon' => 'wav', 'groups' => array('audio', 'html_audio', 'web_audio'),
279 'string' => 'audio'),
280 'webm' => array('type' => 'video/webm', 'icon' => 'video', 'groups' => array('html_video', 'video', 'web_video'),
281 'string' => 'video'),
282 'wmv' => array('type' => 'video/x-ms-wmv', 'icon' => 'wmv', 'groups' => array('video'), 'string' => 'video'),
283 'asf' => array('type' => 'video/x-ms-asf', 'icon' => 'wmv', 'groups' => array('video'), 'string' => 'video'),
284 'wma' => array('type' => 'audio/x-ms-wma', 'icon' => 'audio', 'groups' => array('audio'), 'string' => 'audio'),
286 'xbk' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
287 'xdp' => array('type' => 'application/vnd.adobe.xdp+xml', 'icon' => 'pdf'),
288 'xfd' => array('type' => 'application/vnd.xfdl', 'icon' => 'pdf'),
289 'xfdf' => array('type' => 'application/vnd.adobe.xfdf', 'icon' => 'pdf'),
291 'xls' => array('type' => 'application/vnd.ms-excel', 'icon' => 'spreadsheet', 'groups' => array('spreadsheet')),
292 'xlsx' => array('type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'icon' => 'spreadsheet',
293 'groups' => array('spreadsheet')),
294 'xlsm' => array('type' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
295 'icon' => 'spreadsheet', 'groups' => array('spreadsheet')),
296 'xltx' => array('type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
297 'icon' => 'spreadsheet'),
298 'xltm' => array('type' => 'application/vnd.ms-excel.template.macroEnabled.12', 'icon' => 'spreadsheet'),
299 'xlsb' => array('type' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', 'icon' => 'spreadsheet'),
300 'xlam' => array('type' => 'application/vnd.ms-excel.addin.macroEnabled.12', 'icon' => 'spreadsheet'),
302 'xml' => array('type' => 'application/xml', 'icon' => 'markup'),
303 'xsl' => array('type' => 'text/xml', 'icon' => 'markup'),
305 'zip' => array('type' => 'application/zip', 'icon' => 'archive', 'groups' => array('archive'), 'string' => 'archive')
310 * Given a mimetype - return a valid file extension for it.
312 * @param $mimetype string
313 * @return string|bool False if the mimetype was not known, a string indicating a valid file extension otherwise. It may not
314 * be the only valid file extension - just the first one found.
316 public static function get_file_extension($mimetype) {
317 $types = self::get_types();
318 foreach ($types as $extension => $info) {
319 if ($info['type'] == $mimetype) {
320 return $extension;
323 return false;
327 * Gets all the current types.
329 * @return array Associative array from extension to array of data about type
331 public static function &get_types() {
332 // If it was already done in this request, use cache.
333 if (self::$cachedtypes) {
334 return self::$cachedtypes;
337 // Get defaults.
338 $mimetypes = self::get_default_types();
340 // If there are no custom types, just return.
341 $custom = self::get_custom_types();
342 if (empty($custom)) {
343 return $mimetypes;
346 // Check value is an array.
347 if (!is_array($custom)) {
348 debugging('Invalid $CFG->customfiletypes (not array)', DEBUG_DEVELOPER);
349 return $mimetypes;
352 foreach ($custom as $customentry) {
353 // Each entry is a stdClass object similar to the array values above.
354 if (empty($customentry->extension)) {
355 debugging('Invalid $CFG->customfiletypes entry (extension field required)',
356 DEBUG_DEVELOPER);
357 continue;
360 // To delete a standard entry, set 'deleted' to true.
361 if (!empty($customentry->deleted)) {
362 unset($mimetypes[$customentry->extension]);
363 continue;
366 // Check required fields.
367 if (empty($customentry->type) || empty($customentry->icon)) {
368 debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
369 ' (type and icon fields required)', DEBUG_DEVELOPER);
370 continue;
373 // Build result array.
374 $result = array('type' => $customentry->type, 'icon' => $customentry->icon);
375 if (!empty($customentry->groups)) {
376 if (!is_array($customentry->groups)) {
377 debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
378 ' (groups field not array)', DEBUG_DEVELOPER);
379 continue;
381 $result['groups'] = $customentry->groups;
383 if (!empty($customentry->string)) {
384 if (!is_string($customentry->string)) {
385 debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
386 ' (string field not string)', DEBUG_DEVELOPER);
387 continue;
389 $result['string'] = $customentry->string;
391 if (!empty($customentry->defaulticon)) {
392 if (!is_bool($customentry->defaulticon)) {
393 debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
394 ' (defaulticon field not bool)', DEBUG_DEVELOPER);
395 continue;
397 $result['defaulticon'] = $customentry->defaulticon;
399 if (!empty($customentry->customdescription)) {
400 if (!is_string($customentry->customdescription)) {
401 debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
402 ' (customdescription field not string)', DEBUG_DEVELOPER);
403 continue;
405 // As the name suggests, this field is used only for custom entries.
406 $result['customdescription'] = $customentry->customdescription;
409 // Track whether it is a custom filetype or a modified existing
410 // filetype.
411 if (array_key_exists($customentry->extension, $mimetypes)) {
412 $result['modified'] = true;
413 } else {
414 $result['custom'] = true;
417 // Add result array to list.
418 $mimetypes[$customentry->extension] = $result;
421 self::$cachedtypes = $mimetypes;
422 return self::$cachedtypes;
426 * Gets custom types from config variable, after decoding the JSON if required.
428 * @return array Array of custom types (empty array if none)
430 protected static function get_custom_types() {
431 global $CFG;
432 if (!empty($CFG->customfiletypes)) {
433 if (is_array($CFG->customfiletypes)) {
434 // You can define this as an array in config.php...
435 return $CFG->customfiletypes;
436 } else {
437 // Or as a JSON string in the config table.
438 return json_decode($CFG->customfiletypes);
440 } else {
441 return array();
446 * Sets the custom types into config variable, encoding into JSON.
448 * @param array $types Array of custom types
449 * @throws coding_exception If the custom types are fixed in config.php.
451 protected static function set_custom_types(array $types) {
452 global $CFG;
453 // Check the setting hasn't been forced.
454 if (array_key_exists('customfiletypes', $CFG->config_php_settings)) {
455 throw new coding_exception('Cannot set custom filetypes because they ' .
456 'are defined in config.php');
458 if (empty($types)) {
459 unset_config('customfiletypes');
460 } else {
461 set_config('customfiletypes', json_encode(array_values($types)));
464 // Clear the cached type list.
465 self::reset_caches();
469 * Clears the type cache. This is not needed in normal use as the
470 * set_custom_types function automatically clears the cache. Intended for
471 * use in unit tests.
473 public static function reset_caches() {
474 self::$cachedtypes = null;
478 * Gets the default types that have been deleted. Returns an array containing
479 * the defaults of all those types.
481 * @return array Array (same format as get_mimetypes_array)
483 public static function get_deleted_types() {
484 $defaults = self::get_default_types();
485 $deleted = array();
486 foreach (self::get_custom_types() as $customentry) {
487 if (!empty($customentry->deleted)) {
488 $deleted[$customentry->extension] = $defaults[$customentry->extension];
491 return $deleted;
495 * Adds a new entry to the list of custom filetypes.
497 * @param string $extension File extension without dot, e.g. 'doc'
498 * @param string $mimetype MIME type e.g. 'application/msword'
499 * @param string $coreicon Core icon to use e.g. 'document'
500 * @param array $groups Array of group strings that this type belongs to
501 * @param string $corestring Custom lang string name in mimetypes.php
502 * @param string $customdescription Custom description (plain text/multilang)
503 * @param bool $defaulticon True if this should be the default icon for the type
504 * @throws coding_exception If the extension already exists, or otherwise invalid
506 public static function add_type($extension, $mimetype, $coreicon,
507 array $groups = array(), $corestring = '', $customdescription = '',
508 $defaulticon = false) {
509 // Check for blank extensions or incorrectly including the dot.
510 $extension = (string)$extension;
511 if ($extension === '' || $extension[0] === '.') {
512 throw new coding_exception('Invalid extension .' . $extension);
515 // Check extension not already used.
516 $mimetypes = get_mimetypes_array();
517 if (array_key_exists($extension, $mimetypes)) {
518 throw new coding_exception('Extension ' . $extension . ' already exists');
521 // For default icon, check there isn't already something with default icon
522 // set for that MIME type.
523 if ($defaulticon) {
524 foreach ($mimetypes as $type) {
525 if ($type['type'] === $mimetype && !empty($type['defaulticon'])) {
526 throw new coding_exception('MIME type ' . $mimetype .
527 ' already has a default icon set');
532 // Get existing custom filetype list.
533 $customs = self::get_custom_types();
535 // Check if there's a 'deleted' entry for the extension, if so then get
536 // rid of it.
537 foreach ($customs as $key => $custom) {
538 if ($custom->extension === $extension) {
539 unset($customs[$key]);
543 // Set up config record for new type.
544 $newtype = self::create_config_record($extension, $mimetype, $coreicon, $groups,
545 $corestring, $customdescription, $defaulticon);
547 // See if there's a default value with this extension.
548 $needsadding = true;
549 $defaults = self::get_default_types();
550 if (array_key_exists($extension, $defaults)) {
551 // If it has the same values, we don't need to add it.
552 $defaultvalue = $defaults[$extension];
553 $modified = (array)$newtype;
554 unset($modified['extension']);
555 ksort($defaultvalue);
556 ksort($modified);
557 if ($modified === $defaultvalue) {
558 $needsadding = false;
562 // Add to array and set in config.
563 if ($needsadding) {
564 $customs[] = $newtype;
566 self::set_custom_types($customs);
570 * Updates an entry in the list of filetypes in config.
572 * @param string $extension File extension without dot, e.g. 'doc'
573 * @param string $newextension New file extension (same if not changing)
574 * @param string $mimetype MIME type e.g. 'application/msword'
575 * @param string $coreicon Core icon to use e.g. 'document'
576 * @param array $groups Array of group strings that this type belongs to
577 * @param string $corestring Custom lang string name in mimetypes.php
578 * @param string $customdescription Custom description (plain text/multilang)
579 * @param bool $defaulticon True if this should be the default icon for the type
580 * @throws coding_exception If the new extension already exists, or otherwise invalid
582 public static function update_type($extension, $newextension, $mimetype, $coreicon,
583 array $groups = array(), $corestring = '', $customdescription = '',
584 $defaulticon = false) {
586 // Extension must exist.
587 $extension = (string)$extension;
588 $mimetypes = get_mimetypes_array();
589 if (!array_key_exists($extension, $mimetypes)) {
590 throw new coding_exception('Extension ' . $extension . ' not found');
593 // If there's a new extension then this must not exist.
594 $newextension = (string)$newextension;
595 if ($newextension !== $extension) {
596 if ($newextension === '' || $newextension[0] === '.') {
597 throw new coding_exception('Invalid extension .' . $newextension);
599 if (array_key_exists($newextension, $mimetypes)) {
600 throw new coding_exception('Extension ' . $newextension . ' already exists');
604 // For default icon, check there isn't already something with default icon
605 // set for that MIME type (unless it's this).
606 if ($defaulticon) {
607 foreach ($mimetypes as $ext => $type) {
608 if ($ext !== $extension && $type['type'] === $mimetype &&
609 !empty($type['defaulticon'])) {
610 throw new coding_exception('MIME type ' . $mimetype .
611 ' already has a default icon set');
616 // Delete the old extension and then add the new one (may be same). This
617 // will correctly handle cases when a default type is involved.
618 self::delete_type($extension);
619 self::add_type($newextension, $mimetype, $coreicon, $groups, $corestring,
620 $customdescription, $defaulticon);
624 * Deletes a file type from the config list (or, for a standard one, marks it
625 * as deleted).
627 * @param string $extension File extension without dot, e.g. 'doc'
628 * @throws coding_exception If the extension does not exist, or otherwise invalid
630 public static function delete_type($extension) {
631 // Extension must exist.
632 $mimetypes = get_mimetypes_array();
633 if (!array_key_exists($extension, $mimetypes)) {
634 throw new coding_exception('Extension ' . $extension . ' not found');
637 // Get existing custom filetype list.
638 $customs = self::get_custom_types();
640 // Remove any entries for this extension.
641 foreach ($customs as $key => $custom) {
642 if ($custom->extension === $extension && empty($custom->deleted)) {
643 unset($customs[$key]);
647 // If it was a standard entry (doesn't have 'custom' set) then add a
648 // deleted marker.
649 if (empty($mimetypes[$extension]['custom'])) {
650 $customs[] = (object)array('extension' => $extension, 'deleted' => true);
653 // Save and reset cache.
654 self::set_custom_types($customs);
658 * Reverts a file type to the default. May only be called on types that have
659 * default values. This will undelete the type if necessary or set its values.
660 * If the type is already at default values, does nothing.
662 * @param string $extension File extension without dot, e.g. 'doc'
663 * @return bool True if anything was changed, false if it was already default
664 * @throws coding_exception If the extension is not a default type.
666 public static function revert_type_to_default($extension) {
667 $extension = (string)$extension;
669 // Check it actually is a default type.
670 $defaults = self::get_default_types();
671 if (!array_key_exists($extension, $defaults)) {
672 throw new coding_exception('Extension ' . $extension . ' is not a default type');
675 // Loop through all the custom settings.
676 $changed = false;
677 $customs = self::get_custom_types();
678 foreach ($customs as $key => $customentry) {
679 if ($customentry->extension === $extension) {
680 unset($customs[$key]);
681 $changed = true;
685 // Save changes if any.
686 if ($changed) {
687 self::set_custom_types($customs);
689 return $changed;
693 * Converts function parameters into a record for storing in the JSON value.
695 * @param string $extension File extension without dot, e.g. 'doc'
696 * @param string $mimetype MIME type e.g. 'application/msword'
697 * @param string $coreicon Core icon to use e.g. 'document'
698 * @param array $groups Array of group strings that this type belongs to
699 * @param string $corestring Custom lang string name in mimetypes.php
700 * @param string $customdescription Custom description (plain text/multilang)
701 * @param bool $defaulticon True if this should be the default icon for the type
702 * @return stdClass Record matching the parameters
704 protected static function create_config_record($extension, $mimetype,
705 $coreicon, array $groups, $corestring, $customdescription, $defaulticon) {
706 // Construct new entry.
707 $newentry = (object)array('extension' => (string)$extension, 'type' => (string)$mimetype,
708 'icon' => (string)$coreicon);
709 if ($groups) {
710 if (!is_array($groups)) {
711 throw new coding_exception('Groups must be an array');
713 foreach ($groups as $group) {
714 if (!is_string($group)) {
715 throw new coding_exception('Groups must be an array of strings');
718 $newentry->groups = $groups;
720 if ($corestring) {
721 $newentry->string = (string)$corestring;
723 if ($customdescription) {
724 $newentry->customdescription = (string)$customdescription;
726 if ($defaulticon) {
727 $newentry->defaulticon = true;
729 return $newentry;