You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

5545 lines
255 KiB

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