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