aboutsummaryrefslogtreecommitdiff
path: root/lib/calendar/CalendarExportFile.class.php
blob: 1e0f3fb898fd8cacd9aa29af794bd96b83df329e (plain)
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
<?
# Lifter002: TODO
# Lifter007: TODO

/**
 * CalendarExportFile.class.php
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      Peter Thienel <thienel@data-quest.de>, Suchi & Berg GmbH <info@data-quest.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @package     calendar
 */

class CalendarExportFile extends CalendarExport
{
    private $file_name = 'studip';
    private $tmp_file_name;
    private $path;

    public function __construct(&$writer, $path = null, $file_name = null)
    {
        global $TMP_PATH;

        parent::__construct($writer);

        if (!$file_name) {
            $this->tmp_file_name = $this->makeUniqueFilename();
            $this->file_name .= '.' . $writer->getDefaultFileNameSuffix();
        } else {
            $this->file_name = $file_name;
            $this->tmp_file_name = $file_name;
        }

        if (!$path) {
            $this->path = $TMP_PATH . '/';
        }

        $this->_writer = $writer;
    }

    public function exportFromDatabase($range_id = null, $start = 0, $end = Calendar::CALENDAR_END, $event_types = null, $except = null)
    {
        $this->_createFile();
        parent::exportFromDatabase($range_id, $start, $end, $event_types, $except);
        $this->_closeFile();
    }

    public function exportFromObjects($events)
    {
        $this->_createFile();
        parent::exportFromObjects($events);
        $this->_closeFile();
    }

    public function sendFile()
    {
        if (file_exists($this->path . $this->tmp_file_name)) {
            header('Location: ' . FileManager::getDownloadURLForTemporaryFile($this->tmp_file_name, $this->file_name));
        } else {
            throw new CalendarExportException(_('Die Export-Datei konnte nicht erstellt werden!'));
        }
    }

    public function makeUniqueFileName()
    {
        return md5(uniqid(rand() . "Stud.IP Calendar"));
    }

    // returns file handle
    public function getExport()
    {
        return $this->export;
    }

    public function getFileName()
    {
        return $this->file_name;
    }

    public function getTempFileName()
    {
        return $this->tmp_file_name;
    }

    public function _createFile()
    {
        if (!(is_dir($this->path))) {
            if (!mkdir($this->path)) {
                var_dump($this->path); exit;
                throw new CalendarExportException(_('Das Export-Verzeichnis konnte nicht angelegt werden!'));
            } else {
                if (!chmod($this->path, 0777)) {
                    throw new CalendarExportException(_('Die Zugriffsrechte auf das Export-Verzeichnis konnten nicht geändert werden!'));
                }
            }
        }
        if (file_exists($this->path . $this->tmp_file_name)) {
            if (!unlink($this->path . $this->tmp_file_name)) {
                throw new CalendarExportException(_('Eine bestehende Export-Datei konnte nicht gelöscht werden!'));
            }
        }
        $this->export = fopen($this->path . $this->tmp_file_name, "wb");
        if (!$this->export) {
            throw new CalendarExportException(_("Die Export-Datei konnte nicht erstellt werden!"));
        }
    }

    public function _export($exp)
    {
        fwrite($this->export, $exp);
    }

    public function _closeFile()
    {
        fclose($this->export);
    }
}