Fixed notice on home page and some efficiency with read trackking
[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'),
23 'doc' => array ('type'=>'application/msword', 'icon'=>'word.gif'),
24 'dcr' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'),
25 'dif' => array ('type'=>'video/x-dv', 'icon'=>'video.gif'),
26 'dir' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'),
27 'dxr' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'),
28 'eps' => array ('type'=>'application/postscript', 'icon'=>'pdf.gif'),
29 'gif' => array ('type'=>'image/gif', 'icon'=>'image.gif'),
30 'gtar' => array ('type'=>'application/x-gtar', 'icon'=>'zip.gif'),
31 'gz' => array ('type'=>'application/g-zip', 'icon'=>'zip.gif'),
32 'gzip' => array ('type'=>'application/g-zip', 'icon'=>'zip.gif'),
33 'h' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
18bf47ef 34 'hpp' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
a370c895 35 'hqx' => array ('type'=>'application/mac-binhex40', 'icon'=>'zip.gif'),
36 'html' => array ('type'=>'text/html', 'icon'=>'html.gif'),
37 'htm' => array ('type'=>'text/html', 'icon'=>'html.gif'),
18bf47ef 38 'java' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
a00420fb 39 'jcb' => array ('type'=>'text/xml', 'icon'=>'jcb.gif'),
40 'jcl' => array ('type'=>'text/xml', 'icon'=>'jcl.gif'),
41 'jcw' => array ('type'=>'text/xml', 'icon'=>'jcw.gif'),
42 'jmt' => array ('type'=>'text/xml', 'icon'=>'jmt.gif'),
43 'jmx' => array ('type'=>'text/xml', 'icon'=>'jmx.gif'),
a370c895 44 'jpe' => array ('type'=>'image/jpeg', 'icon'=>'image.gif'),
45 'jpeg' => array ('type'=>'image/jpeg', 'icon'=>'image.gif'),
46 'jpg' => array ('type'=>'image/jpeg', 'icon'=>'image.gif'),
a00420fb 47 'jqz' => array ('type'=>'text/xml', 'icon'=>'jqz.gif'),
a370c895 48 'js' => array ('type'=>'application/x-javascript', 'icon'=>'text.gif'),
49 'latex'=> array ('type'=>'application/x-latex', 'icon'=>'text.gif'),
50 'm' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
51 'mov' => array ('type'=>'video/quicktime', 'icon'=>'video.gif'),
52 'movie'=> array ('type'=>'video/x-sgi-movie', 'icon'=>'video.gif'),
53 'm3u' => array ('type'=>'audio/x-mpegurl', 'icon'=>'audio.gif'),
54 'mp3' => array ('type'=>'audio/mp3', 'icon'=>'audio.gif'),
55 'mp4' => array ('type'=>'video/mp4', 'icon'=>'video.gif'),
56 'mpeg' => array ('type'=>'video/mpeg', 'icon'=>'video.gif'),
57 'mpe' => array ('type'=>'video/mpeg', 'icon'=>'video.gif'),
58 'mpg' => array ('type'=>'video/mpeg', 'icon'=>'video.gif'),
59 'pct' => array ('type'=>'image/pict', 'icon'=>'image.gif'),
60 'pdf' => array ('type'=>'application/pdf', 'icon'=>'pdf.gif'),
61 'php' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
62 'pic' => array ('type'=>'image/pict', 'icon'=>'image.gif'),
63 'pict' => array ('type'=>'image/pict', 'icon'=>'image.gif'),
64 'png' => array ('type'=>'image/png', 'icon'=>'image.gif'),
65 'pps' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'powerpoint.gif'),
66 'ppt' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'powerpoint.gif'),
67 'ps' => array ('type'=>'application/postscript', 'icon'=>'pdf.gif'),
68 'qt' => array ('type'=>'video/quicktime', 'icon'=>'video.gif'),
69 'ra' => array ('type'=>'audio/x-realaudio', 'icon'=>'audio.gif'),
70 'ram' => array ('type'=>'audio/x-pn-realaudio', 'icon'=>'audio.gif'),
a00420fb 71 'rhb' => array ('type'=>'text/xml', 'icon'=>'xml.gif'),
a370c895 72 'rm' => array ('type'=>'audio/x-pn-realaudio', 'icon'=>'audio.gif'),
73 'rtf' => array ('type'=>'text/rtf', 'icon'=>'text.gif'),
74 'rtx' => array ('type'=>'text/richtext', 'icon'=>'text.gif'),
75 'sh' => array ('type'=>'application/x-sh', 'icon'=>'text.gif'),
76 'sit' => array ('type'=>'application/x-stuffit', 'icon'=>'zip.gif'),
77 'smi' => array ('type'=>'application/smil', 'icon'=>'text.gif'),
78 'smil' => array ('type'=>'application/smil', 'icon'=>'text.gif'),
a00420fb 79 'sqt' => array ('type'=>'text/xml', 'icon'=>'xml.gif'),
a370c895 80 'swa' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'),
81 'swf' => array ('type'=>'application/x-shockwave-flash', 'icon'=>'flash.gif'),
82 'swfl' => array ('type'=>'application/x-shockwave-flash', 'icon'=>'flash.gif'),
83 'tar' => array ('type'=>'application/x-tar', 'icon'=>'zip.gif'),
84 'tif' => array ('type'=>'image/tiff', 'icon'=>'image.gif'),
85 'tiff' => array ('type'=>'image/tiff', 'icon'=>'image.gif'),
86 'tex' => array ('type'=>'application/x-tex', 'icon'=>'text.gif'),
87 'texi' => array ('type'=>'application/x-texinfo', 'icon'=>'text.gif'),
88 'texinfo' => array ('type'=>'application/x-texinfo', 'icon'=>'text.gif'),
89 'tsv' => array ('type'=>'text/tab-separated-values', 'icon'=>'text.gif'),
90 'txt' => array ('type'=>'text/plain', 'icon'=>'text.gif'),
91 'wav' => array ('type'=>'audio/wav', 'icon'=>'audio.gif'),
92 'wmv' => array ('type'=>'video/x-ms-wmv', 'icon'=>'avi.gif'),
93 'asf' => array ('type'=>'video/x-ms-asf', 'icon'=>'avi.gif'),
94 'xls' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'excel.gif'),
95 'xml' => array ('type'=>'application/xml', 'icon'=>'xml.gif'),
96 'xsl' => array ('type'=>'text/xml', 'icon'=>'xml.gif'),
97 'zip' => array ('type'=>'application/zip', 'icon'=>'zip.gif')
18bf47ef 98
f1e0649c 99 );
100
a370c895 101 if (eregi('\.([a-z0-9]+)$', $filename, $match)) {
f1e0649c 102 if (isset($mimeinfo[strtolower($match[1])][$element])) {
103 return $mimeinfo[strtolower($match[1])][$element];
104 } else {
a370c895 105 return $mimeinfo['xxx'][$element]; // By default
f1e0649c 106 }
107 } else {
a370c895 108 return $mimeinfo['xxx'][$element]; // By default
f1e0649c 109 }
110}
111
112function send_file($path, $filename, $lifetime=86400 , $filter=false, $pathisstring=false) {
113
114 $mimetype = mimeinfo('type', $filename);
115 $lastmodified = $pathisstring ? time() : filemtime($path);
116 $filesize = $pathisstring ? strlen($path) : filesize($path);
117
a370c895 118 @header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT');
f1e0649c 119 if ($lifetime > 0) {
120 @header('Cache-control: max-age='.$lifetime);
a370c895 121 @header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .'GMT');
f1e0649c 122 @header('Pragma: ');
123 } else {
124 // this part is tricky, displaying of MS Office documents in IE needs
125 // to store the file on disk, but no-cache may prevent it
126 @header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=10');
a370c895 127 @header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT');
f1e0649c 128 @header('Pragma: no-cache');
129 }
130 @header('Accept-Ranges: none'); // PDF compatibility
131 @header('Content-disposition: inline; filename='.$filename);
132
133 if (!$filter) {
134 @header('Content-length: '.$filesize);
135 if ($mimetype == 'text/plain') {
136 @header('Content-type: text/plain; charset='.get_string('thischarset')); //add encoding
137 } else {
138 @header('Content-type: '.$mimetype);
139 }
140 if ($pathisstring) {
141 echo $path;
142 }else {
143 readfile($path);
144 }
145 } else { // Try to put the file through filters
a370c895 146 global $course; // HACK!
f1e0649c 147 if (!empty($course->id)) {
148 $courseid = $course->id;
149 } else {
150 $courseid = SITEID;
151 }
152 if ($mimetype == 'text/html') {
153 $options->noclean = true;
154 $text = $pathisstring ? $path : implode('', file($path));
155 $output = format_text($text, FORMAT_HTML, $options, $courseid);
156
157 @header('Content-length: '.strlen($output));
158 @header('Content-type: text/html');
159 echo $output;
160 } else if ($mimetype == 'text/plain') {
161 $options->newlines = false;
162 $options->noclean = true;
163 $text = htmlentities($pathisstring ? $path : implode('', file($path)));
164 $output = '<pre>'. format_text($text, FORMAT_MOODLE, $options, $courseid) .'</pre>';
165
166 @header('Content-length: '.strlen($output));
167 @header('Content-type: text/html; charset='. get_string('thischarset')); //add encoding
168 echo $output;
169 } else { // Just send it out raw
170 @header('Content-length: '.$filesize);
171 @header('Content-type: '.$mimetype);
172 if ($pathisstring) {
173 echo $path;
174 }else {
175 readfile($path);
176 }
177 }
178 }
179 die; //no more chars to output!!!
180}
181
a43b5308 182function get_records_csv($file, $table) {
599f38f9 183 global $CFG, $db;
184
185 if (!$metacolumns = $db->MetaColumns($CFG->prefix . $table)) {
186 return false;
187 }
188
a77b98eb 189 if(!($handle = @fopen($file, 'r'))) {
599f38f9 190 error('get_records_csv failed to open '.$file);
191 }
192
193 $fieldnames = fgetcsv($handle, 4096);
194 if(empty($fieldnames)) {
195 fclose($handle);
196 return false;
197 }
198
199 $columns = array();
200
201 foreach($metacolumns as $metacolumn) {
202 $ord = array_search($metacolumn->name, $fieldnames);
203 if(is_int($ord)) {
204 $columns[$metacolumn->name] = $ord;
205 }
206 }
207
208 $rows = array();
209
210 while (($data = fgetcsv($handle, 4096)) !== false) {
211 $item = new stdClass;
212 foreach($columns as $name => $ord) {
213 $item->$name = $data[$ord];
214 }
215 $rows[] = $item;
216 }
217
218 fclose($handle);
219 return $rows;
220}
221
a77b98eb 222function put_records_csv($file, $records, $table = NULL) {
223 global $CFG, $db;
224
a1e93da2 225 if (empty($records)) {
a77b98eb 226 return true;
227 }
228
229 $metacolumns = NULL;
230 if ($table !== NULL && !$metacolumns = $db->MetaColumns($CFG->prefix . $table)) {
231 return false;
232 }
233
a1e93da2 234 echo "x";
235
a77b98eb 236 if(!($fp = @fopen($CFG->dataroot.'/temp/'.$file, 'w'))) {
237 error('put_records_csv failed to open '.$file);
238 }
239
a43b5308 240 $proto = reset($records);
241 if(is_object($proto)) {
242 $fields_records = array_keys(get_object_vars($proto));
243 }
244 else if(is_array($proto)) {
245 $fields_records = array_keys($proto);
246 }
247 else {
248 return false;
249 }
a1e93da2 250 echo "x";
a77b98eb 251
252 if(!empty($metacolumns)) {
253 $fields_table = array_map(create_function('$a', 'return $a->name;'), $metacolumns);
254 $fields = array_intersect($fields_records, $fields_table);
255 }
256 else {
257 $fields = $fields_records;
258 }
259
260 fwrite($fp, implode(',', $fields));
261 fwrite($fp, "\r\n");
262
263 foreach($records as $record) {
a43b5308 264 $array = (array)$record;
a77b98eb 265 $values = array();
266 foreach($fields as $field) {
a43b5308 267 if(strpos($array[$field], ',')) {
268 $values[] = '"'.str_replace('"', '\"', $array[$field]).'"';
a77b98eb 269 }
270 else {
a43b5308 271 $values[] = $array[$field];
a77b98eb 272 }
273 }
274 fwrite($fp, implode(',', $values)."\r\n");
275 }
276
277 fclose($fp);
278 return true;
279}
280
7ab8bb71 281if (!function_exists('file_get_contents')) {
282 function file_get_contents($file) {
283 $file = file($file);
284 return !$file ? false : implode('', $file);
285 }
286}
287
a77b98eb 288?>