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