First version of the IMS CP resource module.
[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) {
f1e0649c 142
9b76b451 143 $mimetype = $forcedownload ? 'application/x-forcedownload' : mimeinfo('type', $filename);
f1e0649c 144 $lastmodified = $pathisstring ? time() : filemtime($path);
145 $filesize = $pathisstring ? strlen($path) : filesize($path);
146
69faecce 147 //IE compatibiltiy HACK!
148 if(ini_get('zlib.output_compression')) {
149 ini_set('zlib.output_compression', 'Off');
150 }
151
a370c895 152 @header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
f1e0649c 153 if ($lifetime > 0) {
154 @header('Cache-control: max-age='.$lifetime);
a370c895 155 @header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .'GMT');
f1e0649c 156 @header('Pragma: ');
157 } else {
69faecce 158 // This part is tricky, displaying of MS Office documents in IE needs
159 // to store the file on disk, but no-cache may prevent it.
160 // HTTPS:// sites might have problems with following code in IE, tweak it yourself if needed ;-)
f1e0649c 161 @header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=10');
a370c895 162 @header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
f1e0649c 163 @header('Pragma: no-cache');
164 }
69faecce 165 @header('Accept-Ranges: none'); // Comment out if PDFs do not work...
166
167 if ($forcedownload) {
168 @header('Content-disposition: attachment; filename='.$filename);
169 } else {
170 @header('Content-disposition: inline; filename='.$filename);
171 }
f1e0649c 172
173 if (!$filter) {
174 @header('Content-length: '.$filesize);
175 if ($mimetype == 'text/plain') {
176 @header('Content-type: text/plain; charset='.get_string('thischarset')); //add encoding
177 } else {
178 @header('Content-type: '.$mimetype);
179 }
180 if ($pathisstring) {
181 echo $path;
182 }else {
69faecce 183 readfile_chunked($path);
f1e0649c 184 }
185 } else { // Try to put the file through filters
a370c895 186 global $course; // HACK!
f1e0649c 187 if (!empty($course->id)) {
188 $courseid = $course->id;
189 } else {
190 $courseid = SITEID;
191 }
192 if ($mimetype == 'text/html') {
193 $options->noclean = true;
194 $text = $pathisstring ? $path : implode('', file($path));
195 $output = format_text($text, FORMAT_HTML, $options, $courseid);
196
197 @header('Content-length: '.strlen($output));
198 @header('Content-type: text/html');
199 echo $output;
200 } else if ($mimetype == 'text/plain') {
201 $options->newlines = false;
202 $options->noclean = true;
203 $text = htmlentities($pathisstring ? $path : implode('', file($path)));
204 $output = '<pre>'. format_text($text, FORMAT_MOODLE, $options, $courseid) .'</pre>';
205
206 @header('Content-length: '.strlen($output));
207 @header('Content-type: text/html; charset='. get_string('thischarset')); //add encoding
208 echo $output;
209 } else { // Just send it out raw
210 @header('Content-length: '.$filesize);
211 @header('Content-type: '.$mimetype);
212 if ($pathisstring) {
213 echo $path;
214 }else {
69faecce 215 readfile_chunked($path);
f1e0649c 216 }
217 }
218 }
219 die; //no more chars to output!!!
220}
221
a43b5308 222function get_records_csv($file, $table) {
599f38f9 223 global $CFG, $db;
224
225 if (!$metacolumns = $db->MetaColumns($CFG->prefix . $table)) {
226 return false;
227 }
228
a77b98eb 229 if(!($handle = @fopen($file, 'r'))) {
599f38f9 230 error('get_records_csv failed to open '.$file);
231 }
232
233 $fieldnames = fgetcsv($handle, 4096);
234 if(empty($fieldnames)) {
235 fclose($handle);
236 return false;
237 }
238
239 $columns = array();
240
241 foreach($metacolumns as $metacolumn) {
242 $ord = array_search($metacolumn->name, $fieldnames);
243 if(is_int($ord)) {
244 $columns[$metacolumn->name] = $ord;
245 }
246 }
247
248 $rows = array();
249
250 while (($data = fgetcsv($handle, 4096)) !== false) {
251 $item = new stdClass;
252 foreach($columns as $name => $ord) {
253 $item->$name = $data[$ord];
254 }
255 $rows[] = $item;
256 }
257
258 fclose($handle);
259 return $rows;
260}
261
a77b98eb 262function put_records_csv($file, $records, $table = NULL) {
263 global $CFG, $db;
264
a1e93da2 265 if (empty($records)) {
a77b98eb 266 return true;
267 }
268
269 $metacolumns = NULL;
270 if ($table !== NULL && !$metacolumns = $db->MetaColumns($CFG->prefix . $table)) {
271 return false;
272 }
273
a1e93da2 274 echo "x";
275
a77b98eb 276 if(!($fp = @fopen($CFG->dataroot.'/temp/'.$file, 'w'))) {
277 error('put_records_csv failed to open '.$file);
278 }
279
a43b5308 280 $proto = reset($records);
281 if(is_object($proto)) {
282 $fields_records = array_keys(get_object_vars($proto));
283 }
284 else if(is_array($proto)) {
285 $fields_records = array_keys($proto);
286 }
287 else {
288 return false;
289 }
a1e93da2 290 echo "x";
a77b98eb 291
292 if(!empty($metacolumns)) {
293 $fields_table = array_map(create_function('$a', 'return $a->name;'), $metacolumns);
294 $fields = array_intersect($fields_records, $fields_table);
295 }
296 else {
297 $fields = $fields_records;
298 }
299
300 fwrite($fp, implode(',', $fields));
301 fwrite($fp, "\r\n");
302
303 foreach($records as $record) {
a43b5308 304 $array = (array)$record;
a77b98eb 305 $values = array();
306 foreach($fields as $field) {
a43b5308 307 if(strpos($array[$field], ',')) {
308 $values[] = '"'.str_replace('"', '\"', $array[$field]).'"';
a77b98eb 309 }
310 else {
a43b5308 311 $values[] = $array[$field];
a77b98eb 312 }
313 }
314 fwrite($fp, implode(',', $values)."\r\n");
315 }
316
317 fclose($fp);
318 return true;
319}
320
f401cc97 321
7ab8bb71 322if (!function_exists('file_get_contents')) {
323 function file_get_contents($file) {
324 $file = file($file);
325 return !$file ? false : implode('', $file);
326 }
327}
328
f401cc97 329
330function fulldelete($location) {
331 if (is_dir($location)) {
332 $currdir = opendir($location);
333 while (false !== ($file = readdir($currdir))) {
334 if ($file <> ".." && $file <> ".") {
335 $fullfile = $location."/".$file;
336 if (is_dir($fullfile)) {
337 if (!fulldelete($fullfile)) {
338 return false;
339 }
340 } else {
341 if (!unlink($fullfile)) {
342 return false;
343 }
344 }
345 }
346 }
347 closedir($currdir);
348 if (! rmdir($location)) {
349 return false;
350 }
351
352 } else {
353 if (!unlink($location)) {
354 return false;
355 }
356 }
357 return true;
358}
359
69faecce 360function readfile_chunked($filename,$retbytes=true) {
361 $chunksize = 1*(1024*1024); // 1MB chunks
362 $buffer = '';
363 $cnt =0;// $handle = fopen($filename, 'rb');
364 $handle = fopen($filename, 'rb');
365 if ($handle === false) {
366 return false;
367 }
368
369 while (!feof($handle)) {
370 $buffer = fread($handle, $chunksize);
371 echo $buffer;
372 if ($retbytes) {
373 $cnt += strlen($buffer);}
374 }
375 $status = fclose($handle);
376 if ($retbytes && $status) {
377 return $cnt; // return num. bytes delivered like readfile() does.
378 }
379 return $status;
380}
381
f401cc97 382
a77b98eb 383?>