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