PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

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