PageRenderTime 75ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/includes/phpexcel/PHPExcel/Shared/PCLZip/pclzip.lib.php

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