forked from socialskeptic/sainsburys
/
file.php
853 lines (740 loc) · 23.8 KB
/
file.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
<?php
/**
* Part of the Fuel framework.
*
* @package Fuel
* @version 1.7
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2015 Fuel Development Team
* @link http://fuelphp.com
*/
namespace Fuel\Core;
class FileAccessException extends \FuelException {}
class OutsideAreaException extends \OutOfBoundsException {}
class InvalidPathException extends \FileAccessException {}
// ------------------------------------------------------------------------
/**
* File Class
*
* @package Fuel
* @subpackage Core
* @category Core
*/
class File
{
/**
* @var array loaded area's
*/
protected static $areas = array();
public static function _init()
{
\Config::load('file', true);
// make sure the configured chmod values are octal
$chmod = \Config::get('file.chmod.folders', 0777);
is_string($chmod) and \Config::set('file.chmod.folders', octdec($chmod));
$chmod = \Config::get('file.chmod.files', 0666);
is_string($chmod) and \Config::set('file.chmod.files', octdec($chmod));
static::$areas[null] = \File_Area::forge(\Config::get('file.base_config', array()));
foreach (\Config::get('file.areas', array()) as $name => $config)
{
static::$areas[$name] = \File_Area::forge($config);
}
}
public static function forge(array $config = array())
{
return \File_Area::forge($config);
}
/**
* Instance
*
* @param string|File_Area|null file area name, object or null for base area
* @return File_Area
*/
public static function instance($area = null)
{
if ($area instanceof File_Area)
{
return $area;
}
$instance = array_key_exists($area, static::$areas) ? static::$areas[$area] : false;
if ($instance === false)
{
throw new \InvalidArgumentException('There is no file instance named "'.$area.'".');
}
return $instance;
}
/**
* File & directory objects factory
*
* @param string path to the file or directory
* @param array configuration items
* @param string|File_Area|null file area name, object or null for base area
* @return File_Handler_File
*/
public static function get($path, array $config = array(), $area = null)
{
return static::instance($area)->get_handler($path, $config);
}
/**
* Get the url.
*
* @return bool
*/
public static function get_url($path, array $config = array(), $area = null)
{
return static::get($path, $config, $area)->get_url();
}
/**
* Check for file existence
*
* @param string path to file to check
* @param string|File_Area|null file area name, object or null for base area
* @return bool
*/
public static function exists($path, $area = null)
{
$path = rtrim(static::instance($area)->get_path($path), '\\/');
// resolve symlinks
while ($path and is_link($path))
{
$path = readlink($path);
}
return is_file($path);
}
/**
* Create an empty file
*
* @param string directory where to create file
* @param string filename
* @param string|File_Area|null file area name, object or null for base area
* @return bool
*/
public static function create($basepath, $name, $contents = null, $area = null)
{
$basepath = rtrim(static::instance($area)->get_path($basepath), '\\/').DS;
$new_file = static::instance($area)->get_path($basepath.$name);
if ( ! is_dir($basepath) or ! is_writable($basepath))
{
throw new \InvalidPathException('Invalid basepath: "'.$basepath.'", cannot create file at this location.');
}
elseif (is_file($new_file))
{
throw new \FileAccessException('File: "'.$new_file.'" already exists, cannot be created.');
}
$file = static::open_file(@fopen($new_file, 'c'), true, $area);
fwrite($file, $contents);
static::close_file($file, $area);
return true;
}
/**
* Create an empty directory
*
* @param string directory where to create new dir
* @param string dirname
* @param int (octal) file permissions
* @param string|File_Area|null file area name, object or null for non-specific
* @return bool
*/
public static function create_dir($basepath, $name, $chmod = null, $area = null)
{
$basepath = rtrim(static::instance($area)->get_path($basepath), '\\/').DS;
$new_dir = static::instance($area)->get_path($basepath.trim($name, '\\/'));
is_null($chmod) and $chmod = \Config::get('file.chmod.folders', 0777);
if ( ! is_dir($basepath) or ! is_writable($basepath))
{
throw new \InvalidPathException('Invalid basepath: "'.$basepath.'", cannot create directory at this location.');
}
elseif (is_dir($new_dir))
{
throw new \FileAccessException('Directory: "'.$new_dir.'" exists already, cannot be created.');
}
// unify the path separators, and get the part we need to add to the basepath
$new_dir = substr(str_replace(array('\\', '/'), DS, $new_dir), strpos($new_dir, $name));
// recursively create the directory. we can't use mkdir permissions or recursive
// due to the fact that mkdir is restricted by the current users umask
$basepath = rtrim($basepath, DS);
foreach (explode(DS, $new_dir) as $dir)
{
$basepath .= DS.$dir;
if ( ! is_dir($basepath))
{
try
{
if ( ! mkdir($basepath))
{
return false;
}
chmod($basepath, $chmod);
}
catch (\PHPErrorException $e)
{
return false;
}
}
}
return true;
}
/**
* Read file
*
* @param string file to read
* @param bool whether to use readfile() or file_get_contents()
* @param string|File_Area|null file area name, object or null for base area
* @return IO|string file contents
*/
public static function read($path, $as_string = false, $area = null)
{
$path = static::instance($area)->get_path($path);
if ( ! is_file($path))
{
throw new \InvalidPathException('Cannot read file: "'.$path.'", file does not exists.');
}
$file = static::open_file(@fopen($path, 'r'), LOCK_SH, $area);
$return = $as_string ? file_get_contents($path) : readfile($path);
static::close_file($file, $area);
return $return;
}
/**
* Read directory
*
* @param string directory to read
* @param int depth to recurse directory, 1 is only current and 0 or smaller is unlimited
* @param Array|null array of partial regexes or non-array for default
* @param string|File_Area|null file area name, object or null for base area
* @return array directory contents in an array
*/
public static function read_dir($path, $depth = 0, $filter = null, $area = null)
{
$path = rtrim(static::instance($area)->get_path($path), '\\/').DS;
if ( ! is_dir($path))
{
throw new \InvalidPathException('Invalid path: "'.$path.'", directory cannot be read.');
}
if ( ! $fp = @opendir($path))
{
throw new \FileAccessException('Could not open directory: "'.$path.'" for reading.');
}
// Use default when not set
if ( ! is_array($filter))
{
$filter = array('!^\.');
if ($extensions = static::instance($area)->extensions())
{
foreach($extensions as $ext)
{
$filter[] = '\.'.$ext.'$';
}
}
}
$files = array();
$dirs = array();
$new_depth = $depth - 1;
while (false !== ($file = readdir($fp)))
{
// Remove '.', '..'
if (in_array($file, array('.', '..')))
{
continue;
}
// use filters when given
elseif ( ! empty($filter))
{
$continue = false; // whether or not to continue
$matched = false; // whether any positive pattern matched
$positive = false; // whether positive filters are present
foreach($filter as $f => $type)
{
if (is_numeric($f))
{
// generic rule
$f = $type;
}
else
{
// type specific rule
$is_file = is_file($path.$file);
if (($type === 'file' and ! $is_file) or ($type !== 'file' and $is_file))
{
continue;
}
}
$not = substr($f, 0, 1) === '!'; // whether it's a negative condition
$f = $not ? substr($f, 1) : $f;
// on negative condition a match leads to a continue
if (($match = preg_match('/'.$f.'/uiD', $file) > 0) and $not)
{
$continue = true;
}
$positive = $positive ?: ! $not; // whether a positive condition was encountered
$matched = $matched ?: ($match and ! $not); // whether one of the filters has matched
}
// continue when negative matched or when positive filters and nothing matched
if ($continue or $positive and ! $matched)
{
continue;
}
}
if (@is_dir($path.$file))
{
// Use recursion when depth not depleted or not limited...
if ($depth < 1 or $new_depth > 0)
{
$dirs[$file.DS] = static::read_dir($path.$file.DS, $new_depth, $filter, $area);
}
// ... or set dir to false when not read
else
{
$dirs[$file.DS] = false;
}
}
else
{
$files[] = $file;
}
}
closedir($fp);
// sort dirs & files naturally and return array with dirs on top and files
uksort($dirs, 'strnatcasecmp');
natcasesort($files);
return array_merge($dirs, $files);
}
/**
* Update a file
*
* @param string directory where to write the file
* @param string filename
* @param string|File_Area|null file area name, object or null for base area
* @return bool
*/
public static function update($basepath, $name, $contents = null, $area = null)
{
$basepath = rtrim(static::instance($area)->get_path($basepath), '\\/').DS;
$new_file = static::instance($area)->get_path($basepath.$name);
if ( ! $file = static::open_file(@fopen($new_file, 'w'), true, $area))
{
if ( ! is_dir($basepath) or ! is_writable($basepath))
{
throw new \InvalidPathException('Invalid basepath: "'.$basepath.'", cannot update a file at this location.');
}
throw new \FileAccessException('No write access to: "'.$basepath.'", cannot update a file.');
}
fwrite($file, $contents);
static::close_file($file, $area);
return true;
}
/**
* Append to a file
*
* @param string directory where to write the file
* @param string filename
* @param string|File_Area|null file area name, object or null for base area
* @return bool
*/
public static function append($basepath, $name, $contents = null, $area = null)
{
$basepath = rtrim(static::instance($area)->get_path($basepath), '\\/').DS;
$new_file = static::instance($area)->get_path($basepath.$name);
if ( ! is_file($new_file))
{
throw new \FileAccessException('File: "'.$new_file.'" does not exist, cannot be appended.');
}
if ( ! $file = static::open_file(@fopen($new_file, 'a'), true, $area))
{
if ( ! is_dir($basepath) or ! is_writable($basepath))
{
throw new \InvalidPathException('Invalid basepath: "'.$basepath.'", cannot append to a file at this location.');
}
throw new \FileAccessException('No write access, cannot append to the file: "'.$file.'".');
}
fwrite($file, $contents);
static::close_file($file, $area);
return true;
}
/**
* Get the octal permissions for a file or directory
*
* @param string path to the file or directory
* @param mixed file area name, object or null for base area
* $return string octal file permissions
*/
public static function get_permissions($path, $area = null)
{
$path = static::instance($area)->get_path($path);
if ( ! file_exists($path))
{
throw new \InvalidPathException('Path: "'.$path.'" is not a directory or a file, cannot get permissions.');
}
return substr(sprintf('%o', fileperms($path)), -4);
}
/**
* Get a file's or directory's created or modified timestamp.
*
* @param string path to the file or directory
* @param string modified or created
* @param mixed file area name, object or null for base area
* @return int Unix Timestamp
*/
public static function get_time($path, $type = 'modified', $area = null)
{
$path = static::instance($area)->get_path($path);
if ( ! file_exists($path))
{
throw new \InvalidPathException('Path: "'.$path.'" is not a directory or a file, cannot get creation timestamp.');
}
if ($type === 'modified')
{
return filemtime($path);
}
elseif ($type === 'created')
{
return filectime($path);
}
else
{
throw new \UnexpectedValueException('File::time $type must be "modified" or "created".');
}
}
/**
* Get a file's size.
*
* @param string path to the file or directory
* @param mixed file area name, object or null for base area
* @return int the file's size in bytes
*/
public static function get_size($path, $area = null)
{
$path = static::instance($area)->get_path($path);
if ( ! file_exists($path))
{
throw new \InvalidPathException('Path: "'.$path.'" is not a directory or a file, cannot get size.');
}
return filesize($path);
}
/**
* Rename directory or file
*
* @param string path to file or directory to rename
* @param string new path (full path, can also cause move)
* @param string|File_Area|null source path file area name, object or null for non-specific
* @param string|File_Area|null target path file area name, object or null for non-specific. Defaults to source_area if not set.
* @return bool
*/
public static function rename($path, $new_path, $source_area = null, $target_area = null)
{
$path = static::instance($source_area)->get_path($path);
$new_path = static::instance($target_area ?: $source_area)->get_path($new_path);
return rename($path, $new_path);
}
/**
* Alias for rename(), not needed but consistent with other methods
*/
public static function rename_dir($path, $new_path, $source_area = null, $target_area = null)
{
return static::rename($path, $new_path, $source_area, $target_area);
}
/**
* Copy file
*
* @param string path to file to copy
* @param string new base directory (full path)
* @param string|File_Area|null source path file area name, object or null for non-specific
* @param string|File_Area|null target path file area name, object or null for non-specific. Defaults to source_area if not set.
* @return bool
*/
public static function copy($path, $new_path, $source_area = null, $target_area = null)
{
$path = static::instance($source_area)->get_path($path);
$new_path = static::instance($target_area ?: $source_area)->get_path($new_path);
if ( ! is_file($path))
{
throw new \InvalidPathException('Cannot copy file: given path: "'.$path.'" is not a file.');
}
elseif (file_exists($new_path))
{
throw new \FileAccessException('Cannot copy file: new path: "'.$new_path.'" already exists.');
}
return copy($path, $new_path);
}
/**
* Copy directory
*
* @param string path to directory which contents will be copied
* @param string new base directory (full path)
* @param string|File_Area|null source path file area name, object or null for non-specific
* @param string|File_Area|null target path file area name, object or null for non-specific. Defaults to source_area if not set.
* @return bool
* @throws FileAccessException when something went wrong
*/
public static function copy_dir($path, $new_path, $source_area = null, $target_area = null)
{
$target_area = $target_area ?: $source_area;
$path = rtrim(static::instance($source_area)->get_path($path), '\\/').DS;
$new_path = rtrim(static::instance($target_area)->get_path($new_path), '\\/').DS;
if ( ! is_dir($path))
{
throw new \InvalidPathException('Cannot copy directory: given path: "'.$path.'" is not a directory: '.$path);
}
elseif ( ! file_exists($new_path))
{
$newpath_dirname = pathinfo($new_path, PATHINFO_DIRNAME);
static::create_dir($newpath_dirname, pathinfo($new_path, PATHINFO_BASENAME), fileperms($newpath_dirname) ?: 0777, $target_area);
}
$files = static::read_dir($path, -1, array(), $source_area);
foreach ($files as $dir => $file)
{
if (is_array($file))
{
$check = static::create_dir($new_path.DS, substr($dir, 0, -1), fileperms($path.$dir) ?: 0777, $target_area);
$check and static::copy_dir($path.$dir.DS, $new_path.$dir, $source_area, $target_area);
}
else
{
$check = static::copy($path.$file, $new_path.$file, $source_area, $target_area);
}
// abort if something went wrong
if ( ! $check)
{
throw new \FileAccessException('Directory copy aborted prematurely, part of the operation failed during copying: '.(is_array($file) ? $dir : $file));
}
}
}
/**
* Create a new symlink
*
* @param string target of symlink
* @param string destination of symlink
* @param bool true for file, false for directory
* @param string|File_Area|null file area name, object or null for base area
* @return bool
*/
public static function symlink($path, $link_path, $is_file = true, $area = null)
{
$path = rtrim(static::instance($area)->get_path($path), '\\/');
$link_path = rtrim(static::instance($area)->get_path($link_path), '\\/');
if ($is_file and ! is_file($path))
{
throw new \InvalidPathException('Cannot symlink: given file: "'.$path.'" does not exist.');
}
elseif ( ! $is_file and ! is_dir($path))
{
throw new \InvalidPathException('Cannot symlink: given directory: "'.$path.'" does not exist.');
}
elseif (file_exists($link_path))
{
throw new \FileAccessException('Cannot symlink: link: "'.$link_path.'" already exists.');
}
return symlink($path, $link_path);
}
/**
* Delete file
*
* @param string path to file to delete
* @param string|File_Area|null file area name, object or null for base area
* @return bool
*/
public static function delete($path, $area = null)
{
$path = rtrim(static::instance($area)->get_path($path), '\\/');
if ( ! is_file($path) and ! is_link($path))
{
throw new \InvalidPathException('Cannot delete file: given path "'.$path.'" is not a file.');
}
return unlink($path);
}
/**
* Delete directory
*
* @param string path to directory to delete
* @param bool whether to also delete contents of subdirectories
* @param bool whether to delete the parent dir itself when empty
* @param string|File_Area|null file area name, object or null for base area
* @return bool
*/
public static function delete_dir($path, $recursive = true, $delete_top = true, $area = null)
{
$path = rtrim(static::instance($area)->get_path($path), '\\/').DS;
if ( ! is_dir($path))
{
throw new \InvalidPathException('Cannot delete directory: given path: "'.$path.'" is not a directory.');
}
$files = static::read_dir($path, -1, array(), $area);
$not_empty = false;
$check = true;
foreach ($files as $dir => $file)
{
if (is_array($file))
{
if ($recursive)
{
$check = static::delete_dir($path.$dir, true, true, $area);
}
else
{
$not_empty = true;
}
}
else
{
$check = static::delete($path.$file, $area);
}
// abort if something went wrong
if ( ! $check)
{
throw new \FileAccessException('Directory deletion aborted prematurely, part of the operation failed.');
}
}
if ( ! $not_empty and $delete_top)
{
return rmdir($path);
}
return true;
}
/**
* Open and lock file
*
* @param resource|string file resource or path
* @param constant either valid lock constant or true=LOCK_EX / false=LOCK_UN
* @param string|File_Area|null file area name, object or null for base area
*/
public static function open_file($path, $lock = true, $area = null)
{
if (is_string($path))
{
$path = static::instance($area)->get_path($path);
$resource = fopen($path, 'r+');
}
else
{
$resource = $path;
}
// Make sure the parameter is a valid resource
if ( ! is_resource($resource))
{
return false;
}
// If locks aren't used, don't lock
if ( ! static::instance($area)->use_locks())
{
return $resource;
}
// Accept valid lock constant or set to LOCK_EX
$lock = in_array($lock, array(LOCK_SH, LOCK_EX, LOCK_NB)) ? $lock : LOCK_EX;
// Try to get a lock, timeout after 5 seconds
$lock_mtime = microtime(true);
while ( ! flock($resource, $lock))
{
if (microtime(true) - $lock_mtime > 5)
{
throw new \FileAccessException('Could not secure file lock, timed out after 5 seconds.');
}
}
return $resource;
}
/**
* Close file resource & unlock
*
* @param resource open file resource
* @param string|File_Area|null file area name, object or null for base area
*/
public static function close_file($resource, $area = null)
{
// If locks aren't used, don't unlock
if ( static::instance($area)->use_locks())
{
flock($resource, LOCK_UN);
}
fclose($resource);
}
/**
* Get detailed information about a file
*
* @param string file path
* @param string|File_Area|null file area name, object or null for base area
*/
public static function file_info($path, $area = null)
{
$info = array(
'original' => $path,
'realpath' => '',
'dirname' => '',
'basename' => '',
'filename' => '',
'extension' => '',
'mimetype' => '',
'charset' => '',
'size' => 0,
'permissions' => '',
'time_created' => '',
'time_modified' => '',
);
if ( ! $info['realpath'] = static::instance($area)->get_path($path) or ! is_file($info['realpath']))
{
throw new \InvalidPathException('Filename given is not a valid file.');
}
$info = array_merge($info, pathinfo($info['realpath']));
if ( ! $fileinfo = new \finfo(FILEINFO_MIME, \Config::get('file.magic_file', null)))
{
throw new \InvalidArgumentException('Can not retrieve information about this file.');
}
$fileinfo = explode(';', $fileinfo->file($info['realpath']));
$info['mimetype'] = isset($fileinfo[0]) ? $fileinfo[0] : 'application/octet-stream';
if (isset($fileinfo[1]))
{
$fileinfo = explode('=', $fileinfo[1]);
$info['charset'] = isset($fileinfo[1]) ? $fileinfo[1] : '';
}
$info['size'] = static::get_size($info['realpath'], $area);
$info['permissions'] = static::get_permissions($info['realpath'], $area);
$info['time_created'] = static::get_time($info['realpath'], $type = 'created', $area);
$info['time_modified'] = static::get_time($info['realpath'], $type = 'modified', $area);
return $info;
}
/**
* Download a file
*
* @param string file path
* @param string|null custom name for the file to be downloaded
* @param string|null custom mime type or null for file mime type
* @param string|File_Area|null file area name, object or null for base area
* @param bool delete the file after download when true
* @param string disposition, must be 'attachment' or 'inline'
*/
public static function download($path, $name = null, $mime = null, $area = null, $delete = false, $disposition = 'attachment')
{
$info = static::file_info($path, $area);
$class = get_called_class();
empty($mime) or $info['mimetype'] = $mime;
empty($name) or $info['basename'] = $name;
in_array($disposition, array('inline', 'attachment')) or $disposition = 'attachment';
\Event::register('fuel-shutdown', function () use($info, $area, $class, $delete, $disposition) {
if ( ! $file = call_user_func(array($class, 'open_file'), @fopen($info['realpath'], 'rb'), LOCK_SH, $area))
{
throw new \FileAccessException('Filename given could not be opened for download.');
}
while (ob_get_level() > 0)
{
ob_end_clean();
}
ini_get('zlib.output_compression') and ini_set('zlib.output_compression', 0);
! ini_get('safe_mode') and set_time_limit(0);
header('Content-Type: '.$info['mimetype']);
header('Content-Disposition: '.$disposition.'; filename="'.$info['basename'].'"');
$disposition === 'attachment' and header('Content-Description: File Transfer');
header('Content-Length: '.$info['size']);
header('Content-Transfer-Encoding: binary');
$disposition === 'attachment' and header('Expires: 0');
$disposition === 'attachment' and header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
while( ! feof($file))
{
echo fread($file, 2048);
}
call_user_func(array($class, 'close_file'), $file, $area);
if ($delete)
{
call_user_func(array($class, 'delete'), $info['realpath'], $area);
}
});
exit;
}
}