PageRenderTime 135ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 2ms

/sondage/admin/classes/pclzip/pclzip-trace.lib.php

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