PclZipProxy.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. require_once 'pclzip/pclzip.lib.php';
  3. require_once 'ZipInterface.php';
  4. class PclZipProxyException extends Exception
  5. { }
  6. /**
  7. * Proxy class for the PclZip library
  8. * You need PHP 5.2 at least
  9. * You need Zip Extension or PclZip library
  10. * Encoding : ISO-8859-1
  11. * Last commit by $Author: neveldo $
  12. * Date - $Date: 2009-05-29 10:05:11 +0200 (ven., 29 mai 2009) $
  13. * SVN Revision - $Rev: 28 $
  14. * Id : $Id: odf.php 28 2009-05-29 08:05:11Z neveldo $
  15. *
  16. * @copyright GPL License 2008 - Julien Pauli - Cyril PIERRE de GEYER - Anaska (http://www.anaska.com)
  17. * @license http://www.gnu.org/copyleft/gpl.html GPL License
  18. * @version 1.3
  19. */
  20. class PclZipProxy implements ZipInterface
  21. {
  22. // MODIF YC: use drupal temp directory
  23. // see http://www.odtphp.com/forum/viewtopic.php?f=5&t=20#p126
  24. const TMP_DIR = PCL_ZIP_TMP;
  25. //const TMP_DIR = './tmp';
  26. protected $openned = false;
  27. protected $filename;
  28. protected $pclzip;
  29. /**
  30. * Class constructor
  31. *
  32. * @throws PclZipProxyException
  33. */
  34. public function __construct()
  35. {
  36. if (! class_exists('PclZip')) {
  37. throw new PclZipProxyException('PclZip class not loaded - PclZip library
  38. is required for using PclZipProxy'); ;
  39. }
  40. }
  41. /**
  42. * Open a Zip archive
  43. *
  44. * @param string $filename the name of the archive to open
  45. * @return true if openning has succeeded
  46. */
  47. public function open($filename)
  48. {
  49. if (true === $this->openned) {
  50. $this->close();
  51. }
  52. if (!file_exists(self::TMP_DIR)) {
  53. mkdir(self::TMP_DIR);
  54. }
  55. $this->filename = $filename;
  56. $this->pclzip = new PclZip($this->filename);
  57. $this->openned = true;
  58. return true;
  59. }
  60. /**
  61. * Retrieve the content of a file within the archive from its name
  62. *
  63. * @param string $name the name of the file to extract
  64. * @return the content of the file in a string
  65. */
  66. public function getFromName($name)
  67. {
  68. if (false === $this->openned) {
  69. return false;
  70. }
  71. $name = preg_replace("/(?:\.|\/)*(.*)/", "\\1", $name);
  72. $extraction = $this->pclzip->extract(PCLZIP_OPT_BY_NAME, $name,
  73. PCLZIP_OPT_EXTRACT_AS_STRING);
  74. if (!empty($extraction)) {
  75. return $extraction[0]['content'];
  76. }
  77. return false;
  78. }
  79. /**
  80. * Add a file within the archive from a string
  81. *
  82. * @param string $localname the local path to the file in the archive
  83. * @param string $contents the content of the file
  84. * @return true if the file has been successful added
  85. */
  86. public function addFromString($localname, $contents)
  87. {
  88. if (false === $this->openned) {
  89. return false;
  90. }
  91. if (file_exists($this->filename) && !is_writable($this->filename)) {
  92. return false;
  93. }
  94. $localname = preg_replace("/(?:\.|\/)*(.*)/", "\\1", $localname);
  95. $localpath = dirname($localname);
  96. $tmpfilename = self::TMP_DIR . '/' . basename($localname);
  97. if (false !== file_put_contents($tmpfilename, $contents)) {
  98. $this->pclzip->delete(PCLZIP_OPT_BY_NAME, $localname);
  99. $add = $this->pclzip->add($tmpfilename,
  100. PCLZIP_OPT_REMOVE_PATH, self::TMP_DIR,
  101. PCLZIP_OPT_ADD_PATH, $localpath);
  102. unlink($tmpfilename);
  103. if (!empty($add)) {
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. /**
  110. * Add a file within the archive from a file
  111. *
  112. * @param string $filename the path to the file we want to add
  113. * @param string $localname the local path to the file in the archive
  114. * @return true if the file has been successful added
  115. */
  116. public function addFile($filename, $localname = null)
  117. {
  118. if (false === $this->openned) {
  119. return false;
  120. }
  121. if ((file_exists($this->filename) && !is_writable($this->filename))
  122. || !file_exists($filename)) {
  123. return false;
  124. }
  125. if (isSet($localname)) {
  126. $localname = preg_replace("/(?:\.|\/)*(.*)/", "\\1", $localname);
  127. $localpath = dirname($localname);
  128. $tmpfilename = self::TMP_DIR . '/' . basename($localname);
  129. } else {
  130. $localname = basename($filename);
  131. $tmpfilename = self::TMP_DIR . '/' . $localname;
  132. $localpath = '';
  133. }
  134. if (file_exists($filename)) {
  135. copy($filename, $tmpfilename);
  136. $this->pclzip->delete(PCLZIP_OPT_BY_NAME, $localname);
  137. $this->pclzip->add($tmpfilename,
  138. PCLZIP_OPT_REMOVE_PATH, self::TMP_DIR,
  139. PCLZIP_OPT_ADD_PATH, $localpath);
  140. unlink($tmpfilename);
  141. return true;
  142. }
  143. return false;
  144. }
  145. /**
  146. * Close the Zip archive
  147. * @return true
  148. */
  149. public function close()
  150. {
  151. if (false === $this->openned) {
  152. return false;
  153. }
  154. $this->pclzip = $this->filename = null;
  155. $this->openned = false;
  156. if (file_exists(self::TMP_DIR)) {
  157. $this->_rrmdir(self::TMP_DIR);
  158. rmdir(self::TMP_DIR);
  159. }
  160. return true;
  161. }
  162. /**
  163. * Empty the temporary working directory recursively
  164. * @param $dir the temporary working directory
  165. * @return void
  166. */
  167. private function _rrmdir($dir)
  168. {
  169. if ($handle = opendir($dir)) {
  170. while (false !== ($file = readdir($handle))) {
  171. if ($file != '.' && $file != '..') {
  172. if (is_dir($dir . '/' . $file)) {
  173. $this->_rrmdir($dir . '/' . $file);
  174. rmdir($dir . '/' . $file);
  175. } else {
  176. unlink($dir . '/' . $file);
  177. }
  178. }
  179. }
  180. closedir($handle);
  181. }
  182. }
  183. }
  184. ?>