599f38f9 |
1 | <?php //$Id$ |
2 | |
f1e0649c |
3 | function 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'), |
5395334d |
59 | |
60 | 'odt' => array ('type'=>'application/vnd.oasis.opendocument.text', 'icon'=>'odt.gif'), |
61 | 'ott' => array ('type'=>'application/vnd.oasis.opendocument.text-template', 'icon'=>'odt.gif'), |
62 | 'oth' => array ('type'=>'application/vnd.oasis.opendocument.text-web', 'icon'=>'odt.gif'), |
63 | 'odm' => array ('type'=>'application/vnd.oasis.opendocument.text-master', 'icon'=>'odt.gif'), |
64 | 'odg' => array ('type'=>'application/vnd.oasis.opendocument.graphics', 'icon'=>'odt.gif'), |
65 | 'otg' => array ('type'=>'application/vnd.oasis.opendocument.graphics-template', 'icon'=>'odt.gif'), |
66 | 'odp' => array ('type'=>'application/vnd.oasis.opendocument.presentation', 'icon'=>'odt.gif'), |
67 | 'otp' => array ('type'=>'application/vnd.oasis.opendocument.presentation-template', 'icon'=>'odt.gif'), |
68 | 'ods' => array ('type'=>'application/vnd.oasis.opendocument.spreadsheet', 'icon'=>'odt.gif'), |
69 | 'ots' => array ('type'=>'application/vnd.oasis.opendocument.spreadsheet-template', 'icon'=>'odt.gif'), |
70 | 'odc' => array ('type'=>'application/vnd.oasis.opendocument.chart', 'icon'=>'odt.gif'), |
71 | 'odf' => array ('type'=>'application/vnd.oasis.opendocument.formula', 'icon'=>'odt.gif'), |
72 | 'odb' => array ('type'=>'application/vnd.oasis.opendocument.database', 'icon'=>'odt.gif'), |
73 | 'odi' => array ('type'=>'application/vnd.oasis.opendocument.image', 'icon'=>'odt.gif'), |
74 | |
a370c895 |
75 | 'pct' => array ('type'=>'image/pict', 'icon'=>'image.gif'), |
76 | 'pdf' => array ('type'=>'application/pdf', 'icon'=>'pdf.gif'), |
77 | 'php' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
78 | 'pic' => array ('type'=>'image/pict', 'icon'=>'image.gif'), |
79 | 'pict' => array ('type'=>'image/pict', 'icon'=>'image.gif'), |
80 | 'png' => array ('type'=>'image/png', 'icon'=>'image.gif'), |
81 | 'pps' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'powerpoint.gif'), |
82 | 'ppt' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'powerpoint.gif'), |
83 | 'ps' => array ('type'=>'application/postscript', 'icon'=>'pdf.gif'), |
84 | 'qt' => array ('type'=>'video/quicktime', 'icon'=>'video.gif'), |
85 | 'ra' => array ('type'=>'audio/x-realaudio', 'icon'=>'audio.gif'), |
86 | 'ram' => array ('type'=>'audio/x-pn-realaudio', 'icon'=>'audio.gif'), |
a00420fb |
87 | 'rhb' => array ('type'=>'text/xml', 'icon'=>'xml.gif'), |
a370c895 |
88 | 'rm' => array ('type'=>'audio/x-pn-realaudio', 'icon'=>'audio.gif'), |
89 | 'rtf' => array ('type'=>'text/rtf', 'icon'=>'text.gif'), |
90 | 'rtx' => array ('type'=>'text/richtext', 'icon'=>'text.gif'), |
91 | 'sh' => array ('type'=>'application/x-sh', 'icon'=>'text.gif'), |
92 | 'sit' => array ('type'=>'application/x-stuffit', 'icon'=>'zip.gif'), |
93 | 'smi' => array ('type'=>'application/smil', 'icon'=>'text.gif'), |
94 | 'smil' => array ('type'=>'application/smil', 'icon'=>'text.gif'), |
a00420fb |
95 | 'sqt' => array ('type'=>'text/xml', 'icon'=>'xml.gif'), |
a370c895 |
96 | 'swa' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'), |
97 | 'swf' => array ('type'=>'application/x-shockwave-flash', 'icon'=>'flash.gif'), |
98 | 'swfl' => array ('type'=>'application/x-shockwave-flash', 'icon'=>'flash.gif'), |
5395334d |
99 | |
100 | 'sxw' => array ('type'=>'application/vnd.sun.xml.writer', 'icon'=>'odt.gif'), |
101 | 'stw' => array ('type'=>'application/vnd.sun.xml.writer.template', 'icon'=>'odt.gif'), |
102 | 'sxc' => array ('type'=>'application/vnd.sun.xml.calc', 'icon'=>'odt.gif'), |
103 | 'stc' => array ('type'=>'application/vnd.sun.xml.calc.template', 'icon'=>'odt.gif'), |
104 | 'sxd' => array ('type'=>'application/vnd.sun.xml.draw', 'icon'=>'odt.gif'), |
105 | 'std' => array ('type'=>'application/vnd.sun.xml.draw.template', 'icon'=>'odt.gif'), |
106 | 'sxi' => array ('type'=>'application/vnd.sun.xml.impress', 'icon'=>'odt.gif'), |
107 | 'sti' => array ('type'=>'application/vnd.sun.xml.impress.template', 'icon'=>'odt.gif'), |
108 | 'sxg' => array ('type'=>'application/vnd.sun.xml.writer.global', 'icon'=>'odt.gif'), |
109 | 'sxm' => array ('type'=>'application/vnd.sun.xml.math', 'icon'=>'odt.gif'), |
110 | |
a370c895 |
111 | 'tar' => array ('type'=>'application/x-tar', 'icon'=>'zip.gif'), |
112 | 'tif' => array ('type'=>'image/tiff', 'icon'=>'image.gif'), |
113 | 'tiff' => array ('type'=>'image/tiff', 'icon'=>'image.gif'), |
114 | 'tex' => array ('type'=>'application/x-tex', 'icon'=>'text.gif'), |
115 | 'texi' => array ('type'=>'application/x-texinfo', 'icon'=>'text.gif'), |
116 | 'texinfo' => array ('type'=>'application/x-texinfo', 'icon'=>'text.gif'), |
117 | 'tsv' => array ('type'=>'text/tab-separated-values', 'icon'=>'text.gif'), |
118 | 'txt' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
119 | 'wav' => array ('type'=>'audio/wav', 'icon'=>'audio.gif'), |
120 | 'wmv' => array ('type'=>'video/x-ms-wmv', 'icon'=>'avi.gif'), |
121 | 'asf' => array ('type'=>'video/x-ms-asf', 'icon'=>'avi.gif'), |
122 | 'xls' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'excel.gif'), |
123 | 'xml' => array ('type'=>'application/xml', 'icon'=>'xml.gif'), |
124 | 'xsl' => array ('type'=>'text/xml', 'icon'=>'xml.gif'), |
125 | 'zip' => array ('type'=>'application/zip', 'icon'=>'zip.gif') |
f1e0649c |
126 | ); |
127 | |
a370c895 |
128 | if (eregi('\.([a-z0-9]+)$', $filename, $match)) { |
f1e0649c |
129 | if (isset($mimeinfo[strtolower($match[1])][$element])) { |
130 | return $mimeinfo[strtolower($match[1])][$element]; |
131 | } else { |
a370c895 |
132 | return $mimeinfo['xxx'][$element]; // By default |
f1e0649c |
133 | } |
134 | } else { |
a370c895 |
135 | return $mimeinfo['xxx'][$element]; // By default |
f1e0649c |
136 | } |
137 | } |
138 | |
139 | function send_file($path, $filename, $lifetime=86400 , $filter=false, $pathisstring=false) { |
140 | |
141 | $mimetype = mimeinfo('type', $filename); |
142 | $lastmodified = $pathisstring ? time() : filemtime($path); |
143 | $filesize = $pathisstring ? strlen($path) : filesize($path); |
144 | |
a370c895 |
145 | @header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT'); |
f1e0649c |
146 | if ($lifetime > 0) { |
147 | @header('Cache-control: max-age='.$lifetime); |
a370c895 |
148 | @header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .'GMT'); |
f1e0649c |
149 | @header('Pragma: '); |
150 | } else { |
151 | // this part is tricky, displaying of MS Office documents in IE needs |
152 | // to store the file on disk, but no-cache may prevent it |
153 | @header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=10'); |
a370c895 |
154 | @header('Expires: '. gmdate('D, d M Y H:i:s', 0) .'GMT'); |
f1e0649c |
155 | @header('Pragma: no-cache'); |
156 | } |
157 | @header('Accept-Ranges: none'); // PDF compatibility |
158 | @header('Content-disposition: inline; filename='.$filename); |
159 | |
160 | if (!$filter) { |
161 | @header('Content-length: '.$filesize); |
162 | if ($mimetype == 'text/plain') { |
163 | @header('Content-type: text/plain; charset='.get_string('thischarset')); //add encoding |
164 | } else { |
165 | @header('Content-type: '.$mimetype); |
166 | } |
167 | if ($pathisstring) { |
168 | echo $path; |
169 | }else { |
170 | readfile($path); |
171 | } |
172 | } else { // Try to put the file through filters |
a370c895 |
173 | global $course; // HACK! |
f1e0649c |
174 | if (!empty($course->id)) { |
175 | $courseid = $course->id; |
176 | } else { |
177 | $courseid = SITEID; |
178 | } |
179 | if ($mimetype == 'text/html') { |
180 | $options->noclean = true; |
181 | $text = $pathisstring ? $path : implode('', file($path)); |
182 | $output = format_text($text, FORMAT_HTML, $options, $courseid); |
183 | |
184 | @header('Content-length: '.strlen($output)); |
185 | @header('Content-type: text/html'); |
186 | echo $output; |
187 | } else if ($mimetype == 'text/plain') { |
188 | $options->newlines = false; |
189 | $options->noclean = true; |
190 | $text = htmlentities($pathisstring ? $path : implode('', file($path))); |
191 | $output = '<pre>'. format_text($text, FORMAT_MOODLE, $options, $courseid) .'</pre>'; |
192 | |
193 | @header('Content-length: '.strlen($output)); |
194 | @header('Content-type: text/html; charset='. get_string('thischarset')); //add encoding |
195 | echo $output; |
196 | } else { // Just send it out raw |
197 | @header('Content-length: '.$filesize); |
198 | @header('Content-type: '.$mimetype); |
199 | if ($pathisstring) { |
200 | echo $path; |
201 | }else { |
202 | readfile($path); |
203 | } |
204 | } |
205 | } |
206 | die; //no more chars to output!!! |
207 | } |
208 | |
a43b5308 |
209 | function get_records_csv($file, $table) { |
599f38f9 |
210 | global $CFG, $db; |
211 | |
212 | if (!$metacolumns = $db->MetaColumns($CFG->prefix . $table)) { |
213 | return false; |
214 | } |
215 | |
a77b98eb |
216 | if(!($handle = @fopen($file, 'r'))) { |
599f38f9 |
217 | error('get_records_csv failed to open '.$file); |
218 | } |
219 | |
220 | $fieldnames = fgetcsv($handle, 4096); |
221 | if(empty($fieldnames)) { |
222 | fclose($handle); |
223 | return false; |
224 | } |
225 | |
226 | $columns = array(); |
227 | |
228 | foreach($metacolumns as $metacolumn) { |
229 | $ord = array_search($metacolumn->name, $fieldnames); |
230 | if(is_int($ord)) { |
231 | $columns[$metacolumn->name] = $ord; |
232 | } |
233 | } |
234 | |
235 | $rows = array(); |
236 | |
237 | while (($data = fgetcsv($handle, 4096)) !== false) { |
238 | $item = new stdClass; |
239 | foreach($columns as $name => $ord) { |
240 | $item->$name = $data[$ord]; |
241 | } |
242 | $rows[] = $item; |
243 | } |
244 | |
245 | fclose($handle); |
246 | return $rows; |
247 | } |
248 | |
a77b98eb |
249 | function put_records_csv($file, $records, $table = NULL) { |
250 | global $CFG, $db; |
251 | |
a1e93da2 |
252 | if (empty($records)) { |
a77b98eb |
253 | return true; |
254 | } |
255 | |
256 | $metacolumns = NULL; |
257 | if ($table !== NULL && !$metacolumns = $db->MetaColumns($CFG->prefix . $table)) { |
258 | return false; |
259 | } |
260 | |
a1e93da2 |
261 | echo "x"; |
262 | |
a77b98eb |
263 | if(!($fp = @fopen($CFG->dataroot.'/temp/'.$file, 'w'))) { |
264 | error('put_records_csv failed to open '.$file); |
265 | } |
266 | |
a43b5308 |
267 | $proto = reset($records); |
268 | if(is_object($proto)) { |
269 | $fields_records = array_keys(get_object_vars($proto)); |
270 | } |
271 | else if(is_array($proto)) { |
272 | $fields_records = array_keys($proto); |
273 | } |
274 | else { |
275 | return false; |
276 | } |
a1e93da2 |
277 | echo "x"; |
a77b98eb |
278 | |
279 | if(!empty($metacolumns)) { |
280 | $fields_table = array_map(create_function('$a', 'return $a->name;'), $metacolumns); |
281 | $fields = array_intersect($fields_records, $fields_table); |
282 | } |
283 | else { |
284 | $fields = $fields_records; |
285 | } |
286 | |
287 | fwrite($fp, implode(',', $fields)); |
288 | fwrite($fp, "\r\n"); |
289 | |
290 | foreach($records as $record) { |
a43b5308 |
291 | $array = (array)$record; |
a77b98eb |
292 | $values = array(); |
293 | foreach($fields as $field) { |
a43b5308 |
294 | if(strpos($array[$field], ',')) { |
295 | $values[] = '"'.str_replace('"', '\"', $array[$field]).'"'; |
a77b98eb |
296 | } |
297 | else { |
a43b5308 |
298 | $values[] = $array[$field]; |
a77b98eb |
299 | } |
300 | } |
301 | fwrite($fp, implode(',', $values)."\r\n"); |
302 | } |
303 | |
304 | fclose($fp); |
305 | return true; |
306 | } |
307 | |
f401cc97 |
308 | |
7ab8bb71 |
309 | if (!function_exists('file_get_contents')) { |
310 | function file_get_contents($file) { |
311 | $file = file($file); |
312 | return !$file ? false : implode('', $file); |
313 | } |
314 | } |
315 | |
f401cc97 |
316 | |
317 | function fulldelete($location) { |
318 | if (is_dir($location)) { |
319 | $currdir = opendir($location); |
320 | while (false !== ($file = readdir($currdir))) { |
321 | if ($file <> ".." && $file <> ".") { |
322 | $fullfile = $location."/".$file; |
323 | if (is_dir($fullfile)) { |
324 | if (!fulldelete($fullfile)) { |
325 | return false; |
326 | } |
327 | } else { |
328 | if (!unlink($fullfile)) { |
329 | return false; |
330 | } |
331 | } |
332 | } |
333 | } |
334 | closedir($currdir); |
335 | if (! rmdir($location)) { |
336 | return false; |
337 | } |
338 | |
339 | } else { |
340 | if (!unlink($location)) { |
341 | return false; |
342 | } |
343 | } |
344 | return true; |
345 | } |
346 | |
347 | |
a77b98eb |
348 | ?> |