Merge branch 'MDL-68854-master' of git://github.com/vmdef/moodle
[moodle.git] / lib / classes / filetypes.php
blobe1e04168b9e08b264d176dde948e802ff7329ea8
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'),
99 'fmp4' => array('type' => 'video/mp4', 'icon' => 'mpeg', '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'), 'string' => 'image'),
111 'gtar' => array('type' => 'application/x-gtar', 'icon' => 'archive',
112 'groups' => array('archive'), 'string' => 'archive'),
113 'tgz' => array('type' => 'application/g-zip', 'icon' => 'archive', 'groups' => array('archive'), 'string' => 'archive'),
114 'gz' => array('type' => 'application/g-zip', 'icon' => 'archive', 'groups' => array('archive'), 'string' => 'archive'),
115 'gzip' => array('type' => 'application/g-zip', 'icon' => 'archive',
116 'groups' => array('archive'), 'string' => 'archive'),
117 'h' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
118 'h5p' => array('type' => 'application/zip.h5p', 'icon' => 'h5p', 'string' => 'archive'),
119 'hpp' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
120 'hqx' => array('type' => 'application/mac-binhex40', 'icon' => 'archive',
121 'groups' => array('archive'), 'string' => 'archive'),
122 'htc' => array('type' => 'text/x-component', 'icon' => 'markup'),
123 'html' => array('type' => 'text/html', 'icon' => 'html', 'groups' => array('web_file')),
124 'xhtml' => array('type' => 'application/xhtml+xml', 'icon' => 'html', 'groups' => array('web_file')),
125 'htm' => array('type' => 'text/html', 'icon' => 'html', 'groups' => array('web_file')),
126 'ico' => array('type' => 'image/vnd.microsoft.icon', 'icon' => 'image',
127 'groups' => array('image'), 'string' => 'image'),
128 'ics' => array('type' => 'text/calendar', 'icon' => 'text'),
129 'isf' => array('type' => 'application/inspiration', 'icon' => 'isf'),
130 'ist' => array('type' => 'application/inspiration.template', 'icon' => 'isf'),
131 'java' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
132 'jar' => array('type' => 'application/java-archive', 'icon' => 'archive'),
133 'jcb' => array('type' => 'text/xml', 'icon' => 'markup'),
134 'jcl' => array('type' => 'text/xml', 'icon' => 'markup'),
135 'jcw' => array('type' => 'text/xml', 'icon' => 'markup'),
136 'jmt' => array('type' => 'text/xml', 'icon' => 'markup'),
137 'jmx' => array('type' => 'text/xml', 'icon' => 'markup'),
138 'jnlp' => array('type' => 'application/x-java-jnlp-file', 'icon' => 'markup'),
139 'jpe' => array('type' => 'image/jpeg', 'icon' => 'jpeg', 'groups' => array('image', 'web_image'), 'string' => 'image'),
140 'jpeg' => array('type' => 'image/jpeg', 'icon' => 'jpeg', 'groups' => array('image', 'web_image'), 'string' => 'image'),
141 'jpg' => array('type' => 'image/jpeg', 'icon' => 'jpeg', 'groups' => array('image', 'web_image'), 'string' => 'image'),
142 'jqz' => array('type' => 'text/xml', 'icon' => 'markup'),
143 'js' => array('type' => 'application/x-javascript', 'icon' => 'text', 'groups' => array('web_file')),
144 'json' => array('type' => 'application/json', 'icon' => 'text'),
145 'latex' => array('type' => 'application/x-latex', 'icon' => 'text'),
146 'm' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
147 'mbz' => array('type' => 'application/vnd.moodle.backup', 'icon' => 'moodle'),
148 'mdb' => array('type' => 'application/x-msaccess', 'icon' => 'base'),
149 'mht' => array('type' => 'message/rfc822', 'icon' => 'archive'),
150 'mhtml' => array('type' => 'message/rfc822', 'icon' => 'archive'),
151 'mov' => array('type' => 'video/quicktime', 'icon' => 'quicktime',
152 'groups' => array('video', 'web_video', 'html_video'), 'string' => 'video'),
153 'movie' => array('type' => 'video/x-sgi-movie', 'icon' => 'quicktime', 'groups' => array('video'), 'string' => 'video'),
154 'mw' => array('type' => 'application/maple', 'icon' => 'math'),
155 'mws' => array('type' => 'application/maple', 'icon' => 'math'),
156 'm3u' => array('type' => 'audio/x-mpegurl', 'icon' => 'mp3', 'groups' => array('audio'), 'string' => 'audio'),
157 'm3u8' => array('type' => 'application/x-mpegURL', 'icon' => 'mpeg', 'groups' => array('media_source')),
158 'mp3' => array('type' => 'audio/mp3', 'icon' => 'mp3', 'groups' => array('audio', 'html_audio', 'web_audio'),
159 'string' => 'audio'),
160 'mp4' => array('type' => 'video/mp4', 'icon' => 'mpeg', 'groups' => array('html_video', 'video', 'web_video'),
161 'string' => 'video'),
162 'm4v' => array('type' => 'video/mp4', 'icon' => 'mpeg', 'groups' => array('html_video', 'video', 'web_video'),
163 'string' => 'video'),
164 'm4a' => array('type' => 'audio/mp4', 'icon' => 'mp3', 'groups' => array('audio', 'html_audio', 'web_audio'),
165 'string' => 'audio'),
166 'mpeg' => array('type' => 'video/mpeg', 'icon' => 'mpeg', 'groups' => array('video', 'web_video'),
167 'string' => 'video'),
168 'mpd' => array('type' => 'application/dash+xml', 'icon' => 'mpeg', 'groups' => array('media_source')),
169 'mpe' => array('type' => 'video/mpeg', 'icon' => 'mpeg', 'groups' => array('video', 'web_video'),
170 'string' => 'video'),
171 'mpg' => array('type' => 'video/mpeg', 'icon' => 'mpeg', 'groups' => array('video', 'web_video'),
172 'string' => 'video'),
173 'mpr' => array('type' => 'application/vnd.moodle.profiling', 'icon' => 'moodle'),
175 'nbk' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
176 'notebook' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
178 'odt' => array('type' => 'application/vnd.oasis.opendocument.text', 'icon' => 'writer', 'groups' => array('document')),
179 'ott' => array('type' => 'application/vnd.oasis.opendocument.text-template',
180 'icon' => 'writer', 'groups' => array('document')),
181 'oth' => array('type' => 'application/vnd.oasis.opendocument.text-web', 'icon' => 'oth', 'groups' => array('document')),
182 'odm' => array('type' => 'application/vnd.oasis.opendocument.text-master', 'icon' => 'writer'),
183 'odg' => array('type' => 'application/vnd.oasis.opendocument.graphics', 'icon' => 'draw'),
184 'otg' => array('type' => 'application/vnd.oasis.opendocument.graphics-template', 'icon' => 'draw'),
185 'odp' => array('type' => 'application/vnd.oasis.opendocument.presentation', 'icon' => 'impress',
186 'groups' => array('presentation')),
187 'otp' => array('type' => 'application/vnd.oasis.opendocument.presentation-template', 'icon' => 'impress',
188 'groups' => array('presentation')),
189 'ods' => array('type' => 'application/vnd.oasis.opendocument.spreadsheet',
190 'icon' => 'calc', 'groups' => array('spreadsheet')),
191 'ots' => array('type' => 'application/vnd.oasis.opendocument.spreadsheet-template',
192 'icon' => 'calc', 'groups' => array('spreadsheet')),
193 'odc' => array('type' => 'application/vnd.oasis.opendocument.chart', 'icon' => 'chart'),
194 'odf' => array('type' => 'application/vnd.oasis.opendocument.formula', 'icon' => 'math'),
195 'odb' => array('type' => 'application/vnd.oasis.opendocument.database', 'icon' => 'base'),
196 'odi' => array('type' => 'application/vnd.oasis.opendocument.image', 'icon' => 'draw'),
197 'oga' => array('type' => 'audio/ogg', 'icon' => 'audio', 'groups' => array('audio', 'html_audio', 'web_audio'),
198 'string' => 'audio'),
199 'ogg' => array('type' => 'audio/ogg', 'icon' => 'audio', 'groups' => array('audio', 'html_audio', 'web_audio'),
200 'string' => 'audio'),
201 'ogv' => array('type' => 'video/ogg', 'icon' => 'video', 'groups' => array('html_video', 'video', 'web_video'),
202 'string' => 'video'),
204 'pct' => array('type' => 'image/pict', 'icon' => 'image', 'groups' => array('image'), 'string' => 'image'),
205 'pdf' => array('type' => 'application/pdf', 'icon' => 'pdf', 'groups' => array('document')),
206 'php' => array('type' => 'text/plain', 'icon' => 'sourcecode'),
207 'pic' => array('type' => 'image/pict', 'icon' => 'image', 'groups' => array('image'), 'string' => 'image'),
208 'pict' => array('type' => 'image/pict', 'icon' => 'image', 'groups' => array('image'), 'string' => 'image'),
209 'png' => array('type' => 'image/png', 'icon' => 'png', 'groups' => array('image', 'web_image'), 'string' => 'image'),
210 'pps' => array('type' => 'application/vnd.ms-powerpoint', 'icon' => 'powerpoint', 'groups' => array('presentation')),
211 'ppt' => array('type' => 'application/vnd.ms-powerpoint', 'icon' => 'powerpoint', 'groups' => array('presentation')),
212 'pptx' => array('type' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
213 'icon' => 'powerpoint', 'groups' => array('presentation')),
214 'pptm' => array('type' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12', 'icon' => 'powerpoint',
215 'groups' => array('presentation')),
216 'potx' => array('type' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
217 'icon' => 'powerpoint', 'groups' => array('presentation')),
218 'potm' => array('type' => 'application/vnd.ms-powerpoint.template.macroEnabled.12', 'icon' => 'powerpoint',
219 'groups' => array('presentation')),
220 'ppam' => array('type' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12', 'icon' => 'powerpoint',
221 'groups' => array('presentation')),
222 'ppsx' => array('type' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
223 'icon' => 'powerpoint', 'groups' => array('presentation')),
224 'ppsm' => array('type' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12', 'icon' => 'powerpoint',
225 'groups' => array('presentation')),
226 'ps' => array('type' => 'application/postscript', 'icon' => 'pdf'),
227 'pub' => array('type' => 'application/x-mspublisher', 'icon' => 'publisher', 'groups' => array('presentation')),
229 'qt' => array('type' => 'video/quicktime', 'icon' => 'quicktime',
230 'groups' => array('video', 'web_video'), 'string' => 'video'),
231 'ra' => array('type' => 'audio/x-realaudio-plugin', 'icon' => 'audio',
232 'groups' => array('audio', 'web_audio'), 'string' => 'audio'),
233 'ram' => array('type' => 'audio/x-pn-realaudio-plugin', 'icon' => 'audio',
234 'groups' => array('audio'), 'string' => 'audio'),
235 'rar' => array('type' => 'application/x-rar-compressed', 'icon' => 'archive',
236 'groups' => array('archive'), 'string' => 'archive'),
237 'rhb' => array('type' => 'text/xml', 'icon' => 'markup'),
238 'rm' => array('type' => 'audio/x-pn-realaudio-plugin', 'icon' => 'audio',
239 'groups' => array('audio'), 'string' => 'audio'),
240 'rmvb' => array('type' => 'application/vnd.rn-realmedia-vbr', 'icon' => 'video',
241 'groups' => array('video'), 'string' => 'video'),
242 'rtf' => array('type' => 'text/rtf', 'icon' => 'text', 'groups' => array('document')),
243 'rtx' => array('type' => 'text/richtext', 'icon' => 'text'),
244 'rv' => array('type' => 'audio/x-pn-realaudio-plugin', 'icon' => 'audio',
245 'groups' => array('video'), 'string' => 'video'),
246 'scss' => array('type' => 'text/x-scss', 'icon' => 'text', 'groups' => array('web_file')),
247 'sh' => array('type' => 'application/x-sh', 'icon' => 'sourcecode'),
248 'sit' => array('type' => 'application/x-stuffit', 'icon' => 'archive',
249 'groups' => array('archive'), 'string' => 'archive'),
250 'smi' => array('type' => 'application/smil', 'icon' => 'text'),
251 'smil' => array('type' => 'application/smil', 'icon' => 'text'),
252 'sqt' => array('type' => 'text/xml', 'icon' => 'markup'),
253 'svg' => array('type' => 'image/svg+xml', 'icon' => 'image',
254 'groups' => array('image', 'web_image'), 'string' => 'image'),
255 'svgz' => array('type' => 'image/svg+xml', 'icon' => 'image',
256 'groups' => array('image', 'web_image'), 'string' => 'image'),
257 'swa' => array('type' => 'application/x-director', 'icon' => 'flash'),
258 'swf' => array('type' => 'application/x-shockwave-flash', 'icon' => 'flash', 'groups' => array('video', 'web_video')),
259 'swfl' => array('type' => 'application/x-shockwave-flash', 'icon' => 'flash', 'groups' => array('video', 'web_video')),
261 'sxw' => array('type' => 'application/vnd.sun.xml.writer', 'icon' => 'writer'),
262 'stw' => array('type' => 'application/vnd.sun.xml.writer.template', 'icon' => 'writer'),
263 'sxc' => array('type' => 'application/vnd.sun.xml.calc', 'icon' => 'calc'),
264 'stc' => array('type' => 'application/vnd.sun.xml.calc.template', 'icon' => 'calc'),
265 'sxd' => array('type' => 'application/vnd.sun.xml.draw', 'icon' => 'draw'),
266 'std' => array('type' => 'application/vnd.sun.xml.draw.template', 'icon' => 'draw'),
267 'sxi' => array('type' => 'application/vnd.sun.xml.impress', 'icon' => 'impress', 'groups' => array('presentation')),
268 'sti' => array('type' => 'application/vnd.sun.xml.impress.template', 'icon' => 'impress',
269 'groups' => array('presentation')),
270 'sxg' => array('type' => 'application/vnd.sun.xml.writer.global', 'icon' => 'writer'),
271 'sxm' => array('type' => 'application/vnd.sun.xml.math', 'icon' => 'math'),
273 'tar' => array('type' => 'application/x-tar', 'icon' => 'archive', 'groups' => array('archive'), 'string' => 'archive'),
274 'tif' => array('type' => 'image/tiff', 'icon' => 'tiff', 'groups' => array('image'), 'string' => 'image'),
275 'tiff' => array('type' => 'image/tiff', 'icon' => 'tiff', 'groups' => array('image'), 'string' => 'image'),
276 'tex' => array('type' => 'application/x-tex', 'icon' => 'text'),
277 'texi' => array('type' => 'application/x-texinfo', 'icon' => 'text'),
278 'texinfo' => array('type' => 'application/x-texinfo', 'icon' => 'text'),
279 'ts' => array('type' => 'video/MP2T', 'icon' => 'mpeg', 'groups' => array('video', 'web_video'),
280 'string' => 'video'),
281 'tsv' => array('type' => 'text/tab-separated-values', 'icon' => 'text'),
282 'txt' => array('type' => 'text/plain', 'icon' => 'text', 'defaulticon' => true),
283 'vtt' => array('type' => 'text/vtt', 'icon' => 'text', 'groups' => array('html_track')),
284 'wav' => array('type' => 'audio/wav', 'icon' => 'wav', 'groups' => array('audio', 'html_audio', 'web_audio'),
285 'string' => 'audio'),
286 'webm' => array('type' => 'video/webm', 'icon' => 'video', 'groups' => array('html_video', 'video', 'web_video'),
287 'string' => 'video'),
288 'wmv' => array('type' => 'video/x-ms-wmv', 'icon' => 'wmv', 'groups' => array('video'), 'string' => 'video'),
289 'asf' => array('type' => 'video/x-ms-asf', 'icon' => 'wmv', 'groups' => array('video'), 'string' => 'video'),
290 'wma' => array('type' => 'audio/x-ms-wma', 'icon' => 'audio', 'groups' => array('audio'), 'string' => 'audio'),
292 'xbk' => array('type' => 'application/x-smarttech-notebook', 'icon' => 'archive'),
293 'xdp' => array('type' => 'application/vnd.adobe.xdp+xml', 'icon' => 'pdf'),
294 'xfd' => array('type' => 'application/vnd.xfdl', 'icon' => 'pdf'),
295 'xfdf' => array('type' => 'application/vnd.adobe.xfdf', 'icon' => 'pdf'),
297 'xls' => array('type' => 'application/vnd.ms-excel', 'icon' => 'spreadsheet', 'groups' => array('spreadsheet')),
298 'xlsx' => array('type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'icon' => 'spreadsheet',
299 'groups' => array('spreadsheet')),
300 'xlsm' => array('type' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
301 'icon' => 'spreadsheet', 'groups' => array('spreadsheet')),
302 'xltx' => array('type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
303 'icon' => 'spreadsheet'),
304 'xltm' => array('type' => 'application/vnd.ms-excel.template.macroEnabled.12', 'icon' => 'spreadsheet'),
305 'xlsb' => array('type' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', 'icon' => 'spreadsheet'),
306 'xlam' => array('type' => 'application/vnd.ms-excel.addin.macroEnabled.12', 'icon' => 'spreadsheet'),
308 'xml' => array('type' => 'application/xml', 'icon' => 'markup'),
309 'xsl' => array('type' => 'text/xml', 'icon' => 'markup'),
311 'zip' => array('type' => 'application/zip', 'icon' => 'archive', 'groups' => array('archive'), 'string' => 'archive')
316 * Given a mimetype - return a valid file extension for it.
318 * @param $mimetype string
319 * @return string|bool False if the mimetype was not known, a string indicating a valid file extension otherwise. It may not
320 * be the only valid file extension - just the first one found.
322 public static function get_file_extension($mimetype) {
323 $types = self::get_types();
324 foreach ($types as $extension => $info) {
325 if ($info['type'] == $mimetype) {
326 return $extension;
329 return false;
333 * Gets all the current types.
335 * @return array Associative array from extension to array of data about type
337 public static function &get_types() {
338 // If it was already done in this request, use cache.
339 if (self::$cachedtypes) {
340 return self::$cachedtypes;
343 // Get defaults.
344 $mimetypes = self::get_default_types();
346 // Get custom file types.
347 $custom = self::get_custom_types();
349 // Check value is an array.
350 if (!is_array($custom)) {
351 debugging('Invalid $CFG->customfiletypes (not array)', DEBUG_DEVELOPER);
352 $custom = array();
355 foreach ($custom as $customentry) {
356 // Each entry is a stdClass object similar to the array values above.
357 if (empty($customentry->extension)) {
358 debugging('Invalid $CFG->customfiletypes entry (extension field required)',
359 DEBUG_DEVELOPER);
360 continue;
363 // To delete a standard entry, set 'deleted' to true.
364 if (!empty($customentry->deleted)) {
365 unset($mimetypes[$customentry->extension]);
366 continue;
369 // Check required fields.
370 if (empty($customentry->type) || empty($customentry->icon)) {
371 debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
372 ' (type and icon fields required)', DEBUG_DEVELOPER);
373 continue;
376 // Build result array.
377 $result = array('type' => $customentry->type, 'icon' => $customentry->icon);
378 if (!empty($customentry->groups)) {
379 if (!is_array($customentry->groups)) {
380 debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
381 ' (groups field not array)', DEBUG_DEVELOPER);
382 continue;
384 $result['groups'] = $customentry->groups;
386 if (!empty($customentry->string)) {
387 if (!is_string($customentry->string)) {
388 debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
389 ' (string field not string)', DEBUG_DEVELOPER);
390 continue;
392 $result['string'] = $customentry->string;
394 if (!empty($customentry->defaulticon)) {
395 if (!is_bool($customentry->defaulticon)) {
396 debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
397 ' (defaulticon field not bool)', DEBUG_DEVELOPER);
398 continue;
400 $result['defaulticon'] = $customentry->defaulticon;
402 if (!empty($customentry->customdescription)) {
403 if (!is_string($customentry->customdescription)) {
404 debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
405 ' (customdescription field not string)', DEBUG_DEVELOPER);
406 continue;
408 // As the name suggests, this field is used only for custom entries.
409 $result['customdescription'] = $customentry->customdescription;
412 // Track whether it is a custom filetype or a modified existing
413 // filetype.
414 if (array_key_exists($customentry->extension, $mimetypes)) {
415 $result['modified'] = true;
416 } else {
417 $result['custom'] = true;
420 // Add result array to list.
421 $mimetypes[$customentry->extension] = $result;
424 self::$cachedtypes = $mimetypes;
425 return self::$cachedtypes;
429 * Gets custom types from config variable, after decoding the JSON if required.
431 * @return array Array of custom types (empty array if none)
433 protected static function get_custom_types() {
434 global $CFG;
435 if (!empty($CFG->customfiletypes)) {
436 if (is_array($CFG->customfiletypes)) {
437 // You can define this as an array in config.php...
438 return $CFG->customfiletypes;
439 } else {
440 // Or as a JSON string in the config table.
441 return json_decode($CFG->customfiletypes);
443 } else {
444 return array();
449 * Sets the custom types into config variable, encoding into JSON.
451 * @param array $types Array of custom types
452 * @throws coding_exception If the custom types are fixed in config.php.
454 protected static function set_custom_types(array $types) {
455 global $CFG;
456 // Check the setting hasn't been forced.
457 if (array_key_exists('customfiletypes', $CFG->config_php_settings)) {
458 throw new coding_exception('Cannot set custom filetypes because they ' .
459 'are defined in config.php');
461 if (empty($types)) {
462 unset_config('customfiletypes');
463 } else {
464 set_config('customfiletypes', json_encode(array_values($types)));
467 // Clear the cached type list.
468 self::reset_caches();
472 * Clears the type cache. This is not needed in normal use as the
473 * set_custom_types function automatically clears the cache. Intended for
474 * use in unit tests.
476 public static function reset_caches() {
477 self::$cachedtypes = null;
481 * Gets the default types that have been deleted. Returns an array containing
482 * the defaults of all those types.
484 * @return array Array (same format as get_mimetypes_array)
486 public static function get_deleted_types() {
487 $defaults = self::get_default_types();
488 $deleted = array();
489 foreach (self::get_custom_types() as $customentry) {
490 if (!empty($customentry->deleted)) {
491 $deleted[$customentry->extension] = $defaults[$customentry->extension];
494 return $deleted;
498 * Adds a new entry to the list of custom filetypes.
500 * @param string $extension File extension without dot, e.g. 'doc'
501 * @param string $mimetype MIME type e.g. 'application/msword'
502 * @param string $coreicon Core icon to use e.g. 'document'
503 * @param array $groups Array of group strings that this type belongs to
504 * @param string $corestring Custom lang string name in mimetypes.php
505 * @param string $customdescription Custom description (plain text/multilang)
506 * @param bool $defaulticon True if this should be the default icon for the type
507 * @throws coding_exception If the extension already exists, or otherwise invalid
509 public static function add_type($extension, $mimetype, $coreicon,
510 array $groups = array(), $corestring = '', $customdescription = '',
511 $defaulticon = false) {
512 // Check for blank extensions or incorrectly including the dot.
513 $extension = (string)$extension;
514 if ($extension === '' || $extension[0] === '.') {
515 throw new coding_exception('Invalid extension .' . $extension);
518 // Check extension not already used.
519 $mimetypes = get_mimetypes_array();
520 if (array_key_exists($extension, $mimetypes)) {
521 throw new coding_exception('Extension ' . $extension . ' already exists');
524 // For default icon, check there isn't already something with default icon
525 // set for that MIME type.
526 if ($defaulticon) {
527 foreach ($mimetypes as $type) {
528 if ($type['type'] === $mimetype && !empty($type['defaulticon'])) {
529 throw new coding_exception('MIME type ' . $mimetype .
530 ' already has a default icon set');
535 // Get existing custom filetype list.
536 $customs = self::get_custom_types();
538 // Check if there's a 'deleted' entry for the extension, if so then get
539 // rid of it.
540 foreach ($customs as $key => $custom) {
541 if ($custom->extension === $extension) {
542 unset($customs[$key]);
546 // Set up config record for new type.
547 $newtype = self::create_config_record($extension, $mimetype, $coreicon, $groups,
548 $corestring, $customdescription, $defaulticon);
550 // See if there's a default value with this extension.
551 $needsadding = true;
552 $defaults = self::get_default_types();
553 if (array_key_exists($extension, $defaults)) {
554 // If it has the same values, we don't need to add it.
555 $defaultvalue = $defaults[$extension];
556 $modified = (array)$newtype;
557 unset($modified['extension']);
558 ksort($defaultvalue);
559 ksort($modified);
560 if ($modified === $defaultvalue) {
561 $needsadding = false;
565 // Add to array and set in config.
566 if ($needsadding) {
567 $customs[] = $newtype;
569 self::set_custom_types($customs);
573 * Updates an entry in the list of filetypes in config.
575 * @param string $extension File extension without dot, e.g. 'doc'
576 * @param string $newextension New file extension (same if not changing)
577 * @param string $mimetype MIME type e.g. 'application/msword'
578 * @param string $coreicon Core icon to use e.g. 'document'
579 * @param array $groups Array of group strings that this type belongs to
580 * @param string $corestring Custom lang string name in mimetypes.php
581 * @param string $customdescription Custom description (plain text/multilang)
582 * @param bool $defaulticon True if this should be the default icon for the type
583 * @throws coding_exception If the new extension already exists, or otherwise invalid
585 public static function update_type($extension, $newextension, $mimetype, $coreicon,
586 array $groups = array(), $corestring = '', $customdescription = '',
587 $defaulticon = false) {
589 // Extension must exist.
590 $extension = (string)$extension;
591 $mimetypes = get_mimetypes_array();
592 if (!array_key_exists($extension, $mimetypes)) {
593 throw new coding_exception('Extension ' . $extension . ' not found');
596 // If there's a new extension then this must not exist.
597 $newextension = (string)$newextension;
598 if ($newextension !== $extension) {
599 if ($newextension === '' || $newextension[0] === '.') {
600 throw new coding_exception('Invalid extension .' . $newextension);
602 if (array_key_exists($newextension, $mimetypes)) {
603 throw new coding_exception('Extension ' . $newextension . ' already exists');
607 // For default icon, check there isn't already something with default icon
608 // set for that MIME type (unless it's this).
609 if ($defaulticon) {
610 foreach ($mimetypes as $ext => $type) {
611 if ($ext !== $extension && $type['type'] === $mimetype &&
612 !empty($type['defaulticon'])) {
613 throw new coding_exception('MIME type ' . $mimetype .
614 ' already has a default icon set');
619 // Delete the old extension and then add the new one (may be same). This
620 // will correctly handle cases when a default type is involved.
621 self::delete_type($extension);
622 self::add_type($newextension, $mimetype, $coreicon, $groups, $corestring,
623 $customdescription, $defaulticon);
627 * Deletes a file type from the config list (or, for a standard one, marks it
628 * as deleted).
630 * @param string $extension File extension without dot, e.g. 'doc'
631 * @throws coding_exception If the extension does not exist, or otherwise invalid
633 public static function delete_type($extension) {
634 // Extension must exist.
635 $mimetypes = get_mimetypes_array();
636 if (!array_key_exists($extension, $mimetypes)) {
637 throw new coding_exception('Extension ' . $extension . ' not found');
640 // Get existing custom filetype list.
641 $customs = self::get_custom_types();
643 // Remove any entries for this extension.
644 foreach ($customs as $key => $custom) {
645 if ($custom->extension === $extension && empty($custom->deleted)) {
646 unset($customs[$key]);
650 // If it was a standard entry (doesn't have 'custom' set) then add a
651 // deleted marker.
652 if (empty($mimetypes[$extension]['custom'])) {
653 $customs[] = (object)array('extension' => $extension, 'deleted' => true);
656 // Save and reset cache.
657 self::set_custom_types($customs);
661 * Reverts a file type to the default. May only be called on types that have
662 * default values. This will undelete the type if necessary or set its values.
663 * If the type is already at default values, does nothing.
665 * @param string $extension File extension without dot, e.g. 'doc'
666 * @return bool True if anything was changed, false if it was already default
667 * @throws coding_exception If the extension is not a default type.
669 public static function revert_type_to_default($extension) {
670 $extension = (string)$extension;
672 // Check it actually is a default type.
673 $defaults = self::get_default_types();
674 if (!array_key_exists($extension, $defaults)) {
675 throw new coding_exception('Extension ' . $extension . ' is not a default type');
678 // Loop through all the custom settings.
679 $changed = false;
680 $customs = self::get_custom_types();
681 foreach ($customs as $key => $customentry) {
682 if ($customentry->extension === $extension) {
683 unset($customs[$key]);
684 $changed = true;
688 // Save changes if any.
689 if ($changed) {
690 self::set_custom_types($customs);
692 return $changed;
696 * Converts function parameters into a record for storing in the JSON value.
698 * @param string $extension File extension without dot, e.g. 'doc'
699 * @param string $mimetype MIME type e.g. 'application/msword'
700 * @param string $coreicon Core icon to use e.g. 'document'
701 * @param array $groups Array of group strings that this type belongs to
702 * @param string $corestring Custom lang string name in mimetypes.php
703 * @param string $customdescription Custom description (plain text/multilang)
704 * @param bool $defaulticon True if this should be the default icon for the type
705 * @return stdClass Record matching the parameters
707 protected static function create_config_record($extension, $mimetype,
708 $coreicon, array $groups, $corestring, $customdescription, $defaulticon) {
709 // Construct new entry.
710 $newentry = (object)array('extension' => (string)$extension, 'type' => (string)$mimetype,
711 'icon' => (string)$coreicon);
712 if ($groups) {
713 if (!is_array($groups)) {
714 throw new coding_exception('Groups must be an array');
716 foreach ($groups as $group) {
717 if (!is_string($group)) {
718 throw new coding_exception('Groups must be an array of strings');
721 $newentry->groups = $groups;
723 if ($corestring) {
724 $newentry->string = (string)$corestring;
726 if ($customdescription) {
727 $newentry->customdescription = (string)$customdescription;
729 if ($defaulticon) {
730 $newentry->defaulticon = true;
732 return $newentry;