MDL-68430 filter_mathjaxloader: update default CDN to 2.7.8
[moodle.git] / lib / classes / filetypes.php
blob2d05dbe1b92a4fa0082500dd442655bd7cc6f580
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 // Get custom file types.
341 $custom = self::get_custom_types();
343 // Check value is an array.
344 if (!is_array($custom)) {
345 debugging('Invalid $CFG->customfiletypes (not array)', DEBUG_DEVELOPER);
346 $custom = array();
349 foreach ($custom as $customentry) {
350 // Each entry is a stdClass object similar to the array values above.
351 if (empty($customentry->extension)) {
352 debugging('Invalid $CFG->customfiletypes entry (extension field required)',
353 DEBUG_DEVELOPER);
354 continue;
357 // To delete a standard entry, set 'deleted' to true.
358 if (!empty($customentry->deleted)) {
359 unset($mimetypes[$customentry->extension]);
360 continue;
363 // Check required fields.
364 if (empty($customentry->type) || empty($customentry->icon)) {
365 debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
366 ' (type and icon fields required)', DEBUG_DEVELOPER);
367 continue;
370 // Build result array.
371 $result = array('type' => $customentry->type, 'icon' => $customentry->icon);
372 if (!empty($customentry->groups)) {
373 if (!is_array($customentry->groups)) {
374 debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
375 ' (groups field not array)', DEBUG_DEVELOPER);
376 continue;
378 $result['groups'] = $customentry->groups;
380 if (!empty($customentry->string)) {
381 if (!is_string($customentry->string)) {
382 debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
383 ' (string field not string)', DEBUG_DEVELOPER);
384 continue;
386 $result['string'] = $customentry->string;
388 if (!empty($customentry->defaulticon)) {
389 if (!is_bool($customentry->defaulticon)) {
390 debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
391 ' (defaulticon field not bool)', DEBUG_DEVELOPER);
392 continue;
394 $result['defaulticon'] = $customentry->defaulticon;
396 if (!empty($customentry->customdescription)) {
397 if (!is_string($customentry->customdescription)) {
398 debugging('Invalid $CFG->customfiletypes entry ' . $customentry->extension .
399 ' (customdescription field not string)', DEBUG_DEVELOPER);
400 continue;
402 // As the name suggests, this field is used only for custom entries.
403 $result['customdescription'] = $customentry->customdescription;
406 // Track whether it is a custom filetype or a modified existing
407 // filetype.
408 if (array_key_exists($customentry->extension, $mimetypes)) {
409 $result['modified'] = true;
410 } else {
411 $result['custom'] = true;
414 // Add result array to list.
415 $mimetypes[$customentry->extension] = $result;
418 self::$cachedtypes = $mimetypes;
419 return self::$cachedtypes;
423 * Gets custom types from config variable, after decoding the JSON if required.
425 * @return array Array of custom types (empty array if none)
427 protected static function get_custom_types() {
428 global $CFG;
429 if (!empty($CFG->customfiletypes)) {
430 if (is_array($CFG->customfiletypes)) {
431 // You can define this as an array in config.php...
432 return $CFG->customfiletypes;
433 } else {
434 // Or as a JSON string in the config table.
435 return json_decode($CFG->customfiletypes);
437 } else {
438 return array();
443 * Sets the custom types into config variable, encoding into JSON.
445 * @param array $types Array of custom types
446 * @throws coding_exception If the custom types are fixed in config.php.
448 protected static function set_custom_types(array $types) {
449 global $CFG;
450 // Check the setting hasn't been forced.
451 if (array_key_exists('customfiletypes', $CFG->config_php_settings)) {
452 throw new coding_exception('Cannot set custom filetypes because they ' .
453 'are defined in config.php');
455 if (empty($types)) {
456 unset_config('customfiletypes');
457 } else {
458 set_config('customfiletypes', json_encode(array_values($types)));
461 // Clear the cached type list.
462 self::reset_caches();
466 * Clears the type cache. This is not needed in normal use as the
467 * set_custom_types function automatically clears the cache. Intended for
468 * use in unit tests.
470 public static function reset_caches() {
471 self::$cachedtypes = null;
475 * Gets the default types that have been deleted. Returns an array containing
476 * the defaults of all those types.
478 * @return array Array (same format as get_mimetypes_array)
480 public static function get_deleted_types() {
481 $defaults = self::get_default_types();
482 $deleted = array();
483 foreach (self::get_custom_types() as $customentry) {
484 if (!empty($customentry->deleted)) {
485 $deleted[$customentry->extension] = $defaults[$customentry->extension];
488 return $deleted;
492 * Adds a new entry to the list of custom filetypes.
494 * @param string $extension File extension without dot, e.g. 'doc'
495 * @param string $mimetype MIME type e.g. 'application/msword'
496 * @param string $coreicon Core icon to use e.g. 'document'
497 * @param array $groups Array of group strings that this type belongs to
498 * @param string $corestring Custom lang string name in mimetypes.php
499 * @param string $customdescription Custom description (plain text/multilang)
500 * @param bool $defaulticon True if this should be the default icon for the type
501 * @throws coding_exception If the extension already exists, or otherwise invalid
503 public static function add_type($extension, $mimetype, $coreicon,
504 array $groups = array(), $corestring = '', $customdescription = '',
505 $defaulticon = false) {
506 // Check for blank extensions or incorrectly including the dot.
507 $extension = (string)$extension;
508 if ($extension === '' || $extension[0] === '.') {
509 throw new coding_exception('Invalid extension .' . $extension);
512 // Check extension not already used.
513 $mimetypes = get_mimetypes_array();
514 if (array_key_exists($extension, $mimetypes)) {
515 throw new coding_exception('Extension ' . $extension . ' already exists');
518 // For default icon, check there isn't already something with default icon
519 // set for that MIME type.
520 if ($defaulticon) {
521 foreach ($mimetypes as $type) {
522 if ($type['type'] === $mimetype && !empty($type['defaulticon'])) {
523 throw new coding_exception('MIME type ' . $mimetype .
524 ' already has a default icon set');
529 // Get existing custom filetype list.
530 $customs = self::get_custom_types();
532 // Check if there's a 'deleted' entry for the extension, if so then get
533 // rid of it.
534 foreach ($customs as $key => $custom) {
535 if ($custom->extension === $extension) {
536 unset($customs[$key]);
540 // Set up config record for new type.
541 $newtype = self::create_config_record($extension, $mimetype, $coreicon, $groups,
542 $corestring, $customdescription, $defaulticon);
544 // See if there's a default value with this extension.
545 $needsadding = true;
546 $defaults = self::get_default_types();
547 if (array_key_exists($extension, $defaults)) {
548 // If it has the same values, we don't need to add it.
549 $defaultvalue = $defaults[$extension];
550 $modified = (array)$newtype;
551 unset($modified['extension']);
552 ksort($defaultvalue);
553 ksort($modified);
554 if ($modified === $defaultvalue) {
555 $needsadding = false;
559 // Add to array and set in config.
560 if ($needsadding) {
561 $customs[] = $newtype;
563 self::set_custom_types($customs);
567 * Updates an entry in the list of filetypes in config.
569 * @param string $extension File extension without dot, e.g. 'doc'
570 * @param string $newextension New file extension (same if not changing)
571 * @param string $mimetype MIME type e.g. 'application/msword'
572 * @param string $coreicon Core icon to use e.g. 'document'
573 * @param array $groups Array of group strings that this type belongs to
574 * @param string $corestring Custom lang string name in mimetypes.php
575 * @param string $customdescription Custom description (plain text/multilang)
576 * @param bool $defaulticon True if this should be the default icon for the type
577 * @throws coding_exception If the new extension already exists, or otherwise invalid
579 public static function update_type($extension, $newextension, $mimetype, $coreicon,
580 array $groups = array(), $corestring = '', $customdescription = '',
581 $defaulticon = false) {
583 // Extension must exist.
584 $extension = (string)$extension;
585 $mimetypes = get_mimetypes_array();
586 if (!array_key_exists($extension, $mimetypes)) {
587 throw new coding_exception('Extension ' . $extension . ' not found');
590 // If there's a new extension then this must not exist.
591 $newextension = (string)$newextension;
592 if ($newextension !== $extension) {
593 if ($newextension === '' || $newextension[0] === '.') {
594 throw new coding_exception('Invalid extension .' . $newextension);
596 if (array_key_exists($newextension, $mimetypes)) {
597 throw new coding_exception('Extension ' . $newextension . ' already exists');
601 // For default icon, check there isn't already something with default icon
602 // set for that MIME type (unless it's this).
603 if ($defaulticon) {
604 foreach ($mimetypes as $ext => $type) {
605 if ($ext !== $extension && $type['type'] === $mimetype &&
606 !empty($type['defaulticon'])) {
607 throw new coding_exception('MIME type ' . $mimetype .
608 ' already has a default icon set');
613 // Delete the old extension and then add the new one (may be same). This
614 // will correctly handle cases when a default type is involved.
615 self::delete_type($extension);
616 self::add_type($newextension, $mimetype, $coreicon, $groups, $corestring,
617 $customdescription, $defaulticon);
621 * Deletes a file type from the config list (or, for a standard one, marks it
622 * as deleted).
624 * @param string $extension File extension without dot, e.g. 'doc'
625 * @throws coding_exception If the extension does not exist, or otherwise invalid
627 public static function delete_type($extension) {
628 // Extension must exist.
629 $mimetypes = get_mimetypes_array();
630 if (!array_key_exists($extension, $mimetypes)) {
631 throw new coding_exception('Extension ' . $extension . ' not found');
634 // Get existing custom filetype list.
635 $customs = self::get_custom_types();
637 // Remove any entries for this extension.
638 foreach ($customs as $key => $custom) {
639 if ($custom->extension === $extension && empty($custom->deleted)) {
640 unset($customs[$key]);
644 // If it was a standard entry (doesn't have 'custom' set) then add a
645 // deleted marker.
646 if (empty($mimetypes[$extension]['custom'])) {
647 $customs[] = (object)array('extension' => $extension, 'deleted' => true);
650 // Save and reset cache.
651 self::set_custom_types($customs);
655 * Reverts a file type to the default. May only be called on types that have
656 * default values. This will undelete the type if necessary or set its values.
657 * If the type is already at default values, does nothing.
659 * @param string $extension File extension without dot, e.g. 'doc'
660 * @return bool True if anything was changed, false if it was already default
661 * @throws coding_exception If the extension is not a default type.
663 public static function revert_type_to_default($extension) {
664 $extension = (string)$extension;
666 // Check it actually is a default type.
667 $defaults = self::get_default_types();
668 if (!array_key_exists($extension, $defaults)) {
669 throw new coding_exception('Extension ' . $extension . ' is not a default type');
672 // Loop through all the custom settings.
673 $changed = false;
674 $customs = self::get_custom_types();
675 foreach ($customs as $key => $customentry) {
676 if ($customentry->extension === $extension) {
677 unset($customs[$key]);
678 $changed = true;
682 // Save changes if any.
683 if ($changed) {
684 self::set_custom_types($customs);
686 return $changed;
690 * Converts function parameters into a record for storing in the JSON value.
692 * @param string $extension File extension without dot, e.g. 'doc'
693 * @param string $mimetype MIME type e.g. 'application/msword'
694 * @param string $coreicon Core icon to use e.g. 'document'
695 * @param array $groups Array of group strings that this type belongs to
696 * @param string $corestring Custom lang string name in mimetypes.php
697 * @param string $customdescription Custom description (plain text/multilang)
698 * @param bool $defaulticon True if this should be the default icon for the type
699 * @return stdClass Record matching the parameters
701 protected static function create_config_record($extension, $mimetype,
702 $coreicon, array $groups, $corestring, $customdescription, $defaulticon) {
703 // Construct new entry.
704 $newentry = (object)array('extension' => (string)$extension, 'type' => (string)$mimetype,
705 'icon' => (string)$coreicon);
706 if ($groups) {
707 if (!is_array($groups)) {
708 throw new coding_exception('Groups must be an array');
710 foreach ($groups as $group) {
711 if (!is_string($group)) {
712 throw new coding_exception('Groups must be an array of strings');
715 $newentry->groups = $groups;
717 if ($corestring) {
718 $newentry->string = (string)$corestring;
720 if ($customdescription) {
721 $newentry->customdescription = (string)$customdescription;
723 if ($defaulticon) {
724 $newentry->defaulticon = true;
726 return $newentry;