This repository was archived by the owner on Apr 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBackupRestoreController.php
More file actions
416 lines (352 loc) · 15.9 KB
/
BackupRestoreController.php
File metadata and controls
416 lines (352 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
/*
* Wolf CMS - Content Management Simplified. <http://www.wolfcms.org>
* Copyright (C) 2009-2011 Martijn van der Kleijn <martijn.niji@gmail.com>
*
* This file is part of Wolf CMS. Wolf CMS is licensed under the GNU GPLv3 license.
* Please see license.txt for the full license text.
*/
/**
* The BackupRestore plugin provides administrators with the option of backing
* up their pages, settings and uploaded files to an XML file.
*
* @package Plugins
* @subpackage backup-restore
*
* @author Martijn van der Kleijn <martijn.niji@gmail.com>
* @author Frank Edelhaeuser <mrpace2@gmail.com>
* @copyright Martijn van der Kleijn, 2009-2011
* @license http://www.gnu.org/licenses/gpl.html GPLv3 license
*/
/* Security measure */
if (!defined('IN_CMS')) { exit(); }
/**
*
* @author Martijn van der Kleijn <martijn.niji@gmail.com>
* @author Frank Edelhaeuser <mrpace2@gmail.com>
* @copyright Martijn van der Kleijn, 2009,2010
*/
class BackupRestoreController extends PluginController {
private static function _checkPermission() {
AuthUser::load();
if ( ! AuthUser::isLoggedIn()) {
redirect(get_url('login'));
}
else if ( ! AuthUser::getId() == 1) {
Flash::set('error', __('You do not have permission to access the requested page!'));
redirect(get_url());
}
}
public function __construct() {
self::_checkPermission();
$this->setLayout('backend');
$this->assignToLayout('sidebar', new View('../../plugins/backup_restore/views/sidebar'));
}
public function index() {
$this->documentation();
}
public function documentation() {
$this->display('backup_restore/views/documentation');
}
function settings() {
$this->display('backup_restore/views/settings', array('settings' => Plugin::getAllSettings('backup_restore')));
}
function save() {
if (isset($_POST['settings'])) {
$settings = $_POST['settings'];
$ret = Plugin::setAllSettings($settings, 'backup_restore');
if ($ret) {
Flash::set('success', __('The settings have been saved.'));
}
else {
Flash::set('error', 'An error occured trying to save the settings.');
}
}
else {
Flash::set('error', 'Could not save settings, no settings found.');
}
redirect(get_url('plugin/backup_restore/settings'));
}
function backup() {
$settings = Plugin::getAllSettings('backup_restore');
// All of the tablesnames that belong to Wolf CMS core.
$tablenames = array();
if (strpos(DB_DSN,'mysql') !== false) {
$sql = 'show tables';
}
if (strpos(DB_DSN,'sqlite') !== false) {
$sql = 'SELECT name FROM SQLITE_MASTER WHERE type="table" ORDER BY name';
}
if (strpos(DB_DSN,'pgsql') !== false) {
$sql = "select tablename from pg_tables where schemaname='public'";
}
Record::logQuery($sql);
$pdo = Record::getConnection();
$result = $pdo->query($sql);
while ($col = $result->fetchColumn()) {
$tablenames[] = $col;
}
// All fields that should be wrapped as CDATA
$cdata_fields = array('title', 'content', 'content_html');
// Setup XML for backup
$xmltext = '<?xml version="1.0" encoding="UTF-8"?><wolfcms></wolfcms>';
$xmlobj = new SimpleXMLExtended($xmltext);
$xmlobj->addAttribute('version', CMS_VERSION);
// Retrieve all database information for placement in XML backup
global $__CMS_CONN__;
Record::connection($__CMS_CONN__);
// Generate XML file entry for each table
foreach ($tablenames as $tablename) {
$table = Record::query('SELECT * FROM '.$tablename);
$child = $xmlobj->addChild($tablename.'s');
while ($entry = $table->fetch(PDO::FETCH_ASSOC)) {
$subchild = $child->addChild($tablename);
foreach ($entry as $key => $value) {
if ($key == 'password' && $settings['pwd'] === '0') {
$value = '';
}
if (in_array($key, $cdata_fields, true)) {
$valueChild = $subchild->addCData($key,$value);
}
else {
$valueChild = $subchild->addChild($key,str_replace('&', '&', $value));
}
if ($value === null)
$valueChild->addAttribute('null', true);
}
}
}
// Add XML files entries for all files in upload directory
if ($settings['backupfiles'] == '1') {
$dir = realpath(FILES_DIR);
$this->_backup_directory($xmlobj->addChild('files'), $dir, $dir);
}
// Create the XML file
$file = $xmlobj->asXML();
$filename = 'wolfcms-backup-'.date($settings['stamp']).'.'.$settings['extension'];
// Offer a plain XML file or a zip file for download
if ($settings['zip'] == '1') {
// Create a note file
$note = "---[ NOTES for $filename ]---\n\n";
$note .= "This backup was created for a specific Wolf CMS version, please only restore it\n";
$note .= "on the same version.\n\n";
$note .= "When restoring a backup, upload the UNzipped XML backup file, not this zip file.\n\n";
$note .= 'Created on '.date('Y-m-d').' at '.date('H:i:s').' GTM '.date('O').".\n";
$note .= 'Created with BackupRestore plugin version '.BR_VERSION."\n";
$note .= 'Created for Wolf CMS version '.CMS_VERSION."\n\n";
$note .= '---[ END NOTES ]---';
use_helper('Zip');
$zip = new Zip();
$zip->clear();
$zip->addFile($note, 'readme.txt');
$zip->addFile($file, $filename);
$zip->download($filename.'.zip');
}
else {
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: text/xml; charset=UTF-8');
header('Content-Disposition: attachment; filename='.$filename.';');
header('Content-Transfer-Encoding: 8bit');
header('Content-Length: '.strlen($file));
echo $file;
}
}
function restore() {
if (isset($_POST['action']) && $_POST['action'] == 'restore') {
if (is_uploaded_file($_FILES['restoreFile']['tmp_name'])) {
$fileData = file_get_contents($_FILES['restoreFile']['tmp_name']);
if (isset($fileData) && $fileData !== null && $fileData !== false && $fileData !== '' && strlen($fileData) > 70 ) {
$this->_restore($fileData);
}
else {
Flash::set('error', __('Backup file was not uploaded correctly/completely or is broken.'));
$this->display('backup_restore/views/restore');
}
}
}
else {
$this->display('backup_restore/views/restore', array('settings' => Plugin::getAllSettings('backup_restore')));
}
}
function _restore($fileData) {
global $__CMS_CONN__;
$settings = Plugin::getAllSettings('backup_restore');
// All of the tablesnames that belong to Wolf CMS core.
$tablenames = array();
if (strpos(DB_DSN,'mysql') !== false) {
$sql = 'show tables';
}
if (strpos(DB_DSN,'sqlite') !== false) {
$sql = 'SELECT name FROM SQLITE_MASTER WHERE type="table" ORDER BY name';
}
if (strpos(DB_DSN,'pgsql') !== false) {
$sql = "select tablename from pg_tables where schemaname='public'";
}
Record::logQuery($sql);
$pdo = Record::getConnection();
$result = $pdo->query($sql);
while ($col = $result->fetchColumn()) {
$tablenames[] = $col;
}
// All fields that should be wrapped as CDATA
$cdata_fields = array('title', 'content', 'content_html');
$xml = simplexml_load_string($fileData);
if (false === $xml) {
$errors = '';
foreach(libxml_get_errors() as $error) {
$errors .= $error->message;
$errors .= "<br/>\n";
}
Flash::set('error', 'An error occurred with the XML backup file: :error', array(':error' => $errors));
redirect(get_url('plugin/backup_restore'));
}
// Import each table and table entry
foreach($tablenames as $tablename) {
$container = $tablename.'s';
if (array_key_exists($container, $xml) && count($xml->$container->$tablename) > 0) {
if (strpos(DB_DSN,'sqlite') !== false) {
$sql = 'DELETE FROM '.$tablename;
}
else {
$sql = 'TRUNCATE '.$tablename;
}
Record::logQuery($sql);
if (false === $__CMS_CONN__->exec($sql)) {
Flash::set('error', __('Unable to truncate current table :tablename.', array(':tablename' => $tablename)));
redirect(get_url('plugin/backup_restore'));
}
foreach ($xml->$container->$tablename as $element) {
$keys = array();
$values = array();
$delete_salt = false;
foreach ($element as $key => $value) {
$keys[] = $key;
if ($key === 'password' && empty($value)) {
$delete_salt = true;
if (isset($settings['default_pwd']) && $settings['default_pwd'] !== '') {
$value = sha1($settings['default_pwd']);
}
else {
$value = sha1('pswpsw123');
}
$values[] = $__CMS_CONN__->quote($value);
} else {
$attributes = (array)$value->attributes();
$values[] = (isset($attributes['@attributes']) and $attributes['@attributes']['null']) ?
'NULL' :
$__CMS_CONN__->quote($value);
}
}
if ($delete_salt and isset($keys['salt'])) {
unset($keys['salt']);
}
$sql = 'INSERT INTO '.$tablename.' ('.join(', ', $keys).') VALUES ('.join(', ', $values).')'."\r";
if ($__CMS_CONN__->exec($sql) === false) {
Flash::set('error', __('Unable to reconstruct table :tablename.', array(':tablename' => $tablename)));
redirect(get_url('plugin/backup_restore'));
}
}
}
}
// Erase all uploaded files?
if ($settings['erasefiles'] == '1') {
$this->_cleanup_directory(realpath(FILES_DIR));
}
// Restore directories and files from XML
if (($settings['restorefiles'] == '1') && array_key_exists('files', $xml)) {
// First directories
foreach ($xml->files->directory as $obj) {
$name = realpath(FILES_DIR).'/'.$obj->name;
if (!file_exists($name)) {
if (mkdir($name, 0777, true) === false) {
Flash::set('error', __('Unable to create directory :name.', array(':name' => dirname($obj->name))));
redirect(get_url('plugin/backup_restore'));
}
}
$this->_restore_attributes($name, $obj);
}
// Then files
foreach ($xml->files->file as $obj) {
$name = realpath(FILES_DIR).'/'.$obj->name;
if (file_put_contents($name, base64_decode($obj->content)) === false) {
Flash::set('error', __('Unable to restore file :name.', array(':name' => $obj->name)));
redirect(get_url('plugin/backup_restore'));
}
$this->_restore_attributes($name, $obj);
}
}
Flash::set('success', __('Succesfully restored backup.'));
redirect(get_url('plugin/backup_restore'));
}
function _backup_directory($parent, $dir, $basedir) {
foreach (new DirectoryIterator($dir) as $obj) {
if ($obj->isDot() || $obj->isLink()) {
}
else if ($obj->isDir()) {
$child = $parent->addChild('directory');
$child->addChild('name', substr($obj->getPathname(), strlen($basedir.'/')));
$child->addChild('mode', substr(sprintf('%o', $obj->getPerms()), -4));
$child->addChild('mtime', date(DATE_RFC822, $obj->getMTime()));
$this->_backup_directory($parent, $obj->getPathname(), $basedir);
}
else if ($obj->isFile()) {
$child = $parent->addChild('file');
$child->addChild('name', substr($obj->getPathname(), strlen($basedir.'/')));
$child->addChild('mode', substr(sprintf('%o', $obj->getPerms()), -4));
$child->addChild('mtime', date(DATE_RFC822, $obj->getMTime()));
$child->addChild('content', base64_encode(file_get_contents($obj->getPathname())));
}
}
}
function _cleanup_directory($dir, $basedir) {
if (is_dir($dir)) {
foreach (new DirectoryIterator($dir) as $obj) {
if (!$obj->isDot()) {
if ($obj->isDir()) {
$this->_cleanup_directory($obj->getPathname(), $basedir);
if (rmdir($obj->getPathname()) === false) {
Flash::set('error', __('Unable to delete directory :name.', array(':name' => substr($obj->getPathname(), strlen($basedir.'/')))));
redirect(get_url('plugin/backup_restore'));
}
}
else {
if (unlink($obj->getPathname()) === false) {
Flash::set('error', __('Unable to delete file :name.', array(':name' => substr($obj->getPathname(), strlen($basedir.'/')))));
redirect(get_url('plugin/backup_restore'));
}
}
}
}
}
}
function _restore_attributes($name, $obj) {
// Set file attributes
if (array_key_exists('mode', $obj)) {
if (chmod($name, $obj->mode) === false) {
Flash::set('error', __('Unable to restore attributes for :name.', array(':name' => $obj->name)));
redirect(get_url('plugin/backup_restore'));
}
}
// Set modification time
if (array_key_exists('mtime', $obj)) {
$dt = date_parse($obj->mtime);
$mtime = mktime($dt['hour'], $dt['minute'], $dt['second'], $dt['month'], $dt['day'], $dt['year']);
if (touch($name, $mtime) === false) {
Flash::set('error', __('Unable to restore modification date for :name.', array(':name' => $obj->name)));
redirect(get_url('plugin/backup_restore'));
}
}
}
}
class SimpleXMLExtended extends SimpleXMLElement {
public function addCData($nodename,$cdata_text) {
$sxe = $this->addChild($nodename); //Added a nodename to create inside the function
$node = dom_import_simplexml($sxe);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection($cdata_text));
return $sxe;
}
}