1<?php
2
3/**
4 * Dokuwiki Advanced Import/Export Plugin
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com>
8 */
9
10include_once dirname(__FILE__) . "/../ZipLib.class.php";
11
12class admin_plugin_advanced_import extends DokuWiki_Admin_Plugin
13{
14
15    /**
16     * @return int sort number in admin menu
17     */
18    public function getMenuSort()
19    {
20        return 1;
21    }
22
23    public function forAdminOnly()
24    {
25        return false;
26    }
27
28    public function getMenuIcon()
29    {
30        return dirname(__FILE__) . '/../svg/export.svg';
31    }
32
33    public function getMenuText($language)
34    {
35        return $this->getLang('menu_import');
36    }
37
38    public function handle()
39    {
40        global $INPUT;
41
42        if (!$_REQUEST['cmd']) {
43            return;
44        }
45
46        if (!checkSecurityToken()) {
47            return;
48        }
49
50        $cmd = $INPUT->extract('cmd')->str('cmd');
51
52        if ($cmd) {
53            $cmd = "cmd_$cmd";
54            $this->$cmd();
55        }
56
57    }
58
59    public function html()
60    {
61
62        global $INPUT;
63        global $lang;
64        global $conf;
65        global $ID;
66
67        $lang['toc'] = $this->getLang('menu_import');
68
69        echo '<div id="plugin_advanced_export">';
70        echo $this->locale_xhtml('import');
71        echo '<p>&nbsp;</p>';
72
73        echo '<form action="" method="post" enctype="multipart/form-data" class="form-inline">';
74
75        $this->steps_dispatcher();
76
77        formSecurityToken();
78
79        echo '<input type="hidden" name="do" value="admin" />';
80        echo '<input type="hidden" name="page" value="advanced_import" />';
81
82        echo '</form>';
83        echo '</div>';
84
85    }
86
87    private function cmd_import()
88    {
89
90        global $INPUT;
91        global $conf;
92
93        $extract_dir     = io_mktmpdir();
94        $archive_file    = $INPUT->str('file');
95        $overwrite_pages = ($INPUT->str('overwrite-existing-pages') == 'on' ? true : false);
96        $files           = array_keys($INPUT->arr('files'));
97        $ns              = $INPUT->str('ns');
98        $imported_pages  = array();
99
100        if ($ns == '(root)') {
101            $ns = '';
102        }
103
104        if (!file_exists($archive_file)) {
105            msg($this->getLang('imp_zip_not_found'), -1);
106            return 0;
107        }
108
109        $Zip = new ZipLib;
110
111        if (!$Zip->extract($archive_file, $extract_dir)) {
112            msg($this->getLang('imp_zip_extract_error'), -1);
113            return 0;
114        }
115
116        if (!count($files)) {
117            msg($this->getLang('imp_no_page_selected'), -1);
118            return 0;
119        }
120
121        foreach ($files as $file) {
122
123            $wiki_page = str_replace('/', ':', preg_replace('/\.txt$/', '', $file));
124            $wiki_page = cleanID("$ns:$wiki_page");
125
126            $sum = $this->getLang('imp_page_summary');
127
128            if (page_exists($wiki_page) && !$overwrite_pages) {
129                msg(sprintf($this->getLang('imp_page_already_exists'), $wiki_page), 2);
130                continue;
131            }
132
133            if (!page_exists($wiki_page)) {
134                $sum = $lang['created'] . " - $sum";
135            }
136
137            $wiki_text = io_readFile("$extract_dir/$file");
138
139            checklock($wiki_page);
140            lock($wiki_page);
141            saveWikiText($wiki_page, $wiki_text, $sum);
142            unlock($wiki_page);
143            idx_addPage($wiki_page);
144
145            $imported_pages[] = $wiki_page;
146
147        }
148
149        # Delete import archive
150        unlink($archive_file);
151
152        # Delete the extract directory
153        io_rmdir($extract_dir, true);
154
155        if (count($imported_pages)) {
156            msg($this->getLang('imp_pages_import_success'));
157        }
158
159    }
160
161    private function steps_dispatcher()
162    {
163
164        global $INPUT;
165
166        $step = $INPUT->extract('import')->str('import');
167
168        if (!$step) {
169            return $this->step_upload_form();
170        }
171
172        return call_user_func(array($this, "step_$step"));
173
174    }
175
176    private function step_upload_form()
177    {
178
179        global $lang;
180
181        echo '<input type="file" name="file" required="required" accept=".zip,application/zip,application/x-zip,application/x-zip-compressed" />';
182        echo '<p>&nbsp;</p>';
183
184        echo '<p class="pull-right">';
185        echo sprintf('<button type="submit" name="import[upload_backup]" class="btn btn-primary primary">%s &rarr;</button> ', $this->getLang('imp_upload_backup'));
186        echo '</p>';
187
188    }
189
190    private function step_upload_backup()
191    {
192
193        global $conf;
194        global $lang;
195
196        $tmp_name  = $_FILES['file']['tmp_name'];
197        $file_name = $_FILES['file']['name'];
198        $file_path = $conf['tmpdir'] . "/$file_name";
199
200        move_uploaded_file($tmp_name, $file_path);
201
202        search($namespaces, $conf['datadir'], 'search_namespaces', $options, '');
203
204        echo sprintf('<h3>1. %s</h3>', $this->getLang('imp_select_namespace'));
205
206        echo '<p><select name="ns" class="form-control" required="required">';
207        echo sprintf('<option>%s</option>', $this->getLang('imp_select_namespace'));
208        echo '<option value="(root)" selected="selected">(root)</option>';
209
210        foreach ($namespaces as $namespace) {
211            echo sprintf('<option value="%s">%s</option>', $namespace['id'], $namespace['id']);
212        }
213
214        echo '</select></p>';
215        echo '<p>&nbsp;</p>';
216
217        echo sprintf('<h3>2. %s</h3>', $this->getLang('imp_select_pages'));
218
219        $Zip = new ZipLib;
220
221        echo '<table class="table inline pages" width="100%">';
222        echo '<thead>
223      <tr>
224        <th width="10"><input type="checkbox" class="import-all-pages" title="' . $this->getLang('select_all_pages') . '" /></th>
225        <th>File</th>
226        <th>Size</th>
227      </tr>
228    </thead>';
229        echo '<tbody>';
230
231        foreach ($Zip->get_List($file_path) as $fileinfo) {
232            echo '<tr>';
233            echo sprintf('
234        <td><input type="checkbox" name="files[%s]" class="import-file" /></td>
235        <td>%s</td>
236        <td>%s</td>',
237                $fileinfo['filename'],
238                $fileinfo['filename'],
239                filesize_h($fileinfo['size'])
240            );
241            echo '<tr>';
242        }
243
244        echo '</tbody></table>';
245        echo '<p>&nbsp;</p>';
246
247        echo '<h3>3. Options</h3>';
248        echo '<table class="table inline">';
249        echo sprintf('<tbody>
250      <tr>
251        <td width="10"><input type="checkbox" name="overwrite-existing-pages" /></td>
252        <td>%s</td>
253      </tr>
254    </tbody>', $this->getLang('imp_overwrite_pages'));
255        echo '</table>';
256
257        echo '<p>&nbsp;</p>';
258
259        echo '<p class="pull-right">';
260        echo sprintf('<button type="submit" name="import[upload_form]" class="btn btn-default">&larr; %s</button> ', $lang['btn_back']);
261        echo sprintf('<button type="submit" name="cmd[import]" class="btn btn-primary primary">%s &rarr;</button>', $this->getLang('btn_import'));
262        echo '</p>';
263
264        echo sprintf('<input type="hidden" name="file" value="%s">', $file_path);
265
266    }
267
268}
269