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