Без опису

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @link https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2014 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Shared;
  18. use PhpOffice\PhpWord\Exception\Exception;
  19. use PhpOffice\PhpWord\Settings;
  20. /**
  21. * ZipArchive wrapper
  22. *
  23. * Wraps zip archive functionality of PHP ZipArchive and PCLZip. PHP ZipArchive
  24. * properties and methods are bypassed and used as the model for the PCLZip
  25. * emulation. Only needed PHP ZipArchive features are implemented.
  26. *
  27. * @method bool addFile(string $filename, string $localname = null)
  28. * @method bool addFromString(string $localname, string $contents)
  29. * @method string getNameIndex(int $index)
  30. * @method int locateName(string $name)
  31. *
  32. * @since 0.10.0
  33. */
  34. class ZipArchive
  35. {
  36. /** @const int Flags for open method */
  37. const CREATE = 1; // Emulate \ZipArchive::CREATE
  38. const OVERWRITE = 8; // Emulate \ZipArchive::OVERWRITE
  39. /**
  40. * Number of files (emulate ZipArchive::$numFiles)
  41. *
  42. * @var int
  43. */
  44. public $numFiles = 0;
  45. /**
  46. * Archive filename (emulate ZipArchive::$filename)
  47. *
  48. * @var string
  49. */
  50. public $filename;
  51. /**
  52. * Temporary storage directory
  53. *
  54. * @var string
  55. */
  56. private $tempDir;
  57. /**
  58. * Internal zip archive object
  59. *
  60. * @var \ZipArchive|\PclZip
  61. */
  62. private $zip;
  63. /**
  64. * Use PCLZip (default behaviour)
  65. *
  66. * @var bool
  67. */
  68. private $usePclzip = true;
  69. /**
  70. * Create new instance
  71. */
  72. public function __construct()
  73. {
  74. $this->usePclzip = (Settings::getZipClass() != 'ZipArchive');
  75. if ($this->usePclzip) {
  76. if (!defined('PCLZIP_TEMPORARY_DIR')) {
  77. define('PCLZIP_TEMPORARY_DIR', Settings::getTempDir() . '/');
  78. }
  79. require_once 'PCLZip/pclzip.lib.php';
  80. }
  81. }
  82. /**
  83. * Catch function calls: pass to ZipArchive or PCLZip
  84. *
  85. * `call_user_func_array` can only used for public function, hence the `public` in all `pcl...` methods
  86. *
  87. * @param mixed $function
  88. * @param mixed $args
  89. * @return mixed
  90. */
  91. public function __call($function, $args)
  92. {
  93. // Set object and function
  94. $zipFunction = $function;
  95. if (!$this->usePclzip) {
  96. $zipObject = $this->zip;
  97. } else {
  98. $zipObject = $this;
  99. $zipFunction = "pclzip{$zipFunction}";
  100. }
  101. // Run function
  102. $result = false;
  103. if (method_exists($zipObject, $zipFunction)) {
  104. $result = @call_user_func_array(array($zipObject, $zipFunction), $args);
  105. }
  106. return $result;
  107. }
  108. /**
  109. * Open a new zip archive
  110. *
  111. * @param string $filename The file name of the ZIP archive to open
  112. * @param int $flags The mode to use to open the archive
  113. * @return bool
  114. */
  115. public function open($filename, $flags = null)
  116. {
  117. $result = true;
  118. $this->filename = $filename;
  119. if (!$this->usePclzip) {
  120. $zip = new \ZipArchive();
  121. $result = $zip->open($this->filename, $flags);
  122. // Scrutizer will report the property numFiles does not exist
  123. // See https://github.com/scrutinizer-ci/php-analyzer/issues/190
  124. $this->numFiles = $zip->numFiles;
  125. } else {
  126. $zip = new \PclZip($this->filename);
  127. $this->tempDir = Settings::getTempDir();
  128. $this->numFiles = count($zip->listContent());
  129. }
  130. $this->zip = $zip;
  131. return $result;
  132. }
  133. /**
  134. * Close the active archive
  135. *
  136. * @return bool
  137. * @throws \PhpOffice\PhpWord\Exception\Exception
  138. * @codeCoverageIgnore Can't find any test case. Uncomment when found.
  139. */
  140. public function close()
  141. {
  142. if (!$this->usePclzip) {
  143. if ($this->zip->close() === false) {
  144. throw new Exception("Could not close zip file {$this->filename}.");
  145. }
  146. }
  147. return true;
  148. }
  149. /**
  150. * Extract the archive contents (emulate \ZipArchive)
  151. *
  152. * @param string $destination
  153. * @param string|array $entries
  154. * @return bool
  155. * @since 0.10.0
  156. */
  157. public function extractTo($destination, $entries = null)
  158. {
  159. if (!is_dir($destination)) {
  160. return false;
  161. }
  162. if (!$this->usePclzip) {
  163. return $this->zip->extractTo($destination, $entries);
  164. } else {
  165. return $this->pclzipExtractTo($destination, $entries);
  166. }
  167. }
  168. /**
  169. * Extract file from archive by given file name (emulate \ZipArchive)
  170. *
  171. * @param string $filename Filename for the file in zip archive
  172. * @return string $contents File string contents
  173. */
  174. public function getFromName($filename)
  175. {
  176. if (!$this->usePclzip) {
  177. $contents = $this->zip->getFromName($filename);
  178. if ($contents === false) {
  179. $filename = substr($filename, 1);
  180. $contents = $this->zip->getFromName($filename);
  181. }
  182. } else {
  183. $contents = $this->pclzipGetFromName($filename);
  184. }
  185. return $contents;
  186. }
  187. /**
  188. * Add a new file to the zip archive (emulate \ZipArchive)
  189. *
  190. * @param string $filename Directory/Name of the file to add to the zip archive
  191. * @param string $localname Directory/Name of the file added to the zip
  192. * @return bool
  193. */
  194. public function pclzipAddFile($filename, $localname = null)
  195. {
  196. /** @var \PclZip $zip Type hint */
  197. $zip = $this->zip;
  198. // Bugfix GH-261 https://github.com/PHPOffice/PHPWord/pull/261
  199. $realpathFilename = realpath($filename);
  200. if ($realpathFilename !== false) {
  201. $filename = $realpathFilename;
  202. }
  203. $filenameParts = pathinfo($filename);
  204. $localnameParts = pathinfo($localname);
  205. // To Rename the file while adding it to the zip we
  206. // need to create a temp file with the correct name
  207. $tempFile = false;
  208. if ($filenameParts['basename'] != $localnameParts['basename']) {
  209. $tempFile = true; // temp file created
  210. $temppath = $this->tempDir . DIRECTORY_SEPARATOR . $localnameParts['basename'];
  211. copy($filename, $temppath);
  212. $filename = $temppath;
  213. $filenameParts = pathinfo($temppath);
  214. }
  215. $pathRemoved = $filenameParts['dirname'];
  216. $pathAdded = $localnameParts['dirname'];
  217. $res = $zip->add($filename, PCLZIP_OPT_REMOVE_PATH, $pathRemoved, PCLZIP_OPT_ADD_PATH, $pathAdded);
  218. if ($tempFile) {
  219. // Remove temp file, if created
  220. unlink($this->tempDir . DIRECTORY_SEPARATOR . $localnameParts['basename']);
  221. }
  222. return ($res == 0) ? false : true;
  223. }
  224. /**
  225. * Add a new file to the zip archive from a string of raw data (emulate \ZipArchive)
  226. *
  227. * @param string $localname Directory/Name of the file to add to the zip archive
  228. * @param string $contents String of data to add to the zip archive
  229. * @return bool
  230. */
  231. public function pclzipAddFromString($localname, $contents)
  232. {
  233. /** @var \PclZip $zip Type hint */
  234. $zip = $this->zip;
  235. $filenameParts = pathinfo($localname);
  236. // Write $contents to a temp file
  237. $handle = fopen($this->tempDir . DIRECTORY_SEPARATOR . $filenameParts['basename'], 'wb');
  238. fwrite($handle, $contents);
  239. fclose($handle);
  240. // Add temp file to zip
  241. $filename = $this->tempDir . DIRECTORY_SEPARATOR . $filenameParts['basename'];
  242. $pathRemoved = $this->tempDir;
  243. $pathAdded = $filenameParts['dirname'];
  244. $res = $zip->add($filename, PCLZIP_OPT_REMOVE_PATH, $pathRemoved, PCLZIP_OPT_ADD_PATH, $pathAdded);
  245. // Remove temp file
  246. @unlink($this->tempDir . DIRECTORY_SEPARATOR . $filenameParts['basename']);
  247. return ($res == 0) ? false : true;
  248. }
  249. /**
  250. * Extract the archive contents (emulate \ZipArchive)
  251. *
  252. * @param string $destination
  253. * @param string|array $entries
  254. * @return bool
  255. * @since 0.10.0
  256. */
  257. public function pclzipExtractTo($destination, $entries = null)
  258. {
  259. /** @var \PclZip $zip Type hint */
  260. $zip = $this->zip;
  261. // Extract all files
  262. if (is_null($entries)) {
  263. $result = $zip->extract(PCLZIP_OPT_PATH, $destination);
  264. return ($result > 0) ? true : false;
  265. }
  266. // Extract by entries
  267. if (!is_array($entries)) {
  268. $entries = array($entries);
  269. }
  270. foreach ($entries as $entry) {
  271. $entryIndex = $this->locateName($entry);
  272. $result = $zip->extractByIndex($entryIndex, PCLZIP_OPT_PATH, $destination);
  273. if ($result <= 0) {
  274. return false;
  275. }
  276. }
  277. return true;
  278. }
  279. /**
  280. * Extract file from archive by given file name (emulate \ZipArchive)
  281. *
  282. * @param string $filename Filename for the file in zip archive
  283. * @return string $contents File string contents
  284. */
  285. public function pclzipGetFromName($filename)
  286. {
  287. /** @var \PclZip $zip Type hint */
  288. $zip = $this->zip;
  289. $listIndex = $this->pclzipLocateName($filename);
  290. $contents = false;
  291. if ($listIndex !== false) {
  292. $extracted = $zip->extractByIndex($listIndex, PCLZIP_OPT_EXTRACT_AS_STRING);
  293. } else {
  294. $filename = substr($filename, 1);
  295. $listIndex = $this->pclzipLocateName($filename);
  296. $extracted = $zip->extractByIndex($listIndex, PCLZIP_OPT_EXTRACT_AS_STRING);
  297. }
  298. if ((is_array($extracted)) && ($extracted != 0)) {
  299. $contents = $extracted[0]['content'];
  300. }
  301. return $contents;
  302. }
  303. /**
  304. * Returns the name of an entry using its index (emulate \ZipArchive)
  305. *
  306. * @param int $index
  307. * @return string
  308. * @since 0.10.0
  309. */
  310. public function pclzipGetNameIndex($index)
  311. {
  312. /** @var \PclZip $zip Type hint */
  313. $zip = $this->zip;
  314. $list = $zip->listContent();
  315. if (isset($list[$index])) {
  316. return $list[$index]['filename'];
  317. } else {
  318. return false;
  319. }
  320. }
  321. /**
  322. * Returns the index of the entry in the archive (emulate \ZipArchive)
  323. *
  324. * @param string $filename Filename for the file in zip archive
  325. * @return int
  326. */
  327. public function pclzipLocateName($filename)
  328. {
  329. /** @var \PclZip $zip Type hint */
  330. $zip = $this->zip;
  331. $list = $zip->listContent();
  332. $listCount = count($list);
  333. $listIndex = -1;
  334. for ($i = 0; $i < $listCount; ++$i) {
  335. if (strtolower($list[$i]['filename']) == strtolower($filename) ||
  336. strtolower($list[$i]['stored_filename']) == strtolower($filename)) {
  337. $listIndex = $i;
  338. break;
  339. }
  340. }
  341. return ($listIndex > -1) ? $listIndex : false;
  342. }
  343. }