Просмотр файла catalogdeep/test/pclzip.lib.php

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