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