Newer
Older
<?php
/**
* @file
* A filter for compressing bckups with zip, gz bzip etc.
*/
/**
* A filter for compressing backup files.
*
* @ingroup backup_migrate_filters
*/
class backup_migrate_filter_compression extends backup_migrate_filter {
var $op_weights = array('backup' => 100, 'restore' => -100);
/**
* This function is called on a backup file after the backup has been completed.
*/
ronan
committed
function backup($file, &$settings) {
return $this->_backup_migrate_file_compress($file, $settings);
}
/**
* This function is called on a backup file before importing it.
*/
function restore($file, &$settings) {
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
return $this->_backup_migrate_file_decompress($file);
}
/**
* Get the form for the settings for this filter.
*/
function backup_settings_default() {
return array('compression' => 'none');
}
/**
* Get the form for the settings for this filter.
*/
function backup_settings_form($settings) {
$form = array();
$compression_options = $this->_backup_migrate_get_compression_form_item_options();
$form['file']['compression'] = array(
"#type" => count($compression_options) > 1 ? "select" : 'value',
"#title" => t("Compression"),
"#options" => $compression_options,
"#default_value" => $settings['compression'],
);
return $form;
}
/**
* Return a list of backup filetypes.
*/
function file_types() {
return array(
"gzip" => array(
"extension" => "gz",
"filemime" => "application/x-gzip",
"backup" => TRUE,
"restore" => TRUE,
),
"bzip" => array(
"extension" => "bz",
"filemime" => "application/x-bzip",
"backup" => TRUE,
"restore" => TRUE,
),
"zip" => array(
"extension" => "zip",
"filemime" => "application/zip",
"backup" => TRUE,
"restore" => TRUE,
),
);
}
/**
* Get the compression options as an options array for a form item.
*/
function _backup_migrate_get_compression_form_item_options() {
$compression_options = array("none" => t("No Compression"));
if (@function_exists("gzencode")) {
$compression_options['gzip'] = t("GZip");
}
if (@function_exists("bzcompress")) {
$compression_options['bzip'] = t("BZip");
}
if (class_exists('ZipArchive')) {
$compression_options['zip'] = t("Zip");
}
return $compression_options;
}
/**
* Gzip encode a file.
*/
function _backup_migrate_gzip_encode($source, $dest, $level = 9) {
$success = FALSE;
if (@function_exists("gzopen")) {
if (($fp_out = gzopen($dest, 'wb'. $level)) && ($fp_in = fopen($source, 'rb'))) {
while (!feof($fp_in)) {
gzwrite($fp_out, fread($fp_in, 1024 * 512));
}
$success = TRUE;
}
@fclose($fp_in);
@gzclose($fp_out);
}
return $success;
}
/**
* Gzip decode a file.
*/
function _backup_migrate_gzip_decode($source, $dest) {
$success = FALSE;
if (@function_exists("gzopen")) {
if (($fp_out = fopen($dest, 'wb')) && ($fp_in = gzopen($source, 'rb'))) {
while (!feof($fp_in)) {
fwrite($fp_out, gzread($fp_in, 1024 * 512));
}
$success = TRUE;
}
@gzclose($fp_in);
@fclose($fp_out);
}
return $success;
}
/**
* Bzip encode a file.
*/
function _backup_migrate_bzip_encode($source, $dest) {
$success = FALSE;
if (@function_exists("bzopen")) {
if (($fp_out = bzopen($dest, 'w')) && ($fp_in = fopen($source, 'rb'))) {
while (!feof($fp_in)) {
bzwrite($fp_out, fread($fp_in, 1024 * 512));
}
$success = TRUE;
}
else {
$error = TRUE;
}
@fclose($fp_in);
@bzclose($fp_out);
}
return $success;
}
/**
* Bzip decode a file.
*/
function _backup_migrate_bzip_decode($source, $dest) {
$success = FALSE;
if (@function_exists("bzopen")) {
if (($fp_out = fopen($dest, 'w')) && ($fp_in = bzopen($source, 'r'))) {
while (!feof($fp_in)) {
fwrite($fp_out, gzread($fp_in, 1024 * 512));
}
$success = TRUE;
}
else {
$error = TRUE;
}
@bzclose($fp_in);
@fclose($fp_out);
}
return $success;
}
/**
* Zip encode a file.
*/
function _backup_migrate_zip_encode($source, $dest, $filename) {
$success = FALSE;
if (class_exists('ZipArchive')) {
$zip = new ZipArchive;
$res = $zip->open($dest, constant("ZipArchive::CREATE"));
if ($res === TRUE) {
$zip->addFile($source, $filename);
$success = $zip->close();
}
}
return $success;
}
/**
* Zip decode a file.
*/
function _backup_migrate_zip_decode($source, $dest) {
$success = FALSE;
if (class_exists('ZipArchive')) {
$zip = new ZipArchive;
if (($fp_out = fopen($dest, "w")) && ($zip->open($source))) {
$filename = ($zip->getNameIndex(0));
if ($fp_in = $zip->getStream($filename)) {
while (!feof($fp_in)) {
fwrite($fp_out, fread($fp_in, 1024 * 512));
}
$success = TRUE;
}
}
@fclose($fp_in);
@fclose($fp_out);
}
return $success;
}
/**
* Compress a file with the given settings.
* Also updates settings to reflect new file mime and file extension.
*/
function _backup_migrate_file_compress($file, $settings) {
switch ($settings->filters['compression']) {
case "gzip":
$from = $file->push_type('gzip');
if (!$success = $this->_backup_migrate_gzip_encode($from, $file->filepath(), 9)) {
$file = NULL;
}
break;
case "bzip":
$from = $file->push_type('bzip');
if (!$success = $this->_backup_migrate_bzip_encode($from, $file->filepath())) {
$file = NULL;
}
break;
case "zip":
$filename = $file->filename();
$from = $file->push_type('zip');
if (!$success = $this->_backup_migrate_zip_encode($from, $file->filepath(), $filename)) {
$file = NULL;
}
break;
}
if (!$file) {
_backup_migrate_message("Could not compress backup file. Try backing up without compression.", array(), 'error');
}
return $file;
}
/**
* Decompress a file with the given settings.
* Also updates settings to reflect new file mime and file extension.
*/
function _backup_migrate_file_decompress($file) {
$success = FALSE;
switch ($file->type_id()) {
case "gzip":
$from = $file->pop_type();
$success = $this->_backup_migrate_gzip_decode($from->filepath(), $file->filepath());
break;
case "bzip":
$from = $file->pop_type();
$success = $this->_backup_migrate_bzip_decode($from->filepath(), $file->filepath());
break;
case "zip":
$from = $file->pop_type();
$success = $this->_backup_migrate_zip_decode($from->filepath(), $file->filepath());
break;
default:
return $file;
break;
}
if (!$success) {
_backup_migrate_message("Could not decompress backup file. Please check that the file is valid.", array(), 'error');
}
return $success ? $file : NULL;
}
}