Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Helper;
4
 
5
use PhpOffice\PhpSpreadsheet\Exception;
6
 
7
/**
8
 * Assist downloading files when samples are run in browser.
9
 * Never run as part of unit tests, which are command line.
10
 *
11
 * @codeCoverageIgnore
12
 */
13
class Downloader
14
{
15
    protected string $filepath;
16
 
17
    protected string $filename;
18
 
19
    protected string $filetype;
20
 
21
    protected const CONTENT_TYPES = [
22
        'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
23
        'xls' => 'application/vnd.ms-excel',
24
        'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
25
        'csv' => 'text/csv',
26
        'html' => 'text/html',
27
        'pdf' => 'application/pdf',
28
    ];
29
 
30
    public function __construct(string $folder, string $filename, ?string $filetype = null)
31
    {
32
        if ((is_dir($folder) === false) || (is_readable($folder) === false)) {
33
            throw new Exception('Folder is not accessible');
34
        }
35
        $filepath = "{$folder}/{$filename}";
36
        $this->filepath = (string) realpath($filepath);
37
        $this->filename = basename($filepath);
38
        if ((file_exists($this->filepath) === false) || (is_readable($this->filepath) === false)) {
39
            throw new Exception('File not found, or cannot be read');
40
        }
41
 
42
        $filetype ??= pathinfo($filename, PATHINFO_EXTENSION);
43
        if (array_key_exists(strtolower($filetype), self::CONTENT_TYPES) === false) {
44
            throw new Exception('Invalid filetype: file cannot be downloaded');
45
        }
46
        $this->filetype = strtolower($filetype);
47
    }
48
 
49
    public function download(): void
50
    {
51
        $this->headers();
52
 
53
        readfile($this->filepath);
54
    }
55
 
56
    public function headers(): void
57
    {
58
        // I cannot tell what this ob_clean is paired with.
59
        // I have never seen a problem with it, but someone has - issue 3739.
60
        // Perhaps it should be removed altogether,
61
        // but making it conditional seems harmless, and safer.
62
        if ((int) ob_get_length() > 0) {
63
            ob_clean();
64
        }
65
 
66
        $this->contentType();
67
        $this->contentDisposition();
68
        $this->cacheHeaders();
69
        $this->fileSize();
70
 
71
        flush();
72
    }
73
 
74
    protected function contentType(): void
75
    {
76
        header('Content-Type: ' . self::CONTENT_TYPES[$this->filetype]);
77
    }
78
 
79
    protected function contentDisposition(): void
80
    {
81
        header('Content-Disposition: attachment;filename="' . $this->filename . '"');
82
    }
83
 
84
    protected function cacheHeaders(): void
85
    {
86
        header('Cache-Control: max-age=0');
87
        // If you're serving to IE 9, then the following may be needed
88
        header('Cache-Control: max-age=1');
89
 
90
        // If you're serving to IE over SSL, then the following may be needed
91
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
92
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
93
        header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
94
        header('Pragma: public'); // HTTP/1.0
95
    }
96
 
97
    protected function fileSize(): void
98
    {
99
        header('Content-Length: ' . filesize($this->filepath));
100
    }
101
}