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

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