PageRenderTime 501ms CodeModel.GetById 68ms RepoModel.GetById 2ms app.codeStats 2ms

/lib/pclzip/pclzip.lib.php

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