PageRenderTime 57ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/source/class/class_pclzip.php

https://github.com/jinbo51/DiscuzX
PHP | 2773 lines | 2706 code | 51 blank | 16 comment | 980 complexity | 590336dc44ab438e9ffdbc4f5b48bba1 MD5 | raw file
Possible License(s): BSD-3-Clause

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

  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Copyright (C) 2008-2012 OSDU.Net www.osdu.net admin@osdu.net
  4. // +----------------------------------------------------------------------
  5. // | Licensed: ( http://www.apache.org/licenses/LICENSE-2.0 )
  6. // +----------------------------------------------------------------------
  7. // | Author: ���ֱߵĻ��� QQ:858908467 E-mail:858908467@qq.com
  8. // +----------------------------------------------------------------------
  9. /**
  10. +------------------------------------------------------------------------------
  11. * PhpConcept Library - Zip Module 2.8.2
  12. +------------------------------------------------------------------------------
  13. * Presentation : PclZip is a PHP library that manage ZIP archives.
  14. +------------------------------------------------------------------------------
  15. * Description : See readme.txt and http://www.phpconcept.net
  16. +------------------------------------------------------------------------------
  17. **/
  18. if(!defined('IN_DISCUZ')) {
  19. exit('Access Denied');
  20. }
  21. if (!defined('PCLZIP_READ_BLOCK_SIZE')) {
  22. define('PCLZIP_READ_BLOCK_SIZE', 2048);
  23. }
  24. if (!defined('PCLZIP_SEPARATOR')) {
  25. define('PCLZIP_SEPARATOR', ',');
  26. }
  27. if (!defined('PCLZIP_ERROR_EXTERNAL')) {
  28. define('PCLZIP_ERROR_EXTERNAL', 0);
  29. }
  30. if (!defined('PCLZIP_TEMPORARY_DIR')) {
  31. define('PCLZIP_TEMPORARY_DIR', '');
  32. }
  33. if (!defined('PCLZIP_TEMPORARY_FILE_RATIO')) {
  34. define('PCLZIP_TEMPORARY_FILE_RATIO', 0.47);
  35. }
  36. $g_pclzip_version = "2.8.2";
  37. define('PCLZIP_ERR_USER_ABORTED', 2);
  38. define('PCLZIP_ERR_NO_ERROR', 0);
  39. define('PCLZIP_ERR_WRITE_OPEN_FAIL', -1);
  40. define('PCLZIP_ERR_READ_OPEN_FAIL', -2);
  41. define('PCLZIP_ERR_INVALID_PARAMETER', -3);
  42. define('PCLZIP_ERR_MISSING_FILE', -4);
  43. define('PCLZIP_ERR_FILENAME_TOO_LONG', -5);
  44. define('PCLZIP_ERR_INVALID_ZIP', -6);
  45. define('PCLZIP_ERR_BAD_EXTRACTED_FILE', -7);
  46. define('PCLZIP_ERR_DIR_CREATE_FAIL', -8);
  47. define('PCLZIP_ERR_BAD_EXTENSION', -9);
  48. define('PCLZIP_ERR_BAD_FORMAT', -10);
  49. define('PCLZIP_ERR_DELETE_FILE_FAIL', -11);
  50. define('PCLZIP_ERR_RENAME_FILE_FAIL', -12);
  51. define('PCLZIP_ERR_BAD_CHECKSUM', -13);
  52. define('PCLZIP_ERR_INVALID_ARCHIVE_ZIP', -14);
  53. define('PCLZIP_ERR_MISSING_OPTION_VALUE', -15);
  54. define('PCLZIP_ERR_INVALID_OPTION_VALUE', -16);
  55. define('PCLZIP_ERR_ALREADY_A_DIRECTORY', -17);
  56. define('PCLZIP_ERR_UNSUPPORTED_COMPRESSION', -18);
  57. define('PCLZIP_ERR_UNSUPPORTED_ENCRYPTION', -19);
  58. define('PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE', -20);
  59. define('PCLZIP_ERR_DIRECTORY_RESTRICTION', -21);
  60. define('PCLZIP_OPT_PATH', 77001);
  61. define('PCLZIP_OPT_ADD_PATH', 77002);
  62. define('PCLZIP_OPT_REMOVE_PATH', 77003);
  63. define('PCLZIP_OPT_REMOVE_ALL_PATH', 77004);
  64. define('PCLZIP_OPT_SET_CHMOD', 77005);
  65. define('PCLZIP_OPT_EXTRACT_AS_STRING', 77006);
  66. define('PCLZIP_OPT_NO_COMPRESSION', 77007);
  67. define('PCLZIP_OPT_BY_NAME', 77008);
  68. define('PCLZIP_OPT_BY_INDEX', 77009);
  69. define('PCLZIP_OPT_BY_EREG', 77010);
  70. define('PCLZIP_OPT_BY_PREG', 77011);
  71. define('PCLZIP_OPT_COMMENT', 77012);
  72. define('PCLZIP_OPT_ADD_COMMENT', 77013);
  73. define('PCLZIP_OPT_PREPEND_COMMENT', 77014);
  74. define('PCLZIP_OPT_EXTRACT_IN_OUTPUT', 77015);
  75. define('PCLZIP_OPT_REPLACE_NEWER', 77016);
  76. define('PCLZIP_OPT_STOP_ON_ERROR', 77017);
  77. define('PCLZIP_OPT_EXTRACT_DIR_RESTRICTION', 77019);
  78. define('PCLZIP_OPT_TEMP_FILE_THRESHOLD', 77020);
  79. define('PCLZIP_OPT_ADD_TEMP_FILE_THRESHOLD', 77020);
  80. define('PCLZIP_OPT_TEMP_FILE_ON', 77021);
  81. define('PCLZIP_OPT_ADD_TEMP_FILE_ON', 77021);
  82. define('PCLZIP_OPT_TEMP_FILE_OFF', 77022);
  83. define('PCLZIP_OPT_ADD_TEMP_FILE_OFF', 77022);
  84. define('PCLZIP_ATT_FILE_NAME', 79001);
  85. define('PCLZIP_ATT_FILE_NEW_SHORT_NAME', 79002);
  86. define('PCLZIP_ATT_FILE_NEW_FULL_NAME', 79003);
  87. define('PCLZIP_ATT_FILE_MTIME', 79004);
  88. define('PCLZIP_ATT_FILE_CONTENT', 79005);
  89. define('PCLZIP_ATT_FILE_COMMENT', 79006);
  90. define('PCLZIP_CB_PRE_EXTRACT', 78001);
  91. define('PCLZIP_CB_POST_EXTRACT', 78002);
  92. define('PCLZIP_CB_PRE_ADD', 78003);
  93. define('PCLZIP_CB_POST_ADD', 78004);
  94. class PclZip {
  95. var $zipname = '';
  96. var $zip_fd = 0;
  97. var $error_code = 1;
  98. var $error_string = '';
  99. var $magic_quotes_status;
  100. function PclZip($p_zipname) {
  101. if (!function_exists('gzopen')) {
  102. die('Abort ' . basename(__FILE__) . ' : Missing zlib extensions');
  103. }
  104. $this->zipname = $p_zipname;
  105. $this->zip_fd = 0;
  106. $this->magic_quotes_status = -1;
  107. return;
  108. }
  109. function create($p_filelist) {
  110. $v_result = 1;
  111. $this->privErrorReset();
  112. $v_options = array();
  113. $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
  114. $v_size = func_num_args();
  115. if ($v_size > 1) {
  116. $v_arg_list = func_get_args();
  117. array_shift($v_arg_list);
  118. $v_size--;
  119. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  120. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  121. array(PCLZIP_OPT_REMOVE_PATH => 'optional',
  122. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  123. PCLZIP_OPT_ADD_PATH => 'optional',
  124. PCLZIP_CB_PRE_ADD => 'optional',
  125. PCLZIP_CB_POST_ADD => 'optional',
  126. PCLZIP_OPT_NO_COMPRESSION => 'optional',
  127. PCLZIP_OPT_COMMENT => 'optional',
  128. PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
  129. PCLZIP_OPT_TEMP_FILE_ON => 'optional',
  130. PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
  131. ));
  132. if ($v_result != 1) {
  133. return 0;
  134. }
  135. } else {
  136. $v_options[PCLZIP_OPT_ADD_PATH] = $v_arg_list[0];
  137. if ($v_size == 2) {
  138. $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
  139. } else if ($v_size > 2) {
  140. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
  141. "Invalid number / type of arguments");
  142. return 0;
  143. }
  144. }
  145. }
  146. $this->privOptionDefaultThreshold($v_options);
  147. $v_string_list = array();
  148. $v_att_list = array();
  149. $v_filedescr_list = array();
  150. $p_result_list = array();
  151. if (is_array($p_filelist)) {
  152. if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
  153. $v_att_list = $p_filelist;
  154. } else {
  155. $v_string_list = $p_filelist;
  156. }
  157. } else if (is_string($p_filelist)) {
  158. $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
  159. } else {
  160. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
  161. return 0;
  162. }
  163. if (sizeof($v_string_list) != 0) {
  164. foreach ($v_string_list as $v_string) {
  165. if ($v_string != '') {
  166. $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
  167. } else {
  168. }
  169. }
  170. }
  171. $v_supported_attributes
  172. = array(PCLZIP_ATT_FILE_NAME => 'mandatory',
  173. PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional',
  174. PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional',
  175. PCLZIP_ATT_FILE_MTIME => 'optional',
  176. PCLZIP_ATT_FILE_CONTENT => 'optional',
  177. PCLZIP_ATT_FILE_COMMENT => 'optional'
  178. );
  179. foreach ($v_att_list as $v_entry) {
  180. $v_result = $this->privFileDescrParseAtt($v_entry,
  181. $v_filedescr_list[],
  182. $v_options,
  183. $v_supported_attributes);
  184. if ($v_result != 1) {
  185. return 0;
  186. }
  187. }
  188. $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
  189. if ($v_result != 1) {
  190. return 0;
  191. }
  192. $v_result = $this->privCreate($v_filedescr_list, $p_result_list, $v_options);
  193. if ($v_result != 1) {
  194. return 0;
  195. }
  196. return $p_result_list;
  197. }
  198. function add($p_filelist) {
  199. $v_result = 1;
  200. $this->privErrorReset();
  201. $v_options = array();
  202. $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
  203. $v_size = func_num_args();
  204. if ($v_size > 1) {
  205. $v_arg_list = func_get_args();
  206. array_shift($v_arg_list);
  207. $v_size--;
  208. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  209. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  210. array(PCLZIP_OPT_REMOVE_PATH => 'optional',
  211. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  212. PCLZIP_OPT_ADD_PATH => 'optional',
  213. PCLZIP_CB_PRE_ADD => 'optional',
  214. PCLZIP_CB_POST_ADD => 'optional',
  215. PCLZIP_OPT_NO_COMPRESSION => 'optional',
  216. PCLZIP_OPT_COMMENT => 'optional',
  217. PCLZIP_OPT_ADD_COMMENT => 'optional',
  218. PCLZIP_OPT_PREPEND_COMMENT => 'optional',
  219. PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
  220. PCLZIP_OPT_TEMP_FILE_ON => 'optional',
  221. PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
  222. ));
  223. if ($v_result != 1) {
  224. return 0;
  225. }
  226. } else {
  227. $v_options[PCLZIP_OPT_ADD_PATH] = $v_add_path = $v_arg_list[0];
  228. if ($v_size == 2) {
  229. $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
  230. } else if ($v_size > 2) {
  231. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  232. return 0;
  233. }
  234. }
  235. }
  236. $this->privOptionDefaultThreshold($v_options);
  237. $v_string_list = array();
  238. $v_att_list = array();
  239. $v_filedescr_list = array();
  240. $p_result_list = array();
  241. if (is_array($p_filelist)) {
  242. if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
  243. $v_att_list = $p_filelist;
  244. } else {
  245. $v_string_list = $p_filelist;
  246. }
  247. } else if (is_string($p_filelist)) {
  248. $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
  249. } else {
  250. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type '" . gettype($p_filelist) . "' for p_filelist");
  251. return 0;
  252. }
  253. if (sizeof($v_string_list) != 0) {
  254. foreach ($v_string_list as $v_string) {
  255. $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
  256. }
  257. }
  258. $v_supported_attributes
  259. = array(PCLZIP_ATT_FILE_NAME => 'mandatory',
  260. PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional',
  261. PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional',
  262. PCLZIP_ATT_FILE_MTIME => 'optional',
  263. PCLZIP_ATT_FILE_CONTENT => 'optional',
  264. PCLZIP_ATT_FILE_COMMENT => 'optional'
  265. );
  266. foreach ($v_att_list as $v_entry) {
  267. $v_result = $this->privFileDescrParseAtt($v_entry,
  268. $v_filedescr_list[],
  269. $v_options,
  270. $v_supported_attributes);
  271. if ($v_result != 1) {
  272. return 0;
  273. }
  274. }
  275. $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
  276. if ($v_result != 1) {
  277. return 0;
  278. }
  279. $v_result = $this->privAdd($v_filedescr_list, $p_result_list, $v_options);
  280. if ($v_result != 1) {
  281. return 0;
  282. }
  283. return $p_result_list;
  284. }
  285. function listContent() {
  286. $v_result = 1;
  287. $this->privErrorReset();
  288. if (!$this->privCheckFormat()) {
  289. return (0);
  290. }
  291. $p_list = array();
  292. if (($v_result = $this->privList($p_list)) != 1) {
  293. unset($p_list);
  294. return (0);
  295. }
  296. return $p_list;
  297. }
  298. function extract() {
  299. $v_result = 1;
  300. $this->privErrorReset();
  301. if (!$this->privCheckFormat()) {
  302. return (0);
  303. }
  304. $v_options = array();
  305. $v_path = '';
  306. $v_remove_path = "";
  307. $v_remove_all_path = false;
  308. $v_size = func_num_args();
  309. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
  310. if ($v_size > 0) {
  311. $v_arg_list = func_get_args();
  312. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  313. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  314. array(PCLZIP_OPT_PATH => 'optional',
  315. PCLZIP_OPT_REMOVE_PATH => 'optional',
  316. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  317. PCLZIP_OPT_ADD_PATH => 'optional',
  318. PCLZIP_CB_PRE_EXTRACT => 'optional',
  319. PCLZIP_CB_POST_EXTRACT => 'optional',
  320. PCLZIP_OPT_SET_CHMOD => 'optional',
  321. PCLZIP_OPT_BY_NAME => 'optional',
  322. PCLZIP_OPT_BY_EREG => 'optional',
  323. PCLZIP_OPT_BY_PREG => 'optional',
  324. PCLZIP_OPT_BY_INDEX => 'optional',
  325. PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
  326. PCLZIP_OPT_EXTRACT_IN_OUTPUT => 'optional',
  327. PCLZIP_OPT_REPLACE_NEWER => 'optional',
  328. PCLZIP_OPT_STOP_ON_ERROR => 'optional',
  329. PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional',
  330. PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
  331. PCLZIP_OPT_TEMP_FILE_ON => 'optional',
  332. PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
  333. ));
  334. if ($v_result != 1) {
  335. return 0;
  336. }
  337. if (isset($v_options[PCLZIP_OPT_PATH])) {
  338. $v_path = $v_options[PCLZIP_OPT_PATH];
  339. }
  340. if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
  341. $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
  342. }
  343. if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  344. $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  345. }
  346. if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
  347. if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
  348. $v_path .= '/';
  349. }
  350. $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
  351. }
  352. } else {
  353. $v_path = $v_arg_list[0];
  354. if ($v_size == 2) {
  355. $v_remove_path = $v_arg_list[1];
  356. } else if ($v_size > 2) {
  357. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  358. return 0;
  359. }
  360. }
  361. }
  362. $this->privOptionDefaultThreshold($v_options);
  363. $p_list = array();
  364. $v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path,
  365. $v_remove_all_path, $v_options);
  366. if ($v_result < 1) {
  367. unset($p_list);
  368. return (0);
  369. }
  370. return $p_list;
  371. }
  372. function extractByIndex($p_index) {
  373. $v_result = 1;
  374. $this->privErrorReset();
  375. if (!$this->privCheckFormat()) {
  376. return (0);
  377. }
  378. $v_options = array();
  379. $v_path = '';
  380. $v_remove_path = "";
  381. $v_remove_all_path = false;
  382. $v_size = func_num_args();
  383. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
  384. if ($v_size > 1) {
  385. $v_arg_list = func_get_args();
  386. array_shift($v_arg_list);
  387. $v_size--;
  388. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  389. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  390. array(PCLZIP_OPT_PATH => 'optional',
  391. PCLZIP_OPT_REMOVE_PATH => 'optional',
  392. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  393. PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
  394. PCLZIP_OPT_ADD_PATH => 'optional',
  395. PCLZIP_CB_PRE_EXTRACT => 'optional',
  396. PCLZIP_CB_POST_EXTRACT => 'optional',
  397. PCLZIP_OPT_SET_CHMOD => 'optional',
  398. PCLZIP_OPT_REPLACE_NEWER => 'optional',
  399. PCLZIP_OPT_STOP_ON_ERROR => 'optional',
  400. PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional',
  401. PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
  402. PCLZIP_OPT_TEMP_FILE_ON => 'optional',
  403. PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
  404. ));
  405. if ($v_result != 1) {
  406. return 0;
  407. }
  408. if (isset($v_options[PCLZIP_OPT_PATH])) {
  409. $v_path = $v_options[PCLZIP_OPT_PATH];
  410. }
  411. if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
  412. $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
  413. }
  414. if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  415. $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  416. }
  417. if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
  418. if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
  419. $v_path .= '/';
  420. }
  421. $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
  422. }
  423. if (!isset($v_options[PCLZIP_OPT_EXTRACT_AS_STRING])) {
  424. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
  425. } else {
  426. }
  427. } else {
  428. $v_path = $v_arg_list[0];
  429. if ($v_size == 2) {
  430. $v_remove_path = $v_arg_list[1];
  431. } else if ($v_size > 2) {
  432. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  433. return 0;
  434. }
  435. }
  436. }
  437. $v_arg_trick = array(PCLZIP_OPT_BY_INDEX, $p_index);
  438. $v_options_trick = array();
  439. $v_result = $this->privParseOptions($v_arg_trick, sizeof($v_arg_trick), $v_options_trick,
  440. array(PCLZIP_OPT_BY_INDEX => 'optional'));
  441. if ($v_result != 1) {
  442. return 0;
  443. }
  444. $v_options[PCLZIP_OPT_BY_INDEX] = $v_options_trick[PCLZIP_OPT_BY_INDEX];
  445. $this->privOptionDefaultThreshold($v_options);
  446. if (($v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options)) < 1) {
  447. return (0);
  448. }
  449. return $p_list;
  450. }
  451. function delete() {
  452. $v_result = 1;
  453. $this->privErrorReset();
  454. if (!$this->privCheckFormat()) {
  455. return (0);
  456. }
  457. $v_options = array();
  458. $v_size = func_num_args();
  459. if ($v_size > 0) {
  460. $v_arg_list = func_get_args();
  461. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  462. array(PCLZIP_OPT_BY_NAME => 'optional',
  463. PCLZIP_OPT_BY_EREG => 'optional',
  464. PCLZIP_OPT_BY_PREG => 'optional',
  465. PCLZIP_OPT_BY_INDEX => 'optional'));
  466. if ($v_result != 1) {
  467. return 0;
  468. }
  469. }
  470. $this->privDisableMagicQuotes();
  471. $v_list = array();
  472. if (($v_result = $this->privDeleteByRule($v_list, $v_options)) != 1) {
  473. $this->privSwapBackMagicQuotes();
  474. unset($v_list);
  475. return (0);
  476. }
  477. $this->privSwapBackMagicQuotes();
  478. return $v_list;
  479. }
  480. function deleteByIndex($p_index) {
  481. $p_list = $this->delete(PCLZIP_OPT_BY_INDEX, $p_index);
  482. return $p_list;
  483. }
  484. function properties() {
  485. $this->privErrorReset();
  486. $this->privDisableMagicQuotes();
  487. if (!$this->privCheckFormat()) {
  488. $this->privSwapBackMagicQuotes();
  489. return (0);
  490. }
  491. $v_prop = array();
  492. $v_prop['comment'] = '';
  493. $v_prop['nb'] = 0;
  494. $v_prop['status'] = 'not_exist';
  495. if (@is_file($this->zipname)) {
  496. if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0) {
  497. $this->privSwapBackMagicQuotes();
  498. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \'' . $this->zipname . '\' in binary read mode');
  499. return 0;
  500. }
  501. $v_central_dir = array();
  502. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  503. $this->privSwapBackMagicQuotes();
  504. return 0;
  505. }
  506. $this->privCloseFd();
  507. $v_prop['comment'] = $v_central_dir['comment'];
  508. $v_prop['nb'] = $v_central_dir['entries'];
  509. $v_prop['status'] = 'ok';
  510. }
  511. $this->privSwapBackMagicQuotes();
  512. return $v_prop;
  513. }
  514. function duplicate($p_archive) {
  515. $v_result = 1;
  516. $this->privErrorReset();
  517. if ((is_object($p_archive)) && (get_class($p_archive) == 'pclzip')) {
  518. $v_result = $this->privDuplicate($p_archive->zipname);
  519. } else if (is_string($p_archive)) {
  520. if (!is_file($p_archive)) {
  521. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "No file with filename '" . $p_archive . "'");
  522. $v_result = PCLZIP_ERR_MISSING_FILE;
  523. } else {
  524. $v_result = $this->privDuplicate($p_archive);
  525. }
  526. } else {
  527. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
  528. $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  529. }
  530. return $v_result;
  531. }
  532. function merge($p_archive_to_add) {
  533. $v_result = 1;
  534. $this->privErrorReset();
  535. if (!$this->privCheckFormat()) {
  536. return (0);
  537. }
  538. if ((is_object($p_archive_to_add)) && (get_class($p_archive_to_add) == 'pclzip')) {
  539. $v_result = $this->privMerge($p_archive_to_add);
  540. } else if (is_string($p_archive_to_add)) {
  541. $v_object_archive = new PclZip($p_archive_to_add);
  542. $v_result = $this->privMerge($v_object_archive);
  543. } else {
  544. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
  545. $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  546. }
  547. return $v_result;
  548. }
  549. function errorCode() {
  550. if (PCLZIP_ERROR_EXTERNAL == 1) {
  551. return (PclErrorCode());
  552. } else {
  553. return ($this->error_code);
  554. }
  555. }
  556. function errorName($p_with_code = false) {
  557. $v_name = array(PCLZIP_ERR_NO_ERROR => 'PCLZIP_ERR_NO_ERROR',
  558. PCLZIP_ERR_WRITE_OPEN_FAIL => 'PCLZIP_ERR_WRITE_OPEN_FAIL',
  559. PCLZIP_ERR_READ_OPEN_FAIL => 'PCLZIP_ERR_READ_OPEN_FAIL',
  560. PCLZIP_ERR_INVALID_PARAMETER => 'PCLZIP_ERR_INVALID_PARAMETER',
  561. PCLZIP_ERR_MISSING_FILE => 'PCLZIP_ERR_MISSING_FILE',
  562. PCLZIP_ERR_FILENAME_TOO_LONG => 'PCLZIP_ERR_FILENAME_TOO_LONG',
  563. PCLZIP_ERR_INVALID_ZIP => 'PCLZIP_ERR_INVALID_ZIP',
  564. PCLZIP_ERR_BAD_EXTRACTED_FILE => 'PCLZIP_ERR_BAD_EXTRACTED_FILE',
  565. PCLZIP_ERR_DIR_CREATE_FAIL => 'PCLZIP_ERR_DIR_CREATE_FAIL',
  566. PCLZIP_ERR_BAD_EXTENSION => 'PCLZIP_ERR_BAD_EXTENSION',
  567. PCLZIP_ERR_BAD_FORMAT => 'PCLZIP_ERR_BAD_FORMAT',
  568. PCLZIP_ERR_DELETE_FILE_FAIL => 'PCLZIP_ERR_DELETE_FILE_FAIL',
  569. PCLZIP_ERR_RENAME_FILE_FAIL => 'PCLZIP_ERR_RENAME_FILE_FAIL',
  570. PCLZIP_ERR_BAD_CHECKSUM => 'PCLZIP_ERR_BAD_CHECKSUM',
  571. PCLZIP_ERR_INVALID_ARCHIVE_ZIP => 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP',
  572. PCLZIP_ERR_MISSING_OPTION_VALUE => 'PCLZIP_ERR_MISSING_OPTION_VALUE',
  573. PCLZIP_ERR_INVALID_OPTION_VALUE => 'PCLZIP_ERR_INVALID_OPTION_VALUE',
  574. PCLZIP_ERR_UNSUPPORTED_COMPRESSION => 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION',
  575. PCLZIP_ERR_UNSUPPORTED_ENCRYPTION => 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION',
  576. PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE => 'PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE',
  577. PCLZIP_ERR_DIRECTORY_RESTRICTION => 'PCLZIP_ERR_DIRECTORY_RESTRICTION'
  578. );
  579. if (isset($v_name[$this->error_code])) {
  580. $v_value = $v_name[$this->error_code];
  581. } else {
  582. $v_value = 'NoName';
  583. }
  584. if ($p_with_code) {
  585. return ($v_value . ' (' . $this->error_code . ')');
  586. } else {
  587. return ($v_value);
  588. }
  589. }
  590. function errorInfo($p_full = false) {
  591. if (PCLZIP_ERROR_EXTERNAL == 1) {
  592. return (PclErrorString());
  593. } else {
  594. if ($p_full) {
  595. return ($this->errorName(true) . " : " . $this->error_string);
  596. } else {
  597. return ($this->error_string . " [code " . $this->error_code . "]");
  598. }
  599. }
  600. }
  601. function privCheckFormat($p_level = 0) {
  602. $v_result = true;
  603. clearstatcache();
  604. $this->privErrorReset();
  605. if (!is_file($this->zipname)) {
  606. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "Missing archive file '" . $this->zipname . "'");
  607. return (false);
  608. }
  609. if (!is_readable($this->zipname)) {
  610. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '" . $this->zipname . "'");
  611. return (false);
  612. }
  613. return $v_result;
  614. }
  615. function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options = false) {
  616. $v_result = 1;
  617. $i = 0;
  618. while ($i < $p_size) {
  619. if (!isset($v_requested_options[$p_options_list[$i]])) {
  620. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid optional parameter '" . $p_options_list[$i] . "' for this method");
  621. return PclZip::errorCode();
  622. }
  623. switch ($p_options_list[$i]) {
  624. case PCLZIP_OPT_PATH :
  625. case PCLZIP_OPT_REMOVE_PATH :
  626. case PCLZIP_OPT_ADD_PATH :
  627. if (($i + 1) >= $p_size) {
  628. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  629. return PclZip::errorCode();
  630. }
  631. $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i + 1], FALSE);
  632. $i++;
  633. break;
  634. case PCLZIP_OPT_TEMP_FILE_THRESHOLD :
  635. if (($i + 1) >= $p_size) {
  636. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  637. return PclZip::errorCode();
  638. }
  639. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
  640. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '" . PclZipUtilOptionText($p_options_list[$i]) . "' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
  641. return PclZip::errorCode();
  642. }
  643. $v_value = $p_options_list[$i + 1];
  644. if ((!is_integer($v_value)) || ($v_value < 0)) {
  645. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Integer expected for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  646. return PclZip::errorCode();
  647. }
  648. $v_result_list[$p_options_list[$i]] = $v_value * 1048576;
  649. $i++;
  650. break;
  651. case PCLZIP_OPT_TEMP_FILE_ON :
  652. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
  653. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '" . PclZipUtilOptionText($p_options_list[$i]) . "' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
  654. return PclZip::errorCode();
  655. }
  656. $v_result_list[$p_options_list[$i]] = true;
  657. break;
  658. case PCLZIP_OPT_TEMP_FILE_OFF :
  659. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_ON])) {
  660. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '" . PclZipUtilOptionText($p_options_list[$i]) . "' can not be used with option 'PCLZIP_OPT_TEMP_FILE_ON'");
  661. return PclZip::errorCode();
  662. }
  663. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
  664. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '" . PclZipUtilOptionText($p_options_list[$i]) . "' can not be used with option 'PCLZIP_OPT_TEMP_FILE_THRESHOLD'");
  665. return PclZip::errorCode();
  666. }
  667. $v_result_list[$p_options_list[$i]] = true;
  668. break;
  669. case PCLZIP_OPT_EXTRACT_DIR_RESTRICTION :
  670. if (($i + 1) >= $p_size) {
  671. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  672. return PclZip::errorCode();
  673. }
  674. if (is_string($p_options_list[$i + 1])
  675. && ($p_options_list[$i + 1] != '')
  676. ) {
  677. $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i + 1], FALSE);
  678. $i++;
  679. } else {
  680. }
  681. break;
  682. case PCLZIP_OPT_BY_NAME :
  683. if (($i + 1) >= $p_size) {
  684. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  685. return PclZip::errorCode();
  686. }
  687. if (is_string($p_options_list[$i + 1])) {
  688. $v_result_list[$p_options_list[$i]][0] = $p_options_list[$i + 1];
  689. } else if (is_array($p_options_list[$i + 1])) {
  690. $v_result_list[$p_options_list[$i]] = $p_options_list[$i + 1];
  691. } else {
  692. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  693. return PclZip::errorCode();
  694. }
  695. $i++;
  696. break;
  697. case PCLZIP_OPT_BY_EREG :
  698. $p_options_list[$i] = PCLZIP_OPT_BY_PREG;
  699. case PCLZIP_OPT_BY_PREG :
  700. if (($i + 1) >= $p_size) {
  701. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  702. return PclZip::errorCode();
  703. }
  704. if (is_string($p_options_list[$i + 1])) {
  705. $v_result_list[$p_options_list[$i]] = $p_options_list[$i + 1];
  706. } else {
  707. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  708. return PclZip::errorCode();
  709. }
  710. $i++;
  711. break;
  712. case PCLZIP_OPT_COMMENT :
  713. case PCLZIP_OPT_ADD_COMMENT :
  714. case PCLZIP_OPT_PREPEND_COMMENT :
  715. if (($i + 1) >= $p_size) {
  716. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE,
  717. "Missing parameter value for option '"
  718. . PclZipUtilOptionText($p_options_list[$i])
  719. . "'");
  720. return PclZip::errorCode();
  721. }
  722. if (is_string($p_options_list[$i + 1])) {
  723. $v_result_list[$p_options_list[$i]] = $p_options_list[$i + 1];
  724. } else {
  725. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE,
  726. "Wrong parameter value for option '"
  727. . PclZipUtilOptionText($p_options_list[$i])
  728. . "'");
  729. return PclZip::errorCode();
  730. }
  731. $i++;
  732. break;
  733. case PCLZIP_OPT_BY_INDEX :
  734. if (($i + 1) >= $p_size) {
  735. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  736. return PclZip::errorCode();
  737. }
  738. $v_work_list = array();
  739. if (is_string($p_options_list[$i + 1])) {
  740. $p_options_list[$i + 1] = strtr($p_options_list[$i + 1], ' ', '');
  741. $v_work_list = explode(",", $p_options_list[$i + 1]);
  742. } else if (is_integer($p_options_list[$i + 1])) {
  743. $v_work_list[0] = $p_options_list[$i + 1] . '-' . $p_options_list[$i + 1];
  744. } else if (is_array($p_options_list[$i + 1])) {
  745. $v_work_list = $p_options_list[$i + 1];
  746. } else {
  747. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Value must be integer, string or array for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  748. return PclZip::errorCode();
  749. }
  750. $v_sort_flag = false;
  751. $v_sort_value = 0;
  752. for ($j = 0; $j < sizeof($v_work_list); $j++) {
  753. $v_item_list = explode("-", $v_work_list[$j]);
  754. $v_size_item_list = sizeof($v_item_list);
  755. if ($v_size_item_list == 1) {
  756. $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
  757. $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[0];
  758. } elseif ($v_size_item_list == 2) {
  759. $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
  760. $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[1];
  761. } else {
  762. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Too many values in index range for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  763. return PclZip::errorCode();
  764. }
  765. if ($v_result_list[$p_options_list[$i]][$j]['start'] < $v_sort_value) {
  766. $v_sort_flag = true;
  767. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Invalid order of index range for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  768. return PclZip::errorCode();
  769. }
  770. $v_sort_value = $v_result_list[$p_options_list[$i]][$j]['start'];
  771. }
  772. if ($v_sort_flag) {
  773. }
  774. $i++;
  775. break;
  776. case PCLZIP_OPT_REMOVE_ALL_PATH :
  777. case PCLZIP_OPT_EXTRACT_AS_STRING :
  778. case PCLZIP_OPT_NO_COMPRESSION :
  779. case PCLZIP_OPT_EXTRACT_IN_OUTPUT :
  780. case PCLZIP_OPT_REPLACE_NEWER :
  781. case PCLZIP_OPT_STOP_ON_ERROR :
  782. $v_result_list[$p_options_list[$i]] = true;
  783. break;
  784. case PCLZIP_OPT_SET_CHMOD :
  785. if (($i + 1) >= $p_size) {
  786. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  787. return PclZip::errorCode();
  788. }
  789. $v_result_list[$p_options_list[$i]] = $p_options_list[$i + 1];
  790. $i++;
  791. break;
  792. case PCLZIP_CB_PRE_EXTRACT :
  793. case PCLZIP_CB_POST_EXTRACT :
  794. case PCLZIP_CB_PRE_ADD :
  795. case PCLZIP_CB_POST_ADD :
  796. if (($i + 1) >= $p_size) {
  797. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  798. return PclZip::errorCode();
  799. }
  800. $v_function_name = $p_options_list[$i + 1];
  801. if (!function_exists($v_function_name)) {
  802. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Function '" . $v_function_name . "()' is not an existing function for option '" . PclZipUtilOptionText($p_options_list[$i]) . "'");
  803. return PclZip::errorCode();
  804. }
  805. $v_result_list[$p_options_list[$i]] = $v_function_name;
  806. $i++;
  807. break;
  808. default :
  809. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
  810. "Unknown parameter '"
  811. . $p_options_list[$i] . "'");
  812. return PclZip::errorCode();
  813. }
  814. $i++;
  815. }
  816. if ($v_requested_options !== false) {
  817. for ($key = reset($v_requested_options); $key = key($v_requested_options); $key = next($v_requested_options)) {
  818. if ($v_requested_options[$key] == 'mandatory') {
  819. if (!isset($v_result_list[$key])) {
  820. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter " . PclZipUtilOptionText($key) . "(" . $key . ")");
  821. return PclZip::errorCode();
  822. }
  823. }
  824. }
  825. }
  826. if (!isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
  827. }
  828. return $v_result;
  829. }
  830. function privOptionDefaultThreshold(&$p_options) {
  831. $v_result = 1;
  832. if (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
  833. || isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF])
  834. ) {
  835. return $v_result;
  836. }
  837. $v_memory_limit = ini_get('memory_limit');
  838. $v_memory_limit = trim($v_memory_limit);
  839. $last = strtolower(substr($v_memory_limit, -1));
  840. if ($last == 'g')
  841. $v_memory_limit = $v_memory_limit * 1073741824;
  842. if ($last == 'm')
  843. $v_memory_limit = $v_memory_limit * 1048576;
  844. if ($last == 'k')
  845. $v_memory_limit = $v_memory_limit * 1024;
  846. $p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] = floor($v_memory_limit * PCLZIP_TEMPORARY_FILE_RATIO);
  847. if ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] < 1048576) {
  848. unset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]);
  849. }
  850. return $v_result;
  851. }
  852. function privFileDescrParseAtt(&$p_file_list, &$p_filedescr, $v_options, $v_requested_options = false) {
  853. $v_result = 1;
  854. foreach ($p_file_list as $v_key => $v_value) {
  855. if (!isset($v_requested_options[$v_key])) {
  856. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file attribute '" . $v_key . "' for this file");
  857. return PclZip::errorCode();
  858. }
  859. switch ($v_key) {
  860. case PCLZIP_ATT_FILE_NAME :
  861. if (!is_string($v_value)) {
  862. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type " . gettype($v_value) . ". String expected for attribute '" . PclZipUtilOptionText($v_key) . "'");
  863. return PclZip::errorCode();
  864. }
  865. $p_filedescr['filename'] = PclZipUtilPathReduction($v_value);
  866. if ($p_filedescr['filename'] == '') {
  867. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty filename for attribute '" . PclZipUtilOptionText($v_key) . "'");
  868. return PclZip::errorCode();
  869. }
  870. break;
  871. case PCLZIP_ATT_FILE_NEW_SHORT_NAME :
  872. if (!is_string($v_value)) {
  873. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type " . gettype($v_value) . ". String expected for attribute '" . PclZipUtilOptionText($v_key) . "'");
  874. return PclZip::errorCode();
  875. }
  876. $p_filedescr['new_short_name'] = PclZipUtilPathReduction($v_value);
  877. if ($p_filedescr['new_short_name'] == '') {
  878. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty short filename for attribute '" . PclZipUtilOptionText($v_key) . "'");
  879. return PclZip::errorCode();
  880. }
  881. break;
  882. case PCLZIP_ATT_FILE_NEW_FULL_NAME :
  883. if (!is_string($v_value)) {
  884. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type " . gettype($v_value) . ". String expected for attribute '" . PclZipUtilOptionText($v_key) . "'");
  885. return PclZip::errorCode();
  886. }
  887. $p_filedescr['new_full_name'] = PclZipUtilPathReduction($v_value);
  888. if ($p_filedescr['new_full_name'] == '') {
  889. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty full filename for attribute '" . PclZipUtilOptionText($v_key) . "'");
  890. return PclZip::errorCode();
  891. }
  892. break;
  893. case PCLZIP_ATT_FILE_COMMENT :
  894. if (!is_string($v_value)) {
  895. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type " . gettype($v_value) . ". String expected for attribute '" . PclZipUtilOptionText($v_key) . "'");
  896. return PclZip::errorCode();
  897. }
  898. $p_filedescr['comment'] = $v_value;
  899. break;
  900. case PCLZIP_ATT_FILE_MTIME :
  901. if (!is_integer($v_value)) {
  902. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type " . gettype($v_value) . ". Integer expected for attribute '" . PclZipUtilOptionText($v_key) . "'");
  903. return PclZip::errorCode();
  904. }
  905. $p_filedescr['mtime'] = $v_value;
  906. break;
  907. case PCLZIP_ATT_FILE_CONTENT :
  908. $p_filedescr['content'] = $v_value;
  909. break;
  910. default :
  911. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
  912. "Unknown parameter '" . $v_key . "'");
  913. return PclZip::errorCode();
  914. }
  915. if ($v_requested_options !== false) {
  916. for ($key = reset($v_requested_options); $key = key($v_requested_options); $key = next($v_requested_options)) {
  917. if ($v_requested_options[$key] == 'mandatory') {
  918. if (!isset($p_file_list[$key])) {
  919. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter " . PclZipUtilOptionText($key) . "(" . $key . ")");
  920. return PclZip::errorCode();
  921. }
  922. }
  923. }
  924. }
  925. }
  926. return $v_result;
  927. }
  928. function privFileDescrExpand(&$p_filedescr_list, &$p_options) {
  929. $v_result = 1;
  930. $v_result_list = array();
  931. for ($i = 0; $i < sizeof($p_filedescr_list); $i++) {
  932. $v_descr = $p_filedescr_list[$i];
  933. $v_descr['filename'] = PclZipUtilTranslateWinPath($v_descr['filename'], false);
  934. $v_descr['filename'] = PclZipUtilPathReduction($v_descr['filename']);
  935. if (file_exists($v_descr['filename'])) {
  936. if (@is_file($v_descr['filename'])) {
  937. $v_descr['type'] = 'file';
  938. } else if (@is_dir($v_descr['filename'])) {
  939. $v_descr['type'] = 'folder';
  940. } else if (@is_link($v_descr['filename'])) {
  941. continue;
  942. } else {
  943. continue;
  944. }
  945. } else if (isset($v_descr['content'])) {
  946. $v_descr['type'] = 'virtual_file';
  947. } else {
  948. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '" . $v_descr['filename'] . "' does not exist");
  949. return PclZip::errorCode();
  950. }
  951. $this->privCalculateStoredFilename($v_descr, $p_options);
  952. $v_result_list[sizeof($v_result_list)] = $v_descr;
  953. if ($v_descr['type'] == 'folder') {
  954. $v_dirlist_descr = array();
  955. $v_dirlist_nb = 0;
  956. if ($v_folder_handler = @opendir($v_descr['filename'])) {
  957. while (($v_item_handler = @readdir($v_folder_handler)) !== false) {
  958. if (($v_item_handler == '.') || ($v_item_handler == '..')) {
  959. continue;
  960. }
  961. $v_dirlist_descr[$v_dirlist_nb]['filename'] = $v_descr['filename'] . '/' . $v_item_handler;
  962. if (($v_descr['stored_filename'] != $v_descr['filename'])
  963. && (!isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH]))
  964. ) {
  965. if ($v_descr['stored_filename'] != '') {
  966. $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_descr['stored_filename'] . '/' . $v_item_handler;
  967. } else {
  968. $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_item_handler;
  969. }
  970. }
  971. $v_dirlist_nb++;
  972. }
  973. @closedir($v_folder_handler);
  974. } else {
  975. }
  976. if ($v_dirlist_nb != 0) {
  977. if (($v_result = $this->privFileDescrExpand($v_dirlist_descr, $p_options)) != 1) {
  978. return $v_result;
  979. }
  980. $v_result_list = array_merge($v_result_list, $v_dirlist_descr);
  981. } else {
  982. }
  983. unset($v_dirlist_descr);
  984. }
  985. }
  986. $p_filedescr_list = $v_result_list;
  987. return $v_result;
  988. }
  989. function privCreate($p_filedescr_list, &$p_result_list, &$p_options) {
  990. $v_result = 1;
  991. $v_list_detail = array();
  992. $this->privDisableMagicQuotes();
  993. if (($v_result = $this->privOpenFd('wb')) != 1) {
  994. return $v_result;
  995. }
  996. $v_result = $this->privAddList($p_filedescr_list, $p_result_list, $p_options);
  997. $this->privCloseFd();
  998. $this->privSwapBackMagicQuotes();
  999. return $v_result;
  1000. }
  1001. function privAdd($p_filedescr_list, &$p_result_list, &$p_options) {
  1002. $v_result = 1;
  1003. $v_list_detail = array();
  1004. if ((!is_file($this->zipname)) || (filesize($this->zipname) == 0)) {
  1005. $v_result = $this->privCreate($p_filedescr_list, $p_result_list, $p_options);
  1006. return $v_result;
  1007. }
  1008. $this->privDisableMagicQuotes();
  1009. if (($v_result = $this->privOpenFd('rb')) != 1) {
  1010. $this->privSwapBackMagicQuotes();
  1011. return $v_result;
  1012. }
  1013. $v_central_dir = array();
  1014. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  1015. $this->privCloseFd();
  1016. $this->privSwapBackMagicQuotes();
  1017. return $v_result;
  1018. }
  1019. @rewind($this->zip_fd);
  1020. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.tmp';
  1021. if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0) {
  1022. $this->privCloseFd();
  1023. $this->privSwapBackMagicQuotes();
  1024. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_zip_temp_name . '\' in binary write mode');
  1025. return PclZip::errorCode();
  1026. }
  1027. $v_size = $v_central_dir['offset'];
  1028. while ($v_size != 0) {
  1029. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1030. $v_buffer = fread($this->zip_fd, $v_read_size);
  1031. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  1032. $v_size -= $v_read_size;
  1033. }
  1034. $v_swap = $this->zip_fd;
  1035. $this->zip_fd = $v_zip_temp_fd;
  1036. $v_zip_temp_fd = $v_swap;
  1037. $v_header_list = array();
  1038. if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1) {
  1039. fclose($v_zip_temp_fd);
  1040. $this->privCloseFd();
  1041. @unlink($v_zip_temp_name);
  1042. $this->privSwapBackMagicQuotes();
  1043. return $v_result;
  1044. }
  1045. $v_offset = @ftell($this->zip_fd);
  1046. $v_size = $v_central_dir['size'];
  1047. while ($v_size != 0) {
  1048. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1049. $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
  1050. @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  1051. $v_size -= $v_read_size;
  1052. }
  1053. for ($i = 0, $v_count = 0; $i < sizeof($v_header_list); $i++) {
  1054. if ($v_header_list[$i]['status'] == 'ok') {
  1055. if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  1056. fclose($v_zip_temp_fd);
  1057. $this->privCloseFd();
  1058. @unlink($v_zip_temp_name);
  1059. $this->privSwapBackMagicQuotes();
  1060. return $v_result;
  1061. }
  1062. $v_count++;
  1063. }
  1064. $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  1065. }
  1066. $v_comment = $v_central_dir['comment'];
  1067. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  1068. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  1069. }
  1070. if (isset($p_options[PCLZIP_OPT_ADD_COMMENT])) {
  1071. $v_comment = $v_comment . $p_options[PCLZIP_OPT_ADD_COMMENT];
  1072. }
  1073. if (isset($p_options[PCLZIP_OPT_PREPEND_COMMENT])) {
  1074. $v_comment = $p_options[PCLZIP_OPT_PREPEND_COMMENT] . $v_comment;
  1075. }
  1076. $v_size = @ftell($this->zip_fd) - $v_offset;
  1077. if (($v_result = $this->privWriteCentralHeader($v_count + $v_central_dir['entries'], $v_size, $v_offset, $v_comment)) != 1) {
  1078. unset($v_header_list);
  1079. $this->privSwapBackMagicQuotes();
  1080. return $v_result;
  1081. }
  1082. $v_swap = $this->zip_fd;
  1083. $this->zip_fd = $v_zip_temp_fd;
  1084. $v_zip_temp_fd = $v_swap;
  1085. $this->privCloseFd();
  1086. @fclose($v_zip_temp_fd);
  1087. $this->privSwapBackMagicQuotes();
  1088. @unlink($this->zipname);
  1089. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  1090. return $v_result;
  1091. }
  1092. function privOpenFd($p_mode) {
  1093. $v_result = 1;
  1094. if ($this->zip_fd != 0) {
  1095. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Zip file \'' . $this->zipname . '\' already open');
  1096. return PclZip::errorCode();
  1097. }
  1098. if (($this->zip_fd = @fopen($this->zipname, $p_mode)) == 0) {
  1099. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \'' . $this->zipname . '\' in ' . $p_mode . ' mode');
  1100. return PclZip::errorCode();
  1101. }
  1102. return $v_result;
  1103. }
  1104. function privCloseFd() {
  1105. $v_result = 1;
  1106. if ($this->zip_fd != 0)
  1107. @fclose($this->zip_fd);
  1108. $this->zip_fd = 0;
  1109. return $v_result;
  1110. }
  1111. function privAddList($p_filedescr_list, &$p_result_list, &$p_options) {
  1112. $v_result = 1;
  1113. $v_header_list = array();
  1114. if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1) {
  1115. return $v_result;
  1116. }
  1117. $v_offset = @ftell($this->zip_fd);
  1118. for ($i = 0, $v_count = 0; $i < sizeof($v_header_list); $i++) {
  1119. if ($v_header_list[$i]['status'] == 'ok') {
  1120. if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  1121. return $v_result;
  1122. }
  1123. $v_count++;
  1124. }
  1125. $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  1126. }
  1127. $v_comment = '';
  1128. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  1129. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  1130. }
  1131. $v_size = @ftell($this->zip_fd) - $v_offset;
  1132. if (($v_result = $this->privWriteCentralHeader($v_count, $v_size, $v_offset, $v_comment)) != 1) {
  1133. unset($v_header_list);
  1134. return $v_result;
  1135. }
  1136. return $v_result;
  1137. }
  1138. function privAddFileList($p_filedescr_list, &$p_result_list, &$p_options) {
  1139. $v_result = 1;
  1140. $v_header = array();
  1141. $v_nb = sizeof($p_result_list);
  1142. for ($j = 0; ($j < sizeof($p_filedescr_list)) && ($v_result == 1); $j++) {
  1143. $p_filedescr_list[$j]['filename']
  1144. = PclZipUtilTranslateWinPath($p_filedescr_list[$j]['filename'], false);
  1145. if ($p_filedescr_list[$j]['filename'] == "") {
  1146. continue;
  1147. }
  1148. if (($p_filedescr_list[$j]['type'] != 'virtual_file')
  1149. && (!file_exists($p_filedescr_list[$j]['filename']))
  1150. ) {
  1151. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '" . $p_filedescr_list[$j]['filename'] . "' does not exist");
  1152. return PclZip::errorCode();
  1153. }
  1154. if (($p_filedescr_list[$j]['type'] == 'file')
  1155. || ($p_filedescr_list[$j]['type'] == 'virtual_file')
  1156. || (($p_filedescr_list[$j]['type'] == 'folder')
  1157. && (!isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])
  1158. || !$p_options[PCLZIP_OPT_REMOVE_ALL_PATH]))
  1159. ) {
  1160. $v_result = $this->privAddFile($p_filedescr_list[$j], $v_header,
  1161. $p_options);
  1162. if ($v_result != 1) {
  1163. return $v_result;
  1164. }
  1165. $p_result_list[$v_nb++] = $v_header;
  1166. }
  1167. }
  1168. return $v_result;
  1169. }
  1170. function privAddFile($p_filedescr, &$p_header, &$p_options) {
  1171. $v_result = 1;
  1172. $p_filename = $p_filedescr['filename'];
  1173. if ($p_filename == "") {
  1174. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
  1175. return PclZip::errorCode();
  1176. }
  1177. clearstatcache();
  1178. $p_header['version'] = 20;
  1179. $p_header['version_extracted'] = 10;
  1180. $p_header['flag'] = 0;
  1181. $p_header['compression'] = 0;
  1182. $p_header['crc'] = 0;
  1183. $p_header['compressed_size'] = 0;
  1184. $p_header['filename_len'] = strlen($p_filename);
  1185. $p_header['extra_len'] = 0;
  1186. $p_header['disk'] = 0;
  1187. $p_header['internal'] = 0;
  1188. $p_header['offset'] = 0;
  1189. $p_header['filename'] = $p_filename;
  1190. $p_header['stored_filename'] = $p_filedescr['stored_filename'];
  1191. $p_header['extra'] = '';
  1192. $p_header['status'] = 'ok';
  1193. $p_header['index'] = -1;
  1194. if ($p_filedescr['type'] == 'file') {
  1195. $p_header['external'] = 0x00000000;
  1196. $p_header['size'] = filesize($p_filename);
  1197. } else if ($p_filedescr['type'] == 'folder') {
  1198. $p_header['external'] = 0x00000010;
  1199. $p_header['mtime'] = filemtime($p_filename);
  1200. $p_header['size'] = filesize($p_filename);
  1201. } else if ($p_filedescr['type'] == 'virtual_file') {
  1202. $p_header['external'] = 0x00000000;
  1203. $p_header['size'] = strlen($p_filedescr['content']);
  1204. }
  1205. if (isset($p_filedescr['mtime'])) {
  1206. $p_header['mtime'] = $p_filedescr['mtime'];
  1207. } else if ($p_filedescr['type'] == 'virtual_file') {
  1208. $p_header['mtime'] = time();
  1209. } else {
  1210. $p_header['mtime'] = filemtime($p_filename);
  1211. }
  1212. if (isset($p_filedescr['comment'])) {
  1213. $p_header['comment_len'] = strlen($p_filedescr['comment']);
  1214. $p_header['comment'] = $p_filedescr['comment'];
  1215. } else {
  1216. $p_header['comment_len'] = 0;
  1217. $p_header['comment'] = '';
  1218. }
  1219. if (isset($p_options[PCLZIP_CB_PRE_ADD])) {
  1220. $v_local_header = array();
  1221. $this->privConvertHeader2FileInfo($p_header, $v_local_header);
  1222. $v_result = $p_options[PCLZIP_CB_PRE_ADD](PCLZIP_CB_PRE_ADD, $v_local_header);
  1223. if ($v_result == 0) {
  1224. $p_header['status'] = "skipped";
  1225. $v_result = 1;
  1226. }
  1227. if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
  1228. $p_header['stored_filename'] = PclZipUtilPathReduction($v_local_header['stored_filename']);
  1229. }
  1230. }
  1231. if ($p_header['stored_filename'] == "") {
  1232. $p_header['status'] = "filtered";
  1233. }
  1234. if (strlen($p_header['stored_filename']) > 0xFF) {
  1235. $p_header['status'] = 'filename_too_long';
  1236. }
  1237. if ($p_header['status'] == 'ok') {
  1238. if ($p_filedescr['type'] == 'file') {
  1239. if ((!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF]))
  1240. && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON])
  1241. || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
  1242. && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_header['size'])))
  1243. ) {
  1244. $v_result = $this->privAddFileUsingTempFile($p_filedescr, $p_header, $p_options);
  1245. if ($v_result < PCLZIP_ERR_NO_ERROR) {
  1246. return $v_result;
  1247. }
  1248. } else {
  1249. if (($v_file = @fopen($p_filename, "rb")) == 0) {
  1250. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
  1251. return PclZip::errorCode();
  1252. }
  1253. $v_content = @fread($v_file, $p_header['size']);
  1254. @fclose($v_file);
  1255. $p_header['crc'] = @crc32($v_content);
  1256. if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
  1257. $p_header['compressed_size'] = $p_header['size'];
  1258. $p_header['compression'] = 0;
  1259. } else {
  1260. $v_content = @gzdeflate($v_content);
  1261. $p_header['compressed_size'] = strlen($v_content);
  1262. $p_header['compression'] = 8;
  1263. }
  1264. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  1265. @fclose($v_file);
  1266. return $v_result;
  1267. }
  1268. @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
  1269. }
  1270. } else if ($p_filedescr['type'] == 'virtual_file') {
  1271. $v_content = $p_filedescr['content'];
  1272. $p_header['crc'] = @crc32($v_content);
  1273. if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
  1274. $p_header['compressed_size'] = $p_header['size'];
  1275. $p_header['compression'] = 0;
  1276. } else {
  1277. $v_content = @gzdeflate($v_content);
  1278. $p_header['compressed_size'] = strlen($v_content);
  1279. $p_header['compression'] = 8;
  1280. }
  1281. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  1282. @fclose($v_file);
  1283. return $v_result;
  1284. }
  1285. @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
  1286. } else if ($p_filedescr['type'] == 'folder') {
  1287. if (@substr($p_header['stored_filename'], -1) != '/') {
  1288. $p_header['stored_filename'] .= '/';
  1289. }
  1290. $p_header['size'] = 0;
  1291. $p_header['external'] = 0x00000010;
  1292. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  1293. return $v_result;
  1294. }
  1295. }
  1296. }
  1297. if (isset($p_options[PCLZIP_CB_POST_ADD])) {
  1298. $v_local_header = array();
  1299. $this->privConvertHeader2FileInfo($p_header, $v_local_header);
  1300. $v_result = $p_options[PCLZIP_CB_POST_ADD](PCLZIP_CB_POST_ADD, $v_local_header);
  1301. if ($v_result == 0) {
  1302. $v_result = 1;
  1303. }
  1304. }
  1305. return $v_result;
  1306. }
  1307. function privAddFileUsingTempFile($p_filedescr, &$p_header, &$p_options) {
  1308. $v_result = PCLZIP_ERR_NO_ERROR;
  1309. $p_filename = $p_filedescr['filename'];
  1310. if (($v_file = @fopen($p_filename, "rb")) == 0) {
  1311. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
  1312. return PclZip::errorCode();
  1313. }
  1314. $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.gz';
  1315. if (($v_file_compressed = @gzopen($v_gzip_temp_name, "wb")) == 0) {
  1316. fclose($v_file);
  1317. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary write mode');
  1318. return PclZip::errorCode();
  1319. }
  1320. $v_size = filesize($p_filename);
  1321. while ($v_size != 0) {
  1322. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1323. $v_buffer = @fread($v_file, $v_read_size);
  1324. @gzputs($v_file_compressed, $v_buffer, $v_read_size);
  1325. $v_size -= $v_read_size;
  1326. }
  1327. @fclose($v_file);
  1328. @gzclose($v_file_compressed);
  1329. if (filesize($v_gzip_temp_name) < 18) {
  1330. PclZip::privErrorLog(PCLZIP_

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