1<?php
2
3if(!defined('DOKU_INC')) die();
4
5/**
6 * this part of the plugin handles all ajax requests.
7 *
8 * ajax requests are
9 *   - nsexport_start   start the export process @see prepare_dl()
10 *   - nsexport_check
11 */
12class action_plugin_nsexport_ajax extends DokuWiki_Action_Plugin {
13
14    // temporary directory
15    public $tmp;
16
17    // ID from the export zip
18    public $fileid;
19
20    public function register(Doku_Event_Handler $controller) {
21        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call');
22    }
23
24    /**
25     * route ajax calls to a function
26     */
27    public function handle_ajax_call(Doku_Event $event, $param) {
28        // we don't need this info, but we need this call to populate $conf['plugin']['nsexport']
29        $this->getConf('autoexport');
30        if ($event->data === 'nsexport_start') {
31            $event->preventDefault();
32            $this->prepare_dl();
33            return;
34        }
35
36        if ($event->data === 'nsexport_check') {
37            $event->preventDefault();
38            $this->check();
39            return;
40        }
41    }
42
43    /**
44     * check if the download is ready for download.
45     * print 0 on not ready and 1 on ready
46     */
47    public function check() {
48        $fid = $_REQUEST['key'];
49
50        if (!is_numeric($fid)) {
51            echo '0';
52            return;
53        }
54        if (!$fid) {
55            echo '0';
56            return;
57        }
58        $packer = $this->getPacker();
59        if ($packer === null || !$packer->get_status($fid)) {
60            echo '0';
61            return;
62        }
63        echo '1';
64    }
65
66    /**
67     * @return plugin_nsexport_packer|null
68     */
69    public function getPacker() {
70        $packer_file = DOKU_PLUGIN . 'nsexport/packer/ziphtml/packer.php';
71        if (!file_exists($packer_file)) {
72            return null;
73        }
74        require_once $packer_file;
75        $packer_class = 'plugin_nsexport_packer_ziphtml';
76        $packer = new $packer_class;
77        return $packer;
78    }
79
80    /**
81     * start the download creating process.
82     *
83     * echos a unique id to check back to the client, build the export
84     */
85    public function prepare_dl() {
86        global $INPUT;
87
88        $pages = $INPUT->arr('pages');
89
90
91        // turn off error reporting - we don't want any error messages in the output.
92//        error_reporting(0);
93
94        // try to ignore user abort.
95        ignore_user_abort(true);
96
97        // infinite time limit.
98        set_time_limit(0);
99
100        $packer = $this->getPacker();
101        if ($packer === null) {
102            return false;
103        }
104        $packer->start_packing($pages);
105    }
106}
107