PageRenderTime 74ms CodeModel.GetById 33ms RepoModel.GetById 0ms 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
  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
  1168. if (($v_file = @fopen($p_filename, "rb")) == 0) {
  1169. $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
  1170. return Archive_Zip::errorCode();
  1171. }
  1172. if ($p_params['no_compression']) {
  1173. // ----- Read the file content
  1174. $v_content_compressed = @fread($v_file, $p_header['size']);
  1175. // ----- Calculate the CRC
  1176. $p_header['crc'] = crc32($v_content_compressed);
  1177. } else {
  1178. // ----- Read the file content
  1179. $v_content = @fread($v_file, $p_header['size']);
  1180. // ----- Calculate the CRC
  1181. $p_header['crc'] = crc32($v_content);
  1182. // ----- Compress the file
  1183. $v_content_compressed = gzdeflate($v_content);
  1184. }
  1185. // ----- Set header parameters
  1186. $p_header['compressed_size'] = strlen($v_content_compressed);
  1187. $p_header['compression'] = 8;
  1188. // ----- Call the header generation
  1189. if (($v_result = $this->_writeFileHeader($p_header)) != 1) {
  1190. @fclose($v_file);
  1191. return $v_result;
  1192. }
  1193. // ----- Write the compressed content
  1194. $v_binary_data = pack('a'.$p_header['compressed_size'], $v_content_compressed);
  1195. @fwrite($this->_zip_fd, $v_binary_data, $p_header['compressed_size']);
  1196. // ----- Close the file
  1197. @fclose($v_file);
  1198. } else {
  1199. // ----- Look for a directory
  1200. // ----- Set the file properties
  1201. $p_header['filename'] .= '/';
  1202. $p_header['filename_len']++;
  1203. $p_header['size'] = 0;
  1204. $p_header['external'] = 0x41FF0010; // Value for a folder : to be checked
  1205. // ----- Call the header generation
  1206. if (($v_result = $this->_writeFileHeader($p_header)) != 1) {
  1207. return $v_result;
  1208. }
  1209. }
  1210. }
  1211. // ----- Look for pre-add callback
  1212. if ((isset($p_params[ARCHIVE_ZIP_PARAM_POST_ADD]))
  1213. && ($p_params[ARCHIVE_ZIP_PARAM_POST_ADD] != '')) {
  1214. // ----- Generate a local information
  1215. $v_local_header = array();
  1216. $this->_convertHeader2FileInfo($p_header, $v_local_header);
  1217. // ----- Call the callback
  1218. // Here I do not use call_user_func() because I need to send a reference to the
  1219. // header.
  1220. eval('$v_result = '.$p_params[ARCHIVE_ZIP_PARAM_POST_ADD].'(ARCHIVE_ZIP_PARAM_POST_ADD, $v_local_header);');
  1221. if ($v_result == 0) {
  1222. // ----- Ignored
  1223. $v_result = 1;
  1224. }
  1225. // ----- Update the informations
  1226. // Nothing can be modified
  1227. }
  1228. return $v_result;
  1229. }
  1230. // ---------------------------------------------------------------------------
  1231. // ---------------------------------------------------------------------------
  1232. // Function : _writeFileHeader()
  1233. // Description :
  1234. // Parameters :
  1235. // Return Values :
  1236. // ---------------------------------------------------------------------------
  1237. /**
  1238. * Archive_Zip::_writeFileHeader()
  1239. *
  1240. * { Description }
  1241. *
  1242. * @return int
  1243. */
  1244. function _writeFileHeader(&$p_header)
  1245. {
  1246. $v_result = 1;
  1247. // TBC
  1248. //for(reset($p_header); $key = key($p_header); next($p_header)) {
  1249. //}
  1250. // ----- Store the offset position of the file
  1251. $p_header['offset'] = ftell($this->_zip_fd);
  1252. // ----- Transform UNIX mtime to DOS format mdate/mtime
  1253. $v_date = getdate($p_header['mtime']);
  1254. $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
  1255. $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
  1256. // ----- Packed data
  1257. $v_binary_data = pack("VvvvvvVVVvv", 0x04034b50, $p_header['version'], $p_header['flag'],
  1258. $p_header['compression'], $v_mtime, $v_mdate,
  1259. $p_header['crc'], $p_header['compressed_size'], $p_header['size'],
  1260. strlen($p_header['stored_filename']), $p_header['extra_len']);
  1261. // ----- Write the first 148 bytes of the header in the archive
  1262. fputs($this->_zip_fd, $v_binary_data, 30);
  1263. // ----- Write the variable fields
  1264. if (strlen($p_header['stored_filename']) != 0) {
  1265. fputs($this->_zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
  1266. }
  1267. if ($p_header['extra_len'] != 0) {
  1268. fputs($this->_zip_fd, $p_header['extra'], $p_header['extra_len']);
  1269. }
  1270. return $v_result;
  1271. }
  1272. // ---------------------------------------------------------------------------
  1273. // ---------------------------------------------------------------------------
  1274. // Function : _writeCentralFileHeader()
  1275. // Description :
  1276. // Parameters :
  1277. // Return Values :
  1278. // ---------------------------------------------------------------------------
  1279. /**
  1280. * Archive_Zip::_writeCentralFileHeader()
  1281. *
  1282. * { Description }
  1283. *
  1284. * @return int
  1285. */
  1286. function _writeCentralFileHeader(&$p_header)
  1287. {
  1288. $v_result = 1;
  1289. // TBC
  1290. //for(reset($p_header); $key = key($p_header); next($p_header)) {
  1291. //}
  1292. // ----- Transform UNIX mtime to DOS format mdate/mtime
  1293. $v_date = getdate($p_header['mtime']);
  1294. $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
  1295. $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
  1296. // ----- Packed data
  1297. $v_binary_data = pack("VvvvvvvVVVvvvvvVV", 0x02014b50, $p_header['version'], $p_header['version_extracted'],
  1298. $p_header['flag'], $p_header['compression'], $v_mtime, $v_mdate, $p_header['crc'],
  1299. $p_header['compressed_size'], $p_header['size'],
  1300. strlen($p_header['stored_filename']), $p_header['extra_len'], $p_header['comment_len'],
  1301. $p_header['disk'], $p_header['internal'], $p_header['external'], $p_header['offset']);
  1302. // ----- Write the 42 bytes of the header in the zip file
  1303. fputs($this->_zip_fd, $v_binary_data, 46);
  1304. // ----- Write the variable fields
  1305. if (strlen($p_header['stored_filename']) != 0) {
  1306. fputs($this->_zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
  1307. }
  1308. if ($p_header['extra_len'] != 0) {
  1309. fputs($this->_zip_fd, $p_header['extra'], $p_header['extra_len']);
  1310. }
  1311. if ($p_header['comment_len'] != 0) {
  1312. fputs($this->_zip_fd, $p_header['comment'], $p_header['comment_len']);
  1313. }
  1314. return $v_result;
  1315. }
  1316. // ---------------------------------------------------------------------------
  1317. // ---------------------------------------------------------------------------
  1318. // Function : _writeCentralHeader()
  1319. // Description :
  1320. // Parameters :
  1321. // Return Values :
  1322. // ---------------------------------------------------------------------------
  1323. /**
  1324. * Archive_Zip::_writeCentralHeader()
  1325. *
  1326. * { Description }
  1327. *
  1328. * @return int
  1329. */
  1330. function _writeCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
  1331. {
  1332. $v_result = 1;
  1333. // ----- Packed data
  1334. $v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries, $p_nb_entries, $p_size, $p_offset, strlen($p_comment));
  1335. // ----- Write the 22 bytes of the header in the zip file
  1336. fputs($this->_zip_fd, $v_binary_data, 22);
  1337. // ----- Write the variable fields
  1338. if (strlen($p_comment) != 0) {
  1339. fputs($this->_zip_fd, $p_comment, strlen($p_comment));
  1340. }
  1341. return $v_result;
  1342. }
  1343. // ---------------------------------------------------------------------------
  1344. // ---------------------------------------------------------------------------
  1345. // Function : _list()
  1346. // Description :
  1347. // Parameters :
  1348. // Return Values :
  1349. // ---------------------------------------------------------------------------
  1350. /**
  1351. * Archive_Zip::_list()
  1352. *
  1353. * { Description }
  1354. *
  1355. * @return int
  1356. */
  1357. function _list(&$p_list)
  1358. {
  1359. $v_result = 1;
  1360. // ----- Open the zip file
  1361. if (($this->_zip_fd = @fopen($this->_zipname, 'rb')) == 0) {
  1362. // ----- Error log
  1363. $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->_zipname.'\' in binary read mode');
  1364. return Archive_Zip::errorCode();
  1365. }
  1366. // ----- Read the central directory informations
  1367. $v_central_dir = array();
  1368. if (($v_result = $this->_readEndCentralDir($v_central_dir)) != 1) {
  1369. return $v_result;
  1370. }
  1371. // ----- Go to beginning of Central Dir
  1372. @rewind($this->_zip_fd);
  1373. if (@fseek($this->_zip_fd, $v_central_dir['offset'])) {
  1374. // ----- Error log
  1375. $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  1376. return Archive_Zip::errorCode();
  1377. }
  1378. // ----- Read each entry
  1379. for ($i = 0; $i<$v_central_dir['entries']; $i++) {
  1380. // ----- Read the file header
  1381. if (($v_result = $this->_readCentralFileHeader($v_header)) != 1) {
  1382. return $v_result;
  1383. }
  1384. $v_header['index'] = $i;
  1385. // ----- Get the only interesting attributes
  1386. $this->_convertHeader2FileInfo($v_header, $p_list[$i]);
  1387. unset($v_header);
  1388. }
  1389. // ----- Close the zip file
  1390. $this->_closeFd();
  1391. return $v_result;
  1392. }
  1393. // ---------------------------------------------------------------------------
  1394. // ---------------------------------------------------------------------------
  1395. // Function : _convertHeader2FileInfo()
  1396. // Description :
  1397. // This function takes the file informations from the central directory
  1398. // entries and extract the interesting parameters that will be given back.
  1399. // The resulting file infos are set in the array $p_info
  1400. // $p_info['filename'] : Filename with full path. Given by user (add),
  1401. // extracted in the filesystem (extract).
  1402. // $p_info['stored_filename'] : Stored filename in the archive.
  1403. // $p_info['size'] = Size of the file.
  1404. // $p_info['compressed_size'] = Compressed size of the file.
  1405. // $p_info['mtime'] = Last modification date of the file.
  1406. // $p_info['comment'] = Comment associated with the file.
  1407. // $p_info['folder'] = true/false : indicates if the entry is a folder or not.
  1408. // $p_info['status'] = status of the action on the file.
  1409. // Parameters :
  1410. // Return Values :
  1411. // ---------------------------------------------------------------------------
  1412. /**
  1413. * Archive_Zip::_convertHeader2FileInfo()
  1414. *
  1415. * { Description }
  1416. *
  1417. * @return int
  1418. */
  1419. function _convertHeader2FileInfo($p_header, &$p_info)
  1420. {
  1421. $v_result = 1;
  1422. // ----- Get the interesting attributes
  1423. $p_info['filename'] = $p_header['filename'];
  1424. $p_info['stored_filename'] = $p_header['stored_filename'];
  1425. $p_info['size'] = $p_header['size'];
  1426. $p_info['compressed_size'] = $p_header['compressed_size'];
  1427. $p_info['mtime'] = $p_header['mtime'];
  1428. $p_info['comment'] = $p_header['comment'];
  1429. $p_info['folder'] = (($p_header['external']&0x00000010)==0x00000010);
  1430. $p_info['index'] = $p_header['index'];
  1431. $p_info['status'] = $p_header['status'];
  1432. return $v_result;
  1433. }
  1434. // ---------------------------------------------------------------------------
  1435. // ---------------------------------------------------------------------------
  1436. // Function : _extractByRule()
  1437. // Description :
  1438. // Extract a file or directory depending of rules (by index, by name, ...)
  1439. // Parameters :
  1440. // $p_file_list : An array where will be placed the properties of each
  1441. // extracted file
  1442. // $p_path : Path to add while writing the extracted files
  1443. // $p_remove_path : Path to remove (from the file memorized path) while writing the
  1444. // extracted files. If the path does not match the file path,
  1445. // the file is extracted with its memorized path.
  1446. // $p_remove_path does not apply to 'list' mode.
  1447. // $p_path and $p_remove_path are commulative.
  1448. // Return Values :
  1449. // 1 on success,0 or less on error (see error code list)
  1450. // ---------------------------------------------------------------------------
  1451. /**
  1452. * Archive_Zip::_extractByRule()
  1453. *
  1454. * { Description }
  1455. *
  1456. * @return int
  1457. */
  1458. function _extractByRule(&$p_file_list, &$p_params)
  1459. {
  1460. $v_result = 1;
  1461. $p_path = $p_params['add_path'];
  1462. $p_remove_path = $p_params['remove_path'];
  1463. $p_remove_all_path = $p_params['remove_all_path'];
  1464. // ----- Check the path
  1465. if (($p_path == "")
  1466. || ((substr($p_path, 0, 1) != "/")
  1467. && (substr($p_path, 0, 3) != "../") && (substr($p_path,1,2)!=":/"))) {
  1468. $p_path = "./".$p_path;
  1469. }
  1470. // ----- Reduce the path last (and duplicated) '/'
  1471. if (($p_path != "./") && ($p_path != "/")) {
  1472. // ----- Look for the path end '/'
  1473. while (substr($p_path, -1) == "/") {
  1474. $p_path = substr($p_path, 0, strlen($p_path)-1);
  1475. }
  1476. }
  1477. // ----- Look for path to remove format (should end by /)
  1478. if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/')) {
  1479. $p_remove_path .= '/';
  1480. }
  1481. $p_remove_path_size = strlen($p_remove_path);
  1482. // ----- Open the zip file
  1483. if (($v_result = $this->_openFd('rb')) != 1) {
  1484. return $v_result;
  1485. }
  1486. // ----- Read the central directory informations
  1487. $v_central_dir = array();
  1488. if (($v_result = $this->_readEndCentralDir($v_central_dir)) != 1) {
  1489. // ----- Close the zip file
  1490. $this->_closeFd();
  1491. return $v_result;
  1492. }
  1493. // ----- Start at beginning of Central Dir
  1494. $v_pos_entry = $v_central_dir['offset'];
  1495. // ----- Read each entry
  1496. $j_start = 0;
  1497. for ($i = 0, $v_nb_extracted = 0; $i<$v_central_dir['entries']; $i++) {
  1498. // ----- Read next Central dir entry
  1499. @rewind($this->_zip_fd);
  1500. if (@fseek($this->_zip_fd, $v_pos_entry)) {
  1501. $this->_closeFd();
  1502. $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP,
  1503. 'Invalid archive size');
  1504. return Archive_Zip::errorCode();
  1505. }
  1506. // ----- Read the file header
  1507. $v_header = array();
  1508. if (($v_result = $this->_readCentralFileHeader($v_header)) != 1) {
  1509. $this->_closeFd();
  1510. return $v_result;
  1511. }
  1512. // ----- Store the index
  1513. $v_header['index'] = $i;
  1514. // ----- Store the file position
  1515. $v_pos_entry = ftell($this->_zip_fd);
  1516. // ----- Look for the specific extract rules
  1517. $v_extract = false;
  1518. // ----- Look for extract by name rule
  1519. if ((isset($p_params[ARCHIVE_ZIP_PARAM_BY_NAME]))
  1520. && ($p_params[ARCHIVE_ZIP_PARAM_BY_NAME] != 0)) {
  1521. // ----- Look if the filename is in the list
  1522. for ($j = 0; ($j<sizeof($p_params[ARCHIVE_ZIP_PARAM_BY_NAME])) && (!$v_extract); $j++) {
  1523. // ----- Look for a directory
  1524. if (substr($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j], -1) == "/") {
  1525. // ----- Look if the directory is in the filename path
  1526. if ((strlen($v_header['stored_filename']) > strlen($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j]))
  1527. && (substr($v_header['stored_filename'], 0, strlen($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j])) == $p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j])) {
  1528. $v_extract = true;
  1529. }
  1530. } elseif ($v_header['stored_filename'] == $p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j]) {
  1531. $v_extract = true;
  1532. }
  1533. }
  1534. } else if ( (isset($p_params[ARCHIVE_ZIP_PARAM_BY_PREG]))
  1535. && ($p_params[ARCHIVE_ZIP_PARAM_BY_PREG] != "")) {
  1536. // ----- Look for extract by preg rule
  1537. if (preg_match($p_params[ARCHIVE_ZIP_PARAM_BY_PREG], $v_header['stored_filename'])) {
  1538. $v_extract = true;
  1539. }
  1540. } else if ((isset($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX]))
  1541. && ($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX] != 0)) {
  1542. // ----- Look for extract by index rule
  1543. // ----- Look if the index is in the list
  1544. for ($j = $j_start; ($j<sizeof($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX])) && (!$v_extract); $j++) {
  1545. if (($i>=$p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['start']) && ($i<=$p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['end'])) {
  1546. $v_extract = true;
  1547. }
  1548. if ($i>=$p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['end']) {
  1549. $j_start = $j+1;
  1550. }
  1551. if ($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['start']>$i) {
  1552. break;
  1553. }
  1554. }
  1555. } else {
  1556. // ----- Look for no rule, which means extract all the archive
  1557. $v_extract = true;
  1558. }
  1559. // ----- Look for real extraction
  1560. if ($v_extract) {
  1561. // ----- Go to the file position
  1562. @rewind($this->_zip_fd);
  1563. if (@fseek($this->_zip_fd, $v_header['offset'])) {
  1564. // ----- Close the zip file
  1565. $this->_closeFd();
  1566. // ----- Error log
  1567. $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  1568. return Archive_Zip::errorCode();
  1569. }
  1570. // ----- Look for extraction as string
  1571. if ($p_params[ARCHIVE_ZIP_PARAM_EXTRACT_AS_STRING]) {
  1572. // ----- Extracting the file
  1573. if (($v_result = $this->_extractFileAsString($v_header, $v_string)) != 1) {
  1574. // ----- Close the zip file
  1575. $this->_closeFd();
  1576. return $v_result;
  1577. }
  1578. // ----- Get the only interesting attributes
  1579. if (($v_result = $this->_convertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1) {
  1580. // ----- Close the zip file
  1581. $this->_closeFd();
  1582. return $v_result;
  1583. }
  1584. // ----- Set the file content
  1585. $p_file_list[$v_nb_extracted]['content'] = $v_string;
  1586. // ----- Next extracted file
  1587. $v_nb_extracted++;
  1588. } else {
  1589. // ----- Extracting the file
  1590. if (($v_result = $this->_extractFile($v_header, $p_path, $p_remove_path, $p_remove_all_path, $p_params)) != 1) {
  1591. // ----- Close the zip file
  1592. $this->_closeFd();
  1593. return $v_result;
  1594. }
  1595. // ----- Get the only interesting attributes
  1596. if (($v_result = $this->_convertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
  1597. // ----- Close the zip file
  1598. $this->_closeFd();
  1599. return $v_result;
  1600. }
  1601. }
  1602. }
  1603. }
  1604. // ----- Close the zip file
  1605. $this->_closeFd();
  1606. return $v_result;
  1607. }
  1608. // ---------------------------------------------------------------------------
  1609. // ---------------------------------------------------------------------------
  1610. // Function : _extractFile()
  1611. // Description :
  1612. // Parameters :
  1613. // Return Values :
  1614. // ---------------------------------------------------------------------------
  1615. /**
  1616. * Archive_Zip::_extractFile()
  1617. *
  1618. * { Description }
  1619. *
  1620. * @return int
  1621. */
  1622. function _extractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_params)
  1623. {
  1624. $v_result = 1;
  1625. // ----- Read the file header
  1626. if (($v_result = $this->_readFileHeader($v_header)) != 1) {
  1627. return $v_result;
  1628. }
  1629. // ----- Check that the file header is coherent with $p_entry info
  1630. // TBC
  1631. // ----- Look for all path to remove
  1632. if ($p_remove_all_path == true) {
  1633. // ----- Get the basename of the path
  1634. $p_entry['filename'] = basename($p_entry['filename']);
  1635. } else if ($p_remove_path != "") {
  1636. if ($this->_tool_PathInclusion($p_remove_path, $p_entry['filename']) == 2) {
  1637. // ----- Change the file status
  1638. $p_entry['status'] = "filtered";
  1639. return $v_result;
  1640. }
  1641. $p_remove_path_size = strlen($p_remove_path);
  1642. if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path) {
  1643. // ----- Remove the path
  1644. $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
  1645. }
  1646. }
  1647. // ----- Add the path
  1648. if ($p_path != '') {
  1649. $p_entry['filename'] = $p_path."/".$p_entry['filename'];
  1650. }
  1651. // ----- Look for pre-extract callback
  1652. if ((isset($p_params[ARCHIVE_ZIP_PARAM_PRE_EXTRACT]))
  1653. && ($p_params[ARCHIVE_ZIP_PARAM_PRE_EXTRACT] != '')) {
  1654. // ----- Generate a local information
  1655. $v_local_header = array();
  1656. $this->_convertHeader2FileInfo($p_entry, $v_local_header);
  1657. // ----- Call the callback
  1658. // Here I do not use call_user_func() because I need to send a reference to the
  1659. // header.
  1660. eval('$v_result = '.$p_params[ARCHIVE_ZIP_PARAM_PRE_EXTRACT].'(ARCHIVE_ZIP_PARAM_PRE_EXTRACT, $v_local_header);');
  1661. if ($v_result == 0) {
  1662. // ----- Change the file status
  1663. $p_entry['status'] = "skipped";
  1664. $v_result = 1;
  1665. }
  1666. // ----- Update the informations
  1667. // Only some fields can be modified
  1668. $p_entry['filename'] = $v_local_header['filename'];
  1669. }
  1670. // ----- Trace
  1671. // ----- Look if extraction should be done
  1672. if ($p_entry['status'] == 'ok') {
  1673. // ----- Look for specific actions while the file exist
  1674. if (file_exists($p_entry['filename'])) {
  1675. // ----- Look if file is a directory
  1676. if (is_dir($p_entry['filename'])) {
  1677. // ----- Change the file status
  1678. $p_entry['status'] = "already_a_directory";
  1679. //return $v_result;
  1680. } else if (!is_writeable($p_entry['filename'])) {
  1681. // ----- Look if file is write protected
  1682. // ----- Change the file status
  1683. $p_entry['status'] = "write_protected";
  1684. //return $v_result;
  1685. } else if (filemtime($p_entry['filename']) > $p_entry['mtime']) {
  1686. // ----- Look if the extracted file is older
  1687. // ----- Change the file status
  1688. $p_entry['status'] = "newer_exist";
  1689. //return $v_result;
  1690. }
  1691. } else {
  1692. // ----- Check the directory availability and create it if necessary
  1693. if ((($p_entry['external']&0x00000010)==0x00000010) || (substr($p_entry['filename'], -1) == '/')) {
  1694. $v_dir_to_check = $p_entry['filename'];
  1695. } else if (!strstr($p_entry['filename'], "/")) {
  1696. $v_dir_to_check = "";
  1697. } else {
  1698. $v_dir_to_check = dirname($p_entry['filename']);
  1699. }
  1700. if (($v_result = $this->_dirCheck($v_dir_to_check, (($p_entry['external']&0x00000010)==0x00000010))) != 1) {
  1701. // ----- Change the file status
  1702. $p_entry['status'] = "path_creation_fail";
  1703. //return $v_result;
  1704. $v_result = 1;
  1705. }
  1706. }
  1707. }
  1708. // ----- Look if extraction should be done
  1709. if ($p_entry['status'] == 'ok') {
  1710. // ----- Do the extraction (if not a folder)
  1711. if (!(($p_entry['external']&0x00000010)==0x00000010)) {
  1712. // ----- Look for not compressed file
  1713. if ($p_entry['compressed_size'] == $p_entry['size']) {
  1714. // ----- Opening destination file
  1715. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  1716. // ----- Change the file status
  1717. $p_entry['status'] = "write_error";
  1718. return $v_result;
  1719. }
  1720. // ----- Read the file by ARCHIVE_ZIP_READ_BLOCK_SIZE octets blocks
  1721. $v_size = $p_entry['compressed_size'];
  1722. while ($v_size != 0) {
  1723. $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  1724. $v_buffer = fread($this->_zip_fd, $v_read_size);
  1725. $v_binary_data = pack('a'.$v_read_size, $v_buffer);
  1726. @fwrite($v_dest_file, $v_binary_data, $v_read_size);
  1727. $v_size -= $v_read_size;
  1728. }
  1729. // ----- Closing the destination file
  1730. fclose($v_dest_file);
  1731. // ----- Change the file mtime
  1732. touch($p_entry['filename'], $p_entry['mtime']);
  1733. } else {
  1734. // ----- Trace
  1735. // ----- Opening destination file
  1736. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  1737. // ----- Change the file status
  1738. $p_entry['status'] = "write_error";
  1739. return $v_result;
  1740. }
  1741. // ----- Read the compressed file in a buffer (one shot)
  1742. $v_buffer = @fread($this->_zip_fd, $p_entry['compressed_size']);
  1743. // ----- Decompress the file
  1744. $v_file_content = gzinflate($v_buffer);
  1745. unset($v_buffer);
  1746. // ----- Write the uncompressed data
  1747. @fwrite($v_dest_file, $v_file_content, $p_entry['size']);
  1748. unset($v_file_content);
  1749. // ----- Closing the destination file
  1750. @fclose($v_dest_file);
  1751. // ----- Change the file mtime
  1752. touch($p_entry['filename'], $p_entry['mtime']);
  1753. }
  1754. // ----- Look for chmod option
  1755. if ((isset($p_params[ARCHIVE_ZIP_PARAM_SET_CHMOD]))
  1756. && ($p_params[ARCHIVE_ZIP_PARAM_SET_CHMOD] != 0)) {
  1757. // ----- Change the mode of the file
  1758. chmod($p_entry['filename'], $p_params[ARCHIVE_ZIP_PARAM_SET_CHMOD]);
  1759. }
  1760. }
  1761. }
  1762. // ----- Look for post-extract callback
  1763. if ((isset($p_params[ARCHIVE_ZIP_PARAM_POST_EXTRACT]))
  1764. && ($p_params[ARCHIVE_ZIP_PARAM_POST_EXTRACT] != '')) {
  1765. // ----- Generate a local information
  1766. $v_local_header = array();
  1767. $this->_convertHeader2FileInfo($p_entry, $v_local_header);
  1768. // ----- Call the callback
  1769. // Here I do not use call_user_func() because I need to send a reference to the
  1770. // header.
  1771. eval('$v_result = '.$p_params[ARCHIVE_ZIP_PARAM_POST_EXTRACT].'(ARCHIVE_ZIP_PARAM_POST_EXTRACT, $v_local_header);');
  1772. }
  1773. return $v_result;
  1774. }
  1775. // ---------------------------------------------------------------------------
  1776. // ---------------------------------------------------------------------------
  1777. // Function : _extractFileAsString()
  1778. // Description :
  1779. // Parameters :
  1780. // Return Values :
  1781. // ---------------------------------------------------------------------------
  1782. /**
  1783. * Archive_Zip::_extractFileAsString()
  1784. *
  1785. * { Description }
  1786. *
  1787. * @return int
  1788. */
  1789. function _extractFileAsString(&$p_entry, &$p_string)
  1790. {
  1791. $v_result = 1;
  1792. // ----- Read the file header
  1793. $v_header = array();
  1794. if (($v_result = $this->_readFileHeader($v_header)) != 1) {
  1795. return $v_result;
  1796. }
  1797. // ----- Check that the file header is coherent with $p_entry info
  1798. // TBC
  1799. // ----- Trace
  1800. // ----- Do the extraction (if not a folder)
  1801. if (!(($p_entry['external']&0x00000010)==0x00000010)) {
  1802. // ----- Look for not compressed file
  1803. if ($p_entry['compressed_size'] == $p_entry['size']) {
  1804. // ----- Trace
  1805. // ----- Reading the file
  1806. $p_string = fread($this->_zip_fd, $p_entry['compressed_size']);
  1807. } else {
  1808. // ----- Trace
  1809. // ----- Reading the file
  1810. $v_data = fread($this->_zip_fd, $p_entry['compressed_size']);
  1811. // ----- Decompress the file
  1812. $p_string = gzinflate($v_data);
  1813. }
  1814. // ----- Trace
  1815. } else {
  1816. // TBC : error : can not extract a folder in a string
  1817. }
  1818. return $v_result;
  1819. }
  1820. // ---------------------------------------------------------------------------
  1821. // ---------------------------------------------------------------------------
  1822. // Function : _readFileHeader()
  1823. // Description :
  1824. // Parameters :
  1825. // Return Values :
  1826. // ---------------------------------------------------------------------------
  1827. /**
  1828. * Archive_Zip::_readFileHeader()
  1829. *
  1830. * { Description }
  1831. *
  1832. * @return int
  1833. */
  1834. function _readFileHeader(&$p_header)
  1835. {
  1836. $v_result = 1;
  1837. // ----- Read the 4 bytes signature
  1838. $v_binary_data = @fread($this->_zip_fd, 4);
  1839. $v_data = unpack('Vid', $v_binary_data);
  1840. // ----- Check signature
  1841. if ($v_data['id'] != 0x04034b50) {
  1842. // ----- Error log
  1843. $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  1844. return Archive_Zip::errorCode();
  1845. }
  1846. // ----- Read the first 42 bytes of the header
  1847. $v_binary_data = fread($this->_zip_fd, 26);
  1848. // ----- Look for invalid block size
  1849. if (strlen($v_binary_data) != 26) {
  1850. $p_header['filename'] = "";
  1851. $p_header['status'] = "invalid_header";
  1852. // ----- Error log
  1853. $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
  1854. return Archive_Zip::errorCode();
  1855. }
  1856. // ----- Extract the values
  1857. $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
  1858. // ----- Get filename
  1859. $p_header['filename'] = fread($this->_zip_fd, $v_data['filename_len']);
  1860. // ----- Get extra_fields
  1861. if ($v_data['extra_len'] != 0) {
  1862. $p_header['extra'] = fread($this->_zip_fd, $v_data['extra_len']);
  1863. } else {
  1864. $p_header['extra'] = '';
  1865. }
  1866. // ----- Extract properties
  1867. $p_header['compression'] = $v_data['compression'];
  1868. $p_header['size'] = $v_data['size'];
  1869. $p_header['compressed_size'] = $v_data['compressed_size'];
  1870. $p_header['crc'] = $v_data['crc'];
  1871. $p_header['flag'] = $v_data['flag'];
  1872. // ----- Recuperate date in UNIX format
  1873. $p_header['mdate'] = $v_data['mdate'];
  1874. $p_header['mtime'] = $v_data['mtime'];
  1875. if ($p_header['mdate'] && $p_header['mtime']) {
  1876. // ----- Extract time
  1877. $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
  1878. $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
  1879. $v_seconde = ($p_header['mtime'] & 0x001F)*2;
  1880. // ----- Extract date
  1881. $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
  1882. $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
  1883. $v_day = $p_header['mdate'] & 0x001F;
  1884. // ----- Get UNIX date format
  1885. $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  1886. } else {
  1887. $p_header['mtime'] = time();
  1888. }
  1889. // ----- Other informations
  1890. // TBC
  1891. //for(reset($v_data); $key = key($v_data); next($v_data)) {
  1892. //}
  1893. // ----- Set the stored filename
  1894. $p_header['stored_filename'] = $p_header['filename'];
  1895. // ----- Set the status field
  1896. $p_header['status'] = "ok";
  1897. return $v_result;
  1898. }
  1899. // ---------------------------------------------------------------------------
  1900. // ---------------------------------------------------------------------------
  1901. // Function : _readCentralFileHeader()
  1902. // Description :
  1903. // Parameters :
  1904. // Return Values :
  1905. // ---------------------------------------------------------------------------
  1906. /**
  1907. * Archive_Zip::_readCentralFileHeader()
  1908. *
  1909. * { Description }
  1910. *
  1911. * @return int
  1912. */
  1913. function _readCentralFileHeader(&$p_header)
  1914. {
  1915. $v_result = 1;
  1916. // ----- Read the 4 bytes signature
  1917. $v_binary_data = @fread($this->_zip_fd, 4);
  1918. $v_data = unpack('Vid', $v_binary_data);
  1919. // ----- Check signature
  1920. if ($v_data['id'] != 0x02014b50) {
  1921. // ----- Error log
  1922. $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  1923. return Archive_Zip::errorCode();
  1924. }
  1925. // ----- Read the first 42 bytes of the header
  1926. $v_binary_data = fread($this->_zip_fd, 42);
  1927. // ----- Look for invalid block size
  1928. if (strlen($v_binary_data) != 42) {
  1929. $p_header['filename'] = "";
  1930. $p_header['status'] = "invalid_header";
  1931. // ----- Error log
  1932. $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
  1933. return Archive_Zip::errorCode();
  1934. }
  1935. // ----- Extract the values
  1936. $p_header = unpack('vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', $v_binary_data);
  1937. // ----- Get filename
  1938. if ($p_header['filename_len'] != 0) {
  1939. $p_header['filename'] = fread($this->_zip_fd, $p_header['filename_len']);
  1940. } else {
  1941. $p_header['filename'] = '';
  1942. }
  1943. // ----- Get extra
  1944. if ($p_header['extra_len'] != 0) {
  1945. $p_header['extra'] = fread($this->_zip_fd, $p_header['extra_len']);
  1946. } else {
  1947. $p_header['extra'] = '';
  1948. }
  1949. // ----- Get comment
  1950. if ($p_header['comment_len'] != 0) {
  1951. $p_header['comment'] = fread($this->_zip_fd, $p_header['comment_len']);
  1952. } else {
  1953. $p_header['comment'] = '';
  1954. }
  1955. // ----- Extract properties
  1956. // ----- Recuperate date in UNIX format
  1957. if ($p_header['mdate'] && $p_header['mtime']) {
  1958. // ----- Extract time
  1959. $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
  1960. $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
  1961. $v_seconde = ($p_header['mtime'] & 0x001F)*2;
  1962. // ----- Extract date
  1963. $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
  1964. $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
  1965. $v_day = $p_header['mdate'] & 0x001F;
  1966. // ----- Get UNIX date format
  1967. $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  1968. } else {
  1969. $p_header['mtime'] = time();
  1970. }
  1971. // ----- Set the stored filename
  1972. $p_header['stored_filename'] = $p_header['filename'];
  1973. // ----- Set default status to ok
  1974. $p_header['status'] = 'ok';
  1975. // ----- Look if it is a directory
  1976. if (substr($p_header['filename'], -1) == '/') {
  1977. $p_header['external'] = 0x41FF0010;
  1978. }
  1979. return $v_result;
  1980. }
  1981. // ---------------------------------------------------------------------------
  1982. // ---------------------------------------------------------------------------
  1983. // Function : _readEndCentralDir()
  1984. // Description :
  1985. // Parameters :
  1986. // Return Values :
  1987. // ---------------------------------------------------------------------------
  1988. /**
  1989. * Archive_Zip::_readEndCentralDir()
  1990. *
  1991. * { Description }
  1992. *
  1993. * @return int
  1994. */
  1995. function _readEndCentralDir(&$p_central_dir)
  1996. {
  1997. $v_result = 1;
  1998. // ----- Go to the end of the zip file
  1999. $v_size = filesize($this->_zipname);
  2000. @fseek($this->_zip_fd, $v_size);
  2001. if (@ftell($this->_zip_fd) != $v_size) {
  2002. $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
  2003. 'Unable to go to the end of the archive \''
  2004. .$this->_zipname.'\'');
  2005. return Archive_Zip::errorCode();
  2006. }
  2007. // ----- First try : look if this is an archive with no commentaries
  2008. // (most of the time)
  2009. // in this case the end of central dir is at 22 bytes of the file end
  2010. $v_found = 0;
  2011. if ($v_size > 26) {
  2012. @fseek($this->_zip_fd, $v_size-22);
  2013. if (($v_pos = @ftell($this->_zip_fd)) != ($v_size-22)) {
  2014. $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
  2015. 'Unable to seek back to the middle of the archive \''
  2016. .$this->_zipname.'\'');
  2017. return Archive_Zip::errorCode();
  2018. }
  2019. // ----- Read for bytes
  2020. $v_binary_data = @fread($this->_zip_fd, 4);
  2021. $v_data = unpack('Vid', $v_binary_data);
  2022. // ----- Check signature
  2023. if ($v_data['id'] == 0x06054b50) {
  2024. $v_found = 1;
  2025. }
  2026. $v_pos = ftell($this->_zip_fd);
  2027. }
  2028. // ----- Go back to the maximum possible size of the Central Dir End Record
  2029. if (!$v_found) {
  2030. $v_maximum_size = 65557; // 0xFFFF + 22;
  2031. if ($v_maximum_size > $v_size) {
  2032. $v_maximum_size = $v_size;
  2033. }
  2034. @fseek($this->_zip_fd, $v_size-$v_maximum_size);
  2035. if (@ftell($this->_zip_fd) != ($v_size-$v_maximum_size)) {
  2036. $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
  2037. 'Unable to seek back to the middle of the archive \''
  2038. .$this->_zipname.'\'');
  2039. return Archive_Zip::errorCode();
  2040. }
  2041. // ----- Read byte per byte in order to find the signature
  2042. $v_pos = ftell($this->_zip_fd);
  2043. $v_bytes = 0x00000000;
  2044. while ($v_pos < $v_size) {
  2045. // ----- Read a byte
  2046. $v_byte = @fread($this->_zip_fd, 1);
  2047. // ----- Add the byte
  2048. $v_bytes = ($v_bytes << 8) | Ord($v_byte);
  2049. // ----- Compare the bytes
  2050. if ($v_bytes == 0x504b0506) {
  2051. $v_pos++;
  2052. break;
  2053. }
  2054. $v_pos++;
  2055. }
  2056. // ----- Look if not found end of central dir
  2057. if ($v_pos == $v_size) {
  2058. $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
  2059. "Unable to find End of Central Dir Record signature");
  2060. return Archive_Zip::errorCode();
  2061. }
  2062. }
  2063. // ----- Read the first 18 bytes of the header
  2064. $v_binary_data = fread($this->_zip_fd, 18);
  2065. // ----- Look for invalid block size
  2066. if (strlen($v_binary_data) != 18) {
  2067. $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
  2068. "Invalid End of Central Dir Record size : "
  2069. .strlen($v_binary_data));
  2070. return Archive_Zip::errorCode();
  2071. }
  2072. // ----- Extract the values
  2073. $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
  2074. // ----- Check the global size
  2075. if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
  2076. $this->_errorLog(ARCHIVE_ZIP_ERR_BAD_FORMAT,
  2077. "Fail to find the right signature");
  2078. return Archive_Zip::errorCode();
  2079. }
  2080. // ----- Get comment
  2081. if ($v_data['comment_size'] != 0) {
  2082. $p_central_dir['comment'] = fread($this->_zip_fd, $v_data['comment_size']);
  2083. } else {
  2084. $p_central_dir['comment'] = '';
  2085. }
  2086. $p_central_dir['entries'] = $v_data['entries'];
  2087. $p_central_dir['disk_entries'] = $v_data['disk_entries'];
  2088. $p_central_dir['offset'] = $v_data['offset'];
  2089. $p_central_dir['size'] = $v_data['size'];
  2090. $p_central_dir['disk'] = $v_data['disk'];
  2091. $p_central_dir['disk_start'] = $v_data['disk_start'];
  2092. return $v_result;
  2093. }
  2094. // ---------------------------------------------------------------------------
  2095. // ---------------------------------------------------------------------------
  2096. // Function : _deleteByRule()
  2097. // Description :
  2098. // Parameters :
  2099. // Return Values :
  2100. // ---------------------------------------------------------------------------
  2101. /**
  2102. * Archive_Zip::_deleteByRule()
  2103. *
  2104. * { Description }
  2105. *
  2106. * @return int
  2107. */
  2108. function _deleteByRule(&$p_result_list, &$p_params)
  2109. {
  2110. $v_result = 1;
  2111. $v_list_detail = array();
  2112. // ----- Open the zip file
  2113. if (($v_result = $this->_openFd('rb')) != 1) {
  2114. return $v_result;
  2115. }
  2116. // ----- Read the central directory informations
  2117. $v_central_dir = array();
  2118. if (($v_result = $this->_readEndCentralDir($v_central_dir)) != 1) {
  2119. $this->_closeFd();
  2120. return $v_result;
  2121. }
  2122. // ----- Go to beginning of File
  2123. @rewind($this->_zip_fd);
  2124. // ----- Scan all the files
  2125. // ----- Start at beginning of Central Dir
  2126. $v_pos_entry = $v_central_dir['offset'];
  2127. @rewind($this->_zip_fd);
  2128. if (@fseek($this->_zip_fd, $v_pos_entry)) {
  2129. // ----- Clean
  2130. $this->_closeFd();
  2131. $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP,
  2132. 'Invalid archive size');
  2133. return Archive_Zip::errorCode();
  2134. }
  2135. // ----- Read each entry
  2136. $v_header_list = array();
  2137. $j_start = 0;
  2138. for ($i = 0, $v_nb_extracted = 0; $i<$v_central_dir['entries']; $i++) {
  2139. // ----- Read the file header
  2140. $v_header_list[$v_nb_extracted] = array();
  2141. $v_result = $this->_readCentralFileHeader($v_header_list[$v_nb_extracted]);
  2142. if ($v_result != 1) {
  2143. // ----- Clean
  2144. $this->_closeFd();
  2145. return $v_result;
  2146. }
  2147. // ----- Store the index
  2148. $v_header_list[$v_nb_extracted]['index'] = $i;
  2149. // ----- Look for the specific extract rules
  2150. $v_found = false;
  2151. // ----- Look for extract by name rule
  2152. if ((isset($p_params[ARCHIVE_ZIP_PARAM_BY_NAME]))
  2153. && ($p_params[ARCHIVE_ZIP_PARAM_BY_NAME] != 0)) {
  2154. // ----- Look if the filename is in the list
  2155. for ($j = 0; ($j<sizeof($p_params[ARCHIVE_ZIP_PARAM_BY_NAME])) && (!$v_found); $j++) {
  2156. // ----- Look for a directory
  2157. if (substr($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j], -1) == "/") {
  2158. // ----- Look if the directory is in the filename path
  2159. if ( (strlen($v_header_list[$v_nb_extracted]['stored_filename']) > strlen($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j]))
  2160. && (substr($v_header_list[$v_nb_extracted]['stored_filename'], 0, strlen($p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j])) == $p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j])) {
  2161. $v_found = true;
  2162. } elseif ((($v_header_list[$v_nb_extracted]['external']&0x00000010)==0x00000010) /* Indicates a folder */
  2163. && ($v_header_list[$v_nb_extracted]['stored_filename'].'/' == $p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j])) {
  2164. $v_found = true;
  2165. }
  2166. } elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_params[ARCHIVE_ZIP_PARAM_BY_NAME][$j]) {
  2167. // ----- Look for a filename
  2168. $v_found = true;
  2169. }
  2170. }
  2171. } else if ((isset($p_params[ARCHIVE_ZIP_PARAM_BY_PREG]))
  2172. && ($p_params[ARCHIVE_ZIP_PARAM_BY_PREG] != "")) {
  2173. // ----- Look for extract by preg rule
  2174. if (preg_match($p_params[ARCHIVE_ZIP_PARAM_BY_PREG],
  2175. $v_header_list[$v_nb_extracted]['stored_filename'])) {
  2176. $v_found = true;
  2177. }
  2178. } else if ((isset($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX]))
  2179. && ($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX] != 0)) {
  2180. // ----- Look for extract by index rule
  2181. // ----- Look if the index is in the list
  2182. for ($j = $j_start; ($j<sizeof($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX])) && (!$v_found); $j++) {
  2183. if (($i>=$p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['start'])
  2184. && ($i<=$p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['end'])) {
  2185. $v_found = true;
  2186. }
  2187. if ($i>=$p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['end']) {
  2188. $j_start = $j+1;
  2189. }
  2190. if ($p_params[ARCHIVE_ZIP_PARAM_BY_INDEX][$j]['start']>$i) {
  2191. break;
  2192. }
  2193. }
  2194. }
  2195. // ----- Look for deletion
  2196. if ($v_found) {
  2197. unset($v_header_list[$v_nb_extracted]);
  2198. } else {
  2199. $v_nb_extracted++;
  2200. }
  2201. }
  2202. // ----- Look if something need to be deleted
  2203. if ($v_nb_extracted > 0) {
  2204. // ----- Creates a temporay file
  2205. $v_zip_temp_name = ARCHIVE_ZIP_TEMPORARY_DIR.uniqid('archive_zip-')
  2206. .'.tmp';
  2207. // ----- Creates a temporary zip archive
  2208. $v_temp_zip = new Archive_Zip($v_zip_temp_name);
  2209. // ----- Open the temporary zip file in write mode
  2210. if (($v_result = $v_temp_zip->_openFd('wb')) != 1) {
  2211. $this->_closeFd();
  2212. return $v_result;
  2213. }
  2214. // ----- Look which file need to be kept
  2215. for ($i = 0; $i<sizeof($v_header_list); $i++) {
  2216. // ----- Calculate the position of the header
  2217. @rewind($this->_zip_fd);
  2218. if (@fseek($this->_zip_fd, $v_header_list[$i]['offset'])) {
  2219. // ----- Clean
  2220. $this->_closeFd();
  2221. $v_temp_zip->_closeFd();
  2222. @unlink($v_zip_temp_name);
  2223. $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP,
  2224. 'Invalid archive size');
  2225. return Archive_Zip::errorCode();
  2226. }
  2227. // ----- Read the file header
  2228. if (($v_result = $this->_readFileHeader($v_header_list[$i])) != 1) {
  2229. // ----- Clean
  2230. $this->_closeFd();
  2231. $v_temp_zip->_closeFd();
  2232. @unlink($v_zip_temp_name);
  2233. return $v_result;
  2234. }
  2235. // ----- Write the file header
  2236. $v_result = $v_temp_zip->_writeFileHeader($v_header_list[$i]);
  2237. if ($v_result != 1) {
  2238. // ----- Clean
  2239. $this->_closeFd();
  2240. $v_temp_zip->_closeFd();
  2241. @unlink($v_zip_temp_name);
  2242. return $v_result;
  2243. }
  2244. // ----- Read/write the data block
  2245. $v_result = $this->_tool_CopyBlock($this->_zip_fd,
  2246. $v_temp_zip->_zip_fd,
  2247. $v_header_list[$i]['compressed_size']);
  2248. if ($v_result != 1) {
  2249. // ----- Clean
  2250. $this->_closeFd();
  2251. $v_temp_zip->_closeFd();
  2252. @unlink($v_zip_temp_name);
  2253. return $v_result;
  2254. }
  2255. }
  2256. // ----- Store the offset of the central dir
  2257. $v_offset = @ftell($v_temp_zip->_zip_fd);
  2258. // ----- Re-Create the Central Dir files header
  2259. for ($i = 0; $i<sizeof($v_header_list); $i++) {
  2260. // ----- Create the file header
  2261. $v_result = $v_temp_zip->_writeCentralFileHeader($v_header_list[$i]);
  2262. if ($v_result != 1) {
  2263. // ----- Clean
  2264. $v_temp_zip->_closeFd();
  2265. $this->_closeFd();
  2266. @unlink($v_zip_temp_name);
  2267. return $v_result;
  2268. }
  2269. // ----- Transform the header to a 'usable' info
  2270. $v_temp_zip->_convertHeader2FileInfo($v_header_list[$i],
  2271. $p_result_list[$i]);
  2272. }
  2273. // ----- Zip file comment
  2274. $v_comment = '';
  2275. // ----- Calculate the size of the central header
  2276. $v_size = @ftell($v_temp_zip->_zip_fd)-$v_offset;
  2277. // ----- Create the central dir footer
  2278. $v_result = $v_temp_zip->_writeCentralHeader(sizeof($v_header_list),
  2279. $v_size, $v_offset,
  2280. $v_comment);
  2281. if ($v_result != 1) {
  2282. // ----- Clean
  2283. unset($v_header_list);
  2284. $v_temp_zip->_closeFd();
  2285. $this->_closeFd();
  2286. @unlink($v_zip_temp_name);
  2287. return $v_result;
  2288. }
  2289. // ----- Close
  2290. $v_temp_zip->_closeFd();
  2291. $this->_closeFd();
  2292. // ----- Delete the zip file
  2293. // TBC : I should test the result ...
  2294. @unlink($this->_zipname);
  2295. // ----- Rename the temporary file
  2296. // TBC : I should test the result ...
  2297. //@rename($v_zip_temp_name, $this->_zipname);
  2298. $this->_tool_Rename($v_zip_temp_name, $this->_zipname);
  2299. // ----- Destroy the temporary archive
  2300. unset($v_temp_zip);
  2301. }
  2302. return $v_result;
  2303. }
  2304. // ---------------------------------------------------------------------------
  2305. // ---------------------------------------------------------------------------
  2306. // Function : _dirCheck()
  2307. // Description :
  2308. // Check if a directory exists, if not it creates it and all the parents directory
  2309. // which may be useful.
  2310. // Parameters :
  2311. // $p_dir : Directory path to check.
  2312. // Return Values :
  2313. // 1 : OK
  2314. // -1 : Unable to create directory
  2315. // ---------------------------------------------------------------------------
  2316. /**
  2317. * Archive_Zip::_dirCheck()
  2318. *
  2319. * { Description }
  2320. *
  2321. * @param [type] $p_is_dir
  2322. * @return int
  2323. */
  2324. function _dirCheck($p_dir, $p_is_dir = false)
  2325. {
  2326. $v_result = 1;
  2327. // ----- Remove the final '/'
  2328. if (($p_is_dir) && (substr($p_dir, -1)=='/')) {
  2329. $p_dir = substr($p_dir, 0, strlen($p_dir)-1);
  2330. }
  2331. // ----- Check the directory availability
  2332. if ((is_dir($p_dir)) || ($p_dir == "")) {
  2333. return 1;
  2334. }
  2335. // ----- Extract parent directory
  2336. $p_parent_dir = dirname($p_dir);
  2337. // ----- Just a check
  2338. if ($p_parent_dir != $p_dir) {
  2339. // ----- Look for parent directory
  2340. if ($p_parent_dir != "") {
  2341. if (($v_result = $this->_dirCheck($p_parent_dir)) != 1) {
  2342. return $v_result;
  2343. }
  2344. }
  2345. }
  2346. // ----- Create the directory
  2347. if (!@mkdir($p_dir, 0777)) {
  2348. $this->_errorLog(ARCHIVE_ZIP_ERR_DIR_CREATE_FAIL,
  2349. "Unable to create directory '$p_dir'");
  2350. return Archive_Zip::errorCode();
  2351. }
  2352. return $v_result;
  2353. }
  2354. // ---------------------------------------------------------------------------
  2355. // ---------------------------------------------------------------------------
  2356. // Function : _merge()
  2357. // Description :
  2358. // If $p_archive_to_add does not exist, the function exit with a success result.
  2359. // Parameters :
  2360. // Return Values :
  2361. // ---------------------------------------------------------------------------
  2362. /**
  2363. * Archive_Zip::_merge()
  2364. *
  2365. * { Description }
  2366. *
  2367. * @return int
  2368. */
  2369. function _merge(&$p_archive_to_add)
  2370. {
  2371. $v_result = 1;
  2372. // ----- Look if the archive_to_add exists
  2373. if (!is_file($p_archive_to_add->_zipname)) {
  2374. // ----- Nothing to merge, so merge is a success
  2375. return 1;
  2376. }
  2377. // ----- Look if the archive exists
  2378. if (!is_file($this->_zipname)) {
  2379. // ----- Do a duplicate
  2380. $v_result = $this->_duplicate($p_archive_to_add->_zipname);
  2381. return $v_result;
  2382. }
  2383. // ----- Open the zip file
  2384. if (($v_result = $this->_openFd('rb')) != 1) {
  2385. return $v_result;
  2386. }
  2387. // ----- Read the central directory informations
  2388. $v_central_dir = array();
  2389. if (($v_result = $this->_readEndCentralDir($v_central_dir)) != 1) {
  2390. $this->_closeFd();
  2391. return $v_result;
  2392. }
  2393. // ----- Go to beginning of File
  2394. @rewind($this->_zip_fd);
  2395. // ----- Open the archive_to_add file
  2396. if (($v_result = $p_archive_to_add->_openFd('rb')) != 1) {
  2397. $this->_closeFd();
  2398. return $v_result;
  2399. }
  2400. // ----- Read the central directory informations
  2401. $v_central_dir_to_add = array();
  2402. $v_result = $p_archive_to_add->_readEndCentralDir($v_central_dir_to_add);
  2403. if ($v_result != 1) {
  2404. $this->_closeFd();
  2405. $p_archive_to_add->_closeFd();
  2406. return $v_result;
  2407. }
  2408. // ----- Go to beginning of File
  2409. @rewind($p_archive_to_add->_zip_fd);
  2410. // ----- Creates a temporay file
  2411. $v_zip_temp_name = ARCHIVE_ZIP_TEMPORARY_DIR.uniqid('archive_zip-').'.tmp';
  2412. // ----- Open the temporary file in write mode
  2413. if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0) {
  2414. $this->_closeFd();
  2415. $p_archive_to_add->_closeFd();
  2416. $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
  2417. 'Unable to open temporary file \''
  2418. .$v_zip_temp_name.'\' in binary write mode');
  2419. return Archive_Zip::errorCode();
  2420. }
  2421. // ----- Copy the files from the archive to the temporary file
  2422. // TBC : Here I should better append the file and go back to erase the
  2423. // central dir
  2424. $v_size = $v_central_dir['offset'];
  2425. while ($v_size != 0) {
  2426. $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  2427. ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  2428. $v_buffer = fread($this->_zip_fd, $v_read_size);
  2429. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  2430. $v_size -= $v_read_size;
  2431. }
  2432. // ----- Copy the files from the archive_to_add into the temporary file
  2433. $v_size = $v_central_dir_to_add['offset'];
  2434. while ($v_size != 0) {
  2435. $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  2436. ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  2437. $v_buffer = fread($p_archive_to_add->_zip_fd, $v_read_size);
  2438. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  2439. $v_size -= $v_read_size;
  2440. }
  2441. // ----- Store the offset of the central dir
  2442. $v_offset = @ftell($v_zip_temp_fd);
  2443. // ----- Copy the block of file headers from the old archive
  2444. $v_size = $v_central_dir['size'];
  2445. while ($v_size != 0) {
  2446. $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  2447. ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  2448. $v_buffer = @fread($this->_zip_fd, $v_read_size);
  2449. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  2450. $v_size -= $v_read_size;
  2451. }
  2452. // ----- Copy the block of file headers from the archive_to_add
  2453. $v_size = $v_central_dir_to_add['size'];
  2454. while ($v_size != 0) {
  2455. $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  2456. ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  2457. $v_buffer = @fread($p_archive_to_add->_zip_fd, $v_read_size);
  2458. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  2459. $v_size -= $v_read_size;
  2460. }
  2461. // ----- Zip file comment
  2462. // TBC : I should merge the two comments
  2463. $v_comment = '';
  2464. // ----- Calculate the size of the (new) central header
  2465. $v_size = @ftell($v_zip_temp_fd)-$v_offset;
  2466. // ----- Swap the file descriptor
  2467. // Here is a trick : I swap the temporary fd with the zip fd, in order to use
  2468. // the following methods on the temporary fil and not the real archive fd
  2469. $v_swap = $this->_zip_fd;
  2470. $this->_zip_fd = $v_zip_temp_fd;
  2471. $v_zip_temp_fd = $v_swap;
  2472. // ----- Create the central dir footer
  2473. if (($v_result = $this->_writeCentralHeader($v_central_dir['entries']
  2474. +$v_central_dir_to_add['entries'],
  2475. $v_size, $v_offset,
  2476. $v_comment)) != 1) {
  2477. $this->_closeFd();
  2478. $p_archive_to_add->_closeFd();
  2479. @fclose($v_zip_temp_fd);
  2480. $this->_zip_fd = null;
  2481. // ----- Reset the file list
  2482. unset($v_header_list);
  2483. return $v_result;
  2484. }
  2485. // ----- Swap back the file descriptor
  2486. $v_swap = $this->_zip_fd;
  2487. $this->_zip_fd = $v_zip_temp_fd;
  2488. $v_zip_temp_fd = $v_swap;
  2489. // ----- Close
  2490. $this->_closeFd();
  2491. $p_archive_to_add->_closeFd();
  2492. // ----- Close the temporary file
  2493. @fclose($v_zip_temp_fd);
  2494. // ----- Delete the zip file
  2495. // TBC : I should test the result ...
  2496. @unlink($this->_zipname);
  2497. // ----- Rename the temporary file
  2498. // TBC : I should test the result ...
  2499. //@rename($v_zip_temp_name, $this->_zipname);
  2500. $this->_tool_Rename($v_zip_temp_name, $this->_zipname);
  2501. return $v_result;
  2502. }
  2503. // ---------------------------------------------------------------------------
  2504. // ---------------------------------------------------------------------------
  2505. // Function : _duplicate()
  2506. // Description :
  2507. // Parameters :
  2508. // Return Values :
  2509. // ---------------------------------------------------------------------------
  2510. /**
  2511. * Archive_Zip::_duplicate()
  2512. *
  2513. * { Description }
  2514. * @return int
  2515. */
  2516. function _duplicate($p_archive_filename)
  2517. {
  2518. $v_result = 1;
  2519. // ----- Look if the $p_archive_filename exists
  2520. if (!is_file($p_archive_filename)) {
  2521. // ----- Nothing to duplicate, so duplicate is a success.
  2522. $v_result = 1;
  2523. return $v_result;
  2524. }
  2525. // ----- Open the zip file
  2526. if (($v_result = $this->_openFd('wb')) != 1) {
  2527. return $v_result;
  2528. }
  2529. // ----- Open the temporary file in write mode
  2530. if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0) {
  2531. $this->_closeFd();
  2532. $this->_errorLog(ARCHIVE_ZIP_ERR_READ_OPEN_FAIL,
  2533. 'Unable to open archive file \''
  2534. .$p_archive_filename.'\' in binary write mode');
  2535. return Archive_Zip::errorCode();
  2536. }
  2537. // ----- Copy the files from the archive to the temporary file
  2538. // TBC : Here I should better append the file and go back to erase the
  2539. // central dir
  2540. $v_size = filesize($p_archive_filename);
  2541. while ($v_size != 0) {
  2542. $v_read_size = ($v_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  2543. ? $v_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  2544. $v_buffer = fread($v_zip_temp_fd, $v_read_size);
  2545. @fwrite($this->_zip_fd, $v_buffer, $v_read_size);
  2546. $v_size -= $v_read_size;
  2547. }
  2548. // ----- Close
  2549. $this->_closeFd();
  2550. // ----- Close the temporary file
  2551. @fclose($v_zip_temp_fd);
  2552. return $v_result;
  2553. }
  2554. // ---------------------------------------------------------------------------
  2555. /**
  2556. * Archive_Zip::_check_parameters()
  2557. *
  2558. * { Description }
  2559. *
  2560. * @param integer $p_error_code
  2561. * @param string $p_error_string
  2562. *
  2563. * @return int
  2564. */
  2565. function _check_parameters(&$p_params, $p_default)
  2566. {
  2567. // ----- Check that param is an array
  2568. if (!is_array($p_params)) {
  2569. $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
  2570. 'Unsupported parameter, waiting for an array');
  2571. return Archive_Zip::errorCode();
  2572. }
  2573. // ----- Check that all the params are valid
  2574. for (reset($p_params); list($v_key, $v_value) = each($p_params); ) {
  2575. if (!isset($p_default[$v_key])) {
  2576. $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
  2577. 'Unsupported parameter with key \''.$v_key.'\'');
  2578. return Archive_Zip::errorCode();
  2579. }
  2580. }
  2581. // ----- Set the default values
  2582. for (reset($p_default); list($v_key, $v_value) = each($p_default); ) {
  2583. if (!isset($p_params[$v_key])) {
  2584. $p_params[$v_key] = $p_default[$v_key];
  2585. }
  2586. }
  2587. // ----- Check specific parameters
  2588. $v_callback_list = array ('callback_pre_add','callback_post_add',
  2589. 'callback_pre_extract','callback_post_extract');
  2590. for ($i = 0; $i<sizeof($v_callback_list); $i++) {
  2591. $v_key = $v_callback_list[$i];
  2592. if ((isset($p_params[$v_key])) && ($p_params[$v_key] != '')) {
  2593. if (!function_exists($p_params[$v_key])) {
  2594. $this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAM_VALUE,
  2595. "Callback '".$p_params[$v_key]
  2596. ."()' is not an existing function for "
  2597. ."parameter '".$v_key."'");
  2598. return Archive_Zip::errorCode();
  2599. }
  2600. }
  2601. }
  2602. return(1);
  2603. }
  2604. // ---------------------------------------------------------------------------
  2605. // ---------------------------------------------------------------------------
  2606. // Function : _errorLog()
  2607. // Description :
  2608. // Parameters :
  2609. // ---------------------------------------------------------------------------
  2610. /**
  2611. * Archive_Zip::_errorLog()
  2612. *
  2613. * { Description }
  2614. *
  2615. * @param integer $p_error_code Error code
  2616. * @param string $p_error_string Error message
  2617. *
  2618. * @return void
  2619. */
  2620. function _errorLog($p_error_code = 0, $p_error_string = '')
  2621. {
  2622. $this->_error_code = $p_error_code;
  2623. $this->_error_string = $p_error_string;
  2624. }
  2625. // ---------------------------------------------------------------------------
  2626. // ---------------------------------------------------------------------------
  2627. // Function : _errorReset()
  2628. // Description :
  2629. // Parameters :
  2630. // ---------------------------------------------------------------------------
  2631. /**
  2632. * Archive_Zip::_errorReset()
  2633. *
  2634. * { Description }
  2635. *
  2636. * @return void
  2637. */
  2638. function _errorReset()
  2639. {
  2640. $this->_error_code = 1;
  2641. $this->_error_string = '';
  2642. }
  2643. // ---------------------------------------------------------------------------
  2644. // ---------------------------------------------------------------------------
  2645. // Function : $this->_tool_PathReduction()
  2646. // Description :
  2647. // Parameters :
  2648. // Return Values :
  2649. // ---------------------------------------------------------------------------
  2650. /**
  2651. * _tool_PathReduction()
  2652. *
  2653. * { Description }
  2654. *
  2655. * @return string
  2656. */
  2657. function _tool_PathReduction($p_dir)
  2658. {
  2659. $v_result = "";
  2660. // ----- Look for not empty path
  2661. if ($p_dir != "") {
  2662. // ----- Explode path by directory names
  2663. $v_list = explode("/", $p_dir);
  2664. // ----- Study directories from last to first
  2665. for ($i = sizeof($v_list)-1; $i>=0; $i--) {
  2666. // ----- Look for current path
  2667. if ($v_list[$i] == ".") {
  2668. // ----- Ignore this directory
  2669. // Should be the first $i = 0, but no check is done
  2670. } else if ($v_list[$i] == "..") {
  2671. // ----- Ignore it and ignore the $i-1
  2672. $i--;
  2673. } else if (($v_list[$i] == "") && ($i!=(sizeof($v_list)-1)) && ($i!=0)) {
  2674. // ----- Ignore only the double '//' in path,
  2675. // but not the first and last '/'
  2676. } else {
  2677. $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?"/".$v_result:"");
  2678. }
  2679. }
  2680. }
  2681. return $v_result;
  2682. }
  2683. // ---------------------------------------------------------------------------
  2684. // ---------------------------------------------------------------------------
  2685. // Function : $this->_tool_PathInclusion()
  2686. // Description :
  2687. // This function indicates if the path $p_path is under the $p_dir tree. Or,
  2688. // said in an other way, if the file or sub-dir $p_path is inside the dir
  2689. // $p_dir.
  2690. // The function indicates also if the path is exactly the same as the dir.
  2691. // This function supports path with duplicated '/' like '//', but does not
  2692. // support '.' or '..' statements.
  2693. // Parameters :
  2694. // Return Values :
  2695. // 0 if $p_path is not inside directory $p_dir
  2696. // 1 if $p_path is inside directory $p_dir
  2697. // 2 if $p_path is exactly the same as $p_dir
  2698. // ---------------------------------------------------------------------------
  2699. /**
  2700. * _tool_PathInclusion()
  2701. *
  2702. * { Description }
  2703. *
  2704. * @return int
  2705. */
  2706. function _tool_PathInclusion($p_dir, $p_path)
  2707. {
  2708. $v_result = 1;
  2709. // ----- Explode dir and path by directory separator
  2710. $v_list_dir = explode("/", $p_dir);
  2711. $v_list_path = explode("/", $p_path);
  2712. $v_list_dir_size = sizeof($v_list_dir);
  2713. $v_list_path_size = sizeof($v_list_path);
  2714. // ----- Study directories paths
  2715. $i = 0;
  2716. $j = 0;
  2717. while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
  2718. // ----- Look for empty dir (path reduction)
  2719. if ($v_list_dir[$i] == '') {
  2720. $i++;
  2721. continue;
  2722. }
  2723. if ($v_list_path[$j] == '') {
  2724. $j++;
  2725. continue;
  2726. }
  2727. // ----- Compare the items
  2728. if (($v_list_dir[$i] != $v_list_path[$j])
  2729. && ($v_list_dir[$i] != '')
  2730. && ( $v_list_path[$j] != '')) {
  2731. $v_result = 0;
  2732. }
  2733. // ----- Next items
  2734. $i++;
  2735. $j++;
  2736. }
  2737. // ----- Look if everything seems to be the same
  2738. if ($v_result) {
  2739. // ----- Skip all the empty items
  2740. while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) {
  2741. $j++;
  2742. }
  2743. while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) {
  2744. $i++;
  2745. }
  2746. if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
  2747. // ----- There are exactly the same
  2748. $v_result = 2;
  2749. } else if ($i < $v_list_dir_size) {
  2750. // ----- The path is shorter than the dir
  2751. $v_result = 0;
  2752. }
  2753. }
  2754. return $v_result;
  2755. }
  2756. // ---------------------------------------------------------------------------
  2757. // ---------------------------------------------------------------------------
  2758. // Function : $this->_tool_CopyBlock()
  2759. // Description :
  2760. // Parameters :
  2761. // $p_mode : read/write compression mode
  2762. // 0 : src & dest normal
  2763. // 1 : src gzip, dest normal
  2764. // 2 : src normal, dest gzip
  2765. // 3 : src & dest gzip
  2766. // Return Values :
  2767. // ---------------------------------------------------------------------------
  2768. /**
  2769. * _tool_CopyBlock()
  2770. *
  2771. * { Description }
  2772. *
  2773. * @param integer $p_mode
  2774. *
  2775. * @return int
  2776. */
  2777. function _tool_CopyBlock($p_src, $p_dest, $p_size, $p_mode = 0)
  2778. {
  2779. $v_result = 1;
  2780. if ($p_mode == 0) {
  2781. while ($p_size != 0) {
  2782. $v_read_size = ($p_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  2783. ? $p_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  2784. $v_buffer = @fread($p_src, $v_read_size);
  2785. @fwrite($p_dest, $v_buffer, $v_read_size);
  2786. $p_size -= $v_read_size;
  2787. }
  2788. } else if ($p_mode == 1) {
  2789. while ($p_size != 0) {
  2790. $v_read_size = ($p_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  2791. ? $p_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  2792. $v_buffer = @gzread($p_src, $v_read_size);
  2793. @fwrite($p_dest, $v_buffer, $v_read_size);
  2794. $p_size -= $v_read_size;
  2795. }
  2796. } else if ($p_mode == 2) {
  2797. while ($p_size != 0) {
  2798. $v_read_size = ($p_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  2799. ? $p_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  2800. $v_buffer = @fread($p_src, $v_read_size);
  2801. @gzwrite($p_dest, $v_buffer, $v_read_size);
  2802. $p_size -= $v_read_size;
  2803. }
  2804. } else if ($p_mode == 3) {
  2805. while ($p_size != 0) {
  2806. $v_read_size = ($p_size < ARCHIVE_ZIP_READ_BLOCK_SIZE
  2807. ? $p_size : ARCHIVE_ZIP_READ_BLOCK_SIZE);
  2808. $v_buffer = @gzread($p_src, $v_read_size);
  2809. @gzwrite($p_dest, $v_buffer, $v_read_size);
  2810. $p_size -= $v_read_size;
  2811. }
  2812. }
  2813. return $v_result;
  2814. }
  2815. // ---------------------------------------------------------------------------
  2816. // ---------------------------------------------------------------------------
  2817. // Function : $this->_tool_Rename()
  2818. // Description :
  2819. // This function tries to do a simple rename() function. If it fails, it
  2820. // tries to copy the $p_src file in a new $p_dest file and then unlink the
  2821. // first one.
  2822. // Parameters :
  2823. // $p_src : Old filename
  2824. // $p_dest : New filename
  2825. // Return Values :
  2826. // 1 on success, 0 on failure.
  2827. // ---------------------------------------------------------------------------
  2828. /**
  2829. * _tool_Rename()
  2830. *
  2831. * { Description }
  2832. * @return int
  2833. */
  2834. function _tool_Rename($p_src, $p_dest)
  2835. {
  2836. $v_result = 1;
  2837. // ----- Try to rename the files
  2838. if (!@rename($p_src, $p_dest)) {
  2839. // ----- Try to copy & unlink the src
  2840. if (!@copy($p_src, $p_dest)) {
  2841. $v_result = 0;
  2842. } else if (!@unlink($p_src)) {
  2843. $v_result = 0;
  2844. }
  2845. }
  2846. return $v_result;
  2847. }
  2848. // ---------------------------------------------------------------------------
  2849. // ---------------------------------------------------------------------------
  2850. // Function : $this->_tool_TranslateWinPath()
  2851. // Description :
  2852. // Translate windows path by replacing '\' by '/' and optionally removing
  2853. // drive letter.
  2854. // Parameters :
  2855. // $p_path : path to translate.
  2856. // $p_remove_disk_letter : true | false
  2857. // Return Values :
  2858. // The path translated.
  2859. // ---------------------------------------------------------------------------
  2860. /**
  2861. * _tool_TranslateWinPath()
  2862. *
  2863. * { Description }
  2864. *
  2865. * @param [type] $p_remove_disk_letter
  2866. *
  2867. * @return string
  2868. */
  2869. function _tool_TranslateWinPath($p_path, $p_remove_disk_letter = true)
  2870. {
  2871. if (stristr(php_uname(), 'windows')) {
  2872. // ----- Look for potential disk letter
  2873. if (($p_remove_disk_letter)
  2874. && (($v_position = strpos($p_path, ':')) != false)) {
  2875. $p_path = substr($p_path, $v_position+1);
  2876. }
  2877. // ----- Change potential windows directory separator
  2878. if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0, 1) == '\\')) {
  2879. $p_path = strtr($p_path, '\\', '/');
  2880. }
  2881. }
  2882. return $p_path;
  2883. }
  2884. // ---------------------------------------------------------------------------
  2885. }