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

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