Added the higer level check_moodle_environment()
[moodle.git] / lib / filelib.php
CommitLineData
599f38f9 1<?php //$Id$
2
f1e0649c 3function mimeinfo($element, $filename) {
4 $mimeinfo = array (
a370c895 5 'xxx' => array ('type'=>'document/unknown', 'icon'=>'unknown.gif'),
6 '3gp' => array ('type'=>'video/quicktime', 'icon'=>'video.gif'),
7 'ai' => array ('type'=>'application/postscript', 'icon'=>'image.gif'),
8 'aif' => array ('type'=>'audio/x-aiff', 'icon'=>'audio.gif'),
9 'aiff' => array ('type'=>'audio/x-aiff', 'icon'=>'audio.gif'),
10 'aifc' => array ('type'=>'audio/x-aiff', 'icon'=>'audio.gif'),
11 'applescript' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
12 'asc' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
18bf47ef 13 'asm' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
a370c895 14 'au' => array ('type'=>'audio/au', 'icon'=>'audio.gif'),
15 'avi' => array ('type'=>'video/x-ms-wm', 'icon'=>'avi.gif'),
16 'bmp' => array ('type'=>'image/bmp', 'icon'=>'image.gif'),
18bf47ef 17 'c' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
a370c895 18 'cct' => array ('type'=>'shockwave/director', 'icon'=>'flash.gif'),
18bf47ef 19 'cpp' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
a370c895 20 'cs' => array ('type'=>'application/x-csh', 'icon'=>'text.gif'),
21 'css' => array ('type'=>'text/css', 'icon'=>'text.gif'),
22 'dv' => array ('type'=>'video/x-dv', 'icon'=>'video.gif'),
609d84e3 23 'dmg' => array ('type'=>'application/octet-stream', 'icon'=>'dmg.gif'),
a370c895 24 'doc' => array ('type'=>'application/msword', 'icon'=>'word.gif'),
25 'dcr' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'),
26 'dif' => array ('type'=>'video/x-dv', 'icon'=>'video.gif'),
27 'dir' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'),
28 'dxr' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'),
29 'eps' => array ('type'=>'application/postscript', 'icon'=>'pdf.gif'),
30 'gif' => array ('type'=>'image/gif', 'icon'=>'image.gif'),
31 'gtar' => array ('type'=>'application/x-gtar', 'icon'=>'zip.gif'),
609d84e3 32 'tgz' => array ('type'=>'application/g-zip', 'icon'=>'zip.gif'),
a370c895 33 'gz' => array ('type'=>'application/g-zip', 'icon'=>'zip.gif'),
34 'gzip' => array ('type'=>'application/g-zip', 'icon'=>'zip.gif'),
35 'h' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
18bf47ef 36 'hpp' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
a370c895 37 'hqx' => array ('type'=>'application/mac-binhex40', 'icon'=>'zip.gif'),
38 'html' => array ('type'=>'text/html', 'icon'=>'html.gif'),
39 'htm' => array ('type'=>'text/html', 'icon'=>'html.gif'),
18bf47ef 40 'java' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
a00420fb 41 'jcb' => array ('type'=>'text/xml', 'icon'=>'jcb.gif'),
42 'jcl' => array ('type'=>'text/xml', 'icon'=>'jcl.gif'),
43 'jcw' => array ('type'=>'text/xml', 'icon'=>'jcw.gif'),
44 'jmt' => array ('type'=>'text/xml', 'icon'=>'jmt.gif'),
45 'jmx' => array ('type'=>'text/xml', 'icon'=>'jmx.gif'),
a370c895 46 'jpe' => array ('type'=>'image/jpeg', 'icon'=>'image.gif'),
47 'jpeg' => array ('type'=>'image/jpeg', 'icon'=>'image.gif'),
48 'jpg' => array ('type'=>'image/jpeg', 'icon'=>'image.gif'),
a00420fb 49 'jqz' => array ('type'=>'text/xml', 'icon'=>'jqz.gif'),
a370c895 50 'js' => array ('type'=>'application/x-javascript', 'icon'=>'text.gif'),
51 'latex'=> array ('type'=>'application/x-latex', 'icon'=>'text.gif'),
52 'm' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
53 'mov' => array ('type'=>'video/quicktime', 'icon'=>'video.gif'),
54 'movie'=> array ('type'=>'video/x-sgi-movie', 'icon'=>'video.gif'),
55 'm3u' => array ('type'=>'audio/x-mpegurl', 'icon'=>'audio.gif'),
56 'mp3' => array ('type'=>'audio/mp3', 'icon'=>'audio.gif'),
57 'mp4' => array ('type'=>'video/mp4', 'icon'=>'video.gif'),
58 'mpeg' => array ('type'=>'video/mpeg', 'icon'=>'video.gif'),
59 'mpe' => array ('type'=>'video/mpeg', 'icon'=>'video.gif'),
60 'mpg' => array ('type'=>'video/mpeg', 'icon'=>'video.gif'),
5395334d 61
62 'odt' => array ('type'=>'application/vnd.oasis.opendocument.text', 'icon'=>'odt.gif'),
63 'ott' => array ('type'=>'application/vnd.oasis.opendocument.text-template', 'icon'=>'odt.gif'),
64 'oth' => array ('type'=>'application/vnd.oasis.opendocument.text-web', 'icon'=>'odt.gif'),
65 'odm' => array ('type'=>'application/vnd.oasis.opendocument.text-master', 'icon'=>'odt.gif'),
66 'odg' => array ('type'=>'application/vnd.oasis.opendocument.graphics', 'icon'=>'odt.gif'),
67 'otg' => array ('type'=>'application/vnd.oasis.opendocument.graphics-template', 'icon'=>'odt.gif'),
68 'odp' => array ('type'=>'application/vnd.oasis.opendocument.presentation', 'icon'=>'odt.gif'),
69 'otp' => array ('type'=>'application/vnd.oasis.opendocument.presentation-template', 'icon'=>'odt.gif'),
70 'ods' => array ('type'=>'application/vnd.oasis.opendocument.spreadsheet', 'icon'=>'odt.gif'),
71 'ots' => array ('type'=>'application/vnd.oasis.opendocument.spreadsheet-template', 'icon'=>'odt.gif'),
72 'odc' => array ('type'=>'application/vnd.oasis.opendocument.chart', 'icon'=>'odt.gif'),
73 'odf' => array ('type'=>'application/vnd.oasis.opendocument.formula', 'icon'=>'odt.gif'),
74 'odb' => array ('type'=>'application/vnd.oasis.opendocument.database', 'icon'=>'odt.gif'),
75 'odi' => array ('type'=>'application/vnd.oasis.opendocument.image', 'icon'=>'odt.gif'),
76
a370c895 77 'pct' => array ('type'=>'image/pict', 'icon'=>'image.gif'),
78 'pdf' => array ('type'=>'application/pdf', 'icon'=>'pdf.gif'),
79 'php' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
80 'pic' => array ('type'=>'image/pict', 'icon'=>'image.gif'),
81 'pict' => array ('type'=>'image/pict', 'icon'=>'image.gif'),
82 'png' => array ('type'=>'image/png', 'icon'=>'image.gif'),
83 'pps' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'powerpoint.gif'),
84 'ppt' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'powerpoint.gif'),
85 'ps' => array ('type'=>'application/postscript', 'icon'=>'pdf.gif'),
86 'qt' => array ('type'=>'video/quicktime', 'icon'=>'video.gif'),
87 'ra' => array ('type'=>'audio/x-realaudio', 'icon'=>'audio.gif'),
88 'ram' => array ('type'=>'audio/x-pn-realaudio', 'icon'=>'audio.gif'),
a00420fb 89 'rhb' => array ('type'=>'text/xml', 'icon'=>'xml.gif'),
a370c895 90 'rm' => array ('type'=>'audio/x-pn-realaudio', 'icon'=>'audio.gif'),
91 'rtf' => array ('type'=>'text/rtf', 'icon'=>'text.gif'),
92 'rtx' => array ('type'=>'text/richtext', 'icon'=>'text.gif'),
93 'sh' => array ('type'=>'application/x-sh', 'icon'=>'text.gif'),
94 'sit' => array ('type'=>'application/x-stuffit', 'icon'=>'zip.gif'),
95 'smi' => array ('type'=>'application/smil', 'icon'=>'text.gif'),
96 'smil' => array ('type'=>'application/smil', 'icon'=>'text.gif'),
a00420fb 97 'sqt' => array ('type'=>'text/xml', 'icon'=>'xml.gif'),
a370c895 98 'swa' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'),
99 'swf' => array ('type'=>'application/x-shockwave-flash', 'icon'=>'flash.gif'),
100 'swfl' => array ('type'=>'application/x-shockwave-flash', 'icon'=>'flash.gif'),
5395334d 101
102 'sxw' => array ('type'=>'application/vnd.sun.xml.writer', 'icon'=>'odt.gif'),
103 'stw' => array ('type'=>'application/vnd.sun.xml.writer.template', 'icon'=>'odt.gif'),
104 'sxc' => array ('type'=>'application/vnd.sun.xml.calc', 'icon'=>'odt.gif'),
105 'stc' => array ('type'=>'application/vnd.sun.xml.calc.template', 'icon'=>'odt.gif'),
106 'sxd' => array ('type'=>'application/vnd.sun.xml.draw', 'icon'=>'odt.gif'),
107 'std' => array ('type'=>'application/vnd.sun.xml.draw.template', 'icon'=>'odt.gif'),
108 'sxi' => array ('type'=>'application/vnd.sun.xml.impress', 'icon'=>'odt.gif'),
109 'sti' => array ('type'=>'application/vnd.sun.xml.impress.template', 'icon'=>'odt.gif'),
110 'sxg' => array ('type'=>'application/vnd.sun.xml.writer.global', 'icon'=>'odt.gif'),
111 'sxm' => array ('type'=>'application/vnd.sun.xml.math', 'icon'=>'odt.gif'),
112
a370c895 113 'tar' => array ('type'=>'application/x-tar', 'icon'=>'zip.gif'),
114 'tif' => array ('type'=>'image/tiff', 'icon'=>'image.gif'),
115 'tiff' => array ('type'=>'image/tiff', 'icon'=>'image.gif'),
116 'tex' => array ('type'=>'application/x-tex', 'icon'=>'text.gif'),
117 'texi' => array ('type'=>'application/x-texinfo', 'icon'=>'text.gif'),
118 'texinfo' => array ('type'=>'application/x-texinfo', 'icon'=>'text.gif'),
119 'tsv' => array ('type'=>'text/tab-separated-values', 'icon'=>'text.gif'),
120 'txt' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
121 'wav' => array ('type'=>'audio/wav', 'icon'=>'audio.gif'),
122 'wmv' => array ('type'=>'video/x-ms-wmv', 'icon'=>'avi.gif'),
123 'asf' => array ('type'=>'video/x-ms-asf', 'icon'=>'avi.gif'),
124 'xls' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'excel.gif'),
125 'xml' => array ('type'=>'application/xml', 'icon'=>'xml.gif'),
126 'xsl' => array ('type'=>'text/xml', 'icon'=>'xml.gif'),
127 'zip' => array ('type'=>'application/zip', 'icon'=>'zip.gif')
f1e0649c 128 );
129
a370c895 130 if (eregi('\.([a-z0-9]+)$', $filename, $match)) {
f1e0649c 131 if (isset($mimeinfo[strtolower($match[1])][$element])) {
132 return $mimeinfo[strtolower($match[1])][$element];
133 } else {
a370c895 134 return $mimeinfo['xxx'][$element]; // By default
f1e0649c 135 }
136 } else {
a370c895 137 return $mimeinfo['xxx'][$element]; // By default
f1e0649c 138 }
139}
140
69faecce 141function send_file($path, $filename, $lifetime=86400 , $filter=false, $pathisstring=false,$forcedownload=false) {
85e00626 142 global $CFG;
f1e0649c 143
9b76b451 144 $mimetype = $forcedownload ? 'application/x-forcedownload' : mimeinfo('type', $filename);
f1e0649c 145 $lastmodified = $pathisstring ? time() : filemtime($path);
146 $filesize = $pathisstring ? strlen($path) : filesize($path);
147
69faecce 148 //IE compatibiltiy HACK!
149 if(ini_get('zlib.output_compression')) {
150 ini_set('zlib.output_compression', 'Off');
151 }
152
a370c895 153 @header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
f1e0649c 154 if ($lifetime > 0) {
155 @header('Cache-control: max-age='.$lifetime);
a370c895 156 @header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .'GMT');
f1e0649c 157 @header('Pragma: ');
158 } else {
85e00626 159 if (strpos($CFG->wwwroot, 'https://') === 0) { //https sites - watch out for IE! KB812935 and KB316431
160 @header('Cache-Control: max-age=10');
161 @header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
162 @header('Pragma: ');
163 } else { //normal http - prevent caching at all cost
164 @header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
165 @header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
166 @header('Pragma: no-cache');
167 }
f1e0649c 168 }
69faecce 169 @header('Accept-Ranges: none'); // Comment out if PDFs do not work...
170
171 if ($forcedownload) {
172 @header('Content-disposition: attachment; filename='.$filename);
173 } else {
174 @header('Content-disposition: inline; filename='.$filename);
175 }
f1e0649c 176
177 if (!$filter) {
178 @header('Content-length: '.$filesize);
179 if ($mimetype == 'text/plain') {
6aaa17c7 180 @header('Content-type: text/plain; charset='.current_charset()); //add encoding
f1e0649c 181 } else {
182 @header('Content-type: '.$mimetype);
183 }
184 if ($pathisstring) {
185 echo $path;
186 }else {
69faecce 187 readfile_chunked($path);
f1e0649c 188 }
189 } else { // Try to put the file through filters
a370c895 190 global $course; // HACK!
f1e0649c 191 if (!empty($course->id)) {
192 $courseid = $course->id;
193 } else {
194 $courseid = SITEID;
195 }
196 if ($mimetype == 'text/html') {
197 $options->noclean = true;
198 $text = $pathisstring ? $path : implode('', file($path));
199 $output = format_text($text, FORMAT_HTML, $options, $courseid);
200
201 @header('Content-length: '.strlen($output));
202 @header('Content-type: text/html');
203 echo $output;
204 } else if ($mimetype == 'text/plain') {
205 $options->newlines = false;
206 $options->noclean = true;
207 $text = htmlentities($pathisstring ? $path : implode('', file($path)));
208 $output = '<pre>'. format_text($text, FORMAT_MOODLE, $options, $courseid) .'</pre>';
209
210 @header('Content-length: '.strlen($output));
6aaa17c7 211 @header('Content-type: text/html; charset='. current_charset()); //add encoding
f1e0649c 212 echo $output;
213 } else { // Just send it out raw
214 @header('Content-length: '.$filesize);
215 @header('Content-type: '.$mimetype);
216 if ($pathisstring) {
217 echo $path;
218 }else {
69faecce 219 readfile_chunked($path);
f1e0649c 220 }
221 }
222 }
223 die; //no more chars to output!!!
224}
225
a43b5308 226function get_records_csv($file, $table) {
599f38f9 227 global $CFG, $db;
228
229 if (!$metacolumns = $db->MetaColumns($CFG->prefix . $table)) {
230 return false;
231 }
232
a77b98eb 233 if(!($handle = @fopen($file, 'r'))) {
599f38f9 234 error('get_records_csv failed to open '.$file);
235 }
236
237 $fieldnames = fgetcsv($handle, 4096);
238 if(empty($fieldnames)) {
239 fclose($handle);
240 return false;
241 }
242
243 $columns = array();
244
245 foreach($metacolumns as $metacolumn) {
246 $ord = array_search($metacolumn->name, $fieldnames);
247 if(is_int($ord)) {
248 $columns[$metacolumn->name] = $ord;
249 }
250 }
251
252 $rows = array();
253
254 while (($data = fgetcsv($handle, 4096)) !== false) {
255 $item = new stdClass;
256 foreach($columns as $name => $ord) {
257 $item->$name = $data[$ord];
258 }
259 $rows[] = $item;
260 }
261
262 fclose($handle);
263 return $rows;
264}
265
a77b98eb 266function put_records_csv($file, $records, $table = NULL) {
267 global $CFG, $db;
268
a1e93da2 269 if (empty($records)) {
a77b98eb 270 return true;
271 }
272
273 $metacolumns = NULL;
274 if ($table !== NULL && !$metacolumns = $db->MetaColumns($CFG->prefix . $table)) {
275 return false;
276 }
277
a1e93da2 278 echo "x";
279
a77b98eb 280 if(!($fp = @fopen($CFG->dataroot.'/temp/'.$file, 'w'))) {
281 error('put_records_csv failed to open '.$file);
282 }
283
a43b5308 284 $proto = reset($records);
285 if(is_object($proto)) {
286 $fields_records = array_keys(get_object_vars($proto));
287 }
288 else if(is_array($proto)) {
289 $fields_records = array_keys($proto);
290 }
291 else {
292 return false;
293 }
a1e93da2 294 echo "x";
a77b98eb 295
296 if(!empty($metacolumns)) {
297 $fields_table = array_map(create_function('$a', 'return $a->name;'), $metacolumns);
298 $fields = array_intersect($fields_records, $fields_table);
299 }
300 else {
301 $fields = $fields_records;
302 }
303
304 fwrite($fp, implode(',', $fields));
305 fwrite($fp, "\r\n");
306
307 foreach($records as $record) {
a43b5308 308 $array = (array)$record;
a77b98eb 309 $values = array();
310 foreach($fields as $field) {
a43b5308 311 if(strpos($array[$field], ',')) {
312 $values[] = '"'.str_replace('"', '\"', $array[$field]).'"';
a77b98eb 313 }
314 else {
a43b5308 315 $values[] = $array[$field];
a77b98eb 316 }
317 }
318 fwrite($fp, implode(',', $values)."\r\n");
319 }
320
321 fclose($fp);
322 return true;
323}
324
f401cc97 325
7ab8bb71 326if (!function_exists('file_get_contents')) {
327 function file_get_contents($file) {
328 $file = file($file);
329 return !$file ? false : implode('', $file);
330 }
331}
332
f401cc97 333
334function fulldelete($location) {
335 if (is_dir($location)) {
336 $currdir = opendir($location);
337 while (false !== ($file = readdir($currdir))) {
338 if ($file <> ".." && $file <> ".") {
339 $fullfile = $location."/".$file;
340 if (is_dir($fullfile)) {
341 if (!fulldelete($fullfile)) {
342 return false;
343 }
344 } else {
345 if (!unlink($fullfile)) {
346 return false;
347 }
348 }
349 }
350 }
351 closedir($currdir);
352 if (! rmdir($location)) {
353 return false;
354 }
355
356 } else {
357 if (!unlink($location)) {
358 return false;
359 }
360 }
361 return true;
362}
363
69faecce 364function readfile_chunked($filename,$retbytes=true) {
365 $chunksize = 1*(1024*1024); // 1MB chunks
366 $buffer = '';
367 $cnt =0;// $handle = fopen($filename, 'rb');
368 $handle = fopen($filename, 'rb');
369 if ($handle === false) {
370 return false;
371 }
20371063 372
373 // don't timeout on slow clients
374 @set_time_limit(0);
375
69faecce 376 while (!feof($handle)) {
377 $buffer = fread($handle, $chunksize);
20371063 378
379 // write to the client and flush buffers
380 // to avoid hogging memory
69faecce 381 echo $buffer;
20371063 382 flush();
383 if (ob_get_level() > 0) {
384 ob_flush();
385 }
386
69faecce 387 if ($retbytes) {
388 $cnt += strlen($buffer);}
389 }
390 $status = fclose($handle);
391 if ($retbytes && $status) {
392 return $cnt; // return num. bytes delivered like readfile() does.
393 }
394 return $status;
395}
396
f401cc97 397
a77b98eb 398?>