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