PageRenderTime 44ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/wp-all-import/libraries/pclzip.lib.php

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