1 | <?php |
---|
2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
3 | # |
---|
4 | # This file is part of importExport, a plugin for DotClear2. |
---|
5 | # |
---|
6 | # Copyright (c) 2003-2012 Olivier Meunier & Association Dotclear |
---|
7 | # Licensed under the GPL version 2.0 license. |
---|
8 | # See LICENSE file or |
---|
9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
10 | # |
---|
11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
12 | if (!defined('DC_RC_PATH')) {return;} |
---|
13 | |
---|
14 | class dcImportFlat extends dcIeModule |
---|
15 | { |
---|
16 | protected $status = false; |
---|
17 | |
---|
18 | public function setInfo() |
---|
19 | { |
---|
20 | $this->type = 'import'; |
---|
21 | $this->name = __('Flat file import'); |
---|
22 | $this->description = __('Imports a blog or a full Dotclear installation from flat file.'); |
---|
23 | } |
---|
24 | |
---|
25 | public function process($do) |
---|
26 | { |
---|
27 | if ($do == 'single' || $do == 'full') { |
---|
28 | $this->status = $do; |
---|
29 | return; |
---|
30 | } |
---|
31 | |
---|
32 | $to_unlink = false; |
---|
33 | |
---|
34 | # Single blog import |
---|
35 | $files = $this->getPublicFiles(); |
---|
36 | $single_upl = null; |
---|
37 | if (!empty($_POST['public_single_file']) && in_array($_POST['public_single_file'], $files)) { |
---|
38 | $single_upl = false; |
---|
39 | } elseif (!empty($_FILES['up_single_file'])) { |
---|
40 | $single_upl = true; |
---|
41 | } |
---|
42 | |
---|
43 | if ($single_upl !== null) { |
---|
44 | if ($single_upl) { |
---|
45 | files::uploadStatus($_FILES['up_single_file']); |
---|
46 | $file = DC_TPL_CACHE . '/' . md5(uniqid()); |
---|
47 | if (!move_uploaded_file($_FILES['up_single_file']['tmp_name'], $file)) { |
---|
48 | throw new Exception(__('Unable to move uploaded file.')); |
---|
49 | } |
---|
50 | $to_unlink = true; |
---|
51 | } else { |
---|
52 | $file = $_POST['public_single_file']; |
---|
53 | } |
---|
54 | |
---|
55 | try { |
---|
56 | # Try to unzip file |
---|
57 | $unzip_file = $this->unzip($file); |
---|
58 | if (false !== $unzip_file) { |
---|
59 | $bk = new flatImport($this->core, $unzip_file); |
---|
60 | } |
---|
61 | # Else this is a normal file |
---|
62 | else { |
---|
63 | $bk = new flatImport($this->core, $file); |
---|
64 | } |
---|
65 | |
---|
66 | $bk->importSingle(); |
---|
67 | } catch (Exception $e) { |
---|
68 | @unlink($unzip_file); |
---|
69 | if ($to_unlink) { |
---|
70 | @unlink($file); |
---|
71 | } |
---|
72 | throw $e; |
---|
73 | } |
---|
74 | @unlink($unzip_file); |
---|
75 | if ($to_unlink) { |
---|
76 | @unlink($file); |
---|
77 | } |
---|
78 | http::redirect($this->getURL() . '&do=single'); |
---|
79 | } |
---|
80 | |
---|
81 | # Full import |
---|
82 | $full_upl = null; |
---|
83 | if (!empty($_POST['public_full_file']) && in_array($_POST['public_full_file'], $files)) { |
---|
84 | $full_upl = false; |
---|
85 | } elseif (!empty($_FILES['up_full_file'])) { |
---|
86 | $full_upl = true; |
---|
87 | } |
---|
88 | |
---|
89 | if ($full_upl !== null && $this->core->auth->isSuperAdmin()) { |
---|
90 | if (empty($_POST['your_pwd']) || !$this->core->auth->checkPassword($_POST['your_pwd'])) { |
---|
91 | throw new Exception(__('Password verification failed')); |
---|
92 | } |
---|
93 | |
---|
94 | if ($full_upl) { |
---|
95 | files::uploadStatus($_FILES['up_full_file']); |
---|
96 | $file = DC_TPL_CACHE . '/' . md5(uniqid()); |
---|
97 | if (!move_uploaded_file($_FILES['up_full_file']['tmp_name'], $file)) { |
---|
98 | throw new Exception(__('Unable to move uploaded file.')); |
---|
99 | } |
---|
100 | $to_unlink = true; |
---|
101 | } else { |
---|
102 | $file = $_POST['public_full_file']; |
---|
103 | } |
---|
104 | |
---|
105 | try { |
---|
106 | # Try to unzip file |
---|
107 | $unzip_file = $this->unzip($file); |
---|
108 | if (false !== $unzip_file) { |
---|
109 | $bk = new flatImport($this->core, $unzip_file); |
---|
110 | } |
---|
111 | # Else this is a normal file |
---|
112 | else { |
---|
113 | $bk = new flatImport($this->core, $file); |
---|
114 | } |
---|
115 | |
---|
116 | $bk->importFull(); |
---|
117 | } catch (Exception $e) { |
---|
118 | @unlink($unzip_file); |
---|
119 | if ($to_unlink) { |
---|
120 | @unlink($file); |
---|
121 | } |
---|
122 | throw $e; |
---|
123 | } |
---|
124 | @unlink($unzip_file); |
---|
125 | if ($to_unlink) { |
---|
126 | @unlink($file); |
---|
127 | } |
---|
128 | http::redirect($this->getURL() . '&do=full'); |
---|
129 | } |
---|
130 | |
---|
131 | header('content-type:text/plain'); |
---|
132 | var_dump($_POST); |
---|
133 | exit; |
---|
134 | |
---|
135 | $this->status = true; |
---|
136 | } |
---|
137 | |
---|
138 | public function gui() |
---|
139 | { |
---|
140 | if ($this->status == 'single') { |
---|
141 | dcPage::success(__('Single blog successfully imported.')); |
---|
142 | return; |
---|
143 | } |
---|
144 | if ($this->status == 'full') { |
---|
145 | dcPage::success(__('Content successfully imported.')); |
---|
146 | return; |
---|
147 | } |
---|
148 | |
---|
149 | $public_files = array_merge(array('-' => ''), $this->getPublicFiles()); |
---|
150 | $has_files = (boolean) (count($public_files) - 1); |
---|
151 | |
---|
152 | echo |
---|
153 | '<script type="text/javascript">' . "\n" . |
---|
154 | dcPage::jsVar('dotclear.msg.confirm_full_import', |
---|
155 | __('Are you sure you want to import a full backup file?')) . |
---|
156 | "$(function() {" . |
---|
157 | "$('#up_single_file').change(function() { " . |
---|
158 | "if (this.value != '') { $('#public_single_file').val(''); } " . |
---|
159 | "}); " . |
---|
160 | "$('#public_single_file').change(function() { " . |
---|
161 | "if (this.value != '') { $('#up_single_file').val(''); } " . |
---|
162 | "}); " . |
---|
163 | "$('#up_full_file').change(function() { " . |
---|
164 | "if (this.value != '') { $('#public_full_file').val(''); } " . |
---|
165 | "}); " . |
---|
166 | "$('#public_full_file').change(function() { " . |
---|
167 | "if (this.value != '') { $('#up_full_file').val(''); } " . |
---|
168 | "}); " . |
---|
169 | "$('#formfull').submit(function() { " . |
---|
170 | "return window.confirm(dotclear.msg.confirm_full_import); " . |
---|
171 | "}); " . |
---|
172 | "});\n" . |
---|
173 | "</script>\n"; |
---|
174 | |
---|
175 | echo |
---|
176 | '<form action="' . $this->getURL(true) . '" method="post" enctype="multipart/form-data" class="fieldset">' . |
---|
177 | '<h3>' . __('Single blog') . '</h3>' . |
---|
178 | '<p>' . sprintf(__('This will import a single blog backup as new content in the current blog: <strong>%s</strong>.'), html::escapeHTML($this->core->blog->name)) . '</p>' . |
---|
179 | |
---|
180 | '<p><label for="up_single_file">' . __('Upload a backup file') . |
---|
181 | ' (' . sprintf(__('maximum size %s'), files::size(DC_MAX_UPLOAD_SIZE)) . ')' . ' </label>' . |
---|
182 | ' <input type="file" id="up_single_file" name="up_single_file" size="20" />' . |
---|
183 | '</p>'; |
---|
184 | |
---|
185 | if ($has_files) { |
---|
186 | echo |
---|
187 | '<p><label for="public_single_file" class="">' . __('or pick up a local file in your public directory') . ' </label> ' . |
---|
188 | form::combo('public_single_file', $public_files) . |
---|
189 | '</p>'; |
---|
190 | } |
---|
191 | |
---|
192 | echo |
---|
193 | '<p>' . |
---|
194 | $this->core->formNonce() . |
---|
195 | form::hidden(array('do'), 1) . |
---|
196 | form::hidden(array('MAX_FILE_SIZE'), DC_MAX_UPLOAD_SIZE) . |
---|
197 | '<input type="submit" value="' . __('Import') . '" /></p>' . |
---|
198 | |
---|
199 | '</form>'; |
---|
200 | |
---|
201 | if ($this->core->auth->isSuperAdmin()) { |
---|
202 | echo |
---|
203 | '<form action="' . $this->getURL(true) . '" method="post" enctype="multipart/form-data" id="formfull" class="fieldset">' . |
---|
204 | '<h3>' . __('Multiple blogs') . '</h3>' . |
---|
205 | '<p class="warning">' . __('This will reset all the content of your database, except users.') . '</p>' . |
---|
206 | |
---|
207 | '<p><label for="up_full_file">' . __('Upload a backup file') . ' ' . |
---|
208 | ' (' . sprintf(__('maximum size %s'), files::size(DC_MAX_UPLOAD_SIZE)) . ')' . ' </label>' . |
---|
209 | '<input type="file" id="up_full_file" name="up_full_file" size="20" />' . |
---|
210 | '</p>'; |
---|
211 | |
---|
212 | if ($has_files) { |
---|
213 | echo |
---|
214 | '<p><label for="public_full_file">' . __('or pick up a local file in your public directory') . ' </label>' . |
---|
215 | form::combo('public_full_file', $public_files) . |
---|
216 | '</p>'; |
---|
217 | } |
---|
218 | |
---|
219 | echo |
---|
220 | '<p><label for="your_pwd" class="required"><abbr title="' . __('Required field') . '">*</abbr> ' . __('Your password:') . '</label>' . |
---|
221 | form::password('your_pwd', 20, 255, |
---|
222 | array( |
---|
223 | 'extra_html' => 'required placeholder="' . __('Password') . '"', |
---|
224 | 'autocomplete' => 'current-password' |
---|
225 | ) |
---|
226 | ) . '</p>' . |
---|
227 | |
---|
228 | '<p>' . |
---|
229 | $this->core->formNonce() . |
---|
230 | form::hidden(array('do'), 1) . |
---|
231 | form::hidden(array('MAX_FILE_SIZE'), DC_MAX_UPLOAD_SIZE) . |
---|
232 | '<input type="submit" value="' . __('Import') . '" /></p>' . |
---|
233 | |
---|
234 | '</form>'; |
---|
235 | } |
---|
236 | } |
---|
237 | |
---|
238 | protected function getPublicFiles() |
---|
239 | { |
---|
240 | $public_files = array(); |
---|
241 | $dir = @dir($this->core->blog->public_path); |
---|
242 | if ($dir) { |
---|
243 | while (($entry = $dir->read()) !== false) { |
---|
244 | $entry_path = $dir->path . '/' . $entry; |
---|
245 | |
---|
246 | if (is_file($entry_path) && is_readable($entry_path)) { |
---|
247 | # Do not test each zip file content here, its too long |
---|
248 | if (substr($entry_path, -4) == '.zip') { |
---|
249 | $public_files[$entry] = $entry_path; |
---|
250 | } elseif (self::checkFileContent($entry_path)) { |
---|
251 | $public_files[$entry] = $entry_path; |
---|
252 | } |
---|
253 | } |
---|
254 | } |
---|
255 | } |
---|
256 | return $public_files; |
---|
257 | } |
---|
258 | |
---|
259 | protected static function checkFileContent($entry_path) |
---|
260 | { |
---|
261 | $ret = false; |
---|
262 | |
---|
263 | $fp = fopen($entry_path, 'rb'); |
---|
264 | $ret = strpos(fgets($fp), '///DOTCLEAR|') === 0; |
---|
265 | fclose($fp); |
---|
266 | |
---|
267 | return $ret; |
---|
268 | } |
---|
269 | |
---|
270 | private function unzip($file) |
---|
271 | { |
---|
272 | $zip = new fileUnzip($file); |
---|
273 | |
---|
274 | if ($zip->isEmpty()) { |
---|
275 | $zip->close(); |
---|
276 | return false; //throw new Exception(__('File is empty or not a compressed file.')); |
---|
277 | } |
---|
278 | |
---|
279 | foreach ($zip->getFilesList() as $zip_file) { |
---|
280 | # Check zipped file name |
---|
281 | if (substr($zip_file, -4) != '.txt') { |
---|
282 | continue; |
---|
283 | } |
---|
284 | |
---|
285 | # Check zipped file contents |
---|
286 | $content = $zip->unzip($zip_file); |
---|
287 | if (strpos($content, '///DOTCLEAR|') !== 0) { |
---|
288 | unset($content); |
---|
289 | continue; |
---|
290 | } |
---|
291 | |
---|
292 | $target = path::fullFromRoot($zip_file, dirname($file)); |
---|
293 | |
---|
294 | # Check existing files with same name |
---|
295 | if (file_exists($target)) { |
---|
296 | $zip->close(); |
---|
297 | unset($content); |
---|
298 | throw new Exception(__('Another file with same name exists.')); |
---|
299 | } |
---|
300 | |
---|
301 | # Extract backup content |
---|
302 | if (file_put_contents($target, $content) === false) { |
---|
303 | $zip->close(); |
---|
304 | unset($content); |
---|
305 | throw new Exception(__('Failed to extract backup file.')); |
---|
306 | } |
---|
307 | |
---|
308 | $zip->close(); |
---|
309 | unset($content); |
---|
310 | |
---|
311 | # Return extracted file name |
---|
312 | return $target; |
---|
313 | } |
---|
314 | |
---|
315 | $zip->close(); |
---|
316 | throw new Exception(__('No backup in compressed file.')); |
---|
317 | } |
---|
318 | } |
---|