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