PageRenderTime 75ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 2ms

/wp-admin/includes/class-pclzip.php

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