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 dcExportFlat extends dcIeModule |
---|
15 | { |
---|
16 | public function setInfo() |
---|
17 | { |
---|
18 | $this->type = 'export'; |
---|
19 | $this->name = __('Flat file export'); |
---|
20 | $this->description = __('Exports a blog or a full Dotclear installation to flat file.'); |
---|
21 | } |
---|
22 | |
---|
23 | public function process($do) |
---|
24 | { |
---|
25 | # Export a blog |
---|
26 | if ($do == 'export_blog' && $this->core->auth->check('admin', $this->core->blog->id)) { |
---|
27 | $fullname = $this->core->blog->public_path . '/.backup_' . sha1(uniqid()); |
---|
28 | $blog_id = $this->core->con->escape($this->core->blog->id); |
---|
29 | |
---|
30 | try |
---|
31 | { |
---|
32 | $exp = new flatExport($this->core->con, $fullname, $this->core->prefix); |
---|
33 | fwrite($exp->fp, '///DOTCLEAR|' . DC_VERSION . "|single\n"); |
---|
34 | |
---|
35 | $exp->export('category', |
---|
36 | 'SELECT * FROM ' . $this->core->prefix . 'category ' . |
---|
37 | "WHERE blog_id = '" . $blog_id . "'" |
---|
38 | ); |
---|
39 | $exp->export('link', |
---|
40 | 'SELECT * FROM ' . $this->core->prefix . 'link ' . |
---|
41 | "WHERE blog_id = '" . $blog_id . "'" |
---|
42 | ); |
---|
43 | $exp->export('setting', |
---|
44 | 'SELECT * FROM ' . $this->core->prefix . 'setting ' . |
---|
45 | "WHERE blog_id = '" . $blog_id . "'" |
---|
46 | ); |
---|
47 | $exp->export('post', |
---|
48 | 'SELECT * FROM ' . $this->core->prefix . 'post ' . |
---|
49 | "WHERE blog_id = '" . $blog_id . "'" |
---|
50 | ); |
---|
51 | $exp->export('meta', |
---|
52 | 'SELECT meta_id, meta_type, M.post_id ' . |
---|
53 | 'FROM ' . $this->core->prefix . 'meta M, ' . $this->core->prefix . 'post P ' . |
---|
54 | 'WHERE P.post_id = M.post_id ' . |
---|
55 | "AND P.blog_id = '" . $blog_id . "'" |
---|
56 | ); |
---|
57 | $exp->export('media', |
---|
58 | 'SELECT * FROM ' . $this->core->prefix . "media WHERE media_path = '" . |
---|
59 | $this->core->con->escape($this->core->blog->settings->system->public_path) . "'" |
---|
60 | ); |
---|
61 | $exp->export('post_media', |
---|
62 | 'SELECT media_id, M.post_id ' . |
---|
63 | 'FROM ' . $this->core->prefix . 'post_media M, ' . $this->core->prefix . 'post P ' . |
---|
64 | 'WHERE P.post_id = M.post_id ' . |
---|
65 | "AND P.blog_id = '" . $blog_id . "'" |
---|
66 | ); |
---|
67 | $exp->export('ping', |
---|
68 | 'SELECT ping.post_id, ping_url, ping_dt ' . |
---|
69 | 'FROM ' . $this->core->prefix . 'ping ping, ' . $this->core->prefix . 'post P ' . |
---|
70 | 'WHERE P.post_id = ping.post_id ' . |
---|
71 | "AND P.blog_id = '" . $blog_id . "'" |
---|
72 | ); |
---|
73 | $exp->export('comment', |
---|
74 | 'SELECT C.* ' . |
---|
75 | 'FROM ' . $this->core->prefix . 'comment C, ' . $this->core->prefix . 'post P ' . |
---|
76 | 'WHERE P.post_id = C.post_id ' . |
---|
77 | "AND P.blog_id = '" . $blog_id . "'" |
---|
78 | ); |
---|
79 | |
---|
80 | # --BEHAVIOR-- exportSingle |
---|
81 | $this->core->callBehavior('exportSingle', $this->core, $exp, $blog_id); |
---|
82 | |
---|
83 | $_SESSION['export_file'] = $fullname; |
---|
84 | $_SESSION['export_filename'] = $_POST['file_name']; |
---|
85 | $_SESSION['export_filezip'] = !empty($_POST['file_zip']); |
---|
86 | http::redirect($this->getURL() . '&do=ok'); |
---|
87 | } catch (Exception $e) { |
---|
88 | @unlink($fullname); |
---|
89 | throw $e; |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | # Export all content |
---|
94 | if ($do == 'export_all' && $this->core->auth->isSuperAdmin()) { |
---|
95 | $fullname = $this->core->blog->public_path . '/.backup_' . sha1(uniqid()); |
---|
96 | try |
---|
97 | { |
---|
98 | $exp = new flatExport($this->core->con, $fullname, $this->core->prefix); |
---|
99 | fwrite($exp->fp, '///DOTCLEAR|' . DC_VERSION . "|full\n"); |
---|
100 | $exp->exportTable('blog'); |
---|
101 | $exp->exportTable('category'); |
---|
102 | $exp->exportTable('link'); |
---|
103 | $exp->exportTable('setting'); |
---|
104 | $exp->exportTable('user'); |
---|
105 | $exp->exportTable('pref'); |
---|
106 | $exp->exportTable('permissions'); |
---|
107 | $exp->exportTable('post'); |
---|
108 | $exp->exportTable('meta'); |
---|
109 | $exp->exportTable('media'); |
---|
110 | $exp->exportTable('post_media'); |
---|
111 | $exp->exportTable('log'); |
---|
112 | $exp->exportTable('ping'); |
---|
113 | $exp->exportTable('comment'); |
---|
114 | $exp->exportTable('spamrule'); |
---|
115 | $exp->exportTable('version'); |
---|
116 | |
---|
117 | # --BEHAVIOR-- exportFull |
---|
118 | $this->core->callBehavior('exportFull', $this->core, $exp); |
---|
119 | |
---|
120 | $_SESSION['export_file'] = $fullname; |
---|
121 | $_SESSION['export_filename'] = $_POST['file_name']; |
---|
122 | $_SESSION['export_filezip'] = !empty($_POST['file_zip']); |
---|
123 | http::redirect($this->getURL() . '&do=ok'); |
---|
124 | } catch (Exception $e) { |
---|
125 | @unlink($fullname); |
---|
126 | throw $e; |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | # Send file content |
---|
131 | if ($do == 'ok') { |
---|
132 | if (!file_exists($_SESSION['export_file'])) { |
---|
133 | throw new Exception(__('Export file not found.')); |
---|
134 | } |
---|
135 | |
---|
136 | ob_end_clean(); |
---|
137 | |
---|
138 | if (substr($_SESSION['export_filename'], -4) == '.zip') { |
---|
139 | $_SESSION['export_filename'] = substr($_SESSION['export_filename'], 0, -4); //.'.txt'; |
---|
140 | } |
---|
141 | |
---|
142 | # Flat export |
---|
143 | if (empty($_SESSION['export_filezip'])) { |
---|
144 | |
---|
145 | header('Content-Disposition: attachment;filename=' . $_SESSION['export_filename']); |
---|
146 | header('Content-Type: text/plain; charset=UTF-8'); |
---|
147 | readfile($_SESSION['export_file']); |
---|
148 | |
---|
149 | unlink($_SESSION['export_file']); |
---|
150 | unset($_SESSION['export_file'], $_SESSION['export_filename'], $_SESSION['export_filezip']); |
---|
151 | exit; |
---|
152 | } |
---|
153 | # Zip export |
---|
154 | else { |
---|
155 | try |
---|
156 | { |
---|
157 | $file_zipname = $_SESSION['export_filename'] . '.zip'; |
---|
158 | |
---|
159 | $fp = fopen('php://output', 'wb'); |
---|
160 | $zip = new fileZip($fp); |
---|
161 | $zip->addFile($_SESSION['export_file'], $_SESSION['export_filename']); |
---|
162 | |
---|
163 | header('Content-Disposition: attachment;filename=' . $file_zipname); |
---|
164 | header('Content-Type: application/x-zip'); |
---|
165 | |
---|
166 | $zip->write(); |
---|
167 | |
---|
168 | unlink($_SESSION['export_file']); |
---|
169 | unset($zip, $_SESSION['export_file'], $_SESSION['export_filename'], $file_zipname); |
---|
170 | exit; |
---|
171 | } catch (Exception $e) { |
---|
172 | unset($zip, $_SESSION['export_file'], $_SESSION['export_filename'], $file_zipname); |
---|
173 | @unlink($_SESSION['export_file']); |
---|
174 | |
---|
175 | throw new Exception(__('Failed to compress export file.')); |
---|
176 | } |
---|
177 | } |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | public function gui() |
---|
182 | { |
---|
183 | echo |
---|
184 | '<form action="' . $this->getURL(true) . '" method="post" class="fieldset">' . |
---|
185 | '<h3>' . __('Single blog') . '</h3>' . |
---|
186 | '<p>' . sprintf(__('This will create an export of your current blog: %s'), '<strong>' . html::escapeHTML($this->core->blog->name)) . '</strong>.</p>' . |
---|
187 | |
---|
188 | '<p><label for="file_name">' . __('File name:') . '</label>' . |
---|
189 | form::field('file_name', 50, 255, date('Y-m-d-H-i-') . html::escapeHTML($this->core->blog->id . '-backup.txt')) . |
---|
190 | '</p>' . |
---|
191 | |
---|
192 | '<p><label for="file_zip" class="classic">' . |
---|
193 | form::checkbox(array('file_zip', 'file_zip'), 1) . ' ' . |
---|
194 | __('Compress file') . '</label>' . |
---|
195 | '</p>' . |
---|
196 | |
---|
197 | '<p class="zip-dl"><a href="' . $this->core->decode('admin.media', array('d' => '', 'zipdl' => '1')) . '">' . |
---|
198 | __('You may also want to download your media directory as a zip file') . '</a></p>' . |
---|
199 | |
---|
200 | '<p><input type="submit" value="' . __('Export') . '" />' . |
---|
201 | form::hidden(array('do'), 'export_blog') . |
---|
202 | $this->core->formNonce() . '</p>' . |
---|
203 | |
---|
204 | '</form>'; |
---|
205 | |
---|
206 | if ($this->core->auth->isSuperAdmin()) { |
---|
207 | echo |
---|
208 | '<form action="' . $this->getURL(true) . '" method="post" class="fieldset">' . |
---|
209 | '<h3>' . __('Multiple blogs') . '</h3>' . |
---|
210 | '<p>' . __('This will create an export of all the content of your database.') . '</p>' . |
---|
211 | |
---|
212 | '<p><label for="file_name2">' . __('File name:') . '</label>' . |
---|
213 | form::field(array('file_name', 'file_name2'), 50, 255, date('Y-m-d-H-i-') . 'dotclear-backup.txt') . |
---|
214 | '</p>' . |
---|
215 | |
---|
216 | '<p><label for="file_zip2" class="classic">' . |
---|
217 | form::checkbox(array('file_zip', 'file_zip2'), 1) . ' ' . |
---|
218 | __('Compress file') . '</label>' . |
---|
219 | '</p>' . |
---|
220 | |
---|
221 | '<p><input type="submit" value="' . __('Export') . '" />' . |
---|
222 | form::hidden(array('do'), 'export_all') . |
---|
223 | $this->core->formNonce() . '</p>' . |
---|
224 | |
---|
225 | '</form>'; |
---|
226 | } |
---|
227 | } |
---|
228 | } |
---|