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
10class admin_plugin_advanced_import extends DokuWiki_Admin_Plugin
11{
12
13    /**
14     * @return int sort number in admin menu
15     */
16    public function getMenuSort()
17    {
18        return 1;
19    }
20
21    public function forAdminOnly()
22    {
23        return false;
24    }
25
26    public function getMenuIcon()
27    {
28        return dirname(__FILE__) . '/../svg/export.svg';
29    }
30
31    public function getMenuText($language)
32    {
33        return $this->getLang('menu_import');
34    }
35
36    public function handle()
37    {
38        global $INPUT;
39
40        if (!$_REQUEST['cmd']) {
41            return;
42        }
43
44        if (!checkSecurityToken()) {
45            return;
46        }
47
48        $cmd = $INPUT->extract('cmd')->str('cmd');
49
50        if ($cmd) {
51            $cmd = "cmd_$cmd";
52            $this->$cmd();
53        }
54
55    }
56
57    public function html()
58    {
59
60        global $INPUT;
61        global $lang;
62        global $conf;
63        global $ID;
64
65        $lang['toc'] = $this->getLang('menu_import');
66
67        echo '<div id="plugin_advanced_export">';
68        echo $this->locale_xhtml('import');
69        echo '<p>&nbsp;</p>';
70
71        echo '<form action="" method="post" enctype="multipart/form-data" class="form-inline">';
72
73        $this->steps_dispatcher();
74
75        formSecurityToken();
76
77        echo '<input type="hidden" name="do" value="admin" />';
78        echo '<input type="hidden" name="page" value="advanced_import" />';
79
80        echo '</form>';
81        echo '</div>';
82
83    }
84
85    private function cmd_import()
86    {
87
88        global $INPUT;
89        global $conf;
90
91        $extract_dir     = io_mktmpdir();
92        $archive_file    = $INPUT->str('file');
93        $overwrite_pages = ($INPUT->str('overwrite-existing-pages') == 'on' ? true : false);
94        $files           = array_keys($INPUT->arr('files'));
95        $ns              = $INPUT->str('ns');
96        $imported_pages  = array();
97
98        if ($ns == '(root)') {
99            $ns = '';
100        }
101
102        if (!file_exists($archive_file)) {
103            msg($this->getLang('imp_zip_not_found'), -1);
104            return 0;
105        }
106
107        $Zip = new \splitbrain\PHPArchive\Zip;
108        $Zip->open($archive_file);
109
110        if (!$Zip->extract($extract_dir)) {
111            msg($this->getLang('imp_zip_extract_error'), -1);
112            return 0;
113        }
114
115        if (!count($files)) {
116            msg($this->getLang('imp_no_page_selected'), -1);
117            return 0;
118        }
119
120        foreach ($files as $file) {
121
122            $wiki_page = str_replace('/', ':', preg_replace('/\.txt$/', '', $file));
123            $wiki_page = cleanID("$ns:$wiki_page");
124
125            $sum = $this->getLang('imp_page_summary');
126
127            if (page_exists($wiki_page) && !$overwrite_pages) {
128                msg(sprintf($this->getLang('imp_page_already_exists'), $wiki_page), 2);
129                continue;
130            }
131
132            if (!page_exists($wiki_page)) {
133                $sum = $lang['created'] . " - $sum";
134            }
135
136            $wiki_text = io_readFile("$extract_dir/$file");
137
138            checklock($wiki_page);
139            lock($wiki_page);
140            saveWikiText($wiki_page, $wiki_text, $sum);
141            unlock($wiki_page);
142            idx_addPage($wiki_page);
143
144            $imported_pages[] = $wiki_page;
145
146        }
147
148        # Delete import archive
149        unlink($archive_file);
150
151        # Delete the extract directory
152        io_rmdir($extract_dir, true);
153
154        if (count($imported_pages)) {
155            msg($this->getLang('imp_pages_import_success'));
156        }
157
158    }
159
160    private function steps_dispatcher()
161    {
162
163        global $INPUT;
164
165        $step = $INPUT->extract('import')->str('import');
166
167        if (!$step) {
168            return $this->step_upload_form();
169        }
170
171        return call_user_func(array($this, "step_$step"));
172
173    }
174
175    private function step_upload_form()
176    {
177
178        global $lang;
179
180        echo '<input type="file" name="file" required="required" accept=".zip,application/zip,application/x-zip,application/x-zip-compressed" />';
181        echo '<p>&nbsp;</p>';
182
183        echo '<p class="pull-right">';
184        echo sprintf('<button type="submit" name="import[upload_backup]" class="btn btn-primary primary">%s &rarr;</button> ', $this->getLang('imp_upload_backup'));
185        echo '</p>';
186
187    }
188
189    private function step_upload_backup()
190    {
191
192        global $conf;
193        global $lang;
194
195        $tmp_name  = $_FILES['file']['tmp_name'];
196        $file_name = $_FILES['file']['name'];
197        $file_path = $conf['tmpdir'] . "/$file_name";
198
199        move_uploaded_file($tmp_name, $file_path);
200
201        search($namespaces, $conf['datadir'], 'search_namespaces', $options, '');
202
203        echo sprintf('<h3>1. %s</h3>', $this->getLang('imp_select_namespace'));
204
205        echo '<p><select name="ns" class="form-control" required="required">';
206        echo sprintf('<option>%s</option>', $this->getLang('imp_select_namespace'));
207        echo '<option value="(root)" selected="selected">(root)</option>';
208
209        foreach ($namespaces as $namespace) {
210            echo sprintf('<option value="%s">%s</option>', $namespace['id'], $namespace['id']);
211        }
212
213        echo '</select></p>';
214        echo '<p>&nbsp;</p>';
215
216        echo sprintf('<h3>2. %s</h3>', $this->getLang('imp_select_pages'));
217
218        $Zip = new \splitbrain\PHPArchive\Zip;
219        $Zip->open($file_path);
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->contents() 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->getPath(),
238                $fileinfo->getPath(),
239                filesize_h($fileinfo->getSize())
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