Dotclear

source: plugins/importExport/inc/class.dc.export.flat.php @ 1499:fc4bc1ece727

Revision 1499:fc4bc1ece727, 7.4 KB checked in by Anne Kozlika <kozlika@…>, 12 years ago (diff)

Let fieldsets be fieldsets (work in progress): blogpref, category, blogtheme, langs, media, plugins, user actions, antispam, importexport.

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

Sites map