Просмотр файла backup/pclzip.php

Размер файла: 72.05Kb
  1. <?php
  2. define( 'PCLZIP_READ_BLOCK_SIZE', 2048 );
  3. define( 'PCLZIP_SEPARATOR', ',' );
  4. define( 'PCLZIP_ERROR_EXTERNAL', 0 );
  5. define( 'PCLZIP_TEMPORARY_DIR', '' );
  6. $g_pclzip_version = "2.3";
  7. define( 'PCLZIP_ERR_USER_ABORTED', 2 );
  8. define( 'PCLZIP_ERR_NO_ERROR', 0 );
  9. define( 'PCLZIP_ERR_WRITE_OPEN_FAIL', -1 );
  10. define( 'PCLZIP_ERR_READ_OPEN_FAIL', -2 );
  11. define( 'PCLZIP_ERR_INVALID_PARAMETER', -3 );
  12. define( 'PCLZIP_ERR_MISSING_FILE', -4 );
  13. define( 'PCLZIP_ERR_FILENAME_TOO_LONG', -5 );
  14. define( 'PCLZIP_ERR_INVALID_ZIP', -6 );
  15. define( 'PCLZIP_ERR_BAD_EXTRACTED_FILE', -7 );
  16. define( 'PCLZIP_ERR_DIR_CREATE_FAIL', -8 );
  17. define( 'PCLZIP_ERR_BAD_EXTENSION', -9 );
  18. define( 'PCLZIP_ERR_BAD_FORMAT', -10 );
  19. define( 'PCLZIP_ERR_DELETE_FILE_FAIL', -11 );
  20. define( 'PCLZIP_ERR_RENAME_FILE_FAIL', -12 );
  21. define( 'PCLZIP_ERR_BAD_CHECKSUM', -13 );
  22. define( 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP', -14 );
  23. define( 'PCLZIP_ERR_MISSING_OPTION_VALUE', -15 );
  24. define( 'PCLZIP_ERR_INVALID_OPTION_VALUE', -16 );
  25. define( 'PCLZIP_ERR_ALREADY_A_DIRECTORY', -17 );
  26. define( 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION', -18 );
  27. define( 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION', -19 );
  28. define( 'PCLZIP_OPT_PATH', 77001 );
  29. define( 'PCLZIP_OPT_ADD_PATH', 77002 );
  30. define( 'PCLZIP_OPT_REMOVE_PATH', 77003 );
  31. define( 'PCLZIP_OPT_REMOVE_ALL_PATH', 77004 );
  32. define( 'PCLZIP_OPT_SET_CHMOD', 77005 );
  33. define( 'PCLZIP_OPT_EXTRACT_AS_STRING', 77006 );
  34. define( 'PCLZIP_OPT_NO_COMPRESSION', 77007 );
  35. define( 'PCLZIP_OPT_BY_NAME', 77008 );
  36. define( 'PCLZIP_OPT_BY_INDEX', 77009 );
  37. define( 'PCLZIP_OPT_BY_EREG', 77010 );
  38. define( 'PCLZIP_OPT_BY_PREG', 77011 );
  39. define( 'PCLZIP_OPT_COMMENT', 77012 );
  40. define( 'PCLZIP_OPT_ADD_COMMENT', 77013 );
  41. define( 'PCLZIP_OPT_PREPEND_COMMENT', 77014 );
  42. define( 'PCLZIP_OPT_EXTRACT_IN_OUTPUT', 77015 );
  43. define( 'PCLZIP_OPT_REPLACE_NEWER', 77016 );
  44. define( 'PCLZIP_OPT_STOP_ON_ERROR', 77017 );
  45. define( 'PCLZIP_CB_PRE_EXTRACT', 78001 );
  46. define( 'PCLZIP_CB_POST_EXTRACT', 78002 );
  47. define( 'PCLZIP_CB_PRE_ADD', 78003 );
  48. define( 'PCLZIP_CB_POST_ADD', 78004 );
  49. class PclZip
  50. {
  51. var $zipname = '';
  52. var $zip_fd = 0;
  53. var $error_code = 1;
  54. var $error_string = '';
  55. function PclZip($p_zipname)
  56. {
  57. if (!function_exists('gzopen'))
  58. {
  59. die('Abort '.basename(__FILE__).' : Missing zlib extensions');
  60. }
  61. $this->zipname = $p_zipname;
  62. $this->zip_fd = 0;
  63. return;
  64. }
  65. function create($p_filelist )
  66. {
  67. $v_result=1;
  68. $this->privErrorReset();
  69. $v_options = array();
  70. $v_add_path = "";
  71. $v_remove_path = "";
  72. $v_remove_all_path = false;
  73. $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
  74. $v_size = func_num_args();
  75. if ($v_size > 1) {
  76. $v_arg_list = &func_get_args();
  77. array_shift($v_arg_list);
  78. $v_size--;
  79. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  80. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  81. array (PCLZIP_OPT_REMOVE_PATH => 'optional',
  82. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  83. PCLZIP_OPT_ADD_PATH => 'optional',
  84. PCLZIP_CB_PRE_ADD => 'optional',
  85. PCLZIP_CB_POST_ADD => 'optional',
  86. PCLZIP_OPT_NO_COMPRESSION => 'optional',
  87. PCLZIP_OPT_COMMENT => 'optional'
  88. ));
  89. if ($v_result != 1) {
  90. return 0;
  91. }
  92. if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
  93. $v_add_path = $v_options[PCLZIP_OPT_ADD_PATH];
  94. }
  95. if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
  96. $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
  97. }
  98. if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  99. $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  100. }
  101. }
  102. else {
  103. $v_add_path = $v_arg_list[0];
  104. if ($v_size == 2) {
  105. $v_remove_path = $v_arg_list[1];
  106. }
  107. else if ($v_size > 2) {
  108. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
  109. "Invalid number / type of arguments");
  110. return 0;
  111. }
  112. }
  113. }
  114. $p_result_list = array();
  115. if (is_array($p_filelist))
  116. {
  117. $v_result = $this->privCreate($p_filelist, $p_result_list, $v_add_path, $v_remove_path, $v_remove_all_path, $v_options);
  118. }
  119. else if (is_string($p_filelist))
  120. {
  121. $v_list = explode(PCLZIP_SEPARATOR, $p_filelist);
  122. $v_result = $this->privCreate($v_list, $p_result_list, $v_add_path, $v_remove_path, $v_remove_all_path, $v_options);
  123. }
  124. else
  125. {
  126. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
  127. $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  128. }
  129. if ($v_result != 1)
  130. {
  131. return 0;
  132. }
  133. return $p_result_list;
  134. }
  135. function add($p_filelist )
  136. {
  137. $v_result=1;
  138. $this->privErrorReset();
  139. $v_options = array();
  140. $v_add_path = "";
  141. $v_remove_path = "";
  142. $v_remove_all_path = false;
  143. $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
  144. $v_size = func_num_args();
  145. if ($v_size > 1) {
  146. $v_arg_list = &func_get_args();
  147. array_shift($v_arg_list);
  148. $v_size--;
  149. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  150. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  151. array (PCLZIP_OPT_REMOVE_PATH => 'optional',
  152. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  153. PCLZIP_OPT_ADD_PATH => 'optional',
  154. PCLZIP_CB_PRE_ADD => 'optional',
  155. PCLZIP_CB_POST_ADD => 'optional',
  156. PCLZIP_OPT_NO_COMPRESSION => 'optional',
  157. PCLZIP_OPT_COMMENT => 'optional',
  158. PCLZIP_OPT_ADD_COMMENT => 'optional',
  159. PCLZIP_OPT_PREPEND_COMMENT => 'optional'
  160. ));
  161. if ($v_result != 1) {
  162. return 0;
  163. }
  164. if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
  165. $v_add_path = $v_options[PCLZIP_OPT_ADD_PATH];
  166. }
  167. if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
  168. $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
  169. }
  170. if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  171. $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  172. }
  173. }
  174. else {
  175. $v_add_path = $v_arg_list[0];
  176. if ($v_size == 2) {
  177. $v_remove_path = $v_arg_list[1];
  178. }
  179. else if ($v_size > 2) {
  180. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  181. return 0;
  182. }
  183. }
  184. }
  185. $p_result_list = array();
  186. if (is_array($p_filelist))
  187. {
  188. $v_result = $this->privAdd($p_filelist, $p_result_list, $v_add_path, $v_remove_path, $v_remove_all_path, $v_options);
  189. }
  190. else if (is_string($p_filelist))
  191. {
  192. $v_list = explode(PCLZIP_SEPARATOR, $p_filelist);
  193. $v_result = $this->privAdd($v_list, $p_result_list, $v_add_path, $v_remove_path, $v_remove_all_path, $v_options);
  194. }
  195. else
  196. {
  197. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
  198. $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  199. }
  200. if ($v_result != 1)
  201. {
  202. return 0;
  203. }
  204. return $p_result_list;
  205. }
  206. function listContent()
  207. {
  208. $v_result=1;
  209. $this->privErrorReset();
  210. if (!$this->privCheckFormat()) {
  211. return(0);
  212. }
  213. $p_list = array();
  214. if (($v_result = $this->privList($p_list)) != 1)
  215. {
  216. unset($p_list);
  217. return(0);
  218. }
  219. return $p_list;
  220. }
  221. function extract()
  222. {
  223. $v_result=1;
  224. $this->privErrorReset();
  225. if (!$this->privCheckFormat()) {
  226. return(0);
  227. }
  228. $v_options = array();
  229. $v_path = "./";
  230. $v_remove_path = "";
  231. $v_remove_all_path = false;
  232. $v_size = func_num_args();
  233. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
  234. if ($v_size > 0) {
  235. $v_arg_list = &func_get_args();
  236. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  237. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  238. array (PCLZIP_OPT_PATH => 'optional',
  239. PCLZIP_OPT_REMOVE_PATH => 'optional',
  240. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  241. PCLZIP_OPT_ADD_PATH => 'optional',
  242. PCLZIP_CB_PRE_EXTRACT => 'optional',
  243. PCLZIP_CB_POST_EXTRACT => 'optional',
  244. PCLZIP_OPT_SET_CHMOD => 'optional',
  245. PCLZIP_OPT_BY_NAME => 'optional',
  246. PCLZIP_OPT_BY_EREG => 'optional',
  247. PCLZIP_OPT_BY_PREG => 'optional',
  248. PCLZIP_OPT_BY_INDEX => 'optional',
  249. PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
  250. PCLZIP_OPT_EXTRACT_IN_OUTPUT => 'optional',
  251. PCLZIP_OPT_REPLACE_NEWER => 'optional'
  252. ,PCLZIP_OPT_STOP_ON_ERROR => 'optional'
  253. ));
  254. if ($v_result != 1) {
  255. return 0;
  256. }
  257. if (isset($v_options[PCLZIP_OPT_PATH])) {
  258. $v_path = $v_options[PCLZIP_OPT_PATH];
  259. }
  260. if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
  261. $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
  262. }
  263. if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  264. $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  265. }
  266. if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
  267. if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
  268. $v_path .= '/';
  269. }
  270. $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
  271. }
  272. }
  273. else {
  274. $v_path = $v_arg_list[0];
  275. if ($v_size == 2) {
  276. $v_remove_path = $v_arg_list[1];
  277. }
  278. else if ($v_size > 2) {
  279. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  280. return 0;
  281. }
  282. }
  283. }
  284. $p_list = array();
  285. $v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path,
  286. $v_remove_all_path, $v_options);
  287. if ($v_result < 1) {
  288. unset($p_list);
  289. return(0);
  290. }
  291. return $p_list;
  292. }
  293. function extractByIndex($p_index )
  294. {
  295. $v_result=1;
  296. $this->privErrorReset();
  297. if (!$this->privCheckFormat()) {
  298. return(0);
  299. }
  300. $v_options = array();
  301. $v_path = "./";
  302. $v_remove_path = "";
  303. $v_remove_all_path = false;
  304. $v_size = func_num_args();
  305. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
  306. if ($v_size > 1) {
  307. $v_arg_list = &func_get_args();
  308. array_shift($v_arg_list);
  309. $v_size--;
  310. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  311. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  312. array (PCLZIP_OPT_PATH => 'optional',
  313. PCLZIP_OPT_REMOVE_PATH => 'optional',
  314. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  315. PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
  316. PCLZIP_OPT_ADD_PATH => 'optional',
  317. PCLZIP_CB_PRE_EXTRACT => 'optional',
  318. PCLZIP_CB_POST_EXTRACT => 'optional',
  319. PCLZIP_OPT_SET_CHMOD => 'optional',
  320. PCLZIP_OPT_REPLACE_NEWER => 'optional'
  321. ,PCLZIP_OPT_STOP_ON_ERROR => 'optional'
  322. ));
  323. if ($v_result != 1) {
  324. return 0;
  325. }
  326. if (isset($v_options[PCLZIP_OPT_PATH])) {
  327. $v_path = $v_options[PCLZIP_OPT_PATH];
  328. }
  329. if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
  330. $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
  331. }
  332. if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  333. $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  334. }
  335. if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
  336. if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
  337. $v_path .= '/';
  338. }
  339. $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
  340. }
  341. if (!isset($v_options[PCLZIP_OPT_EXTRACT_AS_STRING])) {
  342. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
  343. }
  344. else {
  345. }
  346. }
  347. else {
  348. $v_path = $v_arg_list[0];
  349. if ($v_size == 2) {
  350. $v_remove_path = $v_arg_list[1];
  351. }
  352. else if ($v_size > 2) {
  353. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  354. return 0;
  355. }
  356. }
  357. }
  358. $v_arg_trick = array (PCLZIP_OPT_BY_INDEX, $p_index);
  359. $v_options_trick = array();
  360. $v_result = $this->privParseOptions($v_arg_trick, sizeof($v_arg_trick), $v_options_trick,
  361. array (PCLZIP_OPT_BY_INDEX => 'optional' ));
  362. if ($v_result != 1) {
  363. return 0;
  364. }
  365. $v_options[PCLZIP_OPT_BY_INDEX] = $v_options_trick[PCLZIP_OPT_BY_INDEX];
  366. if (($v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options)) < 1) {
  367. return(0);
  368. }
  369. return $p_list;
  370. }
  371. function delete()
  372. {
  373. $v_result=1;
  374. $this->privErrorReset();
  375. if (!$this->privCheckFormat()) {
  376. return(0);
  377. }
  378. $v_options = array();
  379. $v_size = func_num_args();
  380. if ($v_size <= 0) {
  381. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing arguments");
  382. return 0;
  383. }
  384. $v_arg_list = &func_get_args();
  385. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  386. array (PCLZIP_OPT_BY_NAME => 'optional',
  387. PCLZIP_OPT_BY_EREG => 'optional',
  388. PCLZIP_OPT_BY_PREG => 'optional',
  389. PCLZIP_OPT_BY_INDEX => 'optional' ));
  390. if ($v_result != 1) {
  391. return 0;
  392. }
  393. if ( (!isset($v_options[PCLZIP_OPT_BY_NAME]))
  394. && (!isset($v_options[PCLZIP_OPT_BY_EREG]))
  395. && (!isset($v_options[PCLZIP_OPT_BY_PREG]))
  396. && (!isset($v_options[PCLZIP_OPT_BY_INDEX]))) {
  397. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "At least one filtering rule must be set");
  398. return 0;
  399. }
  400. $v_list = array();
  401. if (($v_result = $this->privDeleteByRule($v_list, $v_options)) != 1)
  402. {
  403. unset($v_list);
  404. return(0);
  405. }
  406. return $v_list;
  407. }
  408. function deleteByIndex($p_index)
  409. {
  410. $p_list = $this->delete(PCLZIP_OPT_BY_INDEX, $p_index);
  411. return $p_list;
  412. }
  413. function properties()
  414. {
  415. $this->privErrorReset();
  416. if (!$this->privCheckFormat()) {
  417. return(0);
  418. }
  419. $v_prop = array();
  420. $v_prop['comment'] = '';
  421. $v_prop['nb'] = 0;
  422. $v_prop['status'] = 'not_exist';
  423. if (@is_file($this->zipname))
  424. {
  425. if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
  426. {
  427. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
  428. return 0;
  429. }
  430. $v_central_dir = array();
  431. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  432. {
  433. return 0;
  434. }
  435. $this->privCloseFd();
  436. $v_prop['comment'] = $v_central_dir['comment'];
  437. $v_prop['nb'] = $v_central_dir['entries'];
  438. $v_prop['status'] = 'ok';
  439. }
  440. return $v_prop;
  441. }
  442. function duplicate($p_archive)
  443. {
  444. $v_result = 1;
  445. $this->privErrorReset();
  446. if ((is_object($p_archive)) && (get_class($p_archive) == 'pclzip'))
  447. {
  448. $v_result = $this->privDuplicate($p_archive->zipname);
  449. }
  450. else if (is_string($p_archive))
  451. {
  452. if (!is_file($p_archive)) {
  453. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "No file with filename '".$p_archive."'");
  454. $v_result = PCLZIP_ERR_MISSING_FILE;
  455. }
  456. else {
  457. $v_result = $this->privDuplicate($p_archive);
  458. }
  459. }
  460. else
  461. {
  462. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
  463. $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  464. }
  465. return $v_result;
  466. }
  467. function merge($p_archive_to_add)
  468. {
  469. $v_result = 1;
  470. $this->privErrorReset();
  471. if (!$this->privCheckFormat()) {
  472. return(0);
  473. }
  474. if ((is_object($p_archive_to_add)) && (get_class($p_archive_to_add) == 'pclzip'))
  475. {
  476. $v_result = $this->privMerge($p_archive_to_add);
  477. }
  478. else if (is_string($p_archive_to_add))
  479. {
  480. $v_object_archive = new PclZip($p_archive_to_add);
  481. $v_result = $this->privMerge($v_object_archive);
  482. }
  483. else
  484. {
  485. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
  486. $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  487. }
  488. return $v_result;
  489. }
  490. function errorCode()
  491. {
  492. if (PCLZIP_ERROR_EXTERNAL == 1) {
  493. return(PclErrorCode());
  494. }
  495. else {
  496. return($this->error_code);
  497. }
  498. }
  499. function errorName($p_with_code=false)
  500. {
  501. $v_name = array ( PCLZIP_ERR_NO_ERROR => 'PCLZIP_ERR_NO_ERROR',
  502. PCLZIP_ERR_WRITE_OPEN_FAIL => 'PCLZIP_ERR_WRITE_OPEN_FAIL',
  503. PCLZIP_ERR_READ_OPEN_FAIL => 'PCLZIP_ERR_READ_OPEN_FAIL',
  504. PCLZIP_ERR_INVALID_PARAMETER => 'PCLZIP_ERR_INVALID_PARAMETER',
  505. PCLZIP_ERR_MISSING_FILE => 'PCLZIP_ERR_MISSING_FILE',
  506. PCLZIP_ERR_FILENAME_TOO_LONG => 'PCLZIP_ERR_FILENAME_TOO_LONG',
  507. PCLZIP_ERR_INVALID_ZIP => 'PCLZIP_ERR_INVALID_ZIP',
  508. PCLZIP_ERR_BAD_EXTRACTED_FILE => 'PCLZIP_ERR_BAD_EXTRACTED_FILE',
  509. PCLZIP_ERR_DIR_CREATE_FAIL => 'PCLZIP_ERR_DIR_CREATE_FAIL',
  510. PCLZIP_ERR_BAD_EXTENSION => 'PCLZIP_ERR_BAD_EXTENSION',
  511. PCLZIP_ERR_BAD_FORMAT => 'PCLZIP_ERR_BAD_FORMAT',
  512. PCLZIP_ERR_DELETE_FILE_FAIL => 'PCLZIP_ERR_DELETE_FILE_FAIL',
  513. PCLZIP_ERR_RENAME_FILE_FAIL => 'PCLZIP_ERR_RENAME_FILE_FAIL',
  514. PCLZIP_ERR_BAD_CHECKSUM => 'PCLZIP_ERR_BAD_CHECKSUM',
  515. PCLZIP_ERR_INVALID_ARCHIVE_ZIP => 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP',
  516. PCLZIP_ERR_MISSING_OPTION_VALUE => 'PCLZIP_ERR_MISSING_OPTION_VALUE',
  517. PCLZIP_ERR_INVALID_OPTION_VALUE => 'PCLZIP_ERR_INVALID_OPTION_VALUE',
  518. PCLZIP_ERR_UNSUPPORTED_COMPRESSION => 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION',
  519. PCLZIP_ERR_UNSUPPORTED_ENCRYPTION => 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION' );
  520. if (isset($v_name[$this->error_code])) {
  521. $v_value = $v_name[$this->error_code];
  522. }
  523. else {
  524. $v_value = 'NoName';
  525. }
  526. if ($p_with_code) {
  527. return($v_value.' ('.$this->error_code.')');
  528. }
  529. else {
  530. return($v_value);
  531. }
  532. }
  533. function errorInfo($p_full=false)
  534. {
  535. if (PCLZIP_ERROR_EXTERNAL == 1) {
  536. return(PclErrorString());
  537. }
  538. else {
  539. if ($p_full) {
  540. return($this->errorName(true)." : ".$this->error_string);
  541. }
  542. else {
  543. return($this->error_string." [code ".$this->error_code."]");
  544. }
  545. }
  546. }
  547. function privCheckFormat($p_level=0)
  548. {
  549. $v_result = true;
  550. clearstatcache();
  551. $this->privErrorReset();
  552. if (!is_file($this->zipname)) {
  553. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "Missing archive file '".$this->zipname."'");
  554. return(false);
  555. }
  556. if (!is_readable($this->zipname)) {
  557. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '".$this->zipname."'");
  558. return(false);
  559. }
  560. return $v_result;
  561. }
  562. function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options=false)
  563. {
  564. $v_result=1;
  565. $i=0;
  566. while ($i<$p_size) {
  567. if (!isset($v_requested_options[$p_options_list[$i]])) {
  568. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid optional parameter '".$p_options_list[$i]."' for this method");
  569. return PclZip::errorCode();
  570. }
  571. switch ($p_options_list[$i]) {
  572. case PCLZIP_OPT_PATH :
  573. case PCLZIP_OPT_REMOVE_PATH :
  574. case PCLZIP_OPT_ADD_PATH :
  575. if (($i+1) >= $p_size) {
  576. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  577. return PclZip::errorCode();
  578. }
  579. $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], false);
  580. $i++;
  581. break;
  582. case PCLZIP_OPT_BY_NAME :
  583. if (($i+1) >= $p_size) {
  584. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  585. return PclZip::errorCode();
  586. }
  587. if (is_string($p_options_list[$i+1])) {
  588. $v_result_list[$p_options_list[$i]][0] = $p_options_list[$i+1];
  589. }
  590. else if (is_array($p_options_list[$i+1])) {
  591. $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
  592. }
  593. else {
  594. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  595. return PclZip::errorCode();
  596. }
  597. $i++;
  598. break;
  599. case PCLZIP_OPT_BY_EREG :
  600. case PCLZIP_OPT_BY_PREG :
  601. if (($i+1) >= $p_size) {
  602. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  603. return PclZip::errorCode();
  604. }
  605. if (is_string($p_options_list[$i+1])) {
  606. $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
  607. }
  608. else {
  609. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  610. return PclZip::errorCode();
  611. }
  612. $i++;
  613. break;
  614. case PCLZIP_OPT_COMMENT :
  615. case PCLZIP_OPT_ADD_COMMENT :
  616. case PCLZIP_OPT_PREPEND_COMMENT :
  617. if (($i+1) >= $p_size) {
  618. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE,
  619. "Missing parameter value for option '"
  620. .PclZipUtilOptionText($p_options_list[$i])
  621. ."'");
  622. return PclZip::errorCode();
  623. }
  624. if (is_string($p_options_list[$i+1])) {
  625. $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
  626. }
  627. else {
  628. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE,
  629. "Wrong parameter value for option '"
  630. .PclZipUtilOptionText($p_options_list[$i])
  631. ."'");
  632. return PclZip::errorCode();
  633. }
  634. $i++;
  635. break;
  636. case PCLZIP_OPT_BY_INDEX :
  637. if (($i+1) >= $p_size) {
  638. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  639. return PclZip::errorCode();
  640. }
  641. $v_work_list = array();
  642. if (is_string($p_options_list[$i+1])) {
  643. $p_options_list[$i+1] = strtr($p_options_list[$i+1], ' ', '');
  644. $v_work_list = explode(",", $p_options_list[$i+1]);
  645. }
  646. else if (is_integer($p_options_list[$i+1])) {
  647. $v_work_list[0] = $p_options_list[$i+1].'-'.$p_options_list[$i+1];
  648. }
  649. else if (is_array($p_options_list[$i+1])) {
  650. $v_work_list = $p_options_list[$i+1];
  651. }
  652. else {
  653. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Value must be integer, string or array for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  654. return PclZip::errorCode();
  655. }
  656. $v_sort_flag=false;
  657. $v_sort_value=0;
  658. for ($j=0; $j<sizeof($v_work_list); $j++) {
  659. $v_item_list = explode("-", $v_work_list[$j]);
  660. $v_size_item_list = sizeof($v_item_list);
  661. if ($v_size_item_list == 1) {
  662. $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
  663. $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[0];
  664. }
  665. elseif ($v_size_item_list == 2) {
  666. $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
  667. $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[1];
  668. }
  669. else {
  670. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Too many values in index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  671. return PclZip::errorCode();
  672. }
  673. if ($v_result_list[$p_options_list[$i]][$j]['start'] < $v_sort_value) {
  674. $v_sort_flag=true;
  675. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Invalid order of index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  676. return PclZip::errorCode();
  677. }
  678. $v_sort_value = $v_result_list[$p_options_list[$i]][$j]['start'];
  679. }
  680. if ($v_sort_flag) {
  681. }
  682. $i++;
  683. break;
  684. case PCLZIP_OPT_REMOVE_ALL_PATH :
  685. case PCLZIP_OPT_EXTRACT_AS_STRING :
  686. case PCLZIP_OPT_NO_COMPRESSION :
  687. case PCLZIP_OPT_EXTRACT_IN_OUTPUT :
  688. case PCLZIP_OPT_REPLACE_NEWER :
  689. case PCLZIP_OPT_STOP_ON_ERROR :
  690. $v_result_list[$p_options_list[$i]] = true;
  691. break;
  692. case PCLZIP_OPT_SET_CHMOD :
  693. if (($i+1) >= $p_size) {
  694. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  695. return PclZip::errorCode();
  696. }
  697. $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
  698. $i++;
  699. break;
  700. case PCLZIP_CB_PRE_EXTRACT :
  701. case PCLZIP_CB_POST_EXTRACT :
  702. case PCLZIP_CB_PRE_ADD :
  703. case PCLZIP_CB_POST_ADD :
  704. if (($i+1) >= $p_size) {
  705. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  706. return PclZip::errorCode();
  707. }
  708. $v_function_name = $p_options_list[$i+1];
  709. if (!function_exists($v_function_name)) {
  710. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Function '".$v_function_name."()' is not an existing function for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  711. return PclZip::errorCode();
  712. }
  713. $v_result_list[$p_options_list[$i]] = $v_function_name;
  714. $i++;
  715. break;
  716. default :
  717. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
  718. "Unknown parameter '"
  719. .$p_options_list[$i]."'");
  720. return PclZip::errorCode();
  721. }
  722. $i++;
  723. }
  724. if ($v_requested_options !== false) {
  725. for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
  726. if ($v_requested_options[$key] == 'mandatory') {
  727. if (!isset($v_result_list[$key])) {
  728. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
  729. return PclZip::errorCode();
  730. }
  731. }
  732. }
  733. }
  734. return $v_result;
  735. }
  736. function privCreate($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
  737. {
  738. $v_result=1;
  739. $v_list_detail = array();
  740. if (($v_result = $this->privOpenFd('wb')) != 1)
  741. {
  742. return $v_result;
  743. }
  744. $v_result = $this->privAddList($p_list, $p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options);
  745. $this->privCloseFd();
  746. return $v_result;
  747. }
  748. function privAdd($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
  749. {
  750. $v_result=1;
  751. $v_list_detail = array();
  752. if ((!is_file($this->zipname)) || (filesize($this->zipname) == 0))
  753. {
  754. $v_result = $this->privCreate($p_list, $p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options);
  755. return $v_result;
  756. }
  757. if (($v_result=$this->privOpenFd('rb')) != 1)
  758. {
  759. return $v_result;
  760. }
  761. $v_central_dir = array();
  762. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  763. {
  764. $this->privCloseFd();
  765. return $v_result;
  766. }
  767. @rewind($this->zip_fd);
  768. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
  769. if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
  770. {
  771. $this->privCloseFd();
  772. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
  773. return PclZip::errorCode();
  774. }
  775. $v_size = $v_central_dir['offset'];
  776. while ($v_size != 0)
  777. {
  778. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  779. $v_buffer = fread($this->zip_fd, $v_read_size);
  780. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  781. $v_size -= $v_read_size;
  782. }
  783. $v_swap = $this->zip_fd;
  784. $this->zip_fd = $v_zip_temp_fd;
  785. $v_zip_temp_fd = $v_swap;
  786. $v_header_list = array();
  787. if (($v_result = $this->privAddFileList($p_list, $v_header_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options)) != 1)
  788. {
  789. fclose($v_zip_temp_fd);
  790. $this->privCloseFd();
  791. @unlink($v_zip_temp_name);
  792. return $v_result;
  793. }
  794. $v_offset = @ftell($this->zip_fd);
  795. $v_size = $v_central_dir['size'];
  796. while ($v_size != 0)
  797. {
  798. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  799. $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
  800. @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  801. $v_size -= $v_read_size;
  802. }
  803. for ($i=0, $v_count=0; $i<sizeof($v_header_list); $i++)
  804. {
  805. if ($v_header_list[$i]['status'] == 'ok') {
  806. if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  807. fclose($v_zip_temp_fd);
  808. $this->privCloseFd();
  809. @unlink($v_zip_temp_name);
  810. return $v_result;
  811. }
  812. $v_count++;
  813. }
  814. $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  815. }
  816. $v_comment = $v_central_dir['comment'];
  817. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  818. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  819. }
  820. if (isset($p_options[PCLZIP_OPT_ADD_COMMENT])) {
  821. $v_comment = $v_comment.$p_options[PCLZIP_OPT_ADD_COMMENT];
  822. }
  823. if (isset($p_options[PCLZIP_OPT_PREPEND_COMMENT])) {
  824. $v_comment = $p_options[PCLZIP_OPT_PREPEND_COMMENT].$v_comment;
  825. }
  826. $v_size = @ftell($this->zip_fd)-$v_offset;
  827. if (($v_result = $this->privWriteCentralHeader($v_count+$v_central_dir['entries'], $v_size, $v_offset, $v_comment)) != 1)
  828. {
  829. unset($v_header_list);
  830. return $v_result;
  831. }
  832. $v_swap = $this->zip_fd;
  833. $this->zip_fd = $v_zip_temp_fd;
  834. $v_zip_temp_fd = $v_swap;
  835. $this->privCloseFd();
  836. @fclose($v_zip_temp_fd);
  837. @unlink($this->zipname);
  838. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  839. return $v_result;
  840. }
  841. function privOpenFd($p_mode)
  842. {
  843. $v_result=1;
  844. if ($this->zip_fd != 0)
  845. {
  846. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Zip file \''.$this->zipname.'\' already open');
  847. return PclZip::errorCode();
  848. }
  849. if (($this->zip_fd = @fopen($this->zipname, $p_mode)) == 0)
  850. {
  851. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in '.$p_mode.' mode');
  852. return PclZip::errorCode();
  853. }
  854. return $v_result;
  855. }
  856. function privCloseFd()
  857. {
  858. $v_result=1;
  859. if ($this->zip_fd != 0)
  860. @fclose($this->zip_fd);
  861. $this->zip_fd = 0;
  862. return $v_result;
  863. }
  864. function privAddList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
  865. {
  866. $v_result=1;
  867. $v_header_list = array();
  868. if (($v_result = $this->privAddFileList($p_list, $v_header_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options)) != 1)
  869. {
  870. return $v_result;
  871. }
  872. $v_offset = @ftell($this->zip_fd);
  873. for ($i=0,$v_count=0; $i<sizeof($v_header_list); $i++)
  874. {
  875. if ($v_header_list[$i]['status'] == 'ok') {
  876. if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  877. return $v_result;
  878. }
  879. $v_count++;
  880. }
  881. $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  882. }
  883. $v_comment = '';
  884. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  885. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  886. }
  887. $v_size = @ftell($this->zip_fd)-$v_offset;
  888. if (($v_result = $this->privWriteCentralHeader($v_count, $v_size, $v_offset, $v_comment)) != 1)
  889. {
  890. unset($v_header_list);
  891. return $v_result;
  892. }
  893. return $v_result;
  894. }
  895. function privAddFileList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
  896. {
  897. $v_result=1;
  898. $v_header = array();
  899. $v_nb = sizeof($p_result_list);
  900. for ($j=0; ($j<count($p_list)) && ($v_result==1); $j++)
  901. {
  902. $p_filename = PclZipUtilTranslateWinPath($p_list[$j], false);
  903. if ($p_filename == "")
  904. {
  905. continue;
  906. }
  907. if (!file_exists($p_filename))
  908. {
  909. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '$p_filename' does not exists");
  910. return PclZip::errorCode();
  911. }
  912. if ((is_file($p_filename)) || ((is_dir($p_filename)) && !$p_remove_all_dir)) {
  913. if (($v_result = $this->privAddFile($p_filename, $v_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options)) != 1)
  914. {
  915. return $v_result;
  916. }
  917. $p_result_list[$v_nb++] = $v_header;
  918. }
  919. if (@is_dir($p_filename))
  920. {
  921. if ($p_filename != ".")
  922. $v_path = $p_filename."/";
  923. else
  924. $v_path = "";
  925. if ($p_hdir = @opendir($p_filename)) {
  926. $p_hitem = @readdir($p_hdir);
  927. $p_hitem = @readdir($p_hdir);
  928. while (($p_hitem = @readdir($p_hdir)) !== false) {
  929. if (is_file($v_path.$p_hitem)) {
  930. if (($v_result = $this->privAddFile($v_path.$p_hitem, $v_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options)) != 1) {
  931. return $v_result;
  932. }
  933. $p_result_list[$v_nb++] = $v_header;
  934. }
  935. else {
  936. $p_temp_list[0] = $v_path.$p_hitem;
  937. $v_result = $this->privAddFileList($p_temp_list, $p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options);
  938. $v_nb = sizeof($p_result_list);
  939. }
  940. }
  941. }
  942. unset($p_temp_list);
  943. unset($p_hdir);
  944. unset($p_hitem);
  945. }
  946. }
  947. return $v_result;
  948. }
  949. function privAddFile($p_filename, &$p_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
  950. {
  951. $v_result=1;
  952. if ($p_filename == "")
  953. {
  954. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
  955. return PclZip::errorCode();
  956. }
  957. $v_stored_filename = $p_filename;
  958. if ($p_remove_all_dir) {
  959. $v_stored_filename = basename($p_filename);
  960. }
  961. else if ($p_remove_dir != "")
  962. {
  963. if (substr($p_remove_dir, -1) != '/')
  964. $p_remove_dir .= "/";
  965. if ((substr($p_filename, 0, 2) == "./") || (substr($p_remove_dir, 0, 2) == "./"))
  966. {
  967. if ((substr($p_filename, 0, 2) == "./") && (substr($p_remove_dir, 0, 2) != "./"))
  968. $p_remove_dir = "./".$p_remove_dir;
  969. if ((substr($p_filename, 0, 2) != "./") && (substr($p_remove_dir, 0, 2) == "./"))
  970. $p_remove_dir = substr($p_remove_dir, 2);
  971. }
  972. $v_compare = PclZipUtilPathInclusion($p_remove_dir, $p_filename);
  973. if ($v_compare > 0)
  974. {
  975. if ($v_compare == 2) {
  976. $v_stored_filename = "";
  977. }
  978. else {
  979. $v_stored_filename = substr($p_filename, strlen($p_remove_dir));
  980. }
  981. }
  982. }
  983. if ($p_add_dir != "")
  984. {
  985. if (substr($p_add_dir, -1) == "/")
  986. $v_stored_filename = $p_add_dir.$v_stored_filename;
  987. else
  988. $v_stored_filename = $p_add_dir."/".$v_stored_filename;
  989. }
  990. $v_stored_filename = PclZipUtilPathReduction($v_stored_filename);
  991. clearstatcache();
  992. $p_header['version'] = 20;
  993. $p_header['version_extracted'] = 10;
  994. $p_header['flag'] = 0;
  995. $p_header['compression'] = 0;
  996. $p_header['mtime'] = filemtime($p_filename);
  997. $p_header['crc'] = 0;
  998. $p_header['compressed_size'] = 0;
  999. $p_header['size'] = filesize($p_filename);
  1000. $p_header['filename_len'] = strlen($p_filename);
  1001. $p_header['extra_len'] = 0;
  1002. $p_header['comment_len'] = 0;
  1003. $p_header['disk'] = 0;
  1004. $p_header['internal'] = 0;
  1005. $p_header['external'] = (is_file($p_filename)?0x00000000:0x00000010);
  1006. $p_header['offset'] = 0;
  1007. $p_header['filename'] = $p_filename;
  1008. $p_header['stored_filename'] = $v_stored_filename;
  1009. $p_header['extra'] = '';
  1010. $p_header['comment'] = '';
  1011. $p_header['status'] = 'ok';
  1012. $p_header['index'] = -1;
  1013. if (isset($p_options[PCLZIP_CB_PRE_ADD])) {
  1014. $v_local_header = array();
  1015. $this->privConvertHeader2FileInfo($p_header, $v_local_header);
  1016. eval('$v_result = '.$p_options[PCLZIP_CB_PRE_ADD].'(PCLZIP_CB_PRE_ADD, $v_local_header);');
  1017. if ($v_result == 0) {
  1018. $p_header['status'] = "skipped";
  1019. $v_result = 1;
  1020. }
  1021. if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
  1022. $p_header['stored_filename'] = PclZipUtilPathReduction($v_local_header['stored_filename']);
  1023. }
  1024. }
  1025. if ($p_header['stored_filename'] == "") {
  1026. $p_header['status'] = "filtered";
  1027. }
  1028. if (strlen($p_header['stored_filename']) > 0xFF) {
  1029. $p_header['status'] = 'filename_too_long';
  1030. }
  1031. if ($p_header['status'] == 'ok') {
  1032. if (is_file($p_filename))
  1033. {
  1034. if (($v_file = @fopen($p_filename, "rb")) == 0) {
  1035. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
  1036. return PclZip::errorCode();
  1037. }
  1038. if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
  1039. $v_content_compressed = @fread($v_file, $p_header['size']);
  1040. $p_header['crc'] = @crc32($v_content_compressed);
  1041. $p_header['compressed_size'] = $p_header['size'];
  1042. $p_header['compression'] = 0;
  1043. }
  1044. else {
  1045. $v_content = @fread($v_file, $p_header['size']);
  1046. $p_header['crc'] = @crc32($v_content);
  1047. $v_content_compressed = @gzdeflate($v_content);
  1048. $p_header['compressed_size'] = strlen($v_content_compressed);
  1049. $p_header['compression'] = 8;
  1050. }
  1051. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  1052. @fclose($v_file);
  1053. return $v_result;
  1054. }
  1055. $v_binary_data = pack('a'.$p_header['compressed_size'],
  1056. $v_content_compressed);
  1057. @fwrite($this->zip_fd, $v_binary_data, $p_header['compressed_size']);
  1058. @fclose($v_file);
  1059. }
  1060. else {
  1061. if (@substr($p_header['stored_filename'], -1) != '/') {
  1062. $p_header['stored_filename'] .= '/';
  1063. }
  1064. $p_header['size'] = 0;
  1065. $p_header['external'] = 0x00000010;
  1066. if (($v_result = $this->privWriteFileHeader($p_header)) != 1)
  1067. {
  1068. return $v_result;
  1069. }
  1070. }
  1071. }
  1072. if (isset($p_options[PCLZIP_CB_POST_ADD])) {
  1073. $v_local_header = array();
  1074. $this->privConvertHeader2FileInfo($p_header, $v_local_header);
  1075. eval('$v_result = '.$p_options[PCLZIP_CB_POST_ADD].'(PCLZIP_CB_POST_ADD, $v_local_header);');
  1076. if ($v_result == 0) {
  1077. $v_result = 1;
  1078. }
  1079. }
  1080. return $v_result;
  1081. }
  1082. function privWriteFileHeader(&$p_header)
  1083. {
  1084. $v_result=1;
  1085. $p_header['offset'] = ftell($this->zip_fd);
  1086. $v_date = getdate($p_header['mtime']);
  1087. $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
  1088. $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
  1089. $v_binary_data = pack("VvvvvvVVVvv", 0x04034b50,
  1090. $p_header['version_extracted'], $p_header['flag'],
  1091. $p_header['compression'], $v_mtime, $v_mdate,
  1092. $p_header['crc'], $p_header['compressed_size'],
  1093. $p_header['size'],
  1094. strlen($p_header['stored_filename']),
  1095. $p_header['extra_len']);
  1096. fputs($this->zip_fd, $v_binary_data, 30);
  1097. if (strlen($p_header['stored_filename']) != 0)
  1098. {
  1099. fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
  1100. }
  1101. if ($p_header['extra_len'] != 0)
  1102. {
  1103. fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
  1104. }
  1105. return $v_result;
  1106. }
  1107. function privWriteCentralFileHeader(&$p_header)
  1108. {
  1109. $v_result=1;
  1110. $v_date = getdate($p_header['mtime']);
  1111. $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
  1112. $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
  1113. $v_binary_data = pack("VvvvvvvVVVvvvvvVV", 0x02014b50,
  1114. $p_header['version'], $p_header['version_extracted'],
  1115. $p_header['flag'], $p_header['compression'],
  1116. $v_mtime, $v_mdate, $p_header['crc'],
  1117. $p_header['compressed_size'], $p_header['size'],
  1118. strlen($p_header['stored_filename']),
  1119. $p_header['extra_len'], $p_header['comment_len'],
  1120. $p_header['disk'], $p_header['internal'],
  1121. $p_header['external'], $p_header['offset']);
  1122. fputs($this->zip_fd, $v_binary_data, 46);
  1123. if (strlen($p_header['stored_filename']) != 0)
  1124. {
  1125. fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
  1126. }
  1127. if ($p_header['extra_len'] != 0)
  1128. {
  1129. fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
  1130. }
  1131. if ($p_header['comment_len'] != 0)
  1132. {
  1133. fputs($this->zip_fd, $p_header['comment'], $p_header['comment_len']);
  1134. }
  1135. return $v_result;
  1136. }
  1137. function privWriteCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
  1138. {
  1139. $v_result=1;
  1140. $v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries,
  1141. $p_nb_entries, $p_size,
  1142. $p_offset, strlen($p_comment));
  1143. fputs($this->zip_fd, $v_binary_data, 22);
  1144. if (strlen($p_comment) != 0)
  1145. {
  1146. fputs($this->zip_fd, $p_comment, strlen($p_comment));
  1147. }
  1148. return $v_result;
  1149. }
  1150. function privList(&$p_list)
  1151. {
  1152. $v_result=1;
  1153. if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
  1154. {
  1155. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
  1156. return PclZip::errorCode();
  1157. }
  1158. $v_central_dir = array();
  1159. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  1160. {
  1161. return $v_result;
  1162. }
  1163. @rewind($this->zip_fd);
  1164. if (@fseek($this->zip_fd, $v_central_dir['offset']))
  1165. {
  1166. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  1167. return PclZip::errorCode();
  1168. }
  1169. for ($i=0; $i<$v_central_dir['entries']; $i++)
  1170. {
  1171. if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
  1172. {
  1173. return $v_result;
  1174. }
  1175. $v_header['index'] = $i;
  1176. $this->privConvertHeader2FileInfo($v_header, $p_list[$i]);
  1177. unset($v_header);
  1178. }
  1179. $this->privCloseFd();
  1180. return $v_result;
  1181. }
  1182. function privConvertHeader2FileInfo($p_header, &$p_info)
  1183. {
  1184. $v_result=1;
  1185. $p_info['filename'] = $p_header['filename'];
  1186. $p_info['stored_filename'] = $p_header['stored_filename'];
  1187. $p_info['size'] = $p_header['size'];
  1188. $p_info['compressed_size'] = $p_header['compressed_size'];
  1189. $p_info['mtime'] = $p_header['mtime'];
  1190. $p_info['comment'] = $p_header['comment'];
  1191. $p_info['folder'] = (($p_header['external']&0x00000010)==0x00000010);
  1192. $p_info['index'] = $p_header['index'];
  1193. $p_info['status'] = $p_header['status'];
  1194. return $v_result;
  1195. }
  1196. function privExtractByRule(&$p_file_list, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
  1197. {
  1198. $v_result=1;
  1199. if (($p_path == "") || ((substr($p_path, 0, 1) != "/") && (substr($p_path, 0, 3) != "../") && (substr($p_path,1,2)!=":/")))
  1200. $p_path = "./".$p_path;
  1201. if (($p_path != "./") && ($p_path != "/"))
  1202. {
  1203. while (substr($p_path, -1) == "/")
  1204. {
  1205. $p_path = substr($p_path, 0, strlen($p_path)-1);
  1206. }
  1207. }
  1208. if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/'))
  1209. {
  1210. $p_remove_path .= '/';
  1211. }
  1212. $p_remove_path_size = strlen($p_remove_path);
  1213. if (($v_result = $this->privOpenFd('rb')) != 1)
  1214. {
  1215. return $v_result;
  1216. }
  1217. $v_central_dir = array();
  1218. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  1219. {
  1220. $this->privCloseFd();
  1221. return $v_result;
  1222. }
  1223. $v_pos_entry = $v_central_dir['offset'];
  1224. $j_start = 0;
  1225. for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
  1226. {
  1227. @rewind($this->zip_fd);
  1228. if (@fseek($this->zip_fd, $v_pos_entry))
  1229. {
  1230. $this->privCloseFd();
  1231. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  1232. return PclZip::errorCode();
  1233. }
  1234. $v_header = array();
  1235. if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
  1236. {
  1237. $this->privCloseFd();
  1238. return $v_result;
  1239. }
  1240. $v_header['index'] = $i;
  1241. $v_pos_entry = ftell($this->zip_fd);
  1242. $v_extract = false;
  1243. if ( (isset($p_options[PCLZIP_OPT_BY_NAME]))
  1244. && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
  1245. for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_extract); $j++) {
  1246. if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
  1247. if ( (strlen($v_header['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
  1248. && (substr($v_header['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
  1249. $v_extract = true;
  1250. }
  1251. }
  1252. elseif ($v_header['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
  1253. $v_extract = true;
  1254. }
  1255. }
  1256. }
  1257. else if ( (isset($p_options[PCLZIP_OPT_BY_EREG]))
  1258. && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
  1259. if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header['stored_filename'])) {
  1260. $v_extract = true;
  1261. }
  1262. }
  1263. else if ( (isset($p_options[PCLZIP_OPT_BY_PREG]))
  1264. && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
  1265. if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header['stored_filename'])) {
  1266. $v_extract = true;
  1267. }
  1268. }
  1269. else if ( (isset($p_options[PCLZIP_OPT_BY_INDEX]))
  1270. && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
  1271. for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_extract); $j++) {
  1272. if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
  1273. $v_extract = true;
  1274. }
  1275. if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
  1276. $j_start = $j+1;
  1277. }
  1278. if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
  1279. break;
  1280. }
  1281. }
  1282. }
  1283. else {
  1284. $v_extract = true;
  1285. }
  1286. if ( ($v_extract)
  1287. && ( ($v_header['compression'] != 8)
  1288. && ($v_header['compression'] != 0))) {
  1289. $v_header['status'] = 'unsupported_compression';
  1290. if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  1291. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  1292. PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_COMPRESSION,
  1293. "Filename '".$v_header['stored_filename']."' is "
  1294. ."compressed by an unsupported compression "
  1295. ."method (".$v_header['compression'].") ");
  1296. return PclZip::errorCode();
  1297. }
  1298. }
  1299. if (($v_extract) && (($v_header['flag'] & 1) == 1)) {
  1300. $v_header['status'] = 'unsupported_encryption';
  1301. if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  1302. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  1303. PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION,
  1304. "Unsupported encryption for "
  1305. ." filename '".$v_header['stored_filename']
  1306. ."'");
  1307. return PclZip::errorCode();
  1308. }
  1309. }
  1310. if (($v_extract) && ($v_header['status'] != 'ok')) {
  1311. $v_result = $this->privConvertHeader2FileInfo($v_header,
  1312. $p_file_list[$v_nb_extracted++]);
  1313. if ($v_result != 1) {
  1314. $this->privCloseFd();
  1315. return $v_result;
  1316. }
  1317. $v_extract = false;
  1318. }
  1319. if ($v_extract)
  1320. {
  1321. @rewind($this->zip_fd);
  1322. if (@fseek($this->zip_fd, $v_header['offset']))
  1323. {
  1324. $this->privCloseFd();
  1325. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  1326. return PclZip::errorCode();
  1327. }
  1328. if ($p_options[PCLZIP_OPT_EXTRACT_AS_STRING]) {
  1329. $v_result1 = $this->privExtractFileAsString($v_header, $v_string);
  1330. if ($v_result1 < 1) {
  1331. $this->privCloseFd();
  1332. return $v_result1;
  1333. }
  1334. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1)
  1335. {
  1336. $this->privCloseFd();
  1337. return $v_result;
  1338. }
  1339. $p_file_list[$v_nb_extracted]['content'] = $v_string;
  1340. $v_nb_extracted++;
  1341. if ($v_result1 == 2) {
  1342. break;
  1343. }
  1344. }
  1345. elseif ( (isset($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT]))
  1346. && ($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])) {
  1347. $v_result1 = $this->privExtractFileInOutput($v_header, $p_options);
  1348. if ($v_result1 < 1) {
  1349. $this->privCloseFd();
  1350. return $v_result1;
  1351. }
  1352. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
  1353. $this->privCloseFd();
  1354. return $v_result;
  1355. }
  1356. if ($v_result1 == 2) {
  1357. break;
  1358. }
  1359. }
  1360. else {
  1361. $v_result1 = $this->privExtractFile($v_header,
  1362. $p_path, $p_remove_path,
  1363. $p_remove_all_path,
  1364. $p_options);
  1365. if ($v_result1 < 1) {
  1366. $this->privCloseFd();
  1367. return $v_result1;
  1368. }
  1369. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1)
  1370. {
  1371. $this->privCloseFd();
  1372. return $v_result;
  1373. }
  1374. if ($v_result1 == 2) {
  1375. break;
  1376. }
  1377. }
  1378. }
  1379. }
  1380. $this->privCloseFd();
  1381. return $v_result;
  1382. }
  1383. function privExtractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
  1384. {
  1385. $v_result=1;
  1386. if (($v_result = $this->privReadFileHeader($v_header)) != 1)
  1387. {
  1388. return $v_result;
  1389. }
  1390. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  1391. }
  1392. if ($p_remove_all_path == true) {
  1393. $p_entry['filename'] = basename($p_entry['filename']);
  1394. }
  1395. else if ($p_remove_path != "")
  1396. {
  1397. if (PclZipUtilPathInclusion($p_remove_path, $p_entry['filename']) == 2)
  1398. {
  1399. $p_entry['status'] = "filtered";
  1400. return $v_result;
  1401. }
  1402. $p_remove_path_size = strlen($p_remove_path);
  1403. if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path)
  1404. {
  1405. $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
  1406. }
  1407. }
  1408. if ($p_path != '')
  1409. {
  1410. $p_entry['filename'] = $p_path."/".$p_entry['filename'];
  1411. }
  1412. if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  1413. $v_local_header = array();
  1414. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  1415. eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
  1416. if ($v_result == 0) {
  1417. $p_entry['status'] = "skipped";
  1418. $v_result = 1;
  1419. }
  1420. if ($v_result == 2) {
  1421. $p_entry['status'] = "aborted";
  1422. $v_result = PCLZIP_ERR_USER_ABORTED;
  1423. }
  1424. $p_entry['filename'] = $v_local_header['filename'];
  1425. }
  1426. if ($p_entry['status'] == 'ok') {
  1427. if (file_exists($p_entry['filename']))
  1428. {
  1429. if (is_dir($p_entry['filename']))
  1430. {
  1431. $p_entry['status'] = "already_a_directory";
  1432. if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  1433. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  1434. PclZip::privErrorLog(PCLZIP_ERR_ALREADY_A_DIRECTORY,
  1435. "Filename '".$p_entry['filename']."' is "
  1436. ."already used by an existing directory");
  1437. return PclZip::errorCode();
  1438. }
  1439. }
  1440. else if (!is_writeable($p_entry['filename']))
  1441. {
  1442. $p_entry['status'] = "write_protected";
  1443. if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  1444. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  1445. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
  1446. "Filename '".$p_entry['filename']."' exists "
  1447. ."and is write protected");
  1448. return PclZip::errorCode();
  1449. }
  1450. }
  1451. else if (filemtime($p_entry['filename']) > $p_entry['mtime'])
  1452. {
  1453. if ( (isset($p_options[PCLZIP_OPT_REPLACE_NEWER]))
  1454. && ($p_options[PCLZIP_OPT_REPLACE_NEWER]===true)) {
  1455. }
  1456. else {
  1457. $p_entry['status'] = "newer_exist";
  1458. if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  1459. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  1460. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
  1461. "Newer version of '".$p_entry['filename']."' exists "
  1462. ."and option PCLZIP_OPT_REPLACE_NEWER is not selected");
  1463. return PclZip::errorCode();
  1464. }
  1465. }
  1466. }
  1467. else {
  1468. }
  1469. }
  1470. else {
  1471. if ((($p_entry['external']&0x00000010)==0x00000010) || (substr($p_entry['filename'], -1) == '/'))
  1472. $v_dir_to_check = $p_entry['filename'];
  1473. else if (!strstr($p_entry['filename'], "/"))
  1474. $v_dir_to_check = "";
  1475. else
  1476. $v_dir_to_check = dirname($p_entry['filename']);
  1477. if (($v_result = $this->privDirCheck($v_dir_to_check, (($p_entry['external']&0x00000010)==0x00000010))) != 1) {
  1478. $p_entry['status'] = "path_creation_fail";
  1479. $v_result = 1;
  1480. }
  1481. }
  1482. }
  1483. if ($p_entry['status'] == 'ok') {
  1484. if (!(($p_entry['external']&0x00000010)==0x00000010))
  1485. {
  1486. if ($p_entry['compression'] == 0) {
  1487. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0)
  1488. {
  1489. $p_entry['status'] = "write_error";
  1490. return $v_result;
  1491. }
  1492. $v_size = $p_entry['compressed_size'];
  1493. while ($v_size != 0)
  1494. {
  1495. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1496. $v_buffer = fread($this->zip_fd, $v_read_size);
  1497. $v_binary_data = pack('a'.$v_read_size, $v_buffer);
  1498. @fwrite($v_dest_file, $v_binary_data, $v_read_size);
  1499. $v_size -= $v_read_size;
  1500. }
  1501. fclose($v_dest_file);
  1502. touch($p_entry['filename'], $p_entry['mtime']);
  1503. }
  1504. else {
  1505. if (($p_entry['flag'] & 1) == 1) {
  1506. }
  1507. else {
  1508. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  1509. }
  1510. $v_file_content = @gzinflate($v_buffer);
  1511. unset($v_buffer);
  1512. if ($v_file_content === FALSE) {
  1513. $p_entry['status'] = "error";
  1514. return $v_result;
  1515. }
  1516. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  1517. $p_entry['status'] = "write_error";
  1518. return $v_result;
  1519. }
  1520. @fwrite($v_dest_file, $v_file_content, $p_entry['size']);
  1521. unset($v_file_content);
  1522. @fclose($v_dest_file);
  1523. @touch($p_entry['filename'], $p_entry['mtime']);
  1524. }
  1525. if (isset($p_options[PCLZIP_OPT_SET_CHMOD])) {
  1526. @chmod($p_entry['filename'], $p_options[PCLZIP_OPT_SET_CHMOD]);
  1527. }
  1528. }
  1529. }
  1530. if ($p_entry['status'] == "aborted") {
  1531. $p_entry['status'] = "skipped";
  1532. }
  1533. elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  1534. $v_local_header = array();
  1535. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  1536. eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
  1537. if ($v_result == 2) {
  1538. $v_result = PCLZIP_ERR_USER_ABORTED;
  1539. }
  1540. }
  1541. return $v_result;
  1542. }
  1543. function privExtractFileInOutput(&$p_entry, &$p_options)
  1544. {
  1545. $v_result=1;
  1546. if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
  1547. return $v_result;
  1548. }
  1549. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  1550. }
  1551. if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  1552. $v_local_header = array();
  1553. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  1554. eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
  1555. if ($v_result == 0) {
  1556. $p_entry['status'] = "skipped";
  1557. $v_result = 1;
  1558. }
  1559. if ($v_result == 2) {
  1560. $p_entry['status'] = "aborted";
  1561. $v_result = PCLZIP_ERR_USER_ABORTED;
  1562. }
  1563. $p_entry['filename'] = $v_local_header['filename'];
  1564. }
  1565. if ($p_entry['status'] == 'ok') {
  1566. if (!(($p_entry['external']&0x00000010)==0x00000010)) {
  1567. if ($p_entry['compressed_size'] == $p_entry['size']) {
  1568. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  1569. echo $v_buffer;
  1570. unset($v_buffer);
  1571. }
  1572. else {
  1573. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  1574. $v_file_content = gzinflate($v_buffer);
  1575. unset($v_buffer);
  1576. echo $v_file_content;
  1577. unset($v_file_content);
  1578. }
  1579. }
  1580. }
  1581. if ($p_entry['status'] == "aborted") {
  1582. $p_entry['status'] = "skipped";
  1583. }
  1584. elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  1585. $v_local_header = array();
  1586. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  1587. eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
  1588. if ($v_result == 2) {
  1589. $v_result = PCLZIP_ERR_USER_ABORTED;
  1590. }
  1591. }
  1592. return $v_result;
  1593. }
  1594. function privExtractFileAsString(&$p_entry, &$p_string)
  1595. {
  1596. $v_result=1;
  1597. $v_header = array();
  1598. if (($v_result = $this->privReadFileHeader($v_header)) != 1)
  1599. {
  1600. return $v_result;
  1601. }
  1602. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  1603. }
  1604. if (!(($p_entry['external']&0x00000010)==0x00000010))
  1605. {
  1606. if ($p_entry['compressed_size'] == $p_entry['size'])
  1607. {
  1608. $p_string = @fread($this->zip_fd, $p_entry['compressed_size']);
  1609. }
  1610. else
  1611. {
  1612. $v_data = @fread($this->zip_fd, $p_entry['compressed_size']);
  1613. if (($p_string = @gzinflate($v_data)) === FALSE) {
  1614. }
  1615. }
  1616. }
  1617. else {
  1618. }
  1619. return $v_result;
  1620. }
  1621. function privReadFileHeader(&$p_header)
  1622. {
  1623. $v_result=1;
  1624. $v_binary_data = @fread($this->zip_fd, 4);
  1625. $v_data = unpack('Vid', $v_binary_data);
  1626. if ($v_data['id'] != 0x04034b50)
  1627. {
  1628. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  1629. return PclZip::errorCode();
  1630. }
  1631. $v_binary_data = fread($this->zip_fd, 26);
  1632. if (strlen($v_binary_data) != 26)
  1633. {
  1634. $p_header['filename'] = "";
  1635. $p_header['status'] = "invalid_header";
  1636. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
  1637. return PclZip::errorCode();
  1638. }
  1639. $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
  1640. $p_header['filename'] = fread($this->zip_fd, $v_data['filename_len']);
  1641. if ($v_data['extra_len'] != 0) {
  1642. $p_header['extra'] = fread($this->zip_fd, $v_data['extra_len']);
  1643. }
  1644. else {
  1645. $p_header['extra'] = '';
  1646. }
  1647. $p_header['version_extracted'] = $v_data['version'];
  1648. $p_header['compression'] = $v_data['compression'];
  1649. $p_header['size'] = $v_data['size'];
  1650. $p_header['compressed_size'] = $v_data['compressed_size'];
  1651. $p_header['crc'] = $v_data['crc'];
  1652. $p_header['flag'] = $v_data['flag'];
  1653. $p_header['mdate'] = $v_data['mdate'];
  1654. $p_header['mtime'] = $v_data['mtime'];
  1655. if ($p_header['mdate'] && $p_header['mtime'])
  1656. {
  1657. $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
  1658. $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
  1659. $v_seconde = ($p_header['mtime'] & 0x001F)*2;
  1660. $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
  1661. $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
  1662. $v_day = $p_header['mdate'] & 0x001F;
  1663. $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  1664. }
  1665. else
  1666. {
  1667. $p_header['mtime'] = time();
  1668. }
  1669. $p_header['stored_filename'] = $p_header['filename'];
  1670. $p_header['status'] = "ok";
  1671. return $v_result;
  1672. }
  1673. function privReadCentralFileHeader(&$p_header)
  1674. {
  1675. $v_result=1;
  1676. $v_binary_data = @fread($this->zip_fd, 4);
  1677. $v_data = unpack('Vid', $v_binary_data);
  1678. if ($v_data['id'] != 0x02014b50)
  1679. {
  1680. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  1681. return PclZip::errorCode();
  1682. }
  1683. $v_binary_data = fread($this->zip_fd, 42);
  1684. if (strlen($v_binary_data) != 42)
  1685. {
  1686. $p_header['filename'] = "";
  1687. $p_header['status'] = "invalid_header";
  1688. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
  1689. return PclZip::errorCode();
  1690. }
  1691. $p_header = unpack('vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', $v_binary_data);
  1692. if ($p_header['filename_len'] != 0)
  1693. $p_header['filename'] = fread($this->zip_fd, $p_header['filename_len']);
  1694. else
  1695. $p_header['filename'] = '';
  1696. if ($p_header['extra_len'] != 0)
  1697. $p_header['extra'] = fread($this->zip_fd, $p_header['extra_len']);
  1698. else
  1699. $p_header['extra'] = '';
  1700. if ($p_header['comment_len'] != 0)
  1701. $p_header['comment'] = fread($this->zip_fd, $p_header['comment_len']);
  1702. else
  1703. $p_header['comment'] = '';
  1704. if ($p_header['mdate'] && $p_header['mtime'])
  1705. {
  1706. $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
  1707. $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
  1708. $v_seconde = ($p_header['mtime'] & 0x001F)*2;
  1709. $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
  1710. $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
  1711. $v_day = $p_header['mdate'] & 0x001F;
  1712. $p_header['mtime'] = mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  1713. }
  1714. else
  1715. {
  1716. $p_header['mtime'] = time();
  1717. }
  1718. $p_header['stored_filename'] = $p_header['filename'];
  1719. $p_header['status'] = 'ok';
  1720. if (substr($p_header['filename'], -1) == '/') {
  1721. $p_header['external'] = 0x00000010;
  1722. }
  1723. return $v_result;
  1724. }
  1725. function privCheckFileHeaders(&$p_local_header, &$p_central_header)
  1726. {
  1727. $v_result=1;
  1728. if ($p_local_header['filename'] != $p_central_header['filename']) {
  1729. }
  1730. if ($p_local_header['version_extracted'] != $p_central_header['version_extracted']) {
  1731. }
  1732. if ($p_local_header['flag'] != $p_central_header['flag']) {
  1733. }
  1734. if ($p_local_header['compression'] != $p_central_header['compression']) {
  1735. }
  1736. if ($p_local_header['mtime'] != $p_central_header['mtime']) {
  1737. }
  1738. if ($p_local_header['filename_len'] != $p_central_header['filename_len']) {
  1739. }
  1740. if (($p_local_header['flag'] & 8) == 8) {
  1741. $p_local_header['size'] = $p_central_header['size'];
  1742. $p_local_header['compressed_size'] = $p_central_header['compressed_size'];
  1743. $p_local_header['crc'] = $p_central_header['crc'];
  1744. }
  1745. return $v_result;
  1746. }
  1747. function privReadEndCentralDir(&$p_central_dir)
  1748. {
  1749. $v_result=1;
  1750. $v_size = filesize($this->zipname);
  1751. @fseek($this->zip_fd, $v_size);
  1752. if (@ftell($this->zip_fd) != $v_size)
  1753. {
  1754. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \''.$this->zipname.'\'');
  1755. return PclZip::errorCode();
  1756. }
  1757. $v_found = 0;
  1758. if ($v_size > 26) {
  1759. @fseek($this->zip_fd, $v_size-22);
  1760. if (($v_pos = @ftell($this->zip_fd)) != ($v_size-22))
  1761. {
  1762. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
  1763. return PclZip::errorCode();
  1764. }
  1765. $v_binary_data = @fread($this->zip_fd, 4);
  1766. $v_data = @unpack('Vid', $v_binary_data);
  1767. if ($v_data['id'] == 0x06054b50) {
  1768. $v_found = 1;
  1769. }
  1770. $v_pos = ftell($this->zip_fd);
  1771. }
  1772. if (!$v_found) {
  1773. $v_maximum_size = 65557;
  1774. if ($v_maximum_size > $v_size)
  1775. $v_maximum_size = $v_size;
  1776. @fseek($this->zip_fd, $v_size-$v_maximum_size);
  1777. if (@ftell($this->zip_fd) != ($v_size-$v_maximum_size))
  1778. {
  1779. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
  1780. return PclZip::errorCode();
  1781. }
  1782. $v_pos = ftell($this->zip_fd);
  1783. $v_bytes = 0x00000000;
  1784. while ($v_pos < $v_size)
  1785. {
  1786. $v_byte = @fread($this->zip_fd, 1);
  1787. $v_bytes = ($v_bytes << 8) | Ord($v_byte);
  1788. if ($v_bytes == 0x504b0506)
  1789. {
  1790. $v_pos++;
  1791. break;
  1792. }
  1793. $v_pos++;
  1794. }
  1795. if ($v_pos == $v_size)
  1796. {
  1797. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
  1798. return PclZip::errorCode();
  1799. }
  1800. }
  1801. $v_binary_data = fread($this->zip_fd, 18);
  1802. if (strlen($v_binary_data) != 18)
  1803. {
  1804. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
  1805. return PclZip::errorCode();
  1806. }
  1807. $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
  1808. if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
  1809. if (0) {
  1810. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT,
  1811. 'The central dir is not at the end of the archive.'
  1812. .' Some trailing bytes exists after the archive.');
  1813. return PclZip::errorCode();
  1814. }
  1815. }
  1816. if ($v_data['comment_size'] != 0)
  1817. $p_central_dir['comment'] = fread($this->zip_fd, $v_data['comment_size']);
  1818. else
  1819. $p_central_dir['comment'] = '';
  1820. $p_central_dir['entries'] = $v_data['entries'];
  1821. $p_central_dir['disk_entries'] = $v_data['disk_entries'];
  1822. $p_central_dir['offset'] = $v_data['offset'];
  1823. $p_central_dir['size'] = $v_data['size'];
  1824. $p_central_dir['disk'] = $v_data['disk'];
  1825. $p_central_dir['disk_start'] = $v_data['disk_start'];
  1826. return $v_result;
  1827. }
  1828. function privDeleteByRule(&$p_result_list, &$p_options)
  1829. {
  1830. $v_result=1;
  1831. $v_list_detail = array();
  1832. if (($v_result=$this->privOpenFd('rb')) != 1)
  1833. {
  1834. return $v_result;
  1835. }
  1836. $v_central_dir = array();
  1837. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  1838. {
  1839. $this->privCloseFd();
  1840. return $v_result;
  1841. }
  1842. @rewind($this->zip_fd);
  1843. $v_pos_entry = $v_central_dir['offset'];
  1844. @rewind($this->zip_fd);
  1845. if (@fseek($this->zip_fd, $v_pos_entry))
  1846. {
  1847. $this->privCloseFd();
  1848. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  1849. return PclZip::errorCode();
  1850. }
  1851. $v_header_list = array();
  1852. $j_start = 0;
  1853. for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
  1854. {
  1855. $v_header_list[$v_nb_extracted] = array();
  1856. if (($v_result = $this->privReadCentralFileHeader($v_header_list[$v_nb_extracted])) != 1)
  1857. {
  1858. $this->privCloseFd();
  1859. return $v_result;
  1860. }
  1861. $v_header_list[$v_nb_extracted]['index'] = $i;
  1862. $v_found = false;
  1863. if ( (isset($p_options[PCLZIP_OPT_BY_NAME]))
  1864. && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
  1865. for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_found); $j++) {
  1866. if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
  1867. if ( (strlen($v_header_list[$v_nb_extracted]['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
  1868. && (substr($v_header_list[$v_nb_extracted]['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
  1869. $v_found = true;
  1870. }
  1871. elseif ( (($v_header_list[$v_nb_extracted]['external']&0x00000010)==0x00000010)
  1872. && ($v_header_list[$v_nb_extracted]['stored_filename'].'/' == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
  1873. $v_found = true;
  1874. }
  1875. }
  1876. elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
  1877. $v_found = true;
  1878. }
  1879. }
  1880. }
  1881. else if ( (isset($p_options[PCLZIP_OPT_BY_EREG]))
  1882. && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
  1883. if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
  1884. $v_found = true;
  1885. }
  1886. }
  1887. else if ( (isset($p_options[PCLZIP_OPT_BY_PREG]))
  1888. && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
  1889. if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
  1890. $v_found = true;
  1891. }
  1892. }
  1893. else if ( (isset($p_options[PCLZIP_OPT_BY_INDEX]))
  1894. && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
  1895. for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_found); $j++) {
  1896. if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
  1897. $v_found = true;
  1898. }
  1899. if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
  1900. $j_start = $j+1;
  1901. }
  1902. if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
  1903. break;
  1904. }
  1905. }
  1906. }
  1907. if ($v_found)
  1908. {
  1909. unset($v_header_list[$v_nb_extracted]);
  1910. }
  1911. else
  1912. {
  1913. $v_nb_extracted++;
  1914. }
  1915. }
  1916. if ($v_nb_extracted > 0) {
  1917. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
  1918. $v_temp_zip = new PclZip($v_zip_temp_name);
  1919. if (($v_result = $v_temp_zip->privOpenFd('wb')) != 1) {
  1920. $this->privCloseFd();
  1921. return $v_result;
  1922. }
  1923. for ($i=0; $i<sizeof($v_header_list); $i++) {
  1924. @rewind($this->zip_fd);
  1925. if (@fseek($this->zip_fd, $v_header_list[$i]['offset'])) {
  1926. $this->privCloseFd();
  1927. $v_temp_zip->privCloseFd();
  1928. @unlink($v_zip_temp_name);
  1929. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  1930. return PclZip::errorCode();
  1931. }
  1932. $v_local_header = array();
  1933. if (($v_result = $this->privReadFileHeader($v_local_header)) != 1) {
  1934. $this->privCloseFd();
  1935. $v_temp_zip->privCloseFd();
  1936. @unlink($v_zip_temp_name);
  1937. return $v_result;
  1938. }
  1939. if ($this->privCheckFileHeaders($v_local_header,
  1940. $v_header_list[$i]) != 1) {
  1941. }
  1942. unset($v_local_header);
  1943. if (($v_result = $v_temp_zip->privWriteFileHeader($v_header_list[$i])) != 1) {
  1944. $this->privCloseFd();
  1945. $v_temp_zip->privCloseFd();
  1946. @unlink($v_zip_temp_name);
  1947. return $v_result;
  1948. }
  1949. if (($v_result = PclZipUtilCopyBlock($this->zip_fd, $v_temp_zip->zip_fd, $v_header_list[$i]['compressed_size'])) != 1) {
  1950. $this->privCloseFd();
  1951. $v_temp_zip->privCloseFd();
  1952. @unlink($v_zip_temp_name);
  1953. return $v_result;
  1954. }
  1955. }
  1956. $v_offset = @ftell($v_temp_zip->zip_fd);
  1957. for ($i=0; $i<sizeof($v_header_list); $i++) {
  1958. if (($v_result = $v_temp_zip->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  1959. $v_temp_zip->privCloseFd();
  1960. $this->privCloseFd();
  1961. @unlink($v_zip_temp_name);
  1962. return $v_result;
  1963. }
  1964. $v_temp_zip->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  1965. }
  1966. $v_comment = '';
  1967. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  1968. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  1969. }
  1970. $v_size = @ftell($v_temp_zip->zip_fd)-$v_offset;
  1971. if (($v_result = $v_temp_zip->privWriteCentralHeader(sizeof($v_header_list), $v_size, $v_offset, $v_comment)) != 1) {
  1972. unset($v_header_list);
  1973. $v_temp_zip->privCloseFd();
  1974. $this->privCloseFd();
  1975. @unlink($v_zip_temp_name);
  1976. return $v_result;
  1977. }
  1978. $v_temp_zip->privCloseFd();
  1979. $this->privCloseFd();
  1980. @unlink($this->zipname);
  1981. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  1982. unset($v_temp_zip);
  1983. }
  1984. else if ($v_central_dir['entries'] != 0) {
  1985. $this->privCloseFd();
  1986. if (($v_result = $this->privOpenFd('wb')) != 1) {
  1987. return $v_result;
  1988. }
  1989. if (($v_result = $this->privWriteCentralHeader(0, 0, 0, '')) != 1) {
  1990. return $v_result;
  1991. }
  1992. $this->privCloseFd();
  1993. }
  1994. return $v_result;
  1995. }
  1996. function privDirCheck($p_dir, $p_is_dir=false)
  1997. {
  1998. $v_result = 1;
  1999. if (($p_is_dir) && (substr($p_dir, -1)=='/'))
  2000. {
  2001. $p_dir = substr($p_dir, 0, strlen($p_dir)-1);
  2002. }
  2003. if ((is_dir($p_dir)) || ($p_dir == ""))
  2004. {
  2005. return 1;
  2006. }
  2007. $p_parent_dir = dirname($p_dir);
  2008. if ($p_parent_dir != $p_dir)
  2009. {
  2010. if ($p_parent_dir != "")
  2011. {
  2012. if (($v_result = $this->privDirCheck($p_parent_dir)) != 1)
  2013. {
  2014. return $v_result;
  2015. }
  2016. }
  2017. }
  2018. if (!@mkdir($p_dir, 0777))
  2019. {
  2020. PclZip::privErrorLog(PCLZIP_ERR_DIR_CREATE_FAIL, "Unable to create directory '$p_dir'");
  2021. return PclZip::errorCode();
  2022. }
  2023. return $v_result;
  2024. }
  2025. function privMerge(&$p_archive_to_add)
  2026. {
  2027. $v_result=1;
  2028. if (!is_file($p_archive_to_add->zipname))
  2029. {
  2030. $v_result = 1;
  2031. return $v_result;
  2032. }
  2033. if (!is_file($this->zipname))
  2034. {
  2035. $v_result = $this->privDuplicate($p_archive_to_add->zipname);
  2036. return $v_result;
  2037. }
  2038. if (($v_result=$this->privOpenFd('rb')) != 1)
  2039. {
  2040. return $v_result;
  2041. }
  2042. $v_central_dir = array();
  2043. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  2044. {
  2045. $this->privCloseFd();
  2046. return $v_result;
  2047. }
  2048. @rewind($this->zip_fd);
  2049. if (($v_result=$p_archive_to_add->privOpenFd('rb')) != 1)
  2050. {
  2051. $this->privCloseFd();
  2052. return $v_result;
  2053. }
  2054. $v_central_dir_to_add = array();
  2055. if (($v_result = $p_archive_to_add->privReadEndCentralDir($v_central_dir_to_add)) != 1)
  2056. {
  2057. $this->privCloseFd();
  2058. $p_archive_to_add->privCloseFd();
  2059. return $v_result;
  2060. }
  2061. @rewind($p_archive_to_add->zip_fd);
  2062. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
  2063. if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
  2064. {
  2065. $this->privCloseFd();
  2066. $p_archive_to_add->privCloseFd();
  2067. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
  2068. return PclZip::errorCode();
  2069. }
  2070. $v_size = $v_central_dir['offset'];
  2071. while ($v_size != 0)
  2072. {
  2073. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2074. $v_buffer = fread($this->zip_fd, $v_read_size);
  2075. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  2076. $v_size -= $v_read_size;
  2077. }
  2078. $v_size = $v_central_dir_to_add['offset'];
  2079. while ($v_size != 0)
  2080. {
  2081. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2082. $v_buffer = fread($p_archive_to_add->zip_fd, $v_read_size);
  2083. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  2084. $v_size -= $v_read_size;
  2085. }
  2086. $v_offset = @ftell($v_zip_temp_fd);
  2087. $v_size = $v_central_dir['size'];
  2088. while ($v_size != 0)
  2089. {
  2090. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2091. $v_buffer = @fread($this->zip_fd, $v_read_size);
  2092. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  2093. $v_size -= $v_read_size;
  2094. }
  2095. $v_size = $v_central_dir_to_add['size'];
  2096. while ($v_size != 0)
  2097. {
  2098. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2099. $v_buffer = @fread($p_archive_to_add->zip_fd, $v_read_size);
  2100. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  2101. $v_size -= $v_read_size;
  2102. }
  2103. $v_comment = $v_central_dir['comment'].' '.$v_central_dir_to_add['comment'];
  2104. $v_size = @ftell($v_zip_temp_fd)-$v_offset;
  2105. $v_swap = $this->zip_fd;
  2106. $this->zip_fd = $v_zip_temp_fd;
  2107. $v_zip_temp_fd = $v_swap;
  2108. if (($v_result = $this->privWriteCentralHeader($v_central_dir['entries']+$v_central_dir_to_add['entries'], $v_size, $v_offset, $v_comment)) != 1)
  2109. {
  2110. $this->privCloseFd();
  2111. $p_archive_to_add->privCloseFd();
  2112. @fclose($v_zip_temp_fd);
  2113. $this->zip_fd = null;
  2114. unset($v_header_list);
  2115. return $v_result;
  2116. }
  2117. $v_swap = $this->zip_fd;
  2118. $this->zip_fd = $v_zip_temp_fd;
  2119. $v_zip_temp_fd = $v_swap;
  2120. $this->privCloseFd();
  2121. $p_archive_to_add->privCloseFd();
  2122. @fclose($v_zip_temp_fd);
  2123. @unlink($this->zipname);
  2124. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  2125. return $v_result;
  2126. }
  2127. function privDuplicate($p_archive_filename)
  2128. {
  2129. $v_result=1;
  2130. if (!is_file($p_archive_filename))
  2131. {
  2132. $v_result = 1;
  2133. return $v_result;
  2134. }
  2135. if (($v_result=$this->privOpenFd('wb')) != 1)
  2136. {
  2137. return $v_result;
  2138. }
  2139. if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0)
  2140. {
  2141. $this->privCloseFd();
  2142. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive file \''.$p_archive_filename.'\' in binary write mode');
  2143. return PclZip::errorCode();
  2144. }
  2145. $v_size = filesize($p_archive_filename);
  2146. while ($v_size != 0)
  2147. {
  2148. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2149. $v_buffer = fread($v_zip_temp_fd, $v_read_size);
  2150. @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  2151. $v_size -= $v_read_size;
  2152. }
  2153. $this->privCloseFd();
  2154. @fclose($v_zip_temp_fd);
  2155. return $v_result;
  2156. }
  2157. function privErrorLog($p_error_code=0, $p_error_string='')
  2158. {
  2159. if (PCLZIP_ERROR_EXTERNAL == 1) {
  2160. PclError($p_error_code, $p_error_string);
  2161. }
  2162. else {
  2163. $this->error_code = $p_error_code;
  2164. $this->error_string = $p_error_string;
  2165. }
  2166. }
  2167. function privErrorReset()
  2168. {
  2169. if (PCLZIP_ERROR_EXTERNAL == 1) {
  2170. PclErrorReset();
  2171. }
  2172. else {
  2173. $this->error_code = 0;
  2174. $this->error_string = '';
  2175. }
  2176. }
  2177. function privDecrypt($p_encryption_header, &$p_buffer, $p_size, $p_crc)
  2178. {
  2179. $v_result=1;
  2180. $v_pwd = "test";
  2181. $p_buffer = PclZipUtilZipDecrypt($p_buffer, $p_size, $p_encryption_header,
  2182. $p_crc, $v_pwd);
  2183. return $v_result;
  2184. }
  2185. }
  2186. function PclZipUtilPathReduction($p_dir)
  2187. {
  2188. $v_result = "";
  2189. if ($p_dir != "")
  2190. {
  2191. $v_list = explode("/", $p_dir);
  2192. for ($i=sizeof($v_list)-1; $i>=0; $i--)
  2193. {
  2194. if ($v_list[$i] == ".")
  2195. {
  2196. }
  2197. else if ($v_list[$i] == "..")
  2198. {
  2199. $i--;
  2200. }
  2201. else if (($v_list[$i] == "") && ($i!=(sizeof($v_list)-1)) && ($i!=0))
  2202. {
  2203. }
  2204. else
  2205. {
  2206. $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?"/".$v_result:"");
  2207. }
  2208. }
  2209. }
  2210. return $v_result;
  2211. }
  2212. function PclZipUtilPathInclusion($p_dir, $p_path)
  2213. {
  2214. $v_result = 1;
  2215. $v_list_dir = explode("/", $p_dir);
  2216. $v_list_dir_size = sizeof($v_list_dir);
  2217. $v_list_path = explode("/", $p_path);
  2218. $v_list_path_size = sizeof($v_list_path);
  2219. $i = 0;
  2220. $j = 0;
  2221. while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
  2222. if ($v_list_dir[$i] == '') {
  2223. $i++;
  2224. continue;
  2225. }
  2226. if ($v_list_path[$j] == '') {
  2227. $j++;
  2228. continue;
  2229. }
  2230. if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ( $v_list_path[$j] != '')) {
  2231. $v_result = 0;
  2232. }
  2233. $i++;
  2234. $j++;
  2235. }
  2236. if ($v_result) {
  2237. while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) $j++;
  2238. while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) $i++;
  2239. if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
  2240. $v_result = 2;
  2241. }
  2242. else if ($i < $v_list_dir_size) {
  2243. $v_result = 0;
  2244. }
  2245. }
  2246. return $v_result;
  2247. }
  2248. function PclZipUtilCopyBlock($p_src, $p_dest, $p_size, $p_mode=0)
  2249. {
  2250. $v_result = 1;
  2251. if ($p_mode==0)
  2252. {
  2253. while ($p_size != 0)
  2254. {
  2255. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  2256. $v_buffer = @fread($p_src, $v_read_size);
  2257. @fwrite($p_dest, $v_buffer, $v_read_size);
  2258. $p_size -= $v_read_size;
  2259. }
  2260. }
  2261. else if ($p_mode==1)
  2262. {
  2263. while ($p_size != 0)
  2264. {
  2265. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  2266. $v_buffer = @gzread($p_src, $v_read_size);
  2267. @fwrite($p_dest, $v_buffer, $v_read_size);
  2268. $p_size -= $v_read_size;
  2269. }
  2270. }
  2271. else if ($p_mode==2)
  2272. {
  2273. while ($p_size != 0)
  2274. {
  2275. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  2276. $v_buffer = @fread($p_src, $v_read_size);
  2277. @gzwrite($p_dest, $v_buffer, $v_read_size);
  2278. $p_size -= $v_read_size;
  2279. }
  2280. }
  2281. else if ($p_mode==3)
  2282. {
  2283. while ($p_size != 0)
  2284. {
  2285. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  2286. $v_buffer = @gzread($p_src, $v_read_size);
  2287. @gzwrite($p_dest, $v_buffer, $v_read_size);
  2288. $p_size -= $v_read_size;
  2289. }
  2290. }
  2291. return $v_result;
  2292. }
  2293. function PclZipUtilRename($p_src, $p_dest)
  2294. {
  2295. $v_result = 1;
  2296. if (!@rename($p_src, $p_dest)) {
  2297. if (!@copy($p_src, $p_dest)) {
  2298. $v_result = 0;
  2299. }
  2300. else if (!@unlink($p_src)) {
  2301. $v_result = 0;
  2302. }
  2303. }
  2304. return $v_result;
  2305. }
  2306. function PclZipUtilOptionText($p_option)
  2307. {
  2308. $v_list = get_defined_constants();
  2309. for (reset($v_list); $v_key = key($v_list); next($v_list)) {
  2310. $v_prefix = substr($v_key, 0, 10);
  2311. if ((($v_prefix == 'PCLZIP_OPT') || ($v_prefix == 'PCLZIP_CB_'))
  2312. && ($v_list[$v_key] == $p_option)) {
  2313. return $v_key;
  2314. }
  2315. }
  2316. $v_result = 'Unknown';
  2317. return $v_result;
  2318. }
  2319. function PclZipUtilTranslateWinPath($p_path, $p_remove_disk_letter=true)
  2320. {
  2321. if (stristr(php_uname(), 'windows')) {
  2322. if (($p_remove_disk_letter) && (($v_position = strpos($p_path, ':')) != false)) {
  2323. $p_path = substr($p_path, $v_position+1);
  2324. }
  2325. if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0,1) == '\\')) {
  2326. $p_path = strtr($p_path, '\\', '/');
  2327. }
  2328. }
  2329. return $p_path;
  2330. }
  2331. ?>