PageRenderTime 44ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/common/include/funcs/PEAR/Zip.php

https://github.com/PicolSigns/PICOL-Generator
PHP | 3480 lines | 3255 code | 49 blank | 176 comment | 57 complexity | 2feb84f78944f32b048a148ae6331a6c MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /* vim: set ts=4 sw=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This library is free software; you can redistribute it and/or |
  9. // | modify it under the terms of the GNU Lesser General Public |
  10. // | License as published by the Free Software Foundation; either |
  11. // | version 2.1 of the License, or (at your option) any later version. |
  12. // | |
  13. // | This library is distributed in the hope that it will be useful, |
  14. // | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  15. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
  16. // | Lesser General Public License for more details. |
  17. // | |
  18. // | You should have received a copy of the GNU Lesser General Public |
  19. // | License along with this library; if not, write to the Free Software |
  20. // | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
  21. // | MA 02110-1301 USA |
  22. // +----------------------------------------------------------------------+
  23. // | Author: Vincent Blavet <vincent@phpconcept.net> |
  24. // +----------------------------------------------------------------------+
  25. //
  26. // $Id: Zip.php 302924 2010-08-31 14:45:47Z clockwerx $
  27. require_once 'PEAR.php';
  28. // ----- Constants
  29. define('ARCHIVE_ZIP_READ_BLOCK_SIZE', 2048);
  30. // ----- File list separator
  31. define('ARCHIVE_ZIP_SEPARATOR', ',');
  32. // ----- Optional static temporary directory
  33. // By default temporary files are generated in the script current
  34. // path.
  35. // If defined :
  36. // - MUST BE terminated by a '/'.
  37. // - MUST be a valid, already created directory
  38. // Samples :
  39. // define('ARCHIVE_ZIP_TEMPORARY_DIR', '/temp/');
  40. // define('ARCHIVE_ZIP_TEMPORARY_DIR', 'C:/Temp/');
  41. define('ARCHIVE_ZIP_TEMPORARY_DIR', '');
  42. // ----- Error codes
  43. define('ARCHIVE_ZIP_ERR_NO_ERROR', 0);
  44. define('ARCHIVE_ZIP_ERR_WRITE_OPEN_FAIL', -1);
  45. define('ARCHIVE_ZIP_ERR_READ_OPEN_FAIL', -2);
  46. define('ARCHIVE_ZIP_ERR_INVALID_PARAMETER', -3);
  47. define('ARCHIVE_ZIP_ERR_MISSING_FILE', -4);
  48. define('ARCHIVE_ZIP_ERR_FILENAME_TOO_LONG', -5);
  49. define('ARCHIVE_ZIP_ERR_INVALID_ZIP', -6);
  50. define('ARCHIVE_ZIP_ERR_BAD_EXTRACTED_FILE', -7);
  51. define('ARCHIVE_ZIP_ERR_DIR_CREATE_FAIL', -8);
  52. define('ARCHIVE_ZIP_ERR_BAD_EXTENSION', -9);
  53. define('ARCHIVE_ZIP_ERR_BAD_FORMAT', -10);
  54. define('ARCHIVE_ZIP_ERR_DELETE_FILE_FAIL', -11);
  55. define('ARCHIVE_ZIP_ERR_RENAME_FILE_FAIL', -12);
  56. define('ARCHIVE_ZIP_ERR_BAD_CHECKSUM', -13);
  57. define('ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP', -14);
  58. define('ARCHIVE_ZIP_ERR_MISSING_OPTION_VALUE', -15);
  59. define('ARCHIVE_ZIP_ERR_INVALID_PARAM_VALUE', -16);
  60. // ----- Warning codes
  61. define('ARCHIVE_ZIP_WARN_NO_WARNING', 0);
  62. define('ARCHIVE_ZIP_WARN_FILE_EXIST', 1);
  63. // ----- Methods parameters
  64. define('ARCHIVE_ZIP_PARAM_PATH', 'path');
  65. define('ARCHIVE_ZIP_PARAM_ADD_PATH', 'add_path');
  66. define('ARCHIVE_ZIP_PARAM_REMOVE_PATH', 'remove_path');
  67. define('ARCHIVE_ZIP_PARAM_REMOVE_ALL_PATH', 'remove_all_path');
  68. define('ARCHIVE_ZIP_PARAM_SET_CHMOD', 'set_chmod');
  69. define('ARCHIVE_ZIP_PARAM_EXTRACT_AS_STRING', 'extract_as_string');
  70. define('ARCHIVE_ZIP_PARAM_NO_COMPRESSION', 'no_compression');
  71. define('ARCHIVE_ZIP_PARAM_BY_NAME', 'by_name');
  72. define('ARCHIVE_ZIP_PARAM_BY_INDEX', 'by_index');
  73. define('ARCHIVE_ZIP_PARAM_BY_PREG', 'by_preg');
  74. define('ARCHIVE_ZIP_PARAM_PRE_EXTRACT', 'callback_pre_extract');
  75. define('ARCHIVE_ZIP_PARAM_POST_EXTRACT', 'callback_post_extract');
  76. define('ARCHIVE_ZIP_PARAM_PRE_ADD', 'callback_pre_add');
  77. define('ARCHIVE_ZIP_PARAM_POST_ADD', 'callback_post_add');
  78. /**
  79. * Class for manipulating zip archive files
  80. *
  81. * A class which provided common methods to manipulate ZIP formatted
  82. * archive files.
  83. * It provides creation, extraction, deletion and add features.
  84. *
  85. * @author Vincent Blavet <vincent@blavet.net>
  86. * @version $Revision: 302924 $
  87. * @package Archive_Zip
  88. * @category Archive
  89. */
  90. class Archive_Zip
  91. {
  92. /**
  93. * The filename of the zip archive.
  94. *
  95. * @var string Name of the Zip file
  96. */
  97. var $_zipname = '';
  98. /**
  99. * File descriptor of the opened Zip file.
  100. *
  101. * @var int Internal zip file descriptor
  102. */
  103. var $_zip_fd = 0;
  104. /**
  105. * @var int last error code
  106. */
  107. var $_error_code = 1;
  108. /**
  109. * @var string Last error description
  110. */
  111. var $_error_string = '';
  112. // {{{ constructor
  113. /**
  114. * Archive_Zip Class constructor. This flavour of the constructor only
  115. * declare a new Archive_Zip object, identifying it by the name of the
  116. * zip file.
  117. *
  118. * @param string $p_zipname The name of the zip archive to create
  119. *
  120. * @access public
  121. */
  122. function Archive_Zip($p_zipname)
  123. {
  124. // ----- Check the zlib
  125. if (!extension_loaded('zlib')) {
  126. PEAR::loadExtension('zlib');
  127. }
  128. if (!extension_loaded('zlib')) {
  129. die("The extension 'zlib' couldn't be found.\n".
  130. "Please make sure your version of PHP was built ".
  131. "with 'zlib' support.\n");
  132. return false;
  133. }
  134. // ----- Set the attributes
  135. $this->_zipname = $p_zipname;
  136. $this->_zip_fd = 0;
  137. return;
  138. }
  139. // }}}
  140. // {{{ create()
  141. /**
  142. * This method creates a Zip Archive with the filename set with
  143. * the constructor.
  144. * The files and directories indicated in $p_filelist
  145. * are added in the archive.
  146. * When a directory is in the list, the directory and its content is added
  147. * in the archive.
  148. * The methods takes a variable list of parameters in $p_params.
  149. * The supported parameters for this method are :
  150. * 'add_path' : Add a path to the archived files.
  151. * 'remove_path' : Remove the specified 'root' path of the archived files.
  152. * 'remove_all_path' : Remove all the path of the archived files.
  153. * 'no_compression' : The archived files will not be compressed.
  154. *
  155. *
  156. * @param mixed $p_filelist The list of the files or folders to add.
  157. * It can be a string with filenames separated
  158. * by a comma, or an array of filenames.
  159. * @param mixed $p_params An array of variable parameters and values.
  160. *
  161. * @return mixed An array of file description on success,
  162. * an error code on error
  163. */
  164. function create($p_filelist, $p_params = 0)
  165. {
  166. $this->_errorReset();
  167. // ----- Set default values
  168. if ($p_params === 0) {
  169. $p_params = array();
  170. }
  171. if ($this->_check_parameters($p_params,
  172. array('no_compression' => false,
  173. 'add_path' => "",
  174. 'remove_path' => "",
  175. 'remove_all_path' => false)) != 1) {
  176. return 0;
  177. }
  178. // ----- Look if the $p_filelist is really an array
  179. $p_result_list = array();
  180. if (is_array($p_filelist)) {
  181. $v_result = $this->_create($p_filelist, $p_result_list, $p_params);
  182. } else if (is_string($p_filelist)) {
  183. // ----- Create a list with the elements from the string
  184. $v_list = explode(ARCHIVE_ZIP_SEPARATOR, $p_filelist);
  185. $v_result = $this->_create($v_list, $p_result_list, $p_params);
  186. } else {
  187. $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
  188. 'Invalid variable type p_filelist');
  189. $v_result = ARCHIVE_ZIP_ERR_INVALID_PARAMETER;
  190. }
  191. if ($v_result != 1) {
  192. return 0;
  193. }
  194. return $p_result_list;
  195. }
  196. // }}}
  197. // {{{ add()
  198. /**
  199. * This method add files or directory in an existing Zip Archive.
  200. * If the Zip Archive does not exist it is created.
  201. * The files and directories to add are indicated in $p_filelist.
  202. * When a directory is in the list, the directory and its content is added
  203. * in the archive.
  204. * The methods takes a variable list of parameters in $p_params.
  205. * The supported parameters for this method are :
  206. * 'add_path' : Add a path to the archived files.
  207. * 'remove_path' : Remove the specified 'root' path of the archived files.
  208. * 'remove_all_path' : Remove all the path of the archived files.
  209. * 'no_compression' : The archived files will not be compressed.
  210. * 'callback_pre_add' : A callback function that will be called before
  211. * each entry archiving.
  212. * 'callback_post_add' : A callback function that will be called after
  213. * each entry archiving.
  214. *
  215. * @param mixed $p_filelist The list of the files or folders to add.
  216. * It can be a string with filenames separated
  217. * by a comma, or an array of filenames.
  218. * @param mixed $p_params An array of variable parameters and values.
  219. *
  220. * @return mixed An array of file description on success,
  221. * 0 on an unrecoverable failure, an error code is logged.
  222. * @access public
  223. */
  224. function add($p_filelist, $p_params = 0)
  225. {
  226. $this->_errorReset();
  227. // ----- Set default values
  228. if ($p_params === 0) {
  229. $p_params = array();
  230. }
  231. if ($this->_check_parameters($p_params,
  232. array ('no_compression' => false,
  233. 'add_path' => '',
  234. 'remove_path' => '',
  235. 'remove_all_path' => false,
  236. 'callback_pre_add' => '',
  237. 'callback_post_add' => '')) != 1) {
  238. return 0;
  239. }
  240. // ----- Look if the $p_filelist is really an array
  241. $p_result_list = array();
  242. if (is_array($p_filelist)) {
  243. // ----- Call the create fct
  244. $v_result = $this->_add($p_filelist, $p_result_list, $p_params);
  245. } else if (is_string($p_filelist)) {
  246. // ----- Create a list with the elements from the string
  247. $v_list = explode(ARCHIVE_ZIP_SEPARATOR, $p_filelist);
  248. // ----- Call the create fct
  249. $v_result = $this->_add($v_list, $p_result_list, $p_params);
  250. } else {
  251. $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
  252. "add() : Invalid variable type p_filelist");
  253. $v_result = ARCHIVE_ZIP_ERR_INVALID_PARAMETER;
  254. }
  255. if ($v_result != 1) {
  256. return 0;
  257. }
  258. return $p_result_list;
  259. }
  260. // }}}
  261. // {{{ listContent()
  262. /**
  263. * This method gives the names and properties of the files and directories
  264. * which are present in the zip archive.
  265. * The properties of each entries in the list are :
  266. * filename : Name of the file.
  267. * For create() or add() it's the filename given by the user.
  268. * For an extract() it's the filename of the extracted file.
  269. * stored_filename : Name of the file / directory stored in the archive.
  270. * size : Size of the stored file.
  271. * compressed_size : Size of the file's data compressed in the archive
  272. * (without the zip headers overhead)
  273. * mtime : Last known modification date of the file (UNIX timestamp)
  274. * comment : Comment associated with the file
  275. * folder : true | false (indicates if the entry is a folder)
  276. * index : index of the file in the archive (-1 when not available)
  277. * status : status of the action on the entry (depending of the action) :
  278. * Values are :
  279. * ok : OK !
  280. * filtered : the file/dir was not extracted (filtered by user)
  281. * already_a_directory : the file can't be extracted because a
  282. * directory with the same name already
  283. * exists
  284. * write_protected : the file can't be extracted because a file
  285. * with the same name already exists and is
  286. * write protected
  287. * newer_exist : the file was not extracted because a newer
  288. * file already exists
  289. * path_creation_fail : the file is not extracted because the
  290. * folder does not exists and can't be
  291. * created
  292. * write_error : the file was not extracted because there was a
  293. * error while writing the file
  294. * read_error : the file was not extracted because there was a
  295. * error while reading the file
  296. * invalid_header : the file was not extracted because of an
  297. * archive format error (bad file header)
  298. * Note that each time a method can continue operating when there
  299. * is an error on a single file, the error is only logged in the file status.
  300. *
  301. * @access public
  302. * @return mixed An array of file description on success,
  303. * 0 on an unrecoverable failure, an error code is logged.
  304. */
  305. function listContent()
  306. {
  307. $this->_errorReset();
  308. // ----- Check archive
  309. if (!$this->_checkFormat()) {
  310. return(0);
  311. }
  312. $v_list = array();
  313. if ($this->_list($v_list) != 1) {
  314. unset($v_list);
  315. return(0);
  316. }
  317. return $v_list;
  318. }
  319. // }}}
  320. // {{{ extract()
  321. /**
  322. * This method extract the files and folders which are in the zip archive.
  323. * It can extract all the archive or a part of the archive by using filter
  324. * feature (extract by name, by index, by preg). The extraction
  325. * can occur in the current path or an other path.
  326. * All the advanced features are activated by the use of variable
  327. * parameters.
  328. * The return value is an array of entry descriptions which gives
  329. * information on extracted files (See listContent()).
  330. * The method may return a success value (an array) even if some files
  331. * are not correctly extracted (see the file status in listContent()).
  332. * The supported variable parameters for this method are :
  333. * 'add_path' : Path where the files and directories are to be extracted
  334. * 'remove_path' : First part ('root' part) of the memorized path
  335. * (if similar) to remove while extracting.
  336. * 'remove_all_path' : Remove all the memorized path while extracting.
  337. * 'extract_as_string' :
  338. * 'set_chmod' : After the extraction of the file the indicated mode
  339. * will be set.
  340. * 'by_name' : It can be a string with file/dir names separated by ',',
  341. * or an array of file/dir names to extract from the archive.
  342. * 'by_index' : A string with range of indexes separated by ',',
  343. * (sample "1,3-5,12").
  344. * 'by_preg' : A regular expression (preg) that must match the extracted
  345. * filename.
  346. * 'callback_pre_extract' : A callback function that will be called before
  347. * each entry extraction.
  348. * 'callback_post_extract' : A callback function that will be called after
  349. * each entry extraction.
  350. *
  351. * @param mixed $p_params An array of variable parameters and values.
  352. *
  353. * @return mixed An array of file description on success,
  354. * 0 on an unrecoverable failure, an error code is logged.
  355. */
  356. function extract($p_params = 0)
  357. {
  358. $this->_errorReset();
  359. // ----- Check archive
  360. if (!$this->_checkFormat()) {
  361. return(0);
  362. }
  363. // ----- Set default values
  364. if ($p_params === 0) {
  365. $p_params = array();
  366. }
  367. if ($this->_check_parameters($p_params,
  368. array ('extract_as_string' => false,
  369. 'add_path' => '',
  370. 'remove_path' => '',
  371. 'remove_all_path' => false,
  372. 'callback_pre_extract' => '',
  373. 'callback_post_extract' => '',
  374. 'set_chmod' => 0,
  375. 'by_name' => '',
  376. 'by_index' => '',
  377. 'by_preg' => '') ) != 1) {
  378. return 0;
  379. }
  380. // ----- Call the extracting fct
  381. $v_list = array();
  382. if ($this->_extractByRule($v_list, $p_params) != 1) {
  383. unset($v_list);
  384. return(0);
  385. }
  386. return $v_list;
  387. }
  388. // }}}
  389. // {{{ delete()
  390. /**
  391. * This methods delete archive entries in the zip archive.
  392. * Notice that at least one filtering rule (set by the variable parameter
  393. * list) must be set.
  394. * Also notice that if you delete a folder entry, only the folder entry
  395. * is deleted, not all the files bellonging to this folder.
  396. * The supported variable parameters for this method are :
  397. * 'by_name' : It can be a string with file/dir names separated by ',',
  398. * or an array of file/dir names to delete from the archive.
  399. * 'by_index' : A string with range of indexes separated by ',',
  400. * (sample "1,3-5,12").
  401. * 'by_preg' : A regular expression (preg) that must match the extracted
  402. * filename.
  403. *
  404. * @param mixed $p_params An array of variable parameters and values.
  405. *
  406. * @return mixed An array of file description on success,
  407. * 0 on an unrecoverable failure, an error code is logged.
  408. */
  409. function delete($p_params)
  410. {
  411. $this->_errorReset();
  412. // ----- Check archive
  413. if (!$this->_checkFormat()) {
  414. return(0);
  415. }
  416. // ----- Set default values
  417. if ($this->_check_parameters($p_params,
  418. array ('by_name' => '',
  419. 'by_index' => '',
  420. 'by_preg' => '') ) != 1) {
  421. return 0;
  422. }
  423. // ----- Check that at least one rule is set
  424. if (($p_params['by_name'] == '')
  425. && ($p_params['by_index'] == '')
  426. && ($p_params['by_preg'] == '')) {
  427. $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
  428. 'At least one filtering rule must'
  429. .' be set as parameter');
  430. return 0;
  431. }
  432. // ----- Call the delete fct
  433. $v_list = array();
  434. if ($this->_deleteByRule($v_list, $p_params) != 1) {
  435. unset($v_list);
  436. return(0);
  437. }
  438. return $v_list;
  439. }
  440. // }}}
  441. // {{{ properties()
  442. /**
  443. * This method gives the global properties of the archive.
  444. * The properties are :
  445. * nb : Number of files in the archive
  446. * comment : Comment associated with the archive file
  447. * status : not_exist, ok
  448. *
  449. * @return mixed An array with the global properties or 0 on error.
  450. */
  451. function properties()
  452. {
  453. $this->_errorReset();
  454. // ----- Check archive
  455. if (!$this->_checkFormat()) {
  456. return(0);
  457. }
  458. // ----- Default properties
  459. $v_prop = array();
  460. $v_prop['comment'] = '';
  461. $v_prop['nb'] = 0;
  462. $v_prop['status'] = 'not_exist';
  463. // ----- Look if file exists
  464. if (@is_file($this->_zipname)) {
  465. // ----- Open the zip file
  466. if (($this->_zip_fd = @fopen($this->_zipname, 'rb')) == 0) {
  467. $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
  468. 'Unable to open archive \''.$this->_zipname
  469. .'\' in binary read mode');
  470. return 0;
  471. }
  472. // ----- Read the central directory informations
  473. $v_central_dir = array();
  474. if (($v_result = $this->_readEndCentralDir($v_central_dir)) != 1) {
  475. return 0;
  476. }
  477. $this->_closeFd();
  478. // ----- Set the user attributes
  479. $v_prop['comment'] = $v_central_dir['comment'];
  480. $v_prop['nb'] = $v_central_dir['entries'];
  481. $v_prop['status'] = 'ok';
  482. }
  483. return $v_prop;
  484. }
  485. // }}}
  486. // {{{ duplicate()
  487. /**
  488. * This method creates an archive by copying the content of an other one.
  489. * If the archive already exist, it is replaced by the new one without
  490. * any warning.
  491. *
  492. *
  493. * @param mixed $p_archive It can be a valid Archive_Zip object or
  494. * the filename of a valid zip archive.
  495. *
  496. * @return integer 1 on success, 0 on failure.
  497. */
  498. function duplicate($p_archive)
  499. {
  500. $this->_errorReset();
  501. // ----- Look if the $p_archive is a Archive_Zip object
  502. if ((is_object($p_archive))
  503. && (strtolower(get_class($p_archive)) == 'archive_zip')) {
  504. $v_result = $this->_duplicate($p_archive->_zipname);
  505. } else if (is_string($p_archive)) {
  506. // ----- Check that $p_archive is a valid zip file
  507. // TBC : Should also check the archive format
  508. if (!is_file($p_archive)) {
  509. $this->_errorLog(ARCHIVE_ZIP_ERR_MISSING_FILE,
  510. "No file with filename '".$p_archive."'");
  511. $v_result = ARCHIVE_ZIP_ERR_MISSING_FILE;
  512. } else {
  513. $v_result = $this->_duplicate($p_archive);
  514. }
  515. } else {
  516. $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
  517. "Invalid variable type p_archive_to_add");
  518. $v_result = ARCHIVE_ZIP_ERR_INVALID_PARAMETER;
  519. }
  520. return $v_result;
  521. }
  522. // }}}
  523. // {{{ merge()
  524. /**
  525. * This method merge a valid zip archive at the end of the
  526. * archive identified by the Archive_Zip object.
  527. * If the archive ($this) does not exist, the merge becomes a duplicate.
  528. * If the archive to add does not exist, the merge is a success.
  529. *
  530. * @param mixed $p_archive_to_add It can be a valid Archive_Zip object or
  531. * the filename of a valid zip archive.
  532. *
  533. * @return integer 1 on success, 0 on failure.
  534. */
  535. function merge($p_archive_to_add)
  536. {
  537. $v_result = 1;
  538. $this->_errorReset();
  539. // ----- Check archive
  540. if (!$this->_checkFormat()) {
  541. return(0);
  542. }
  543. // ----- Look if the $p_archive_to_add is a Archive_Zip object
  544. if ((is_object($p_archive_to_add))
  545. && (strtolower(get_class($p_archive_to_add)) == 'archive_zip')) {
  546. $v_result = $this->_merge($p_archive_to_add);
  547. } else if (is_string($p_archive_to_add)) {
  548. // ----- Create a temporary archive
  549. $v_object_archive = new Archive_Zip($p_archive_to_add);
  550. // ----- Merge the archive
  551. $v_result = $this->_merge($v_object_archive);
  552. } else {
  553. $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
  554. "Invalid variable type p_archive_to_add");
  555. $v_result = ARCHIVE_ZIP_ERR_INVALID_PARAMETER;
  556. }
  557. return $v_result;
  558. }
  559. // }}}
  560. // {{{ errorCode()
  561. /**
  562. * Method that gives the lastest error code.
  563. *
  564. * @access public
  565. * @return integer The error code value.
  566. */
  567. function errorCode()
  568. {
  569. return($this->_error_code);
  570. }
  571. // }}}
  572. // {{{ errorName()
  573. /**
  574. * This method gives the latest error code name.
  575. *
  576. * @param boolean $p_with_code If true, gives the name and the int value.
  577. *
  578. * @access public
  579. * @return string The error name.
  580. */
  581. function errorName($p_with_code = false)
  582. {
  583. $v_const_list = get_defined_constants();
  584. // ----- Extract error constants from all const.
  585. for (reset($v_const_list);
  586. list($v_key, $v_value) = each($v_const_list);) {
  587. if (substr($v_key, 0, strlen('ARCHIVE_ZIP_ERR_')) =='ARCHIVE_ZIP_ERR_') {
  588. $v_error_list[$v_key] = $v_value;
  589. }
  590. }
  591. // ----- Search the name form the code value
  592. $v_key = array_search($this->_error_code, $v_error_list, true);
  593. if ($v_key!=false) {
  594. $v_value = $v_key;
  595. } else {
  596. $v_value = 'NoName';
  597. }
  598. if ($p_with_code) {
  599. return($v_value.' ('.$this->_error_code.')');
  600. } else {
  601. return($v_value);
  602. }
  603. }
  604. // }}}
  605. // {{{ errorInfo()
  606. /**
  607. * This method returns the description associated with the latest error.
  608. *
  609. * @param boolean $p_full If set to true gives the description with the
  610. * error code, the name and the description.
  611. * If set to false gives only the description
  612. * and the error code.
  613. *
  614. * @access public
  615. * @return string The error description.
  616. */
  617. function errorInfo($p_full = false)
  618. {
  619. if ($p_full) {
  620. return($this->errorName(true)." : ".$this->_error_string);
  621. } else {
  622. return($this->_error_string." [code ".$this->_error_code."]");
  623. }
  624. }
  625. // }}}
  626. // -----------------------------------------------------------------------------
  627. // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
  628. // ***** *****
  629. // ***** THESES FUNCTIONS MUST NOT BE USED DIRECTLY *****
  630. // -----------------------------------------------------------------------------
  631. // ---------------------------------------------------------------------------
  632. // Function : _checkFormat()
  633. // Description :
  634. // This method check that the archive exists and is a valid zip archive.
  635. // Several level of check exists. (futur)
  636. // Parameters :
  637. // $p_level : Level of check. Default 0.
  638. // 0 : Check the first bytes (magic codes) (default value))
  639. // 1 : 0 + Check the central directory (futur)
  640. // 2 : 1 + Check each file header (futur)
  641. // Return Values :
  642. // true on success,
  643. // false on error, the error code is set.
  644. // ---------------------------------------------------------------------------
  645. /**
  646. * Archive_Zip::_checkFormat()
  647. *
  648. * { Description }
  649. *
  650. * @param integer $p_level
  651. *
  652. * @return bool
  653. */
  654. function _checkFormat($p_level = 0)
  655. {
  656. $v_result = true;
  657. // ----- Reset the error handler
  658. $this->_errorReset();
  659. // ----- Look if the file exits
  660. if (!is_file($this->_zipname)) {
  661. // ----- Error log
  662. $this->_errorLog(ARCHIVE_ZIP_ERR_MISSING_FILE,
  663. "Missing archive file '".$this->_zipname."'");
  664. return(false);
  665. }
  666. // ----- Check that the file is readeable
  667. if (!is_readable($this->_zipname)) {
  668. // ----- Error log
  669. $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
  670. "Unable to read archive '".$this->_zipname."'");
  671. return(false);
  672. }
  673. // ----- Check the magic code
  674. // TBC
  675. // ----- Check the central header
  676. // TBC
  677. // ----- Check each file header
  678. // TBC
  679. return $v_result;
  680. }
  681. // ---------------------------------------------------------------------------
  682. // ---------------------------------------------------------------------------
  683. // Function : _create()
  684. // Description :
  685. // Parameters :
  686. // Return Values :
  687. // ---------------------------------------------------------------------------
  688. /**
  689. * Archive_Zip::_create()
  690. *
  691. * { Description }
  692. *
  693. * @return int
  694. */
  695. function _create($p_list, &$p_result_list, &$p_params)
  696. {
  697. $v_result = 1;
  698. $v_list_detail = array();
  699. $p_add_dir = $p_params['add_path'];
  700. $p_remove_dir = $p_params['remove_path'];
  701. $p_remove_all_dir = $p_params['remove_all_path'];
  702. // ----- Open the file in write mode
  703. if (($v_result = $this->_openFd('wb')) != 1) {
  704. return $v_result;
  705. }
  706. // ----- Add the list of files
  707. $v_result = $this->_addList($p_list, $p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_params);
  708. // ----- Close
  709. $this->_closeFd();
  710. return $v_result;
  711. }
  712. // ---------------------------------------------------------------------------
  713. // ---------------------------------------------------------------------------
  714. // Function : _add()
  715. // Description :
  716. // Parameters :
  717. // Return Values :
  718. // ---------------------------------------------------------------------------
  719. /**
  720. * Archive_Zip::_add()
  721. *
  722. * { Description }
  723. *
  724. * @return int
  725. */
  726. function _add($p_list, &$p_result_list, &$p_params)
  727. {
  728. $v_result = 1;
  729. $v_list_detail = array();
  730. $p_add_dir = $p_params['add_path'];
  731. $p_remove_dir = $p_params['remove_path'];
  732. $p_remove_all_dir = $p_params['remove_all_path'];
  733. // ----- Look if the archive exists or is empty and need to be created
  734. if ((!is_file($this->_zipname)) || (filesize($this->_zipname) == 0)) {
  735. $v_result = $this->_create($p_list, $p_result_list, $p_params);
  736. return $v_result;
  737. }
  738. // ----- Open the zip file
  739. if (($v_result = $this->_openFd('rb')) != 1) {
  740. return $v_result;
  741. }
  742. // ----- Read the central directory informations
  743. $v_central_dir = array();
  744. if (($v_result = $this->_readEndCentralDir($v_central_dir)) != 1) {
  745. $this->_closeFd();
  746. return $v_result;
  747. }
  748. // ----- Go to beginning of File
  749. @rewind($this->_zip_fd);
  750. // ----- Creates a temporay file
  751. $v_zip_temp_name = ARCHIVE_ZIP_TEMPORARY_DIR.uniqid('archive_zip-').'.tmp';
  752. // ----- Open the temporary file in write mode
  753. if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0) {
  754. $this->_closeFd();
  755. $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
  756. 'Unable to open temporary file \''
  757. .$v_zip_temp_name.'\' in binary write mode');
  758. return Archive_Zip::errorCode();
  759. }
  760. // ----- Copy the files from the archive to the temporary file
  761. // TBC : Here I should better append the file and go back to erase the
  762. // central dir
  763. $v_size = $v_central_dir['offset'];
  764. while ($v_size != 0) {
  765. $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  766. ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  767. $v_buffer = fread($this->_zip_fd, $v_read_size);
  768. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  769. $v_size -= $v_read_size;
  770. }
  771. // ----- Swap the file descriptor
  772. // Here is a trick : I swap the temporary fd with the zip fd, in order to
  773. // use the following methods on the temporary fil and not the real archive
  774. $v_swap = $this->_zip_fd;
  775. $this->_zip_fd = $v_zip_temp_fd;
  776. $v_zip_temp_fd = $v_swap;
  777. // ----- Add the files
  778. $v_header_list = array();
  779. if (($v_result = $this->_addFileList($p_list, $v_header_list,
  780. $p_add_dir, $p_remove_dir,
  781. $p_remove_all_dir, $p_params)) != 1) {
  782. fclose($v_zip_temp_fd);
  783. $this->_closeFd();
  784. @unlink($v_zip_temp_name);
  785. return $v_result;
  786. }
  787. // ----- Store the offset of the central dir
  788. $v_offset = @ftell($this->_zip_fd);
  789. // ----- Copy the block of file headers from the old archive
  790. $v_size = $v_central_dir['size'];
  791. while ($v_size != 0) {
  792. $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  793. ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  794. $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
  795. @fwrite($this->_zip_fd, $v_buffer, $v_read_size);
  796. $v_size -= $v_read_size;
  797. }
  798. // ----- Create the Central Dir files header
  799. for ($i = 0, $v_count = 0; $i<sizeof($v_header_list); $i++) {
  800. // ----- Create the file header
  801. if ($v_header_list[$i]['status'] == 'ok') {
  802. if (($v_result = $this->_writeCentralFileHeader($v_header_list[$i]))!=1) {
  803. fclose($v_zip_temp_fd);
  804. $this->_closeFd();
  805. @unlink($v_zip_temp_name);
  806. return $v_result;
  807. }
  808. $v_count++;
  809. }
  810. // ----- Transform the header to a 'usable' info
  811. $this->_convertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  812. }
  813. // ----- Zip file comment
  814. $v_comment = '';
  815. // ----- Calculate the size of the central header
  816. $v_size = @ftell($this->_zip_fd)-$v_offset;
  817. // ----- Create the central dir footer
  818. if (($v_result = $this->_writeCentralHeader($v_count
  819. +$v_central_dir['entries'],
  820. $v_size, $v_offset,
  821. $v_comment)) != 1) {
  822. // ----- Reset the file list
  823. unset($v_header_list);
  824. return $v_result;
  825. }
  826. // ----- Swap back the file descriptor
  827. $v_swap = $this->_zip_fd;
  828. $this->_zip_fd = $v_zip_temp_fd;
  829. $v_zip_temp_fd = $v_swap;
  830. // ----- Close
  831. $this->_closeFd();
  832. // ----- Close the temporary file
  833. @fclose($v_zip_temp_fd);
  834. // ----- Delete the zip file
  835. // TBC : I should test the result ...
  836. @unlink($this->_zipname);
  837. // ----- Rename the temporary file
  838. // TBC : I should test the result ...
  839. //@rename($v_zip_temp_name, $this->_zipname);
  840. $this->_tool_Rename($v_zip_temp_name, $this->_zipname);
  841. return $v_result;
  842. }
  843. // ---------------------------------------------------------------------------
  844. // ---------------------------------------------------------------------------
  845. // Function : _openFd()
  846. // Description :
  847. // Parameters :
  848. // ---------------------------------------------------------------------------
  849. /**
  850. * Archive_Zip::_openFd()
  851. *
  852. * { Description }
  853. *
  854. * @return int
  855. */
  856. function _openFd($p_mode)
  857. {
  858. $v_result = 1;
  859. // ----- Look if already open
  860. if ($this->_zip_fd != 0) {
  861. $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
  862. 'Zip file \''.$this->_zipname.'\' already open');
  863. return Archive_Zip::errorCode();
  864. }
  865. // ----- Open the zip file
  866. if (($this->_zip_fd = @fopen($this->_zipname, $p_mode)) == 0) {
  867. $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
  868. 'Unable to open archive \''.$this->_zipname
  869. .'\' in '.$p_mode.' mode');
  870. return Archive_Zip::errorCode();
  871. }
  872. return $v_result;
  873. }
  874. // ---------------------------------------------------------------------------
  875. // ---------------------------------------------------------------------------
  876. // Function : _closeFd()
  877. // Description :
  878. // Parameters :
  879. // ---------------------------------------------------------------------------
  880. /**
  881. * Archive_Zip::_closeFd()
  882. *
  883. * { Description }
  884. *
  885. * @return int
  886. */
  887. function _closeFd()
  888. {
  889. $v_result = 1;
  890. if ($this->_zip_fd != 0) {
  891. @fclose($this->_zip_fd);
  892. }
  893. $this->_zip_fd = 0;
  894. return $v_result;
  895. }
  896. // ---------------------------------------------------------------------------
  897. // ---------------------------------------------------------------------------
  898. // Function : _addList()
  899. // Description :
  900. // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
  901. // different from the real path of the file. This is usefull if you want to have PclTar
  902. // running in any directory, and memorize relative path from an other directory.
  903. // Parameters :
  904. // $p_list : An array containing the file or directory names to add in the tar
  905. // $p_result_list : list of added files with their properties (specially the status field)
  906. // $p_add_dir : Path to add in the filename path archived
  907. // $p_remove_dir : Path to remove in the filename path archived
  908. // Return Values :
  909. // ---------------------------------------------------------------------------
  910. /**
  911. * Archive_Zip::_addList()
  912. *
  913. * { Description }
  914. *
  915. * @return int
  916. */
  917. function _addList($p_list, &$p_result_list,
  918. $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_params)
  919. {
  920. $v_result = 1;
  921. // ----- Add the files
  922. $v_header_list = array();
  923. if (($v_result = $this->_addFileList($p_list, $v_header_list,
  924. $p_add_dir, $p_remove_dir,
  925. $p_remove_all_dir, $p_params)) != 1) {
  926. return $v_result;
  927. }
  928. // ----- Store the offset of the central dir
  929. $v_offset = @ftell($this->_zip_fd);
  930. // ----- Create the Central Dir files header
  931. for ($i = 0, $v_count = 0; $i<sizeof($v_header_list); $i++) {
  932. // ----- Create the file header
  933. if ($v_header_list[$i]['status'] == 'ok') {
  934. if (($v_result = $this->_writeCentralFileHeader($v_header_list[$i])) != 1) {
  935. return $v_result;
  936. }
  937. $v_count++;
  938. }
  939. // ----- Transform the header to a 'usable' info
  940. $this->_convertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  941. }
  942. // ----- Zip file comment
  943. $v_comment = '';
  944. // ----- Calculate the size of the central header
  945. $v_size = @ftell($this->_zip_fd)-$v_offset;
  946. // ----- Create the central dir footer
  947. if (($v_result = $this->_writeCentralHeader($v_count, $v_size, $v_offset,
  948. $v_comment)) != 1) {
  949. // ----- Reset the file list
  950. unset($v_header_list);
  951. return $v_result;
  952. }
  953. return $v_result;
  954. }
  955. // ---------------------------------------------------------------------------
  956. // ---------------------------------------------------------------------------
  957. // Function : _addFileList()
  958. // Description :
  959. // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
  960. // different from the real path of the file. This is usefull if you want to
  961. // run the lib in any directory, and memorize relative path from an other directory.
  962. // Parameters :
  963. // $p_list : An array containing the file or directory names to add in the tar
  964. // $p_result_list : list of added files with their properties (specially the status field)
  965. // $p_add_dir : Path to add in the filename path archived
  966. // $p_remove_dir : Path to remove in the filename path archived
  967. // Return Values :
  968. // ---------------------------------------------------------------------------
  969. /**
  970. * Archive_Zip::_addFileList()
  971. *
  972. * { Description }
  973. *
  974. * @return int
  975. */
  976. function _addFileList($p_list, &$p_result_list,
  977. $p_add_dir, $p_remove_dir, $p_remove_all_dir,
  978. &$p_params)
  979. {
  980. $v_result = 1;
  981. $v_header = array();
  982. // ----- Recuperate the current number of elt in list
  983. $v_nb = sizeof($p_result_list);
  984. // ----- Loop on the files
  985. for ($j = 0; ($j<count($p_list)) && ($v_result == 1); $j++) {
  986. // ----- Recuperate the filename
  987. $p_filename = $this->_tool_TranslateWinPath($p_list[$j], false);
  988. // ----- Skip empty file names
  989. if ($p_filename == "") {
  990. continue;
  991. }
  992. // ----- Check the filename
  993. if (!file_exists($p_filename)) {
  994. $this->_errorLog(ARCHIVE_ZIP_ERR_MISSING_FILE,
  995. "File '$p_filename' does not exists");
  996. return Archive_Zip::errorCode();
  997. }
  998. // ----- Look if it is a file or a dir with no all pathnre move
  999. if ((is_file($p_filename)) || ((is_dir($p_filename)) && !$p_remove_all_dir)) {
  1000. // ----- Add the file
  1001. if (($v_result = $this->_addFile($p_filename, $v_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_params)) != 1) {
  1002. return $v_result;
  1003. }
  1004. // ----- Store the file infos
  1005. $p_result_list[$v_nb++] = $v_header;
  1006. }
  1007. // ----- Look for directory
  1008. if (is_dir($p_filename)) {
  1009. // ----- Look for path
  1010. if ($p_filename != ".") {
  1011. $v_path = $p_filename."/";
  1012. } else {
  1013. $v_path = "";
  1014. }
  1015. // ----- Read the directory for files and sub-directories
  1016. $p_hdir = opendir($p_filename);
  1017. $p_hitem = readdir($p_hdir); // '.' directory
  1018. $p_hitem = readdir($p_hdir); // '..' directory
  1019. while ($p_hitem = readdir($p_hdir)) {
  1020. // ----- Look for a file
  1021. if (is_file($v_path.$p_hitem)) {
  1022. // ----- Add the file
  1023. if (($v_result = $this->_addFile($v_path.$p_hitem, $v_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_params)) != 1) {
  1024. return $v_result;
  1025. }
  1026. // ----- Store the file infos
  1027. $p_result_list[$v_nb++] = $v_header;
  1028. } else {
  1029. // ----- Recursive call to _addFileList()
  1030. // ----- Need an array as parameter
  1031. $p_temp_list[0] = $v_path.$p_hitem;
  1032. $v_result = $this->_addFileList($p_temp_list, $p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_params);
  1033. // ----- Update the number of elements of the list
  1034. $v_nb = sizeof($p_result_list);
  1035. }
  1036. }
  1037. // ----- Free memory for the recursive loop
  1038. unset($p_temp_list);
  1039. unset($p_hdir);
  1040. unset($p_hitem);
  1041. }
  1042. }
  1043. return $v_result;
  1044. }
  1045. // ---------------------------------------------------------------------------
  1046. // ---------------------------------------------------------------------------
  1047. // Function : _addFile()
  1048. // Description :
  1049. // Parameters :
  1050. // Return Values :
  1051. // ---------------------------------------------------------------------------
  1052. /**
  1053. * Archive_Zip::_addFile()
  1054. *
  1055. * { Description }
  1056. *
  1057. * @return int
  1058. */
  1059. function _addFile($p_filename, &$p_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_params)
  1060. {
  1061. $v_result = 1;
  1062. if ($p_filename == "") {
  1063. // ----- Error log
  1064. $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
  1065. return Archive_Zip::errorCode();
  1066. }
  1067. // ----- Calculate the stored filename
  1068. $v_stored_filename = $p_filename;
  1069. // ----- Look for all path to remove
  1070. if ($p_remove_all_dir) {
  1071. $v_stored_filename = basename($p_filename);
  1072. } else if ($p_remove_dir != "") {
  1073. if (substr($p_remove_dir, -1) != '/') {
  1074. $p_remove_dir .= "/";
  1075. }
  1076. if ((substr($p_filename, 0, 2) == "./") || (substr($p_remove_dir, 0, 2) == "./")) {
  1077. if ((substr($p_filename, 0, 2) == "./") && (substr($p_remove_dir, 0, 2) != "./")) {
  1078. $p_remove_dir = "./".$p_remove_dir;
  1079. }
  1080. if ((substr($p_filename, 0, 2) != "./") && (substr($p_remove_dir, 0, 2) == "./")) {
  1081. $p_remove_dir = substr($p_remove_dir, 2);
  1082. }
  1083. }
  1084. $v_compare = $this->_tool_PathInclusion($p_remove_dir, $p_filename);
  1085. if ($v_compare > 0) {
  1086. if ($v_compare == 2) {
  1087. $v_stored_filename = "";
  1088. } else {
  1089. $v_stored_filename = substr($p_filename, strlen($p_remove_dir));
  1090. }
  1091. }
  1092. }
  1093. // ----- Look for path to add
  1094. if ($p_add_dir != "") {
  1095. if (substr($p_add_dir, -1) == "/") {
  1096. $v_stored_filename = $p_add_dir.$v_stored_filename;
  1097. } else {
  1098. $v_stored_filename = $p_add_dir."/".$v_stored_filename;
  1099. }
  1100. }
  1101. // ----- Filename (reduce the path of stored name)
  1102. $v_stored_filename = $this->_tool_PathReduction($v_stored_filename);
  1103. /* filename length moved after call-back in release 1.3
  1104. // ----- Check the path length
  1105. if (strlen($v_stored_filename) > 0xFF) {
  1106. // ----- Error log
  1107. $this->_errorLog(-5, "Stored file name is too long (max. 255) : '$v_stored_filename'");
  1108. return Archive_Zip::errorCode();
  1109. }
  1110. */
  1111. // ----- Set the file properties
  1112. clearstatcache();
  1113. $p_header['comment'] = '';
  1114. $p_header['comment_len'] = 0;
  1115. $p_header['compressed_size'] = 0;
  1116. $p_header['compression'] = 0;
  1117. $p_header['crc'] = 0;
  1118. $p_header['disk'] = 0;
  1119. $p_header['external'] = (is_file($p_filename)?0xFE49FFE0:0x41FF0010);
  1120. $p_header['extra'] = '';
  1121. $p_header['extra_len'] = 0;
  1122. $p_header['filename'] = $p_filename;
  1123. $p_header['filename_len'] = strlen($p_filename);
  1124. $p_header['flag'] = 0;
  1125. $p_header['index'] = -1;
  1126. $p_header['internal'] = 0;
  1127. $p_header['mtime'] = filemtime($p_filename);
  1128. $p_header['offset'] = 0;
  1129. $p_header['size'] = filesize($p_filename);
  1130. $p_header['status'] = 'ok';
  1131. $p_header['stored_filename'] = $v_stored_filename;
  1132. $p_header['version'] = 20;
  1133. $p_header['version_extracted'] = 10;
  1134. // ----- Look for pre-add callback
  1135. if ((isset($p_params[ARCHIVE_ZIP_PARAM_PRE_ADD]))
  1136. && ($p_params[ARCHIVE_ZIP_PARAM_PRE_ADD] != '')) {
  1137. // ----- Generate a local information
  1138. $v_local_header = array();
  1139. $this->_convertHeader2FileInfo($p_header, $v_local_header);
  1140. // ----- Call the callback
  1141. // Here I do not use call_user_func() because I need to send a reference to the
  1142. // header.
  1143. eval('$v_result = '.$p_params[ARCHIVE_ZIP_PARAM_PRE_ADD].'(ARCHIVE_ZIP_PARAM_PRE_ADD, $v_local_header);');
  1144. if ($v_result == 0) {
  1145. // ----- Change the file status
  1146. $p_header['status'] = "skipped";
  1147. $v_result = 1;
  1148. }
  1149. // ----- Update the informations
  1150. // Only some fields can be modified
  1151. if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
  1152. $p_header['stored_filename'] = $this->_tool_PathReduction($v_local_header['stored_filename']);
  1153. }
  1154. }
  1155. // ----- Look for empty stored filename
  1156. if ($p_header['stored_filename'] == "") {
  1157. $p_header['status'] = "filtered";
  1158. }
  1159. // ----- Check the path length
  1160. if (strlen($p_header['stored_filename']) > 0xFF) {
  1161. $p_header['status'] = 'filename_too_long';
  1162. }
  1163. // ----- Look if no error, or file not skipped
  1164. if ($p_header['status'] == 'ok') {
  1165. // ----- Look for a file
  1166. if (is_file($p_filename)) {
  1167. // ----- Open the source fileā€¦

Large files files are truncated, but you can click here to view the full file