Просмотр файла mc-2.7.0/libraries/pclzip.lib.php

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