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