PageRenderTime 51ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/administrator/includes/pcl/pclzip.lib.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 4978 lines | 2233 code | 722 blank | 2023 comment | 677 complexity | 6a8d1fe91a1f34a7533ffe3a98145b9f MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * @version $Id: pclzip.lib.php 768 2005-10-31 10:32:18Z stingrey $
  4. * @package Joomla
  5. */
  6. // no direct access
  7. defined( '_VALID_MOS' ) or die( 'Restricted access' );
  8. // --------------------------------------------------------------------------------
  9. // PhpConcept Library - Zip Module 2.1
  10. // --------------------------------------------------------------------------------
  11. // License GNU/LGPL - Vincent Blavet - December 2003
  12. // http://www.phpconcept.net
  13. // --------------------------------------------------------------------------------
  14. //
  15. // Presentation :
  16. // PclZip is a PHP library that manage ZIP archives.
  17. // So far tests show that archives generated by PclZip are readable by
  18. // WinZip application and other tools.
  19. //
  20. // Description :
  21. // See readme.txt and http://www.phpconcept.net
  22. //
  23. // Warning :
  24. // This library and the associated files are non commercial, non professional
  25. // work.
  26. // It should not have unexpected results. However if any damage is caused by
  27. // this software the author can not be responsible.
  28. // The use of this software is at the risk of the user.
  29. //
  30. // --------------------------------------------------------------------------------
  31. // $Id: pclzip.lib.php 768 2005-10-31 10:32:18Z stingrey $
  32. // --------------------------------------------------------------------------------
  33. // ----- Constants
  34. define( 'PCLZIP_READ_BLOCK_SIZE', 2048 );
  35. // ----- File list separator
  36. // In version 1.x of PclZip, the separator for file list is a space
  37. // (which is not a very smart choice, specifically for windows paths !).
  38. // A better separator should be a comma (,). This constant gives you the
  39. // abilty to change that.
  40. // However notice that changing this value, may have impact on existing
  41. // scripts, using space separated filenames.
  42. // Recommanded values for compatibility with older versions :
  43. //define( 'PCLZIP_SEPARATOR', ' ' );
  44. // Recommanded values for smart separation of filenames.
  45. define( 'PCLZIP_SEPARATOR', ',' );
  46. // ----- Error configuration
  47. // 0 : PclZip Class integrated error handling
  48. // 1 : PclError external library error handling. By enabling this
  49. // you must ensure that you have included PclError library.
  50. // [2,...] : reserved for futur use
  51. define( 'PCLZIP_ERROR_EXTERNAL', 0 );
  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. define( 'PCLZIP_TEMPORARY_DIR', '' );
  62. // --------------------------------------------------------------------------------
  63. // ***** UNDER THIS LINE NOTHING NEEDS TO BE MODIFIED *****
  64. // --------------------------------------------------------------------------------
  65. // ----- Global variables
  66. $g_pclzip_version = "2.1";
  67. // ----- Error codes
  68. // -1 : Unable to open file in binary write mode
  69. // -2 : Unable to open file in binary read mode
  70. // -3 : Invalid parameters
  71. // -4 : File does not exist
  72. // -5 : Filename is too long (max. 255)
  73. // -6 : Not a valid zip file
  74. // -7 : Invalid extracted file size
  75. // -8 : Unable to create directory
  76. // -9 : Invalid archive extension
  77. // -10 : Invalid archive format
  78. // -11 : Unable to delete file (unlink)
  79. // -12 : Unable to rename file (rename)
  80. // -13 : Invalid header checksum
  81. // -14 : Invalid archive size
  82. define( 'PCLZIP_ERR_USER_ABORTED', 2 );
  83. define( 'PCLZIP_ERR_NO_ERROR', 0 );
  84. define( 'PCLZIP_ERR_WRITE_OPEN_FAIL', -1 );
  85. define( 'PCLZIP_ERR_READ_OPEN_FAIL', -2 );
  86. define( 'PCLZIP_ERR_INVALID_PARAMETER', -3 );
  87. define( 'PCLZIP_ERR_MISSING_FILE', -4 );
  88. define( 'PCLZIP_ERR_FILENAME_TOO_LONG', -5 );
  89. define( 'PCLZIP_ERR_INVALID_ZIP', -6 );
  90. define( 'PCLZIP_ERR_BAD_EXTRACTED_FILE', -7 );
  91. define( 'PCLZIP_ERR_DIR_CREATE_FAIL', -8 );
  92. define( 'PCLZIP_ERR_BAD_EXTENSION', -9 );
  93. define( 'PCLZIP_ERR_BAD_FORMAT', -10 );
  94. define( 'PCLZIP_ERR_DELETE_FILE_FAIL', -11 );
  95. define( 'PCLZIP_ERR_RENAME_FILE_FAIL', -12 );
  96. define( 'PCLZIP_ERR_BAD_CHECKSUM', -13 );
  97. define( 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP', -14 );
  98. define( 'PCLZIP_ERR_MISSING_OPTION_VALUE', -15 );
  99. define( 'PCLZIP_ERR_INVALID_OPTION_VALUE', -16 );
  100. // ----- Options values
  101. define( 'PCLZIP_OPT_PATH', 77001 );
  102. define( 'PCLZIP_OPT_ADD_PATH', 77002 );
  103. define( 'PCLZIP_OPT_REMOVE_PATH', 77003 );
  104. define( 'PCLZIP_OPT_REMOVE_ALL_PATH', 77004 );
  105. define( 'PCLZIP_OPT_SET_CHMOD', 77005 );
  106. define( 'PCLZIP_OPT_EXTRACT_AS_STRING', 77006 );
  107. define( 'PCLZIP_OPT_NO_COMPRESSION', 77007 );
  108. define( 'PCLZIP_OPT_BY_NAME', 77008 );
  109. define( 'PCLZIP_OPT_BY_INDEX', 77009 );
  110. define( 'PCLZIP_OPT_BY_EREG', 77010 );
  111. define( 'PCLZIP_OPT_BY_PREG', 77011 );
  112. define( 'PCLZIP_OPT_COMMENT', 77012 );
  113. define( 'PCLZIP_OPT_ADD_COMMENT', 77013 );
  114. define( 'PCLZIP_OPT_PREPEND_COMMENT', 77014 );
  115. define( 'PCLZIP_OPT_EXTRACT_IN_OUTPUT', 77015 );
  116. // ----- Call backs values
  117. define( 'PCLZIP_CB_PRE_EXTRACT', 78001 );
  118. define( 'PCLZIP_CB_POST_EXTRACT', 78002 );
  119. define( 'PCLZIP_CB_PRE_ADD', 78003 );
  120. define( 'PCLZIP_CB_POST_ADD', 78004 );
  121. /* For futur use
  122. define( 'PCLZIP_CB_PRE_LIST', 78005 );
  123. define( 'PCLZIP_CB_POST_LIST', 78006 );
  124. define( 'PCLZIP_CB_PRE_DELETE', 78007 );
  125. define( 'PCLZIP_CB_POST_DELETE', 78008 );
  126. */
  127. // --------------------------------------------------------------------------------
  128. // Class : PclZip
  129. // Description :
  130. // PclZip is the class that represent a Zip archive.
  131. // The public methods allow the manipulation of the archive.
  132. // Attributes :
  133. // Attributes must not be accessed directly.
  134. // Methods :
  135. // PclZip() : Object creator
  136. // create() : Creates the Zip archive
  137. // listContent() : List the content of the Zip archive
  138. // extract() : Extract the content of the archive
  139. // properties() : List the properties of the archive
  140. // --------------------------------------------------------------------------------
  141. class PclZip
  142. {
  143. // ----- Filename of the zip file
  144. var $zipname = '';
  145. // ----- File descriptor of the zip file
  146. var $zip_fd = 0;
  147. // ----- Internal error handling
  148. var $error_code = 1;
  149. var $error_string = '';
  150. // --------------------------------------------------------------------------------
  151. // Function : PclZip()
  152. // Description :
  153. // Creates a PclZip object and set the name of the associated Zip archive
  154. // filename.
  155. // Note that no real action is taken, if the archive does not exist it is not
  156. // created. Use create() for that.
  157. // --------------------------------------------------------------------------------
  158. function PclZip($p_zipname)
  159. {
  160. //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::PclZip', "zipname=$p_zipname");
  161. // ----- Tests the zlib
  162. if (!function_exists('gzopen'))
  163. {
  164. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 1, "zlib extension seems to be missing");
  165. die('Abort '.basename(__FILE__).' : Missing zlib extensions');
  166. }
  167. // ----- Set the attributes
  168. $this->zipname = $p_zipname;
  169. $this->zip_fd = 0;
  170. // ----- Return
  171. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 1);
  172. return;
  173. }
  174. // --------------------------------------------------------------------------------
  175. // --------------------------------------------------------------------------------
  176. // Function :
  177. // create($p_filelist, $p_add_dir="", $p_remove_dir="")
  178. // create($p_filelist, $p_option, $p_option_value, ...)
  179. // Description :
  180. // This method supports two different synopsis. The first one is historical.
  181. // This method creates a Zip Archive. The Zip file is created in the
  182. // filesystem. The files and directories indicated in $p_filelist
  183. // are added in the archive. See the parameters description for the
  184. // supported format of $p_filelist.
  185. // When a directory is in the list, the directory and its content is added
  186. // in the archive.
  187. // In this synopsis, the function takes an optional variable list of
  188. // options. See bellow the supported options.
  189. // Parameters :
  190. // $p_filelist : An array containing file or directory names, or
  191. // a string containing one filename or one directory name, or
  192. // a string containing a list of filenames and/or directory
  193. // names separated by spaces.
  194. // $p_add_dir : A path to add before the real path of the archived file,
  195. //in order to have it memorized in the archive.
  196. // $p_remove_dir : A path to remove from the real path of the file to archive,
  197. // in order to have a shorter path memorized in the archive.
  198. // When $p_add_dir and $p_remove_dir are set, $p_remove_dir
  199. // is removed first, before $p_add_dir is added.
  200. // Options :
  201. // PCLZIP_OPT_ADD_PATH :
  202. // PCLZIP_OPT_REMOVE_PATH :
  203. // PCLZIP_OPT_REMOVE_ALL_PATH :
  204. // PCLZIP_OPT_COMMENT :
  205. // PCLZIP_CB_PRE_ADD :
  206. // PCLZIP_CB_POST_ADD :
  207. // Return Values :
  208. // 0 on failure,
  209. // The list of the added files, with a status of the add action.
  210. // (see PclZip::listContent() for list entry format)
  211. // --------------------------------------------------------------------------------
  212. // function create($p_filelist, $p_add_dir="", $p_remove_dir="")
  213. function create($p_filelist /*, options */)
  214. {
  215. //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::create', "filelist='$p_filelist', ...");
  216. $v_result=1;
  217. // ----- Reset the error handler
  218. $this->privErrorReset();
  219. // ----- Set default values
  220. $v_options = array();
  221. $v_add_path = "";
  222. $v_remove_path = "";
  223. $v_remove_all_path = false;
  224. $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
  225. // ----- Look for variable options arguments
  226. $v_size = func_num_args();
  227. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
  228. // ----- Look for arguments
  229. if ($v_size > 1) {
  230. // ----- Get the arguments
  231. $v_arg_list = &func_get_args();
  232. // ----- Remove form the options list the first argument
  233. array_shift($v_arg_list);
  234. $v_size--;
  235. // ----- Look for first arg
  236. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  237. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options detected");
  238. // ----- Parse the options
  239. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  240. array (PCLZIP_OPT_REMOVE_PATH => 'optional',
  241. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  242. PCLZIP_OPT_ADD_PATH => 'optional',
  243. PCLZIP_CB_PRE_ADD => 'optional',
  244. PCLZIP_CB_POST_ADD => 'optional',
  245. PCLZIP_OPT_NO_COMPRESSION => 'optional',
  246. PCLZIP_OPT_COMMENT => 'optional' ));
  247. if ($v_result != 1) {
  248. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
  249. return 0;
  250. }
  251. // ----- Set the arguments
  252. if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
  253. $v_add_path = $v_options[PCLZIP_OPT_ADD_PATH];
  254. }
  255. if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
  256. $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
  257. }
  258. if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  259. $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  260. }
  261. }
  262. // ----- Look for 2 args
  263. // Here we need to support the first historic synopsis of the
  264. // method.
  265. else {
  266. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
  267. // ----- Get the first argument
  268. $v_add_path = $v_arg_list[0];
  269. // ----- Look for the optional second argument
  270. if ($v_size == 2) {
  271. $v_remove_path = $v_arg_list[1];
  272. }
  273. else if ($v_size > 2) {
  274. // ----- Error log
  275. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
  276. "Invalid number / type of arguments");
  277. // ----- Return
  278. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
  279. return 0;
  280. }
  281. }
  282. }
  283. // ----- Trace
  284. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "add_path='$v_add_path', remove_path='$v_remove_path', remove_all_path='".($v_remove_all_path?'true':'false')."'");
  285. // ----- Look if the $p_filelist is really an array
  286. $p_result_list = array();
  287. if (is_array($p_filelist))
  288. {
  289. // ----- Call the create fct
  290. $v_result = $this->privCreate($p_filelist, $p_result_list, $v_add_path, $v_remove_path, $v_remove_all_path, $v_options);
  291. }
  292. // ----- Look if the $p_filelist is a string
  293. else if (is_string($p_filelist))
  294. {
  295. // ----- Create a list with the elements from the string
  296. $v_list = explode(PCLZIP_SEPARATOR, $p_filelist);
  297. // ----- Call the create fct
  298. $v_result = $this->privCreate($v_list, $p_result_list, $v_add_path, $v_remove_path, $v_remove_all_path, $v_options);
  299. }
  300. // ----- Invalid variable
  301. else
  302. {
  303. // ----- Error log
  304. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
  305. $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  306. }
  307. if ($v_result != 1)
  308. {
  309. // ----- Return
  310. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
  311. return 0;
  312. }
  313. // ----- Return
  314. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_result_list);
  315. return $p_result_list;
  316. }
  317. // --------------------------------------------------------------------------------
  318. // --------------------------------------------------------------------------------
  319. // Function :
  320. // add($p_filelist, $p_add_dir="", $p_remove_dir="")
  321. // add($p_filelist, $p_option, $p_option_value, ...)
  322. // Description :
  323. // This method supports two synopsis. The first one is historical.
  324. // This methods add the list of files in an existing archive.
  325. // If a file with the same name already exists, it is added at the end of the
  326. // archive, the first one is still present.
  327. // If the archive does not exist, it is created.
  328. // Parameters :
  329. // $p_filelist : An array containing file or directory names, or
  330. // a string containing one filename or one directory name, or
  331. // a string containing a list of filenames and/or directory
  332. // names separated by spaces.
  333. // $p_add_dir : A path to add before the real path of the archived file,
  334. //in order to have it memorized in the archive.
  335. // $p_remove_dir : A path to remove from the real path of the file to archive,
  336. // in order to have a shorter path memorized in the archive.
  337. // When $p_add_dir and $p_remove_dir are set, $p_remove_dir
  338. // is removed first, before $p_add_dir is added.
  339. // Options :
  340. // PCLZIP_OPT_ADD_PATH :
  341. // PCLZIP_OPT_REMOVE_PATH :
  342. // PCLZIP_OPT_REMOVE_ALL_PATH :
  343. // PCLZIP_OPT_COMMENT :
  344. // PCLZIP_OPT_ADD_COMMENT :
  345. // PCLZIP_OPT_PREPEND_COMMENT :
  346. // PCLZIP_CB_PRE_ADD :
  347. // PCLZIP_CB_POST_ADD :
  348. // Return Values :
  349. // 0 on failure,
  350. // The list of the added files, with a status of the add action.
  351. // (see PclZip::listContent() for list entry format)
  352. // --------------------------------------------------------------------------------
  353. // function add($p_filelist, $p_add_dir="", $p_remove_dir="")
  354. function add($p_filelist /* options */)
  355. {
  356. //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::add', "filelist='$p_filelist', ...");
  357. $v_result=1;
  358. // ----- Reset the error handler
  359. $this->privErrorReset();
  360. // ----- Set default values
  361. $v_options = array();
  362. $v_add_path = "";
  363. $v_remove_path = "";
  364. $v_remove_all_path = false;
  365. $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
  366. // ----- Look for variable options arguments
  367. $v_size = func_num_args();
  368. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
  369. // ----- Look for arguments
  370. if ($v_size > 1) {
  371. // ----- Get the arguments
  372. $v_arg_list = &func_get_args();
  373. // ----- Remove form the options list the first argument
  374. array_shift($v_arg_list);
  375. $v_size--;
  376. // ----- Look for first arg
  377. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  378. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options detected");
  379. // ----- Parse the options
  380. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  381. array (PCLZIP_OPT_REMOVE_PATH => 'optional',
  382. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  383. PCLZIP_OPT_ADD_PATH => 'optional',
  384. PCLZIP_CB_PRE_ADD => 'optional',
  385. PCLZIP_CB_POST_ADD => 'optional',
  386. PCLZIP_OPT_NO_COMPRESSION => 'optional',
  387. PCLZIP_OPT_COMMENT => 'optional',
  388. PCLZIP_OPT_ADD_COMMENT => 'optional',
  389. PCLZIP_OPT_PREPEND_COMMENT => 'optional' ));
  390. if ($v_result != 1) {
  391. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
  392. return 0;
  393. }
  394. // ----- Set the arguments
  395. if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
  396. $v_add_path = $v_options[PCLZIP_OPT_ADD_PATH];
  397. }
  398. if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
  399. $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
  400. }
  401. if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  402. $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  403. }
  404. }
  405. // ----- Look for 2 args
  406. // Here we need to support the first historic synopsis of the
  407. // method.
  408. else {
  409. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
  410. // ----- Get the first argument
  411. $v_add_path = $v_arg_list[0];
  412. // ----- Look for the optional second argument
  413. if ($v_size == 2) {
  414. $v_remove_path = $v_arg_list[1];
  415. }
  416. else if ($v_size > 2) {
  417. // ----- Error log
  418. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  419. // ----- Return
  420. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
  421. return 0;
  422. }
  423. }
  424. }
  425. // ----- Trace
  426. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "add_path='$v_add_path', remove_path='$v_remove_path', remove_all_path='".($v_remove_all_path?'true':'false')."'");
  427. // ----- Look if the $p_filelist is really an array
  428. $p_result_list = array();
  429. if (is_array($p_filelist))
  430. {
  431. // ----- Call the create fct
  432. $v_result = $this->privAdd($p_filelist, $p_result_list, $v_add_path, $v_remove_path, $v_remove_all_path, $v_options);
  433. }
  434. // ----- Look if the $p_filelist is a string
  435. else if (is_string($p_filelist))
  436. {
  437. // ----- Create a list with the elements from the string
  438. $v_list = explode(PCLZIP_SEPARATOR, $p_filelist);
  439. // ----- Call the create fct
  440. $v_result = $this->privAdd($v_list, $p_result_list, $v_add_path, $v_remove_path, $v_remove_all_path, $v_options);
  441. }
  442. // ----- Invalid variable
  443. else
  444. {
  445. // ----- Error log
  446. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
  447. $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  448. }
  449. if ($v_result != 1)
  450. {
  451. // ----- Return
  452. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
  453. return 0;
  454. }
  455. // ----- Return
  456. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_result_list);
  457. return $p_result_list;
  458. }
  459. // --------------------------------------------------------------------------------
  460. // --------------------------------------------------------------------------------
  461. // Function : listContent()
  462. // Description :
  463. // This public method, gives the list of the files and directories, with their
  464. // properties.
  465. // The properties of each entries in the list are (used also in other functions) :
  466. // filename : Name of the file. For a create or add action it is the filename
  467. //given by the user. For an extract function it is the filename
  468. //of the extracted file.
  469. // stored_filename : Name of the file / directory stored in the archive.
  470. // size : Size of the stored file.
  471. // compressed_size : Size of the file's data compressed in the archive
  472. // (without the headers overhead)
  473. // mtime : Last known modification date of the file (UNIX timestamp)
  474. // comment : Comment associated with the file
  475. // folder : true | false
  476. // index : index of the file in the archive
  477. // status : status of the action (depending of the action) :
  478. // Values are :
  479. //ok : OK !
  480. //filtered : the file / dir is not extracted (filtered by user)
  481. //already_a_directory : the file can not be extracted because a
  482. // directory with the same name already exists
  483. //write_protected : the file can not be extracted because a file
  484. // with the same name already exists and is
  485. // write protected
  486. //newer_exist : the file was not extracted because a newer file exists
  487. //path_creation_fail : the file is not extracted because the folder
  488. // does not exists and can not be created
  489. //write_error : the file was not extracted because there was a
  490. // error while writing the file
  491. //read_error : the file was not extracted because there was a error
  492. // while reading the file
  493. //invalid_header : the file was not extracted because of an archive
  494. // format error (bad file header)
  495. // Note that each time a method can continue operating when there
  496. // is an action error on a file, the error is only logged in the file status.
  497. // Return Values :
  498. // 0 on an unrecoverable failure,
  499. // The list of the files in the archive.
  500. // --------------------------------------------------------------------------------
  501. function listContent()
  502. {
  503. //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::listContent', "");
  504. $v_result=1;
  505. // ----- Reset the error handler
  506. $this->privErrorReset();
  507. // ----- Check archive
  508. if (!$this->privCheckFormat()) {
  509. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
  510. return(0);
  511. }
  512. // ----- Call the extracting fct
  513. $p_list = array();
  514. if (($v_result = $this->privList($p_list)) != 1)
  515. {
  516. unset($p_list);
  517. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
  518. return(0);
  519. }
  520. // ----- Return
  521. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
  522. return $p_list;
  523. }
  524. // --------------------------------------------------------------------------------
  525. // --------------------------------------------------------------------------------
  526. // Function :
  527. // extract($p_path="./", $p_remove_path="")
  528. // extract([$p_option, $p_option_value, ...])
  529. // Description :
  530. // This method supports two synopsis. The first one is historical.
  531. // This method extract all the files / directories from the archive to the
  532. // folder indicated in $p_path.
  533. // If you want to ignore the 'root' part of path of the memorized files
  534. // you can indicate this in the optional $p_remove_path parameter.
  535. // By default, if a newer file with the same name already exists, the
  536. // file is not extracted.
  537. //
  538. // If both PCLZIP_OPT_PATH and PCLZIP_OPT_ADD_PATH aoptions
  539. // are used, the path indicated in PCLZIP_OPT_ADD_PATH is append
  540. // at the end of the path value of PCLZIP_OPT_PATH.
  541. // Parameters :
  542. // $p_path : Path where the files and directories are to be extracted
  543. // $p_remove_path : First part ('root' part) of the memorized path
  544. //(if any similar) to remove while extracting.
  545. // Options :
  546. // PCLZIP_OPT_PATH :
  547. // PCLZIP_OPT_ADD_PATH :
  548. // PCLZIP_OPT_REMOVE_PATH :
  549. // PCLZIP_OPT_REMOVE_ALL_PATH :
  550. // PCLZIP_CB_PRE_EXTRACT :
  551. // PCLZIP_CB_POST_EXTRACT :
  552. // Return Values :
  553. // 0 or a negative value on failure,
  554. // The list of the extracted files, with a status of the action.
  555. // (see PclZip::listContent() for list entry format)
  556. // --------------------------------------------------------------------------------
  557. //function extract($p_path="./", $p_remove_path="")
  558. function extract(/* options */)
  559. {
  560. //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::extract", "");
  561. $v_result=1;
  562. // ----- Reset the error handler
  563. $this->privErrorReset();
  564. // ----- Check archive
  565. if (!$this->privCheckFormat()) {
  566. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
  567. return(0);
  568. }
  569. // ----- Set default values
  570. $v_options = array();
  571. $v_path = "./";
  572. $v_remove_path = "";
  573. $v_remove_all_path = false;
  574. // ----- Look for variable options arguments
  575. $v_size = func_num_args();
  576. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
  577. // ----- Default values for option
  578. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
  579. // ----- Look for arguments
  580. if ($v_size > 0) {
  581. // ----- Get the arguments
  582. $v_arg_list = func_get_args();
  583. // ----- Look for first arg
  584. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  585. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options");
  586. // ----- Parse the options
  587. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  588. array (PCLZIP_OPT_PATH => 'optional',
  589. PCLZIP_OPT_REMOVE_PATH => 'optional',
  590. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  591. PCLZIP_OPT_ADD_PATH => 'optional',
  592. PCLZIP_CB_PRE_EXTRACT => 'optional',
  593. PCLZIP_CB_POST_EXTRACT => 'optional',
  594. PCLZIP_OPT_SET_CHMOD => 'optional',
  595. PCLZIP_OPT_BY_NAME => 'optional',
  596. PCLZIP_OPT_BY_EREG => 'optional',
  597. PCLZIP_OPT_BY_PREG => 'optional',
  598. PCLZIP_OPT_BY_INDEX => 'optional',
  599. PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
  600. PCLZIP_OPT_EXTRACT_IN_OUTPUT => 'optional' ));
  601. if ($v_result != 1) {
  602. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
  603. return 0;
  604. }
  605. // ----- Set the arguments
  606. if (isset($v_options[PCLZIP_OPT_PATH])) {
  607. $v_path = $v_options[PCLZIP_OPT_PATH];
  608. }
  609. if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
  610. $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
  611. }
  612. if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  613. $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  614. }
  615. if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
  616. // ----- Check for '/' in last path char
  617. if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
  618. $v_path .= '/';
  619. }
  620. $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
  621. }
  622. }
  623. // ----- Look for 2 args
  624. // Here we need to support the first historic synopsis of the
  625. // method.
  626. else {
  627. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
  628. // ----- Get the first argument
  629. $v_path = $v_arg_list[0];
  630. // ----- Look for the optional second argument
  631. if ($v_size == 2) {
  632. $v_remove_path = $v_arg_list[1];
  633. }
  634. else if ($v_size > 2) {
  635. // ----- Error log
  636. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  637. // ----- Return
  638. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
  639. return 0;
  640. }
  641. }
  642. }
  643. // ----- Trace
  644. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "path='$v_path', remove_path='$v_remove_path', remove_all_path='".($v_remove_path?'true':'false')."'");
  645. // ----- Call the extracting fct
  646. $p_list = array();
  647. $v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path,
  648. $v_remove_all_path, $v_options);
  649. if ($v_result < 1) {
  650. unset($p_list);
  651. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
  652. return(0);
  653. }
  654. // ----- Return
  655. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
  656. return $p_list;
  657. }
  658. // --------------------------------------------------------------------------------
  659. // --------------------------------------------------------------------------------
  660. // Function :
  661. // extractByIndex($p_index, $p_path="./", $p_remove_path="")
  662. // extractByIndex($p_index, [$p_option, $p_option_value, ...])
  663. // Description :
  664. // This method supports two synopsis. The first one is historical.
  665. // This method is doing a partial extract of the archive.
  666. // The extracted files or folders are identified by their index in the
  667. // archive (from 0 to n).
  668. // Note that if the index identify a folder, only the folder entry is
  669. // extracted, not all the files included in the archive.
  670. // Parameters :
  671. // $p_index : A single index (integer) or a string of indexes of files to
  672. // extract. The form of the string is "0,4-6,8-12" with only numbers
  673. // and '-' for range or ',' to separate ranges. No spaces or ';'
  674. // are allowed.
  675. // $p_path : Path where the files and directories are to be extracted
  676. // $p_remove_path : First part ('root' part) of the memorized path
  677. //(if any similar) to remove while extracting.
  678. // Options :
  679. // PCLZIP_OPT_PATH :
  680. // PCLZIP_OPT_ADD_PATH :
  681. // PCLZIP_OPT_REMOVE_PATH :
  682. // PCLZIP_OPT_REMOVE_ALL_PATH :
  683. // PCLZIP_OPT_EXTRACT_AS_STRING : The files are extracted as strings and
  684. // not as files.
  685. // The resulting content is in a new field 'content' in the file
  686. // structure.
  687. // This option must be used alone (any other options are ignored).
  688. // PCLZIP_CB_PRE_EXTRACT :
  689. // PCLZIP_CB_POST_EXTRACT :
  690. // Return Values :
  691. // 0 on failure,
  692. // The list of the extracted files, with a status of the action.
  693. // (see PclZip::listContent() for list entry format)
  694. // --------------------------------------------------------------------------------
  695. function extractByIndex($p_index /* $options */)
  696. {
  697. //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::extractByIndex", "index='$p_index', ...");
  698. $v_result=1;
  699. // ----- Reset the error handler
  700. $this->privErrorReset();
  701. // ----- Check archive
  702. if (!$this->privCheckFormat()) {
  703. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
  704. return(0);
  705. }
  706. // ----- Set default values
  707. $v_options = array();
  708. $v_path = "./";
  709. $v_remove_path = "";
  710. $v_remove_all_path = false;
  711. // ----- Look for variable options arguments
  712. $v_size = func_num_args();
  713. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
  714. // ----- Default values for option
  715. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
  716. // ----- Look for arguments
  717. if ($v_size > 1) {
  718. // ----- Get the arguments
  719. $v_arg_list = &func_get_args();
  720. // ----- Remove form the options list the first argument
  721. array_shift($v_arg_list);
  722. $v_size--;
  723. // ----- Look for first arg
  724. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  725. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options");
  726. // ----- Parse the options
  727. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  728. array (PCLZIP_OPT_PATH => 'optional',
  729. PCLZIP_OPT_REMOVE_PATH => 'optional',
  730. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  731. PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
  732. PCLZIP_OPT_ADD_PATH => 'optional',
  733. PCLZIP_CB_PRE_EXTRACT => 'optional',
  734. PCLZIP_CB_POST_EXTRACT => 'optional',
  735. PCLZIP_OPT_SET_CHMOD => 'optional' ));
  736. if ($v_result != 1) {
  737. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
  738. return 0;
  739. }
  740. // ----- Set the arguments
  741. if (isset($v_options[PCLZIP_OPT_PATH])) {
  742. $v_path = $v_options[PCLZIP_OPT_PATH];
  743. }
  744. if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
  745. $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
  746. }
  747. if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  748. $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  749. }
  750. if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
  751. // ----- Check for '/' in last path char
  752. if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
  753. $v_path .= '/';
  754. }
  755. $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
  756. }
  757. if (!isset($v_options[PCLZIP_OPT_EXTRACT_AS_STRING])) {
  758. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
  759. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Option PCLZIP_OPT_EXTRACT_AS_STRING not set.");
  760. }
  761. else {
  762. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Option PCLZIP_OPT_EXTRACT_AS_STRING set.");
  763. }
  764. }
  765. // ----- Look for 2 args
  766. // Here we need to support the first historic synopsis of the
  767. // method.
  768. else {
  769. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
  770. // ----- Get the first argument
  771. $v_path = $v_arg_list[0];
  772. // ----- Look for the optional second argument
  773. if ($v_size == 2) {
  774. $v_remove_path = $v_arg_list[1];
  775. }
  776. else if ($v_size > 2) {
  777. // ----- Error log
  778. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  779. // ----- Return
  780. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
  781. return 0;
  782. }
  783. }
  784. }
  785. // ----- Trace
  786. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "index='$p_index', path='$v_path', remove_path='$v_remove_path', remove_all_path='".($v_remove_path?'true':'false')."'");
  787. // ----- Trick
  788. // Here I want to reuse extractByRule(), so I need to parse the $p_index
  789. // with privParseOptions()
  790. $v_arg_trick = array (PCLZIP_OPT_BY_INDEX, $p_index);
  791. $v_options_trick = array();
  792. $v_result = $this->privParseOptions($v_arg_trick, sizeof($v_arg_trick), $v_options_trick,
  793. array (PCLZIP_OPT_BY_INDEX => 'optional' ));
  794. if ($v_result != 1) {
  795. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
  796. return 0;
  797. }
  798. $v_options[PCLZIP_OPT_BY_INDEX] = $v_options_trick[PCLZIP_OPT_BY_INDEX];
  799. // ----- Call the extracting fct
  800. if (($v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options)) < 1) {
  801. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
  802. return(0);
  803. }
  804. // ----- Return
  805. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
  806. return $p_list;
  807. }
  808. // --------------------------------------------------------------------------------
  809. // --------------------------------------------------------------------------------
  810. // Function :
  811. // delete([$p_option, $p_option_value, ...])
  812. // Description :
  813. // Parameters :
  814. // None
  815. // Options :
  816. // PCLZIP_OPT_BY_INDEX :
  817. // Return Values :
  818. // 0 on failure,
  819. // The list of the files which are still present in the archive.
  820. // (see PclZip::listContent() for list entry format)
  821. // --------------------------------------------------------------------------------
  822. function delete(/* options */)
  823. {
  824. //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::delete", "");
  825. $v_result=1;
  826. // ----- Reset the error handler
  827. $this->privErrorReset();
  828. // ----- Check archive
  829. if (!$this->privCheckFormat()) {
  830. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
  831. return(0);
  832. }
  833. // ----- Set default values
  834. $v_options = array();
  835. // ----- Look for variable options arguments
  836. $v_size = func_num_args();
  837. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
  838. // ----- Look for no arguments
  839. if ($v_size <= 0) {
  840. // ----- Error log
  841. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing arguments");
  842. // ----- Return
  843. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
  844. return 0;
  845. }
  846. // ----- Get the arguments
  847. $v_arg_list = &func_get_args();
  848. // ----- Parse the options
  849. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  850. array (PCLZIP_OPT_BY_NAME => 'optional',
  851. PCLZIP_OPT_BY_EREG => 'optional',
  852. PCLZIP_OPT_BY_PREG => 'optional',
  853. PCLZIP_OPT_BY_INDEX => 'optional' ));
  854. if ($v_result != 1) {
  855. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
  856. return 0;
  857. }
  858. // ----- Check that at least one rule is set
  859. if ( (!isset($v_options[PCLZIP_OPT_BY_NAME]))
  860. && (!isset($v_options[PCLZIP_OPT_BY_EREG]))
  861. && (!isset($v_options[PCLZIP_OPT_BY_PREG]))
  862. && (!isset($v_options[PCLZIP_OPT_BY_INDEX]))) {
  863. // ----- Error log
  864. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "At least one filtering rule must be set");
  865. // ----- Return
  866. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
  867. return 0;
  868. }
  869. // ----- Call the delete fct
  870. $v_list = array();
  871. if (($v_result = $this->privDeleteByRule($v_list, $v_options)) != 1)
  872. {
  873. unset($v_list);
  874. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
  875. return(0);
  876. }
  877. // ----- Return
  878. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_list);
  879. return $v_list;
  880. }
  881. // --------------------------------------------------------------------------------
  882. // --------------------------------------------------------------------------------
  883. // Function : deleteByIndex()
  884. // Description :
  885. // ***** Deprecated *****
  886. // delete(PCLZIP_OPT_BY_INDEX, $p_index) should be prefered.
  887. // --------------------------------------------------------------------------------
  888. function deleteByIndex($p_index)
  889. {
  890. //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::deleteByIndex", "index='$p_index'");
  891. $p_list = $this->delete(PCLZIP_OPT_BY_INDEX, $p_index);
  892. // ----- Return
  893. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
  894. return $p_list;
  895. }
  896. // --------------------------------------------------------------------------------
  897. // --------------------------------------------------------------------------------
  898. // Function : properties()
  899. // Description :
  900. // This method gives the properties of the archive.
  901. // The properties are :
  902. // nb : Number of files in the archive
  903. // comment : Comment associated with the archive file
  904. // status : not_exist, ok
  905. // Parameters :
  906. // None
  907. // Return Values :
  908. // 0 on failure,
  909. // An array with the archive properties.
  910. // --------------------------------------------------------------------------------
  911. function properties()
  912. {
  913. //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::properties", "");
  914. // ----- Reset the error handler
  915. $this->privErrorReset();
  916. // ----- Check archive
  917. if (!$this->privCheckFormat()) {
  918. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
  919. return(0);
  920. }
  921. // ----- Default properties
  922. $v_prop = array();
  923. $v_prop['comment'] = '';
  924. $v_prop['nb'] = 0;
  925. $v_prop['status'] = 'not_exist';
  926. // ----- Look if file exists
  927. if (@is_file($this->zipname))
  928. {
  929. // ----- Open the zip file
  930. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
  931. if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
  932. {
  933. // ----- Error log
  934. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
  935. // ----- Return
  936. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), 0);
  937. return 0;
  938. }
  939. // ----- Read the central directory informations
  940. $v_central_dir = array();
  941. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  942. {
  943. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
  944. return 0;
  945. }
  946. // ----- Close the zip file
  947. $this->privCloseFd();
  948. // ----- Set the user attributes
  949. $v_prop['comment'] = $v_central_dir['comment'];
  950. $v_prop['nb'] = $v_central_dir['entries'];
  951. $v_prop['status'] = 'ok';
  952. }
  953. // ----- Return
  954. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_prop);
  955. return $v_prop;
  956. }
  957. // --------------------------------------------------------------------------------
  958. // --------------------------------------------------------------------------------
  959. // Function : duplicate()
  960. // Description :
  961. // This method creates an archive by copying the content of an other one. If
  962. // the archive already exist, it is replaced by the new one without any warning.
  963. // Parameters :
  964. // $p_archive : The filename of a valid archive, or
  965. //a valid PclZip object.
  966. // Return Values :
  967. // 1 on success.
  968. // 0 or a negative value on error (error code).
  969. // --------------------------------------------------------------------------------
  970. function duplicate($p_archive)
  971. {
  972. //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::duplicate", "");
  973. $v_result = 1;
  974. // ----- Reset the error handler
  975. $this->privErrorReset();
  976. // ----- Look if the $p_archive is a PclZip object
  977. if ((is_object($p_archive)) && (get_class($p_archive) == 'pclzip'))
  978. {
  979. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The parameter is valid PclZip object '".$p_archive->zipname."'");
  980. // ----- Duplicate the archive
  981. $v_result = $this->privDuplicate($p_archive->zipname);
  982. }
  983. // ----- Look if the $p_archive is a string (so a filename)
  984. else if (is_string($p_archive))
  985. {
  986. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The parameter is a filename '$p_archive'");
  987. // ----- Check that $p_archive is a valid zip file
  988. // TBC : Should also check the archive format
  989. if (!is_file($p_archive)) {
  990. // ----- Error log
  991. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "No file with filename '".$p_archive."'");
  992. $v_result = PCLZIP_ERR_MISSING_FILE;
  993. }
  994. else {
  995. // ----- Duplicate the archive
  996. $v_result = $this->privDuplicate($p_archive);
  997. }
  998. }
  999. // ----- Invalid variable
  1000. else
  1001. {
  1002. // ----- Error log
  1003. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
  1004. $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  1005. }
  1006. // ----- Return
  1007. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
  1008. return $v_result;
  1009. }
  1010. // --------------------------------------------------------------------------------
  1011. // --------------------------------------------------------------------------------
  1012. // Function : merge()
  1013. // Description :
  1014. // This method merge the $p_archive_to_add archive at the end of the current
  1015. // one ($this).
  1016. // If the archive ($this) does not exist, the merge becomes a duplicate.
  1017. // If the $p_archive_to_add archive does not exist, the merge is a success.
  1018. // Parameters :
  1019. // $p_archive_to_add : It can be directly the filename of a valid zip archive,
  1020. // or a PclZip object archive.
  1021. // Return Values :
  1022. // 1 on success,
  1023. // 0 or negative values on error (see below).
  1024. // --------------------------------------------------------------------------------
  1025. function merge($p_archive_to_add)
  1026. {
  1027. //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::merge", "");
  1028. $v_result = 1;
  1029. // ----- Reset the error handler
  1030. $this->privErrorReset();
  1031. // ----- Check archive
  1032. if (!$this->privCheckFormat()) {
  1033. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
  1034. return(0);
  1035. }
  1036. // ----- Look if the $p_archive_to_add is a PclZip object
  1037. if ((is_object($p_archive_to_add)) && (get_class($p_archive_to_add) == 'pclzip'))
  1038. {
  1039. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The parameter is valid PclZip object");
  1040. // ----- Merge the archive
  1041. $v_result = $this->privMerge($p_archive_to_add);
  1042. }
  1043. // ----- Look if the $p_archive_to_add is a string (so a filename)
  1044. else if (is_string($p_archive_to_add))
  1045. {
  1046. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The parameter is a filename");
  1047. // ----- Create a temporary archive
  1048. $v_object_archive = new PclZip($p_archive_to_add);
  1049. // ----- Merge the archive
  1050. $v_result = $this->privMerge($v_object_archive);
  1051. }
  1052. // ----- Invalid variable
  1053. else
  1054. {
  1055. // ----- Error log
  1056. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
  1057. $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  1058. }
  1059. // ----- Return
  1060. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
  1061. return $v_result;
  1062. }
  1063. // --------------------------------------------------------------------------------
  1064. // --------------------------------------------------------------------------------
  1065. // Function : errorCode()
  1066. // Description :
  1067. // Parameters :
  1068. // --------------------------------------------------------------------------------
  1069. function errorCode()
  1070. {
  1071. if (PCLZIP_ERROR_EXTERNAL == 1) {
  1072. return(PclErrorCode());
  1073. }
  1074. else {
  1075. return($this->error_code);
  1076. }
  1077. }
  1078. // --------------------------------------------------------------------------------
  1079. // --------------------------------------------------------------------------------
  1080. // Function : errorName()
  1081. // Description :
  1082. // Parameters :
  1083. // --------------------------------------------------------------------------------
  1084. function errorName($p_with_code=false)
  1085. {
  1086. $v_name = array ( PCLZIP_ERR_NO_ERROR => 'PCLZIP_ERR_NO_ERROR',
  1087. PCLZIP_ERR_WRITE_OPEN_FAIL => 'PCLZIP_ERR_WRITE_OPEN_FAIL',
  1088. PCLZIP_ERR_READ_OPEN_FAIL => 'PCLZIP_ERR_READ_OPEN_FAIL',
  1089. PCLZIP_ERR_INVALID_PARAMETER => 'PCLZIP_ERR_INVALID_PARAMETER',
  1090. PCLZIP_ERR_MISSING_FILE => 'PCLZIP_ERR_MISSING_FILE',
  1091. PCLZIP_ERR_FILENAME_TOO_LONG => 'PCLZIP_ERR_FILENAME_TOO_LONG',
  1092. PCLZIP_ERR_INVALID_ZIP => 'PCLZIP_ERR_INVALID_ZIP',
  1093. PCLZIP_ERR_BAD_EXTRACTED_FILE => 'PCLZIP_ERR_BAD_EXTRACTED_FILE',
  1094. PCLZIP_ERR_DIR_CREATE_FAIL => 'PCLZIP_ERR_DIR_CREATE_FAIL',
  1095. PCLZIP_ERR_BAD_EXTENSION => 'PCLZIP_ERR_BAD_EXTENSION',
  1096. PCLZIP_ERR_BAD_FORMAT => 'PCLZIP_ERR_BAD_FORMAT',
  1097. PCLZIP_ERR_DELETE_FILE_FAIL => 'PCLZIP_ERR_DELETE_FILE_FAIL',
  1098. PCLZIP_ERR_RENAME_FILE_FAIL => 'PCLZIP_ERR_RENAME_FILE_FAIL',
  1099. PCLZIP_ERR_BAD_CHECKSUM => 'PCLZIP_ERR_BAD_CHECKSUM',
  1100. PCLZIP_ERR_INVALID_ARCHIVE_ZIP => 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP',
  1101. PCLZIP_ERR_MISSING_OPTION_VALUE => 'PCLZIP_ERR_MISSING_OPTION_VALUE',
  1102. PCLZIP_ERR_INVALID_OPTION_VALUE => 'PCLZIP_ERR_INVALID_OPTION_VALUE' );
  1103. if (isset($v_name[$this->error_code])) {
  1104. $v_value = $v_name[$this->error_code];
  1105. }
  1106. else {
  1107. $v_value = 'NoName';
  1108. }
  1109. if ($p_with_code) {
  1110. return($v_value.' ('.$this->error_code.')');
  1111. }
  1112. else {
  1113. return($v_value);
  1114. }
  1115. }
  1116. // --------------------------------------------------------------------------------
  1117. // --------------------------------------------------------------------------------
  1118. // Function : errorInfo()
  1119. // Description :
  1120. // Parameters :
  1121. // --------------------------------------------------------------------------------
  1122. function errorInfo($p_full=false)
  1123. {
  1124. if (PCLZIP_ERROR_EXTERNAL == 1) {
  1125. return(PclErrorString());
  1126. }
  1127. else {
  1128. if ($p_full) {
  1129. return($this->errorName(true)." : ".$this->error_string);
  1130. }
  1131. else {
  1132. return($this->error_string." [code ".$this->error_code."]");
  1133. }
  1134. }
  1135. }
  1136. // --------------------------------------------------------------------------------
  1137. // --------------------------------------------------------------------------------
  1138. // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
  1139. // **********
  1140. // ***** THESES FUNCTIONS MUST NOT BE USED DIRECTLY *****
  1141. // --------------------------------------------------------------------------------
  1142. // --------------------------------------------------------------------------------
  1143. // Function : privCheckFormat()
  1144. // Description :
  1145. // This method check that the archive exists and is a valid zip archive.
  1146. // Several level of check exists. (futur)
  1147. // Parameters :
  1148. // $p_level : Level of check. Default 0.
  1149. // 0 : Check the first bytes (magic codes) (default value))
  1150. // 1 : 0 + Check the central directory (futur)
  1151. // 2 : 1 + Check each file header (futur)
  1152. // Return Values :
  1153. // true on success,
  1154. // false on error, the error code is set.
  1155. // --------------------------------------------------------------------------------
  1156. function privCheckFormat($p_level=0)
  1157. {
  1158. //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCheckFormat", "");
  1159. $v_result = true;
  1160. // ----- Reset the file system cache
  1161. clearstatcache();
  1162. // ----- Reset the error handler
  1163. $this->privErrorReset();
  1164. // ----- Look if the file exits
  1165. if (!is_file($this->zipname)) {
  1166. // ----- Error log
  1167. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "Missing archive file '".$this->zipname."'");
  1168. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, false, PclZip::errorInfo());
  1169. return(false);
  1170. }
  1171. // ----- Check that the file is readeable
  1172. if (!is_readable($this->zipname)) {
  1173. // ----- Error log
  1174. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '".$this->zipname."'");
  1175. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, false, PclZip::errorInfo());
  1176. return(false);
  1177. }
  1178. // ----- Check the magic code
  1179. // TBC
  1180. // ----- Check the central header
  1181. // TBC
  1182. // ----- Check each file header
  1183. // TBC
  1184. // ----- Return
  1185. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
  1186. return $v_result;
  1187. }
  1188. // --------------------------------------------------------------------------------
  1189. // --------------------------------------------------------------------------------
  1190. // Function : privParseOptions()
  1191. // Description :
  1192. // This internal methods reads the variable list of arguments ($p_options_list,
  1193. // $p_size) and generate an array with the options and values ($v_result_list).
  1194. // $v_requested_options contains the options that can be present and those that
  1195. // must be present.
  1196. // $v_requested_options is an array, with the option value as key, and 'optional',
  1197. // or 'mandatory' as value.
  1198. // Parameters :
  1199. // See above.
  1200. // Return Values :
  1201. // 1 on success.
  1202. // 0 on failure.
  1203. // --------------------------------------------------------------------------------
  1204. function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options=false)
  1205. {
  1206. //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privParseOptions", "");
  1207. $v_result=1;
  1208. // ----- Read the options
  1209. $i=0;
  1210. while ($i<$p_size) {
  1211. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Looking for table index $i, option = '".PclZipUtilOptionText($p_options_list[$i])."(".$p_options_list[$i].")'");
  1212. // ----- Check if the option is requested
  1213. if (!isset($v_requested_options[$p_options_list[$i]])) {
  1214. // ----- Error log
  1215. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid optional parameter '".$p_options_list[$i]."' for this method");
  1216. // ----- Return
  1217. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
  1218. return PclZip::errorCode();
  1219. }
  1220. // ----- Look for next option
  1221. switch ($p_options_list[$i]) {
  1222. // ----- Look for options that request a path value
  1223. case PCLZIP_OPT_PATH :
  1224. case PCLZIP_OPT_REMOVE_PATH :
  1225. case PCLZIP_OPT_ADD_PATH :
  1226. // ----- Check the number of parameters
  1227. if (($i+1) >= $p_size) {
  1228. // ----- Error log
  1229. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1230. // ----- Return
  1231. //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
  1232. return PclZip::errorCode();
  1233. }
  1234. // ----- Get the value
  1235. $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], false);
  1236. //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
  1237. $i++;
  1238. break;
  1239. // ----- Look for options that request an array of string for value
  1240. case PCLZIP_OPT_BY_NAME :
  1241. // ----- Check the number of parameters
  1242. if (($i+1) >= $p_size) {
  1243. // ----- Error log
  1244. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  1245. //…

Large files files are truncated, but you can click here to view the full file