1<?php
2/**
3 * Siteexport SendFile Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     i-net software <tools@inetsoftware.de>
7 * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
8 */
9
10// must be run within Dokuwiki
11if (!defined('DOKU_INC')) die();
12
13if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
14require_once(DOKU_PLUGIN . 'siteexport/inc/debug.php');
15require_once(DOKU_PLUGIN . 'siteexport/inc/functions.php');
16
17class action_plugin_siteexport_sendfile extends DokuWiki_Action_Plugin {
18
19    public function register(Doku_Event_Handler $controller) {
20        // Download of a file
21
22        $controller->register_hook('MEDIA_SENDFILE', 'BEFORE', $this, 'siteexport_sendfile');
23        $controller->register_hook('FETCH_MEDIA_STATUS', 'BEFORE', $this, 'siteexport_sendfile_not_found');
24    }
25
26    /*
27     * Redirect File to real File
28     */
29    public function siteexport_sendfile(Doku_Event &$event, $args) {
30        global $conf;
31
32        if (empty($_REQUEST['siteexport']) /* || $event->data['orig'] != $this->getConf('zipfilename') */) {
33            return;
34        }
35
36        $functions = new siteexport_functions();
37        $functions->debug->message("Starting to send a file from siteexporter", null, 2);
38        $filewriter = new siteexport_zipfilewriter($functions);
39        $functions->settings->pattern = $_REQUEST['siteexport'];
40
41        // Try injecting another name ... can't do, because sendFile sets this right after me and right before sending the actual data.
42        // header('Content-Disposition: attachment; filename="'. basename($functions->settings->zipFile) .'";');
43
44        // Try getting the cached file ...
45        $event->data['file'] = $functions->getCacheFileNameForPattern();
46
47        $functions->debug->message("fetching cached file from pattern '{$functions->settings->pattern}' with name '{$event->data['file']}'", null, 2);
48        $functions->debug->message("Event Data Before:", $event->data, 3);
49
50        $functions->checkIfCacheFileExistsForFileWithPattern($event->data['file'], $_REQUEST['siteexport']);
51
52        $filewriter->getOnlyFileInZip($event->data);
53
54        header('Set-Cookie: fileDownload=true; path=' . DOKU_BASE);
55        header('Cache-Control: max-age=60, must-revalidate');
56
57        $functions->debug->message("Event Data After:", $event->data, 3);
58    }
59
60    public function siteexport_sendfile_not_found(Doku_Event &$event, $args)
61    {
62        if (empty($_REQUEST['siteexport']) ||
63        /**
64        $event->data['media'] != $this->getConf('zipfilename')
65        /*/
66        $event->data['status'] >= 500
67        //*/
68        ) { return true; }
69        $event->data['status'] = 200;
70        return true;
71    }
72}
73