PageRenderTime 111ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 1ms

/administrator/includes/pcl/pclzip.lib.php

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