Просмотр файла system/libs/pclzip.lib.php

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