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

/PHPExcel_1.7.8-with_documentation-msoffice_format/Classes/PHPExcel/Shared/PCLZip/pclzip.lib.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip-alpes
PHP | 5694 lines | 2952 code | 915 blank | 1827 comment | 956 complexity | 3ee0a4d8a06cedc0a56f29e8f351ef72 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT, LGPL-3.0, LGPL-2.0, JSON
  1. <?php
  2. // --------------------------------------------------------------------------------
  3. // PhpConcept Library - Zip Module 2.8.2
  4. // --------------------------------------------------------------------------------
  5. // License GNU/LGPL - Vincent Blavet - August 2009
  6. // http://www.phpconcept.net
  7. // --------------------------------------------------------------------------------
  8. //
  9. // Presentation :
  10. // PclZip is a PHP library that manage ZIP archives.
  11. // So far tests show that archives generated by PclZip are readable by
  12. // WinZip application and other tools.
  13. //
  14. // Description :
  15. // See readme.txt and http://www.phpconcept.net
  16. //
  17. // Warning :
  18. // This library and the associated files are non commercial, non professional
  19. // work.
  20. // It should not have unexpected results. However if any damage is caused by
  21. // this software the author can not be responsible.
  22. // The use of this software is at the risk of the user.
  23. //
  24. // --------------------------------------------------------------------------------
  25. // $Id: pclzip.lib.php,v 1.60 2009/09/30 21:01:04 vblavet Exp $
  26. // --------------------------------------------------------------------------------
  27. // ----- Constants
  28. if (!defined('PCLZIP_READ_BLOCK_SIZE')) {
  29. define( 'PCLZIP_READ_BLOCK_SIZE', 2048 );
  30. }
  31. // ----- File list separator
  32. // In version 1.x of PclZip, the separator for file list is a space
  33. // (which is not a very smart choice, specifically for windows paths !).
  34. // A better separator should be a comma (,). This constant gives you the
  35. // abilty to change that.
  36. // However notice that changing this value, may have impact on existing
  37. // scripts, using space separated filenames.
  38. // Recommanded values for compatibility with older versions :
  39. //define( 'PCLZIP_SEPARATOR', ' ' );
  40. // Recommanded values for smart separation of filenames.
  41. if (!defined('PCLZIP_SEPARATOR')) {
  42. define( 'PCLZIP_SEPARATOR', ',' );
  43. }
  44. // ----- Error configuration
  45. // 0 : PclZip Class integrated error handling
  46. // 1 : PclError external library error handling. By enabling this
  47. // you must ensure that you have included PclError library.
  48. // [2,...] : reserved for futur use
  49. if (!defined('PCLZIP_ERROR_EXTERNAL')) {
  50. define( 'PCLZIP_ERROR_EXTERNAL', 0 );
  51. }
  52. // ----- Optional static temporary directory
  53. // By default temporary files are generated in the script current
  54. // path.
  55. // If defined :
  56. // - MUST BE terminated by a '/'.
  57. // - MUST be a valid, already created directory
  58. // Samples :
  59. // define( 'PCLZIP_TEMPORARY_DIR', '/temp/' );
  60. // define( 'PCLZIP_TEMPORARY_DIR', 'C:/Temp/' );
  61. if (!defined('PCLZIP_TEMPORARY_DIR')) {
  62. define( 'PCLZIP_TEMPORARY_DIR', '' );
  63. }
  64. // ----- Optional threshold ratio for use of temporary files
  65. // Pclzip sense the size of the file to add/extract and decide to
  66. // use or not temporary file. The algorythm is looking for
  67. // memory_limit of PHP and apply a ratio.
  68. // threshold = memory_limit * ratio.
  69. // Recommended values are under 0.5. Default 0.47.
  70. // Samples :
  71. // define( 'PCLZIP_TEMPORARY_FILE_RATIO', 0.5 );
  72. if (!defined('PCLZIP_TEMPORARY_FILE_RATIO')) {
  73. define( 'PCLZIP_TEMPORARY_FILE_RATIO', 0.47 );
  74. }
  75. // --------------------------------------------------------------------------------
  76. // ***** UNDER THIS LINE NOTHING NEEDS TO BE MODIFIED *****
  77. // --------------------------------------------------------------------------------
  78. // ----- Global variables
  79. $g_pclzip_version = "2.8.2";
  80. // ----- Error codes
  81. // -1 : Unable to open file in binary write mode
  82. // -2 : Unable to open file in binary read mode
  83. // -3 : Invalid parameters
  84. // -4 : File does not exist
  85. // -5 : Filename is too long (max. 255)
  86. // -6 : Not a valid zip file
  87. // -7 : Invalid extracted file size
  88. // -8 : Unable to create directory
  89. // -9 : Invalid archive extension
  90. // -10 : Invalid archive format
  91. // -11 : Unable to delete file (unlink)
  92. // -12 : Unable to rename file (rename)
  93. // -13 : Invalid header checksum
  94. // -14 : Invalid archive size
  95. define( 'PCLZIP_ERR_USER_ABORTED', 2 );
  96. define( 'PCLZIP_ERR_NO_ERROR', 0 );
  97. define( 'PCLZIP_ERR_WRITE_OPEN_FAIL', -1 );
  98. define( 'PCLZIP_ERR_READ_OPEN_FAIL', -2 );
  99. define( 'PCLZIP_ERR_INVALID_PARAMETER', -3 );
  100. define( 'PCLZIP_ERR_MISSING_FILE', -4 );
  101. define( 'PCLZIP_ERR_FILENAME_TOO_LONG', -5 );
  102. define( 'PCLZIP_ERR_INVALID_ZIP', -6 );
  103. define( 'PCLZIP_ERR_BAD_EXTRACTED_FILE', -7 );
  104. define( 'PCLZIP_ERR_DIR_CREATE_FAIL', -8 );
  105. define( 'PCLZIP_ERR_BAD_EXTENSION', -9 );
  106. define( 'PCLZIP_ERR_BAD_FORMAT', -10 );
  107. define( 'PCLZIP_ERR_DELETE_FILE_FAIL', -11 );
  108. define( 'PCLZIP_ERR_RENAME_FILE_FAIL', -12 );
  109. define( 'PCLZIP_ERR_BAD_CHECKSUM', -13 );
  110. define( 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP', -14 );
  111. define( 'PCLZIP_ERR_MISSING_OPTION_VALUE', -15 );
  112. define( 'PCLZIP_ERR_INVALID_OPTION_VALUE', -16 );
  113. define( 'PCLZIP_ERR_ALREADY_A_DIRECTORY', -17 );
  114. define( 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION', -18 );
  115. define( 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION', -19 );
  116. define( 'PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE', -20 );
  117. define( 'PCLZIP_ERR_DIRECTORY_RESTRICTION', -21 );
  118. // ----- Options values
  119. define( 'PCLZIP_OPT_PATH', 77001 );
  120. define( 'PCLZIP_OPT_ADD_PATH', 77002 );
  121. define( 'PCLZIP_OPT_REMOVE_PATH', 77003 );
  122. define( 'PCLZIP_OPT_REMOVE_ALL_PATH', 77004 );
  123. define( 'PCLZIP_OPT_SET_CHMOD', 77005 );
  124. define( 'PCLZIP_OPT_EXTRACT_AS_STRING', 77006 );
  125. define( 'PCLZIP_OPT_NO_COMPRESSION', 77007 );
  126. define( 'PCLZIP_OPT_BY_NAME', 77008 );
  127. define( 'PCLZIP_OPT_BY_INDEX', 77009 );
  128. define( 'PCLZIP_OPT_BY_EREG', 77010 );
  129. define( 'PCLZIP_OPT_BY_PREG', 77011 );
  130. define( 'PCLZIP_OPT_COMMENT', 77012 );
  131. define( 'PCLZIP_OPT_ADD_COMMENT', 77013 );
  132. define( 'PCLZIP_OPT_PREPEND_COMMENT', 77014 );
  133. define( 'PCLZIP_OPT_EXTRACT_IN_OUTPUT', 77015 );
  134. define( 'PCLZIP_OPT_REPLACE_NEWER', 77016 );
  135. define( 'PCLZIP_OPT_STOP_ON_ERROR', 77017 );
  136. // Having big trouble with crypt. Need to multiply 2 long int
  137. // which is not correctly supported by PHP ...
  138. //define( 'PCLZIP_OPT_CRYPT', 77018 );
  139. define( 'PCLZIP_OPT_EXTRACT_DIR_RESTRICTION', 77019 );
  140. define( 'PCLZIP_OPT_TEMP_FILE_THRESHOLD', 77020 );
  141. define( 'PCLZIP_OPT_ADD_TEMP_FILE_THRESHOLD', 77020 ); // alias
  142. define( 'PCLZIP_OPT_TEMP_FILE_ON', 77021 );
  143. define( 'PCLZIP_OPT_ADD_TEMP_FILE_ON', 77021 ); // alias
  144. define( 'PCLZIP_OPT_TEMP_FILE_OFF', 77022 );
  145. define( 'PCLZIP_OPT_ADD_TEMP_FILE_OFF', 77022 ); // alias
  146. // ----- File description attributes
  147. define( 'PCLZIP_ATT_FILE_NAME', 79001 );
  148. define( 'PCLZIP_ATT_FILE_NEW_SHORT_NAME', 79002 );
  149. define( 'PCLZIP_ATT_FILE_NEW_FULL_NAME', 79003 );
  150. define( 'PCLZIP_ATT_FILE_MTIME', 79004 );
  151. define( 'PCLZIP_ATT_FILE_CONTENT', 79005 );
  152. define( 'PCLZIP_ATT_FILE_COMMENT', 79006 );
  153. // ----- Call backs values
  154. define( 'PCLZIP_CB_PRE_EXTRACT', 78001 );
  155. define( 'PCLZIP_CB_POST_EXTRACT', 78002 );
  156. define( 'PCLZIP_CB_PRE_ADD', 78003 );
  157. define( 'PCLZIP_CB_POST_ADD', 78004 );
  158. /* For futur use
  159. define( 'PCLZIP_CB_PRE_LIST', 78005 );
  160. define( 'PCLZIP_CB_POST_LIST', 78006 );
  161. define( 'PCLZIP_CB_PRE_DELETE', 78007 );
  162. define( 'PCLZIP_CB_POST_DELETE', 78008 );
  163. */
  164. // --------------------------------------------------------------------------------
  165. // Class : PclZip
  166. // Description :
  167. // PclZip is the class that represent a Zip archive.
  168. // The public methods allow the manipulation of the archive.
  169. // Attributes :
  170. // Attributes must not be accessed directly.
  171. // Methods :
  172. // PclZip() : Object creator
  173. // create() : Creates the Zip archive
  174. // listContent() : List the content of the Zip archive
  175. // extract() : Extract the content of the archive
  176. // properties() : List the properties of the archive
  177. // --------------------------------------------------------------------------------
  178. class PclZip
  179. {
  180. // ----- Filename of the zip file
  181. var $zipname = '';
  182. // ----- File descriptor of the zip file
  183. var $zip_fd = 0;
  184. // ----- Internal error handling
  185. var $error_code = 1;
  186. var $error_string = '';
  187. // ----- Current status of the magic_quotes_runtime
  188. // This value store the php configuration for magic_quotes
  189. // The class can then disable the magic_quotes and reset it after
  190. var $magic_quotes_status;
  191. // --------------------------------------------------------------------------------
  192. // Function : PclZip()
  193. // Description :
  194. // Creates a PclZip object and set the name of the associated Zip archive
  195. // filename.
  196. // Note that no real action is taken, if the archive does not exist it is not
  197. // created. Use create() for that.
  198. // --------------------------------------------------------------------------------
  199. function PclZip($p_zipname)
  200. {
  201. // ----- Tests the zlib
  202. if (!function_exists('gzopen'))
  203. {
  204. die('Abort '.basename(__FILE__).' : Missing zlib extensions');
  205. }
  206. // ----- Set the attributes
  207. $this->zipname = $p_zipname;
  208. $this->zip_fd = 0;
  209. $this->magic_quotes_status = -1;
  210. // ----- Return
  211. return;
  212. }
  213. // --------------------------------------------------------------------------------
  214. // --------------------------------------------------------------------------------
  215. // Function :
  216. // create($p_filelist, $p_add_dir="", $p_remove_dir="")
  217. // create($p_filelist, $p_option, $p_option_value, ...)
  218. // Description :
  219. // This method supports two different synopsis. The first one is historical.
  220. // This method creates a Zip Archive. The Zip file is created in the
  221. // filesystem. The files and directories indicated in $p_filelist
  222. // are added in the archive. See the parameters description for the
  223. // supported format of $p_filelist.
  224. // When a directory is in the list, the directory and its content is added
  225. // in the archive.
  226. // In this synopsis, the function takes an optional variable list of
  227. // options. See bellow the supported options.
  228. // Parameters :
  229. // $p_filelist : An array containing file or directory names, or
  230. // a string containing one filename or one directory name, or
  231. // a string containing a list of filenames and/or directory
  232. // names separated by spaces.
  233. // $p_add_dir : A path to add before the real path of the archived file,
  234. // in order to have it memorized in the archive.
  235. // $p_remove_dir : A path to remove from the real path of the file to archive,
  236. // in order to have a shorter path memorized in the archive.
  237. // When $p_add_dir and $p_remove_dir are set, $p_remove_dir
  238. // is removed first, before $p_add_dir is added.
  239. // Options :
  240. // PCLZIP_OPT_ADD_PATH :
  241. // PCLZIP_OPT_REMOVE_PATH :
  242. // PCLZIP_OPT_REMOVE_ALL_PATH :
  243. // PCLZIP_OPT_COMMENT :
  244. // PCLZIP_CB_PRE_ADD :
  245. // PCLZIP_CB_POST_ADD :
  246. // Return Values :
  247. // 0 on failure,
  248. // The list of the added files, with a status of the add action.
  249. // (see PclZip::listContent() for list entry format)
  250. // --------------------------------------------------------------------------------
  251. function create($p_filelist)
  252. {
  253. $v_result=1;
  254. // ----- Reset the error handler
  255. $this->privErrorReset();
  256. // ----- Set default values
  257. $v_options = array();
  258. $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
  259. // ----- Look for variable options arguments
  260. $v_size = func_num_args();
  261. // ----- Look for arguments
  262. if ($v_size > 1) {
  263. // ----- Get the arguments
  264. $v_arg_list = func_get_args();
  265. // ----- Remove from the options list the first argument
  266. array_shift($v_arg_list);
  267. $v_size--;
  268. // ----- Look for first arg
  269. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  270. // ----- Parse the options
  271. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  272. array (PCLZIP_OPT_REMOVE_PATH => 'optional',
  273. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  274. PCLZIP_OPT_ADD_PATH => 'optional',
  275. PCLZIP_CB_PRE_ADD => 'optional',
  276. PCLZIP_CB_POST_ADD => 'optional',
  277. PCLZIP_OPT_NO_COMPRESSION => 'optional',
  278. PCLZIP_OPT_COMMENT => 'optional',
  279. PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
  280. PCLZIP_OPT_TEMP_FILE_ON => 'optional',
  281. PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
  282. //, PCLZIP_OPT_CRYPT => 'optional'
  283. ));
  284. if ($v_result != 1) {
  285. return 0;
  286. }
  287. }
  288. // ----- Look for 2 args
  289. // Here we need to support the first historic synopsis of the
  290. // method.
  291. else {
  292. // ----- Get the first argument
  293. $v_options[PCLZIP_OPT_ADD_PATH] = $v_arg_list[0];
  294. // ----- Look for the optional second argument
  295. if ($v_size == 2) {
  296. $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
  297. }
  298. else if ($v_size > 2) {
  299. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
  300. "Invalid number / type of arguments");
  301. return 0;
  302. }
  303. }
  304. }
  305. // ----- Look for default option values
  306. $this->privOptionDefaultThreshold($v_options);
  307. // ----- Init
  308. $v_string_list = array();
  309. $v_att_list = array();
  310. $v_filedescr_list = array();
  311. $p_result_list = array();
  312. // ----- Look if the $p_filelist is really an array
  313. if (is_array($p_filelist)) {
  314. // ----- Look if the first element is also an array
  315. // This will mean that this is a file description entry
  316. if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
  317. $v_att_list = $p_filelist;
  318. }
  319. // ----- The list is a list of string names
  320. else {
  321. $v_string_list = $p_filelist;
  322. }
  323. }
  324. // ----- Look if the $p_filelist is a string
  325. else if (is_string($p_filelist)) {
  326. // ----- Create a list from the string
  327. $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
  328. }
  329. // ----- Invalid variable type for $p_filelist
  330. else {
  331. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
  332. return 0;
  333. }
  334. // ----- Reformat the string list
  335. if (sizeof($v_string_list) != 0) {
  336. foreach ($v_string_list as $v_string) {
  337. if ($v_string != '') {
  338. $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
  339. }
  340. else {
  341. }
  342. }
  343. }
  344. // ----- For each file in the list check the attributes
  345. $v_supported_attributes
  346. = array ( PCLZIP_ATT_FILE_NAME => 'mandatory'
  347. ,PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
  348. ,PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
  349. ,PCLZIP_ATT_FILE_MTIME => 'optional'
  350. ,PCLZIP_ATT_FILE_CONTENT => 'optional'
  351. ,PCLZIP_ATT_FILE_COMMENT => 'optional'
  352. );
  353. foreach ($v_att_list as $v_entry) {
  354. $v_result = $this->privFileDescrParseAtt($v_entry,
  355. $v_filedescr_list[],
  356. $v_options,
  357. $v_supported_attributes);
  358. if ($v_result != 1) {
  359. return 0;
  360. }
  361. }
  362. // ----- Expand the filelist (expand directories)
  363. $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
  364. if ($v_result != 1) {
  365. return 0;
  366. }
  367. // ----- Call the create fct
  368. $v_result = $this->privCreate($v_filedescr_list, $p_result_list, $v_options);
  369. if ($v_result != 1) {
  370. return 0;
  371. }
  372. // ----- Return
  373. return $p_result_list;
  374. }
  375. // --------------------------------------------------------------------------------
  376. // --------------------------------------------------------------------------------
  377. // Function :
  378. // add($p_filelist, $p_add_dir="", $p_remove_dir="")
  379. // add($p_filelist, $p_option, $p_option_value, ...)
  380. // Description :
  381. // This method supports two synopsis. The first one is historical.
  382. // This methods add the list of files in an existing archive.
  383. // If a file with the same name already exists, it is added at the end of the
  384. // archive, the first one is still present.
  385. // If the archive does not exist, it is created.
  386. // Parameters :
  387. // $p_filelist : An array containing file or directory names, or
  388. // a string containing one filename or one directory name, or
  389. // a string containing a list of filenames and/or directory
  390. // names separated by spaces.
  391. // $p_add_dir : A path to add before the real path of the archived file,
  392. // in order to have it memorized in the archive.
  393. // $p_remove_dir : A path to remove from the real path of the file to archive,
  394. // in order to have a shorter path memorized in the archive.
  395. // When $p_add_dir and $p_remove_dir are set, $p_remove_dir
  396. // is removed first, before $p_add_dir is added.
  397. // Options :
  398. // PCLZIP_OPT_ADD_PATH :
  399. // PCLZIP_OPT_REMOVE_PATH :
  400. // PCLZIP_OPT_REMOVE_ALL_PATH :
  401. // PCLZIP_OPT_COMMENT :
  402. // PCLZIP_OPT_ADD_COMMENT :
  403. // PCLZIP_OPT_PREPEND_COMMENT :
  404. // PCLZIP_CB_PRE_ADD :
  405. // PCLZIP_CB_POST_ADD :
  406. // Return Values :
  407. // 0 on failure,
  408. // The list of the added files, with a status of the add action.
  409. // (see PclZip::listContent() for list entry format)
  410. // --------------------------------------------------------------------------------
  411. function add($p_filelist)
  412. {
  413. $v_result=1;
  414. // ----- Reset the error handler
  415. $this->privErrorReset();
  416. // ----- Set default values
  417. $v_options = array();
  418. $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
  419. // ----- Look for variable options arguments
  420. $v_size = func_num_args();
  421. // ----- Look for arguments
  422. if ($v_size > 1) {
  423. // ----- Get the arguments
  424. $v_arg_list = func_get_args();
  425. // ----- Remove form the options list the first argument
  426. array_shift($v_arg_list);
  427. $v_size--;
  428. // ----- Look for first arg
  429. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  430. // ----- Parse the options
  431. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  432. array (PCLZIP_OPT_REMOVE_PATH => 'optional',
  433. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  434. PCLZIP_OPT_ADD_PATH => 'optional',
  435. PCLZIP_CB_PRE_ADD => 'optional',
  436. PCLZIP_CB_POST_ADD => 'optional',
  437. PCLZIP_OPT_NO_COMPRESSION => 'optional',
  438. PCLZIP_OPT_COMMENT => 'optional',
  439. PCLZIP_OPT_ADD_COMMENT => 'optional',
  440. PCLZIP_OPT_PREPEND_COMMENT => 'optional',
  441. PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
  442. PCLZIP_OPT_TEMP_FILE_ON => 'optional',
  443. PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
  444. //, PCLZIP_OPT_CRYPT => 'optional'
  445. ));
  446. if ($v_result != 1) {
  447. return 0;
  448. }
  449. }
  450. // ----- Look for 2 args
  451. // Here we need to support the first historic synopsis of the
  452. // method.
  453. else {
  454. // ----- Get the first argument
  455. $v_options[PCLZIP_OPT_ADD_PATH] = $v_add_path = $v_arg_list[0];
  456. // ----- Look for the optional second argument
  457. if ($v_size == 2) {
  458. $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
  459. }
  460. else if ($v_size > 2) {
  461. // ----- Error log
  462. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  463. // ----- Return
  464. return 0;
  465. }
  466. }
  467. }
  468. // ----- Look for default option values
  469. $this->privOptionDefaultThreshold($v_options);
  470. // ----- Init
  471. $v_string_list = array();
  472. $v_att_list = array();
  473. $v_filedescr_list = array();
  474. $p_result_list = array();
  475. // ----- Look if the $p_filelist is really an array
  476. if (is_array($p_filelist)) {
  477. // ----- Look if the first element is also an array
  478. // This will mean that this is a file description entry
  479. if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
  480. $v_att_list = $p_filelist;
  481. }
  482. // ----- The list is a list of string names
  483. else {
  484. $v_string_list = $p_filelist;
  485. }
  486. }
  487. // ----- Look if the $p_filelist is a string
  488. else if (is_string($p_filelist)) {
  489. // ----- Create a list from the string
  490. $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
  491. }
  492. // ----- Invalid variable type for $p_filelist
  493. else {
  494. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type '".gettype($p_filelist)."' for p_filelist");
  495. return 0;
  496. }
  497. // ----- Reformat the string list
  498. if (sizeof($v_string_list) != 0) {
  499. foreach ($v_string_list as $v_string) {
  500. $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
  501. }
  502. }
  503. // ----- For each file in the list check the attributes
  504. $v_supported_attributes
  505. = array ( PCLZIP_ATT_FILE_NAME => 'mandatory'
  506. ,PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
  507. ,PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
  508. ,PCLZIP_ATT_FILE_MTIME => 'optional'
  509. ,PCLZIP_ATT_FILE_CONTENT => 'optional'
  510. ,PCLZIP_ATT_FILE_COMMENT => 'optional'
  511. );
  512. foreach ($v_att_list as $v_entry) {
  513. $v_result = $this->privFileDescrParseAtt($v_entry,
  514. $v_filedescr_list[],
  515. $v_options,
  516. $v_supported_attributes);
  517. if ($v_result != 1) {
  518. return 0;
  519. }
  520. }
  521. // ----- Expand the filelist (expand directories)
  522. $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
  523. if ($v_result != 1) {
  524. return 0;
  525. }
  526. // ----- Call the create fct
  527. $v_result = $this->privAdd($v_filedescr_list, $p_result_list, $v_options);
  528. if ($v_result != 1) {
  529. return 0;
  530. }
  531. // ----- Return
  532. return $p_result_list;
  533. }
  534. // --------------------------------------------------------------------------------
  535. // --------------------------------------------------------------------------------
  536. // Function : listContent()
  537. // Description :
  538. // This public method, gives the list of the files and directories, with their
  539. // properties.
  540. // The properties of each entries in the list are (used also in other functions) :
  541. // filename : Name of the file. For a create or add action it is the filename
  542. // given by the user. For an extract function it is the filename
  543. // of the extracted file.
  544. // stored_filename : Name of the file / directory stored in the archive.
  545. // size : Size of the stored file.
  546. // compressed_size : Size of the file's data compressed in the archive
  547. // (without the headers overhead)
  548. // mtime : Last known modification date of the file (UNIX timestamp)
  549. // comment : Comment associated with the file
  550. // folder : true | false
  551. // index : index of the file in the archive
  552. // status : status of the action (depending of the action) :
  553. // Values are :
  554. // ok : OK !
  555. // filtered : the file / dir is not extracted (filtered by user)
  556. // already_a_directory : the file can not be extracted because a
  557. // directory with the same name already exists
  558. // write_protected : the file can not be extracted because a file
  559. // with the same name already exists and is
  560. // write protected
  561. // newer_exist : the file was not extracted because a newer file exists
  562. // path_creation_fail : the file is not extracted because the folder
  563. // does not exist and can not be created
  564. // write_error : the file was not extracted because there was a
  565. // error while writing the file
  566. // read_error : the file was not extracted because there was a error
  567. // while reading the file
  568. // invalid_header : the file was not extracted because of an archive
  569. // format error (bad file header)
  570. // Note that each time a method can continue operating when there
  571. // is an action error on a file, the error is only logged in the file status.
  572. // Return Values :
  573. // 0 on an unrecoverable failure,
  574. // The list of the files in the archive.
  575. // --------------------------------------------------------------------------------
  576. function listContent()
  577. {
  578. $v_result=1;
  579. // ----- Reset the error handler
  580. $this->privErrorReset();
  581. // ----- Check archive
  582. if (!$this->privCheckFormat()) {
  583. return(0);
  584. }
  585. // ----- Call the extracting fct
  586. $p_list = array();
  587. if (($v_result = $this->privList($p_list)) != 1)
  588. {
  589. unset($p_list);
  590. return(0);
  591. }
  592. // ----- Return
  593. return $p_list;
  594. }
  595. // --------------------------------------------------------------------------------
  596. // --------------------------------------------------------------------------------
  597. // Function :
  598. // extract($p_path="./", $p_remove_path="")
  599. // extract([$p_option, $p_option_value, ...])
  600. // Description :
  601. // This method supports two synopsis. The first one is historical.
  602. // This method extract all the files / directories from the archive to the
  603. // folder indicated in $p_path.
  604. // If you want to ignore the 'root' part of path of the memorized files
  605. // you can indicate this in the optional $p_remove_path parameter.
  606. // By default, if a newer file with the same name already exists, the
  607. // file is not extracted.
  608. //
  609. // If both PCLZIP_OPT_PATH and PCLZIP_OPT_ADD_PATH aoptions
  610. // are used, the path indicated in PCLZIP_OPT_ADD_PATH is append
  611. // at the end of the path value of PCLZIP_OPT_PATH.
  612. // Parameters :
  613. // $p_path : Path where the files and directories are to be extracted
  614. // $p_remove_path : First part ('root' part) of the memorized path
  615. // (if any similar) to remove while extracting.
  616. // Options :
  617. // PCLZIP_OPT_PATH :
  618. // PCLZIP_OPT_ADD_PATH :
  619. // PCLZIP_OPT_REMOVE_PATH :
  620. // PCLZIP_OPT_REMOVE_ALL_PATH :
  621. // PCLZIP_CB_PRE_EXTRACT :
  622. // PCLZIP_CB_POST_EXTRACT :
  623. // Return Values :
  624. // 0 or a negative value on failure,
  625. // The list of the extracted files, with a status of the action.
  626. // (see PclZip::listContent() for list entry format)
  627. // --------------------------------------------------------------------------------
  628. function extract()
  629. {
  630. $v_result=1;
  631. // ----- Reset the error handler
  632. $this->privErrorReset();
  633. // ----- Check archive
  634. if (!$this->privCheckFormat()) {
  635. return(0);
  636. }
  637. // ----- Set default values
  638. $v_options = array();
  639. // $v_path = "./";
  640. $v_path = '';
  641. $v_remove_path = "";
  642. $v_remove_all_path = false;
  643. // ----- Look for variable options arguments
  644. $v_size = func_num_args();
  645. // ----- Default values for option
  646. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
  647. // ----- Look for arguments
  648. if ($v_size > 0) {
  649. // ----- Get the arguments
  650. $v_arg_list = func_get_args();
  651. // ----- Look for first arg
  652. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  653. // ----- Parse the options
  654. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  655. array (PCLZIP_OPT_PATH => 'optional',
  656. PCLZIP_OPT_REMOVE_PATH => 'optional',
  657. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  658. PCLZIP_OPT_ADD_PATH => 'optional',
  659. PCLZIP_CB_PRE_EXTRACT => 'optional',
  660. PCLZIP_CB_POST_EXTRACT => 'optional',
  661. PCLZIP_OPT_SET_CHMOD => 'optional',
  662. PCLZIP_OPT_BY_NAME => 'optional',
  663. PCLZIP_OPT_BY_EREG => 'optional',
  664. PCLZIP_OPT_BY_PREG => 'optional',
  665. PCLZIP_OPT_BY_INDEX => 'optional',
  666. PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
  667. PCLZIP_OPT_EXTRACT_IN_OUTPUT => 'optional',
  668. PCLZIP_OPT_REPLACE_NEWER => 'optional'
  669. ,PCLZIP_OPT_STOP_ON_ERROR => 'optional'
  670. ,PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional',
  671. PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
  672. PCLZIP_OPT_TEMP_FILE_ON => 'optional',
  673. PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
  674. ));
  675. if ($v_result != 1) {
  676. return 0;
  677. }
  678. // ----- Set the arguments
  679. if (isset($v_options[PCLZIP_OPT_PATH])) {
  680. $v_path = $v_options[PCLZIP_OPT_PATH];
  681. }
  682. if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
  683. $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
  684. }
  685. if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  686. $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  687. }
  688. if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
  689. // ----- Check for '/' in last path char
  690. if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
  691. $v_path .= '/';
  692. }
  693. $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
  694. }
  695. }
  696. // ----- Look for 2 args
  697. // Here we need to support the first historic synopsis of the
  698. // method.
  699. else {
  700. // ----- Get the first argument
  701. $v_path = $v_arg_list[0];
  702. // ----- Look for the optional second argument
  703. if ($v_size == 2) {
  704. $v_remove_path = $v_arg_list[1];
  705. }
  706. else if ($v_size > 2) {
  707. // ----- Error log
  708. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  709. // ----- Return
  710. return 0;
  711. }
  712. }
  713. }
  714. // ----- Look for default option values
  715. $this->privOptionDefaultThreshold($v_options);
  716. // ----- Trace
  717. // ----- Call the extracting fct
  718. $p_list = array();
  719. $v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path,
  720. $v_remove_all_path, $v_options);
  721. if ($v_result < 1) {
  722. unset($p_list);
  723. return(0);
  724. }
  725. // ----- Return
  726. return $p_list;
  727. }
  728. // --------------------------------------------------------------------------------
  729. // --------------------------------------------------------------------------------
  730. // Function :
  731. // extractByIndex($p_index, $p_path="./", $p_remove_path="")
  732. // extractByIndex($p_index, [$p_option, $p_option_value, ...])
  733. // Description :
  734. // This method supports two synopsis. The first one is historical.
  735. // This method is doing a partial extract of the archive.
  736. // The extracted files or folders are identified by their index in the
  737. // archive (from 0 to n).
  738. // Note that if the index identify a folder, only the folder entry is
  739. // extracted, not all the files included in the archive.
  740. // Parameters :
  741. // $p_index : A single index (integer) or a string of indexes of files to
  742. // extract. The form of the string is "0,4-6,8-12" with only numbers
  743. // and '-' for range or ',' to separate ranges. No spaces or ';'
  744. // are allowed.
  745. // $p_path : Path where the files and directories are to be extracted
  746. // $p_remove_path : First part ('root' part) of the memorized path
  747. // (if any similar) to remove while extracting.
  748. // Options :
  749. // PCLZIP_OPT_PATH :
  750. // PCLZIP_OPT_ADD_PATH :
  751. // PCLZIP_OPT_REMOVE_PATH :
  752. // PCLZIP_OPT_REMOVE_ALL_PATH :
  753. // PCLZIP_OPT_EXTRACT_AS_STRING : The files are extracted as strings and
  754. // not as files.
  755. // The resulting content is in a new field 'content' in the file
  756. // structure.
  757. // This option must be used alone (any other options are ignored).
  758. // PCLZIP_CB_PRE_EXTRACT :
  759. // PCLZIP_CB_POST_EXTRACT :
  760. // Return Values :
  761. // 0 on failure,
  762. // The list of the extracted files, with a status of the action.
  763. // (see PclZip::listContent() for list entry format)
  764. // --------------------------------------------------------------------------------
  765. //function extractByIndex($p_index, options...)
  766. function extractByIndex($p_index)
  767. {
  768. $v_result=1;
  769. // ----- Reset the error handler
  770. $this->privErrorReset();
  771. // ----- Check archive
  772. if (!$this->privCheckFormat()) {
  773. return(0);
  774. }
  775. // ----- Set default values
  776. $v_options = array();
  777. // $v_path = "./";
  778. $v_path = '';
  779. $v_remove_path = "";
  780. $v_remove_all_path = false;
  781. // ----- Look for variable options arguments
  782. $v_size = func_num_args();
  783. // ----- Default values for option
  784. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
  785. // ----- Look for arguments
  786. if ($v_size > 1) {
  787. // ----- Get the arguments
  788. $v_arg_list = func_get_args();
  789. // ----- Remove form the options list the first argument
  790. array_shift($v_arg_list);
  791. $v_size--;
  792. // ----- Look for first arg
  793. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  794. // ----- Parse the options
  795. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  796. array (PCLZIP_OPT_PATH => 'optional',
  797. PCLZIP_OPT_REMOVE_PATH => 'optional',
  798. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  799. PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
  800. PCLZIP_OPT_ADD_PATH => 'optional',
  801. PCLZIP_CB_PRE_EXTRACT => 'optional',
  802. PCLZIP_CB_POST_EXTRACT => 'optional',
  803. PCLZIP_OPT_SET_CHMOD => 'optional',
  804. PCLZIP_OPT_REPLACE_NEWER => 'optional'
  805. ,PCLZIP_OPT_STOP_ON_ERROR => 'optional'
  806. ,PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional',
  807. PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
  808. PCLZIP_OPT_TEMP_FILE_ON => 'optional',
  809. PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
  810. ));
  811. if ($v_result != 1) {
  812. return 0;
  813. }
  814. // ----- Set the arguments
  815. if (isset($v_options[PCLZIP_OPT_PATH])) {
  816. $v_path = $v_options[PCLZIP_OPT_PATH];
  817. }
  818. if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
  819. $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
  820. }
  821. if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  822. $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  823. }
  824. if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
  825. // ----- Check for '/' in last path char
  826. if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
  827. $v_path .= '/';
  828. }
  829. $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
  830. }
  831. if (!isset($v_options[PCLZIP_OPT_EXTRACT_AS_STRING])) {
  832. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
  833. }
  834. else {
  835. }
  836. }
  837. // ----- Look for 2 args
  838. // Here we need to support the first historic synopsis of the
  839. // method.
  840. else {
  841. // ----- Get the first argument
  842. $v_path = $v_arg_list[0];
  843. // ----- Look for the optional second argument
  844. if ($v_size == 2) {
  845. $v_remove_path = $v_arg_list[1];
  846. }
  847. else if ($v_size > 2) {
  848. // ----- Error log
  849. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  850. // ----- Return
  851. return 0;
  852. }
  853. }
  854. }
  855. // ----- Trace
  856. // ----- Trick
  857. // Here I want to reuse extractByRule(), so I need to parse the $p_index
  858. // with privParseOptions()
  859. $v_arg_trick = array (PCLZIP_OPT_BY_INDEX, $p_index);
  860. $v_options_trick = array();
  861. $v_result = $this->privParseOptions($v_arg_trick, sizeof($v_arg_trick), $v_options_trick,
  862. array (PCLZIP_OPT_BY_INDEX => 'optional' ));
  863. if ($v_result != 1) {
  864. return 0;
  865. }
  866. $v_options[PCLZIP_OPT_BY_INDEX] = $v_options_trick[PCLZIP_OPT_BY_INDEX];
  867. // ----- Look for default option values
  868. $this->privOptionDefaultThreshold($v_options);
  869. // ----- Call the extracting fct
  870. if (($v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options)) < 1) {
  871. return(0);
  872. }
  873. // ----- Return
  874. return $p_list;
  875. }
  876. // --------------------------------------------------------------------------------
  877. // --------------------------------------------------------------------------------
  878. // Function :
  879. // delete([$p_option, $p_option_value, ...])
  880. // Description :
  881. // This method removes files from the archive.
  882. // If no parameters are given, then all the archive is emptied.
  883. // Parameters :
  884. // None or optional arguments.
  885. // Options :
  886. // PCLZIP_OPT_BY_INDEX :
  887. // PCLZIP_OPT_BY_NAME :
  888. // PCLZIP_OPT_BY_EREG :
  889. // PCLZIP_OPT_BY_PREG :
  890. // Return Values :
  891. // 0 on failure,
  892. // The list of the files which are still present in the archive.
  893. // (see PclZip::listContent() for list entry format)
  894. // --------------------------------------------------------------------------------
  895. function delete()
  896. {
  897. $v_result=1;
  898. // ----- Reset the error handler
  899. $this->privErrorReset();
  900. // ----- Check archive
  901. if (!$this->privCheckFormat()) {
  902. return(0);
  903. }
  904. // ----- Set default values
  905. $v_options = array();
  906. // ----- Look for variable options arguments
  907. $v_size = func_num_args();
  908. // ----- Look for arguments
  909. if ($v_size > 0) {
  910. // ----- Get the arguments
  911. $v_arg_list = func_get_args();
  912. // ----- Parse the options
  913. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  914. array (PCLZIP_OPT_BY_NAME => 'optional',
  915. PCLZIP_OPT_BY_EREG => 'optional',
  916. PCLZIP_OPT_BY_PREG => 'optional',
  917. PCLZIP_OPT_BY_INDEX => 'optional' ));
  918. if ($v_result != 1) {
  919. return 0;
  920. }
  921. }
  922. // ----- Magic quotes trick
  923. $this->privDisableMagicQuotes();
  924. // ----- Call the delete fct
  925. $v_list = array();
  926. if (($v_result = $this->privDeleteByRule($v_list, $v_options)) != 1) {
  927. $this->privSwapBackMagicQuotes();
  928. unset($v_list);
  929. return(0);
  930. }
  931. // ----- Magic quotes trick
  932. $this->privSwapBackMagicQuotes();
  933. // ----- Return
  934. return $v_list;
  935. }
  936. // --------------------------------------------------------------------------------
  937. // --------------------------------------------------------------------------------
  938. // Function : deleteByIndex()
  939. // Description :
  940. // ***** Deprecated *****
  941. // delete(PCLZIP_OPT_BY_INDEX, $p_index) should be prefered.
  942. // --------------------------------------------------------------------------------
  943. function deleteByIndex($p_index)
  944. {
  945. $p_list = $this->delete(PCLZIP_OPT_BY_INDEX, $p_index);
  946. // ----- Return
  947. return $p_list;
  948. }
  949. // --------------------------------------------------------------------------------
  950. // --------------------------------------------------------------------------------
  951. // Function : properties()
  952. // Description :
  953. // This method gives the properties of the archive.
  954. // The properties are :
  955. // nb : Number of files in the archive
  956. // comment : Comment associated with the archive file
  957. // status : not_exist, ok
  958. // Parameters :
  959. // None
  960. // Return Values :
  961. // 0 on failure,
  962. // An array with the archive properties.
  963. // --------------------------------------------------------------------------------
  964. function properties()
  965. {
  966. // ----- Reset the error handler
  967. $this->privErrorReset();
  968. // ----- Magic quotes trick
  969. $this->privDisableMagicQuotes();
  970. // ----- Check archive
  971. if (!$this->privCheckFormat()) {
  972. $this->privSwapBackMagicQuotes();
  973. return(0);
  974. }
  975. // ----- Default properties
  976. $v_prop = array();
  977. $v_prop['comment'] = '';
  978. $v_prop['nb'] = 0;
  979. $v_prop['status'] = 'not_exist';
  980. // ----- Look if file exists
  981. if (@is_file($this->zipname))
  982. {
  983. // ----- Open the zip file
  984. if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
  985. {
  986. $this->privSwapBackMagicQuotes();
  987. // ----- Error log
  988. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
  989. // ----- Return
  990. return 0;
  991. }
  992. // ----- Read the central directory informations
  993. $v_central_dir = array();
  994. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  995. {
  996. $this->privSwapBackMagicQuotes();
  997. return 0;
  998. }
  999. // ----- Close the zip file
  1000. $this->privCloseFd();
  1001. // ----- Set the user attributes
  1002. $v_prop['comment'] = $v_central_dir['comment'];
  1003. $v_prop['nb'] = $v_central_dir['entries'];
  1004. $v_prop['status'] = 'ok';
  1005. }
  1006. // ----- Magic quotes trick
  1007. $this->privSwapBackMagicQuotes();
  1008. // ----- Return
  1009. return $v_prop;
  1010. }
  1011. // --------------------------------------------------------------------------------
  1012. // --------------------------------------------------------------------------------
  1013. // Function : duplicate()
  1014. // Description :
  1015. // This method creates an archive by copying the content of an other one. If
  1016. // the archive already exist, it is replaced by the new one without any warning.
  1017. // Parameters :
  1018. // $p_archive : The filename of a valid archive, or
  1019. // a valid PclZip object.
  1020. // Return Values :
  1021. // 1 on success.
  1022. // 0 or a negative value on error (error code).
  1023. // --------------------------------------------------------------------------------
  1024. function duplicate($p_archive)
  1025. {
  1026. $v_result = 1;
  1027. // ----- Reset the error handler
  1028. $this->privErrorReset();
  1029. // ----- Look if the $p_archive is a PclZip object
  1030. if ((is_object($p_archive)) && (get_class($p_archive) == 'pclzip'))
  1031. {
  1032. // ----- Duplicate the archive
  1033. $v_result = $this->privDuplicate($p_archive->zipname);
  1034. }
  1035. // ----- Look if the $p_archive is a string (so a filename)
  1036. else if (is_string($p_archive))
  1037. {
  1038. // ----- Check that $p_archive is a valid zip file
  1039. // TBC : Should also check the archive format
  1040. if (!is_file($p_archive)) {
  1041. // ----- Error log
  1042. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "No file with filename '".$p_archive."'");
  1043. $v_result = PCLZIP_ERR_MISSING_FILE;
  1044. }
  1045. else {
  1046. // ----- Duplicate the archive
  1047. $v_result = $this->privDuplicate($p_archive);
  1048. }
  1049. }
  1050. // ----- Invalid variable
  1051. else
  1052. {
  1053. // ----- Error log
  1054. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
  1055. $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  1056. }
  1057. // ----- Return
  1058. return $v_result;
  1059. }
  1060. // --------------------------------------------------------------------------------
  1061. // --------------------------------------------------------------------------------
  1062. // Function : merge()
  1063. // Description :
  1064. // This method merge the $p_archive_to_add archive at the end of the current
  1065. // one ($this).
  1066. // If the archive ($this) does not exist, the merge becomes a duplicate.
  1067. // If the $p_archive_to_add archive does not exist, the merge is a success.
  1068. // Parameters :
  1069. // $p_archive_to_add : It can be directly the filename of a valid zip archive,
  1070. // or a PclZip object archive.
  1071. // Return Values :
  1072. // 1 on success,
  1073. // 0 or negative values on error (see below).
  1074. // --------------------------------------------------------------------------------
  1075. function merge($p_archive_to_add)
  1076. {
  1077. $v_result = 1;
  1078. // ----- Reset the error handler
  1079. $this->privErrorReset();
  1080. // ----- Check archive
  1081. if (!$this->privCheckFormat()) {
  1082. return(0);
  1083. }
  1084. // ----- Look if the $p_archive_to_add is a PclZip object
  1085. if ((is_object($p_archive_to_add)) && (get_class($p_archive_to_add) == 'pclzip'))
  1086. {
  1087. // ----- Merge the archive
  1088. $v_result = $this->privMerge($p_archive_to_add);
  1089. }
  1090. // ----- Look if the $p_archive_to_add is a string (so a filename)
  1091. else if (is_string($p_archive_to_add))
  1092. {
  1093. // ----- Create a temporary archive
  1094. $v_object_archive = new PclZip($p_archive_to_add);
  1095. // ----- Merge the archive
  1096. $v_result = $this->privMerge($v_object_archive);
  1097. }
  1098. // ----- Invalid variable
  1099. else
  1100. {
  1101. // ----- Error log
  1102. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
  1103. $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  1104. }
  1105. // ----- Return
  1106. return $v_result;
  1107. }
  1108. // --------------------------------------------------------------------------------
  1109. // --------------------------------------------------------------------------------
  1110. // Function : errorCode()
  1111. // Description :
  1112. // Parameters :
  1113. // --------------------------------------------------------------------------------
  1114. function errorCode()
  1115. {
  1116. if (PCLZIP_ERROR_EXTERNAL == 1) {
  1117. return(PclErrorCode());
  1118. }
  1119. else {
  1120. return($this->error_code);
  1121. }
  1122. }
  1123. // --------------------------------------------------------------------------------
  1124. // --------------------------------------------------------------------------------
  1125. // Function : errorName()
  1126. // Description :
  1127. // Parameters :
  1128. // --------------------------------------------------------------------------------
  1129. function errorName($p_with_code=false)
  1130. {
  1131. $v_name = array ( PCLZIP_ERR_NO_ERROR => 'PCLZIP_ERR_NO_ERROR',
  1132. PCLZIP_ERR_WRITE_OPEN_FAIL => 'PCLZIP_ERR_WRITE_OPEN_FAIL',
  1133. PCLZIP_ERR_READ_OPEN_FAIL => 'PCLZIP_ERR_READ_OPEN_FAIL',
  1134. PCLZIP_ERR_INVALID_PARAMETER => 'PCLZIP_ERR_INVALID_PARAMETER',
  1135. PCLZIP_ERR_MISSING_FILE => 'PCLZIP_ERR_MISSING_FILE',
  1136. PCLZIP_ERR_FILENAME_TOO_LONG => 'PCLZIP_ERR_FILENAME_TOO_LONG',
  1137. PCLZIP_ERR_INVALID_ZIP => 'PCLZIP_ERR_INVALID_ZIP',
  1138. PCLZIP_ERR_BAD_EXTRACTED_FILE => 'PCLZIP_ERR_BAD_EXTRACTED_FILE',
  1139. PCLZIP_ERR_DIR_CREATE_FAIL => 'PCLZIP_ERR_DIR_CREATE_FAIL',
  1140. PCLZIP_ERR_BAD_EXTENSION => 'PCLZIP_ERR_BAD_EXTENSION',
  1141. PCLZIP_ERR_BAD_FORMAT => 'PCLZIP_ERR_BAD_FORMAT',
  1142. PCLZIP_ERR_DELETE_FILE_FAIL => 'PCLZIP_ERR_DELETE_FILE_FAIL',
  1143. PCLZIP_ERR_RENAME_FILE_FAIL => 'PCLZIP_ERR_RENAME_FILE_FAIL',
  1144. PCLZIP_ERR_BAD_CHECKSUM => 'PCLZIP_ERR_BAD_CHECKSUM',
  1145. PCLZIP_ERR_INVALID_ARCHIVE_ZIP => 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP',
  1146. PCLZIP_ERR_MISSING_OPTION_VALUE => 'PCLZIP_ERR_MISSING_OPTION_VALUE',
  1147. PCLZIP_ERR_INVALID_OPTION_VALUE => 'PCLZIP_ERR_INVALID_OPTION_VALUE',
  1148. PCLZIP_ERR_UNSUPPORTED_COMPRESSION => 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION',
  1149. PCLZIP_ERR_UNSUPPORTED_ENCRYPTION => 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION'
  1150. ,PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE => 'PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE'
  1151. ,PCLZIP_ERR_DIRECTORY_RESTRICTION => 'PCLZIP_ERR_DIRECTORY_RESTRICTION'
  1152. );
  1153. if (isset($v_name[$this->error_code])) {
  1154. $v_value = $v_name[$this->error_code];
  1155. }
  1156. else {
  1157. $v_value = 'NoName';
  1158. }
  1159. if ($p_with_code) {
  1160. return($v_value.' ('.$this->error_code.')');
  1161. }
  1162. else {
  1163. return($v_value);
  1164. }
  1165. }
  1166. // --------------------------------------------------------------------------------
  1167. // --------------------------------------------------------------------------------
  1168. // Function : errorInfo()
  1169. // Description :
  1170. // Parameters :
  1171. // --------------------------------------------------------------------------------
  1172. function errorInfo($p_full=false)
  1173. {
  1174. if (PCLZIP_ERROR_EXTERNAL == 1) {
  1175. return(PclErrorString());
  1176. }
  1177. else {
  1178. if ($p_full) {
  1179. return($this->errorName(true)." : ".$this->error_string);
  1180. }
  1181. else {
  1182. return($this->error_string." [code ".$this->error_code."]");
  1183. }
  1184. }
  1185. }
  1186. // --------------------------------------------------------------------------------
  1187. // --------------------------------------------------------------------------------
  1188. // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
  1189. // ***** *****
  1190. // ***** THESES FUNCTIONS MUST NOT BE USED DIRECTLY *****
  1191. // --------------------------------------------------------------------------------
  1192. // --------------------------------------------------------------------------------
  1193. // Function : privCheckFormat()
  1194. // Description :
  1195. // This method check that the archive exists and is a valid zip archive.
  1196. // Several level of check exists. (futur)
  1197. // Parameters :
  1198. // $p_level : Level of check. Default 0.
  1199. // 0 : Check the first bytes (magic codes) (default value))
  1200. // 1 : 0 + Check the central directory (futur)
  1201. // 2 : 1 + Check each file header (futur)
  1202. // Return Values :
  1203. // true on success,
  1204. // false on error, the error code is set.
  1205. // --------------------------------------------------------------------------------
  1206. function privCheckFormat($p_level=0)
  1207. {
  1208. $v_result = true;
  1209. // ----- Reset the file system cache
  1210. clearstatcache();
  1211. // ----- Reset the error handler
  1212. $this->privErrorReset();
  1213. // ----- Look if the file exits
  1214. if (!is_file($this->zipname)) {
  1215. // ----- Error log
  1216. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "Missing archive file '".$this->zipname."'");
  1217. return(false);
  1218. }
  1219. // ----- Check that the file is readeable
  1220. if (!is_readable($this->zipname)) {
  1221. // ----- Error log
  1222. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '".$this->zipname."'");
  1223. return(false);
  1224. }
  1225. // ----- Check the magic code
  1226. // TBC
  1227. // ----- Check the central header
  1228. // TBC
  1229. // ----- Check each file header
  1230. // TBC
  1231. // ----- Return
  1232. return $v_result;
  1233. }
  1234. // --------------------------------------------------------------------------------
  1235. // --------------------------------------------------------------------------------
  1236. // Function : privParseOptions()
  1237. // Description :
  1238. // This internal methods reads the variable list of arguments ($p_options_list,
  1239. // $p_size) and generate an array with the options and values ($v_result_list).
  1240. // $v_requested_options contains the options that can be present and those that
  1241. // must be present.
  1242. // $v_requested_options is an array, with the option value as key, and 'optional',
  1243. // or 'mandatory' as value.
  1244. // Parameters :
  1245. // See above.
  1246. // Return Values :
  1247. // 1 on success.
  1248. // 0 on failure.
  1249. // --------------------------------------------------------------------------------
  1250. function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options=false)
  1251. {
  1252. $v_result=1;
  1253. // ----- Read the options
  1254. $i=0;
  1255. while ($i<$p_size) {
  1256. // ----- Check if the option is supported
  1257. if (!isset($v_requested_options[$p_options_list[$i]])) {
  1258. // ----- Error log
  1259. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid optional parameter '".$p_options_list[$i]."' for this method");
  1260. // ----- Return
  1261. return PclZip::errorCode();
  1262. }
  1263. // ----- Look for next option
  1264. switch ($p_options_list[$i]) {
  1265. // ----- Look for options that request a path value
  1266. case PCLZIP_OPT_PATH :
  1267. case PCLZIP_OPT_REMOVE_PATH :
  1268. case PCLZIP_OPT_ADD_PATH :
  1269. // ----- Check the number of parameters
  1270. if (($i+1) >= $p_size) {
  1271. // ----- Error log
  1272. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1273. // ----- Return
  1274. return PclZip::errorCode();
  1275. }
  1276. // ----- Get the value
  1277. $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], FALSE);
  1278. $i++;
  1279. break;
  1280. case PCLZIP_OPT_TEMP_FILE_THRESHOLD :
  1281. // ----- Check the number of parameters
  1282. if (($i+1) >= $p_size) {
  1283. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1284. return PclZip::errorCode();
  1285. }
  1286. // ----- Check for incompatible options
  1287. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
  1288. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
  1289. return PclZip::errorCode();
  1290. }
  1291. // ----- Check the value
  1292. $v_value = $p_options_list[$i+1];
  1293. if ((!is_integer($v_value)) || ($v_value<0)) {
  1294. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Integer expected for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1295. return PclZip::errorCode();
  1296. }
  1297. // ----- Get the value (and convert it in bytes)
  1298. $v_result_list[$p_options_list[$i]] = $v_value*1048576;
  1299. $i++;
  1300. break;
  1301. case PCLZIP_OPT_TEMP_FILE_ON :
  1302. // ----- Check for incompatible options
  1303. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
  1304. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
  1305. return PclZip::errorCode();
  1306. }
  1307. $v_result_list[$p_options_list[$i]] = true;
  1308. break;
  1309. case PCLZIP_OPT_TEMP_FILE_OFF :
  1310. // ----- Check for incompatible options
  1311. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_ON])) {
  1312. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_ON'");
  1313. return PclZip::errorCode();
  1314. }
  1315. // ----- Check for incompatible options
  1316. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
  1317. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_THRESHOLD'");
  1318. return PclZip::errorCode();
  1319. }
  1320. $v_result_list[$p_options_list[$i]] = true;
  1321. break;
  1322. case PCLZIP_OPT_EXTRACT_DIR_RESTRICTION :
  1323. // ----- Check the number of parameters
  1324. if (($i+1) >= $p_size) {
  1325. // ----- Error log
  1326. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1327. // ----- Return
  1328. return PclZip::errorCode();
  1329. }
  1330. // ----- Get the value
  1331. if ( is_string($p_options_list[$i+1])
  1332. && ($p_options_list[$i+1] != '')) {
  1333. $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], FALSE);
  1334. $i++;
  1335. }
  1336. else {
  1337. }
  1338. break;
  1339. // ----- Look for options that request an array of string for value
  1340. case PCLZIP_OPT_BY_NAME :
  1341. // ----- Check the number of parameters
  1342. if (($i+1) >= $p_size) {
  1343. // ----- Error log
  1344. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1345. // ----- Return
  1346. return PclZip::errorCode();
  1347. }
  1348. // ----- Get the value
  1349. if (is_string($p_options_list[$i+1])) {
  1350. $v_result_list[$p_options_list[$i]][0] = $p_options_list[$i+1];
  1351. }
  1352. else if (is_array($p_options_list[$i+1])) {
  1353. $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
  1354. }
  1355. else {
  1356. // ----- Error log
  1357. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1358. // ----- Return
  1359. return PclZip::errorCode();
  1360. }
  1361. $i++;
  1362. break;
  1363. // ----- Look for options that request an EREG or PREG expression
  1364. case PCLZIP_OPT_BY_EREG :
  1365. // ereg() is deprecated starting with PHP 5.3. Move PCLZIP_OPT_BY_EREG
  1366. // to PCLZIP_OPT_BY_PREG
  1367. $p_options_list[$i] = PCLZIP_OPT_BY_PREG;
  1368. case PCLZIP_OPT_BY_PREG :
  1369. //case PCLZIP_OPT_CRYPT :
  1370. // ----- Check the number of parameters
  1371. if (($i+1) >= $p_size) {
  1372. // ----- Error log
  1373. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1374. // ----- Return
  1375. return PclZip::errorCode();
  1376. }
  1377. // ----- Get the value
  1378. if (is_string($p_options_list[$i+1])) {
  1379. $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
  1380. }
  1381. else {
  1382. // ----- Error log
  1383. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1384. // ----- Return
  1385. return PclZip::errorCode();
  1386. }
  1387. $i++;
  1388. break;
  1389. // ----- Look for options that takes a string
  1390. case PCLZIP_OPT_COMMENT :
  1391. case PCLZIP_OPT_ADD_COMMENT :
  1392. case PCLZIP_OPT_PREPEND_COMMENT :
  1393. // ----- Check the number of parameters
  1394. if (($i+1) >= $p_size) {
  1395. // ----- Error log
  1396. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE,
  1397. "Missing parameter value for option '"
  1398. .PclZipUtilOptionText($p_options_list[$i])
  1399. ."'");
  1400. // ----- Return
  1401. return PclZip::errorCode();
  1402. }
  1403. // ----- Get the value
  1404. if (is_string($p_options_list[$i+1])) {
  1405. $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
  1406. }
  1407. else {
  1408. // ----- Error log
  1409. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE,
  1410. "Wrong parameter value for option '"
  1411. .PclZipUtilOptionText($p_options_list[$i])
  1412. ."'");
  1413. // ----- Return
  1414. return PclZip::errorCode();
  1415. }
  1416. $i++;
  1417. break;
  1418. // ----- Look for options that request an array of index
  1419. case PCLZIP_OPT_BY_INDEX :
  1420. // ----- Check the number of parameters
  1421. if (($i+1) >= $p_size) {
  1422. // ----- Error log
  1423. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1424. // ----- Return
  1425. return PclZip::errorCode();
  1426. }
  1427. // ----- Get the value
  1428. $v_work_list = array();
  1429. if (is_string($p_options_list[$i+1])) {
  1430. // ----- Remove spaces
  1431. $p_options_list[$i+1] = strtr($p_options_list[$i+1], ' ', '');
  1432. // ----- Parse items
  1433. $v_work_list = explode(",", $p_options_list[$i+1]);
  1434. }
  1435. else if (is_integer($p_options_list[$i+1])) {
  1436. $v_work_list[0] = $p_options_list[$i+1].'-'.$p_options_list[$i+1];
  1437. }
  1438. else if (is_array($p_options_list[$i+1])) {
  1439. $v_work_list = $p_options_list[$i+1];
  1440. }
  1441. else {
  1442. // ----- Error log
  1443. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Value must be integer, string or array for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1444. // ----- Return
  1445. return PclZip::errorCode();
  1446. }
  1447. // ----- Reduce the index list
  1448. // each index item in the list must be a couple with a start and
  1449. // an end value : [0,3], [5-5], [8-10], ...
  1450. // ----- Check the format of each item
  1451. $v_sort_flag=false;
  1452. $v_sort_value=0;
  1453. for ($j=0; $j<sizeof($v_work_list); $j++) {
  1454. // ----- Explode the item
  1455. $v_item_list = explode("-", $v_work_list[$j]);
  1456. $v_size_item_list = sizeof($v_item_list);
  1457. // ----- TBC : Here we might check that each item is a
  1458. // real integer ...
  1459. // ----- Look for single value
  1460. if ($v_size_item_list == 1) {
  1461. // ----- Set the option value
  1462. $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
  1463. $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[0];
  1464. }
  1465. elseif ($v_size_item_list == 2) {
  1466. // ----- Set the option value
  1467. $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
  1468. $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[1];
  1469. }
  1470. else {
  1471. // ----- Error log
  1472. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Too many values in index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1473. // ----- Return
  1474. return PclZip::errorCode();
  1475. }
  1476. // ----- Look for list sort
  1477. if ($v_result_list[$p_options_list[$i]][$j]['start'] < $v_sort_value) {
  1478. $v_sort_flag=true;
  1479. // ----- TBC : An automatic sort should be writen ...
  1480. // ----- Error log
  1481. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Invalid order of index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1482. // ----- Return
  1483. return PclZip::errorCode();
  1484. }
  1485. $v_sort_value = $v_result_list[$p_options_list[$i]][$j]['start'];
  1486. }
  1487. // ----- Sort the items
  1488. if ($v_sort_flag) {
  1489. // TBC : To Be Completed
  1490. }
  1491. // ----- Next option
  1492. $i++;
  1493. break;
  1494. // ----- Look for options that request no value
  1495. case PCLZIP_OPT_REMOVE_ALL_PATH :
  1496. case PCLZIP_OPT_EXTRACT_AS_STRING :
  1497. case PCLZIP_OPT_NO_COMPRESSION :
  1498. case PCLZIP_OPT_EXTRACT_IN_OUTPUT :
  1499. case PCLZIP_OPT_REPLACE_NEWER :
  1500. case PCLZIP_OPT_STOP_ON_ERROR :
  1501. $v_result_list[$p_options_list[$i]] = true;
  1502. break;
  1503. // ----- Look for options that request an octal value
  1504. case PCLZIP_OPT_SET_CHMOD :
  1505. // ----- Check the number of parameters
  1506. if (($i+1) >= $p_size) {
  1507. // ----- Error log
  1508. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1509. // ----- Return
  1510. return PclZip::errorCode();
  1511. }
  1512. // ----- Get the value
  1513. $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
  1514. $i++;
  1515. break;
  1516. // ----- Look for options that request a call-back
  1517. case PCLZIP_CB_PRE_EXTRACT :
  1518. case PCLZIP_CB_POST_EXTRACT :
  1519. case PCLZIP_CB_PRE_ADD :
  1520. case PCLZIP_CB_POST_ADD :
  1521. /* for futur use
  1522. case PCLZIP_CB_PRE_DELETE :
  1523. case PCLZIP_CB_POST_DELETE :
  1524. case PCLZIP_CB_PRE_LIST :
  1525. case PCLZIP_CB_POST_LIST :
  1526. */
  1527. // ----- Check the number of parameters
  1528. if (($i+1) >= $p_size) {
  1529. // ----- Error log
  1530. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1531. // ----- Return
  1532. return PclZip::errorCode();
  1533. }
  1534. // ----- Get the value
  1535. $v_function_name = $p_options_list[$i+1];
  1536. // ----- Check that the value is a valid existing function
  1537. if (!function_exists($v_function_name)) {
  1538. // ----- Error log
  1539. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Function '".$v_function_name."()' is not an existing function for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1540. // ----- Return
  1541. return PclZip::errorCode();
  1542. }
  1543. // ----- Set the attribute
  1544. $v_result_list[$p_options_list[$i]] = $v_function_name;
  1545. $i++;
  1546. break;
  1547. default :
  1548. // ----- Error log
  1549. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
  1550. "Unknown parameter '"
  1551. .$p_options_list[$i]."'");
  1552. // ----- Return
  1553. return PclZip::errorCode();
  1554. }
  1555. // ----- Next options
  1556. $i++;
  1557. }
  1558. // ----- Look for mandatory options
  1559. if ($v_requested_options !== false) {
  1560. for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
  1561. // ----- Look for mandatory option
  1562. if ($v_requested_options[$key] == 'mandatory') {
  1563. // ----- Look if present
  1564. if (!isset($v_result_list[$key])) {
  1565. // ----- Error log
  1566. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
  1567. // ----- Return
  1568. return PclZip::errorCode();
  1569. }
  1570. }
  1571. }
  1572. }
  1573. // ----- Look for default values
  1574. if (!isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
  1575. }
  1576. // ----- Return
  1577. return $v_result;
  1578. }
  1579. // --------------------------------------------------------------------------------
  1580. // --------------------------------------------------------------------------------
  1581. // Function : privOptionDefaultThreshold()
  1582. // Description :
  1583. // Parameters :
  1584. // Return Values :
  1585. // --------------------------------------------------------------------------------
  1586. function privOptionDefaultThreshold(&$p_options)
  1587. {
  1588. $v_result=1;
  1589. if (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
  1590. || isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF])) {
  1591. return $v_result;
  1592. }
  1593. // ----- Get 'memory_limit' configuration value
  1594. $v_memory_limit = ini_get('memory_limit');
  1595. $v_memory_limit = trim($v_memory_limit);
  1596. $last = strtolower(substr($v_memory_limit, -1));
  1597. if($last == 'g')
  1598. //$v_memory_limit = $v_memory_limit*1024*1024*1024;
  1599. $v_memory_limit = $v_memory_limit*1073741824;
  1600. if($last == 'm')
  1601. //$v_memory_limit = $v_memory_limit*1024*1024;
  1602. $v_memory_limit = $v_memory_limit*1048576;
  1603. if($last == 'k')
  1604. $v_memory_limit = $v_memory_limit*1024;
  1605. $p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] = floor($v_memory_limit*PCLZIP_TEMPORARY_FILE_RATIO);
  1606. // ----- Sanity check : No threshold if value lower than 1M
  1607. if ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] < 1048576) {
  1608. unset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]);
  1609. }
  1610. // ----- Return
  1611. return $v_result;
  1612. }
  1613. // --------------------------------------------------------------------------------
  1614. // --------------------------------------------------------------------------------
  1615. // Function : privFileDescrParseAtt()
  1616. // Description :
  1617. // Parameters :
  1618. // Return Values :
  1619. // 1 on success.
  1620. // 0 on failure.
  1621. // --------------------------------------------------------------------------------
  1622. function privFileDescrParseAtt(&$p_file_list, &$p_filedescr, $v_options, $v_requested_options=false)
  1623. {
  1624. $v_result=1;
  1625. // ----- For each file in the list check the attributes
  1626. foreach ($p_file_list as $v_key => $v_value) {
  1627. // ----- Check if the option is supported
  1628. if (!isset($v_requested_options[$v_key])) {
  1629. // ----- Error log
  1630. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file attribute '".$v_key."' for this file");
  1631. // ----- Return
  1632. return PclZip::errorCode();
  1633. }
  1634. // ----- Look for attribute
  1635. switch ($v_key) {
  1636. case PCLZIP_ATT_FILE_NAME :
  1637. if (!is_string($v_value)) {
  1638. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
  1639. return PclZip::errorCode();
  1640. }
  1641. $p_filedescr['filename'] = PclZipUtilPathReduction($v_value);
  1642. if ($p_filedescr['filename'] == '') {
  1643. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty filename for attribute '".PclZipUtilOptionText($v_key)."'");
  1644. return PclZip::errorCode();
  1645. }
  1646. break;
  1647. case PCLZIP_ATT_FILE_NEW_SHORT_NAME :
  1648. if (!is_string($v_value)) {
  1649. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
  1650. return PclZip::errorCode();
  1651. }
  1652. $p_filedescr['new_short_name'] = PclZipUtilPathReduction($v_value);
  1653. if ($p_filedescr['new_short_name'] == '') {
  1654. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty short filename for attribute '".PclZipUtilOptionText($v_key)."'");
  1655. return PclZip::errorCode();
  1656. }
  1657. break;
  1658. case PCLZIP_ATT_FILE_NEW_FULL_NAME :
  1659. if (!is_string($v_value)) {
  1660. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
  1661. return PclZip::errorCode();
  1662. }
  1663. $p_filedescr['new_full_name'] = PclZipUtilPathReduction($v_value);
  1664. if ($p_filedescr['new_full_name'] == '') {
  1665. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty full filename for attribute '".PclZipUtilOptionText($v_key)."'");
  1666. return PclZip::errorCode();
  1667. }
  1668. break;
  1669. // ----- Look for options that takes a string
  1670. case PCLZIP_ATT_FILE_COMMENT :
  1671. if (!is_string($v_value)) {
  1672. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
  1673. return PclZip::errorCode();
  1674. }
  1675. $p_filedescr['comment'] = $v_value;
  1676. break;
  1677. case PCLZIP_ATT_FILE_MTIME :
  1678. if (!is_integer($v_value)) {
  1679. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". Integer expected for attribute '".PclZipUtilOptionText($v_key)."'");
  1680. return PclZip::errorCode();
  1681. }
  1682. $p_filedescr['mtime'] = $v_value;
  1683. break;
  1684. case PCLZIP_ATT_FILE_CONTENT :
  1685. $p_filedescr['content'] = $v_value;
  1686. break;
  1687. default :
  1688. // ----- Error log
  1689. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
  1690. "Unknown parameter '".$v_key."'");
  1691. // ----- Return
  1692. return PclZip::errorCode();
  1693. }
  1694. // ----- Look for mandatory options
  1695. if ($v_requested_options !== false) {
  1696. for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
  1697. // ----- Look for mandatory option
  1698. if ($v_requested_options[$key] == 'mandatory') {
  1699. // ----- Look if present
  1700. if (!isset($p_file_list[$key])) {
  1701. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
  1702. return PclZip::errorCode();
  1703. }
  1704. }
  1705. }
  1706. }
  1707. // end foreach
  1708. }
  1709. // ----- Return
  1710. return $v_result;
  1711. }
  1712. // --------------------------------------------------------------------------------
  1713. // --------------------------------------------------------------------------------
  1714. // Function : privFileDescrExpand()
  1715. // Description :
  1716. // This method look for each item of the list to see if its a file, a folder
  1717. // or a string to be added as file. For any other type of files (link, other)
  1718. // just ignore the item.
  1719. // Then prepare the information that will be stored for that file.
  1720. // When its a folder, expand the folder with all the files that are in that
  1721. // folder (recursively).
  1722. // Parameters :
  1723. // Return Values :
  1724. // 1 on success.
  1725. // 0 on failure.
  1726. // --------------------------------------------------------------------------------
  1727. function privFileDescrExpand(&$p_filedescr_list, &$p_options)
  1728. {
  1729. $v_result=1;
  1730. // ----- Create a result list
  1731. $v_result_list = array();
  1732. // ----- Look each entry
  1733. for ($i=0; $i<sizeof($p_filedescr_list); $i++) {
  1734. // ----- Get filedescr
  1735. $v_descr = $p_filedescr_list[$i];
  1736. // ----- Reduce the filename
  1737. $v_descr['filename'] = PclZipUtilTranslateWinPath($v_descr['filename'], false);
  1738. $v_descr['filename'] = PclZipUtilPathReduction($v_descr['filename']);
  1739. // ----- Look for real file or folder
  1740. if (file_exists($v_descr['filename'])) {
  1741. if (@is_file($v_descr['filename'])) {
  1742. $v_descr['type'] = 'file';
  1743. }
  1744. else if (@is_dir($v_descr['filename'])) {
  1745. $v_descr['type'] = 'folder';
  1746. }
  1747. else if (@is_link($v_descr['filename'])) {
  1748. // skip
  1749. continue;
  1750. }
  1751. else {
  1752. // skip
  1753. continue;
  1754. }
  1755. }
  1756. // ----- Look for string added as file
  1757. else if (isset($v_descr['content'])) {
  1758. $v_descr['type'] = 'virtual_file';
  1759. }
  1760. // ----- Missing file
  1761. else {
  1762. // ----- Error log
  1763. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '".$v_descr['filename']."' does not exist");
  1764. // ----- Return
  1765. return PclZip::errorCode();
  1766. }
  1767. // ----- Calculate the stored filename
  1768. $this->privCalculateStoredFilename($v_descr, $p_options);
  1769. // ----- Add the descriptor in result list
  1770. $v_result_list[sizeof($v_result_list)] = $v_descr;
  1771. // ----- Look for folder
  1772. if ($v_descr['type'] == 'folder') {
  1773. // ----- List of items in folder
  1774. $v_dirlist_descr = array();
  1775. $v_dirlist_nb = 0;
  1776. if ($v_folder_handler = @opendir($v_descr['filename'])) {
  1777. while (($v_item_handler = @readdir($v_folder_handler)) !== false) {
  1778. // ----- Skip '.' and '..'
  1779. if (($v_item_handler == '.') || ($v_item_handler == '..')) {
  1780. continue;
  1781. }
  1782. // ----- Compose the full filename
  1783. $v_dirlist_descr[$v_dirlist_nb]['filename'] = $v_descr['filename'].'/'.$v_item_handler;
  1784. // ----- Look for different stored filename
  1785. // Because the name of the folder was changed, the name of the
  1786. // files/sub-folders also change
  1787. if (($v_descr['stored_filename'] != $v_descr['filename'])
  1788. && (!isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH]))) {
  1789. if ($v_descr['stored_filename'] != '') {
  1790. $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_descr['stored_filename'].'/'.$v_item_handler;
  1791. }
  1792. else {
  1793. $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_item_handler;
  1794. }
  1795. }
  1796. $v_dirlist_nb++;
  1797. }
  1798. @closedir($v_folder_handler);
  1799. }
  1800. else {
  1801. // TBC : unable to open folder in read mode
  1802. }
  1803. // ----- Expand each element of the list
  1804. if ($v_dirlist_nb != 0) {
  1805. // ----- Expand
  1806. if (($v_result = $this->privFileDescrExpand($v_dirlist_descr, $p_options)) != 1) {
  1807. return $v_result;
  1808. }
  1809. // ----- Concat the resulting list
  1810. $v_result_list = array_merge($v_result_list, $v_dirlist_descr);
  1811. }
  1812. else {
  1813. }
  1814. // ----- Free local array
  1815. unset($v_dirlist_descr);
  1816. }
  1817. }
  1818. // ----- Get the result list
  1819. $p_filedescr_list = $v_result_list;
  1820. // ----- Return
  1821. return $v_result;
  1822. }
  1823. // --------------------------------------------------------------------------------
  1824. // --------------------------------------------------------------------------------
  1825. // Function : privCreate()
  1826. // Description :
  1827. // Parameters :
  1828. // Return Values :
  1829. // --------------------------------------------------------------------------------
  1830. function privCreate($p_filedescr_list, &$p_result_list, &$p_options)
  1831. {
  1832. $v_result=1;
  1833. $v_list_detail = array();
  1834. // ----- Magic quotes trick
  1835. $this->privDisableMagicQuotes();
  1836. // ----- Open the file in write mode
  1837. if (($v_result = $this->privOpenFd('wb')) != 1)
  1838. {
  1839. // ----- Return
  1840. return $v_result;
  1841. }
  1842. // ----- Add the list of files
  1843. $v_result = $this->privAddList($p_filedescr_list, $p_result_list, $p_options);
  1844. // ----- Close
  1845. $this->privCloseFd();
  1846. // ----- Magic quotes trick
  1847. $this->privSwapBackMagicQuotes();
  1848. // ----- Return
  1849. return $v_result;
  1850. }
  1851. // --------------------------------------------------------------------------------
  1852. // --------------------------------------------------------------------------------
  1853. // Function : privAdd()
  1854. // Description :
  1855. // Parameters :
  1856. // Return Values :
  1857. // --------------------------------------------------------------------------------
  1858. function privAdd($p_filedescr_list, &$p_result_list, &$p_options)
  1859. {
  1860. $v_result=1;
  1861. $v_list_detail = array();
  1862. // ----- Look if the archive exists or is empty
  1863. if ((!is_file($this->zipname)) || (filesize($this->zipname) == 0))
  1864. {
  1865. // ----- Do a create
  1866. $v_result = $this->privCreate($p_filedescr_list, $p_result_list, $p_options);
  1867. // ----- Return
  1868. return $v_result;
  1869. }
  1870. // ----- Magic quotes trick
  1871. $this->privDisableMagicQuotes();
  1872. // ----- Open the zip file
  1873. if (($v_result=$this->privOpenFd('rb')) != 1)
  1874. {
  1875. // ----- Magic quotes trick
  1876. $this->privSwapBackMagicQuotes();
  1877. // ----- Return
  1878. return $v_result;
  1879. }
  1880. // ----- Read the central directory informations
  1881. $v_central_dir = array();
  1882. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  1883. {
  1884. $this->privCloseFd();
  1885. $this->privSwapBackMagicQuotes();
  1886. return $v_result;
  1887. }
  1888. // ----- Go to beginning of File
  1889. @rewind($this->zip_fd);
  1890. // ----- Creates a temporay file
  1891. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
  1892. // ----- Open the temporary file in write mode
  1893. if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
  1894. {
  1895. $this->privCloseFd();
  1896. $this->privSwapBackMagicQuotes();
  1897. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
  1898. // ----- Return
  1899. return PclZip::errorCode();
  1900. }
  1901. // ----- Copy the files from the archive to the temporary file
  1902. // TBC : Here I should better append the file and go back to erase the central dir
  1903. $v_size = $v_central_dir['offset'];
  1904. while ($v_size != 0)
  1905. {
  1906. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1907. $v_buffer = fread($this->zip_fd, $v_read_size);
  1908. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  1909. $v_size -= $v_read_size;
  1910. }
  1911. // ----- Swap the file descriptor
  1912. // Here is a trick : I swap the temporary fd with the zip fd, in order to use
  1913. // the following methods on the temporary fil and not the real archive
  1914. $v_swap = $this->zip_fd;
  1915. $this->zip_fd = $v_zip_temp_fd;
  1916. $v_zip_temp_fd = $v_swap;
  1917. // ----- Add the files
  1918. $v_header_list = array();
  1919. if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1)
  1920. {
  1921. fclose($v_zip_temp_fd);
  1922. $this->privCloseFd();
  1923. @unlink($v_zip_temp_name);
  1924. $this->privSwapBackMagicQuotes();
  1925. // ----- Return
  1926. return $v_result;
  1927. }
  1928. // ----- Store the offset of the central dir
  1929. $v_offset = @ftell($this->zip_fd);
  1930. // ----- Copy the block of file headers from the old archive
  1931. $v_size = $v_central_dir['size'];
  1932. while ($v_size != 0)
  1933. {
  1934. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1935. $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
  1936. @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  1937. $v_size -= $v_read_size;
  1938. }
  1939. // ----- Create the Central Dir files header
  1940. for ($i=0, $v_count=0; $i<sizeof($v_header_list); $i++)
  1941. {
  1942. // ----- Create the file header
  1943. if ($v_header_list[$i]['status'] == 'ok') {
  1944. if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  1945. fclose($v_zip_temp_fd);
  1946. $this->privCloseFd();
  1947. @unlink($v_zip_temp_name);
  1948. $this->privSwapBackMagicQuotes();
  1949. // ----- Return
  1950. return $v_result;
  1951. }
  1952. $v_count++;
  1953. }
  1954. // ----- Transform the header to a 'usable' info
  1955. $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  1956. }
  1957. // ----- Zip file comment
  1958. $v_comment = $v_central_dir['comment'];
  1959. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  1960. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  1961. }
  1962. if (isset($p_options[PCLZIP_OPT_ADD_COMMENT])) {
  1963. $v_comment = $v_comment.$p_options[PCLZIP_OPT_ADD_COMMENT];
  1964. }
  1965. if (isset($p_options[PCLZIP_OPT_PREPEND_COMMENT])) {
  1966. $v_comment = $p_options[PCLZIP_OPT_PREPEND_COMMENT].$v_comment;
  1967. }
  1968. // ----- Calculate the size of the central header
  1969. $v_size = @ftell($this->zip_fd)-$v_offset;
  1970. // ----- Create the central dir footer
  1971. if (($v_result = $this->privWriteCentralHeader($v_count+$v_central_dir['entries'], $v_size, $v_offset, $v_comment)) != 1)
  1972. {
  1973. // ----- Reset the file list
  1974. unset($v_header_list);
  1975. $this->privSwapBackMagicQuotes();
  1976. // ----- Return
  1977. return $v_result;
  1978. }
  1979. // ----- Swap back the file descriptor
  1980. $v_swap = $this->zip_fd;
  1981. $this->zip_fd = $v_zip_temp_fd;
  1982. $v_zip_temp_fd = $v_swap;
  1983. // ----- Close
  1984. $this->privCloseFd();
  1985. // ----- Close the temporary file
  1986. @fclose($v_zip_temp_fd);
  1987. // ----- Magic quotes trick
  1988. $this->privSwapBackMagicQuotes();
  1989. // ----- Delete the zip file
  1990. // TBC : I should test the result ...
  1991. @unlink($this->zipname);
  1992. // ----- Rename the temporary file
  1993. // TBC : I should test the result ...
  1994. //@rename($v_zip_temp_name, $this->zipname);
  1995. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  1996. // ----- Return
  1997. return $v_result;
  1998. }
  1999. // --------------------------------------------------------------------------------
  2000. // --------------------------------------------------------------------------------
  2001. // Function : privOpenFd()
  2002. // Description :
  2003. // Parameters :
  2004. // --------------------------------------------------------------------------------
  2005. function privOpenFd($p_mode)
  2006. {
  2007. $v_result=1;
  2008. // ----- Look if already open
  2009. if ($this->zip_fd != 0)
  2010. {
  2011. // ----- Error log
  2012. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Zip file \''.$this->zipname.'\' already open');
  2013. // ----- Return
  2014. return PclZip::errorCode();
  2015. }
  2016. // ----- Open the zip file
  2017. if (($this->zip_fd = @fopen($this->zipname, $p_mode)) == 0)
  2018. {
  2019. // ----- Error log
  2020. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in '.$p_mode.' mode');
  2021. // ----- Return
  2022. return PclZip::errorCode();
  2023. }
  2024. // ----- Return
  2025. return $v_result;
  2026. }
  2027. // --------------------------------------------------------------------------------
  2028. // --------------------------------------------------------------------------------
  2029. // Function : privCloseFd()
  2030. // Description :
  2031. // Parameters :
  2032. // --------------------------------------------------------------------------------
  2033. function privCloseFd()
  2034. {
  2035. $v_result=1;
  2036. if ($this->zip_fd != 0)
  2037. @fclose($this->zip_fd);
  2038. $this->zip_fd = 0;
  2039. // ----- Return
  2040. return $v_result;
  2041. }
  2042. // --------------------------------------------------------------------------------
  2043. // --------------------------------------------------------------------------------
  2044. // Function : privAddList()
  2045. // Description :
  2046. // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
  2047. // different from the real path of the file. This is usefull if you want to have PclTar
  2048. // running in any directory, and memorize relative path from an other directory.
  2049. // Parameters :
  2050. // $p_list : An array containing the file or directory names to add in the tar
  2051. // $p_result_list : list of added files with their properties (specially the status field)
  2052. // $p_add_dir : Path to add in the filename path archived
  2053. // $p_remove_dir : Path to remove in the filename path archived
  2054. // Return Values :
  2055. // --------------------------------------------------------------------------------
  2056. // function privAddList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
  2057. function privAddList($p_filedescr_list, &$p_result_list, &$p_options)
  2058. {
  2059. $v_result=1;
  2060. // ----- Add the files
  2061. $v_header_list = array();
  2062. if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1)
  2063. {
  2064. // ----- Return
  2065. return $v_result;
  2066. }
  2067. // ----- Store the offset of the central dir
  2068. $v_offset = @ftell($this->zip_fd);
  2069. // ----- Create the Central Dir files header
  2070. for ($i=0,$v_count=0; $i<sizeof($v_header_list); $i++)
  2071. {
  2072. // ----- Create the file header
  2073. if ($v_header_list[$i]['status'] == 'ok') {
  2074. if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  2075. // ----- Return
  2076. return $v_result;
  2077. }
  2078. $v_count++;
  2079. }
  2080. // ----- Transform the header to a 'usable' info
  2081. $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  2082. }
  2083. // ----- Zip file comment
  2084. $v_comment = '';
  2085. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  2086. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  2087. }
  2088. // ----- Calculate the size of the central header
  2089. $v_size = @ftell($this->zip_fd)-$v_offset;
  2090. // ----- Create the central dir footer
  2091. if (($v_result = $this->privWriteCentralHeader($v_count, $v_size, $v_offset, $v_comment)) != 1)
  2092. {
  2093. // ----- Reset the file list
  2094. unset($v_header_list);
  2095. // ----- Return
  2096. return $v_result;
  2097. }
  2098. // ----- Return
  2099. return $v_result;
  2100. }
  2101. // --------------------------------------------------------------------------------
  2102. // --------------------------------------------------------------------------------
  2103. // Function : privAddFileList()
  2104. // Description :
  2105. // Parameters :
  2106. // $p_filedescr_list : An array containing the file description
  2107. // or directory names to add in the zip
  2108. // $p_result_list : list of added files with their properties (specially the status field)
  2109. // Return Values :
  2110. // --------------------------------------------------------------------------------
  2111. function privAddFileList($p_filedescr_list, &$p_result_list, &$p_options)
  2112. {
  2113. $v_result=1;
  2114. $v_header = array();
  2115. // ----- Recuperate the current number of elt in list
  2116. $v_nb = sizeof($p_result_list);
  2117. // ----- Loop on the files
  2118. for ($j=0; ($j<sizeof($p_filedescr_list)) && ($v_result==1); $j++) {
  2119. // ----- Format the filename
  2120. $p_filedescr_list[$j]['filename']
  2121. = PclZipUtilTranslateWinPath($p_filedescr_list[$j]['filename'], false);
  2122. // ----- Skip empty file names
  2123. // TBC : Can this be possible ? not checked in DescrParseAtt ?
  2124. if ($p_filedescr_list[$j]['filename'] == "") {
  2125. continue;
  2126. }
  2127. // ----- Check the filename
  2128. if ( ($p_filedescr_list[$j]['type'] != 'virtual_file')
  2129. && (!file_exists($p_filedescr_list[$j]['filename']))) {
  2130. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '".$p_filedescr_list[$j]['filename']."' does not exist");
  2131. return PclZip::errorCode();
  2132. }
  2133. // ----- Look if it is a file or a dir with no all path remove option
  2134. // or a dir with all its path removed
  2135. // if ( (is_file($p_filedescr_list[$j]['filename']))
  2136. // || ( is_dir($p_filedescr_list[$j]['filename'])
  2137. if ( ($p_filedescr_list[$j]['type'] == 'file')
  2138. || ($p_filedescr_list[$j]['type'] == 'virtual_file')
  2139. || ( ($p_filedescr_list[$j]['type'] == 'folder')
  2140. && ( !isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])
  2141. || !$p_options[PCLZIP_OPT_REMOVE_ALL_PATH]))
  2142. ) {
  2143. // ----- Add the file
  2144. $v_result = $this->privAddFile($p_filedescr_list[$j], $v_header,
  2145. $p_options);
  2146. if ($v_result != 1) {
  2147. return $v_result;
  2148. }
  2149. // ----- Store the file infos
  2150. $p_result_list[$v_nb++] = $v_header;
  2151. }
  2152. }
  2153. // ----- Return
  2154. return $v_result;
  2155. }
  2156. // --------------------------------------------------------------------------------
  2157. // --------------------------------------------------------------------------------
  2158. // Function : privAddFile()
  2159. // Description :
  2160. // Parameters :
  2161. // Return Values :
  2162. // --------------------------------------------------------------------------------
  2163. function privAddFile($p_filedescr, &$p_header, &$p_options)
  2164. {
  2165. $v_result=1;
  2166. // ----- Working variable
  2167. $p_filename = $p_filedescr['filename'];
  2168. // TBC : Already done in the fileAtt check ... ?
  2169. if ($p_filename == "") {
  2170. // ----- Error log
  2171. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
  2172. // ----- Return
  2173. return PclZip::errorCode();
  2174. }
  2175. // ----- Look for a stored different filename
  2176. /* TBC : Removed
  2177. if (isset($p_filedescr['stored_filename'])) {
  2178. $v_stored_filename = $p_filedescr['stored_filename'];
  2179. }
  2180. else {
  2181. $v_stored_filename = $p_filedescr['stored_filename'];
  2182. }
  2183. */
  2184. // ----- Set the file properties
  2185. clearstatcache();
  2186. $p_header['version'] = 20;
  2187. $p_header['version_extracted'] = 10;
  2188. $p_header['flag'] = 0;
  2189. $p_header['compression'] = 0;
  2190. $p_header['crc'] = 0;
  2191. $p_header['compressed_size'] = 0;
  2192. $p_header['filename_len'] = strlen($p_filename);
  2193. $p_header['extra_len'] = 0;
  2194. $p_header['disk'] = 0;
  2195. $p_header['internal'] = 0;
  2196. $p_header['offset'] = 0;
  2197. $p_header['filename'] = $p_filename;
  2198. // TBC : Removed $p_header['stored_filename'] = $v_stored_filename;
  2199. $p_header['stored_filename'] = $p_filedescr['stored_filename'];
  2200. $p_header['extra'] = '';
  2201. $p_header['status'] = 'ok';
  2202. $p_header['index'] = -1;
  2203. // ----- Look for regular file
  2204. if ($p_filedescr['type']=='file') {
  2205. $p_header['external'] = 0x00000000;
  2206. $p_header['size'] = filesize($p_filename);
  2207. }
  2208. // ----- Look for regular folder
  2209. else if ($p_filedescr['type']=='folder') {
  2210. $p_header['external'] = 0x00000010;
  2211. $p_header['mtime'] = filemtime($p_filename);
  2212. $p_header['size'] = filesize($p_filename);
  2213. }
  2214. // ----- Look for virtual file
  2215. else if ($p_filedescr['type'] == 'virtual_file') {
  2216. $p_header['external'] = 0x00000000;
  2217. $p_header['size'] = strlen($p_filedescr['content']);
  2218. }
  2219. // ----- Look for filetime
  2220. if (isset($p_filedescr['mtime'])) {
  2221. $p_header['mtime'] = $p_filedescr['mtime'];
  2222. }
  2223. else if ($p_filedescr['type'] == 'virtual_file') {
  2224. $p_header['mtime'] = time();
  2225. }
  2226. else {
  2227. $p_header['mtime'] = filemtime($p_filename);
  2228. }
  2229. // ------ Look for file comment
  2230. if (isset($p_filedescr['comment'])) {
  2231. $p_header['comment_len'] = strlen($p_filedescr['comment']);
  2232. $p_header['comment'] = $p_filedescr['comment'];
  2233. }
  2234. else {
  2235. $p_header['comment_len'] = 0;
  2236. $p_header['comment'] = '';
  2237. }
  2238. // ----- Look for pre-add callback
  2239. if (isset($p_options[PCLZIP_CB_PRE_ADD])) {
  2240. // ----- Generate a local information
  2241. $v_local_header = array();
  2242. $this->privConvertHeader2FileInfo($p_header, $v_local_header);
  2243. // ----- Call the callback
  2244. // Here I do not use call_user_func() because I need to send a reference to the
  2245. // header.
  2246. // eval('$v_result = '.$p_options[PCLZIP_CB_PRE_ADD].'(PCLZIP_CB_PRE_ADD, $v_local_header);');
  2247. $v_result = $p_options[PCLZIP_CB_PRE_ADD](PCLZIP_CB_PRE_ADD, $v_local_header);
  2248. if ($v_result == 0) {
  2249. // ----- Change the file status
  2250. $p_header['status'] = "skipped";
  2251. $v_result = 1;
  2252. }
  2253. // ----- Update the informations
  2254. // Only some fields can be modified
  2255. if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
  2256. $p_header['stored_filename'] = PclZipUtilPathReduction($v_local_header['stored_filename']);
  2257. }
  2258. }
  2259. // ----- Look for empty stored filename
  2260. if ($p_header['stored_filename'] == "") {
  2261. $p_header['status'] = "filtered";
  2262. }
  2263. // ----- Check the path length
  2264. if (strlen($p_header['stored_filename']) > 0xFF) {
  2265. $p_header['status'] = 'filename_too_long';
  2266. }
  2267. // ----- Look if no error, or file not skipped
  2268. if ($p_header['status'] == 'ok') {
  2269. // ----- Look for a file
  2270. if ($p_filedescr['type'] == 'file') {
  2271. // ----- Look for using temporary file to zip
  2272. if ( (!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF]))
  2273. && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON])
  2274. || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
  2275. && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_header['size'])) ) ) {
  2276. $v_result = $this->privAddFileUsingTempFile($p_filedescr, $p_header, $p_options);
  2277. if ($v_result < PCLZIP_ERR_NO_ERROR) {
  2278. return $v_result;
  2279. }
  2280. }
  2281. // ----- Use "in memory" zip algo
  2282. else {
  2283. // ----- Open the source file
  2284. if (($v_file = @fopen($p_filename, "rb")) == 0) {
  2285. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
  2286. return PclZip::errorCode();
  2287. }
  2288. // ----- Read the file content
  2289. $v_content = @fread($v_file, $p_header['size']);
  2290. // ----- Close the file
  2291. @fclose($v_file);
  2292. // ----- Calculate the CRC
  2293. $p_header['crc'] = @crc32($v_content);
  2294. // ----- Look for no compression
  2295. if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
  2296. // ----- Set header parameters
  2297. $p_header['compressed_size'] = $p_header['size'];
  2298. $p_header['compression'] = 0;
  2299. }
  2300. // ----- Look for normal compression
  2301. else {
  2302. // ----- Compress the content
  2303. $v_content = @gzdeflate($v_content);
  2304. // ----- Set header parameters
  2305. $p_header['compressed_size'] = strlen($v_content);
  2306. $p_header['compression'] = 8;
  2307. }
  2308. // ----- Call the header generation
  2309. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  2310. @fclose($v_file);
  2311. return $v_result;
  2312. }
  2313. // ----- Write the compressed (or not) content
  2314. @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
  2315. }
  2316. }
  2317. // ----- Look for a virtual file (a file from string)
  2318. else if ($p_filedescr['type'] == 'virtual_file') {
  2319. $v_content = $p_filedescr['content'];
  2320. // ----- Calculate the CRC
  2321. $p_header['crc'] = @crc32($v_content);
  2322. // ----- Look for no compression
  2323. if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
  2324. // ----- Set header parameters
  2325. $p_header['compressed_size'] = $p_header['size'];
  2326. $p_header['compression'] = 0;
  2327. }
  2328. // ----- Look for normal compression
  2329. else {
  2330. // ----- Compress the content
  2331. $v_content = @gzdeflate($v_content);
  2332. // ----- Set header parameters
  2333. $p_header['compressed_size'] = strlen($v_content);
  2334. $p_header['compression'] = 8;
  2335. }
  2336. // ----- Call the header generation
  2337. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  2338. @fclose($v_file);
  2339. return $v_result;
  2340. }
  2341. // ----- Write the compressed (or not) content
  2342. @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
  2343. }
  2344. // ----- Look for a directory
  2345. else if ($p_filedescr['type'] == 'folder') {
  2346. // ----- Look for directory last '/'
  2347. if (@substr($p_header['stored_filename'], -1) != '/') {
  2348. $p_header['stored_filename'] .= '/';
  2349. }
  2350. // ----- Set the file properties
  2351. $p_header['size'] = 0;
  2352. //$p_header['external'] = 0x41FF0010; // Value for a folder : to be checked
  2353. $p_header['external'] = 0x00000010; // Value for a folder : to be checked
  2354. // ----- Call the header generation
  2355. if (($v_result = $this->privWriteFileHeader($p_header)) != 1)
  2356. {
  2357. return $v_result;
  2358. }
  2359. }
  2360. }
  2361. // ----- Look for post-add callback
  2362. if (isset($p_options[PCLZIP_CB_POST_ADD])) {
  2363. // ----- Generate a local information
  2364. $v_local_header = array();
  2365. $this->privConvertHeader2FileInfo($p_header, $v_local_header);
  2366. // ----- Call the callback
  2367. // Here I do not use call_user_func() because I need to send a reference to the
  2368. // header.
  2369. // eval('$v_result = '.$p_options[PCLZIP_CB_POST_ADD].'(PCLZIP_CB_POST_ADD, $v_local_header);');
  2370. $v_result = $p_options[PCLZIP_CB_POST_ADD](PCLZIP_CB_POST_ADD, $v_local_header);
  2371. if ($v_result == 0) {
  2372. // ----- Ignored
  2373. $v_result = 1;
  2374. }
  2375. // ----- Update the informations
  2376. // Nothing can be modified
  2377. }
  2378. // ----- Return
  2379. return $v_result;
  2380. }
  2381. // --------------------------------------------------------------------------------
  2382. // --------------------------------------------------------------------------------
  2383. // Function : privAddFileUsingTempFile()
  2384. // Description :
  2385. // Parameters :
  2386. // Return Values :
  2387. // --------------------------------------------------------------------------------
  2388. function privAddFileUsingTempFile($p_filedescr, &$p_header, &$p_options)
  2389. {
  2390. $v_result=PCLZIP_ERR_NO_ERROR;
  2391. // ----- Working variable
  2392. $p_filename = $p_filedescr['filename'];
  2393. // ----- Open the source file
  2394. if (($v_file = @fopen($p_filename, "rb")) == 0) {
  2395. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
  2396. return PclZip::errorCode();
  2397. }
  2398. // ----- Creates a compressed temporary file
  2399. $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.gz';
  2400. if (($v_file_compressed = @gzopen($v_gzip_temp_name, "wb")) == 0) {
  2401. fclose($v_file);
  2402. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary write mode');
  2403. return PclZip::errorCode();
  2404. }
  2405. // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  2406. $v_size = filesize($p_filename);
  2407. while ($v_size != 0) {
  2408. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2409. $v_buffer = @fread($v_file, $v_read_size);
  2410. //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
  2411. @gzputs($v_file_compressed, $v_buffer, $v_read_size);
  2412. $v_size -= $v_read_size;
  2413. }
  2414. // ----- Close the file
  2415. @fclose($v_file);
  2416. @gzclose($v_file_compressed);
  2417. // ----- Check the minimum file size
  2418. if (filesize($v_gzip_temp_name) < 18) {
  2419. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'gzip temporary file \''.$v_gzip_temp_name.'\' has invalid filesize - should be minimum 18 bytes');
  2420. return PclZip::errorCode();
  2421. }
  2422. // ----- Extract the compressed attributes
  2423. if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0) {
  2424. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
  2425. return PclZip::errorCode();
  2426. }
  2427. // ----- Read the gzip file header
  2428. $v_binary_data = @fread($v_file_compressed, 10);
  2429. $v_data_header = unpack('a1id1/a1id2/a1cm/a1flag/Vmtime/a1xfl/a1os', $v_binary_data);
  2430. // ----- Check some parameters
  2431. $v_data_header['os'] = bin2hex($v_data_header['os']);
  2432. // ----- Read the gzip file footer
  2433. @fseek($v_file_compressed, filesize($v_gzip_temp_name)-8);
  2434. $v_binary_data = @fread($v_file_compressed, 8);
  2435. $v_data_footer = unpack('Vcrc/Vcompressed_size', $v_binary_data);
  2436. // ----- Set the attributes
  2437. $p_header['compression'] = ord($v_data_header['cm']);
  2438. //$p_header['mtime'] = $v_data_header['mtime'];
  2439. $p_header['crc'] = $v_data_footer['crc'];
  2440. $p_header['compressed_size'] = filesize($v_gzip_temp_name)-18;
  2441. // ----- Close the file
  2442. @fclose($v_file_compressed);
  2443. // ----- Call the header generation
  2444. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  2445. return $v_result;
  2446. }
  2447. // ----- Add the compressed data
  2448. if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0)
  2449. {
  2450. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
  2451. return PclZip::errorCode();
  2452. }
  2453. // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  2454. fseek($v_file_compressed, 10);
  2455. $v_size = $p_header['compressed_size'];
  2456. while ($v_size != 0)
  2457. {
  2458. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2459. $v_buffer = @fread($v_file_compressed, $v_read_size);
  2460. //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
  2461. @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  2462. $v_size -= $v_read_size;
  2463. }
  2464. // ----- Close the file
  2465. @fclose($v_file_compressed);
  2466. // ----- Unlink the temporary file
  2467. @unlink($v_gzip_temp_name);
  2468. // ----- Return
  2469. return $v_result;
  2470. }
  2471. // --------------------------------------------------------------------------------
  2472. // --------------------------------------------------------------------------------
  2473. // Function : privCalculateStoredFilename()
  2474. // Description :
  2475. // Based on file descriptor properties and global options, this method
  2476. // calculate the filename that will be stored in the archive.
  2477. // Parameters :
  2478. // Return Values :
  2479. // --------------------------------------------------------------------------------
  2480. function privCalculateStoredFilename(&$p_filedescr, &$p_options)
  2481. {
  2482. $v_result=1;
  2483. // ----- Working variables
  2484. $p_filename = $p_filedescr['filename'];
  2485. if (isset($p_options[PCLZIP_OPT_ADD_PATH])) {
  2486. $p_add_dir = $p_options[PCLZIP_OPT_ADD_PATH];
  2487. }
  2488. else {
  2489. $p_add_dir = '';
  2490. }
  2491. if (isset($p_options[PCLZIP_OPT_REMOVE_PATH])) {
  2492. $p_remove_dir = $p_options[PCLZIP_OPT_REMOVE_PATH];
  2493. }
  2494. else {
  2495. $p_remove_dir = '';
  2496. }
  2497. if (isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  2498. $p_remove_all_dir = $p_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  2499. }
  2500. else {
  2501. $p_remove_all_dir = 0;
  2502. }
  2503. // ----- Look for full name change
  2504. if (isset($p_filedescr['new_full_name'])) {
  2505. // ----- Remove drive letter if any
  2506. $v_stored_filename = PclZipUtilTranslateWinPath($p_filedescr['new_full_name']);
  2507. }
  2508. // ----- Look for path and/or short name change
  2509. else {
  2510. // ----- Look for short name change
  2511. // Its when we cahnge just the filename but not the path
  2512. if (isset($p_filedescr['new_short_name'])) {
  2513. $v_path_info = pathinfo($p_filename);
  2514. $v_dir = '';
  2515. if ($v_path_info['dirname'] != '') {
  2516. $v_dir = $v_path_info['dirname'].'/';
  2517. }
  2518. $v_stored_filename = $v_dir.$p_filedescr['new_short_name'];
  2519. }
  2520. else {
  2521. // ----- Calculate the stored filename
  2522. $v_stored_filename = $p_filename;
  2523. }
  2524. // ----- Look for all path to remove
  2525. if ($p_remove_all_dir) {
  2526. $v_stored_filename = basename($p_filename);
  2527. }
  2528. // ----- Look for partial path remove
  2529. else if ($p_remove_dir != "") {
  2530. if (substr($p_remove_dir, -1) != '/')
  2531. $p_remove_dir .= "/";
  2532. if ( (substr($p_filename, 0, 2) == "./")
  2533. || (substr($p_remove_dir, 0, 2) == "./")) {
  2534. if ( (substr($p_filename, 0, 2) == "./")
  2535. && (substr($p_remove_dir, 0, 2) != "./")) {
  2536. $p_remove_dir = "./".$p_remove_dir;
  2537. }
  2538. if ( (substr($p_filename, 0, 2) != "./")
  2539. && (substr($p_remove_dir, 0, 2) == "./")) {
  2540. $p_remove_dir = substr($p_remove_dir, 2);
  2541. }
  2542. }
  2543. $v_compare = PclZipUtilPathInclusion($p_remove_dir,
  2544. $v_stored_filename);
  2545. if ($v_compare > 0) {
  2546. if ($v_compare == 2) {
  2547. $v_stored_filename = "";
  2548. }
  2549. else {
  2550. $v_stored_filename = substr($v_stored_filename,
  2551. strlen($p_remove_dir));
  2552. }
  2553. }
  2554. }
  2555. // ----- Remove drive letter if any
  2556. $v_stored_filename = PclZipUtilTranslateWinPath($v_stored_filename);
  2557. // ----- Look for path to add
  2558. if ($p_add_dir != "") {
  2559. if (substr($p_add_dir, -1) == "/")
  2560. $v_stored_filename = $p_add_dir.$v_stored_filename;
  2561. else
  2562. $v_stored_filename = $p_add_dir."/".$v_stored_filename;
  2563. }
  2564. }
  2565. // ----- Filename (reduce the path of stored name)
  2566. $v_stored_filename = PclZipUtilPathReduction($v_stored_filename);
  2567. $p_filedescr['stored_filename'] = $v_stored_filename;
  2568. // ----- Return
  2569. return $v_result;
  2570. }
  2571. // --------------------------------------------------------------------------------
  2572. // --------------------------------------------------------------------------------
  2573. // Function : privWriteFileHeader()
  2574. // Description :
  2575. // Parameters :
  2576. // Return Values :
  2577. // --------------------------------------------------------------------------------
  2578. function privWriteFileHeader(&$p_header)
  2579. {
  2580. $v_result=1;
  2581. // ----- Store the offset position of the file
  2582. $p_header['offset'] = ftell($this->zip_fd);
  2583. // ----- Transform UNIX mtime to DOS format mdate/mtime
  2584. $v_date = getdate($p_header['mtime']);
  2585. $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
  2586. $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
  2587. // ----- Packed data
  2588. $v_binary_data = pack("VvvvvvVVVvv", 0x04034b50,
  2589. $p_header['version_extracted'], $p_header['flag'],
  2590. $p_header['compression'], $v_mtime, $v_mdate,
  2591. $p_header['crc'], $p_header['compressed_size'],
  2592. $p_header['size'],
  2593. strlen($p_header['stored_filename']),
  2594. $p_header['extra_len']);
  2595. // ----- Write the first 148 bytes of the header in the archive
  2596. fputs($this->zip_fd, $v_binary_data, 30);
  2597. // ----- Write the variable fields
  2598. if (strlen($p_header['stored_filename']) != 0)
  2599. {
  2600. fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
  2601. }
  2602. if ($p_header['extra_len'] != 0)
  2603. {
  2604. fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
  2605. }
  2606. // ----- Return
  2607. return $v_result;
  2608. }
  2609. // --------------------------------------------------------------------------------
  2610. // --------------------------------------------------------------------------------
  2611. // Function : privWriteCentralFileHeader()
  2612. // Description :
  2613. // Parameters :
  2614. // Return Values :
  2615. // --------------------------------------------------------------------------------
  2616. function privWriteCentralFileHeader(&$p_header)
  2617. {
  2618. $v_result=1;
  2619. // TBC
  2620. //for(reset($p_header); $key = key($p_header); next($p_header)) {
  2621. //}
  2622. // ----- Transform UNIX mtime to DOS format mdate/mtime
  2623. $v_date = getdate($p_header['mtime']);
  2624. $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
  2625. $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
  2626. // ----- Packed data
  2627. $v_binary_data = pack("VvvvvvvVVVvvvvvVV", 0x02014b50,
  2628. $p_header['version'], $p_header['version_extracted'],
  2629. $p_header['flag'], $p_header['compression'],
  2630. $v_mtime, $v_mdate, $p_header['crc'],
  2631. $p_header['compressed_size'], $p_header['size'],
  2632. strlen($p_header['stored_filename']),
  2633. $p_header['extra_len'], $p_header['comment_len'],
  2634. $p_header['disk'], $p_header['internal'],
  2635. $p_header['external'], $p_header['offset']);
  2636. // ----- Write the 42 bytes of the header in the zip file
  2637. fputs($this->zip_fd, $v_binary_data, 46);
  2638. // ----- Write the variable fields
  2639. if (strlen($p_header['stored_filename']) != 0)
  2640. {
  2641. fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
  2642. }
  2643. if ($p_header['extra_len'] != 0)
  2644. {
  2645. fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
  2646. }
  2647. if ($p_header['comment_len'] != 0)
  2648. {
  2649. fputs($this->zip_fd, $p_header['comment'], $p_header['comment_len']);
  2650. }
  2651. // ----- Return
  2652. return $v_result;
  2653. }
  2654. // --------------------------------------------------------------------------------
  2655. // --------------------------------------------------------------------------------
  2656. // Function : privWriteCentralHeader()
  2657. // Description :
  2658. // Parameters :
  2659. // Return Values :
  2660. // --------------------------------------------------------------------------------
  2661. function privWriteCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
  2662. {
  2663. $v_result=1;
  2664. // ----- Packed data
  2665. $v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries,
  2666. $p_nb_entries, $p_size,
  2667. $p_offset, strlen($p_comment));
  2668. // ----- Write the 22 bytes of the header in the zip file
  2669. fputs($this->zip_fd, $v_binary_data, 22);
  2670. // ----- Write the variable fields
  2671. if (strlen($p_comment) != 0)
  2672. {
  2673. fputs($this->zip_fd, $p_comment, strlen($p_comment));
  2674. }
  2675. // ----- Return
  2676. return $v_result;
  2677. }
  2678. // --------------------------------------------------------------------------------
  2679. // --------------------------------------------------------------------------------
  2680. // Function : privList()
  2681. // Description :
  2682. // Parameters :
  2683. // Return Values :
  2684. // --------------------------------------------------------------------------------
  2685. function privList(&$p_list)
  2686. {
  2687. $v_result=1;
  2688. // ----- Magic quotes trick
  2689. $this->privDisableMagicQuotes();
  2690. // ----- Open the zip file
  2691. if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
  2692. {
  2693. // ----- Magic quotes trick
  2694. $this->privSwapBackMagicQuotes();
  2695. // ----- Error log
  2696. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
  2697. // ----- Return
  2698. return PclZip::errorCode();
  2699. }
  2700. // ----- Read the central directory informations
  2701. $v_central_dir = array();
  2702. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  2703. {
  2704. $this->privSwapBackMagicQuotes();
  2705. return $v_result;
  2706. }
  2707. // ----- Go to beginning of Central Dir
  2708. @rewind($this->zip_fd);
  2709. if (@fseek($this->zip_fd, $v_central_dir['offset']))
  2710. {
  2711. $this->privSwapBackMagicQuotes();
  2712. // ----- Error log
  2713. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  2714. // ----- Return
  2715. return PclZip::errorCode();
  2716. }
  2717. // ----- Read each entry
  2718. for ($i=0; $i<$v_central_dir['entries']; $i++)
  2719. {
  2720. // ----- Read the file header
  2721. if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
  2722. {
  2723. $this->privSwapBackMagicQuotes();
  2724. return $v_result;
  2725. }
  2726. $v_header['index'] = $i;
  2727. // ----- Get the only interesting attributes
  2728. $this->privConvertHeader2FileInfo($v_header, $p_list[$i]);
  2729. unset($v_header);
  2730. }
  2731. // ----- Close the zip file
  2732. $this->privCloseFd();
  2733. // ----- Magic quotes trick
  2734. $this->privSwapBackMagicQuotes();
  2735. // ----- Return
  2736. return $v_result;
  2737. }
  2738. // --------------------------------------------------------------------------------
  2739. // --------------------------------------------------------------------------------
  2740. // Function : privConvertHeader2FileInfo()
  2741. // Description :
  2742. // This function takes the file informations from the central directory
  2743. // entries and extract the interesting parameters that will be given back.
  2744. // The resulting file infos are set in the array $p_info
  2745. // $p_info['filename'] : Filename with full path. Given by user (add),
  2746. // extracted in the filesystem (extract).
  2747. // $p_info['stored_filename'] : Stored filename in the archive.
  2748. // $p_info['size'] = Size of the file.
  2749. // $p_info['compressed_size'] = Compressed size of the file.
  2750. // $p_info['mtime'] = Last modification date of the file.
  2751. // $p_info['comment'] = Comment associated with the file.
  2752. // $p_info['folder'] = true/false : indicates if the entry is a folder or not.
  2753. // $p_info['status'] = status of the action on the file.
  2754. // $p_info['crc'] = CRC of the file content.
  2755. // Parameters :
  2756. // Return Values :
  2757. // --------------------------------------------------------------------------------
  2758. function privConvertHeader2FileInfo($p_header, &$p_info)
  2759. {
  2760. $v_result=1;
  2761. // ----- Get the interesting attributes
  2762. $v_temp_path = PclZipUtilPathReduction($p_header['filename']);
  2763. $p_info['filename'] = $v_temp_path;
  2764. $v_temp_path = PclZipUtilPathReduction($p_header['stored_filename']);
  2765. $p_info['stored_filename'] = $v_temp_path;
  2766. $p_info['size'] = $p_header['size'];
  2767. $p_info['compressed_size'] = $p_header['compressed_size'];
  2768. $p_info['mtime'] = $p_header['mtime'];
  2769. $p_info['comment'] = $p_header['comment'];
  2770. $p_info['folder'] = (($p_header['external']&0x00000010)==0x00000010);
  2771. $p_info['index'] = $p_header['index'];
  2772. $p_info['status'] = $p_header['status'];
  2773. $p_info['crc'] = $p_header['crc'];
  2774. // ----- Return
  2775. return $v_result;
  2776. }
  2777. // --------------------------------------------------------------------------------
  2778. // --------------------------------------------------------------------------------
  2779. // Function : privExtractByRule()
  2780. // Description :
  2781. // Extract a file or directory depending of rules (by index, by name, ...)
  2782. // Parameters :
  2783. // $p_file_list : An array where will be placed the properties of each
  2784. // extracted file
  2785. // $p_path : Path to add while writing the extracted files
  2786. // $p_remove_path : Path to remove (from the file memorized path) while writing the
  2787. // extracted files. If the path does not match the file path,
  2788. // the file is extracted with its memorized path.
  2789. // $p_remove_path does not apply to 'list' mode.
  2790. // $p_path and $p_remove_path are commulative.
  2791. // Return Values :
  2792. // 1 on success,0 or less on error (see error code list)
  2793. // --------------------------------------------------------------------------------
  2794. function privExtractByRule(&$p_file_list, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
  2795. {
  2796. $v_result=1;
  2797. // ----- Magic quotes trick
  2798. $this->privDisableMagicQuotes();
  2799. // ----- Check the path
  2800. if ( ($p_path == "")
  2801. || ( (substr($p_path, 0, 1) != "/")
  2802. && (substr($p_path, 0, 3) != "../")
  2803. && (substr($p_path,1,2)!=":/")))
  2804. $p_path = "./".$p_path;
  2805. // ----- Reduce the path last (and duplicated) '/'
  2806. if (($p_path != "./") && ($p_path != "/"))
  2807. {
  2808. // ----- Look for the path end '/'
  2809. while (substr($p_path, -1) == "/")
  2810. {
  2811. $p_path = substr($p_path, 0, strlen($p_path)-1);
  2812. }
  2813. }
  2814. // ----- Look for path to remove format (should end by /)
  2815. if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/'))
  2816. {
  2817. $p_remove_path .= '/';
  2818. }
  2819. $p_remove_path_size = strlen($p_remove_path);
  2820. // ----- Open the zip file
  2821. if (($v_result = $this->privOpenFd('rb')) != 1)
  2822. {
  2823. $this->privSwapBackMagicQuotes();
  2824. return $v_result;
  2825. }
  2826. // ----- Read the central directory informations
  2827. $v_central_dir = array();
  2828. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  2829. {
  2830. // ----- Close the zip file
  2831. $this->privCloseFd();
  2832. $this->privSwapBackMagicQuotes();
  2833. return $v_result;
  2834. }
  2835. // ----- Start at beginning of Central Dir
  2836. $v_pos_entry = $v_central_dir['offset'];
  2837. // ----- Read each entry
  2838. $j_start = 0;
  2839. for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
  2840. {
  2841. // ----- Read next Central dir entry
  2842. @rewind($this->zip_fd);
  2843. if (@fseek($this->zip_fd, $v_pos_entry))
  2844. {
  2845. // ----- Close the zip file
  2846. $this->privCloseFd();
  2847. $this->privSwapBackMagicQuotes();
  2848. // ----- Error log
  2849. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  2850. // ----- Return
  2851. return PclZip::errorCode();
  2852. }
  2853. // ----- Read the file header
  2854. $v_header = array();
  2855. if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
  2856. {
  2857. // ----- Close the zip file
  2858. $this->privCloseFd();
  2859. $this->privSwapBackMagicQuotes();
  2860. return $v_result;
  2861. }
  2862. // ----- Store the index
  2863. $v_header['index'] = $i;
  2864. // ----- Store the file position
  2865. $v_pos_entry = ftell($this->zip_fd);
  2866. // ----- Look for the specific extract rules
  2867. $v_extract = false;
  2868. // ----- Look for extract by name rule
  2869. if ( (isset($p_options[PCLZIP_OPT_BY_NAME]))
  2870. && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
  2871. // ----- Look if the filename is in the list
  2872. for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_extract); $j++) {
  2873. // ----- Look for a directory
  2874. if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
  2875. // ----- Look if the directory is in the filename path
  2876. if ( (strlen($v_header['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
  2877. && (substr($v_header['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
  2878. $v_extract = true;
  2879. }
  2880. }
  2881. // ----- Look for a filename
  2882. elseif ($v_header['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
  2883. $v_extract = true;
  2884. }
  2885. }
  2886. }
  2887. // ----- Look for extract by ereg rule
  2888. // ereg() is deprecated with PHP 5.3
  2889. /*
  2890. else if ( (isset($p_options[PCLZIP_OPT_BY_EREG]))
  2891. && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
  2892. if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header['stored_filename'])) {
  2893. $v_extract = true;
  2894. }
  2895. }
  2896. */
  2897. // ----- Look for extract by preg rule
  2898. else if ( (isset($p_options[PCLZIP_OPT_BY_PREG]))
  2899. && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
  2900. if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header['stored_filename'])) {
  2901. $v_extract = true;
  2902. }
  2903. }
  2904. // ----- Look for extract by index rule
  2905. else if ( (isset($p_options[PCLZIP_OPT_BY_INDEX]))
  2906. && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
  2907. // ----- Look if the index is in the list
  2908. for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_extract); $j++) {
  2909. if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
  2910. $v_extract = true;
  2911. }
  2912. if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
  2913. $j_start = $j+1;
  2914. }
  2915. if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
  2916. break;
  2917. }
  2918. }
  2919. }
  2920. // ----- Look for no rule, which means extract all the archive
  2921. else {
  2922. $v_extract = true;
  2923. }
  2924. // ----- Check compression method
  2925. if ( ($v_extract)
  2926. && ( ($v_header['compression'] != 8)
  2927. && ($v_header['compression'] != 0))) {
  2928. $v_header['status'] = 'unsupported_compression';
  2929. // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  2930. if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  2931. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  2932. $this->privSwapBackMagicQuotes();
  2933. PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_COMPRESSION,
  2934. "Filename '".$v_header['stored_filename']."' is "
  2935. ."compressed by an unsupported compression "
  2936. ."method (".$v_header['compression'].") ");
  2937. return PclZip::errorCode();
  2938. }
  2939. }
  2940. // ----- Check encrypted files
  2941. if (($v_extract) && (($v_header['flag'] & 1) == 1)) {
  2942. $v_header['status'] = 'unsupported_encryption';
  2943. // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  2944. if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  2945. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  2946. $this->privSwapBackMagicQuotes();
  2947. PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION,
  2948. "Unsupported encryption for "
  2949. ." filename '".$v_header['stored_filename']
  2950. ."'");
  2951. return PclZip::errorCode();
  2952. }
  2953. }
  2954. // ----- Look for real extraction
  2955. if (($v_extract) && ($v_header['status'] != 'ok')) {
  2956. $v_result = $this->privConvertHeader2FileInfo($v_header,
  2957. $p_file_list[$v_nb_extracted++]);
  2958. if ($v_result != 1) {
  2959. $this->privCloseFd();
  2960. $this->privSwapBackMagicQuotes();
  2961. return $v_result;
  2962. }
  2963. $v_extract = false;
  2964. }
  2965. // ----- Look for real extraction
  2966. if ($v_extract)
  2967. {
  2968. // ----- Go to the file position
  2969. @rewind($this->zip_fd);
  2970. if (@fseek($this->zip_fd, $v_header['offset']))
  2971. {
  2972. // ----- Close the zip file
  2973. $this->privCloseFd();
  2974. $this->privSwapBackMagicQuotes();
  2975. // ----- Error log
  2976. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  2977. // ----- Return
  2978. return PclZip::errorCode();
  2979. }
  2980. // ----- Look for extraction as string
  2981. if ($p_options[PCLZIP_OPT_EXTRACT_AS_STRING]) {
  2982. $v_string = '';
  2983. // ----- Extracting the file
  2984. $v_result1 = $this->privExtractFileAsString($v_header, $v_string, $p_options);
  2985. if ($v_result1 < 1) {
  2986. $this->privCloseFd();
  2987. $this->privSwapBackMagicQuotes();
  2988. return $v_result1;
  2989. }
  2990. // ----- Get the only interesting attributes
  2991. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1)
  2992. {
  2993. // ----- Close the zip file
  2994. $this->privCloseFd();
  2995. $this->privSwapBackMagicQuotes();
  2996. return $v_result;
  2997. }
  2998. // ----- Set the file content
  2999. $p_file_list[$v_nb_extracted]['content'] = $v_string;
  3000. // ----- Next extracted file
  3001. $v_nb_extracted++;
  3002. // ----- Look for user callback abort
  3003. if ($v_result1 == 2) {
  3004. break;
  3005. }
  3006. }
  3007. // ----- Look for extraction in standard output
  3008. elseif ( (isset($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT]))
  3009. && ($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])) {
  3010. // ----- Extracting the file in standard output
  3011. $v_result1 = $this->privExtractFileInOutput($v_header, $p_options);
  3012. if ($v_result1 < 1) {
  3013. $this->privCloseFd();
  3014. $this->privSwapBackMagicQuotes();
  3015. return $v_result1;
  3016. }
  3017. // ----- Get the only interesting attributes
  3018. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
  3019. $this->privCloseFd();
  3020. $this->privSwapBackMagicQuotes();
  3021. return $v_result;
  3022. }
  3023. // ----- Look for user callback abort
  3024. if ($v_result1 == 2) {
  3025. break;
  3026. }
  3027. }
  3028. // ----- Look for normal extraction
  3029. else {
  3030. // ----- Extracting the file
  3031. $v_result1 = $this->privExtractFile($v_header,
  3032. $p_path, $p_remove_path,
  3033. $p_remove_all_path,
  3034. $p_options);
  3035. if ($v_result1 < 1) {
  3036. $this->privCloseFd();
  3037. $this->privSwapBackMagicQuotes();
  3038. return $v_result1;
  3039. }
  3040. // ----- Get the only interesting attributes
  3041. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1)
  3042. {
  3043. // ----- Close the zip file
  3044. $this->privCloseFd();
  3045. $this->privSwapBackMagicQuotes();
  3046. return $v_result;
  3047. }
  3048. // ----- Look for user callback abort
  3049. if ($v_result1 == 2) {
  3050. break;
  3051. }
  3052. }
  3053. }
  3054. }
  3055. // ----- Close the zip file
  3056. $this->privCloseFd();
  3057. $this->privSwapBackMagicQuotes();
  3058. // ----- Return
  3059. return $v_result;
  3060. }
  3061. // --------------------------------------------------------------------------------
  3062. // --------------------------------------------------------------------------------
  3063. // Function : privExtractFile()
  3064. // Description :
  3065. // Parameters :
  3066. // Return Values :
  3067. //
  3068. // 1 : ... ?
  3069. // PCLZIP_ERR_USER_ABORTED(2) : User ask for extraction stop in callback
  3070. // --------------------------------------------------------------------------------
  3071. function privExtractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
  3072. {
  3073. $v_result=1;
  3074. // ----- Read the file header
  3075. if (($v_result = $this->privReadFileHeader($v_header)) != 1)
  3076. {
  3077. // ----- Return
  3078. return $v_result;
  3079. }
  3080. // ----- Check that the file header is coherent with $p_entry info
  3081. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  3082. // TBC
  3083. }
  3084. // ----- Look for all path to remove
  3085. if ($p_remove_all_path == true) {
  3086. // ----- Look for folder entry that not need to be extracted
  3087. if (($p_entry['external']&0x00000010)==0x00000010) {
  3088. $p_entry['status'] = "filtered";
  3089. return $v_result;
  3090. }
  3091. // ----- Get the basename of the path
  3092. $p_entry['filename'] = basename($p_entry['filename']);
  3093. }
  3094. // ----- Look for path to remove
  3095. else if ($p_remove_path != "")
  3096. {
  3097. if (PclZipUtilPathInclusion($p_remove_path, $p_entry['filename']) == 2)
  3098. {
  3099. // ----- Change the file status
  3100. $p_entry['status'] = "filtered";
  3101. // ----- Return
  3102. return $v_result;
  3103. }
  3104. $p_remove_path_size = strlen($p_remove_path);
  3105. if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path)
  3106. {
  3107. // ----- Remove the path
  3108. $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
  3109. }
  3110. }
  3111. // ----- Add the path
  3112. if ($p_path != '') {
  3113. $p_entry['filename'] = $p_path."/".$p_entry['filename'];
  3114. }
  3115. // ----- Check a base_dir_restriction
  3116. if (isset($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION])) {
  3117. $v_inclusion
  3118. = PclZipUtilPathInclusion($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION],
  3119. $p_entry['filename']);
  3120. if ($v_inclusion == 0) {
  3121. PclZip::privErrorLog(PCLZIP_ERR_DIRECTORY_RESTRICTION,
  3122. "Filename '".$p_entry['filename']."' is "
  3123. ."outside PCLZIP_OPT_EXTRACT_DIR_RESTRICTION");
  3124. return PclZip::errorCode();
  3125. }
  3126. }
  3127. // ----- Look for pre-extract callback
  3128. if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  3129. // ----- Generate a local information
  3130. $v_local_header = array();
  3131. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  3132. // ----- Call the callback
  3133. // Here I do not use call_user_func() because I need to send a reference to the
  3134. // header.
  3135. // eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
  3136. $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  3137. if ($v_result == 0) {
  3138. // ----- Change the file status
  3139. $p_entry['status'] = "skipped";
  3140. $v_result = 1;
  3141. }
  3142. // ----- Look for abort result
  3143. if ($v_result == 2) {
  3144. // ----- This status is internal and will be changed in 'skipped'
  3145. $p_entry['status'] = "aborted";
  3146. $v_result = PCLZIP_ERR_USER_ABORTED;
  3147. }
  3148. // ----- Update the informations
  3149. // Only some fields can be modified
  3150. $p_entry['filename'] = $v_local_header['filename'];
  3151. }
  3152. // ----- Look if extraction should be done
  3153. if ($p_entry['status'] == 'ok') {
  3154. // ----- Look for specific actions while the file exist
  3155. if (file_exists($p_entry['filename']))
  3156. {
  3157. // ----- Look if file is a directory
  3158. if (is_dir($p_entry['filename']))
  3159. {
  3160. // ----- Change the file status
  3161. $p_entry['status'] = "already_a_directory";
  3162. // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  3163. // For historical reason first PclZip implementation does not stop
  3164. // when this kind of error occurs.
  3165. if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  3166. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  3167. PclZip::privErrorLog(PCLZIP_ERR_ALREADY_A_DIRECTORY,
  3168. "Filename '".$p_entry['filename']."' is "
  3169. ."already used by an existing directory");
  3170. return PclZip::errorCode();
  3171. }
  3172. }
  3173. // ----- Look if file is write protected
  3174. else if (!is_writeable($p_entry['filename']))
  3175. {
  3176. // ----- Change the file status
  3177. $p_entry['status'] = "write_protected";
  3178. // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  3179. // For historical reason first PclZip implementation does not stop
  3180. // when this kind of error occurs.
  3181. if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  3182. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  3183. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
  3184. "Filename '".$p_entry['filename']."' exists "
  3185. ."and is write protected");
  3186. return PclZip::errorCode();
  3187. }
  3188. }
  3189. // ----- Look if the extracted file is older
  3190. else if (filemtime($p_entry['filename']) > $p_entry['mtime'])
  3191. {
  3192. // ----- Change the file status
  3193. if ( (isset($p_options[PCLZIP_OPT_REPLACE_NEWER]))
  3194. && ($p_options[PCLZIP_OPT_REPLACE_NEWER]===true)) {
  3195. }
  3196. else {
  3197. $p_entry['status'] = "newer_exist";
  3198. // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
  3199. // For historical reason first PclZip implementation does not stop
  3200. // when this kind of error occurs.
  3201. if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  3202. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  3203. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
  3204. "Newer version of '".$p_entry['filename']."' exists "
  3205. ."and option PCLZIP_OPT_REPLACE_NEWER is not selected");
  3206. return PclZip::errorCode();
  3207. }
  3208. }
  3209. }
  3210. else {
  3211. }
  3212. }
  3213. // ----- Check the directory availability and create it if necessary
  3214. else {
  3215. if ((($p_entry['external']&0x00000010)==0x00000010) || (substr($p_entry['filename'], -1) == '/'))
  3216. $v_dir_to_check = $p_entry['filename'];
  3217. else if (!strstr($p_entry['filename'], "/"))
  3218. $v_dir_to_check = "";
  3219. else
  3220. $v_dir_to_check = dirname($p_entry['filename']);
  3221. if (($v_result = $this->privDirCheck($v_dir_to_check, (($p_entry['external']&0x00000010)==0x00000010))) != 1) {
  3222. // ----- Change the file status
  3223. $p_entry['status'] = "path_creation_fail";
  3224. // ----- Return
  3225. //return $v_result;
  3226. $v_result = 1;
  3227. }
  3228. }
  3229. }
  3230. // ----- Look if extraction should be done
  3231. if ($p_entry['status'] == 'ok') {
  3232. // ----- Do the extraction (if not a folder)
  3233. if (!(($p_entry['external']&0x00000010)==0x00000010))
  3234. {
  3235. // ----- Look for not compressed file
  3236. if ($p_entry['compression'] == 0) {
  3237. // ----- Opening destination file
  3238. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0)
  3239. {
  3240. // ----- Change the file status
  3241. $p_entry['status'] = "write_error";
  3242. // ----- Return
  3243. return $v_result;
  3244. }
  3245. // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  3246. $v_size = $p_entry['compressed_size'];
  3247. while ($v_size != 0)
  3248. {
  3249. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  3250. $v_buffer = @fread($this->zip_fd, $v_read_size);
  3251. /* Try to speed up the code
  3252. $v_binary_data = pack('a'.$v_read_size, $v_buffer);
  3253. @fwrite($v_dest_file, $v_binary_data, $v_read_size);
  3254. */
  3255. @fwrite($v_dest_file, $v_buffer, $v_read_size);
  3256. $v_size -= $v_read_size;
  3257. }
  3258. // ----- Closing the destination file
  3259. fclose($v_dest_file);
  3260. // ----- Change the file mtime
  3261. touch($p_entry['filename'], $p_entry['mtime']);
  3262. }
  3263. else {
  3264. // ----- TBC
  3265. // Need to be finished
  3266. if (($p_entry['flag'] & 1) == 1) {
  3267. PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION, 'File \''.$p_entry['filename'].'\' is encrypted. Encrypted files are not supported.');
  3268. return PclZip::errorCode();
  3269. }
  3270. // ----- Look for using temporary file to unzip
  3271. if ( (!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF]))
  3272. && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON])
  3273. || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
  3274. && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_entry['size'])) ) ) {
  3275. $v_result = $this->privExtractFileUsingTempFile($p_entry, $p_options);
  3276. if ($v_result < PCLZIP_ERR_NO_ERROR) {
  3277. return $v_result;
  3278. }
  3279. }
  3280. // ----- Look for extract in memory
  3281. else {
  3282. // ----- Read the compressed file in a buffer (one shot)
  3283. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  3284. // ----- Decompress the file
  3285. $v_file_content = @gzinflate($v_buffer);
  3286. unset($v_buffer);
  3287. if ($v_file_content === FALSE) {
  3288. // ----- Change the file status
  3289. // TBC
  3290. $p_entry['status'] = "error";
  3291. return $v_result;
  3292. }
  3293. // ----- Opening destination file
  3294. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  3295. // ----- Change the file status
  3296. $p_entry['status'] = "write_error";
  3297. return $v_result;
  3298. }
  3299. // ----- Write the uncompressed data
  3300. @fwrite($v_dest_file, $v_file_content, $p_entry['size']);
  3301. unset($v_file_content);
  3302. // ----- Closing the destination file
  3303. @fclose($v_dest_file);
  3304. }
  3305. // ----- Change the file mtime
  3306. @touch($p_entry['filename'], $p_entry['mtime']);
  3307. }
  3308. // ----- Look for chmod option
  3309. if (isset($p_options[PCLZIP_OPT_SET_CHMOD])) {
  3310. // ----- Change the mode of the file
  3311. @chmod($p_entry['filename'], $p_options[PCLZIP_OPT_SET_CHMOD]);
  3312. }
  3313. }
  3314. }
  3315. // ----- Change abort status
  3316. if ($p_entry['status'] == "aborted") {
  3317. $p_entry['status'] = "skipped";
  3318. }
  3319. // ----- Look for post-extract callback
  3320. elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  3321. // ----- Generate a local information
  3322. $v_local_header = array();
  3323. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  3324. // ----- Call the callback
  3325. // Here I do not use call_user_func() because I need to send a reference to the
  3326. // header.
  3327. // eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
  3328. $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
  3329. // ----- Look for abort result
  3330. if ($v_result == 2) {
  3331. $v_result = PCLZIP_ERR_USER_ABORTED;
  3332. }
  3333. }
  3334. // ----- Return
  3335. return $v_result;
  3336. }
  3337. // --------------------------------------------------------------------------------
  3338. // --------------------------------------------------------------------------------
  3339. // Function : privExtractFileUsingTempFile()
  3340. // Description :
  3341. // Parameters :
  3342. // Return Values :
  3343. // --------------------------------------------------------------------------------
  3344. function privExtractFileUsingTempFile(&$p_entry, &$p_options)
  3345. {
  3346. $v_result=1;
  3347. // ----- Creates a temporary file
  3348. $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.gz';
  3349. if (($v_dest_file = @fopen($v_gzip_temp_name, "wb")) == 0) {
  3350. fclose($v_file);
  3351. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary write mode');
  3352. return PclZip::errorCode();
  3353. }
  3354. // ----- Write gz file format header
  3355. $v_binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($p_entry['compression']), Chr(0x00), time(), Chr(0x00), Chr(3));
  3356. @fwrite($v_dest_file, $v_binary_data, 10);
  3357. // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  3358. $v_size = $p_entry['compressed_size'];
  3359. while ($v_size != 0)
  3360. {
  3361. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  3362. $v_buffer = @fread($this->zip_fd, $v_read_size);
  3363. //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
  3364. @fwrite($v_dest_file, $v_buffer, $v_read_size);
  3365. $v_size -= $v_read_size;
  3366. }
  3367. // ----- Write gz file format footer
  3368. $v_binary_data = pack('VV', $p_entry['crc'], $p_entry['size']);
  3369. @fwrite($v_dest_file, $v_binary_data, 8);
  3370. // ----- Close the temporary file
  3371. @fclose($v_dest_file);
  3372. // ----- Opening destination file
  3373. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  3374. $p_entry['status'] = "write_error";
  3375. return $v_result;
  3376. }
  3377. // ----- Open the temporary gz file
  3378. if (($v_src_file = @gzopen($v_gzip_temp_name, 'rb')) == 0) {
  3379. @fclose($v_dest_file);
  3380. $p_entry['status'] = "read_error";
  3381. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
  3382. return PclZip::errorCode();
  3383. }
  3384. // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  3385. $v_size = $p_entry['size'];
  3386. while ($v_size != 0) {
  3387. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  3388. $v_buffer = @gzread($v_src_file, $v_read_size);
  3389. //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
  3390. @fwrite($v_dest_file, $v_buffer, $v_read_size);
  3391. $v_size -= $v_read_size;
  3392. }
  3393. @fclose($v_dest_file);
  3394. @gzclose($v_src_file);
  3395. // ----- Delete the temporary file
  3396. @unlink($v_gzip_temp_name);
  3397. // ----- Return
  3398. return $v_result;
  3399. }
  3400. // --------------------------------------------------------------------------------
  3401. // --------------------------------------------------------------------------------
  3402. // Function : privExtractFileInOutput()
  3403. // Description :
  3404. // Parameters :
  3405. // Return Values :
  3406. // --------------------------------------------------------------------------------
  3407. function privExtractFileInOutput(&$p_entry, &$p_options)
  3408. {
  3409. $v_result=1;
  3410. // ----- Read the file header
  3411. if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
  3412. return $v_result;
  3413. }
  3414. // ----- Check that the file header is coherent with $p_entry info
  3415. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  3416. // TBC
  3417. }
  3418. // ----- Look for pre-extract callback
  3419. if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  3420. // ----- Generate a local information
  3421. $v_local_header = array();
  3422. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  3423. // ----- Call the callback
  3424. // Here I do not use call_user_func() because I need to send a reference to the
  3425. // header.
  3426. // eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
  3427. $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  3428. if ($v_result == 0) {
  3429. // ----- Change the file status
  3430. $p_entry['status'] = "skipped";
  3431. $v_result = 1;
  3432. }
  3433. // ----- Look for abort result
  3434. if ($v_result == 2) {
  3435. // ----- This status is internal and will be changed in 'skipped'
  3436. $p_entry['status'] = "aborted";
  3437. $v_result = PCLZIP_ERR_USER_ABORTED;
  3438. }
  3439. // ----- Update the informations
  3440. // Only some fields can be modified
  3441. $p_entry['filename'] = $v_local_header['filename'];
  3442. }
  3443. // ----- Trace
  3444. // ----- Look if extraction should be done
  3445. if ($p_entry['status'] == 'ok') {
  3446. // ----- Do the extraction (if not a folder)
  3447. if (!(($p_entry['external']&0x00000010)==0x00000010)) {
  3448. // ----- Look for not compressed file
  3449. if ($p_entry['compressed_size'] == $p_entry['size']) {
  3450. // ----- Read the file in a buffer (one shot)
  3451. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  3452. // ----- Send the file to the output
  3453. echo $v_buffer;
  3454. unset($v_buffer);
  3455. }
  3456. else {
  3457. // ----- Read the compressed file in a buffer (one shot)
  3458. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  3459. // ----- Decompress the file
  3460. $v_file_content = gzinflate($v_buffer);
  3461. unset($v_buffer);
  3462. // ----- Send the file to the output
  3463. echo $v_file_content;
  3464. unset($v_file_content);
  3465. }
  3466. }
  3467. }
  3468. // ----- Change abort status
  3469. if ($p_entry['status'] == "aborted") {
  3470. $p_entry['status'] = "skipped";
  3471. }
  3472. // ----- Look for post-extract callback
  3473. elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  3474. // ----- Generate a local information
  3475. $v_local_header = array();
  3476. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  3477. // ----- Call the callback
  3478. // Here I do not use call_user_func() because I need to send a reference to the
  3479. // header.
  3480. // eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
  3481. $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
  3482. // ----- Look for abort result
  3483. if ($v_result == 2) {
  3484. $v_result = PCLZIP_ERR_USER_ABORTED;
  3485. }
  3486. }
  3487. return $v_result;
  3488. }
  3489. // --------------------------------------------------------------------------------
  3490. // --------------------------------------------------------------------------------
  3491. // Function : privExtractFileAsString()
  3492. // Description :
  3493. // Parameters :
  3494. // Return Values :
  3495. // --------------------------------------------------------------------------------
  3496. function privExtractFileAsString(&$p_entry, &$p_string, &$p_options)
  3497. {
  3498. $v_result=1;
  3499. // ----- Read the file header
  3500. $v_header = array();
  3501. if (($v_result = $this->privReadFileHeader($v_header)) != 1)
  3502. {
  3503. // ----- Return
  3504. return $v_result;
  3505. }
  3506. // ----- Check that the file header is coherent with $p_entry info
  3507. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  3508. // TBC
  3509. }
  3510. // ----- Look for pre-extract callback
  3511. if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  3512. // ----- Generate a local information
  3513. $v_local_header = array();
  3514. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  3515. // ----- Call the callback
  3516. // Here I do not use call_user_func() because I need to send a reference to the
  3517. // header.
  3518. // eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
  3519. $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  3520. if ($v_result == 0) {
  3521. // ----- Change the file status
  3522. $p_entry['status'] = "skipped";
  3523. $v_result = 1;
  3524. }
  3525. // ----- Look for abort result
  3526. if ($v_result == 2) {
  3527. // ----- This status is internal and will be changed in 'skipped'
  3528. $p_entry['status'] = "aborted";
  3529. $v_result = PCLZIP_ERR_USER_ABORTED;
  3530. }
  3531. // ----- Update the informations
  3532. // Only some fields can be modified
  3533. $p_entry['filename'] = $v_local_header['filename'];
  3534. }
  3535. // ----- Look if extraction should be done
  3536. if ($p_entry['status'] == 'ok') {
  3537. // ----- Do the extraction (if not a folder)
  3538. if (!(($p_entry['external']&0x00000010)==0x00000010)) {
  3539. // ----- Look for not compressed file
  3540. // if ($p_entry['compressed_size'] == $p_entry['size'])
  3541. if ($p_entry['compression'] == 0) {
  3542. // ----- Reading the file
  3543. $p_string = @fread($this->zip_fd, $p_entry['compressed_size']);
  3544. }
  3545. else {
  3546. // ----- Reading the file
  3547. $v_data = @fread($this->zip_fd, $p_entry['compressed_size']);
  3548. // ----- Decompress the file
  3549. if (($p_string = @gzinflate($v_data)) === FALSE) {
  3550. // TBC
  3551. }
  3552. }
  3553. // ----- Trace
  3554. }
  3555. else {
  3556. // TBC : error : can not extract a folder in a string
  3557. }
  3558. }
  3559. // ----- Change abort status
  3560. if ($p_entry['status'] == "aborted") {
  3561. $p_entry['status'] = "skipped";
  3562. }
  3563. // ----- Look for post-extract callback
  3564. elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  3565. // ----- Generate a local information
  3566. $v_local_header = array();
  3567. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  3568. // ----- Swap the content to header
  3569. $v_local_header['content'] = $p_string;
  3570. $p_string = '';
  3571. // ----- Call the callback
  3572. // Here I do not use call_user_func() because I need to send a reference to the
  3573. // header.
  3574. // eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
  3575. $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
  3576. // ----- Swap back the content to header
  3577. $p_string = $v_local_header['content'];
  3578. unset($v_local_header['content']);
  3579. // ----- Look for abort result
  3580. if ($v_result == 2) {
  3581. $v_result = PCLZIP_ERR_USER_ABORTED;
  3582. }
  3583. }
  3584. // ----- Return
  3585. return $v_result;
  3586. }
  3587. // --------------------------------------------------------------------------------
  3588. // --------------------------------------------------------------------------------
  3589. // Function : privReadFileHeader()
  3590. // Description :
  3591. // Parameters :
  3592. // Return Values :
  3593. // --------------------------------------------------------------------------------
  3594. function privReadFileHeader(&$p_header)
  3595. {
  3596. $v_result=1;
  3597. // ----- Read the 4 bytes signature
  3598. $v_binary_data = @fread($this->zip_fd, 4);
  3599. $v_data = unpack('Vid', $v_binary_data);
  3600. // ----- Check signature
  3601. if ($v_data['id'] != 0x04034b50)
  3602. {
  3603. // ----- Error log
  3604. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  3605. // ----- Return
  3606. return PclZip::errorCode();
  3607. }
  3608. // ----- Read the first 42 bytes of the header
  3609. $v_binary_data = fread($this->zip_fd, 26);
  3610. // ----- Look for invalid block size
  3611. if (strlen($v_binary_data) != 26)
  3612. {
  3613. $p_header['filename'] = "";
  3614. $p_header['status'] = "invalid_header";
  3615. // ----- Error log
  3616. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
  3617. // ----- Return
  3618. return PclZip::errorCode();
  3619. }
  3620. // ----- Extract the values
  3621. $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
  3622. // ----- Get filename
  3623. $p_header['filename'] = fread($this->zip_fd, $v_data['filename_len']);
  3624. // ----- Get extra_fields
  3625. if ($v_data['extra_len'] != 0) {
  3626. $p_header['extra'] = fread($this->zip_fd, $v_data['extra_len']);
  3627. }
  3628. else {
  3629. $p_header['extra'] = '';
  3630. }
  3631. // ----- Extract properties
  3632. $p_header['version_extracted'] = $v_data['version'];
  3633. $p_header['compression'] = $v_data['compression'];
  3634. $p_header['size'] = $v_data['size'];
  3635. $p_header['compressed_size'] = $v_data['compressed_size'];
  3636. $p_header['crc'] = $v_data['crc'];
  3637. $p_header['flag'] = $v_data['flag'];
  3638. $p_header['filename_len'] = $v_data['filename_len'];
  3639. // ----- Recuperate date in UNIX format
  3640. $p_header['mdate'] = $v_data['mdate'];
  3641. $p_header['mtime'] = $v_data['mtime'];
  3642. if ($p_header['mdate'] && $p_header['mtime'])
  3643. {
  3644. // ----- Extract time
  3645. $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
  3646. $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
  3647. $v_seconde = ($p_header['mtime'] & 0x001F)*2;
  3648. // ----- Extract date
  3649. $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
  3650. $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
  3651. $v_day = $p_header['mdate'] & 0x001F;
  3652. // ----- Get UNIX date format
  3653. $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  3654. }
  3655. else
  3656. {
  3657. $p_header['mtime'] = time();
  3658. }
  3659. // TBC
  3660. //for(reset($v_data); $key = key($v_data); next($v_data)) {
  3661. //}
  3662. // ----- Set the stored filename
  3663. $p_header['stored_filename'] = $p_header['filename'];
  3664. // ----- Set the status field
  3665. $p_header['status'] = "ok";
  3666. // ----- Return
  3667. return $v_result;
  3668. }
  3669. // --------------------------------------------------------------------------------
  3670. // --------------------------------------------------------------------------------
  3671. // Function : privReadCentralFileHeader()
  3672. // Description :
  3673. // Parameters :
  3674. // Return Values :
  3675. // --------------------------------------------------------------------------------
  3676. function privReadCentralFileHeader(&$p_header)
  3677. {
  3678. $v_result=1;
  3679. // ----- Read the 4 bytes signature
  3680. $v_binary_data = @fread($this->zip_fd, 4);
  3681. $v_data = unpack('Vid', $v_binary_data);
  3682. // ----- Check signature
  3683. if ($v_data['id'] != 0x02014b50)
  3684. {
  3685. // ----- Error log
  3686. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  3687. // ----- Return
  3688. return PclZip::errorCode();
  3689. }
  3690. // ----- Read the first 42 bytes of the header
  3691. $v_binary_data = fread($this->zip_fd, 42);
  3692. // ----- Look for invalid block size
  3693. if (strlen($v_binary_data) != 42)
  3694. {
  3695. $p_header['filename'] = "";
  3696. $p_header['status'] = "invalid_header";
  3697. // ----- Error log
  3698. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
  3699. // ----- Return
  3700. return PclZip::errorCode();
  3701. }
  3702. // ----- Extract the values
  3703. $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);
  3704. // ----- Get filename
  3705. if ($p_header['filename_len'] != 0)
  3706. $p_header['filename'] = fread($this->zip_fd, $p_header['filename_len']);
  3707. else
  3708. $p_header['filename'] = '';
  3709. // ----- Get extra
  3710. if ($p_header['extra_len'] != 0)
  3711. $p_header['extra'] = fread($this->zip_fd, $p_header['extra_len']);
  3712. else
  3713. $p_header['extra'] = '';
  3714. // ----- Get comment
  3715. if ($p_header['comment_len'] != 0)
  3716. $p_header['comment'] = fread($this->zip_fd, $p_header['comment_len']);
  3717. else
  3718. $p_header['comment'] = '';
  3719. // ----- Extract properties
  3720. // ----- Recuperate date in UNIX format
  3721. //if ($p_header['mdate'] && $p_header['mtime'])
  3722. // TBC : bug : this was ignoring time with 0/0/0
  3723. if (1)
  3724. {
  3725. // ----- Extract time
  3726. $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
  3727. $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
  3728. $v_seconde = ($p_header['mtime'] & 0x001F)*2;
  3729. // ----- Extract date
  3730. $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
  3731. $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
  3732. $v_day = $p_header['mdate'] & 0x001F;
  3733. // ----- Get UNIX date format
  3734. $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  3735. }
  3736. else
  3737. {
  3738. $p_header['mtime'] = time();
  3739. }
  3740. // ----- Set the stored filename
  3741. $p_header['stored_filename'] = $p_header['filename'];
  3742. // ----- Set default status to ok
  3743. $p_header['status'] = 'ok';
  3744. // ----- Look if it is a directory
  3745. if (substr($p_header['filename'], -1) == '/') {
  3746. //$p_header['external'] = 0x41FF0010;
  3747. $p_header['external'] = 0x00000010;
  3748. }
  3749. // ----- Return
  3750. return $v_result;
  3751. }
  3752. // --------------------------------------------------------------------------------
  3753. // --------------------------------------------------------------------------------
  3754. // Function : privCheckFileHeaders()
  3755. // Description :
  3756. // Parameters :
  3757. // Return Values :
  3758. // 1 on success,
  3759. // 0 on error;
  3760. // --------------------------------------------------------------------------------
  3761. function privCheckFileHeaders(&$p_local_header, &$p_central_header)
  3762. {
  3763. $v_result=1;
  3764. // ----- Check the static values
  3765. // TBC
  3766. if ($p_local_header['filename'] != $p_central_header['filename']) {
  3767. }
  3768. if ($p_local_header['version_extracted'] != $p_central_header['version_extracted']) {
  3769. }
  3770. if ($p_local_header['flag'] != $p_central_header['flag']) {
  3771. }
  3772. if ($p_local_header['compression'] != $p_central_header['compression']) {
  3773. }
  3774. if ($p_local_header['mtime'] != $p_central_header['mtime']) {
  3775. }
  3776. if ($p_local_header['filename_len'] != $p_central_header['filename_len']) {
  3777. }
  3778. // ----- Look for flag bit 3
  3779. if (($p_local_header['flag'] & 8) == 8) {
  3780. $p_local_header['size'] = $p_central_header['size'];
  3781. $p_local_header['compressed_size'] = $p_central_header['compressed_size'];
  3782. $p_local_header['crc'] = $p_central_header['crc'];
  3783. }
  3784. // ----- Return
  3785. return $v_result;
  3786. }
  3787. // --------------------------------------------------------------------------------
  3788. // --------------------------------------------------------------------------------
  3789. // Function : privReadEndCentralDir()
  3790. // Description :
  3791. // Parameters :
  3792. // Return Values :
  3793. // --------------------------------------------------------------------------------
  3794. function privReadEndCentralDir(&$p_central_dir)
  3795. {
  3796. $v_result=1;
  3797. // ----- Go to the end of the zip file
  3798. $v_size = filesize($this->zipname);
  3799. @fseek($this->zip_fd, $v_size);
  3800. if (@ftell($this->zip_fd) != $v_size)
  3801. {
  3802. // ----- Error log
  3803. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \''.$this->zipname.'\'');
  3804. // ----- Return
  3805. return PclZip::errorCode();
  3806. }
  3807. // ----- First try : look if this is an archive with no commentaries (most of the time)
  3808. // in this case the end of central dir is at 22 bytes of the file end
  3809. $v_found = 0;
  3810. if ($v_size > 26) {
  3811. @fseek($this->zip_fd, $v_size-22);
  3812. if (($v_pos = @ftell($this->zip_fd)) != ($v_size-22))
  3813. {
  3814. // ----- Error log
  3815. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
  3816. // ----- Return
  3817. return PclZip::errorCode();
  3818. }
  3819. // ----- Read for bytes
  3820. $v_binary_data = @fread($this->zip_fd, 4);
  3821. $v_data = @unpack('Vid', $v_binary_data);
  3822. // ----- Check signature
  3823. if ($v_data['id'] == 0x06054b50) {
  3824. $v_found = 1;
  3825. }
  3826. $v_pos = ftell($this->zip_fd);
  3827. }
  3828. // ----- Go back to the maximum possible size of the Central Dir End Record
  3829. if (!$v_found) {
  3830. $v_maximum_size = 65557; // 0xFFFF + 22;
  3831. if ($v_maximum_size > $v_size)
  3832. $v_maximum_size = $v_size;
  3833. @fseek($this->zip_fd, $v_size-$v_maximum_size);
  3834. if (@ftell($this->zip_fd) != ($v_size-$v_maximum_size))
  3835. {
  3836. // ----- Error log
  3837. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
  3838. // ----- Return
  3839. return PclZip::errorCode();
  3840. }
  3841. // ----- Read byte per byte in order to find the signature
  3842. $v_pos = ftell($this->zip_fd);
  3843. $v_bytes = 0x00000000;
  3844. while ($v_pos < $v_size)
  3845. {
  3846. // ----- Read a byte
  3847. $v_byte = @fread($this->zip_fd, 1);
  3848. // ----- Add the byte
  3849. //$v_bytes = ($v_bytes << 8) | Ord($v_byte);
  3850. // Note we mask the old value down such that once shifted we can never end up with more than a 32bit number
  3851. // Otherwise on systems where we have 64bit integers the check below for the magic number will fail.
  3852. $v_bytes = ( ($v_bytes & 0xFFFFFF) << 8) | Ord($v_byte);
  3853. // ----- Compare the bytes
  3854. if ($v_bytes == 0x504b0506)
  3855. {
  3856. $v_pos++;
  3857. break;
  3858. }
  3859. $v_pos++;
  3860. }
  3861. // ----- Look if not found end of central dir
  3862. if ($v_pos == $v_size)
  3863. {
  3864. // ----- Error log
  3865. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
  3866. // ----- Return
  3867. return PclZip::errorCode();
  3868. }
  3869. }
  3870. // ----- Read the first 18 bytes of the header
  3871. $v_binary_data = fread($this->zip_fd, 18);
  3872. // ----- Look for invalid block size
  3873. if (strlen($v_binary_data) != 18)
  3874. {
  3875. // ----- Error log
  3876. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
  3877. // ----- Return
  3878. return PclZip::errorCode();
  3879. }
  3880. // ----- Extract the values
  3881. $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
  3882. // ----- Check the global size
  3883. if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
  3884. // ----- Removed in release 2.2 see readme file
  3885. // The check of the file size is a little too strict.
  3886. // Some bugs where found when a zip is encrypted/decrypted with 'crypt'.
  3887. // While decrypted, zip has training 0 bytes
  3888. if (0) {
  3889. // ----- Error log
  3890. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT,
  3891. 'The central dir is not at the end of the archive.'
  3892. .' Some trailing bytes exists after the archive.');
  3893. // ----- Return
  3894. return PclZip::errorCode();
  3895. }
  3896. }
  3897. // ----- Get comment
  3898. if ($v_data['comment_size'] != 0) {
  3899. $p_central_dir['comment'] = fread($this->zip_fd, $v_data['comment_size']);
  3900. }
  3901. else
  3902. $p_central_dir['comment'] = '';
  3903. $p_central_dir['entries'] = $v_data['entries'];
  3904. $p_central_dir['disk_entries'] = $v_data['disk_entries'];
  3905. $p_central_dir['offset'] = $v_data['offset'];
  3906. $p_central_dir['size'] = $v_data['size'];
  3907. $p_central_dir['disk'] = $v_data['disk'];
  3908. $p_central_dir['disk_start'] = $v_data['disk_start'];
  3909. // TBC
  3910. //for(reset($p_central_dir); $key = key($p_central_dir); next($p_central_dir)) {
  3911. //}
  3912. // ----- Return
  3913. return $v_result;
  3914. }
  3915. // --------------------------------------------------------------------------------
  3916. // --------------------------------------------------------------------------------
  3917. // Function : privDeleteByRule()
  3918. // Description :
  3919. // Parameters :
  3920. // Return Values :
  3921. // --------------------------------------------------------------------------------
  3922. function privDeleteByRule(&$p_result_list, &$p_options)
  3923. {
  3924. $v_result=1;
  3925. $v_list_detail = array();
  3926. // ----- Open the zip file
  3927. if (($v_result=$this->privOpenFd('rb')) != 1)
  3928. {
  3929. // ----- Return
  3930. return $v_result;
  3931. }
  3932. // ----- Read the central directory informations
  3933. $v_central_dir = array();
  3934. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  3935. {
  3936. $this->privCloseFd();
  3937. return $v_result;
  3938. }
  3939. // ----- Go to beginning of File
  3940. @rewind($this->zip_fd);
  3941. // ----- Scan all the files
  3942. // ----- Start at beginning of Central Dir
  3943. $v_pos_entry = $v_central_dir['offset'];
  3944. @rewind($this->zip_fd);
  3945. if (@fseek($this->zip_fd, $v_pos_entry))
  3946. {
  3947. // ----- Close the zip file
  3948. $this->privCloseFd();
  3949. // ----- Error log
  3950. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  3951. // ----- Return
  3952. return PclZip::errorCode();
  3953. }
  3954. // ----- Read each entry
  3955. $v_header_list = array();
  3956. $j_start = 0;
  3957. for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
  3958. {
  3959. // ----- Read the file header
  3960. $v_header_list[$v_nb_extracted] = array();
  3961. if (($v_result = $this->privReadCentralFileHeader($v_header_list[$v_nb_extracted])) != 1)
  3962. {
  3963. // ----- Close the zip file
  3964. $this->privCloseFd();
  3965. return $v_result;
  3966. }
  3967. // ----- Store the index
  3968. $v_header_list[$v_nb_extracted]['index'] = $i;
  3969. // ----- Look for the specific extract rules
  3970. $v_found = false;
  3971. // ----- Look for extract by name rule
  3972. if ( (isset($p_options[PCLZIP_OPT_BY_NAME]))
  3973. && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
  3974. // ----- Look if the filename is in the list
  3975. for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_found); $j++) {
  3976. // ----- Look for a directory
  3977. if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
  3978. // ----- Look if the directory is in the filename path
  3979. if ( (strlen($v_header_list[$v_nb_extracted]['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
  3980. && (substr($v_header_list[$v_nb_extracted]['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
  3981. $v_found = true;
  3982. }
  3983. elseif ( (($v_header_list[$v_nb_extracted]['external']&0x00000010)==0x00000010) /* Indicates a folder */
  3984. && ($v_header_list[$v_nb_extracted]['stored_filename'].'/' == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
  3985. $v_found = true;
  3986. }
  3987. }
  3988. // ----- Look for a filename
  3989. elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
  3990. $v_found = true;
  3991. }
  3992. }
  3993. }
  3994. // ----- Look for extract by ereg rule
  3995. // ereg() is deprecated with PHP 5.3
  3996. /*
  3997. else if ( (isset($p_options[PCLZIP_OPT_BY_EREG]))
  3998. && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
  3999. if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
  4000. $v_found = true;
  4001. }
  4002. }
  4003. */
  4004. // ----- Look for extract by preg rule
  4005. else if ( (isset($p_options[PCLZIP_OPT_BY_PREG]))
  4006. && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
  4007. if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
  4008. $v_found = true;
  4009. }
  4010. }
  4011. // ----- Look for extract by index rule
  4012. else if ( (isset($p_options[PCLZIP_OPT_BY_INDEX]))
  4013. && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
  4014. // ----- Look if the index is in the list
  4015. for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_found); $j++) {
  4016. if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
  4017. $v_found = true;
  4018. }
  4019. if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
  4020. $j_start = $j+1;
  4021. }
  4022. if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
  4023. break;
  4024. }
  4025. }
  4026. }
  4027. else {
  4028. $v_found = true;
  4029. }
  4030. // ----- Look for deletion
  4031. if ($v_found)
  4032. {
  4033. unset($v_header_list[$v_nb_extracted]);
  4034. }
  4035. else
  4036. {
  4037. $v_nb_extracted++;
  4038. }
  4039. }
  4040. // ----- Look if something need to be deleted
  4041. if ($v_nb_extracted > 0) {
  4042. // ----- Creates a temporay file
  4043. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
  4044. // ----- Creates a temporary zip archive
  4045. $v_temp_zip = new PclZip($v_zip_temp_name);
  4046. // ----- Open the temporary zip file in write mode
  4047. if (($v_result = $v_temp_zip->privOpenFd('wb')) != 1) {
  4048. $this->privCloseFd();
  4049. // ----- Return
  4050. return $v_result;
  4051. }
  4052. // ----- Look which file need to be kept
  4053. for ($i=0; $i<sizeof($v_header_list); $i++) {
  4054. // ----- Calculate the position of the header
  4055. @rewind($this->zip_fd);
  4056. if (@fseek($this->zip_fd, $v_header_list[$i]['offset'])) {
  4057. // ----- Close the zip file
  4058. $this->privCloseFd();
  4059. $v_temp_zip->privCloseFd();
  4060. @unlink($v_zip_temp_name);
  4061. // ----- Error log
  4062. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  4063. // ----- Return
  4064. return PclZip::errorCode();
  4065. }
  4066. // ----- Read the file header
  4067. $v_local_header = array();
  4068. if (($v_result = $this->privReadFileHeader($v_local_header)) != 1) {
  4069. // ----- Close the zip file
  4070. $this->privCloseFd();
  4071. $v_temp_zip->privCloseFd();
  4072. @unlink($v_zip_temp_name);
  4073. // ----- Return
  4074. return $v_result;
  4075. }
  4076. // ----- Check that local file header is same as central file header
  4077. if ($this->privCheckFileHeaders($v_local_header,
  4078. $v_header_list[$i]) != 1) {
  4079. // TBC
  4080. }
  4081. unset($v_local_header);
  4082. // ----- Write the file header
  4083. if (($v_result = $v_temp_zip->privWriteFileHeader($v_header_list[$i])) != 1) {
  4084. // ----- Close the zip file
  4085. $this->privCloseFd();
  4086. $v_temp_zip->privCloseFd();
  4087. @unlink($v_zip_temp_name);
  4088. // ----- Return
  4089. return $v_result;
  4090. }
  4091. // ----- Read/write the data block
  4092. if (($v_result = PclZipUtilCopyBlock($this->zip_fd, $v_temp_zip->zip_fd, $v_header_list[$i]['compressed_size'])) != 1) {
  4093. // ----- Close the zip file
  4094. $this->privCloseFd();
  4095. $v_temp_zip->privCloseFd();
  4096. @unlink($v_zip_temp_name);
  4097. // ----- Return
  4098. return $v_result;
  4099. }
  4100. }
  4101. // ----- Store the offset of the central dir
  4102. $v_offset = @ftell($v_temp_zip->zip_fd);
  4103. // ----- Re-Create the Central Dir files header
  4104. for ($i=0; $i<sizeof($v_header_list); $i++) {
  4105. // ----- Create the file header
  4106. if (($v_result = $v_temp_zip->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  4107. $v_temp_zip->privCloseFd();
  4108. $this->privCloseFd();
  4109. @unlink($v_zip_temp_name);
  4110. // ----- Return
  4111. return $v_result;
  4112. }
  4113. // ----- Transform the header to a 'usable' info
  4114. $v_temp_zip->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  4115. }
  4116. // ----- Zip file comment
  4117. $v_comment = '';
  4118. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  4119. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  4120. }
  4121. // ----- Calculate the size of the central header
  4122. $v_size = @ftell($v_temp_zip->zip_fd)-$v_offset;
  4123. // ----- Create the central dir footer
  4124. if (($v_result = $v_temp_zip->privWriteCentralHeader(sizeof($v_header_list), $v_size, $v_offset, $v_comment)) != 1) {
  4125. // ----- Reset the file list
  4126. unset($v_header_list);
  4127. $v_temp_zip->privCloseFd();
  4128. $this->privCloseFd();
  4129. @unlink($v_zip_temp_name);
  4130. // ----- Return
  4131. return $v_result;
  4132. }
  4133. // ----- Close
  4134. $v_temp_zip->privCloseFd();
  4135. $this->privCloseFd();
  4136. // ----- Delete the zip file
  4137. // TBC : I should test the result ...
  4138. @unlink($this->zipname);
  4139. // ----- Rename the temporary file
  4140. // TBC : I should test the result ...
  4141. //@rename($v_zip_temp_name, $this->zipname);
  4142. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  4143. // ----- Destroy the temporary archive
  4144. unset($v_temp_zip);
  4145. }
  4146. // ----- Remove every files : reset the file
  4147. else if ($v_central_dir['entries'] != 0) {
  4148. $this->privCloseFd();
  4149. if (($v_result = $this->privOpenFd('wb')) != 1) {
  4150. return $v_result;
  4151. }
  4152. if (($v_result = $this->privWriteCentralHeader(0, 0, 0, '')) != 1) {
  4153. return $v_result;
  4154. }
  4155. $this->privCloseFd();
  4156. }
  4157. // ----- Return
  4158. return $v_result;
  4159. }
  4160. // --------------------------------------------------------------------------------
  4161. // --------------------------------------------------------------------------------
  4162. // Function : privDirCheck()
  4163. // Description :
  4164. // Check if a directory exists, if not it creates it and all the parents directory
  4165. // which may be useful.
  4166. // Parameters :
  4167. // $p_dir : Directory path to check.
  4168. // Return Values :
  4169. // 1 : OK
  4170. // -1 : Unable to create directory
  4171. // --------------------------------------------------------------------------------
  4172. function privDirCheck($p_dir, $p_is_dir=false)
  4173. {
  4174. $v_result = 1;
  4175. // ----- Remove the final '/'
  4176. if (($p_is_dir) && (substr($p_dir, -1)=='/'))
  4177. {
  4178. $p_dir = substr($p_dir, 0, strlen($p_dir)-1);
  4179. }
  4180. // ----- Check the directory availability
  4181. if ((is_dir($p_dir)) || ($p_dir == ""))
  4182. {
  4183. return 1;
  4184. }
  4185. // ----- Extract parent directory
  4186. $p_parent_dir = dirname($p_dir);
  4187. // ----- Just a check
  4188. if ($p_parent_dir != $p_dir)
  4189. {
  4190. // ----- Look for parent directory
  4191. if ($p_parent_dir != "")
  4192. {
  4193. if (($v_result = $this->privDirCheck($p_parent_dir)) != 1)
  4194. {
  4195. return $v_result;
  4196. }
  4197. }
  4198. }
  4199. // ----- Create the directory
  4200. if (!@mkdir($p_dir, 0777))
  4201. {
  4202. // ----- Error log
  4203. PclZip::privErrorLog(PCLZIP_ERR_DIR_CREATE_FAIL, "Unable to create directory '$p_dir'");
  4204. // ----- Return
  4205. return PclZip::errorCode();
  4206. }
  4207. // ----- Return
  4208. return $v_result;
  4209. }
  4210. // --------------------------------------------------------------------------------
  4211. // --------------------------------------------------------------------------------
  4212. // Function : privMerge()
  4213. // Description :
  4214. // If $p_archive_to_add does not exist, the function exit with a success result.
  4215. // Parameters :
  4216. // Return Values :
  4217. // --------------------------------------------------------------------------------
  4218. function privMerge(&$p_archive_to_add)
  4219. {
  4220. $v_result=1;
  4221. // ----- Look if the archive_to_add exists
  4222. if (!is_file($p_archive_to_add->zipname))
  4223. {
  4224. // ----- Nothing to merge, so merge is a success
  4225. $v_result = 1;
  4226. // ----- Return
  4227. return $v_result;
  4228. }
  4229. // ----- Look if the archive exists
  4230. if (!is_file($this->zipname))
  4231. {
  4232. // ----- Do a duplicate
  4233. $v_result = $this->privDuplicate($p_archive_to_add->zipname);
  4234. // ----- Return
  4235. return $v_result;
  4236. }
  4237. // ----- Open the zip file
  4238. if (($v_result=$this->privOpenFd('rb')) != 1)
  4239. {
  4240. // ----- Return
  4241. return $v_result;
  4242. }
  4243. // ----- Read the central directory informations
  4244. $v_central_dir = array();
  4245. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  4246. {
  4247. $this->privCloseFd();
  4248. return $v_result;
  4249. }
  4250. // ----- Go to beginning of File
  4251. @rewind($this->zip_fd);
  4252. // ----- Open the archive_to_add file
  4253. if (($v_result=$p_archive_to_add->privOpenFd('rb')) != 1)
  4254. {
  4255. $this->privCloseFd();
  4256. // ----- Return
  4257. return $v_result;
  4258. }
  4259. // ----- Read the central directory informations
  4260. $v_central_dir_to_add = array();
  4261. if (($v_result = $p_archive_to_add->privReadEndCentralDir($v_central_dir_to_add)) != 1)
  4262. {
  4263. $this->privCloseFd();
  4264. $p_archive_to_add->privCloseFd();
  4265. return $v_result;
  4266. }
  4267. // ----- Go to beginning of File
  4268. @rewind($p_archive_to_add->zip_fd);
  4269. // ----- Creates a temporay file
  4270. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
  4271. // ----- Open the temporary file in write mode
  4272. if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
  4273. {
  4274. $this->privCloseFd();
  4275. $p_archive_to_add->privCloseFd();
  4276. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
  4277. // ----- Return
  4278. return PclZip::errorCode();
  4279. }
  4280. // ----- Copy the files from the archive to the temporary file
  4281. // TBC : Here I should better append the file and go back to erase the central dir
  4282. $v_size = $v_central_dir['offset'];
  4283. while ($v_size != 0)
  4284. {
  4285. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  4286. $v_buffer = fread($this->zip_fd, $v_read_size);
  4287. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  4288. $v_size -= $v_read_size;
  4289. }
  4290. // ----- Copy the files from the archive_to_add into the temporary file
  4291. $v_size = $v_central_dir_to_add['offset'];
  4292. while ($v_size != 0)
  4293. {
  4294. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  4295. $v_buffer = fread($p_archive_to_add->zip_fd, $v_read_size);
  4296. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  4297. $v_size -= $v_read_size;
  4298. }
  4299. // ----- Store the offset of the central dir
  4300. $v_offset = @ftell($v_zip_temp_fd);
  4301. // ----- Copy the block of file headers from the old archive
  4302. $v_size = $v_central_dir['size'];
  4303. while ($v_size != 0)
  4304. {
  4305. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  4306. $v_buffer = @fread($this->zip_fd, $v_read_size);
  4307. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  4308. $v_size -= $v_read_size;
  4309. }
  4310. // ----- Copy the block of file headers from the archive_to_add
  4311. $v_size = $v_central_dir_to_add['size'];
  4312. while ($v_size != 0)
  4313. {
  4314. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  4315. $v_buffer = @fread($p_archive_to_add->zip_fd, $v_read_size);
  4316. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  4317. $v_size -= $v_read_size;
  4318. }
  4319. // ----- Merge the file comments
  4320. $v_comment = $v_central_dir['comment'].' '.$v_central_dir_to_add['comment'];
  4321. // ----- Calculate the size of the (new) central header
  4322. $v_size = @ftell($v_zip_temp_fd)-$v_offset;
  4323. // ----- Swap the file descriptor
  4324. // Here is a trick : I swap the temporary fd with the zip fd, in order to use
  4325. // the following methods on the temporary fil and not the real archive fd
  4326. $v_swap = $this->zip_fd;
  4327. $this->zip_fd = $v_zip_temp_fd;
  4328. $v_zip_temp_fd = $v_swap;
  4329. // ----- Create the central dir footer
  4330. if (($v_result = $this->privWriteCentralHeader($v_central_dir['entries']+$v_central_dir_to_add['entries'], $v_size, $v_offset, $v_comment)) != 1)
  4331. {
  4332. $this->privCloseFd();
  4333. $p_archive_to_add->privCloseFd();
  4334. @fclose($v_zip_temp_fd);
  4335. $this->zip_fd = null;
  4336. // ----- Reset the file list
  4337. unset($v_header_list);
  4338. // ----- Return
  4339. return $v_result;
  4340. }
  4341. // ----- Swap back the file descriptor
  4342. $v_swap = $this->zip_fd;
  4343. $this->zip_fd = $v_zip_temp_fd;
  4344. $v_zip_temp_fd = $v_swap;
  4345. // ----- Close
  4346. $this->privCloseFd();
  4347. $p_archive_to_add->privCloseFd();
  4348. // ----- Close the temporary file
  4349. @fclose($v_zip_temp_fd);
  4350. // ----- Delete the zip file
  4351. // TBC : I should test the result ...
  4352. @unlink($this->zipname);
  4353. // ----- Rename the temporary file
  4354. // TBC : I should test the result ...
  4355. //@rename($v_zip_temp_name, $this->zipname);
  4356. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  4357. // ----- Return
  4358. return $v_result;
  4359. }
  4360. // --------------------------------------------------------------------------------
  4361. // --------------------------------------------------------------------------------
  4362. // Function : privDuplicate()
  4363. // Description :
  4364. // Parameters :
  4365. // Return Values :
  4366. // --------------------------------------------------------------------------------
  4367. function privDuplicate($p_archive_filename)
  4368. {
  4369. $v_result=1;
  4370. // ----- Look if the $p_archive_filename exists
  4371. if (!is_file($p_archive_filename))
  4372. {
  4373. // ----- Nothing to duplicate, so duplicate is a success.
  4374. $v_result = 1;
  4375. // ----- Return
  4376. return $v_result;
  4377. }
  4378. // ----- Open the zip file
  4379. if (($v_result=$this->privOpenFd('wb')) != 1)
  4380. {
  4381. // ----- Return
  4382. return $v_result;
  4383. }
  4384. // ----- Open the temporary file in write mode
  4385. if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0)
  4386. {
  4387. $this->privCloseFd();
  4388. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive file \''.$p_archive_filename.'\' in binary write mode');
  4389. // ----- Return
  4390. return PclZip::errorCode();
  4391. }
  4392. // ----- Copy the files from the archive to the temporary file
  4393. // TBC : Here I should better append the file and go back to erase the central dir
  4394. $v_size = filesize($p_archive_filename);
  4395. while ($v_size != 0)
  4396. {
  4397. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  4398. $v_buffer = fread($v_zip_temp_fd, $v_read_size);
  4399. @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  4400. $v_size -= $v_read_size;
  4401. }
  4402. // ----- Close
  4403. $this->privCloseFd();
  4404. // ----- Close the temporary file
  4405. @fclose($v_zip_temp_fd);
  4406. // ----- Return
  4407. return $v_result;
  4408. }
  4409. // --------------------------------------------------------------------------------
  4410. // --------------------------------------------------------------------------------
  4411. // Function : privErrorLog()
  4412. // Description :
  4413. // Parameters :
  4414. // --------------------------------------------------------------------------------
  4415. function privErrorLog($p_error_code=0, $p_error_string='')
  4416. {
  4417. if (PCLZIP_ERROR_EXTERNAL == 1) {
  4418. PclError($p_error_code, $p_error_string);
  4419. }
  4420. else {
  4421. $this->error_code = $p_error_code;
  4422. $this->error_string = $p_error_string;
  4423. }
  4424. }
  4425. // --------------------------------------------------------------------------------
  4426. // --------------------------------------------------------------------------------
  4427. // Function : privErrorReset()
  4428. // Description :
  4429. // Parameters :
  4430. // --------------------------------------------------------------------------------
  4431. function privErrorReset()
  4432. {
  4433. if (PCLZIP_ERROR_EXTERNAL == 1) {
  4434. PclErrorReset();
  4435. }
  4436. else {
  4437. $this->error_code = 0;
  4438. $this->error_string = '';
  4439. }
  4440. }
  4441. // --------------------------------------------------------------------------------
  4442. // --------------------------------------------------------------------------------
  4443. // Function : privDisableMagicQuotes()
  4444. // Description :
  4445. // Parameters :
  4446. // Return Values :
  4447. // --------------------------------------------------------------------------------
  4448. function privDisableMagicQuotes()
  4449. {
  4450. $v_result=1;
  4451. // ----- Look if function exists
  4452. if ( (!function_exists("get_magic_quotes_runtime"))
  4453. || (!function_exists("set_magic_quotes_runtime"))) {
  4454. return $v_result;
  4455. }
  4456. // ----- Look if already done
  4457. if ($this->magic_quotes_status != -1) {
  4458. return $v_result;
  4459. }
  4460. // ----- Get and memorize the magic_quote value
  4461. $this->magic_quotes_status = @get_magic_quotes_runtime();
  4462. // ----- Disable magic_quotes
  4463. if ($this->magic_quotes_status == 1) {
  4464. @set_magic_quotes_runtime(0);
  4465. }
  4466. // ----- Return
  4467. return $v_result;
  4468. }
  4469. // --------------------------------------------------------------------------------
  4470. // --------------------------------------------------------------------------------
  4471. // Function : privSwapBackMagicQuotes()
  4472. // Description :
  4473. // Parameters :
  4474. // Return Values :
  4475. // --------------------------------------------------------------------------------
  4476. function privSwapBackMagicQuotes()
  4477. {
  4478. $v_result=1;
  4479. // ----- Look if function exists
  4480. if ( (!function_exists("get_magic_quotes_runtime"))
  4481. || (!function_exists("set_magic_quotes_runtime"))) {
  4482. return $v_result;
  4483. }
  4484. // ----- Look if something to do
  4485. if ($this->magic_quotes_status != -1) {
  4486. return $v_result;
  4487. }
  4488. // ----- Swap back magic_quotes
  4489. if ($this->magic_quotes_status == 1) {
  4490. @set_magic_quotes_runtime($this->magic_quotes_status);
  4491. }
  4492. // ----- Return
  4493. return $v_result;
  4494. }
  4495. // --------------------------------------------------------------------------------
  4496. }
  4497. // End of class
  4498. // --------------------------------------------------------------------------------
  4499. // --------------------------------------------------------------------------------
  4500. // Function : PclZipUtilPathReduction()
  4501. // Description :
  4502. // Parameters :
  4503. // Return Values :
  4504. // --------------------------------------------------------------------------------
  4505. function PclZipUtilPathReduction($p_dir)
  4506. {
  4507. $v_result = "";
  4508. // ----- Look for not empty path
  4509. if ($p_dir != "") {
  4510. // ----- Explode path by directory names
  4511. $v_list = explode("/", $p_dir);
  4512. // ----- Study directories from last to first
  4513. $v_skip = 0;
  4514. for ($i=sizeof($v_list)-1; $i>=0; $i--) {
  4515. // ----- Look for current path
  4516. if ($v_list[$i] == ".") {
  4517. // ----- Ignore this directory
  4518. // Should be the first $i=0, but no check is done
  4519. }
  4520. else if ($v_list[$i] == "..") {
  4521. $v_skip++;
  4522. }
  4523. else if ($v_list[$i] == "") {
  4524. // ----- First '/' i.e. root slash
  4525. if ($i == 0) {
  4526. $v_result = "/".$v_result;
  4527. if ($v_skip > 0) {
  4528. // ----- It is an invalid path, so the path is not modified
  4529. // TBC
  4530. $v_result = $p_dir;
  4531. $v_skip = 0;
  4532. }
  4533. }
  4534. // ----- Last '/' i.e. indicates a directory
  4535. else if ($i == (sizeof($v_list)-1)) {
  4536. $v_result = $v_list[$i];
  4537. }
  4538. // ----- Double '/' inside the path
  4539. else {
  4540. // ----- Ignore only the double '//' in path,
  4541. // but not the first and last '/'
  4542. }
  4543. }
  4544. else {
  4545. // ----- Look for item to skip
  4546. if ($v_skip > 0) {
  4547. $v_skip--;
  4548. }
  4549. else {
  4550. $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?"/".$v_result:"");
  4551. }
  4552. }
  4553. }
  4554. // ----- Look for skip
  4555. if ($v_skip > 0) {
  4556. while ($v_skip > 0) {
  4557. $v_result = '../'.$v_result;
  4558. $v_skip--;
  4559. }
  4560. }
  4561. }
  4562. // ----- Return
  4563. return $v_result;
  4564. }
  4565. // --------------------------------------------------------------------------------
  4566. // --------------------------------------------------------------------------------
  4567. // Function : PclZipUtilPathInclusion()
  4568. // Description :
  4569. // This function indicates if the path $p_path is under the $p_dir tree. Or,
  4570. // said in an other way, if the file or sub-dir $p_path is inside the dir
  4571. // $p_dir.
  4572. // The function indicates also if the path is exactly the same as the dir.
  4573. // This function supports path with duplicated '/' like '//', but does not
  4574. // support '.' or '..' statements.
  4575. // Parameters :
  4576. // Return Values :
  4577. // 0 if $p_path is not inside directory $p_dir
  4578. // 1 if $p_path is inside directory $p_dir
  4579. // 2 if $p_path is exactly the same as $p_dir
  4580. // --------------------------------------------------------------------------------
  4581. function PclZipUtilPathInclusion($p_dir, $p_path)
  4582. {
  4583. $v_result = 1;
  4584. // ----- Look for path beginning by ./
  4585. if ( ($p_dir == '.')
  4586. || ((strlen($p_dir) >=2) && (substr($p_dir, 0, 2) == './'))) {
  4587. $p_dir = PclZipUtilTranslateWinPath(getcwd(), FALSE).'/'.substr($p_dir, 1);
  4588. }
  4589. if ( ($p_path == '.')
  4590. || ((strlen($p_path) >=2) && (substr($p_path, 0, 2) == './'))) {
  4591. $p_path = PclZipUtilTranslateWinPath(getcwd(), FALSE).'/'.substr($p_path, 1);
  4592. }
  4593. // ----- Explode dir and path by directory separator
  4594. $v_list_dir = explode("/", $p_dir);
  4595. $v_list_dir_size = sizeof($v_list_dir);
  4596. $v_list_path = explode("/", $p_path);
  4597. $v_list_path_size = sizeof($v_list_path);
  4598. // ----- Study directories paths
  4599. $i = 0;
  4600. $j = 0;
  4601. while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
  4602. // ----- Look for empty dir (path reduction)
  4603. if ($v_list_dir[$i] == '') {
  4604. $i++;
  4605. continue;
  4606. }
  4607. if ($v_list_path[$j] == '') {
  4608. $j++;
  4609. continue;
  4610. }
  4611. // ----- Compare the items
  4612. if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ( $v_list_path[$j] != '')) {
  4613. $v_result = 0;
  4614. }
  4615. // ----- Next items
  4616. $i++;
  4617. $j++;
  4618. }
  4619. // ----- Look if everything seems to be the same
  4620. if ($v_result) {
  4621. // ----- Skip all the empty items
  4622. while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) $j++;
  4623. while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) $i++;
  4624. if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
  4625. // ----- There are exactly the same
  4626. $v_result = 2;
  4627. }
  4628. else if ($i < $v_list_dir_size) {
  4629. // ----- The path is shorter than the dir
  4630. $v_result = 0;
  4631. }
  4632. }
  4633. // ----- Return
  4634. return $v_result;
  4635. }
  4636. // --------------------------------------------------------------------------------
  4637. // --------------------------------------------------------------------------------
  4638. // Function : PclZipUtilCopyBlock()
  4639. // Description :
  4640. // Parameters :
  4641. // $p_mode : read/write compression mode
  4642. // 0 : src & dest normal
  4643. // 1 : src gzip, dest normal
  4644. // 2 : src normal, dest gzip
  4645. // 3 : src & dest gzip
  4646. // Return Values :
  4647. // --------------------------------------------------------------------------------
  4648. function PclZipUtilCopyBlock($p_src, $p_dest, $p_size, $p_mode=0)
  4649. {
  4650. $v_result = 1;
  4651. if ($p_mode==0)
  4652. {
  4653. while ($p_size != 0)
  4654. {
  4655. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  4656. $v_buffer = @fread($p_src, $v_read_size);
  4657. @fwrite($p_dest, $v_buffer, $v_read_size);
  4658. $p_size -= $v_read_size;
  4659. }
  4660. }
  4661. else if ($p_mode==1)
  4662. {
  4663. while ($p_size != 0)
  4664. {
  4665. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  4666. $v_buffer = @gzread($p_src, $v_read_size);
  4667. @fwrite($p_dest, $v_buffer, $v_read_size);
  4668. $p_size -= $v_read_size;
  4669. }
  4670. }
  4671. else if ($p_mode==2)
  4672. {
  4673. while ($p_size != 0)
  4674. {
  4675. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  4676. $v_buffer = @fread($p_src, $v_read_size);
  4677. @gzwrite($p_dest, $v_buffer, $v_read_size);
  4678. $p_size -= $v_read_size;
  4679. }
  4680. }
  4681. else if ($p_mode==3)
  4682. {
  4683. while ($p_size != 0)
  4684. {
  4685. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  4686. $v_buffer = @gzread($p_src, $v_read_size);
  4687. @gzwrite($p_dest, $v_buffer, $v_read_size);
  4688. $p_size -= $v_read_size;
  4689. }
  4690. }
  4691. // ----- Return
  4692. return $v_result;
  4693. }
  4694. // --------------------------------------------------------------------------------
  4695. // --------------------------------------------------------------------------------
  4696. // Function : PclZipUtilRename()
  4697. // Description :
  4698. // This function tries to do a simple rename() function. If it fails, it
  4699. // tries to copy the $p_src file in a new $p_dest file and then unlink the
  4700. // first one.
  4701. // Parameters :
  4702. // $p_src : Old filename
  4703. // $p_dest : New filename
  4704. // Return Values :
  4705. // 1 on success, 0 on failure.
  4706. // --------------------------------------------------------------------------------
  4707. function PclZipUtilRename($p_src, $p_dest)
  4708. {
  4709. $v_result = 1;
  4710. // ----- Try to rename the files
  4711. if (!@rename($p_src, $p_dest)) {
  4712. // ----- Try to copy & unlink the src
  4713. if (!@copy($p_src, $p_dest)) {
  4714. $v_result = 0;
  4715. }
  4716. else if (!@unlink($p_src)) {
  4717. $v_result = 0;
  4718. }
  4719. }
  4720. // ----- Return
  4721. return $v_result;
  4722. }
  4723. // --------------------------------------------------------------------------------
  4724. // --------------------------------------------------------------------------------
  4725. // Function : PclZipUtilOptionText()
  4726. // Description :
  4727. // Translate option value in text. Mainly for debug purpose.
  4728. // Parameters :
  4729. // $p_option : the option value.
  4730. // Return Values :
  4731. // The option text value.
  4732. // --------------------------------------------------------------------------------
  4733. function PclZipUtilOptionText($p_option)
  4734. {
  4735. $v_list = get_defined_constants();
  4736. for (reset($v_list); $v_key = key($v_list); next($v_list)) {
  4737. $v_prefix = substr($v_key, 0, 10);
  4738. if (( ($v_prefix == 'PCLZIP_OPT')
  4739. || ($v_prefix == 'PCLZIP_CB_')
  4740. || ($v_prefix == 'PCLZIP_ATT'))
  4741. && ($v_list[$v_key] == $p_option)) {
  4742. return $v_key;
  4743. }
  4744. }
  4745. $v_result = 'Unknown';
  4746. return $v_result;
  4747. }
  4748. // --------------------------------------------------------------------------------
  4749. // --------------------------------------------------------------------------------
  4750. // Function : PclZipUtilTranslateWinPath()
  4751. // Description :
  4752. // Translate windows path by replacing '\' by '/' and optionally removing
  4753. // drive letter.
  4754. // Parameters :
  4755. // $p_path : path to translate.
  4756. // $p_remove_disk_letter : true | false
  4757. // Return Values :
  4758. // The path translated.
  4759. // --------------------------------------------------------------------------------
  4760. function PclZipUtilTranslateWinPath($p_path, $p_remove_disk_letter=true)
  4761. {
  4762. if (stristr(php_uname(), 'windows')) {
  4763. // ----- Look for potential disk letter
  4764. if (($p_remove_disk_letter) && (($v_position = strpos($p_path, ':')) != false)) {
  4765. $p_path = substr($p_path, $v_position+1);
  4766. }
  4767. // ----- Change potential windows directory separator
  4768. if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0,1) == '\\')) {
  4769. $p_path = strtr($p_path, '\\', '/');
  4770. }
  4771. }
  4772. return $p_path;
  4773. }
  4774. // --------------------------------------------------------------------------------
  4775. ?>