PageRenderTime 70ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/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
  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_ERR_BAD_FORMAT, 'gzip temporary file \'' . $v_gzip_temp_name . '\' has invalid filesize - should be minimum 18 bytes');
  1331. return PclZip::errorCode();
  1332. }
  1333. if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0) {
  1334. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary read mode');
  1335. return PclZip::errorCode();
  1336. }
  1337. $v_binary_data = @fread($v_file_compressed, 10);
  1338. $v_data_header = unpack('a1id1/a1id2/a1cm/a1flag/Vmtime/a1xfl/a1os', $v_binary_data);
  1339. $v_data_header['os'] = bin2hex($v_data_header['os']);
  1340. @fseek($v_file_compressed, filesize($v_gzip_temp_name) - 8);
  1341. $v_binary_data = @fread($v_file_compressed, 8);
  1342. $v_data_footer = unpack('Vcrc/Vcompressed_size', $v_binary_data);
  1343. $p_header['compression'] = ord($v_data_header['cm']);
  1344. $p_header['crc'] = $v_data_footer['crc'];
  1345. $p_header['compressed_size'] = filesize($v_gzip_temp_name) - 18;
  1346. @fclose($v_file_compressed);
  1347. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  1348. return $v_result;
  1349. }
  1350. if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0) {
  1351. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary read mode');
  1352. return PclZip::errorCode();
  1353. }
  1354. fseek($v_file_compressed, 10);
  1355. $v_size = $p_header['compressed_size'];
  1356. while ($v_size != 0) {
  1357. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1358. $v_buffer = @fread($v_file_compressed, $v_read_size);
  1359. @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  1360. $v_size -= $v_read_size;
  1361. }
  1362. @fclose($v_file_compressed);
  1363. @unlink($v_gzip_temp_name);
  1364. return $v_result;
  1365. }
  1366. function privCalculateStoredFilename(&$p_filedescr, &$p_options) {
  1367. $v_result = 1;
  1368. $p_filename = $p_filedescr['filename'];
  1369. if (isset($p_options[PCLZIP_OPT_ADD_PATH])) {
  1370. $p_add_dir = $p_options[PCLZIP_OPT_ADD_PATH];
  1371. } else {
  1372. $p_add_dir = '';
  1373. }
  1374. if (isset($p_options[PCLZIP_OPT_REMOVE_PATH])) {
  1375. $p_remove_dir = $p_options[PCLZIP_OPT_REMOVE_PATH];
  1376. } else {
  1377. $p_remove_dir = '';
  1378. }
  1379. if (isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  1380. $p_remove_all_dir = $p_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  1381. } else {
  1382. $p_remove_all_dir = 0;
  1383. }
  1384. if (isset($p_filedescr['new_full_name'])) {
  1385. $v_stored_filename = PclZipUtilTranslateWinPath($p_filedescr['new_full_name']);
  1386. } else {
  1387. if (isset($p_filedescr['new_short_name'])) {
  1388. $v_path_info = pathinfo($p_filename);
  1389. $v_dir = '';
  1390. if ($v_path_info['dirname'] != '') {
  1391. $v_dir = $v_path_info['dirname'] . '/';
  1392. }
  1393. $v_stored_filename = $v_dir . $p_filedescr['new_short_name'];
  1394. } else {
  1395. $v_stored_filename = $p_filename;
  1396. }
  1397. if ($p_remove_all_dir) {
  1398. $v_stored_filename = basename($p_filename);
  1399. } else if ($p_remove_dir != "") {
  1400. if (substr($p_remove_dir, -1) != '/')
  1401. $p_remove_dir .= "/";
  1402. if ((substr($p_filename, 0, 2) == "./")
  1403. || (substr($p_remove_dir, 0, 2) == "./")
  1404. ) {
  1405. if ((substr($p_filename, 0, 2) == "./")
  1406. && (substr($p_remove_dir, 0, 2) != "./")
  1407. ) {
  1408. $p_remove_dir = "./" . $p_remove_dir;
  1409. }
  1410. if ((substr($p_filename, 0, 2) != "./")
  1411. && (substr($p_remove_dir, 0, 2) == "./")
  1412. ) {
  1413. $p_remove_dir = substr($p_remove_dir, 2);
  1414. }
  1415. }
  1416. $v_compare = PclZipUtilPathInclusion($p_remove_dir,
  1417. $v_stored_filename);
  1418. if ($v_compare > 0) {
  1419. if ($v_compare == 2) {
  1420. $v_stored_filename = "";
  1421. } else {
  1422. $v_stored_filename = substr($v_stored_filename,
  1423. strlen($p_remove_dir));
  1424. }
  1425. }
  1426. }
  1427. $v_stored_filename = PclZipUtilTranslateWinPath($v_stored_filename);
  1428. if ($p_add_dir != "") {
  1429. if (substr($p_add_dir, -1) == "/")
  1430. $v_stored_filename = $p_add_dir . $v_stored_filename;
  1431. else
  1432. $v_stored_filename = $p_add_dir . "/" . $v_stored_filename;
  1433. }
  1434. }
  1435. $v_stored_filename = PclZipUtilPathReduction($v_stored_filename);
  1436. $p_filedescr['stored_filename'] = $v_stored_filename;
  1437. return $v_result;
  1438. }
  1439. function privWriteFileHeader(&$p_header) {
  1440. $v_result = 1;
  1441. $p_header['offset'] = ftell($this->zip_fd);
  1442. $v_date = getdate($p_header['mtime']);
  1443. $v_mtime = ($v_date['hours'] << 11) + ($v_date['minutes'] << 5) + $v_date['seconds'] / 2;
  1444. $v_mdate = (($v_date['year'] - 1980) << 9) + ($v_date['mon'] << 5) + $v_date['mday'];
  1445. $v_binary_data = pack("VvvvvvVVVvv", 0x04034b50,
  1446. $p_header['version_extracted'], $p_header['flag'],
  1447. $p_header['compression'], $v_mtime, $v_mdate,
  1448. $p_header['crc'], $p_header['compressed_size'],
  1449. $p_header['size'],
  1450. strlen($p_header['stored_filename']),
  1451. $p_header['extra_len']);
  1452. fputs($this->zip_fd, $v_binary_data, 30);
  1453. if (strlen($p_header['stored_filename']) != 0) {
  1454. fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
  1455. }
  1456. if ($p_header['extra_len'] != 0) {
  1457. fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
  1458. }
  1459. return $v_result;
  1460. }
  1461. function privWriteCentralFileHeader(&$p_header) {
  1462. $v_result = 1;
  1463. $v_date = getdate($p_header['mtime']);
  1464. $v_mtime = ($v_date['hours'] << 11) + ($v_date['minutes'] << 5) + $v_date['seconds'] / 2;
  1465. $v_mdate = (($v_date['year'] - 1980) << 9) + ($v_date['mon'] << 5) + $v_date['mday'];
  1466. $v_binary_data = pack("VvvvvvvVVVvvvvvVV", 0x02014b50,
  1467. $p_header['version'], $p_header['version_extracted'],
  1468. $p_header['flag'], $p_header['compression'],
  1469. $v_mtime, $v_mdate, $p_header['crc'],
  1470. $p_header['compressed_size'], $p_header['size'],
  1471. strlen($p_header['stored_filename']),
  1472. $p_header['extra_len'], $p_header['comment_len'],
  1473. $p_header['disk'], $p_header['internal'],
  1474. $p_header['external'], $p_header['offset']);
  1475. fputs($this->zip_fd, $v_binary_data, 46);
  1476. if (strlen($p_header['stored_filename']) != 0) {
  1477. fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
  1478. }
  1479. if ($p_header['extra_len'] != 0) {
  1480. fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
  1481. }
  1482. if ($p_header['comment_len'] != 0) {
  1483. fputs($this->zip_fd, $p_header['comment'], $p_header['comment_len']);
  1484. }
  1485. return $v_result;
  1486. }
  1487. function privWriteCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment) {
  1488. $v_result = 1;
  1489. $v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries,
  1490. $p_nb_entries, $p_size,
  1491. $p_offset, strlen($p_comment));
  1492. fputs($this->zip_fd, $v_binary_data, 22);
  1493. if (strlen($p_comment) != 0) {
  1494. fputs($this->zip_fd, $p_comment, strlen($p_comment));
  1495. }
  1496. return $v_result;
  1497. }
  1498. function privList(&$p_list) {
  1499. $v_result = 1;
  1500. $this->privDisableMagicQuotes();
  1501. if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0) {
  1502. $this->privSwapBackMagicQuotes();
  1503. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \'' . $this->zipname . '\' in binary read mode');
  1504. return PclZip::errorCode();
  1505. }
  1506. $v_central_dir = array();
  1507. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  1508. $this->privSwapBackMagicQuotes();
  1509. return $v_result;
  1510. }
  1511. @rewind($this->zip_fd);
  1512. if (@fseek($this->zip_fd, $v_central_dir['offset'])) {
  1513. $this->privSwapBackMagicQuotes();
  1514. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  1515. return PclZip::errorCode();
  1516. }
  1517. for ($i = 0; $i < $v_central_dir['entries']; $i++) {
  1518. if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1) {
  1519. $this->privSwapBackMagicQuotes();
  1520. return $v_result;
  1521. }
  1522. $v_header['index'] = $i;
  1523. $this->privConvertHeader2FileInfo($v_header, $p_list[$i]);
  1524. unset($v_header);
  1525. }
  1526. $this->privCloseFd();
  1527. $this->privSwapBackMagicQuotes();
  1528. return $v_result;
  1529. }
  1530. function privConvertHeader2FileInfo($p_header, &$p_info) {
  1531. $v_result = 1;
  1532. $v_temp_path = PclZipUtilPathReduction($p_header['filename']);
  1533. $p_info['filename'] = $v_temp_path;
  1534. $v_temp_path = PclZipUtilPathReduction($p_header['stored_filename']);
  1535. $p_info['stored_filename'] = $v_temp_path;
  1536. $p_info['size'] = $p_header['size'];
  1537. $p_info['compressed_size'] = $p_header['compressed_size'];
  1538. $p_info['mtime'] = $p_header['mtime'];
  1539. $p_info['comment'] = $p_header['comment'];
  1540. $p_info['folder'] = (($p_header['external'] & 0x00000010) == 0x00000010);
  1541. $p_info['index'] = $p_header['index'];
  1542. $p_info['status'] = $p_header['status'];
  1543. $p_info['crc'] = $p_header['crc'];
  1544. return $v_result;
  1545. }
  1546. function privExtractByRule(&$p_file_list, $p_path, $p_remove_path, $p_remove_all_path, &$p_options) {
  1547. $v_result = 1;
  1548. $this->privDisableMagicQuotes();
  1549. if (($p_path == "")
  1550. || ((substr($p_path, 0, 1) != "/")
  1551. && (substr($p_path, 0, 3) != "../")
  1552. && (substr($p_path, 1, 2) != ":/"))
  1553. )
  1554. $p_path = "./" . $p_path;
  1555. if (($p_path != "./") && ($p_path != "/")) {
  1556. while (substr($p_path, -1) == "/") {
  1557. $p_path = substr($p_path, 0, strlen($p_path) - 1);
  1558. }
  1559. }
  1560. if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/')) {
  1561. $p_remove_path .= '/';
  1562. }
  1563. $p_remove_path_size = strlen($p_remove_path);
  1564. if (($v_result = $this->privOpenFd('rb')) != 1) {
  1565. $this->privSwapBackMagicQuotes();
  1566. return $v_result;
  1567. }
  1568. $v_central_dir = array();
  1569. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  1570. $this->privCloseFd();
  1571. $this->privSwapBackMagicQuotes();
  1572. return $v_result;
  1573. }
  1574. $v_pos_entry = $v_central_dir['offset'];
  1575. $j_start = 0;
  1576. for ($i = 0, $v_nb_extracted = 0; $i < $v_central_dir['entries']; $i++) {
  1577. @rewind($this->zip_fd);
  1578. if (@fseek($this->zip_fd, $v_pos_entry)) {
  1579. $this->privCloseFd();
  1580. $this->privSwapBackMagicQuotes();
  1581. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  1582. return PclZip::errorCode();
  1583. }
  1584. $v_header = array();
  1585. if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1) {
  1586. $this->privCloseFd();
  1587. $this->privSwapBackMagicQuotes();
  1588. return $v_result;
  1589. }
  1590. $v_header['index'] = $i;
  1591. $v_pos_entry = ftell($this->zip_fd);
  1592. $v_extract = false;
  1593. if ((isset($p_options[PCLZIP_OPT_BY_NAME]))
  1594. && ($p_options[PCLZIP_OPT_BY_NAME] != 0)
  1595. ) {
  1596. for ($j = 0; ($j < sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_extract); $j++) {
  1597. if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
  1598. if ((strlen($v_header['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
  1599. && (substr($v_header['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])
  1600. ) {
  1601. $v_extract = true;
  1602. }
  1603. } elseif ($v_header['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
  1604. $v_extract = true;
  1605. }
  1606. }
  1607. } else if ((isset($p_options[PCLZIP_OPT_BY_PREG]))
  1608. && ($p_options[PCLZIP_OPT_BY_PREG] != "")
  1609. ) {
  1610. if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header['stored_filename'])) {
  1611. $v_extract = true;
  1612. }
  1613. } else if ((isset($p_options[PCLZIP_OPT_BY_INDEX]))
  1614. && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)
  1615. ) {
  1616. for ($j = $j_start; ($j < sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_extract); $j++) {
  1617. if (($i >= $p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i <= $p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
  1618. $v_extract = true;
  1619. }
  1620. if ($i >= $p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
  1621. $j_start = $j + 1;
  1622. }
  1623. if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start'] > $i) {
  1624. break;
  1625. }
  1626. }
  1627. } else {
  1628. $v_extract = true;
  1629. }
  1630. if (($v_extract)
  1631. && (($v_header['compression'] != 8)
  1632. && ($v_header['compression'] != 0))
  1633. ) {
  1634. $v_header['status'] = 'unsupported_compression';
  1635. if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  1636. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)
  1637. ) {
  1638. $this->privSwapBackMagicQuotes();
  1639. PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_COMPRESSION,
  1640. "Filename '" . $v_header['stored_filename'] . "' is "
  1641. . "compressed by an unsupported compression "
  1642. . "method (" . $v_header['compression'] . ") ");
  1643. return PclZip::errorCode();
  1644. }
  1645. }
  1646. if (($v_extract) && (($v_header['flag'] & 1) == 1)) {
  1647. $v_header['status'] = 'unsupported_encryption';
  1648. if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  1649. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)
  1650. ) {
  1651. $this->privSwapBackMagicQuotes();
  1652. PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION,
  1653. "Unsupported encryption for "
  1654. . " filename '" . $v_header['stored_filename']
  1655. . "'");
  1656. return PclZip::errorCode();
  1657. }
  1658. }
  1659. if (($v_extract) && ($v_header['status'] != 'ok')) {
  1660. $v_result = $this->privConvertHeader2FileInfo($v_header,
  1661. $p_file_list[$v_nb_extracted++]);
  1662. if ($v_result != 1) {
  1663. $this->privCloseFd();
  1664. $this->privSwapBackMagicQuotes();
  1665. return $v_result;
  1666. }
  1667. $v_extract = false;
  1668. }
  1669. if ($v_extract) {
  1670. @rewind($this->zip_fd);
  1671. if (@fseek($this->zip_fd, $v_header['offset'])) {
  1672. $this->privCloseFd();
  1673. $this->privSwapBackMagicQuotes();
  1674. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  1675. return PclZip::errorCode();
  1676. }
  1677. if ($p_options[PCLZIP_OPT_EXTRACT_AS_STRING]) {
  1678. $v_string = '';
  1679. $v_result1 = $this->privExtractFileAsString($v_header, $v_string, $p_options);
  1680. if ($v_result1 < 1) {
  1681. $this->privCloseFd();
  1682. $this->privSwapBackMagicQuotes();
  1683. return $v_result1;
  1684. }
  1685. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1) {
  1686. $this->privCloseFd();
  1687. $this->privSwapBackMagicQuotes();
  1688. return $v_result;
  1689. }
  1690. $p_file_list[$v_nb_extracted]['content'] = $v_string;
  1691. $v_nb_extracted++;
  1692. if ($v_result1 == 2) {
  1693. break;
  1694. }
  1695. } elseif ((isset($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT]))
  1696. && ($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])
  1697. ) {
  1698. $v_result1 = $this->privExtractFileInOutput($v_header, $p_options);
  1699. if ($v_result1 < 1) {
  1700. $this->privCloseFd();
  1701. $this->privSwapBackMagicQuotes();
  1702. return $v_result1;
  1703. }
  1704. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
  1705. $this->privCloseFd();
  1706. $this->privSwapBackMagicQuotes();
  1707. return $v_result;
  1708. }
  1709. if ($v_result1 == 2) {
  1710. break;
  1711. }
  1712. } else {
  1713. $v_result1 = $this->privExtractFile($v_header,
  1714. $p_path, $p_remove_path,
  1715. $p_remove_all_path,
  1716. $p_options);
  1717. if ($v_result1 < 1) {
  1718. $this->privCloseFd();
  1719. $this->privSwapBackMagicQuotes();
  1720. return $v_result1;
  1721. }
  1722. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
  1723. $this->privCloseFd();
  1724. $this->privSwapBackMagicQuotes();
  1725. return $v_result;
  1726. }
  1727. if ($v_result1 == 2) {
  1728. break;
  1729. }
  1730. }
  1731. }
  1732. }
  1733. $this->privCloseFd();
  1734. $this->privSwapBackMagicQuotes();
  1735. return $v_result;
  1736. }
  1737. function privExtractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_options) {
  1738. $v_result = 1;
  1739. if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
  1740. return $v_result;
  1741. }
  1742. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  1743. }
  1744. if ($p_remove_all_path == true) {
  1745. if (($p_entry['external'] & 0x00000010) == 0x00000010) {
  1746. $p_entry['status'] = "filtered";
  1747. return $v_result;
  1748. }
  1749. $p_entry['filename'] = basename($p_entry['filename']);
  1750. } else if ($p_remove_path != "") {
  1751. if (PclZipUtilPathInclusion($p_remove_path, $p_entry['filename']) == 2) {
  1752. $p_entry['status'] = "filtered";
  1753. return $v_result;
  1754. }
  1755. $p_remove_path_size = strlen($p_remove_path);
  1756. if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path) {
  1757. $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
  1758. }
  1759. }
  1760. if ($p_path != '') {
  1761. $p_entry['filename'] = $p_path . "/" . $p_entry['filename'];
  1762. }
  1763. if (isset($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION])) {
  1764. $v_inclusion
  1765. = PclZipUtilPathInclusion($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION],
  1766. $p_entry['filename']);
  1767. if ($v_inclusion == 0) {
  1768. PclZip::privErrorLog(PCLZIP_ERR_DIRECTORY_RESTRICTION,
  1769. "Filename '" . $p_entry['filename'] . "' is "
  1770. . "outside PCLZIP_OPT_EXTRACT_DIR_RESTRICTION");
  1771. return PclZip::errorCode();
  1772. }
  1773. }
  1774. if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  1775. $v_local_header = array();
  1776. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  1777. $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  1778. if ($v_result == 0) {
  1779. $p_entry['status'] = "skipped";
  1780. $v_result = 1;
  1781. }
  1782. if ($v_result == 2) {
  1783. $p_entry['status'] = "aborted";
  1784. $v_result = PCLZIP_ERR_USER_ABORTED;
  1785. }
  1786. $p_entry['filename'] = $v_local_header['filename'];
  1787. }
  1788. if ($p_entry['status'] == 'ok') {
  1789. if (file_exists($p_entry['filename'])) {
  1790. if (is_dir($p_entry['filename'])) {
  1791. $p_entry['status'] = "already_a_directory";
  1792. if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  1793. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)
  1794. ) {
  1795. PclZip::privErrorLog(PCLZIP_ERR_ALREADY_A_DIRECTORY,
  1796. "Filename '" . $p_entry['filename'] . "' is "
  1797. . "already used by an existing directory");
  1798. return PclZip::errorCode();
  1799. }
  1800. } else if (!is_writeable($p_entry['filename'])) {
  1801. $p_entry['status'] = "write_protected";
  1802. if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  1803. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)
  1804. ) {
  1805. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
  1806. "Filename '" . $p_entry['filename'] . "' exists "
  1807. . "and is write protected");
  1808. return PclZip::errorCode();
  1809. }
  1810. } else if (filemtime($p_entry['filename']) > $p_entry['mtime']) {
  1811. if ((isset($p_options[PCLZIP_OPT_REPLACE_NEWER]))
  1812. && ($p_options[PCLZIP_OPT_REPLACE_NEWER] === true)
  1813. ) {
  1814. } else {
  1815. $p_entry['status'] = "newer_exist";
  1816. if ((isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  1817. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR] === true)
  1818. ) {
  1819. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
  1820. "Newer version of '" . $p_entry['filename'] . "' exists "
  1821. . "and option PCLZIP_OPT_REPLACE_NEWER is not selected");
  1822. return PclZip::errorCode();
  1823. }
  1824. }
  1825. } else {
  1826. }
  1827. } else {
  1828. if ((($p_entry['external'] & 0x00000010) == 0x00000010) || (substr($p_entry['filename'], -1) == '/'))
  1829. $v_dir_to_check = $p_entry['filename'];
  1830. else if (!strstr($p_entry['filename'], "/"))
  1831. $v_dir_to_check = "";
  1832. else
  1833. $v_dir_to_check = dirname($p_entry['filename']);
  1834. if (($v_result = $this->privDirCheck($v_dir_to_check, (($p_entry['external'] & 0x00000010) == 0x00000010))) != 1) {
  1835. $p_entry['status'] = "path_creation_fail";
  1836. $v_result = 1;
  1837. }
  1838. }
  1839. }
  1840. if ($p_entry['status'] == 'ok') {
  1841. if (!(($p_entry['external'] & 0x00000010) == 0x00000010)) {
  1842. if ($p_entry['compression'] == 0) {
  1843. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  1844. $p_entry['status'] = "write_error";
  1845. return $v_result;
  1846. }
  1847. $v_size = $p_entry['compressed_size'];
  1848. while ($v_size != 0) {
  1849. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1850. $v_buffer = @fread($this->zip_fd, $v_read_size);
  1851. @fwrite($v_dest_file, $v_buffer, $v_read_size);
  1852. $v_size -= $v_read_size;
  1853. }
  1854. fclose($v_dest_file);
  1855. touch($p_entry['filename'], $p_entry['mtime']);
  1856. } else {
  1857. if (($p_entry['flag'] & 1) == 1) {
  1858. PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION, 'File \'' . $p_entry['filename'] . '\' is encrypted. Encrypted files are not supported.');
  1859. return PclZip::errorCode();
  1860. }
  1861. if ((!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF]))
  1862. && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON])
  1863. || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
  1864. && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_entry['size'])))
  1865. ) {
  1866. $v_result = $this->privExtractFileUsingTempFile($p_entry, $p_options);
  1867. if ($v_result < PCLZIP_ERR_NO_ERROR) {
  1868. return $v_result;
  1869. }
  1870. } else {
  1871. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  1872. $v_file_content = @gzinflate($v_buffer);
  1873. unset($v_buffer);
  1874. if ($v_file_content === FALSE) {
  1875. $p_entry['status'] = "error";
  1876. return $v_result;
  1877. }
  1878. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  1879. $p_entry['status'] = "write_error";
  1880. return $v_result;
  1881. }
  1882. @fwrite($v_dest_file, $v_file_content, $p_entry['size']);
  1883. unset($v_file_content);
  1884. @fclose($v_dest_file);
  1885. }
  1886. @touch($p_entry['filename'], $p_entry['mtime']);
  1887. }
  1888. if (isset($p_options[PCLZIP_OPT_SET_CHMOD])) {
  1889. @chmod($p_entry['filename'], $p_options[PCLZIP_OPT_SET_CHMOD]);
  1890. }
  1891. }
  1892. }
  1893. if ($p_entry['status'] == "aborted") {
  1894. $p_entry['status'] = "skipped";
  1895. } elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  1896. $v_local_header = array();
  1897. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  1898. $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
  1899. if ($v_result == 2) {
  1900. $v_result = PCLZIP_ERR_USER_ABORTED;
  1901. }
  1902. }
  1903. return $v_result;
  1904. }
  1905. function privExtractFileUsingTempFile(&$p_entry, &$p_options) {
  1906. $v_result = 1;
  1907. $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.gz';
  1908. if (($v_dest_file = @fopen($v_gzip_temp_name, "wb")) == 0) {
  1909. fclose($v_file);
  1910. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary write mode');
  1911. return PclZip::errorCode();
  1912. }
  1913. $v_binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($p_entry['compression']), Chr(0x00), time(), Chr(0x00), Chr(3));
  1914. @fwrite($v_dest_file, $v_binary_data, 10);
  1915. $v_size = $p_entry['compressed_size'];
  1916. while ($v_size != 0) {
  1917. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1918. $v_buffer = @fread($this->zip_fd, $v_read_size);
  1919. @fwrite($v_dest_file, $v_buffer, $v_read_size);
  1920. $v_size -= $v_read_size;
  1921. }
  1922. $v_binary_data = pack('VV', $p_entry['crc'], $p_entry['size']);
  1923. @fwrite($v_dest_file, $v_binary_data, 8);
  1924. @fclose($v_dest_file);
  1925. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  1926. $p_entry['status'] = "write_error";
  1927. return $v_result;
  1928. }
  1929. if (($v_src_file = @gzopen($v_gzip_temp_name, 'rb')) == 0) {
  1930. @fclose($v_dest_file);
  1931. $p_entry['status'] = "read_error";
  1932. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary read mode');
  1933. return PclZip::errorCode();
  1934. }
  1935. $v_size = $p_entry['size'];
  1936. while ($v_size != 0) {
  1937. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1938. $v_buffer = @gzread($v_src_file, $v_read_size);
  1939. @fwrite($v_dest_file, $v_buffer, $v_read_size);
  1940. $v_size -= $v_read_size;
  1941. }
  1942. @fclose($v_dest_file);
  1943. @gzclose($v_src_file);
  1944. @unlink($v_gzip_temp_name);
  1945. return $v_result;
  1946. }
  1947. function privExtractFileInOutput(&$p_entry, &$p_options) {
  1948. $v_result = 1;
  1949. if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
  1950. return $v_result;
  1951. }
  1952. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  1953. }
  1954. if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  1955. $v_local_header = array();
  1956. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  1957. $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  1958. if ($v_result == 0) {
  1959. $p_entry['status'] = "skipped";
  1960. $v_result = 1;
  1961. }
  1962. if ($v_result == 2) {
  1963. $p_entry['status'] = "aborted";
  1964. $v_result = PCLZIP_ERR_USER_ABORTED;
  1965. }
  1966. $p_entry['filename'] = $v_local_header['filename'];
  1967. }
  1968. if ($p_entry['status'] == 'ok') {
  1969. if (!(($p_entry['external'] & 0x00000010) == 0x00000010)) {
  1970. if ($p_entry['compressed_size'] == $p_entry['size']) {
  1971. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  1972. echo $v_buffer;
  1973. unset($v_buffer);
  1974. } else {
  1975. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  1976. $v_file_content = gzinflate($v_buffer);
  1977. unset($v_buffer);
  1978. echo $v_file_content;
  1979. unset($v_file_content);
  1980. }
  1981. }
  1982. }
  1983. if ($p_entry['status'] == "aborted") {
  1984. $p_entry['status'] = "skipped";
  1985. } elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  1986. $v_local_header = array();
  1987. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  1988. $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
  1989. if ($v_result == 2) {
  1990. $v_result = PCLZIP_ERR_USER_ABORTED;
  1991. }
  1992. }
  1993. return $v_result;
  1994. }
  1995. function privExtractFileAsString(&$p_entry, &$p_string, &$p_options) {
  1996. $v_result = 1;
  1997. $v_header = array();
  1998. if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
  1999. return $v_result;
  2000. }
  2001. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  2002. }
  2003. if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  2004. $v_local_header = array();
  2005. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  2006. $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  2007. if ($v_result == 0) {
  2008. $p_entry['status'] = "skipped";
  2009. $v_result = 1;
  2010. }
  2011. if ($v_result == 2) {
  2012. $p_entry['status'] = "aborted";
  2013. $v_result = PCLZIP_ERR_USER_ABORTED;
  2014. }
  2015. $p_entry['filename'] = $v_local_header['filename'];
  2016. }
  2017. if ($p_entry['status'] == 'ok') {
  2018. if (!(($p_entry['external'] & 0x00000010) == 0x00000010)) {
  2019. if ($p_entry['compression'] == 0) {
  2020. $p_string = @fread($this->zip_fd, $p_entry['compressed_size']);
  2021. } else {
  2022. $v_data = @fread($this->zip_fd, $p_entry['compressed_size']);
  2023. if (($p_string = @gzinflate($v_data)) === FALSE) {
  2024. }
  2025. }
  2026. } else {
  2027. }
  2028. }
  2029. if ($p_entry['status'] == "aborted") {
  2030. $p_entry['status'] = "skipped";
  2031. } elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  2032. $v_local_header = array();
  2033. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  2034. $v_local_header['content'] = $p_string;
  2035. $p_string = '';
  2036. $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
  2037. $p_string = $v_local_header['content'];
  2038. unset($v_local_header['content']);
  2039. if ($v_result == 2) {
  2040. $v_result = PCLZIP_ERR_USER_ABORTED;
  2041. }
  2042. }
  2043. return $v_result;
  2044. }
  2045. function privReadFileHeader(&$p_header) {
  2046. $v_result = 1;
  2047. $v_binary_data = @fread($this->zip_fd, 4);
  2048. $v_data = unpack('Vid', $v_binary_data);
  2049. if ($v_data['id'] != 0x04034b50) {
  2050. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  2051. return PclZip::errorCode();
  2052. }
  2053. $v_binary_data = fread($this->zip_fd, 26);
  2054. if (strlen($v_binary_data) != 26) {
  2055. $p_header['filename'] = "";
  2056. $p_header['status'] = "invalid_header";
  2057. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : " . strlen($v_binary_data));
  2058. return PclZip::errorCode();
  2059. }
  2060. $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
  2061. $p_header['filename'] = fread($this->zip_fd, $v_data['filename_len']);
  2062. if ($v_data['extra_len'] != 0) {
  2063. $p_header['extra'] = fread($this->zip_fd, $v_data['extra_len']);
  2064. } else {
  2065. $p_header['extra'] = '';
  2066. }
  2067. $p_header['version_extracted'] = $v_data['version'];
  2068. $p_header['compression'] = $v_data['compression'];
  2069. $p_header['size'] = $v_data['size'];
  2070. $p_header['compressed_size'] = $v_data['compressed_size'];
  2071. $p_header['crc'] = $v_data['crc'];
  2072. $p_header['flag'] = $v_data['flag'];
  2073. $p_header['filename_len'] = $v_data['filename_len'];
  2074. $p_header['mdate'] = $v_data['mdate'];
  2075. $p_header['mtime'] = $v_data['mtime'];
  2076. if ($p_header['mdate'] && $p_header['mtime']) {
  2077. $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
  2078. $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
  2079. $v_seconde = ($p_header['mtime'] & 0x001F) * 2;
  2080. $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
  2081. $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
  2082. $v_day = $p_header['mdate'] & 0x001F;
  2083. $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  2084. } else {
  2085. $p_header['mtime'] = time();
  2086. }
  2087. $p_header['stored_filename'] = $p_header['filename'];
  2088. $p_header['status'] = "ok";
  2089. return $v_result;
  2090. }
  2091. function privReadCentralFileHeader(&$p_header) {
  2092. $v_result = 1;
  2093. $v_binary_data = @fread($this->zip_fd, 4);
  2094. $v_data = unpack('Vid', $v_binary_data);
  2095. if ($v_data['id'] != 0x02014b50) {
  2096. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  2097. return PclZip::errorCode();
  2098. }
  2099. $v_binary_data = fread($this->zip_fd, 42);
  2100. if (strlen($v_binary_data) != 42) {
  2101. $p_header['filename'] = "";
  2102. $p_header['status'] = "invalid_header";
  2103. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : " . strlen($v_binary_data));
  2104. return PclZip::errorCode();
  2105. }
  2106. $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);
  2107. if ($p_header['filename_len'] != 0)
  2108. $p_header['filename'] = fread($this->zip_fd, $p_header['filename_len']);
  2109. else
  2110. $p_header['filename'] = '';
  2111. if ($p_header['extra_len'] != 0)
  2112. $p_header['extra'] = fread($this->zip_fd, $p_header['extra_len']);
  2113. else
  2114. $p_header['extra'] = '';
  2115. if ($p_header['comment_len'] != 0)
  2116. $p_header['comment'] = fread($this->zip_fd, $p_header['comment_len']);
  2117. else
  2118. $p_header['comment'] = '';
  2119. if (1) {
  2120. $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
  2121. $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
  2122. $v_seconde = ($p_header['mtime'] & 0x001F) * 2;
  2123. $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
  2124. $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
  2125. $v_day = $p_header['mdate'] & 0x001F;
  2126. $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  2127. } else {
  2128. $p_header['mtime'] = time();
  2129. }
  2130. $p_header['stored_filename'] = $p_header['filename'];
  2131. $p_header['status'] = 'ok';
  2132. if (substr($p_header['filename'], -1) == '/') {
  2133. $p_header['external'] = 0x00000010;
  2134. }
  2135. return $v_result;
  2136. }
  2137. function privCheckFileHeaders(&$p_local_header, &$p_central_header) {
  2138. $v_result = 1;
  2139. if ($p_local_header['filename'] != $p_central_header['filename']) {
  2140. }
  2141. if ($p_local_header['version_extracted'] != $p_central_header['version_extracted']) {
  2142. }
  2143. if ($p_local_header['flag'] != $p_central_header['flag']) {
  2144. }
  2145. if ($p_local_header['compression'] != $p_central_header['compression']) {
  2146. }
  2147. if ($p_local_header['mtime'] != $p_central_header['mtime']) {
  2148. }
  2149. if ($p_local_header['filename_len'] != $p_central_header['filename_len']) {
  2150. }
  2151. if (($p_local_header['flag'] & 8) == 8) {
  2152. $p_local_header['size'] = $p_central_header['size'];
  2153. $p_local_header['compressed_size'] = $p_central_header['compressed_size'];
  2154. $p_local_header['crc'] = $p_central_header['crc'];
  2155. }
  2156. return $v_result;
  2157. }
  2158. function privReadEndCentralDir(&$p_central_dir) {
  2159. $v_result = 1;
  2160. $v_size = filesize($this->zipname);
  2161. @fseek($this->zip_fd, $v_size);
  2162. if (@ftell($this->zip_fd) != $v_size) {
  2163. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \'' . $this->zipname . '\'');
  2164. return PclZip::errorCode();
  2165. }
  2166. $v_found = 0;
  2167. if ($v_size > 26) {
  2168. @fseek($this->zip_fd, $v_size - 22);
  2169. if (($v_pos = @ftell($this->zip_fd)) != ($v_size - 22)) {
  2170. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \'' . $this->zipname . '\'');
  2171. return PclZip::errorCode();
  2172. }
  2173. $v_binary_data = @fread($this->zip_fd, 4);
  2174. $v_data = @unpack('Vid', $v_binary_data);
  2175. if ($v_data['id'] == 0x06054b50) {
  2176. $v_found = 1;
  2177. }
  2178. $v_pos = ftell($this->zip_fd);
  2179. }
  2180. if (!$v_found) {
  2181. $v_maximum_size = 65557;
  2182. if ($v_maximum_size > $v_size)
  2183. $v_maximum_size = $v_size;
  2184. @fseek($this->zip_fd, $v_size - $v_maximum_size);
  2185. if (@ftell($this->zip_fd) != ($v_size - $v_maximum_size)) {
  2186. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \'' . $this->zipname . '\'');
  2187. return PclZip::errorCode();
  2188. }
  2189. $v_pos = ftell($this->zip_fd);
  2190. $v_bytes = 0x00000000;
  2191. while ($v_pos < $v_size) {
  2192. $v_byte = @fread($this->zip_fd, 1);
  2193. $v_bytes = (($v_bytes & 0xFFFFFF) << 8) | Ord($v_byte);
  2194. if ($v_bytes == 0x504b0506) {
  2195. $v_pos++;
  2196. break;
  2197. }
  2198. $v_pos++;
  2199. }
  2200. if ($v_pos == $v_size) {
  2201. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
  2202. return PclZip::errorCode();
  2203. }
  2204. }
  2205. $v_binary_data = fread($this->zip_fd, 18);
  2206. if (strlen($v_binary_data) != 18) {
  2207. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : " . strlen($v_binary_data));
  2208. return PclZip::errorCode();
  2209. }
  2210. $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
  2211. if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
  2212. if (0) {
  2213. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT,
  2214. 'The central dir is not at the end of the archive.'
  2215. . ' Some trailing bytes exists after the archive.');
  2216. return PclZip::errorCode();
  2217. }
  2218. }
  2219. if ($v_data['comment_size'] != 0) {
  2220. $p_central_dir['comment'] = fread($this->zip_fd, $v_data['comment_size']);
  2221. } else
  2222. $p_central_dir['comment'] = '';
  2223. $p_central_dir['entries'] = $v_data['entries'];
  2224. $p_central_dir['disk_entries'] = $v_data['disk_entries'];
  2225. $p_central_dir['offset'] = $v_data['offset'];
  2226. $p_central_dir['size'] = $v_data['size'];
  2227. $p_central_dir['disk'] = $v_data['disk'];
  2228. $p_central_dir['disk_start'] = $v_data['disk_start'];
  2229. return $v_result;
  2230. }
  2231. function privDeleteByRule(&$p_result_list, &$p_options) {
  2232. $v_result = 1;
  2233. $v_list_detail = array();
  2234. if (($v_result = $this->privOpenFd('rb')) != 1) {
  2235. return $v_result;
  2236. }
  2237. $v_central_dir = array();
  2238. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  2239. $this->privCloseFd();
  2240. return $v_result;
  2241. }
  2242. @rewind($this->zip_fd);
  2243. $v_pos_entry = $v_central_dir['offset'];
  2244. @rewind($this->zip_fd);
  2245. if (@fseek($this->zip_fd, $v_pos_entry)) {
  2246. $this->privCloseFd();
  2247. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  2248. return PclZip::errorCode();
  2249. }
  2250. $v_header_list = array();
  2251. $j_start = 0;
  2252. for ($i = 0, $v_nb_extracted = 0; $i < $v_central_dir['entries']; $i++) {
  2253. $v_header_list[$v_nb_extracted] = array();
  2254. if (($v_result = $this->privReadCentralFileHeader($v_header_list[$v_nb_extracted])) != 1) {
  2255. $this->privCloseFd();
  2256. return $v_result;
  2257. }
  2258. $v_header_list[$v_nb_extracted]['index'] = $i;
  2259. $v_found = false;
  2260. if ((isset($p_options[PCLZIP_OPT_BY_NAME]))
  2261. && ($p_options[PCLZIP_OPT_BY_NAME] != 0)
  2262. ) {
  2263. for ($j = 0; ($j < sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_found); $j++) {
  2264. if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
  2265. if ((strlen($v_header_list[$v_nb_extracted]['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
  2266. && (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])
  2267. ) {
  2268. $v_found = true;
  2269. } elseif ((($v_header_list[$v_nb_extracted]['external'] & 0x00000010) == 0x00000010)
  2270. && ($v_header_list[$v_nb_extracted]['stored_filename'] . '/' == $p_options[PCLZIP_OPT_BY_NAME][$j])
  2271. ) {
  2272. $v_found = true;
  2273. }
  2274. } elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
  2275. $v_found = true;
  2276. }
  2277. }
  2278. } else if ((isset($p_options[PCLZIP_OPT_BY_PREG]))
  2279. && ($p_options[PCLZIP_OPT_BY_PREG] != "")
  2280. ) {
  2281. if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
  2282. $v_found = true;
  2283. }
  2284. } else if ((isset($p_options[PCLZIP_OPT_BY_INDEX]))
  2285. && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)
  2286. ) {
  2287. for ($j = $j_start; ($j < sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_found); $j++) {
  2288. if (($i >= $p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i <= $p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
  2289. $v_found = true;
  2290. }
  2291. if ($i >= $p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
  2292. $j_start = $j + 1;
  2293. }
  2294. if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start'] > $i) {
  2295. break;
  2296. }
  2297. }
  2298. } else {
  2299. $v_found = true;
  2300. }
  2301. if ($v_found) {
  2302. unset($v_header_list[$v_nb_extracted]);
  2303. } else {
  2304. $v_nb_extracted++;
  2305. }
  2306. }
  2307. if ($v_nb_extracted > 0) {
  2308. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.tmp';
  2309. $v_temp_zip = new PclZip($v_zip_temp_name);
  2310. if (($v_result = $v_temp_zip->privOpenFd('wb')) != 1) {
  2311. $this->privCloseFd();
  2312. return $v_result;
  2313. }
  2314. for ($i = 0; $i < sizeof($v_header_list); $i++) {
  2315. @rewind($this->zip_fd);
  2316. if (@fseek($this->zip_fd, $v_header_list[$i]['offset'])) {
  2317. $this->privCloseFd();
  2318. $v_temp_zip->privCloseFd();
  2319. @unlink($v_zip_temp_name);
  2320. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  2321. return PclZip::errorCode();
  2322. }
  2323. $v_local_header = array();
  2324. if (($v_result = $this->privReadFileHeader($v_local_header)) != 1) {
  2325. $this->privCloseFd();
  2326. $v_temp_zip->privCloseFd();
  2327. @unlink($v_zip_temp_name);
  2328. return $v_result;
  2329. }
  2330. if ($this->privCheckFileHeaders($v_local_header,
  2331. $v_header_list[$i]) != 1
  2332. ) {
  2333. }
  2334. unset($v_local_header);
  2335. if (($v_result = $v_temp_zip->privWriteFileHeader($v_header_list[$i])) != 1) {
  2336. $this->privCloseFd();
  2337. $v_temp_zip->privCloseFd();
  2338. @unlink($v_zip_temp_name);
  2339. return $v_result;
  2340. }
  2341. if (($v_result = PclZipUtilCopyBlock($this->zip_fd, $v_temp_zip->zip_fd, $v_header_list[$i]['compressed_size'])) != 1) {
  2342. $this->privCloseFd();
  2343. $v_temp_zip->privCloseFd();
  2344. @unlink($v_zip_temp_name);
  2345. return $v_result;
  2346. }
  2347. }
  2348. $v_offset = @ftell($v_temp_zip->zip_fd);
  2349. for ($i = 0; $i < sizeof($v_header_list); $i++) {
  2350. if (($v_result = $v_temp_zip->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  2351. $v_temp_zip->privCloseFd();
  2352. $this->privCloseFd();
  2353. @unlink($v_zip_temp_name);
  2354. return $v_result;
  2355. }
  2356. $v_temp_zip->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  2357. }
  2358. $v_comment = '';
  2359. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  2360. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  2361. }
  2362. $v_size = @ftell($v_temp_zip->zip_fd) - $v_offset;
  2363. if (($v_result = $v_temp_zip->privWriteCentralHeader(sizeof($v_header_list), $v_size, $v_offset, $v_comment)) != 1) {
  2364. unset($v_header_list);
  2365. $v_temp_zip->privCloseFd();
  2366. $this->privCloseFd();
  2367. @unlink($v_zip_temp_name);
  2368. return $v_result;
  2369. }
  2370. $v_temp_zip->privCloseFd();
  2371. $this->privCloseFd();
  2372. @unlink($this->zipname);
  2373. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  2374. unset($v_temp_zip);
  2375. } else if ($v_central_dir['entries'] != 0) {
  2376. $this->privCloseFd();
  2377. if (($v_result = $this->privOpenFd('wb')) != 1) {
  2378. return $v_result;
  2379. }
  2380. if (($v_result = $this->privWriteCentralHeader(0, 0, 0, '')) != 1) {
  2381. return $v_result;
  2382. }
  2383. $this->privCloseFd();
  2384. }
  2385. return $v_result;
  2386. }
  2387. function privDirCheck($p_dir, $p_is_dir = false) {
  2388. $v_result = 1;
  2389. if (($p_is_dir) && (substr($p_dir, -1) == '/')) {
  2390. $p_dir = substr($p_dir, 0, strlen($p_dir) - 1);
  2391. }
  2392. if ((is_dir($p_dir)) || ($p_dir == "")) {
  2393. return 1;
  2394. }
  2395. $p_parent_dir = dirname($p_dir);
  2396. if ($p_parent_dir != $p_dir) {
  2397. if ($p_parent_dir != "") {
  2398. if (($v_result = $this->privDirCheck($p_parent_dir)) != 1) {
  2399. return $v_result;
  2400. }
  2401. }
  2402. }
  2403. if (!@mkdir($p_dir, 0777)) {
  2404. PclZip::privErrorLog(PCLZIP_ERR_DIR_CREATE_FAIL, "Unable to create directory '$p_dir'");
  2405. return PclZip::errorCode();
  2406. }
  2407. return $v_result;
  2408. }
  2409. function privMerge(&$p_archive_to_add) {
  2410. $v_result = 1;
  2411. if (!is_file($p_archive_to_add->zipname)) {
  2412. $v_result = 1;
  2413. return $v_result;
  2414. }
  2415. if (!is_file($this->zipname)) {
  2416. $v_result = $this->privDuplicate($p_archive_to_add->zipname);
  2417. return $v_result;
  2418. }
  2419. if (($v_result = $this->privOpenFd('rb')) != 1) {
  2420. return $v_result;
  2421. }
  2422. $v_central_dir = array();
  2423. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
  2424. $this->privCloseFd();
  2425. return $v_result;
  2426. }
  2427. @rewind($this->zip_fd);
  2428. if (($v_result = $p_archive_to_add->privOpenFd('rb')) != 1) {
  2429. $this->privCloseFd();
  2430. return $v_result;
  2431. }
  2432. $v_central_dir_to_add = array();
  2433. if (($v_result = $p_archive_to_add->privReadEndCentralDir($v_central_dir_to_add)) != 1) {
  2434. $this->privCloseFd();
  2435. $p_archive_to_add->privCloseFd();
  2436. return $v_result;
  2437. }
  2438. @rewind($p_archive_to_add->zip_fd);
  2439. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.tmp';
  2440. if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0) {
  2441. $this->privCloseFd();
  2442. $p_archive_to_add->privCloseFd();
  2443. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_zip_temp_name . '\' in binary write mode');
  2444. return PclZip::errorCode();
  2445. }
  2446. $v_size = $v_central_dir['offset'];
  2447. while ($v_size != 0) {
  2448. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2449. $v_buffer = fread($this->zip_fd, $v_read_size);
  2450. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  2451. $v_size -= $v_read_size;
  2452. }
  2453. $v_size = $v_central_dir_to_add['offset'];
  2454. while ($v_size != 0) {
  2455. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2456. $v_buffer = fread($p_archive_to_add->zip_fd, $v_read_size);
  2457. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  2458. $v_size -= $v_read_size;
  2459. }
  2460. $v_offset = @ftell($v_zip_temp_fd);
  2461. $v_size = $v_central_dir['size'];
  2462. while ($v_size != 0) {
  2463. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2464. $v_buffer = @fread($this->zip_fd, $v_read_size);
  2465. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  2466. $v_size -= $v_read_size;
  2467. }
  2468. $v_size = $v_central_dir_to_add['size'];
  2469. while ($v_size != 0) {
  2470. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2471. $v_buffer = @fread($p_archive_to_add->zip_fd, $v_read_size);
  2472. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  2473. $v_size -= $v_read_size;
  2474. }
  2475. $v_comment = $v_central_dir['comment'] . ' ' . $v_central_dir_to_add['comment'];
  2476. $v_size = @ftell($v_zip_temp_fd) - $v_offset;
  2477. $v_swap = $this->zip_fd;
  2478. $this->zip_fd = $v_zip_temp_fd;
  2479. $v_zip_temp_fd = $v_swap;
  2480. if (($v_result = $this->privWriteCentralHeader($v_central_dir['entries'] + $v_central_dir_to_add['entries'], $v_size, $v_offset, $v_comment)) != 1) {
  2481. $this->privCloseFd();
  2482. $p_archive_to_add->privCloseFd();
  2483. @fclose($v_zip_temp_fd);
  2484. $this->zip_fd = null;
  2485. unset($v_header_list);
  2486. return $v_result;
  2487. }
  2488. $v_swap = $this->zip_fd;
  2489. $this->zip_fd = $v_zip_temp_fd;
  2490. $v_zip_temp_fd = $v_swap;
  2491. $this->privCloseFd();
  2492. $p_archive_to_add->privCloseFd();
  2493. @fclose($v_zip_temp_fd);
  2494. @unlink($this->zipname);
  2495. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  2496. return $v_result;
  2497. }
  2498. function privDuplicate($p_archive_filename) {
  2499. $v_result = 1;
  2500. if (!is_file($p_archive_filename)) {
  2501. $v_result = 1;
  2502. return $v_result;
  2503. }
  2504. if (($v_result = $this->privOpenFd('wb')) != 1) {
  2505. return $v_result;
  2506. }
  2507. if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0) {
  2508. $this->privCloseFd();
  2509. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive file \'' . $p_archive_filename . '\' in binary write mode');
  2510. return PclZip::errorCode();
  2511. }
  2512. $v_size = filesize($p_archive_filename);
  2513. while ($v_size != 0) {
  2514. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2515. $v_buffer = fread($v_zip_temp_fd, $v_read_size);
  2516. @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  2517. $v_size -= $v_read_size;
  2518. }
  2519. $this->privCloseFd();
  2520. @fclose($v_zip_temp_fd);
  2521. return $v_result;
  2522. }
  2523. function privErrorLog($p_error_code = 0, $p_error_string = '') {
  2524. if (PCLZIP_ERROR_EXTERNAL == 1) {
  2525. PclError($p_error_code, $p_error_string);
  2526. } else {
  2527. $this->error_code = $p_error_code;
  2528. $this->error_string = $p_error_string;
  2529. }
  2530. }
  2531. function privErrorReset() {
  2532. if (PCLZIP_ERROR_EXTERNAL == 1) {
  2533. PclErrorReset();
  2534. } else {
  2535. $this->error_code = 0;
  2536. $this->error_string = '';
  2537. }
  2538. }
  2539. function privDisableMagicQuotes() {
  2540. $v_result = 1;
  2541. if ((!function_exists("get_magic_quotes_runtime"))
  2542. || (!function_exists("set_magic_quotes_runtime"))
  2543. ) {
  2544. return $v_result;
  2545. }
  2546. if ($this->magic_quotes_status != -1) {
  2547. return $v_result;
  2548. }
  2549. $this->magic_quotes_status = @get_magic_quotes_runtime();
  2550. if ($this->magic_quotes_status == 1) {
  2551. @set_magic_quotes_runtime(0);
  2552. }
  2553. return $v_result;
  2554. }
  2555. function privSwapBackMagicQuotes() {
  2556. $v_result = 1;
  2557. if ((!function_exists("get_magic_quotes_runtime"))
  2558. || (!function_exists("set_magic_quotes_runtime"))
  2559. ) {
  2560. return $v_result;
  2561. }
  2562. if ($this->magic_quotes_status != -1) {
  2563. return $v_result;
  2564. }
  2565. if ($this->magic_quotes_status == 1) {
  2566. @set_magic_quotes_runtime($this->magic_quotes_status);
  2567. }
  2568. return $v_result;
  2569. }
  2570. }
  2571. function PclZipUtilPathReduction($p_dir) {
  2572. $v_result = "";
  2573. if ($p_dir != "") {
  2574. $v_list = explode("/", $p_dir);
  2575. $v_skip = 0;
  2576. for ($i = sizeof($v_list) - 1; $i >= 0; $i--) {
  2577. if ($v_list[$i] == ".") {
  2578. } else if ($v_list[$i] == "..") {
  2579. $v_skip++;
  2580. } else if ($v_list[$i] == "") {
  2581. if ($i == 0) {
  2582. $v_result = "/" . $v_result;
  2583. if ($v_skip > 0) {
  2584. $v_result = $p_dir;
  2585. $v_skip = 0;
  2586. }
  2587. } else if ($i == (sizeof($v_list) - 1)) {
  2588. $v_result = $v_list[$i];
  2589. } else {
  2590. }
  2591. } else {
  2592. if ($v_skip > 0) {
  2593. $v_skip--;
  2594. } else {
  2595. $v_result = $v_list[$i] . ($i != (sizeof($v_list) - 1) ? "/" . $v_result : "");
  2596. }
  2597. }
  2598. }
  2599. if ($v_skip > 0) {
  2600. while ($v_skip > 0) {
  2601. $v_result = '../' . $v_result;
  2602. $v_skip--;
  2603. }
  2604. }
  2605. }
  2606. return $v_result;
  2607. }
  2608. function PclZipUtilPathInclusion($p_dir, $p_path) {
  2609. $v_result = 1;
  2610. if (($p_dir == '.')
  2611. || ((strlen($p_dir) >= 2) && (substr($p_dir, 0, 2) == './'))
  2612. ) {
  2613. $p_dir = PclZipUtilTranslateWinPath(getcwd(), FALSE) . '/' . substr($p_dir, 1);
  2614. }
  2615. if (($p_path == '.')
  2616. || ((strlen($p_path) >= 2) && (substr($p_path, 0, 2) == './'))
  2617. ) {
  2618. $p_path = PclZipUtilTranslateWinPath(getcwd(), FALSE) . '/' . substr($p_path, 1);
  2619. }
  2620. $v_list_dir = explode("/", $p_dir);
  2621. $v_list_dir_size = sizeof($v_list_dir);
  2622. $v_list_path = explode("/", $p_path);
  2623. $v_list_path_size = sizeof($v_list_path);
  2624. $i = 0;
  2625. $j = 0;
  2626. while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
  2627. if ($v_list_dir[$i] == '') {
  2628. $i++;
  2629. continue;
  2630. }
  2631. if ($v_list_path[$j] == '') {
  2632. $j++;
  2633. continue;
  2634. }
  2635. if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ($v_list_path[$j] != '')) {
  2636. $v_result = 0;
  2637. }
  2638. $i++;
  2639. $j++;
  2640. }
  2641. if ($v_result) {
  2642. while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) $j++;
  2643. while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) $i++;
  2644. if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
  2645. $v_result = 2;
  2646. } else if ($i < $v_list_dir_size) {
  2647. $v_result = 0;
  2648. }
  2649. }
  2650. return $v_result;
  2651. }
  2652. function PclZipUtilCopyBlock($p_src, $p_dest, $p_size, $p_mode = 0) {
  2653. $v_result = 1;
  2654. if ($p_mode == 0) {
  2655. while ($p_size != 0) {
  2656. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  2657. $v_buffer = @fread($p_src, $v_read_size);
  2658. @fwrite($p_dest, $v_buffer, $v_read_size);
  2659. $p_size -= $v_read_size;
  2660. }
  2661. } else if ($p_mode == 1) {
  2662. while ($p_size != 0) {
  2663. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  2664. $v_buffer = @gzread($p_src, $v_read_size);
  2665. @fwrite($p_dest, $v_buffer, $v_read_size);
  2666. $p_size -= $v_read_size;
  2667. }
  2668. } else if ($p_mode == 2) {
  2669. while ($p_size != 0) {
  2670. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  2671. $v_buffer = @fread($p_src, $v_read_size);
  2672. @gzwrite($p_dest, $v_buffer, $v_read_size);
  2673. $p_size -= $v_read_size;
  2674. }
  2675. } else if ($p_mode == 3) {
  2676. while ($p_size != 0) {
  2677. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  2678. $v_buffer = @gzread($p_src, $v_read_size);
  2679. @gzwrite($p_dest, $v_buffer, $v_read_size);
  2680. $p_size -= $v_read_size;
  2681. }
  2682. }
  2683. return $v_result;
  2684. }
  2685. function PclZipUtilRename($p_src, $p_dest) {
  2686. $v_result = 1;
  2687. if (!@rename($p_src, $p_dest)) {
  2688. if (!@copy($p_src, $p_dest)) {
  2689. $v_result = 0;
  2690. } else if (!@unlink($p_src)) {
  2691. $v_result = 0;
  2692. }
  2693. }
  2694. return $v_result;
  2695. }
  2696. function PclZipUtilOptionText($p_option) {
  2697. $v_list = get_defined_constants();
  2698. for (reset($v_list); $v_key = key($v_list); next($v_list)) {
  2699. $v_prefix = substr($v_key, 0, 10);
  2700. if ((($v_prefix == 'PCLZIP_OPT')
  2701. || ($v_prefix == 'PCLZIP_CB_')
  2702. || ($v_prefix == 'PCLZIP_ATT'))
  2703. && ($v_list[$v_key] == $p_option)
  2704. ) {
  2705. return $v_key;
  2706. }
  2707. }
  2708. $v_result = 'Unknown';
  2709. return $v_result;
  2710. }
  2711. function PclZipUtilTranslateWinPath($p_path, $p_remove_disk_letter = true) {
  2712. if (stristr(php_uname(), 'windows')) {
  2713. if (($p_remove_disk_letter) && (($v_position = strpos($p_path, ':')) != false)) {
  2714. $p_path = substr($p_path, $v_position + 1);
  2715. }
  2716. if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0, 1) == '\\')) {
  2717. $p_path = strtr($p_path, '\\', '/');
  2718. }
  2719. }
  2720. return $p_path;
  2721. }
  2722. ?>