filters.compression.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * @file
  4. * A filter for compressing backups with Zip, gzip, and bzip2.
  5. */
  6. /**
  7. * A filter for compressing backup files.
  8. *
  9. * @ingroup backup_migrate_filters
  10. */
  11. class backup_migrate_filter_compression extends backup_migrate_filter {
  12. var $op_weights = array('backup' => 100, 'restore' => -100);
  13. /**
  14. * This function is called on a backup file after the backup has been completed.
  15. */
  16. function backup($file, &$settings) {
  17. return $this->_backup_migrate_file_compress($file, $settings);
  18. }
  19. /**
  20. * This function is called on a backup file before importing it.
  21. */
  22. function restore($file, &$settings) {
  23. return $this->_backup_migrate_file_decompress($file);
  24. }
  25. /**
  26. * Get the form for the settings for this filter.
  27. */
  28. function backup_settings_default() {
  29. $options = $this->_backup_migrate_get_compression_form_item_options();
  30. return array('compression' => isset($options['gzip']) ? 'gzip' : 'none');
  31. }
  32. /**
  33. * Get the form for the settings for this filter.
  34. */
  35. function backup_settings_form($settings) {
  36. $form = array();
  37. $compression_options = $this->_backup_migrate_get_compression_form_item_options();
  38. $form['file']['compression'] = array(
  39. "#type" => count($compression_options) > 1 ? "select" : 'value',
  40. "#title" => t("Compression"),
  41. "#options" => $compression_options,
  42. "#default_value" => $settings['compression'],
  43. );
  44. return $form;
  45. }
  46. /**
  47. * Return a list of backup filetypes.
  48. */
  49. function file_types() {
  50. return array(
  51. "gzip" => array(
  52. "extension" => "gz",
  53. "filemime" => "application/x-gzip",
  54. "backup" => TRUE,
  55. "restore" => TRUE,
  56. ),
  57. "bzip" => array(
  58. "extension" => "bz",
  59. "filemime" => "application/x-bzip",
  60. "backup" => TRUE,
  61. "restore" => TRUE,
  62. ),
  63. "bzip2" => array(
  64. "extension" => "bz2",
  65. "filemime" => "application/x-bzip",
  66. "backup" => TRUE,
  67. "restore" => TRUE,
  68. ),
  69. "zip" => array(
  70. "extension" => "zip",
  71. "filemime" => "application/zip",
  72. "backup" => TRUE,
  73. "restore" => TRUE,
  74. ),
  75. );
  76. }
  77. /**
  78. * Get the compression options as an options array for a form item.
  79. */
  80. function _backup_migrate_get_compression_form_item_options() {
  81. $compression_options = array("none" => t("No Compression"));
  82. if (@function_exists("gzencode")) {
  83. $compression_options['gzip'] = t("gzip");
  84. }
  85. if (@function_exists("bzcompress")) {
  86. $compression_options['bzip'] = t("bzip2");
  87. }
  88. if (class_exists('ZipArchive')) {
  89. $compression_options['zip'] = t("Zip", array(), array('context' => 'compression format'));
  90. }
  91. return $compression_options;
  92. }
  93. /**
  94. * Encode a file with gzip.
  95. */
  96. function _backup_migrate_gzip_encode($source, $dest, $level = 9) {
  97. $success = FALSE;
  98. if (@function_exists("gzopen")) {
  99. if (($fp_out = gzopen($dest, 'wb'. $level)) && ($fp_in = fopen($source, 'rb'))) {
  100. while (!feof($fp_in)) {
  101. gzwrite($fp_out, fread($fp_in, 1024 * 512));
  102. }
  103. $success = TRUE;
  104. }
  105. @fclose($fp_in);
  106. @gzclose($fp_out);
  107. }
  108. return $success;
  109. }
  110. /**
  111. * Decode a file with gzip.
  112. */
  113. function _backup_migrate_gzip_decode($source, $dest) {
  114. $success = FALSE;
  115. if (@function_exists("gzopen")) {
  116. if (($fp_out = fopen($dest, 'wb')) && ($fp_in = gzopen($source, 'rb'))) {
  117. while (!feof($fp_in)) {
  118. fwrite($fp_out, gzread($fp_in, 1024 * 512));
  119. }
  120. $success = TRUE;
  121. }
  122. @gzclose($fp_in);
  123. @fclose($fp_out);
  124. }
  125. return $success;
  126. }
  127. /**
  128. * Encode a file with bzip2.
  129. */
  130. function _backup_migrate_bzip_encode($source, $dest) {
  131. $success = FALSE;
  132. if (@function_exists("bzopen")) {
  133. if (($fp_out = bzopen($dest, 'w')) && ($fp_in = fopen($source, 'rb'))) {
  134. while (!feof($fp_in)) {
  135. bzwrite($fp_out, fread($fp_in, 1024 * 512));
  136. }
  137. $success = TRUE;
  138. }
  139. else {
  140. $error = TRUE;
  141. }
  142. @fclose($fp_in);
  143. @bzclose($fp_out);
  144. }
  145. return $success;
  146. }
  147. /**
  148. * Decode a file with bzip2.
  149. */
  150. function _backup_migrate_bzip_decode($source, $dest) {
  151. $success = FALSE;
  152. if (@function_exists("bzopen")) {
  153. if (($fp_out = fopen($dest, 'w')) && ($fp_in = bzopen($source, 'r'))) {
  154. while (!feof($fp_in)) {
  155. fwrite($fp_out, gzread($fp_in, 1024 * 512));
  156. }
  157. $success = TRUE;
  158. }
  159. else {
  160. $error = TRUE;
  161. }
  162. @bzclose($fp_in);
  163. @fclose($fp_out);
  164. }
  165. return $success;
  166. }
  167. /**
  168. * Encode a file with Zip.
  169. */
  170. function _backup_migrate_zip_encode($source, $dest, $filename) {
  171. $success = FALSE;
  172. if (class_exists('ZipArchive')) {
  173. $zip = new ZipArchive;
  174. $res = $zip->open($dest, constant("ZipArchive::CREATE"));
  175. if ($res === TRUE) {
  176. $zip->addFile($source, $filename);
  177. $success = $zip->close();
  178. }
  179. }
  180. return $success;
  181. }
  182. /**
  183. * Decode a file with Zip.
  184. */
  185. function _backup_migrate_zip_decode($source, $dest) {
  186. $success = FALSE;
  187. if (class_exists('ZipArchive')) {
  188. $zip = new ZipArchive;
  189. if (($fp_out = fopen($dest, "w")) && ($zip->open($source))) {
  190. $filename = ($zip->getNameIndex(0));
  191. if ($fp_in = $zip->getStream($filename)) {
  192. while (!feof($fp_in)) {
  193. fwrite($fp_out, fread($fp_in, 1024 * 512));
  194. }
  195. $success = TRUE;
  196. }
  197. }
  198. @fclose($fp_in);
  199. @fclose($fp_out);
  200. }
  201. return $success;
  202. }
  203. /**
  204. * Compress a file with the given settings.
  205. * Also updates settings to reflect new file mime and file extension.
  206. */
  207. function _backup_migrate_file_compress($file, $settings) {
  208. switch ($settings->filters['compression']) {
  209. case "gzip":
  210. $from = $file->push_type('gzip');
  211. if (!$success = $this->_backup_migrate_gzip_encode($from, $file->filepath(), 9)) {
  212. $file = NULL;
  213. }
  214. break;
  215. case "bzip":
  216. $from = $file->push_type('bzip2');
  217. if (!$success = $this->_backup_migrate_bzip_encode($from, $file->filepath())) {
  218. $file = NULL;
  219. }
  220. break;
  221. case "zip":
  222. $filename = $file->filename();
  223. $from = $file->push_type('zip');
  224. if (!$success = $this->_backup_migrate_zip_encode($from, $file->filepath(), $filename)) {
  225. $file = NULL;
  226. }
  227. break;
  228. }
  229. if (!$file) {
  230. _backup_migrate_message("Could not compress backup file. Try backing up without compression.", array(), 'error');
  231. }
  232. return $file;
  233. }
  234. /**
  235. * Decompress a file with the given settings.
  236. * Also updates settings to reflect new file mime and file extension.
  237. */
  238. function _backup_migrate_file_decompress($file) {
  239. $success = FALSE;
  240. switch ($file->type_id()) {
  241. case "gzip":
  242. $from = $file->pop_type();
  243. $success = $this->_backup_migrate_gzip_decode($from->filepath(), $file->filepath());
  244. break;
  245. case "bzip":
  246. case "bzip2":
  247. $from = $file->pop_type();
  248. $success = $this->_backup_migrate_bzip_decode($from->filepath(), $file->filepath());
  249. break;
  250. case "zip":
  251. $from = $file->pop_type();
  252. $success = $this->_backup_migrate_zip_decode($from->filepath(), $file->filepath());
  253. break;
  254. default:
  255. return $file;
  256. break;
  257. }
  258. if (!$success) {
  259. _backup_migrate_message("Could not decompress backup file. Please check that the file is valid.", array(), 'error');
  260. }
  261. return $success ? $file : NULL;
  262. }
  263. }