599f38f9 |
1 | <?php //$Id$ |
2 | |
4c8c65ec |
3 | define('BYTESERVING_BOUNDARY', 's1k2o3d4a5k6s7'); //unique string constant |
4 | |
74369ab5 |
5 | function get_file_url($path, $options=null, $type='coursefile') { |
6 | global $CFG; |
7 | |
8 | $path = trim($path, '/'); // no leading and trailing slashes |
9 | |
10 | // type of file |
11 | switch ($type) { |
12 | case 'coursefile': |
13 | default: |
14 | $url = "$CFG->wwwroot/file.php"; |
15 | } |
16 | |
17 | if ($CFG->slasharguments) { |
18 | $parts = explode('/', $path); |
5e19ea32 |
19 | $parts = array_map('rawurlencode', $parts); |
74369ab5 |
20 | $path = implode('/', $parts); |
21 | $ffurl = "$CFG->wwwroot/file.php/$path"; |
22 | $separator = '?'; |
23 | } else { |
5e19ea32 |
24 | $path = rawurlencode("/$path"); |
74369ab5 |
25 | $ffurl = "$CFG->wwwroot/file.php?file=$path"; |
26 | $separator = '&'; |
27 | } |
28 | |
29 | if ($options) { |
30 | foreach ($options as $name=>$value) { |
31 | $ffurl = $ffurl.$separator.$name.'='.$value; |
32 | $separator = '&'; |
33 | } |
34 | } |
35 | |
36 | return $ffurl; |
37 | } |
38 | |
8ee88311 |
39 | /** |
5f8bdc17 |
40 | * Fetches content of file from Internet (using proxy if defined). Uses cURL extension if present. |
599f06cf |
41 | * Due to security concerns only downloads from http(s) sources are supported. |
42 | * |
43 | * @param string $url file url starting with http(s):// |
5ef082df |
44 | * @param array $headers http headers, null if none. If set, should be an |
45 | * associative array of header name => value pairs. |
6bf55889 |
46 | * @param array $postdata array means use POST request with given parameters |
47 | * @param bool $fullresponse return headers, responses, etc in a similar way snoopy does |
5ef082df |
48 | * (if false, just returns content) |
49 | * @param int $timeout timeout for complete download process including all file transfer |
44e02d79 |
50 | * (default 5 minutes) |
51 | * @param int $connecttimeout timeout for connection to server; this is the timeout that |
52 | * usually happens if the remote server is completely down (default 20 seconds); |
53 | * may not work when using proxy |
83947a36 |
54 | * @param bool $skipcertverify If true, the peer's SSL certificate will not be checked. Only use this when already in a trusted location. |
8ee88311 |
55 | * @return mixed false if request failed or content of the file as string if ok. |
56 | */ |
83947a36 |
57 | function download_file_content($url, $headers=null, $postdata=null, $fullresponse=false, $timeout=300, $connecttimeout=20, $skipcertverify=false) { |
e27f0765 |
58 | global $CFG; |
59 | |
599f06cf |
60 | // some extra security |
61 | $newlines = array("\r", "\n"); |
62 | if (is_array($headers) ) { |
63 | foreach ($headers as $key => $value) { |
64 | $headers[$key] = str_replace($newlines, '', $value); |
65 | } |
66 | } |
67 | $url = str_replace($newlines, '', $url); |
68 | if (!preg_match('|^https?://|i', $url)) { |
69 | if ($fullresponse) { |
70 | $response = new object(); |
71 | $response->status = 0; |
72 | $response->headers = array(); |
73 | $response->response_code = 'Invalid protocol specified in url'; |
74 | $response->results = ''; |
75 | $response->error = 'Invalid protocol specified in url'; |
76 | return $response; |
77 | } else { |
78 | return false; |
79 | } |
80 | } |
81 | |
15c31560 |
82 | // check if proxy (if used) should be bypassed for this url |
83 | $proxybypass = is_proxybypass( $url ); |
599f06cf |
84 | |
6bf55889 |
85 | if (!extension_loaded('curl') or ($ch = curl_init($url)) === false) { |
5f8bdc17 |
86 | require_once($CFG->libdir.'/snoopy/Snoopy.class.inc'); |
87 | $snoopy = new Snoopy(); |
6bf55889 |
88 | $snoopy->read_timeout = $timeout; |
44e02d79 |
89 | $snoopy->_fp_timeout = $connecttimeout; |
15c31560 |
90 | if (!$proxybypass) { |
91 | $snoopy->proxy_host = $CFG->proxyhost; |
92 | $snoopy->proxy_port = $CFG->proxyport; |
93 | if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) { |
94 | // this will probably fail, but let's try it anyway |
95 | $snoopy->proxy_user = $CFG->proxyuser; |
96 | $snoopy->proxy_password = $CFG->proxypassword; |
97 | } |
5f8bdc17 |
98 | } |
15c31560 |
99 | |
6bf55889 |
100 | if (is_array($headers) ) { |
101 | $client->rawheaders = $headers; |
102 | } |
103 | |
104 | if (is_array($postdata)) { |
105 | $fetch = @$snoopy->fetch($url, $postdata); // use more specific debug code bellow |
106 | } else { |
107 | $fetch = @$snoopy->fetch($url); // use more specific debug code bellow |
108 | } |
109 | |
110 | if ($fetch) { |
111 | if ($fullresponse) { |
112 | //fix header line endings |
113 | foreach ($snoopy->headers as $key=>$unused) { |
114 | $snoopy->headers[$key] = trim($snoopy->headers[$key]); |
115 | } |
116 | $response = new object(); |
117 | $response->status = $snoopy->status; |
118 | $response->headers = $snoopy->headers; |
119 | $response->response_code = trim($snoopy->response_code); |
120 | $response->results = $snoopy->results; |
121 | $response->error = $snoopy->error; |
122 | return $response; |
123 | |
124 | } else if ($snoopy->status != 200) { |
5f8bdc17 |
125 | debugging("Snoopy request for \"$url\" failed, http response code: ".$snoopy->response_code, DEBUG_ALL); |
126 | return false; |
6bf55889 |
127 | |
5f8bdc17 |
128 | } else { |
129 | return $snoopy->results; |
130 | } |
131 | } else { |
6bf55889 |
132 | if ($fullresponse) { |
133 | $response = new object(); |
134 | $response->status = $snoopy->status; |
135 | $response->headers = array(); |
136 | $response->response_code = $snoopy->response_code; |
137 | $response->results = ''; |
138 | $response->error = $snoopy->error; |
139 | return $response; |
140 | } else { |
141 | debugging("Snoopy request for \"$url\" failed with: ".$snoopy->error, DEBUG_ALL); |
142 | return false; |
143 | } |
144 | } |
145 | } |
146 | |
599f06cf |
147 | // set extra headers |
6bf55889 |
148 | if (is_array($headers) ) { |
149 | $headers2 = array(); |
150 | foreach ($headers as $key => $value) { |
6bf55889 |
151 | $headers2[] = "$key: $value"; |
152 | } |
153 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers2); |
154 | } |
155 | |
83947a36 |
156 | |
157 | if ($skipcertverify) { |
158 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
159 | } |
160 | |
6bf55889 |
161 | // use POST if requested |
162 | if (is_array($postdata)) { |
163 | foreach ($postdata as $k=>$v) { |
164 | $postdata[$k] = urlencode($k).'='.urlencode($v); |
5f8bdc17 |
165 | } |
6bf55889 |
166 | $postdata = implode('&', $postdata); |
167 | curl_setopt($ch, CURLOPT_POST, true); |
168 | curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); |
5f8bdc17 |
169 | } |
170 | |
8ee88311 |
171 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
6bf55889 |
172 | curl_setopt($ch, CURLOPT_HEADER, true); |
44e02d79 |
173 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connecttimeout); |
174 | curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); |
6bf55889 |
175 | if (!ini_get('open_basedir') and !ini_get('safe_mode')) { |
599f06cf |
176 | // TODO: add version test for '7.10.5' |
6bf55889 |
177 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
178 | curl_setopt($ch, CURLOPT_MAXREDIRS, 5); |
179 | } |
180 | |
15c31560 |
181 | if (!empty($CFG->proxyhost) and !$proxybypass) { |
5f8bdc17 |
182 | // SOCKS supported in PHP5 only |
183 | if (!empty($CFG->proxytype) and ($CFG->proxytype == 'SOCKS5')) { |
184 | if (defined('CURLPROXY_SOCKS5')) { |
185 | curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); |
186 | } else { |
5f8bdc17 |
187 | curl_close($ch); |
599f06cf |
188 | if ($fullresponse) { |
189 | $response = new object(); |
190 | $response->status = '0'; |
191 | $response->headers = array(); |
192 | $response->response_code = 'SOCKS5 proxy is not supported in PHP4'; |
193 | $response->results = ''; |
194 | $response->error = 'SOCKS5 proxy is not supported in PHP4'; |
195 | return $response; |
196 | } else { |
197 | debugging("SOCKS5 proxy is not supported in PHP4.", DEBUG_ALL); |
198 | return false; |
199 | } |
5f8bdc17 |
200 | } |
201 | } |
202 | |
08ec989f |
203 | curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false); |
204 | |
8ee88311 |
205 | if (empty($CFG->proxyport)) { |
e27f0765 |
206 | curl_setopt($ch, CURLOPT_PROXY, $CFG->proxyhost); |
8ee88311 |
207 | } else { |
e27f0765 |
208 | curl_setopt($ch, CURLOPT_PROXY, $CFG->proxyhost.':'.$CFG->proxyport); |
8ee88311 |
209 | } |
5f8bdc17 |
210 | |
211 | if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) { |
8ee88311 |
212 | curl_setopt($ch, CURLOPT_PROXYUSERPWD, $CFG->proxyuser.':'.$CFG->proxypassword); |
5f8bdc17 |
213 | if (defined('CURLOPT_PROXYAUTH')) { |
214 | // any proxy authentication if PHP 5.1 |
215 | curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM); |
216 | } |
8ee88311 |
217 | } |
218 | } |
6bf55889 |
219 | |
599f06cf |
220 | $data = curl_exec($ch); |
08ec989f |
221 | |
599f06cf |
222 | // try to detect encoding problems |
6bf55889 |
223 | if ((curl_errno($ch) == 23 or curl_errno($ch) == 61) and defined('CURLOPT_ENCODING')) { |
224 | curl_setopt($ch, CURLOPT_ENCODING, 'none'); |
599f06cf |
225 | $data = curl_exec($ch); |
6bf55889 |
226 | } |
227 | |
08ec989f |
228 | if (curl_errno($ch)) { |
6bf55889 |
229 | $error = curl_error($ch); |
230 | $error_no = curl_errno($ch); |
231 | curl_close($ch); |
232 | |
233 | if ($fullresponse) { |
234 | $response = new object(); |
235 | if ($error_no == 28) { |
236 | $response->status = '-100'; // mimic snoopy |
237 | } else { |
238 | $response->status = '0'; |
239 | } |
240 | $response->headers = array(); |
241 | $response->response_code = $error; |
242 | $response->results = ''; |
243 | $response->error = $error; |
244 | return $response; |
245 | } else { |
599f06cf |
246 | debugging("cURL request for \"$url\" failed with: $error ($error_no)", DEBUG_ALL); |
6bf55889 |
247 | return false; |
248 | } |
5f8bdc17 |
249 | |
250 | } else { |
6bf55889 |
251 | $info = curl_getinfo($ch); |
252 | curl_close($ch); |
599f06cf |
253 | |
254 | if (empty($info['http_code'])) { |
255 | // for security reasons we support only true http connections (Location: file:// exploit prevention) |
256 | $response = new object(); |
257 | $response->status = '0'; |
258 | $response->headers = array(); |
259 | $response->response_code = 'Unknown cURL error'; |
260 | $response->results = ''; // do NOT change this! |
261 | $response->error = 'Unknown cURL error'; |
262 | |
263 | } else { |
264 | // strip redirect headers and get headers array and content |
265 | $data = explode("\r\n\r\n", $data, $info['redirect_count'] + 2); |
266 | $results = array_pop($data); |
267 | $headers = array_pop($data); |
268 | $headers = explode("\r\n", trim($headers)); |
269 | |
270 | $response = new object();; |
271 | $response->status = (string)$info['http_code']; |
272 | $response->headers = $headers; |
273 | $response->response_code = $headers[0]; |
274 | $response->results = $results; |
275 | $response->error = ''; |
276 | } |
6bf55889 |
277 | |
278 | if ($fullresponse) { |
279 | return $response; |
280 | } else if ($info['http_code'] != 200) { |
599f06cf |
281 | debugging("cURL request for \"$url\" failed, HTTP response code: ".$response->response_code, DEBUG_ALL); |
6bf55889 |
282 | return false; |
283 | } else { |
284 | return $response->results; |
5f8bdc17 |
285 | } |
08ec989f |
286 | } |
8ee88311 |
287 | } |
288 | |
3ce73b14 |
289 | /** |
76ca1ff1 |
290 | * @return List of information about file types based on extensions. |
3ce73b14 |
291 | * Associative array of extension (lower-case) to associative array |
292 | * from 'element name' to data. Current element names are 'type' and 'icon'. |
76ca1ff1 |
293 | * Unknown types should use the 'xxx' entry which includes defaults. |
3ce73b14 |
294 | */ |
295 | function get_mimetypes_array() { |
296 | return array ( |
a370c895 |
297 | 'xxx' => array ('type'=>'document/unknown', 'icon'=>'unknown.gif'), |
298 | '3gp' => array ('type'=>'video/quicktime', 'icon'=>'video.gif'), |
299 | 'ai' => array ('type'=>'application/postscript', 'icon'=>'image.gif'), |
300 | 'aif' => array ('type'=>'audio/x-aiff', 'icon'=>'audio.gif'), |
301 | 'aiff' => array ('type'=>'audio/x-aiff', 'icon'=>'audio.gif'), |
302 | 'aifc' => array ('type'=>'audio/x-aiff', 'icon'=>'audio.gif'), |
303 | 'applescript' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
304 | 'asc' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
18bf47ef |
305 | 'asm' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
a370c895 |
306 | 'au' => array ('type'=>'audio/au', 'icon'=>'audio.gif'), |
307 | 'avi' => array ('type'=>'video/x-ms-wm', 'icon'=>'avi.gif'), |
308 | 'bmp' => array ('type'=>'image/bmp', 'icon'=>'image.gif'), |
18bf47ef |
309 | 'c' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
a370c895 |
310 | 'cct' => array ('type'=>'shockwave/director', 'icon'=>'flash.gif'), |
18bf47ef |
311 | 'cpp' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
a370c895 |
312 | 'cs' => array ('type'=>'application/x-csh', 'icon'=>'text.gif'), |
76ca1ff1 |
313 | 'css' => array ('type'=>'text/css', 'icon'=>'text.gif'), |
6ae5e482 |
314 | 'csv' => array ('type'=>'text/csv', 'icon'=>'excel.gif'), |
a370c895 |
315 | 'dv' => array ('type'=>'video/x-dv', 'icon'=>'video.gif'), |
609d84e3 |
316 | 'dmg' => array ('type'=>'application/octet-stream', 'icon'=>'dmg.gif'), |
a370c895 |
317 | 'doc' => array ('type'=>'application/msword', 'icon'=>'word.gif'), |
68da9722 |
318 | 'docx' => array ('type'=>'application/msword', 'icon'=>'docx.gif'), |
319 | 'docm' => array ('type'=>'application/msword', 'icon'=>'docm.gif'), |
320 | 'dotx' => array ('type'=>'application/msword', 'icon'=>'dotx.gif'), |
a370c895 |
321 | 'dcr' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'), |
322 | 'dif' => array ('type'=>'video/x-dv', 'icon'=>'video.gif'), |
323 | 'dir' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'), |
324 | 'dxr' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'), |
325 | 'eps' => array ('type'=>'application/postscript', 'icon'=>'pdf.gif'), |
ee7f231d |
326 | 'fdf' => array ('type'=>'application/pdf', 'icon'=>'pdf.gif'), |
759bc3c8 |
327 | 'flv' => array ('type'=>'video/x-flv', 'icon'=>'video.gif'), |
a370c895 |
328 | 'gif' => array ('type'=>'image/gif', 'icon'=>'image.gif'), |
329 | 'gtar' => array ('type'=>'application/x-gtar', 'icon'=>'zip.gif'), |
759bc3c8 |
330 | 'tgz' => array ('type'=>'application/g-zip', 'icon'=>'zip.gif'), |
a370c895 |
331 | 'gz' => array ('type'=>'application/g-zip', 'icon'=>'zip.gif'), |
332 | 'gzip' => array ('type'=>'application/g-zip', 'icon'=>'zip.gif'), |
333 | 'h' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
18bf47ef |
334 | 'hpp' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
a370c895 |
335 | 'hqx' => array ('type'=>'application/mac-binhex40', 'icon'=>'zip.gif'), |
70ee2841 |
336 | 'htc' => array ('type'=>'text/x-component', 'icon'=>'text.gif'), |
a370c895 |
337 | 'html' => array ('type'=>'text/html', 'icon'=>'html.gif'), |
1659a998 |
338 | 'xhtml'=> array ('type'=>'application/xhtml+xml', 'icon'=>'html.gif'), |
a370c895 |
339 | 'htm' => array ('type'=>'text/html', 'icon'=>'html.gif'), |
08297dcb |
340 | 'ico' => array ('type'=>'image/vnd.microsoft.icon', 'icon'=>'image.gif'), |
4b270c4c |
341 | 'ics' => array ('type'=>'text/calendar', 'icon'=>'text.gif'), |
08297dcb |
342 | 'isf' => array ('type'=>'application/inspiration', 'icon'=>'isf.gif'), |
343 | 'ist' => array ('type'=>'application/inspiration.template', 'icon'=>'isf.gif'), |
18bf47ef |
344 | 'java' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
a00420fb |
345 | 'jcb' => array ('type'=>'text/xml', 'icon'=>'jcb.gif'), |
346 | 'jcl' => array ('type'=>'text/xml', 'icon'=>'jcl.gif'), |
347 | 'jcw' => array ('type'=>'text/xml', 'icon'=>'jcw.gif'), |
348 | 'jmt' => array ('type'=>'text/xml', 'icon'=>'jmt.gif'), |
349 | 'jmx' => array ('type'=>'text/xml', 'icon'=>'jmx.gif'), |
a370c895 |
350 | 'jpe' => array ('type'=>'image/jpeg', 'icon'=>'image.gif'), |
351 | 'jpeg' => array ('type'=>'image/jpeg', 'icon'=>'image.gif'), |
352 | 'jpg' => array ('type'=>'image/jpeg', 'icon'=>'image.gif'), |
a00420fb |
353 | 'jqz' => array ('type'=>'text/xml', 'icon'=>'jqz.gif'), |
a370c895 |
354 | 'js' => array ('type'=>'application/x-javascript', 'icon'=>'text.gif'), |
355 | 'latex'=> array ('type'=>'application/x-latex', 'icon'=>'text.gif'), |
356 | 'm' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
357 | 'mov' => array ('type'=>'video/quicktime', 'icon'=>'video.gif'), |
358 | 'movie'=> array ('type'=>'video/x-sgi-movie', 'icon'=>'video.gif'), |
359 | 'm3u' => array ('type'=>'audio/x-mpegurl', 'icon'=>'audio.gif'), |
360 | 'mp3' => array ('type'=>'audio/mp3', 'icon'=>'audio.gif'), |
361 | 'mp4' => array ('type'=>'video/mp4', 'icon'=>'video.gif'), |
362 | 'mpeg' => array ('type'=>'video/mpeg', 'icon'=>'video.gif'), |
363 | 'mpe' => array ('type'=>'video/mpeg', 'icon'=>'video.gif'), |
364 | 'mpg' => array ('type'=>'video/mpeg', 'icon'=>'video.gif'), |
5395334d |
365 | |
366 | 'odt' => array ('type'=>'application/vnd.oasis.opendocument.text', 'icon'=>'odt.gif'), |
367 | 'ott' => array ('type'=>'application/vnd.oasis.opendocument.text-template', 'icon'=>'odt.gif'), |
368 | 'oth' => array ('type'=>'application/vnd.oasis.opendocument.text-web', 'icon'=>'odt.gif'), |
e10bc440 |
369 | 'odm' => array ('type'=>'application/vnd.oasis.opendocument.text-master', 'icon'=>'odm.gif'), |
370 | 'odg' => array ('type'=>'application/vnd.oasis.opendocument.graphics', 'icon'=>'odg.gif'), |
371 | 'otg' => array ('type'=>'application/vnd.oasis.opendocument.graphics-template', 'icon'=>'odg.gif'), |
372 | 'odp' => array ('type'=>'application/vnd.oasis.opendocument.presentation', 'icon'=>'odp.gif'), |
373 | 'otp' => array ('type'=>'application/vnd.oasis.opendocument.presentation-template', 'icon'=>'odp.gif'), |
374 | 'ods' => array ('type'=>'application/vnd.oasis.opendocument.spreadsheet', 'icon'=>'ods.gif'), |
375 | 'ots' => array ('type'=>'application/vnd.oasis.opendocument.spreadsheet-template', 'icon'=>'ods.gif'), |
376 | 'odc' => array ('type'=>'application/vnd.oasis.opendocument.chart', 'icon'=>'odc.gif'), |
377 | 'odf' => array ('type'=>'application/vnd.oasis.opendocument.formula', 'icon'=>'odf.gif'), |
378 | 'odb' => array ('type'=>'application/vnd.oasis.opendocument.database', 'icon'=>'odb.gif'), |
379 | 'odi' => array ('type'=>'application/vnd.oasis.opendocument.image', 'icon'=>'odi.gif'), |
5395334d |
380 | |
a370c895 |
381 | 'pct' => array ('type'=>'image/pict', 'icon'=>'image.gif'), |
382 | 'pdf' => array ('type'=>'application/pdf', 'icon'=>'pdf.gif'), |
383 | 'php' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
384 | 'pic' => array ('type'=>'image/pict', 'icon'=>'image.gif'), |
385 | 'pict' => array ('type'=>'image/pict', 'icon'=>'image.gif'), |
386 | 'png' => array ('type'=>'image/png', 'icon'=>'image.gif'), |
387 | 'pps' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'powerpoint.gif'), |
388 | 'ppt' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'powerpoint.gif'), |
68da9722 |
389 | 'pptx' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'pptx.gif'), |
390 | 'pptm' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'pptm.gif'), |
391 | 'potx' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'potx.gif'), |
392 | 'potm' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'potm.gif'), |
393 | 'ppam' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'ppam.gif'), |
394 | 'ppsx' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'ppsx.gif'), |
395 | 'ppsm' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'ppsm.gif'), |
a370c895 |
396 | 'ps' => array ('type'=>'application/postscript', 'icon'=>'pdf.gif'), |
397 | 'qt' => array ('type'=>'video/quicktime', 'icon'=>'video.gif'), |
398 | 'ra' => array ('type'=>'audio/x-realaudio', 'icon'=>'audio.gif'), |
399 | 'ram' => array ('type'=>'audio/x-pn-realaudio', 'icon'=>'audio.gif'), |
a00420fb |
400 | 'rhb' => array ('type'=>'text/xml', 'icon'=>'xml.gif'), |
a370c895 |
401 | 'rm' => array ('type'=>'audio/x-pn-realaudio', 'icon'=>'audio.gif'), |
402 | 'rtf' => array ('type'=>'text/rtf', 'icon'=>'text.gif'), |
403 | 'rtx' => array ('type'=>'text/richtext', 'icon'=>'text.gif'), |
404 | 'sh' => array ('type'=>'application/x-sh', 'icon'=>'text.gif'), |
405 | 'sit' => array ('type'=>'application/x-stuffit', 'icon'=>'zip.gif'), |
406 | 'smi' => array ('type'=>'application/smil', 'icon'=>'text.gif'), |
407 | 'smil' => array ('type'=>'application/smil', 'icon'=>'text.gif'), |
a00420fb |
408 | 'sqt' => array ('type'=>'text/xml', 'icon'=>'xml.gif'), |
4db69ffb |
409 | 'svg' => array ('type'=>'image/svg+xml', 'icon'=>'image.gif'), |
410 | 'svgz' => array ('type'=>'image/svg+xml', 'icon'=>'image.gif'), |
a370c895 |
411 | 'swa' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'), |
412 | 'swf' => array ('type'=>'application/x-shockwave-flash', 'icon'=>'flash.gif'), |
413 | 'swfl' => array ('type'=>'application/x-shockwave-flash', 'icon'=>'flash.gif'), |
5395334d |
414 | |
415 | 'sxw' => array ('type'=>'application/vnd.sun.xml.writer', 'icon'=>'odt.gif'), |
416 | 'stw' => array ('type'=>'application/vnd.sun.xml.writer.template', 'icon'=>'odt.gif'), |
417 | 'sxc' => array ('type'=>'application/vnd.sun.xml.calc', 'icon'=>'odt.gif'), |
418 | 'stc' => array ('type'=>'application/vnd.sun.xml.calc.template', 'icon'=>'odt.gif'), |
419 | 'sxd' => array ('type'=>'application/vnd.sun.xml.draw', 'icon'=>'odt.gif'), |
420 | 'std' => array ('type'=>'application/vnd.sun.xml.draw.template', 'icon'=>'odt.gif'), |
421 | 'sxi' => array ('type'=>'application/vnd.sun.xml.impress', 'icon'=>'odt.gif'), |
422 | 'sti' => array ('type'=>'application/vnd.sun.xml.impress.template', 'icon'=>'odt.gif'), |
423 | 'sxg' => array ('type'=>'application/vnd.sun.xml.writer.global', 'icon'=>'odt.gif'), |
424 | 'sxm' => array ('type'=>'application/vnd.sun.xml.math', 'icon'=>'odt.gif'), |
425 | |
a370c895 |
426 | 'tar' => array ('type'=>'application/x-tar', 'icon'=>'zip.gif'), |
427 | 'tif' => array ('type'=>'image/tiff', 'icon'=>'image.gif'), |
428 | 'tiff' => array ('type'=>'image/tiff', 'icon'=>'image.gif'), |
429 | 'tex' => array ('type'=>'application/x-tex', 'icon'=>'text.gif'), |
430 | 'texi' => array ('type'=>'application/x-texinfo', 'icon'=>'text.gif'), |
431 | 'texinfo' => array ('type'=>'application/x-texinfo', 'icon'=>'text.gif'), |
432 | 'tsv' => array ('type'=>'text/tab-separated-values', 'icon'=>'text.gif'), |
433 | 'txt' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
434 | 'wav' => array ('type'=>'audio/wav', 'icon'=>'audio.gif'), |
435 | 'wmv' => array ('type'=>'video/x-ms-wmv', 'icon'=>'avi.gif'), |
436 | 'asf' => array ('type'=>'video/x-ms-asf', 'icon'=>'avi.gif'), |
ee7f231d |
437 | 'xdp' => array ('type'=>'application/pdf', 'icon'=>'pdf.gif'), |
438 | 'xfd' => array ('type'=>'application/pdf', 'icon'=>'pdf.gif'), |
439 | 'xfdf' => array ('type'=>'application/pdf', 'icon'=>'pdf.gif'), |
a370c895 |
440 | 'xls' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'excel.gif'), |
68da9722 |
441 | 'xlsx' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'xlsx.gif'), |
442 | 'xlsm' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'xlsm.gif'), |
443 | 'xltx' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'xltx.gif'), |
444 | 'xltm' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'xltm.gif'), |
445 | 'xlsb' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'xlsb.gif'), |
446 | 'xlam' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'xlam.gif'), |
a370c895 |
447 | 'xml' => array ('type'=>'application/xml', 'icon'=>'xml.gif'), |
448 | 'xsl' => array ('type'=>'text/xml', 'icon'=>'xml.gif'), |
449 | 'zip' => array ('type'=>'application/zip', 'icon'=>'zip.gif') |
f1e0649c |
450 | ); |
3ce73b14 |
451 | } |
452 | |
76ca1ff1 |
453 | /** |
3ce73b14 |
454 | * Obtains information about a filetype based on its extension. Will |
455 | * use a default if no information is present about that particular |
456 | * extension. |
76ca1ff1 |
457 | * @param string $element Desired information (usually 'icon' |
3ce73b14 |
458 | * for icon filename or 'type' for MIME type) |
76ca1ff1 |
459 | * @param string $filename Filename we're looking up |
3ce73b14 |
460 | * @return string Requested piece of information from array |
461 | */ |
462 | function mimeinfo($element, $filename) { |
f36cbf1d |
463 | static $mimeinfo = null; |
464 | if (is_null($mimeinfo)) { |
465 | $mimeinfo = get_mimetypes_array(); |
466 | } |
f1e0649c |
467 | |
a370c895 |
468 | if (eregi('\.([a-z0-9]+)$', $filename, $match)) { |
f1e0649c |
469 | if (isset($mimeinfo[strtolower($match[1])][$element])) { |
470 | return $mimeinfo[strtolower($match[1])][$element]; |
471 | } else { |
a370c895 |
472 | return $mimeinfo['xxx'][$element]; // By default |
f1e0649c |
473 | } |
474 | } else { |
a370c895 |
475 | return $mimeinfo['xxx'][$element]; // By default |
f1e0649c |
476 | } |
477 | } |
478 | |
76ca1ff1 |
479 | /** |
3ce73b14 |
480 | * Obtains information about a filetype based on the MIME type rather than |
481 | * the other way around. |
482 | * @param string $element Desired information (usually 'icon') |
76ca1ff1 |
483 | * @param string $mimetype MIME type we're looking up |
3ce73b14 |
484 | * @return string Requested piece of information from array |
485 | */ |
486 | function mimeinfo_from_type($element, $mimetype) { |
487 | static $mimeinfo; |
488 | $mimeinfo=get_mimetypes_array(); |
76ca1ff1 |
489 | |
3ce73b14 |
490 | foreach($mimeinfo as $values) { |
491 | if($values['type']==$mimetype) { |
492 | if(isset($values[$element])) { |
493 | return $values[$element]; |
494 | } |
495 | break; |
496 | } |
497 | } |
498 | return $mimeinfo['xxx'][$element]; // Default |
499 | } |
b9709b76 |
500 | |
42ead7d7 |
501 | /** |
502 | * Get information about a filetype based on the icon file. |
503 | * @param string $element Desired information (usually 'icon') |
504 | * @param string $icon Icon file path. |
505 | * @return string Requested piece of information from array |
506 | */ |
507 | function mimeinfo_from_icon($element, $icon) { |
508 | static $mimeinfo; |
509 | $mimeinfo=get_mimetypes_array(); |
510 | |
511 | if (preg_match("/\/(.*)/", $icon, $matches)) { |
512 | $icon = $matches[1]; |
513 | } |
514 | $info = $mimeinfo['xxx'][$element]; // Default |
515 | foreach($mimeinfo as $values) { |
516 | if($values['icon']==$icon) { |
517 | if(isset($values[$element])) { |
518 | $info = $values[$element]; |
519 | } |
520 | //No break, for example for 'excel.gif' we don't want 'csv'! |
521 | } |
522 | } |
523 | return $info; |
524 | } |
525 | |
c0381e22 |
526 | /** |
76ca1ff1 |
527 | * Obtains descriptions for file types (e.g. 'Microsoft Word document') from the |
528 | * mimetypes.php language file. |
c0381e22 |
529 | * @param string $mimetype MIME type (can be obtained using the mimeinfo function) |
530 | * @param bool $capitalise If true, capitalises first character of result |
76ca1ff1 |
531 | * @return string Text description |
c0381e22 |
532 | */ |
533 | function get_mimetype_description($mimetype,$capitalise=false) { |
534 | $result=get_string($mimetype,'mimetypes'); |
535 | // Surrounded by square brackets indicates that there isn't a string for that |
536 | // (maybe there is a better way to find this out?) |
537 | if(strpos($result,'[')===0) { |
538 | $result=get_string('document/unknown','mimetypes'); |
76ca1ff1 |
539 | } |
c0381e22 |
540 | if($capitalise) { |
541 | $result=ucfirst($result); |
542 | } |
543 | return $result; |
544 | } |
545 | |
76ca1ff1 |
546 | /** |
547 | * Handles the sending of file data to the user's browser, including support for |
548 | * byteranges etc. |
ba75ad94 |
549 | * @param string $path Path of file on disk (including real filename), or actual content of file as string |
550 | * @param string $filename Filename to send |
551 | * @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours) |
552 | * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only |
553 | * @param bool $pathisstring If true (default false), $path is the content to send and not the pathname |
554 | * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin |
555 | * @param string $mimetype Include to specify the MIME type; leave blank to have it guess the type from $filename |
b9709b76 |
556 | */ |
ba75ad94 |
557 | function send_file($path, $filename, $lifetime=86400 , $filter=0, $pathisstring=false, $forcedownload=false, $mimetype='') { |
6800d78e |
558 | global $CFG, $COURSE, $SESSION; |
f1e0649c |
559 | |
ba4e0b05 |
560 | // Use given MIME type if specified, otherwise guess it using mimeinfo. |
561 | // IE, Konqueror and Opera open html file directly in browser from web even when directed to save it to disk :-O |
562 | // only Firefox saves all files locally before opening when content-disposition: attachment stated |
563 | $isFF = check_browser_version('Firefox', '1.5'); // only FF > 1.5 properly tested |
76ca1ff1 |
564 | $mimetype = ($forcedownload and !$isFF) ? 'application/x-forcedownload' : |
ba4e0b05 |
565 | ($mimetype ? $mimetype : mimeinfo('type', $filename)); |
f1e0649c |
566 | $lastmodified = $pathisstring ? time() : filemtime($path); |
567 | $filesize = $pathisstring ? strlen($path) : filesize($path); |
568 | |
36bddcf5 |
569 | /* - MDL-13949 |
ee7f231d |
570 | //Adobe Acrobat Reader XSS prevention |
571 | if ($mimetype=='application/pdf' or mimeinfo('type', $filename)=='application/pdf') { |
572 | //please note that it prevents opening of pdfs in browser when http referer disabled |
573 | //or file linked from another site; browser caching of pdfs is now disabled too |
c57d8874 |
574 | if (!empty($_SERVER['HTTP_RANGE'])) { |
575 | //already byteserving |
76ca1ff1 |
576 | $lifetime = 1; // >0 needed for byteserving |
c57d8874 |
577 | } else if (empty($_SERVER['HTTP_REFERER']) or strpos($_SERVER['HTTP_REFERER'], $CFG->wwwroot)!==0) { |
ee7f231d |
578 | $mimetype = 'application/x-forcedownload'; |
579 | $forcedownload = true; |
580 | $lifetime = 0; |
581 | } else { |
76ca1ff1 |
582 | $lifetime = 1; // >0 needed for byteserving |
ee7f231d |
583 | } |
b8806ccc |
584 | } |
36bddcf5 |
585 | */ |
f3f7610c |
586 | |
69faecce |
587 | //IE compatibiltiy HACK! |
4c8c65ec |
588 | if (ini_get('zlib.output_compression')) { |
69faecce |
589 | ini_set('zlib.output_compression', 'Off'); |
590 | } |
591 | |
4c8c65ec |
592 | //try to disable automatic sid rewrite in cookieless mode |
8914cb82 |
593 | @ini_set("session.use_trans_sid", "false"); |
4c8c65ec |
594 | |
595 | //do not put '@' before the next header to detect incorrect moodle configurations, |
596 | //error should be better than "weird" empty lines for admins/users |
597 | //TODO: should we remove all those @ before the header()? Are all of the values supported on all servers? |
598 | header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT'); |
599 | |
4f047de2 |
600 | // if user is using IE, urlencode the filename so that multibyte file name will show up correctly on popup |
601 | if (check_browser_version('MSIE')) { |
4638009b |
602 | $filename = rawurlencode($filename); |
4f047de2 |
603 | } |
604 | |
4c8c65ec |
605 | if ($forcedownload) { |
4638009b |
606 | @header('Content-Disposition: attachment; filename="'.$filename.'"'); |
4c8c65ec |
607 | } else { |
4638009b |
608 | @header('Content-Disposition: inline; filename="'.$filename.'"'); |
4c8c65ec |
609 | } |
610 | |
f1e0649c |
611 | if ($lifetime > 0) { |
4c8c65ec |
612 | @header('Cache-Control: max-age='.$lifetime); |
613 | @header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT'); |
f1e0649c |
614 | @header('Pragma: '); |
4c8c65ec |
615 | |
616 | if (empty($CFG->disablebyteserving) && !$pathisstring && $mimetype != 'text/plain' && $mimetype != 'text/html') { |
617 | |
618 | @header('Accept-Ranges: bytes'); |
619 | |
620 | if (!empty($_SERVER['HTTP_RANGE']) && strpos($_SERVER['HTTP_RANGE'],'bytes=') !== FALSE) { |
621 | // byteserving stuff - for acrobat reader and download accelerators |
622 | // see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35 |
623 | // inspired by: http://www.coneural.org/florian/papers/04_byteserving.php |
624 | $ranges = false; |
625 | if (preg_match_all('/(\d*)-(\d*)/', $_SERVER['HTTP_RANGE'], $ranges, PREG_SET_ORDER)) { |
626 | foreach ($ranges as $key=>$value) { |
627 | if ($ranges[$key][1] == '') { |
628 | //suffix case |
629 | $ranges[$key][1] = $filesize - $ranges[$key][2]; |
630 | $ranges[$key][2] = $filesize - 1; |
631 | } else if ($ranges[$key][2] == '' || $ranges[$key][2] > $filesize - 1) { |
632 | //fix range length |
633 | $ranges[$key][2] = $filesize - 1; |
634 | } |
635 | if ($ranges[$key][2] != '' && $ranges[$key][2] < $ranges[$key][1]) { |
636 | //invalid byte-range ==> ignore header |
637 | $ranges = false; |
638 | break; |
639 | } |
640 | //prepare multipart header |
641 | $ranges[$key][0] = "\r\n--".BYTESERVING_BOUNDARY."\r\nContent-Type: $mimetype\r\n"; |
642 | $ranges[$key][0] .= "Content-Range: bytes {$ranges[$key][1]}-{$ranges[$key][2]}/$filesize\r\n\r\n"; |
643 | } |
644 | } else { |
645 | $ranges = false; |
646 | } |
647 | if ($ranges) { |
648 | byteserving_send_file($path, $mimetype, $ranges); |
649 | } |
650 | } |
651 | } else { |
652 | /// Do not byteserve (disabled, strings, text and html files). |
653 | @header('Accept-Ranges: none'); |
654 | } |
655 | } else { // Do not cache files in proxies and browsers |
85e00626 |
656 | if (strpos($CFG->wwwroot, 'https://') === 0) { //https sites - watch out for IE! KB812935 and KB316431 |
657 | @header('Cache-Control: max-age=10'); |
4c8c65ec |
658 | @header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT'); |
85e00626 |
659 | @header('Pragma: '); |
660 | } else { //normal http - prevent caching at all cost |
661 | @header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0'); |
4c8c65ec |
662 | @header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT'); |
85e00626 |
663 | @header('Pragma: no-cache'); |
664 | } |
4c8c65ec |
665 | @header('Accept-Ranges: none'); // Do not allow byteserving when caching disabled |
69faecce |
666 | } |
f1e0649c |
667 | |
b9709b76 |
668 | if (empty($filter)) { |
4c8c65ec |
669 | if ($mimetype == 'text/html' && !empty($CFG->usesid) && empty($_COOKIE['MoodleSession'.$CFG->sessioncookie])) { |
670 | //cookieless mode - rewrite links |
671 | @header('Content-Type: text/html'); |
672 | $path = $pathisstring ? $path : implode('', file($path)); |
6800d78e |
673 | $path = $SESSION->sid_ob_rewrite($path); |
4c8c65ec |
674 | $filesize = strlen($path); |
675 | $pathisstring = true; |
676 | } else if ($mimetype == 'text/plain') { |
810944af |
677 | @header('Content-Type: Text/plain; charset=utf-8'); //add encoding |
f1e0649c |
678 | } else { |
4c8c65ec |
679 | @header('Content-Type: '.$mimetype); |
f1e0649c |
680 | } |
4c8c65ec |
681 | @header('Content-Length: '.$filesize); |
682 | while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite |
f1e0649c |
683 | if ($pathisstring) { |
684 | echo $path; |
4c8c65ec |
685 | } else { |
69faecce |
686 | readfile_chunked($path); |
f1e0649c |
687 | } |
688 | } else { // Try to put the file through filters |
f1e0649c |
689 | if ($mimetype == 'text/html') { |
a17c57b5 |
690 | $options = new object(); |
f1e0649c |
691 | $options->noclean = true; |
a17c57b5 |
692 | $options->nocache = true; // temporary workaround for MDL-5136 |
f1e0649c |
693 | $text = $pathisstring ? $path : implode('', file($path)); |
76ca1ff1 |
694 | |
3ace5ee4 |
695 | $text = file_modify_html_header($text); |
60f9e36e |
696 | $output = format_text($text, FORMAT_HTML, $options, $COURSE->id); |
4c8c65ec |
697 | if (!empty($CFG->usesid) && empty($_COOKIE['MoodleSession'.$CFG->sessioncookie])) { |
698 | //cookieless mode - rewrite links |
6800d78e |
699 | $output = $SESSION->sid_ob_rewrite($output); |
4c8c65ec |
700 | } |
f1e0649c |
701 | |
4c8c65ec |
702 | @header('Content-Length: '.strlen($output)); |
703 | @header('Content-Type: text/html'); |
704 | while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite |
f1e0649c |
705 | echo $output; |
b9709b76 |
706 | // only filter text if filter all files is selected |
707 | } else if (($mimetype == 'text/plain') and ($filter == 1)) { |
60f9e36e |
708 | $options = new object(); |
f1e0649c |
709 | $options->newlines = false; |
710 | $options->noclean = true; |
711 | $text = htmlentities($pathisstring ? $path : implode('', file($path))); |
60f9e36e |
712 | $output = '<pre>'. format_text($text, FORMAT_MOODLE, $options, $COURSE->id) .'</pre>'; |
4c8c65ec |
713 | if (!empty($CFG->usesid) && empty($_COOKIE['MoodleSession'.$CFG->sessioncookie])) { |
714 | //cookieless mode - rewrite links |
6800d78e |
715 | $output = $SESSION->sid_ob_rewrite($output); |
4c8c65ec |
716 | } |
f1e0649c |
717 | |
4c8c65ec |
718 | @header('Content-Length: '.strlen($output)); |
810944af |
719 | @header('Content-Type: text/html; charset=utf-8'); //add encoding |
4c8c65ec |
720 | while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite |
f1e0649c |
721 | echo $output; |
722 | } else { // Just send it out raw |
4c8c65ec |
723 | @header('Content-Length: '.$filesize); |
724 | @header('Content-Type: '.$mimetype); |
725 | while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite |
f1e0649c |
726 | if ($pathisstring) { |
727 | echo $path; |
728 | }else { |
69faecce |
729 | readfile_chunked($path); |
f1e0649c |
730 | } |
731 | } |
732 | } |
733 | die; //no more chars to output!!! |
734 | } |
735 | |
a43b5308 |
736 | function get_records_csv($file, $table) { |
f33e1ed4 |
737 | global $CFG, $DB; |
599f38f9 |
738 | |
f33e1ed4 |
739 | if (!$metacolumns = $DB->get_columns($table)) { |
599f38f9 |
740 | return false; |
741 | } |
742 | |
a77b98eb |
743 | if(!($handle = @fopen($file, 'r'))) { |
5a2a5331 |
744 | print_error('get_records_csv failed to open '.$file); |
599f38f9 |
745 | } |
746 | |
747 | $fieldnames = fgetcsv($handle, 4096); |
748 | if(empty($fieldnames)) { |
749 | fclose($handle); |
750 | return false; |
751 | } |
752 | |
753 | $columns = array(); |
754 | |
755 | foreach($metacolumns as $metacolumn) { |
756 | $ord = array_search($metacolumn->name, $fieldnames); |
757 | if(is_int($ord)) { |
758 | $columns[$metacolumn->name] = $ord; |
759 | } |
760 | } |
761 | |
762 | $rows = array(); |
763 | |
764 | while (($data = fgetcsv($handle, 4096)) !== false) { |
765 | $item = new stdClass; |
766 | foreach($columns as $name => $ord) { |
767 | $item->$name = $data[$ord]; |
768 | } |
769 | $rows[] = $item; |
770 | } |
771 | |
772 | fclose($handle); |
773 | return $rows; |
774 | } |
775 | |
a77b98eb |
776 | function put_records_csv($file, $records, $table = NULL) { |
f33e1ed4 |
777 | global $CFG, $DB; |
a77b98eb |
778 | |
a1e93da2 |
779 | if (empty($records)) { |
a77b98eb |
780 | return true; |
781 | } |
782 | |
783 | $metacolumns = NULL; |
f33e1ed4 |
784 | if ($table !== NULL && !$metacolumns = $DB->get_columns($table)) { |
a77b98eb |
785 | return false; |
786 | } |
787 | |
a1e93da2 |
788 | echo "x"; |
789 | |
a77b98eb |
790 | if(!($fp = @fopen($CFG->dataroot.'/temp/'.$file, 'w'))) { |
5a2a5331 |
791 | print_error('put_records_csv failed to open '.$file); |
a77b98eb |
792 | } |
793 | |
a43b5308 |
794 | $proto = reset($records); |
795 | if(is_object($proto)) { |
796 | $fields_records = array_keys(get_object_vars($proto)); |
797 | } |
798 | else if(is_array($proto)) { |
799 | $fields_records = array_keys($proto); |
800 | } |
801 | else { |
802 | return false; |
803 | } |
a1e93da2 |
804 | echo "x"; |
a77b98eb |
805 | |
806 | if(!empty($metacolumns)) { |
807 | $fields_table = array_map(create_function('$a', 'return $a->name;'), $metacolumns); |
808 | $fields = array_intersect($fields_records, $fields_table); |
809 | } |
810 | else { |
811 | $fields = $fields_records; |
812 | } |
813 | |
814 | fwrite($fp, implode(',', $fields)); |
815 | fwrite($fp, "\r\n"); |
816 | |
817 | foreach($records as $record) { |
a43b5308 |
818 | $array = (array)$record; |
a77b98eb |
819 | $values = array(); |
820 | foreach($fields as $field) { |
a43b5308 |
821 | if(strpos($array[$field], ',')) { |
822 | $values[] = '"'.str_replace('"', '\"', $array[$field]).'"'; |
a77b98eb |
823 | } |
824 | else { |
a43b5308 |
825 | $values[] = $array[$field]; |
a77b98eb |
826 | } |
827 | } |
828 | fwrite($fp, implode(',', $values)."\r\n"); |
829 | } |
830 | |
831 | fclose($fp); |
832 | return true; |
833 | } |
834 | |
f401cc97 |
835 | |
34763a79 |
836 | /** |
76ca1ff1 |
837 | * Recursively delete the file or folder with path $location. That is, |
34763a79 |
838 | * if it is a file delete it. If it is a folder, delete all its content |
76ca1ff1 |
839 | * then delete it. If $location does not exist to start, that is not |
840 | * considered an error. |
841 | * |
34763a79 |
842 | * @param $location the path to remove. |
843 | */ |
4c8c65ec |
844 | function fulldelete($location) { |
f401cc97 |
845 | if (is_dir($location)) { |
846 | $currdir = opendir($location); |
847 | while (false !== ($file = readdir($currdir))) { |
848 | if ($file <> ".." && $file <> ".") { |
849 | $fullfile = $location."/".$file; |
4c8c65ec |
850 | if (is_dir($fullfile)) { |
f401cc97 |
851 | if (!fulldelete($fullfile)) { |
852 | return false; |
853 | } |
854 | } else { |
855 | if (!unlink($fullfile)) { |
856 | return false; |
857 | } |
4c8c65ec |
858 | } |
f401cc97 |
859 | } |
4c8c65ec |
860 | } |
f401cc97 |
861 | closedir($currdir); |
862 | if (! rmdir($location)) { |
863 | return false; |
864 | } |
865 | |
34763a79 |
866 | } else if (file_exists($location)) { |
f401cc97 |
867 | if (!unlink($location)) { |
868 | return false; |
869 | } |
870 | } |
871 | return true; |
872 | } |
873 | |
4c8c65ec |
874 | /** |
875 | * Improves memory consumptions and works around buggy readfile() in PHP 5.0.4 (2MB readfile limit). |
876 | */ |
877 | function readfile_chunked($filename, $retbytes=true) { |
878 | $chunksize = 1*(1024*1024); // 1MB chunks - must be less than 2MB! |
69faecce |
879 | $buffer = ''; |
76ca1ff1 |
880 | $cnt =0; |
69faecce |
881 | $handle = fopen($filename, 'rb'); |
882 | if ($handle === false) { |
883 | return false; |
884 | } |
20371063 |
885 | |
69faecce |
886 | while (!feof($handle)) { |
68913aec |
887 | @set_time_limit(60*60); //reset time limit to 60 min - should be enough for 1 MB chunk |
69faecce |
888 | $buffer = fread($handle, $chunksize); |
889 | echo $buffer; |
20371063 |
890 | flush(); |
69faecce |
891 | if ($retbytes) { |
4c8c65ec |
892 | $cnt += strlen($buffer); |
893 | } |
69faecce |
894 | } |
895 | $status = fclose($handle); |
896 | if ($retbytes && $status) { |
897 | return $cnt; // return num. bytes delivered like readfile() does. |
898 | } |
899 | return $status; |
900 | } |
901 | |
4c8c65ec |
902 | /** |
903 | * Send requested byterange of file. |
904 | */ |
905 | function byteserving_send_file($filename, $mimetype, $ranges) { |
906 | $chunksize = 1*(1024*1024); // 1MB chunks - must be less than 2MB! |
907 | $handle = fopen($filename, 'rb'); |
908 | if ($handle === false) { |
909 | die; |
910 | } |
911 | if (count($ranges) == 1) { //only one range requested |
912 | $length = $ranges[0][2] - $ranges[0][1] + 1; |
913 | @header('HTTP/1.1 206 Partial content'); |
914 | @header('Content-Length: '.$length); |
915 | @header('Content-Range: bytes '.$ranges[0][1].'-'.$ranges[0][2].'/'.filesize($filename)); |
916 | @header('Content-Type: '.$mimetype); |
917 | while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite |
918 | $buffer = ''; |
919 | fseek($handle, $ranges[0][1]); |
920 | while (!feof($handle) && $length > 0) { |
68913aec |
921 | @set_time_limit(60*60); //reset time limit to 60 min - should be enough for 1 MB chunk |
4c8c65ec |
922 | $buffer = fread($handle, ($chunksize < $length ? $chunksize : $length)); |
923 | echo $buffer; |
924 | flush(); |
925 | $length -= strlen($buffer); |
926 | } |
927 | fclose($handle); |
928 | die; |
929 | } else { // multiple ranges requested - not tested much |
930 | $totallength = 0; |
931 | foreach($ranges as $range) { |
aba588a7 |
932 | $totallength += strlen($range[0]) + $range[2] - $range[1] + 1; |
4c8c65ec |
933 | } |
aba588a7 |
934 | $totallength += strlen("\r\n--".BYTESERVING_BOUNDARY."--\r\n"); |
4c8c65ec |
935 | @header('HTTP/1.1 206 Partial content'); |
936 | @header('Content-Length: '.$totallength); |
937 | @header('Content-Type: multipart/byteranges; boundary='.BYTESERVING_BOUNDARY); |
938 | //TODO: check if "multipart/x-byteranges" is more compatible with current readers/browsers/servers |
939 | while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite |
940 | foreach($ranges as $range) { |
941 | $length = $range[2] - $range[1] + 1; |
942 | echo $range[0]; |
943 | $buffer = ''; |
944 | fseek($handle, $range[1]); |
945 | while (!feof($handle) && $length > 0) { |
68913aec |
946 | @set_time_limit(60*60); //reset time limit to 60 min - should be enough for 1 MB chunk |
4c8c65ec |
947 | $buffer = fread($handle, ($chunksize < $length ? $chunksize : $length)); |
948 | echo $buffer; |
949 | flush(); |
950 | $length -= strlen($buffer); |
951 | } |
952 | } |
953 | echo "\r\n--".BYTESERVING_BOUNDARY."--\r\n"; |
954 | fclose($handle); |
955 | die; |
956 | } |
957 | } |
f401cc97 |
958 | |
3ace5ee4 |
959 | /** |
960 | * add includes (js and css) into uploaded files |
961 | * before returning them, useful for themes and utf.js includes |
962 | * @param string text - text to search and replace |
963 | * @return string - text with added head includes |
964 | */ |
965 | function file_modify_html_header($text) { |
966 | // first look for <head> tag |
967 | global $CFG; |
76ca1ff1 |
968 | |
3ace5ee4 |
969 | $stylesheetshtml = ''; |
970 | foreach ($CFG->stylesheets as $stylesheet) { |
971 | $stylesheetshtml .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n"; |
76ca1ff1 |
972 | } |
973 | |
3ace5ee4 |
974 | $filters = explode(",", $CFG->textfilters); |
975 | if (in_array('filter/mediaplugin', $filters)) { |
76ca1ff1 |
976 | // this script is needed by most media filter plugins. |
977 | $ufo = "\n".'<script type="text/javascript" src="'.$CFG->wwwroot.'/lib/ufo.js"></script>'."\n"; |
3ace5ee4 |
978 | } else { |
76ca1ff1 |
979 | $ufo = ''; |
3ace5ee4 |
980 | } |
76ca1ff1 |
981 | |
3ace5ee4 |
982 | preg_match('/\<head\>|\<HEAD\>/', $text, $matches); |
983 | if ($matches) { |
984 | $replacement = '<head>'.$ufo.$stylesheetshtml; |
985 | $text = preg_replace('/\<head\>|\<HEAD\>/', $replacement, $text, 1); |
76ca1ff1 |
986 | return $text; |
3ace5ee4 |
987 | } |
76ca1ff1 |
988 | |
3ace5ee4 |
989 | // if not, look for <html> tag, and stick <head> right after |
990 | preg_match('/\<html\>|\<HTML\>/', $text, $matches); |
991 | if ($matches) { |
992 | // replace <html> tag with <html><head>includes</head> |
13534ef7 |
993 | $replacement = '<html>'."\n".'<head>'.$ufo.$stylesheetshtml.'</head>'; |
3ace5ee4 |
994 | $text = preg_replace('/\<html\>|\<HTML\>/', $replacement, $text, 1); |
76ca1ff1 |
995 | return $text; |
3ace5ee4 |
996 | } |
76ca1ff1 |
997 | |
3ace5ee4 |
998 | // if not, look for <body> tag, and stick <head> before body |
999 | preg_match('/\<body\>|\<BODY\>/', $text, $matches); |
1000 | if ($matches) { |
13534ef7 |
1001 | $replacement = '<head>'.$ufo.$stylesheetshtml.'</head>'."\n".'<body>'; |
3ace5ee4 |
1002 | $text = preg_replace('/\<body\>|\<BODY\>/', $replacement, $text, 1); |
76ca1ff1 |
1003 | return $text; |
1004 | } |
1005 | |
3ace5ee4 |
1006 | // if not, just stick a <head> tag at the beginning |
1007 | $text = '<head>'.$ufo.$stylesheetshtml.'</head>'."\n".$text; |
1008 | return $text; |
1009 | } |
1010 | |
a77b98eb |
1011 | ?> |