599f38f9 |
1 | <?php //$Id$ |
2 | |
8ee88311 |
3 | require_once($CFG->libdir.'/libcurlemu/libcurlemu.inc.php'); // might be moved to setup.php later |
4 | |
4c8c65ec |
5 | define('BYTESERVING_BOUNDARY', 's1k2o3d4a5k6s7'); //unique string constant |
6 | |
8ee88311 |
7 | /** |
8 | * Fetches content of file from Internet (using proxy if defined). |
9 | * |
10 | * @return mixed false if request failed or content of the file as string if ok. |
11 | */ |
12 | function download_file_content($url) { |
13 | $ch = curl_init($url); |
14 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
15 | curl_setopt($ch, CURLOPT_HEADER, false); |
16 | if (!empty($CFG->proxyhost)) { |
17 | curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true); |
18 | if (empty($CFG->proxyport)) { |
19 | curl_setopt($ch, CURLOPT_PROXY, $CFG->proxy); |
20 | } else { |
21 | curl_setopt($ch, CURLOPT_PROXY, $CFG->proxy.':'.$CFG->proxyport); |
22 | } |
23 | if(!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) { |
24 | curl_setopt($ch, CURLOPT_PROXYUSERPWD, $CFG->proxyuser.':'.$CFG->proxypassword); |
25 | } |
26 | } |
27 | $result = curl_exec($ch); |
28 | curl_close($ch); |
29 | return $result; |
30 | } |
31 | |
3ce73b14 |
32 | /** |
33 | * @return List of information about file types based on extensions. |
34 | * Associative array of extension (lower-case) to associative array |
35 | * from 'element name' to data. Current element names are 'type' and 'icon'. |
36 | * Unknown types should use the 'xxx' entry which includes defaults. |
37 | */ |
38 | function get_mimetypes_array() { |
39 | return array ( |
a370c895 |
40 | 'xxx' => array ('type'=>'document/unknown', 'icon'=>'unknown.gif'), |
41 | '3gp' => array ('type'=>'video/quicktime', 'icon'=>'video.gif'), |
42 | 'ai' => array ('type'=>'application/postscript', 'icon'=>'image.gif'), |
43 | 'aif' => array ('type'=>'audio/x-aiff', 'icon'=>'audio.gif'), |
44 | 'aiff' => array ('type'=>'audio/x-aiff', 'icon'=>'audio.gif'), |
45 | 'aifc' => array ('type'=>'audio/x-aiff', 'icon'=>'audio.gif'), |
46 | 'applescript' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
47 | 'asc' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
18bf47ef |
48 | 'asm' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
a370c895 |
49 | 'au' => array ('type'=>'audio/au', 'icon'=>'audio.gif'), |
50 | 'avi' => array ('type'=>'video/x-ms-wm', 'icon'=>'avi.gif'), |
51 | 'bmp' => array ('type'=>'image/bmp', 'icon'=>'image.gif'), |
18bf47ef |
52 | 'c' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
a370c895 |
53 | 'cct' => array ('type'=>'shockwave/director', 'icon'=>'flash.gif'), |
18bf47ef |
54 | 'cpp' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
a370c895 |
55 | 'cs' => array ('type'=>'application/x-csh', 'icon'=>'text.gif'), |
56 | 'css' => array ('type'=>'text/css', 'icon'=>'text.gif'), |
57 | 'dv' => array ('type'=>'video/x-dv', 'icon'=>'video.gif'), |
609d84e3 |
58 | 'dmg' => array ('type'=>'application/octet-stream', 'icon'=>'dmg.gif'), |
a370c895 |
59 | 'doc' => array ('type'=>'application/msword', 'icon'=>'word.gif'), |
60 | 'dcr' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'), |
61 | 'dif' => array ('type'=>'video/x-dv', 'icon'=>'video.gif'), |
62 | 'dir' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'), |
63 | 'dxr' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'), |
64 | 'eps' => array ('type'=>'application/postscript', 'icon'=>'pdf.gif'), |
ee7f231d |
65 | 'fdf' => array ('type'=>'application/pdf', 'icon'=>'pdf.gif'), |
a370c895 |
66 | 'gif' => array ('type'=>'image/gif', 'icon'=>'image.gif'), |
67 | 'gtar' => array ('type'=>'application/x-gtar', 'icon'=>'zip.gif'), |
609d84e3 |
68 | 'tgz' => array ('type'=>'application/g-zip', 'icon'=>'zip.gif'), |
a370c895 |
69 | 'gz' => array ('type'=>'application/g-zip', 'icon'=>'zip.gif'), |
70 | 'gzip' => array ('type'=>'application/g-zip', 'icon'=>'zip.gif'), |
71 | 'h' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
18bf47ef |
72 | 'hpp' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
a370c895 |
73 | 'hqx' => array ('type'=>'application/mac-binhex40', 'icon'=>'zip.gif'), |
70ee2841 |
74 | 'htc' => array ('type'=>'text/x-component', 'icon'=>'text.gif'), |
a370c895 |
75 | 'html' => array ('type'=>'text/html', 'icon'=>'html.gif'), |
76 | 'htm' => array ('type'=>'text/html', 'icon'=>'html.gif'), |
18bf47ef |
77 | 'java' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
a00420fb |
78 | 'jcb' => array ('type'=>'text/xml', 'icon'=>'jcb.gif'), |
79 | 'jcl' => array ('type'=>'text/xml', 'icon'=>'jcl.gif'), |
80 | 'jcw' => array ('type'=>'text/xml', 'icon'=>'jcw.gif'), |
81 | 'jmt' => array ('type'=>'text/xml', 'icon'=>'jmt.gif'), |
82 | 'jmx' => array ('type'=>'text/xml', 'icon'=>'jmx.gif'), |
a370c895 |
83 | 'jpe' => array ('type'=>'image/jpeg', 'icon'=>'image.gif'), |
84 | 'jpeg' => array ('type'=>'image/jpeg', 'icon'=>'image.gif'), |
85 | 'jpg' => array ('type'=>'image/jpeg', 'icon'=>'image.gif'), |
a00420fb |
86 | 'jqz' => array ('type'=>'text/xml', 'icon'=>'jqz.gif'), |
a370c895 |
87 | 'js' => array ('type'=>'application/x-javascript', 'icon'=>'text.gif'), |
88 | 'latex'=> array ('type'=>'application/x-latex', 'icon'=>'text.gif'), |
89 | 'm' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
90 | 'mov' => array ('type'=>'video/quicktime', 'icon'=>'video.gif'), |
91 | 'movie'=> array ('type'=>'video/x-sgi-movie', 'icon'=>'video.gif'), |
92 | 'm3u' => array ('type'=>'audio/x-mpegurl', 'icon'=>'audio.gif'), |
93 | 'mp3' => array ('type'=>'audio/mp3', 'icon'=>'audio.gif'), |
94 | 'mp4' => array ('type'=>'video/mp4', 'icon'=>'video.gif'), |
95 | 'mpeg' => array ('type'=>'video/mpeg', 'icon'=>'video.gif'), |
96 | 'mpe' => array ('type'=>'video/mpeg', 'icon'=>'video.gif'), |
97 | 'mpg' => array ('type'=>'video/mpeg', 'icon'=>'video.gif'), |
5395334d |
98 | |
99 | 'odt' => array ('type'=>'application/vnd.oasis.opendocument.text', 'icon'=>'odt.gif'), |
100 | 'ott' => array ('type'=>'application/vnd.oasis.opendocument.text-template', 'icon'=>'odt.gif'), |
101 | 'oth' => array ('type'=>'application/vnd.oasis.opendocument.text-web', 'icon'=>'odt.gif'), |
102 | 'odm' => array ('type'=>'application/vnd.oasis.opendocument.text-master', 'icon'=>'odt.gif'), |
103 | 'odg' => array ('type'=>'application/vnd.oasis.opendocument.graphics', 'icon'=>'odt.gif'), |
104 | 'otg' => array ('type'=>'application/vnd.oasis.opendocument.graphics-template', 'icon'=>'odt.gif'), |
105 | 'odp' => array ('type'=>'application/vnd.oasis.opendocument.presentation', 'icon'=>'odt.gif'), |
106 | 'otp' => array ('type'=>'application/vnd.oasis.opendocument.presentation-template', 'icon'=>'odt.gif'), |
107 | 'ods' => array ('type'=>'application/vnd.oasis.opendocument.spreadsheet', 'icon'=>'odt.gif'), |
108 | 'ots' => array ('type'=>'application/vnd.oasis.opendocument.spreadsheet-template', 'icon'=>'odt.gif'), |
109 | 'odc' => array ('type'=>'application/vnd.oasis.opendocument.chart', 'icon'=>'odt.gif'), |
110 | 'odf' => array ('type'=>'application/vnd.oasis.opendocument.formula', 'icon'=>'odt.gif'), |
111 | 'odb' => array ('type'=>'application/vnd.oasis.opendocument.database', 'icon'=>'odt.gif'), |
112 | 'odi' => array ('type'=>'application/vnd.oasis.opendocument.image', 'icon'=>'odt.gif'), |
113 | |
a370c895 |
114 | 'pct' => array ('type'=>'image/pict', 'icon'=>'image.gif'), |
115 | 'pdf' => array ('type'=>'application/pdf', 'icon'=>'pdf.gif'), |
116 | 'php' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
117 | 'pic' => array ('type'=>'image/pict', 'icon'=>'image.gif'), |
118 | 'pict' => array ('type'=>'image/pict', 'icon'=>'image.gif'), |
119 | 'png' => array ('type'=>'image/png', 'icon'=>'image.gif'), |
120 | 'pps' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'powerpoint.gif'), |
121 | 'ppt' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'powerpoint.gif'), |
122 | 'ps' => array ('type'=>'application/postscript', 'icon'=>'pdf.gif'), |
123 | 'qt' => array ('type'=>'video/quicktime', 'icon'=>'video.gif'), |
124 | 'ra' => array ('type'=>'audio/x-realaudio', 'icon'=>'audio.gif'), |
125 | 'ram' => array ('type'=>'audio/x-pn-realaudio', 'icon'=>'audio.gif'), |
a00420fb |
126 | 'rhb' => array ('type'=>'text/xml', 'icon'=>'xml.gif'), |
a370c895 |
127 | 'rm' => array ('type'=>'audio/x-pn-realaudio', 'icon'=>'audio.gif'), |
128 | 'rtf' => array ('type'=>'text/rtf', 'icon'=>'text.gif'), |
129 | 'rtx' => array ('type'=>'text/richtext', 'icon'=>'text.gif'), |
130 | 'sh' => array ('type'=>'application/x-sh', 'icon'=>'text.gif'), |
131 | 'sit' => array ('type'=>'application/x-stuffit', 'icon'=>'zip.gif'), |
132 | 'smi' => array ('type'=>'application/smil', 'icon'=>'text.gif'), |
133 | 'smil' => array ('type'=>'application/smil', 'icon'=>'text.gif'), |
a00420fb |
134 | 'sqt' => array ('type'=>'text/xml', 'icon'=>'xml.gif'), |
4db69ffb |
135 | 'svg' => array ('type'=>'image/svg+xml', 'icon'=>'image.gif'), |
136 | 'svgz' => array ('type'=>'image/svg+xml', 'icon'=>'image.gif'), |
a370c895 |
137 | 'swa' => array ('type'=>'application/x-director', 'icon'=>'flash.gif'), |
138 | 'swf' => array ('type'=>'application/x-shockwave-flash', 'icon'=>'flash.gif'), |
139 | 'swfl' => array ('type'=>'application/x-shockwave-flash', 'icon'=>'flash.gif'), |
5395334d |
140 | |
141 | 'sxw' => array ('type'=>'application/vnd.sun.xml.writer', 'icon'=>'odt.gif'), |
142 | 'stw' => array ('type'=>'application/vnd.sun.xml.writer.template', 'icon'=>'odt.gif'), |
143 | 'sxc' => array ('type'=>'application/vnd.sun.xml.calc', 'icon'=>'odt.gif'), |
144 | 'stc' => array ('type'=>'application/vnd.sun.xml.calc.template', 'icon'=>'odt.gif'), |
145 | 'sxd' => array ('type'=>'application/vnd.sun.xml.draw', 'icon'=>'odt.gif'), |
146 | 'std' => array ('type'=>'application/vnd.sun.xml.draw.template', 'icon'=>'odt.gif'), |
147 | 'sxi' => array ('type'=>'application/vnd.sun.xml.impress', 'icon'=>'odt.gif'), |
148 | 'sti' => array ('type'=>'application/vnd.sun.xml.impress.template', 'icon'=>'odt.gif'), |
149 | 'sxg' => array ('type'=>'application/vnd.sun.xml.writer.global', 'icon'=>'odt.gif'), |
150 | 'sxm' => array ('type'=>'application/vnd.sun.xml.math', 'icon'=>'odt.gif'), |
151 | |
a370c895 |
152 | 'tar' => array ('type'=>'application/x-tar', 'icon'=>'zip.gif'), |
153 | 'tif' => array ('type'=>'image/tiff', 'icon'=>'image.gif'), |
154 | 'tiff' => array ('type'=>'image/tiff', 'icon'=>'image.gif'), |
155 | 'tex' => array ('type'=>'application/x-tex', 'icon'=>'text.gif'), |
156 | 'texi' => array ('type'=>'application/x-texinfo', 'icon'=>'text.gif'), |
157 | 'texinfo' => array ('type'=>'application/x-texinfo', 'icon'=>'text.gif'), |
158 | 'tsv' => array ('type'=>'text/tab-separated-values', 'icon'=>'text.gif'), |
159 | 'txt' => array ('type'=>'text/plain', 'icon'=>'text.gif'), |
160 | 'wav' => array ('type'=>'audio/wav', 'icon'=>'audio.gif'), |
161 | 'wmv' => array ('type'=>'video/x-ms-wmv', 'icon'=>'avi.gif'), |
162 | 'asf' => array ('type'=>'video/x-ms-asf', 'icon'=>'avi.gif'), |
ee7f231d |
163 | 'xdp' => array ('type'=>'application/pdf', 'icon'=>'pdf.gif'), |
164 | 'xfd' => array ('type'=>'application/pdf', 'icon'=>'pdf.gif'), |
165 | 'xfdf' => array ('type'=>'application/pdf', 'icon'=>'pdf.gif'), |
a370c895 |
166 | 'xls' => array ('type'=>'application/vnd.ms-excel', 'icon'=>'excel.gif'), |
167 | 'xml' => array ('type'=>'application/xml', 'icon'=>'xml.gif'), |
168 | 'xsl' => array ('type'=>'text/xml', 'icon'=>'xml.gif'), |
169 | 'zip' => array ('type'=>'application/zip', 'icon'=>'zip.gif') |
f1e0649c |
170 | ); |
3ce73b14 |
171 | } |
172 | |
173 | /** |
174 | * Obtains information about a filetype based on its extension. Will |
175 | * use a default if no information is present about that particular |
176 | * extension. |
177 | * @param string $element Desired information (usually 'icon' |
178 | * for icon filename or 'type' for MIME type) |
179 | * @param string $filename Filename we're looking up |
180 | * @return string Requested piece of information from array |
181 | */ |
182 | function mimeinfo($element, $filename) { |
183 | static $mimeinfo; |
184 | $mimeinfo=get_mimetypes_array(); |
f1e0649c |
185 | |
a370c895 |
186 | if (eregi('\.([a-z0-9]+)$', $filename, $match)) { |
f1e0649c |
187 | if (isset($mimeinfo[strtolower($match[1])][$element])) { |
188 | return $mimeinfo[strtolower($match[1])][$element]; |
189 | } else { |
a370c895 |
190 | return $mimeinfo['xxx'][$element]; // By default |
f1e0649c |
191 | } |
192 | } else { |
a370c895 |
193 | return $mimeinfo['xxx'][$element]; // By default |
f1e0649c |
194 | } |
195 | } |
196 | |
3ce73b14 |
197 | /** |
198 | * Obtains information about a filetype based on the MIME type rather than |
199 | * the other way around. |
200 | * @param string $element Desired information (usually 'icon') |
201 | * @param string $mimetype MIME type we're looking up |
202 | * @return string Requested piece of information from array |
203 | */ |
204 | function mimeinfo_from_type($element, $mimetype) { |
205 | static $mimeinfo; |
206 | $mimeinfo=get_mimetypes_array(); |
207 | |
208 | foreach($mimeinfo as $values) { |
209 | if($values['type']==$mimetype) { |
210 | if(isset($values[$element])) { |
211 | return $values[$element]; |
212 | } |
213 | break; |
214 | } |
215 | } |
216 | return $mimeinfo['xxx'][$element]; // Default |
217 | } |
b9709b76 |
218 | |
c0381e22 |
219 | /** |
220 | * Obtains descriptions for file types (e.g. 'Microsoft Word document') from the |
221 | * mimetypes.php language file. |
222 | * @param string $mimetype MIME type (can be obtained using the mimeinfo function) |
223 | * @param bool $capitalise If true, capitalises first character of result |
224 | * @return string Text description |
225 | */ |
226 | function get_mimetype_description($mimetype,$capitalise=false) { |
227 | $result=get_string($mimetype,'mimetypes'); |
228 | // Surrounded by square brackets indicates that there isn't a string for that |
229 | // (maybe there is a better way to find this out?) |
230 | if(strpos($result,'[')===0) { |
231 | $result=get_string('document/unknown','mimetypes'); |
232 | } |
233 | if($capitalise) { |
234 | $result=ucfirst($result); |
235 | } |
236 | return $result; |
237 | } |
238 | |
b9709b76 |
239 | /** |
ba75ad94 |
240 | * Handles the sending of file data to the user's browser, including support for |
241 | * byteranges etc. |
242 | * @param string $path Path of file on disk (including real filename), or actual content of file as string |
243 | * @param string $filename Filename to send |
244 | * @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours) |
245 | * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only |
246 | * @param bool $pathisstring If true (default false), $path is the content to send and not the pathname |
247 | * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin |
248 | * @param string $mimetype Include to specify the MIME type; leave blank to have it guess the type from $filename |
b9709b76 |
249 | */ |
ba75ad94 |
250 | function send_file($path, $filename, $lifetime=86400 , $filter=0, $pathisstring=false, $forcedownload=false, $mimetype='') { |
60f9e36e |
251 | global $CFG, $COURSE; |
f1e0649c |
252 | |
ba4e0b05 |
253 | // Use given MIME type if specified, otherwise guess it using mimeinfo. |
254 | // IE, Konqueror and Opera open html file directly in browser from web even when directed to save it to disk :-O |
255 | // only Firefox saves all files locally before opening when content-disposition: attachment stated |
256 | $isFF = check_browser_version('Firefox', '1.5'); // only FF > 1.5 properly tested |
257 | $mimetype = ($forcedownload and !$isFF) ? 'application/x-forcedownload' : |
258 | ($mimetype ? $mimetype : mimeinfo('type', $filename)); |
f1e0649c |
259 | $lastmodified = $pathisstring ? time() : filemtime($path); |
260 | $filesize = $pathisstring ? strlen($path) : filesize($path); |
261 | |
ee7f231d |
262 | //Adobe Acrobat Reader XSS prevention |
263 | if ($mimetype=='application/pdf' or mimeinfo('type', $filename)=='application/pdf') { |
264 | //please note that it prevents opening of pdfs in browser when http referer disabled |
265 | //or file linked from another site; browser caching of pdfs is now disabled too |
266 | if (empty($_SERVER['HTTP_REFERER']) or strpos($_SERVER['HTTP_REFERER'], $CFG->wwwroot)!==0) { |
267 | $mimetype = 'application/x-forcedownload'; |
268 | $forcedownload = true; |
269 | $lifetime = 0; |
270 | } else { |
271 | $lifetime = 1; // >0 needed for byteserving |
272 | } |
b8806ccc |
273 | } |
f3f7610c |
274 | |
69faecce |
275 | //IE compatibiltiy HACK! |
4c8c65ec |
276 | if (ini_get('zlib.output_compression')) { |
69faecce |
277 | ini_set('zlib.output_compression', 'Off'); |
278 | } |
279 | |
4c8c65ec |
280 | //try to disable automatic sid rewrite in cookieless mode |
8914cb82 |
281 | @ini_set("session.use_trans_sid", "false"); |
4c8c65ec |
282 | |
283 | //do not put '@' before the next header to detect incorrect moodle configurations, |
284 | //error should be better than "weird" empty lines for admins/users |
285 | //TODO: should we remove all those @ before the header()? Are all of the values supported on all servers? |
286 | header('Last-Modified: '. gmdate('D, d M Y H:i:s', $lastmodified) .' GMT'); |
287 | |
288 | if ($forcedownload) { |
289 | @header('Content-Disposition: attachment; filename='.$filename); |
290 | } else { |
291 | @header('Content-Disposition: inline; filename='.$filename); |
292 | } |
293 | |
f1e0649c |
294 | if ($lifetime > 0) { |
4c8c65ec |
295 | @header('Cache-Control: max-age='.$lifetime); |
296 | @header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT'); |
f1e0649c |
297 | @header('Pragma: '); |
4c8c65ec |
298 | |
299 | if (empty($CFG->disablebyteserving) && !$pathisstring && $mimetype != 'text/plain' && $mimetype != 'text/html') { |
300 | |
301 | @header('Accept-Ranges: bytes'); |
302 | |
303 | if (!empty($_SERVER['HTTP_RANGE']) && strpos($_SERVER['HTTP_RANGE'],'bytes=') !== FALSE) { |
304 | // byteserving stuff - for acrobat reader and download accelerators |
305 | // see: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35 |
306 | // inspired by: http://www.coneural.org/florian/papers/04_byteserving.php |
307 | $ranges = false; |
308 | if (preg_match_all('/(\d*)-(\d*)/', $_SERVER['HTTP_RANGE'], $ranges, PREG_SET_ORDER)) { |
309 | foreach ($ranges as $key=>$value) { |
310 | if ($ranges[$key][1] == '') { |
311 | //suffix case |
312 | $ranges[$key][1] = $filesize - $ranges[$key][2]; |
313 | $ranges[$key][2] = $filesize - 1; |
314 | } else if ($ranges[$key][2] == '' || $ranges[$key][2] > $filesize - 1) { |
315 | //fix range length |
316 | $ranges[$key][2] = $filesize - 1; |
317 | } |
318 | if ($ranges[$key][2] != '' && $ranges[$key][2] < $ranges[$key][1]) { |
319 | //invalid byte-range ==> ignore header |
320 | $ranges = false; |
321 | break; |
322 | } |
323 | //prepare multipart header |
324 | $ranges[$key][0] = "\r\n--".BYTESERVING_BOUNDARY."\r\nContent-Type: $mimetype\r\n"; |
325 | $ranges[$key][0] .= "Content-Range: bytes {$ranges[$key][1]}-{$ranges[$key][2]}/$filesize\r\n\r\n"; |
326 | } |
327 | } else { |
328 | $ranges = false; |
329 | } |
330 | if ($ranges) { |
331 | byteserving_send_file($path, $mimetype, $ranges); |
332 | } |
333 | } |
334 | } else { |
335 | /// Do not byteserve (disabled, strings, text and html files). |
336 | @header('Accept-Ranges: none'); |
337 | } |
338 | } else { // Do not cache files in proxies and browsers |
85e00626 |
339 | if (strpos($CFG->wwwroot, 'https://') === 0) { //https sites - watch out for IE! KB812935 and KB316431 |
340 | @header('Cache-Control: max-age=10'); |
4c8c65ec |
341 | @header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT'); |
85e00626 |
342 | @header('Pragma: '); |
343 | } else { //normal http - prevent caching at all cost |
344 | @header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0'); |
4c8c65ec |
345 | @header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT'); |
85e00626 |
346 | @header('Pragma: no-cache'); |
347 | } |
4c8c65ec |
348 | @header('Accept-Ranges: none'); // Do not allow byteserving when caching disabled |
69faecce |
349 | } |
f1e0649c |
350 | |
b9709b76 |
351 | if (empty($filter)) { |
4c8c65ec |
352 | if ($mimetype == 'text/html' && !empty($CFG->usesid) && empty($_COOKIE['MoodleSession'.$CFG->sessioncookie])) { |
353 | //cookieless mode - rewrite links |
354 | @header('Content-Type: text/html'); |
355 | $path = $pathisstring ? $path : implode('', file($path)); |
356 | $path = sid_ob_rewrite($path); |
357 | $filesize = strlen($path); |
358 | $pathisstring = true; |
359 | } else if ($mimetype == 'text/plain') { |
810944af |
360 | @header('Content-Type: Text/plain; charset=utf-8'); //add encoding |
f1e0649c |
361 | } else { |
4c8c65ec |
362 | @header('Content-Type: '.$mimetype); |
f1e0649c |
363 | } |
4c8c65ec |
364 | @header('Content-Length: '.$filesize); |
365 | while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite |
f1e0649c |
366 | if ($pathisstring) { |
367 | echo $path; |
4c8c65ec |
368 | } else { |
69faecce |
369 | readfile_chunked($path); |
f1e0649c |
370 | } |
371 | } else { // Try to put the file through filters |
f1e0649c |
372 | if ($mimetype == 'text/html') { |
a17c57b5 |
373 | $options = new object(); |
f1e0649c |
374 | $options->noclean = true; |
a17c57b5 |
375 | $options->nocache = true; // temporary workaround for MDL-5136 |
f1e0649c |
376 | $text = $pathisstring ? $path : implode('', file($path)); |
60f9e36e |
377 | $output = format_text($text, FORMAT_HTML, $options, $COURSE->id); |
4c8c65ec |
378 | if (!empty($CFG->usesid) && empty($_COOKIE['MoodleSession'.$CFG->sessioncookie])) { |
379 | //cookieless mode - rewrite links |
380 | $output = sid_ob_rewrite($output); |
381 | } |
f1e0649c |
382 | |
4c8c65ec |
383 | @header('Content-Length: '.strlen($output)); |
384 | @header('Content-Type: text/html'); |
385 | while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite |
f1e0649c |
386 | echo $output; |
b9709b76 |
387 | // only filter text if filter all files is selected |
388 | } else if (($mimetype == 'text/plain') and ($filter == 1)) { |
60f9e36e |
389 | $options = new object(); |
f1e0649c |
390 | $options->newlines = false; |
391 | $options->noclean = true; |
392 | $text = htmlentities($pathisstring ? $path : implode('', file($path))); |
60f9e36e |
393 | $output = '<pre>'. format_text($text, FORMAT_MOODLE, $options, $COURSE->id) .'</pre>'; |
4c8c65ec |
394 | if (!empty($CFG->usesid) && empty($_COOKIE['MoodleSession'.$CFG->sessioncookie])) { |
395 | //cookieless mode - rewrite links |
396 | $output = sid_ob_rewrite($output); |
397 | } |
f1e0649c |
398 | |
4c8c65ec |
399 | @header('Content-Length: '.strlen($output)); |
810944af |
400 | @header('Content-Type: text/html; charset=utf-8'); //add encoding |
4c8c65ec |
401 | while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite |
f1e0649c |
402 | echo $output; |
403 | } else { // Just send it out raw |
4c8c65ec |
404 | @header('Content-Length: '.$filesize); |
405 | @header('Content-Type: '.$mimetype); |
406 | while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite |
f1e0649c |
407 | if ($pathisstring) { |
408 | echo $path; |
409 | }else { |
69faecce |
410 | readfile_chunked($path); |
f1e0649c |
411 | } |
412 | } |
413 | } |
414 | die; //no more chars to output!!! |
415 | } |
416 | |
a43b5308 |
417 | function get_records_csv($file, $table) { |
599f38f9 |
418 | global $CFG, $db; |
419 | |
420 | if (!$metacolumns = $db->MetaColumns($CFG->prefix . $table)) { |
421 | return false; |
422 | } |
423 | |
a77b98eb |
424 | if(!($handle = @fopen($file, 'r'))) { |
599f38f9 |
425 | error('get_records_csv failed to open '.$file); |
426 | } |
427 | |
428 | $fieldnames = fgetcsv($handle, 4096); |
429 | if(empty($fieldnames)) { |
430 | fclose($handle); |
431 | return false; |
432 | } |
433 | |
434 | $columns = array(); |
435 | |
436 | foreach($metacolumns as $metacolumn) { |
437 | $ord = array_search($metacolumn->name, $fieldnames); |
438 | if(is_int($ord)) { |
439 | $columns[$metacolumn->name] = $ord; |
440 | } |
441 | } |
442 | |
443 | $rows = array(); |
444 | |
445 | while (($data = fgetcsv($handle, 4096)) !== false) { |
446 | $item = new stdClass; |
447 | foreach($columns as $name => $ord) { |
448 | $item->$name = $data[$ord]; |
449 | } |
450 | $rows[] = $item; |
451 | } |
452 | |
453 | fclose($handle); |
454 | return $rows; |
455 | } |
456 | |
a77b98eb |
457 | function put_records_csv($file, $records, $table = NULL) { |
458 | global $CFG, $db; |
459 | |
a1e93da2 |
460 | if (empty($records)) { |
a77b98eb |
461 | return true; |
462 | } |
463 | |
464 | $metacolumns = NULL; |
465 | if ($table !== NULL && !$metacolumns = $db->MetaColumns($CFG->prefix . $table)) { |
466 | return false; |
467 | } |
468 | |
a1e93da2 |
469 | echo "x"; |
470 | |
a77b98eb |
471 | if(!($fp = @fopen($CFG->dataroot.'/temp/'.$file, 'w'))) { |
472 | error('put_records_csv failed to open '.$file); |
473 | } |
474 | |
a43b5308 |
475 | $proto = reset($records); |
476 | if(is_object($proto)) { |
477 | $fields_records = array_keys(get_object_vars($proto)); |
478 | } |
479 | else if(is_array($proto)) { |
480 | $fields_records = array_keys($proto); |
481 | } |
482 | else { |
483 | return false; |
484 | } |
a1e93da2 |
485 | echo "x"; |
a77b98eb |
486 | |
487 | if(!empty($metacolumns)) { |
488 | $fields_table = array_map(create_function('$a', 'return $a->name;'), $metacolumns); |
489 | $fields = array_intersect($fields_records, $fields_table); |
490 | } |
491 | else { |
492 | $fields = $fields_records; |
493 | } |
494 | |
495 | fwrite($fp, implode(',', $fields)); |
496 | fwrite($fp, "\r\n"); |
497 | |
498 | foreach($records as $record) { |
a43b5308 |
499 | $array = (array)$record; |
a77b98eb |
500 | $values = array(); |
501 | foreach($fields as $field) { |
a43b5308 |
502 | if(strpos($array[$field], ',')) { |
503 | $values[] = '"'.str_replace('"', '\"', $array[$field]).'"'; |
a77b98eb |
504 | } |
505 | else { |
a43b5308 |
506 | $values[] = $array[$field]; |
a77b98eb |
507 | } |
508 | } |
509 | fwrite($fp, implode(',', $values)."\r\n"); |
510 | } |
511 | |
512 | fclose($fp); |
513 | return true; |
514 | } |
515 | |
f401cc97 |
516 | |
34763a79 |
517 | /** |
518 | * Recursively delete the file or folder with path $location. That is, |
519 | * if it is a file delete it. If it is a folder, delete all its content |
520 | * then delete it. If $location does not exist to start, that is not |
521 | * considered an error. |
522 | * |
523 | * @param $location the path to remove. |
524 | */ |
4c8c65ec |
525 | function fulldelete($location) { |
f401cc97 |
526 | if (is_dir($location)) { |
527 | $currdir = opendir($location); |
528 | while (false !== ($file = readdir($currdir))) { |
529 | if ($file <> ".." && $file <> ".") { |
530 | $fullfile = $location."/".$file; |
4c8c65ec |
531 | if (is_dir($fullfile)) { |
f401cc97 |
532 | if (!fulldelete($fullfile)) { |
533 | return false; |
534 | } |
535 | } else { |
536 | if (!unlink($fullfile)) { |
537 | return false; |
538 | } |
4c8c65ec |
539 | } |
f401cc97 |
540 | } |
4c8c65ec |
541 | } |
f401cc97 |
542 | closedir($currdir); |
543 | if (! rmdir($location)) { |
544 | return false; |
545 | } |
546 | |
34763a79 |
547 | } else if (file_exists($location)) { |
f401cc97 |
548 | if (!unlink($location)) { |
549 | return false; |
550 | } |
551 | } |
552 | return true; |
553 | } |
554 | |
4c8c65ec |
555 | /** |
556 | * Improves memory consumptions and works around buggy readfile() in PHP 5.0.4 (2MB readfile limit). |
557 | */ |
558 | function readfile_chunked($filename, $retbytes=true) { |
559 | $chunksize = 1*(1024*1024); // 1MB chunks - must be less than 2MB! |
69faecce |
560 | $buffer = ''; |
561 | $cnt =0;// $handle = fopen($filename, 'rb'); |
562 | $handle = fopen($filename, 'rb'); |
563 | if ($handle === false) { |
564 | return false; |
565 | } |
20371063 |
566 | |
69faecce |
567 | while (!feof($handle)) { |
68913aec |
568 | @set_time_limit(60*60); //reset time limit to 60 min - should be enough for 1 MB chunk |
69faecce |
569 | $buffer = fread($handle, $chunksize); |
570 | echo $buffer; |
20371063 |
571 | flush(); |
69faecce |
572 | if ($retbytes) { |
4c8c65ec |
573 | $cnt += strlen($buffer); |
574 | } |
69faecce |
575 | } |
576 | $status = fclose($handle); |
577 | if ($retbytes && $status) { |
578 | return $cnt; // return num. bytes delivered like readfile() does. |
579 | } |
580 | return $status; |
581 | } |
582 | |
4c8c65ec |
583 | /** |
584 | * Send requested byterange of file. |
585 | */ |
586 | function byteserving_send_file($filename, $mimetype, $ranges) { |
587 | $chunksize = 1*(1024*1024); // 1MB chunks - must be less than 2MB! |
588 | $handle = fopen($filename, 'rb'); |
589 | if ($handle === false) { |
590 | die; |
591 | } |
592 | if (count($ranges) == 1) { //only one range requested |
593 | $length = $ranges[0][2] - $ranges[0][1] + 1; |
594 | @header('HTTP/1.1 206 Partial content'); |
595 | @header('Content-Length: '.$length); |
596 | @header('Content-Range: bytes '.$ranges[0][1].'-'.$ranges[0][2].'/'.filesize($filename)); |
597 | @header('Content-Type: '.$mimetype); |
598 | while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite |
599 | $buffer = ''; |
600 | fseek($handle, $ranges[0][1]); |
601 | while (!feof($handle) && $length > 0) { |
68913aec |
602 | @set_time_limit(60*60); //reset time limit to 60 min - should be enough for 1 MB chunk |
4c8c65ec |
603 | $buffer = fread($handle, ($chunksize < $length ? $chunksize : $length)); |
604 | echo $buffer; |
605 | flush(); |
606 | $length -= strlen($buffer); |
607 | } |
608 | fclose($handle); |
609 | die; |
610 | } else { // multiple ranges requested - not tested much |
611 | $totallength = 0; |
612 | foreach($ranges as $range) { |
aba588a7 |
613 | $totallength += strlen($range[0]) + $range[2] - $range[1] + 1; |
4c8c65ec |
614 | } |
aba588a7 |
615 | $totallength += strlen("\r\n--".BYTESERVING_BOUNDARY."--\r\n"); |
4c8c65ec |
616 | @header('HTTP/1.1 206 Partial content'); |
617 | @header('Content-Length: '.$totallength); |
618 | @header('Content-Type: multipart/byteranges; boundary='.BYTESERVING_BOUNDARY); |
619 | //TODO: check if "multipart/x-byteranges" is more compatible with current readers/browsers/servers |
620 | while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite |
621 | foreach($ranges as $range) { |
622 | $length = $range[2] - $range[1] + 1; |
623 | echo $range[0]; |
624 | $buffer = ''; |
625 | fseek($handle, $range[1]); |
626 | while (!feof($handle) && $length > 0) { |
68913aec |
627 | @set_time_limit(60*60); //reset time limit to 60 min - should be enough for 1 MB chunk |
4c8c65ec |
628 | $buffer = fread($handle, ($chunksize < $length ? $chunksize : $length)); |
629 | echo $buffer; |
630 | flush(); |
631 | $length -= strlen($buffer); |
632 | } |
633 | } |
634 | echo "\r\n--".BYTESERVING_BOUNDARY."--\r\n"; |
635 | fclose($handle); |
636 | die; |
637 | } |
638 | } |
f401cc97 |
639 | |
a77b98eb |
640 | ?> |