PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/core/xpdo/compression/xpdozip.class.php

https://github.com/gbds/revolution
PHP | 203 lines | 118 code | 10 blank | 75 comment | 40 complexity | 5c5f5563410a445d49e965d204f4333a MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2006, 2007, 2008, 2009, 2010 by Jason Coward <xpdo@opengeek.com>
  4. *
  5. * This file is part of xPDO.
  6. *
  7. * xPDO is free software; you can redistribute it and/or modify it under the
  8. * terms of the GNU General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option) any later
  10. * version.
  11. *
  12. * xPDO is distributed in the hope that it will be useful, but WITHOUT ANY
  13. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  14. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * xPDO; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
  18. * Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. /**
  21. * A class to abstract compression support for ZIP format.
  22. *
  23. * This file contains the xPDOZip class to represent ZIP archives.
  24. *
  25. * @package xpdo
  26. * @subpackage compression
  27. */
  28. /**
  29. * Represents a compressed archive in ZIP format.
  30. *
  31. * @package xpdo
  32. * @subpackage compression
  33. */
  34. class xPDOZip {
  35. const CREATE = 'create';
  36. const OVERWRITE = 'overwrite';
  37. const ZIP_TARGET = 'zip_target';
  38. public $xpdo = null;
  39. protected $_filename = '';
  40. protected $_options = array();
  41. protected $_archive = null;
  42. protected $_errors = array();
  43. /**
  44. * Construct an instance representing a specific archive.
  45. *
  46. * @param xPDO &$xpdo A reference to an xPDO instance.
  47. * @param string $filename The name of the archive the instance will represent.
  48. * @param array $options An array of options for this instance.
  49. */
  50. public function __construct(xPDO &$xpdo, $filename, array $options = array()) {
  51. $this->xpdo =& $xpdo;
  52. $this->_filename = is_string($filename) ? $filename : '';
  53. $this->_options = is_array($options) ? $options : array();
  54. $this->_archive = new ZipArchive();
  55. if (!empty($this->_filename) && file_exists(dirname($this->_filename))) {
  56. if (file_exists($this->_filename)) {
  57. if ($this->getOption(xPDOZip::OVERWRITE, null, false) && is_writable($this->_filename)) {
  58. if ($this->_archive->open($this->_filename, ZIPARCHIVE::OVERWRITE) !== true) {
  59. $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "xPDOZip: Error opening archive at {$this->_filename} for OVERWRITE");
  60. }
  61. } else {
  62. if ($this->_archive->open($this->_filename) !== true) {
  63. $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "xPDOZip: Error opening archive at {$this->_filename}");
  64. }
  65. }
  66. } elseif ($this->getOption(xPDOZip::CREATE, null, false) && is_writable(dirname($this->_filename))) {
  67. if ($this->_archive->open($this->_filename, ZIPARCHIVE::CREATE) !== true) {
  68. $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "xPDOZip: Could not create archive at {$this->_filename}");
  69. }
  70. } else {
  71. $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "xPDOZip: The location specified is not writable: {$this->_filename}");
  72. }
  73. } else {
  74. $this->xpdo->log(xPDO::LOG_LEVEL_ERROR, "xPDOZip: The location specified does not exist: {$this->_filename}");
  75. }
  76. }
  77. /**
  78. * Pack the contents from the source into the archive.
  79. *
  80. * @todo Implement custom file filtering options
  81. *
  82. * @param string $source The path to the source file(s) to pack.
  83. * @param array $options An array of options for the operation.
  84. * @return array An array of results for the operation.
  85. */
  86. public function pack($source, array $options = array()) {
  87. $results = array();
  88. if ($this->_archive) {
  89. $target = $this->getOption(xPDOZip::ZIP_TARGET, $options, '');
  90. if (is_dir($source)) {
  91. if ($dh = opendir($source)) {
  92. if ($source[strlen($source) - 1] !== '/') $source .= '/';
  93. while (($file = readdir($dh)) !== false) {
  94. if (is_dir($source . $file)) {
  95. if (($file !== '.') && ($file !== '..')) {
  96. $results = $results + $this->pack($source . $file . '/', array_merge($options, array(xPDOZip::ZIP_TARGET => $target . $file . '/')));
  97. }
  98. } elseif (is_file($source . $file)) {
  99. if ($this->_archive->addFile($source . $file, $target . $file)) {
  100. $results[$target . $file] = "Successfully packed {$target}{$file} from {$source}{$file}";
  101. } else {
  102. $results[$target . $file] = "Error packing {$target}{$file} from {$source}{$file}";
  103. $this->_errors[] = $results[$target . $file];
  104. }
  105. } else {
  106. $results[$target . $file] = "Error packing {$target}{$file} from {$source}{$file}";
  107. $this->_errors[] = $results[$target . $file];
  108. }
  109. }
  110. }
  111. } elseif (is_file($source)) {
  112. $file = basename($source);
  113. if ($this->_archive->addFile($source, $target . $file)) {
  114. $results[$target . $file] = "Successfully packed {$target}{$file} from {$source}";
  115. } else {
  116. $results[$target . $file] = "Error packing {$target}{$file} from {$source}";
  117. $this->_errors[] = $results[$target . $file];
  118. }
  119. } else {
  120. $this->_errors[]= "Invalid source specified: {$source}";
  121. }
  122. }
  123. return $results;
  124. }
  125. /**
  126. * Unpack the compressed contents from the archive to the target.
  127. *
  128. * @param string $target The path of the target location to unpack the files.
  129. * @param array $options An array of options for the operation.
  130. * @return array An array of results for the operation.
  131. */
  132. public function unpack($target, $options = array()) {
  133. $results = false;
  134. if ($this->_archive) {
  135. if (is_dir($target) && is_writable($target)) {
  136. $results = $this->_archive->extractTo($target);
  137. }
  138. }
  139. return $results;
  140. }
  141. /**
  142. * Close the archive.
  143. */
  144. public function close() {
  145. if ($this->_archive) {
  146. $this->_archive->close();
  147. }
  148. }
  149. /**
  150. * Get an option from supplied options, the xPDOZip instance options, or xpdo itself.
  151. *
  152. * @param string $key Unique identifier for the option.
  153. * @param array $options A set of explicit options to override those from xPDO or the
  154. * xPDOZip instance.
  155. * @param mixed $default An optional default value to return if no value is found.
  156. * @return mixed The value of the option.
  157. */
  158. public function getOption($key, $options = null, $default = null) {
  159. $option = $default;
  160. if (is_array($key)) {
  161. if (!is_array($option)) {
  162. $default= $option;
  163. $option= array();
  164. }
  165. foreach ($key as $k) {
  166. $option[$k]= $this->getOption($k, $options, $default);
  167. }
  168. } elseif (is_string($key) && !empty($key)) {
  169. if (is_array($options) && !empty($options) && array_key_exists($key, $options)) {
  170. $option = $options[$key];
  171. } elseif (is_array($this->_options) && !empty($this->_options) && array_key_exists($key, $this->_options)) {
  172. $option = $this->_options[$key];
  173. } else {
  174. $option = $this->xpdo->getOption($key, null, $default);
  175. }
  176. }
  177. return $option;
  178. }
  179. /**
  180. * Detect if errors occurred during any operations.
  181. *
  182. * @return boolean True if errors exist, otherwise false.
  183. */
  184. public function hasError() {
  185. return !empty($this->_errors);
  186. }
  187. /**
  188. * Return any errors from operations on the archive.
  189. */
  190. public function getErrors() {
  191. return $this->_errors;
  192. }
  193. }