Просмотр файла engine/classes/zip.php

Размер файла: 120.53Kb
  1. <?php
  2.  
  3. if (!defined('PCLZIP_READ_BLOCK_SIZE')) {
  4. define( 'PCLZIP_READ_BLOCK_SIZE', 2048 );
  5. }
  6. if (!defined('PCLZIP_SEPARATOR')) {
  7. define( 'PCLZIP_SEPARATOR', ',' );
  8. }
  9.  
  10. if (!defined('PCLZIP_ERROR_EXTERNAL')) {
  11. define( 'PCLZIP_ERROR_EXTERNAL', 0 );
  12. }
  13.  
  14. if (!defined('PCLZIP_TEMPORARY_DIR')) {
  15. define( 'PCLZIP_TEMPORARY_DIR', '' );
  16. }
  17.  
  18. if (!defined('PCLZIP_TEMPORARY_FILE_RATIO')) {
  19. define( 'PCLZIP_TEMPORARY_FILE_RATIO', 0.47 );
  20. }
  21.  
  22. $g_pclzip_version = "2.8.2";
  23.  
  24. define( 'PCLZIP_ERR_USER_ABORTED', 2 );
  25. define( 'PCLZIP_ERR_NO_ERROR', 0 );
  26. define( 'PCLZIP_ERR_WRITE_OPEN_FAIL', -1 );
  27. define( 'PCLZIP_ERR_READ_OPEN_FAIL', -2 );
  28. define( 'PCLZIP_ERR_INVALID_PARAMETER', -3 );
  29. define( 'PCLZIP_ERR_MISSING_FILE', -4 );
  30. define( 'PCLZIP_ERR_FILENAME_TOO_LONG', -5 );
  31. define( 'PCLZIP_ERR_INVALID_ZIP', -6 );
  32. define( 'PCLZIP_ERR_BAD_EXTRACTED_FILE', -7 );
  33. define( 'PCLZIP_ERR_DIR_CREATE_FAIL', -8 );
  34. define( 'PCLZIP_ERR_BAD_EXTENSION', -9 );
  35. define( 'PCLZIP_ERR_BAD_FORMAT', -10 );
  36. define( 'PCLZIP_ERR_DELETE_FILE_FAIL', -11 );
  37. define( 'PCLZIP_ERR_RENAME_FILE_FAIL', -12 );
  38. define( 'PCLZIP_ERR_BAD_CHECKSUM', -13 );
  39. define( 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP', -14 );
  40. define( 'PCLZIP_ERR_MISSING_OPTION_VALUE', -15 );
  41. define( 'PCLZIP_ERR_INVALID_OPTION_VALUE', -16 );
  42. define( 'PCLZIP_ERR_ALREADY_A_DIRECTORY', -17 );
  43. define( 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION', -18 );
  44. define( 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION', -19 );
  45. define( 'PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE', -20 );
  46. define( 'PCLZIP_ERR_DIRECTORY_RESTRICTION', -21 );
  47.  
  48. define( 'PCLZIP_OPT_PATH', 77001 );
  49. define( 'PCLZIP_OPT_ADD_PATH', 77002 );
  50. define( 'PCLZIP_OPT_REMOVE_PATH', 77003 );
  51. define( 'PCLZIP_OPT_REMOVE_ALL_PATH', 77004 );
  52. define( 'PCLZIP_OPT_SET_CHMOD', 77005 );
  53. define( 'PCLZIP_OPT_EXTRACT_AS_STRING', 77006 );
  54. define( 'PCLZIP_OPT_NO_COMPRESSION', 77007 );
  55. define( 'PCLZIP_OPT_BY_NAME', 77008 );
  56. define( 'PCLZIP_OPT_BY_INDEX', 77009 );
  57. define( 'PCLZIP_OPT_BY_EREG', 77010 );
  58. define( 'PCLZIP_OPT_BY_PREG', 77011 );
  59. define( 'PCLZIP_OPT_COMMENT', 77012 );
  60. define( 'PCLZIP_OPT_ADD_COMMENT', 77013 );
  61. define( 'PCLZIP_OPT_PREPEND_COMMENT', 77014 );
  62. define( 'PCLZIP_OPT_EXTRACT_IN_OUTPUT', 77015 );
  63. define( 'PCLZIP_OPT_REPLACE_NEWER', 77016 );
  64. define( 'PCLZIP_OPT_STOP_ON_ERROR', 77017 );
  65. define( 'PCLZIP_OPT_EXTRACT_DIR_RESTRICTION', 77019 );
  66. define( 'PCLZIP_OPT_TEMP_FILE_THRESHOLD', 77020 );
  67. define( 'PCLZIP_OPT_ADD_TEMP_FILE_THRESHOLD', 77020 ); define( 'PCLZIP_OPT_TEMP_FILE_ON', 77021 );
  68. define( 'PCLZIP_OPT_ADD_TEMP_FILE_ON', 77021 ); define( 'PCLZIP_OPT_TEMP_FILE_OFF', 77022 );
  69. define( 'PCLZIP_OPT_ADD_TEMP_FILE_OFF', 77022 );
  70. define( 'PCLZIP_OPT_SUBSTITUE_FILE', 77023 );
  71. define( 'PCLZIP_ATT_FILE_NAME', 79001 );
  72. define( 'PCLZIP_ATT_FILE_NEW_SHORT_NAME', 79002 );
  73. define( 'PCLZIP_ATT_FILE_NEW_FULL_NAME', 79003 );
  74. define( 'PCLZIP_ATT_FILE_MTIME', 79004 );
  75. define( 'PCLZIP_ATT_FILE_CONTENT', 79005 );
  76. define( 'PCLZIP_ATT_FILE_COMMENT', 79006 );
  77. define( 'PCLZIP_CB_PRE_EXTRACT', 78001 );
  78. define( 'PCLZIP_CB_POST_EXTRACT', 78002 );
  79. define( 'PCLZIP_CB_PRE_ADD', 78003 );
  80. define( 'PCLZIP_CB_POST_ADD', 78004 );
  81.  
  82. class PclZip
  83. {
  84.  
  85. public $zipname = '';
  86. public $zip_fd = 0;
  87. public $error_code = 1;
  88. public $error_string = '';
  89. public $magic_quotes_status;
  90.  
  91. public function __construct($p_zipname)
  92. {
  93.  
  94. if (!function_exists('gzopen'))
  95. {
  96. die('Abort '.basename(__FILE__).' : Missing zlib extensions');
  97. }
  98.  
  99. $this->zipname = $p_zipname;
  100. $this->zip_fd = 0;
  101. $this->magic_quotes_status = -1;
  102.  
  103. return;
  104. }
  105.  
  106. public function create($p_filelist)
  107. {
  108. $v_result=1;
  109. $this->privErrorReset();
  110. $v_options = array();
  111. $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
  112. $v_size = func_num_args();
  113.  
  114. if ($v_size > 1) {
  115. $v_arg_list = func_get_args();
  116. array_shift($v_arg_list);
  117. $v_size--;
  118.  
  119. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  120.  
  121. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  122. array (PCLZIP_OPT_REMOVE_PATH => 'optional',
  123. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  124. PCLZIP_OPT_ADD_PATH => 'optional',
  125. PCLZIP_CB_PRE_ADD => 'optional',
  126. PCLZIP_CB_POST_ADD => 'optional',
  127. PCLZIP_OPT_NO_COMPRESSION => 'optional',
  128. PCLZIP_OPT_COMMENT => 'optional',
  129. PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
  130. PCLZIP_OPT_TEMP_FILE_ON => 'optional',
  131. PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
  132. ));
  133. if ($v_result != 1) {
  134. return 0;
  135. }
  136. }
  137. else {
  138. $v_options[PCLZIP_OPT_ADD_PATH] = $v_arg_list[0];
  139. if ($v_size == 2) {
  140. $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
  141. }
  142. else if ($v_size > 2) {
  143. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
  144. "Invalid number / type of arguments");
  145. return 0;
  146. }
  147. }
  148. }
  149. $this->privOptionDefaultThreshold($v_options);
  150.  
  151. $v_string_list = array();
  152. $v_att_list = array();
  153. $v_filedescr_list = array();
  154. $p_result_list = array();
  155. if (is_array($p_filelist)) {
  156. if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
  157. $v_att_list = $p_filelist;
  158. }
  159. else {
  160. $v_string_list = $p_filelist;
  161. }
  162. }
  163.  
  164. else if (is_string($p_filelist)) {
  165. $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
  166. }
  167. else {
  168. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
  169. return 0;
  170. }
  171. if (sizeof($v_string_list) != 0) {
  172. foreach ($v_string_list as $v_string) {
  173. if ($v_string != '') {
  174. $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
  175. }
  176. else {
  177. }
  178. }
  179. }
  180. $v_supported_attributes = array ( PCLZIP_ATT_FILE_NAME => 'mandatory'
  181. ,PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
  182. ,PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
  183. ,PCLZIP_ATT_FILE_MTIME => 'optional'
  184. ,PCLZIP_ATT_FILE_CONTENT => 'optional'
  185. ,PCLZIP_ATT_FILE_COMMENT => 'optional'
  186. );
  187. foreach ($v_att_list as $v_entry) {
  188. $v_result = $this->privFileDescrParseAtt($v_entry,
  189. $v_filedescr_list[],
  190. $v_options,
  191. $v_supported_attributes);
  192. if ($v_result != 1) {
  193. return 0;
  194. }
  195. }
  196.  
  197. $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
  198. if ($v_result != 1) {
  199. return 0;
  200. }
  201.  
  202. $v_result = $this->privCreate($v_filedescr_list, $p_result_list, $v_options);
  203. if ($v_result != 1) {
  204. return 0;
  205. }
  206.  
  207. return $p_result_list;
  208. }
  209.  
  210. function add($p_filelist)
  211. {
  212. $v_result=1;
  213.  
  214. $this->privErrorReset();
  215.  
  216. $v_options = array();
  217. $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
  218.  
  219. $v_size = func_num_args();
  220.  
  221. if ($v_size > 1) {
  222. $v_arg_list = func_get_args();
  223. array_shift($v_arg_list);
  224. $v_size--;
  225. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  226. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  227. array (PCLZIP_OPT_REMOVE_PATH => 'optional',
  228. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  229. PCLZIP_OPT_ADD_PATH => 'optional',
  230. PCLZIP_CB_PRE_ADD => 'optional',
  231. PCLZIP_CB_POST_ADD => 'optional',
  232. PCLZIP_OPT_NO_COMPRESSION => 'optional',
  233. PCLZIP_OPT_COMMENT => 'optional',
  234. PCLZIP_OPT_ADD_COMMENT => 'optional',
  235. PCLZIP_OPT_PREPEND_COMMENT => 'optional',
  236. PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
  237. PCLZIP_OPT_TEMP_FILE_ON => 'optional',
  238. PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
  239. ));
  240. if ($v_result != 1) {
  241. return 0;
  242. }
  243. }
  244. else {
  245. $v_options[PCLZIP_OPT_ADD_PATH] = $v_add_path = $v_arg_list[0];
  246. if ($v_size == 2) {
  247. $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
  248. }
  249. else if ($v_size > 2) {
  250. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  251. return 0;
  252. }
  253. }
  254. }
  255.  
  256. $this->privOptionDefaultThreshold($v_options);
  257.  
  258. $v_string_list = array();
  259. $v_att_list = array();
  260. $v_filedescr_list = array();
  261. $p_result_list = array();
  262. if (is_array($p_filelist)) {
  263. if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
  264. $v_att_list = $p_filelist;
  265. }
  266. else {
  267. $v_string_list = $p_filelist;
  268. }
  269. }
  270.  
  271. else if (is_string($p_filelist)) {
  272. $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
  273. }
  274.  
  275. else {
  276. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type '".gettype($p_filelist)."' for p_filelist");
  277. return 0;
  278. }
  279. if (sizeof($v_string_list) != 0) {
  280. foreach ($v_string_list as $v_string) {
  281. $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
  282. }
  283. }
  284. $v_supported_attributes
  285. = array ( PCLZIP_ATT_FILE_NAME => 'mandatory'
  286. ,PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
  287. ,PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
  288. ,PCLZIP_ATT_FILE_MTIME => 'optional'
  289. ,PCLZIP_ATT_FILE_CONTENT => 'optional'
  290. ,PCLZIP_ATT_FILE_COMMENT => 'optional'
  291. );
  292. foreach ($v_att_list as $v_entry) {
  293. $v_result = $this->privFileDescrParseAtt($v_entry,
  294. $v_filedescr_list[],
  295. $v_options,
  296. $v_supported_attributes);
  297. if ($v_result != 1) {
  298. return 0;
  299. }
  300. }
  301.  
  302. $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
  303. if ($v_result != 1) {
  304. return 0;
  305. }
  306.  
  307. $v_result = $this->privAdd($v_filedescr_list, $p_result_list, $v_options);
  308. if ($v_result != 1) {
  309. return 0;
  310. }
  311.  
  312. return $p_result_list;
  313. }
  314.  
  315. function listContent()
  316. {
  317. $v_result=1;
  318.  
  319. $this->privErrorReset();
  320.  
  321. if (!$this->privCheckFormat()) {
  322. return(0);
  323. }
  324.  
  325. $p_list = array();
  326. if (($v_result = $this->privList($p_list)) != 1)
  327. {
  328. unset($p_list);
  329. return(0);
  330. }
  331.  
  332. return $p_list;
  333. }
  334.  
  335. function extract()
  336. {
  337. $v_result=1;
  338. $this->privErrorReset();
  339.  
  340. if (!$this->privCheckFormat()) {
  341. return(0);
  342. }
  343.  
  344. $v_options = array();
  345. $v_path = '';
  346. $v_remove_path = "";
  347. $v_remove_all_path = false;
  348.  
  349. $v_size = func_num_args();
  350. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
  351. $v_options[PCLZIP_OPT_SUBSTITUE_FILE] = FALSE;
  352. if ($v_size > 0) {
  353. $v_arg_list = func_get_args();
  354. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  355. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  356. array (PCLZIP_OPT_PATH => 'optional',
  357. PCLZIP_OPT_REMOVE_PATH => 'optional',
  358. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  359. PCLZIP_OPT_ADD_PATH => 'optional',
  360. PCLZIP_CB_PRE_EXTRACT => 'optional',
  361. PCLZIP_CB_POST_EXTRACT => 'optional',
  362. PCLZIP_OPT_SET_CHMOD => 'optional',
  363. PCLZIP_OPT_BY_NAME => 'optional',
  364. PCLZIP_OPT_BY_EREG => 'optional',
  365. PCLZIP_OPT_BY_PREG => 'optional',
  366. PCLZIP_OPT_BY_INDEX => 'optional',
  367. PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
  368. PCLZIP_OPT_EXTRACT_IN_OUTPUT => 'optional',
  369. PCLZIP_OPT_REPLACE_NEWER => 'optional',
  370. PCLZIP_OPT_STOP_ON_ERROR => 'optional',
  371. PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional',
  372. PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
  373. PCLZIP_OPT_TEMP_FILE_ON => 'optional',
  374. PCLZIP_OPT_TEMP_FILE_OFF => 'optional',
  375. PCLZIP_OPT_SUBSTITUE_FILE => 'optional',
  376. ));
  377. if ($v_result != 1) {
  378. return 0;
  379. }
  380. if (isset($v_options[PCLZIP_OPT_PATH])) {
  381. $v_path = $v_options[PCLZIP_OPT_PATH];
  382. }
  383. if (isset($v_options[PCLZIP_OPT_SUBSTITUE_FILE])) {
  384. $v_path = $v_options[PCLZIP_OPT_SUBSTITUE_FILE];
  385. }
  386. if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
  387. $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
  388. }
  389. if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  390. $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  391. }
  392. if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
  393. if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
  394. $v_path .= '/';
  395. }
  396. $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
  397. }
  398. }
  399. else {
  400. $v_path = $v_arg_list[0];
  401. if ($v_size == 2) {
  402. $v_remove_path = $v_arg_list[1];
  403. }
  404. else if ($v_size > 2) {
  405. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  406. return 0;
  407. }
  408. }
  409. }
  410.  
  411. $this->privOptionDefaultThreshold($v_options);
  412.  
  413. $p_list = array();
  414. $v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path,
  415. $v_remove_all_path, $v_options);
  416. if ($v_result < 1) {
  417. unset($p_list);
  418. return(0);
  419. }
  420.  
  421. return $p_list;
  422. }
  423. function extractByIndex($p_index)
  424. {
  425. $v_result=1;
  426. $this->privErrorReset();
  427.  
  428. if (!$this->privCheckFormat()) {
  429. return(0);
  430. }
  431.  
  432. $v_options = array();
  433. $v_path = '';
  434. $v_remove_path = "";
  435. $v_remove_all_path = false;
  436.  
  437. $v_size = func_num_args();
  438. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
  439.  
  440. if ($v_size > 1) {
  441. $v_arg_list = func_get_args();
  442. array_shift($v_arg_list);
  443. $v_size--;
  444. if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
  445. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  446. array (PCLZIP_OPT_PATH => 'optional',
  447. PCLZIP_OPT_REMOVE_PATH => 'optional',
  448. PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
  449. PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
  450. PCLZIP_OPT_ADD_PATH => 'optional',
  451. PCLZIP_CB_PRE_EXTRACT => 'optional',
  452. PCLZIP_CB_POST_EXTRACT => 'optional',
  453. PCLZIP_OPT_SET_CHMOD => 'optional',
  454. PCLZIP_OPT_REPLACE_NEWER => 'optional'
  455. ,PCLZIP_OPT_STOP_ON_ERROR => 'optional'
  456. ,PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional',
  457. PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
  458. PCLZIP_OPT_TEMP_FILE_ON => 'optional',
  459. PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
  460. ));
  461. if ($v_result != 1) {
  462. return 0;
  463. }
  464. if (isset($v_options[PCLZIP_OPT_PATH])) {
  465. $v_path = $v_options[PCLZIP_OPT_PATH];
  466. }
  467. if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
  468. $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
  469. }
  470. if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  471. $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  472. }
  473. if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
  474. if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
  475. $v_path .= '/';
  476. }
  477. $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
  478. }
  479. if (!isset($v_options[PCLZIP_OPT_EXTRACT_AS_STRING])) {
  480. $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
  481. }
  482. else {
  483. }
  484. }
  485. else {
  486. $v_path = $v_arg_list[0];
  487. if ($v_size == 2) {
  488. $v_remove_path = $v_arg_list[1];
  489. }
  490. else if ($v_size > 2) {
  491. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
  492. return 0;
  493. }
  494. }
  495. }
  496.  
  497. $v_arg_trick = array (PCLZIP_OPT_BY_INDEX, $p_index);
  498. $v_options_trick = array();
  499. $v_result = $this->privParseOptions($v_arg_trick, sizeof($v_arg_trick), $v_options_trick,
  500. array (PCLZIP_OPT_BY_INDEX => 'optional' ));
  501. if ($v_result != 1) {
  502. return 0;
  503. }
  504. $v_options[PCLZIP_OPT_BY_INDEX] = $v_options_trick[PCLZIP_OPT_BY_INDEX];
  505.  
  506. $this->privOptionDefaultThreshold($v_options);
  507.  
  508. if (($v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options)) < 1) {
  509. return(0);
  510. }
  511.  
  512. return $p_list;
  513. }
  514.  
  515. function delete()
  516. {
  517. $v_result=1;
  518. $this->privErrorReset();
  519.  
  520. if (!$this->privCheckFormat()) {
  521. return(0);
  522. }
  523.  
  524. $v_options = array();
  525. $v_size = func_num_args();
  526.  
  527. if ($v_size > 0) {
  528. $v_arg_list = func_get_args();
  529. $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
  530. array (PCLZIP_OPT_BY_NAME => 'optional',
  531. PCLZIP_OPT_BY_EREG => 'optional',
  532. PCLZIP_OPT_BY_PREG => 'optional',
  533. PCLZIP_OPT_BY_INDEX => 'optional' ));
  534. if ($v_result != 1) {
  535. return 0;
  536. }
  537. }
  538.  
  539. $this->privDisableMagicQuotes();
  540.  
  541. $v_list = array();
  542. if (($v_result = $this->privDeleteByRule($v_list, $v_options)) != 1) {
  543. $this->privSwapBackMagicQuotes();
  544. unset($v_list);
  545. return(0);
  546. }
  547.  
  548. $this->privSwapBackMagicQuotes();
  549.  
  550. return $v_list;
  551. }
  552.  
  553. function deleteByIndex($p_index)
  554. {
  555. $p_list = $this->delete(PCLZIP_OPT_BY_INDEX, $p_index);
  556.  
  557. return $p_list;
  558. }
  559.  
  560. function properties()
  561. {
  562.  
  563. $this->privErrorReset();
  564. $this->privDisableMagicQuotes();
  565.  
  566. if (!$this->privCheckFormat()) {
  567. $this->privSwapBackMagicQuotes();
  568. return(0);
  569. }
  570.  
  571. $v_prop = array();
  572. $v_prop['comment'] = '';
  573. $v_prop['nb'] = 0;
  574. $v_prop['status'] = 'not_exist';
  575.  
  576. if (@is_file($this->zipname))
  577. {
  578. if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
  579. {
  580. $this->privSwapBackMagicQuotes();
  581. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
  582. return 0;
  583. }
  584. $v_central_dir = array();
  585. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  586. {
  587. $this->privSwapBackMagicQuotes();
  588. return 0;
  589. }
  590. $this->privCloseFd();
  591. $v_prop['comment'] = $v_central_dir['comment'];
  592. $v_prop['nb'] = $v_central_dir['entries'];
  593. $v_prop['status'] = 'ok';
  594. }
  595.  
  596. $this->privSwapBackMagicQuotes();
  597.  
  598. return $v_prop;
  599. }
  600.  
  601. function duplicate($p_archive)
  602. {
  603. $v_result = 1;
  604. $this->privErrorReset();
  605.  
  606. if ((is_object($p_archive)) && (get_class($p_archive) == 'pclzip'))
  607. {
  608. $v_result = $this->privDuplicate($p_archive->zipname);
  609. }
  610. elseif (is_string($p_archive))
  611. {
  612. if (!is_file($p_archive)) {
  613. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "No file with filename '".$p_archive."'");
  614. $v_result = PCLZIP_ERR_MISSING_FILE;
  615. }
  616. else {
  617. $v_result = $this->privDuplicate($p_archive);
  618. }
  619. }
  620. else
  621. {
  622. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
  623. $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  624. }
  625.  
  626. return $v_result;
  627. }
  628.  
  629. function merge($p_archive_to_add)
  630. {
  631. $v_result = 1;
  632. $this->privErrorReset();
  633.  
  634. if (!$this->privCheckFormat()) {
  635. return(0);
  636. }
  637.  
  638. if ((is_object($p_archive_to_add)) && (get_class($p_archive_to_add) == 'pclzip'))
  639. {
  640. $v_result = $this->privMerge($p_archive_to_add);
  641. }
  642. else if (is_string($p_archive_to_add))
  643. {
  644. $v_object_archive = new PclZip($p_archive_to_add);
  645. $v_result = $this->privMerge($v_object_archive);
  646. }
  647. else
  648. {
  649. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
  650. $v_result = PCLZIP_ERR_INVALID_PARAMETER;
  651. }
  652.  
  653. return $v_result;
  654. }
  655.  
  656. function errorCode()
  657. {
  658. if (PCLZIP_ERROR_EXTERNAL == 1) {
  659. return(PclErrorCode());
  660. }
  661. else {
  662. return($this->error_code);
  663. }
  664. }
  665.  
  666. function errorName($p_with_code=false)
  667. {
  668. $v_name = array ( PCLZIP_ERR_NO_ERROR => 'PCLZIP_ERR_NO_ERROR',
  669. PCLZIP_ERR_WRITE_OPEN_FAIL => 'PCLZIP_ERR_WRITE_OPEN_FAIL',
  670. PCLZIP_ERR_READ_OPEN_FAIL => 'PCLZIP_ERR_READ_OPEN_FAIL',
  671. PCLZIP_ERR_INVALID_PARAMETER => 'PCLZIP_ERR_INVALID_PARAMETER',
  672. PCLZIP_ERR_MISSING_FILE => 'PCLZIP_ERR_MISSING_FILE',
  673. PCLZIP_ERR_FILENAME_TOO_LONG => 'PCLZIP_ERR_FILENAME_TOO_LONG',
  674. PCLZIP_ERR_INVALID_ZIP => 'PCLZIP_ERR_INVALID_ZIP',
  675. PCLZIP_ERR_BAD_EXTRACTED_FILE => 'PCLZIP_ERR_BAD_EXTRACTED_FILE',
  676. PCLZIP_ERR_DIR_CREATE_FAIL => 'PCLZIP_ERR_DIR_CREATE_FAIL',
  677. PCLZIP_ERR_BAD_EXTENSION => 'PCLZIP_ERR_BAD_EXTENSION',
  678. PCLZIP_ERR_BAD_FORMAT => 'PCLZIP_ERR_BAD_FORMAT',
  679. PCLZIP_ERR_DELETE_FILE_FAIL => 'PCLZIP_ERR_DELETE_FILE_FAIL',
  680. PCLZIP_ERR_RENAME_FILE_FAIL => 'PCLZIP_ERR_RENAME_FILE_FAIL',
  681. PCLZIP_ERR_BAD_CHECKSUM => 'PCLZIP_ERR_BAD_CHECKSUM',
  682. PCLZIP_ERR_INVALID_ARCHIVE_ZIP => 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP',
  683. PCLZIP_ERR_MISSING_OPTION_VALUE => 'PCLZIP_ERR_MISSING_OPTION_VALUE',
  684. PCLZIP_ERR_INVALID_OPTION_VALUE => 'PCLZIP_ERR_INVALID_OPTION_VALUE',
  685. PCLZIP_ERR_UNSUPPORTED_COMPRESSION => 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION',
  686. PCLZIP_ERR_UNSUPPORTED_ENCRYPTION => 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION'
  687. ,PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE => 'PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE'
  688. ,PCLZIP_ERR_DIRECTORY_RESTRICTION => 'PCLZIP_ERR_DIRECTORY_RESTRICTION'
  689. );
  690.  
  691. if (isset($v_name[$this->error_code])) {
  692. $v_value = $v_name[$this->error_code];
  693. }
  694. else {
  695. $v_value = 'NoName';
  696. }
  697.  
  698. if ($p_with_code) {
  699. return($v_value.' ('.$this->error_code.')');
  700. }
  701. else {
  702. return($v_value);
  703. }
  704. }
  705.  
  706. function errorInfo($p_full=false)
  707. {
  708. if (PCLZIP_ERROR_EXTERNAL == 1) {
  709. return(PclErrorString());
  710. }
  711. else {
  712. if ($p_full) {
  713. return($this->errorName(true)." : ".$this->error_string);
  714. }
  715. else {
  716. return($this->error_string." [code ".$this->error_code."]");
  717. }
  718. }
  719. }
  720.  
  721. function privCheckFormat($p_level=0)
  722. {
  723. $v_result = true;
  724. clearstatcache();
  725.  
  726. $this->privErrorReset();
  727.  
  728. if (!is_file($this->zipname)) {
  729. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "Missing archive file '".$this->zipname."'");
  730. return(false);
  731. }
  732.  
  733. if (!is_readable($this->zipname)) {
  734. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '".$this->zipname."'");
  735. return(false);
  736. }
  737.  
  738. return $v_result;
  739. }
  740.  
  741. function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options=false)
  742. {
  743. $v_result=1;
  744. $i=0;
  745. while ($i<$p_size) {
  746. if (!isset($v_requested_options[$p_options_list[$i]])) {
  747. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid optional parameter '".$p_options_list[$i]."' for this method");
  748. return PclZip::errorCode();
  749. }
  750.  
  751. switch ($p_options_list[$i]) {
  752. case PCLZIP_OPT_PATH :
  753. case PCLZIP_OPT_REMOVE_PATH :
  754. case PCLZIP_OPT_ADD_PATH :
  755. case PCLZIP_OPT_SUBSTITUE_FILE:
  756. if (($i+1) >= $p_size) {
  757.  
  758. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  759.  
  760. return PclZip::errorCode();
  761. }
  762. $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], FALSE);
  763. $i++;
  764. break;
  765.  
  766. case PCLZIP_OPT_TEMP_FILE_THRESHOLD :
  767. if (($i+1) >= $p_size) {
  768. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  769. return PclZip::errorCode();
  770. }
  771. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
  772. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
  773. return PclZip::errorCode();
  774. }
  775. $v_value = $p_options_list[$i+1];
  776. if ((!is_integer($v_value)) || ($v_value<0)) {
  777. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Integer expected for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  778. return PclZip::errorCode();
  779. }
  780. $v_result_list[$p_options_list[$i]] = $v_value*1048576;
  781. $i++;
  782. break;
  783. case PCLZIP_OPT_TEMP_FILE_ON :
  784. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
  785. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
  786. return PclZip::errorCode();
  787. }
  788. $v_result_list[$p_options_list[$i]] = true;
  789. break;
  790. case PCLZIP_OPT_TEMP_FILE_OFF :
  791. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_ON])) {
  792. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_ON'");
  793. return PclZip::errorCode();
  794. }
  795. if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
  796. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_THRESHOLD'");
  797. return PclZip::errorCode();
  798. }
  799. $v_result_list[$p_options_list[$i]] = true;
  800. break;
  801. case PCLZIP_OPT_EXTRACT_DIR_RESTRICTION :
  802. if (($i+1) >= $p_size) {
  803. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  804. return PclZip::errorCode();
  805. }
  806. if ( is_string($p_options_list[$i+1])
  807. && ($p_options_list[$i+1] != '')) {
  808. $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], FALSE);
  809. $i++;
  810. }
  811. else {
  812. }
  813. break;
  814. case PCLZIP_OPT_BY_NAME :
  815. if (($i+1) >= $p_size) {
  816.  
  817. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  818.  
  819. return PclZip::errorCode();
  820. }
  821.  
  822. if (is_string($p_options_list[$i+1])) {
  823. $v_result_list[$p_options_list[$i]][0] = $p_options_list[$i+1];
  824. }
  825. else if (is_array($p_options_list[$i+1])) {
  826. $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
  827. }
  828. else {
  829. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  830.  
  831. return PclZip::errorCode();
  832. }
  833. $i++;
  834. break;
  835.  
  836. case PCLZIP_OPT_BY_EREG :
  837.  
  838. $p_options_list[$i] = PCLZIP_OPT_BY_PREG;
  839. case PCLZIP_OPT_BY_PREG :
  840.  
  841. if (($i+1) >= $p_size) {
  842. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  843.  
  844. return PclZip::errorCode();
  845. }
  846.  
  847. if (is_string($p_options_list[$i+1])) {
  848. $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
  849. }
  850. else {
  851. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  852.  
  853. return PclZip::errorCode();
  854. }
  855. $i++;
  856. break;
  857.  
  858. case PCLZIP_OPT_COMMENT :
  859. case PCLZIP_OPT_ADD_COMMENT :
  860. case PCLZIP_OPT_PREPEND_COMMENT :
  861.  
  862. if (($i+1) >= $p_size) {
  863. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE,
  864. "Missing parameter value for option '"
  865. .PclZipUtilOptionText($p_options_list[$i])
  866. ."'");
  867.  
  868. return PclZip::errorCode();
  869. }
  870.  
  871. if (is_string($p_options_list[$i+1])) {
  872. $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
  873. }
  874. else {
  875.  
  876. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE,
  877. "Wrong parameter value for option '"
  878. .PclZipUtilOptionText($p_options_list[$i])
  879. ."'");
  880.  
  881. return PclZip::errorCode();
  882. }
  883. $i++;
  884. break;
  885.  
  886. case PCLZIP_OPT_BY_INDEX :
  887.  
  888. if (($i+1) >= $p_size) {
  889.  
  890. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  891.  
  892. return PclZip::errorCode();
  893. }
  894.  
  895. $v_work_list = array();
  896. if (is_string($p_options_list[$i+1])) {
  897.  
  898. $p_options_list[$i+1] = strtr($p_options_list[$i+1], ' ', '');
  899.  
  900. $v_work_list = explode(",", $p_options_list[$i+1]);
  901. }
  902. else if (is_integer($p_options_list[$i+1])) {
  903. $v_work_list[0] = $p_options_list[$i+1].'-'.$p_options_list[$i+1];
  904. }
  905. else if (is_array($p_options_list[$i+1])) {
  906. $v_work_list = $p_options_list[$i+1];
  907. }
  908. else {
  909. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Value must be integer, string or array for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  910.  
  911. return PclZip::errorCode();
  912. }
  913.  
  914. $v_sort_flag=false;
  915. $v_sort_value=0;
  916. for ($j=0; $j<sizeof($v_work_list); $j++) {
  917. $v_item_list = explode("-", $v_work_list[$j]);
  918. $v_size_item_list = sizeof($v_item_list);
  919.  
  920. if ($v_size_item_list == 1) {
  921.  
  922. $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
  923. $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[0];
  924. }
  925. elseif ($v_size_item_list == 2) {
  926. $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
  927. $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[1];
  928. }
  929. else {
  930. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Too many values in index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  931.  
  932. return PclZip::errorCode();
  933. }
  934.  
  935. if ($v_result_list[$p_options_list[$i]][$j]['start'] < $v_sort_value) {
  936. $v_sort_flag=true;
  937.  
  938. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Invalid order of index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  939.  
  940. return PclZip::errorCode();
  941. }
  942. $v_sort_value = $v_result_list[$p_options_list[$i]][$j]['start'];
  943. }
  944. if ($v_sort_flag) {
  945. }
  946.  
  947. $i++;
  948. break;
  949.  
  950. case PCLZIP_OPT_REMOVE_ALL_PATH :
  951. case PCLZIP_OPT_EXTRACT_AS_STRING :
  952. case PCLZIP_OPT_NO_COMPRESSION :
  953. case PCLZIP_OPT_EXTRACT_IN_OUTPUT :
  954. case PCLZIP_OPT_REPLACE_NEWER :
  955. case PCLZIP_OPT_STOP_ON_ERROR :
  956. $v_result_list[$p_options_list[$i]] = true;
  957. break;
  958.  
  959. case PCLZIP_OPT_SET_CHMOD :
  960.  
  961. if (($i+1) >= $p_size) {
  962.  
  963. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  964.  
  965. return PclZip::errorCode();
  966. }
  967.  
  968. $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
  969. $i++;
  970. break;
  971.  
  972. case PCLZIP_CB_PRE_EXTRACT :
  973. case PCLZIP_CB_POST_EXTRACT :
  974. case PCLZIP_CB_PRE_ADD :
  975. case PCLZIP_CB_POST_ADD :
  976.  
  977. if (($i+1) >= $p_size) {
  978.  
  979. PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  980.  
  981. return PclZip::errorCode();
  982. }
  983.  
  984. $v_function_name = $p_options_list[$i+1];
  985.  
  986. if (!function_exists($v_function_name)) {
  987.  
  988. PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Function '".$v_function_name."()' is not an existing function for option '".PclZipUtilOptionText($p_options_list[$i])."'");
  989.  
  990. return PclZip::errorCode();
  991. }
  992.  
  993. $v_result_list[$p_options_list[$i]] = $v_function_name;
  994. $i++;
  995. break;
  996.  
  997. default :
  998. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
  999. "Unknown parameter '"
  1000. .$p_options_list[$i]."'");
  1001.  
  1002. return PclZip::errorCode();
  1003. }
  1004.  
  1005. $i++;
  1006. }
  1007.  
  1008. if ($v_requested_options !== false) {
  1009. for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
  1010. if ($v_requested_options[$key] == 'mandatory') {
  1011. if (!isset($v_result_list[$key])) {
  1012. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
  1013.  
  1014. return PclZip::errorCode();
  1015. }
  1016. }
  1017. }
  1018. }
  1019. if (!isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
  1020. }
  1021.  
  1022. return $v_result;
  1023. }
  1024.  
  1025. function privOptionDefaultThreshold(&$p_options)
  1026. {
  1027. $v_result=1;
  1028. if (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
  1029. || isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF])) {
  1030. return $v_result;
  1031. }
  1032.  
  1033. $v_memory_limit = ini_get('memory_limit');
  1034. $v_memory_limit = trim($v_memory_limit);
  1035. $last = strtolower(substr($v_memory_limit, -1));
  1036. if($last == 'g')
  1037. $v_memory_limit = $v_memory_limit*1073741824;
  1038. if($last == 'm')
  1039. $v_memory_limit = $v_memory_limit*1048576;
  1040. if($last == 'k')
  1041. $v_memory_limit = $v_memory_limit*1024;
  1042. $p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] = floor($v_memory_limit*PCLZIP_TEMPORARY_FILE_RATIO);
  1043.  
  1044. if ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] < 1048576) {
  1045. unset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]);
  1046. }
  1047. return $v_result;
  1048. }
  1049.  
  1050. function privFileDescrParseAtt(&$p_file_list, &$p_filedescr, $v_options, $v_requested_options=false)
  1051. {
  1052. $v_result=1;
  1053. foreach ($p_file_list as $v_key => $v_value) {
  1054. if (!isset($v_requested_options[$v_key])) {
  1055. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file attribute '".$v_key."' for this file");
  1056.  
  1057. return PclZip::errorCode();
  1058. }
  1059.  
  1060. switch ($v_key) {
  1061. case PCLZIP_ATT_FILE_NAME :
  1062. if (!is_string($v_value)) {
  1063. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
  1064. return PclZip::errorCode();
  1065. }
  1066.  
  1067. $p_filedescr['filename'] = PclZipUtilPathReduction($v_value);
  1068. if ($p_filedescr['filename'] == '') {
  1069. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty filename for attribute '".PclZipUtilOptionText($v_key)."'");
  1070. return PclZip::errorCode();
  1071. }
  1072.  
  1073. break;
  1074.  
  1075. case PCLZIP_ATT_FILE_NEW_SHORT_NAME :
  1076. if (!is_string($v_value)) {
  1077. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
  1078. return PclZip::errorCode();
  1079. }
  1080.  
  1081. $p_filedescr['new_short_name'] = PclZipUtilPathReduction($v_value);
  1082.  
  1083. if ($p_filedescr['new_short_name'] == '') {
  1084. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty short filename for attribute '".PclZipUtilOptionText($v_key)."'");
  1085. return PclZip::errorCode();
  1086. }
  1087. break;
  1088.  
  1089. case PCLZIP_ATT_FILE_NEW_FULL_NAME :
  1090. if (!is_string($v_value)) {
  1091. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
  1092. return PclZip::errorCode();
  1093. }
  1094.  
  1095. $p_filedescr['new_full_name'] = PclZipUtilPathReduction($v_value);
  1096.  
  1097. if ($p_filedescr['new_full_name'] == '') {
  1098. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty full filename for attribute '".PclZipUtilOptionText($v_key)."'");
  1099. return PclZip::errorCode();
  1100. }
  1101. break;
  1102.  
  1103. case PCLZIP_ATT_FILE_COMMENT :
  1104. if (!is_string($v_value)) {
  1105. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
  1106. return PclZip::errorCode();
  1107. }
  1108.  
  1109. $p_filedescr['comment'] = $v_value;
  1110. break;
  1111.  
  1112. case PCLZIP_ATT_FILE_MTIME :
  1113. if (!is_integer($v_value)) {
  1114. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". Integer expected for attribute '".PclZipUtilOptionText($v_key)."'");
  1115. return PclZip::errorCode();
  1116. }
  1117.  
  1118. $p_filedescr['mtime'] = $v_value;
  1119. break;
  1120.  
  1121. case PCLZIP_ATT_FILE_CONTENT :
  1122. $p_filedescr['content'] = $v_value;
  1123. break;
  1124.  
  1125. default :
  1126. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
  1127. "Unknown parameter '".$v_key."'");
  1128.  
  1129. return PclZip::errorCode();
  1130. }
  1131.  
  1132. if ($v_requested_options !== false) {
  1133. for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
  1134. if ($v_requested_options[$key] == 'mandatory') {
  1135. if (!isset($p_file_list[$key])) {
  1136. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
  1137. return PclZip::errorCode();
  1138. }
  1139. }
  1140. }
  1141. }
  1142. }
  1143. return $v_result;
  1144. }
  1145.  
  1146. function privFileDescrExpand(&$p_filedescr_list, &$p_options)
  1147. {
  1148. $v_result=1;
  1149. $v_result_list = array();
  1150. for ($i=0; $i<sizeof($p_filedescr_list); $i++) {
  1151. $v_descr = $p_filedescr_list[$i];
  1152. $v_descr['filename'] = PclZipUtilTranslateWinPath($v_descr['filename'], false);
  1153. $v_descr['filename'] = PclZipUtilPathReduction($v_descr['filename']);
  1154. if (file_exists($v_descr['filename'])) {
  1155. if (@is_file($v_descr['filename'])) {
  1156. $v_descr['type'] = 'file';
  1157. }
  1158. else if (@is_dir($v_descr['filename'])) {
  1159. $v_descr['type'] = 'folder';
  1160. }
  1161. else if (@is_link($v_descr['filename'])) {
  1162. continue;
  1163. }
  1164. else {
  1165. continue;
  1166. }
  1167. }
  1168. else if (isset($v_descr['content'])) {
  1169. $v_descr['type'] = 'virtual_file';
  1170. }
  1171. else {
  1172. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '".$v_descr['filename']."' does not exist");
  1173.  
  1174. return PclZip::errorCode();
  1175. }
  1176. $this->privCalculateStoredFilename($v_descr, $p_options);
  1177. $v_result_list[sizeof($v_result_list)] = $v_descr;
  1178. if ($v_descr['type'] == 'folder') {
  1179. $v_dirlist_descr = array();
  1180. $v_dirlist_nb = 0;
  1181. if ($v_folder_handler = @opendir($v_descr['filename'])) {
  1182. while (($v_item_handler = @readdir($v_folder_handler)) !== false) {
  1183.  
  1184. if (($v_item_handler == '.') || ($v_item_handler == '..')) {
  1185. continue;
  1186. }
  1187. $v_dirlist_descr[$v_dirlist_nb]['filename'] = $v_descr['filename'].'/'.$v_item_handler;
  1188.  
  1189. if (($v_descr['stored_filename'] != $v_descr['filename'])
  1190. && (!isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH]))) {
  1191. if ($v_descr['stored_filename'] != '') {
  1192. $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_descr['stored_filename'].'/'.$v_item_handler;
  1193. }
  1194. else {
  1195. $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_item_handler;
  1196. }
  1197. }
  1198. $v_dirlist_nb++;
  1199. }
  1200. @closedir($v_folder_handler);
  1201. }
  1202. else {
  1203. }
  1204. if ($v_dirlist_nb != 0) {
  1205. if (($v_result = $this->privFileDescrExpand($v_dirlist_descr, $p_options)) != 1) {
  1206. return $v_result;
  1207. }
  1208. $v_result_list = array_merge($v_result_list, $v_dirlist_descr);
  1209. }
  1210. else {
  1211. }
  1212. unset($v_dirlist_descr);
  1213. }
  1214. }
  1215. $p_filedescr_list = $v_result_list;
  1216.  
  1217. return $v_result;
  1218. }
  1219.  
  1220. function privCreate($p_filedescr_list, &$p_result_list, &$p_options)
  1221. {
  1222. $v_result=1;
  1223. $v_list_detail = array();
  1224. $this->privDisableMagicQuotes();
  1225.  
  1226. if (($v_result = $this->privOpenFd('wb')) != 1)
  1227. {
  1228. return $v_result;
  1229. }
  1230.  
  1231. $v_result = $this->privAddList($p_filedescr_list, $p_result_list, $p_options);
  1232.  
  1233. $this->privCloseFd();
  1234.  
  1235. $this->privSwapBackMagicQuotes();
  1236.  
  1237. return $v_result;
  1238. }
  1239.  
  1240. function privAdd($p_filedescr_list, &$p_result_list, &$p_options)
  1241. {
  1242. $v_result=1;
  1243. $v_list_detail = array();
  1244.  
  1245. if ((!is_file($this->zipname)) || (filesize($this->zipname) == 0))
  1246. {
  1247.  
  1248. $v_result = $this->privCreate($p_filedescr_list, $p_result_list, $p_options);
  1249.  
  1250. return $v_result;
  1251. }
  1252. $this->privDisableMagicQuotes();
  1253.  
  1254. if (($v_result=$this->privOpenFd('rb')) != 1)
  1255. {
  1256. $this->privSwapBackMagicQuotes();
  1257.  
  1258. return $v_result;
  1259. }
  1260.  
  1261. $v_central_dir = array();
  1262. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  1263. {
  1264. $this->privCloseFd();
  1265. $this->privSwapBackMagicQuotes();
  1266. return $v_result;
  1267. }
  1268.  
  1269. @rewind($this->zip_fd);
  1270.  
  1271. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
  1272.  
  1273. if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
  1274. {
  1275. $this->privCloseFd();
  1276. $this->privSwapBackMagicQuotes();
  1277.  
  1278. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
  1279.  
  1280. return PclZip::errorCode();
  1281. }
  1282.  
  1283. $v_size = $v_central_dir['offset'];
  1284. while ($v_size != 0)
  1285. {
  1286. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1287. $v_buffer = fread($this->zip_fd, $v_read_size);
  1288. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  1289. $v_size -= $v_read_size;
  1290. }
  1291.  
  1292. $v_swap = $this->zip_fd;
  1293. $this->zip_fd = $v_zip_temp_fd;
  1294. $v_zip_temp_fd = $v_swap;
  1295.  
  1296. $v_header_list = array();
  1297. if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1)
  1298. {
  1299. fclose($v_zip_temp_fd);
  1300. $this->privCloseFd();
  1301. @unlink($v_zip_temp_name);
  1302. $this->privSwapBackMagicQuotes();
  1303.  
  1304. return $v_result;
  1305. }
  1306.  
  1307. $v_offset = @ftell($this->zip_fd);
  1308.  
  1309. $v_size = $v_central_dir['size'];
  1310. while ($v_size != 0)
  1311. {
  1312. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1313. $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
  1314. @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  1315. $v_size -= $v_read_size;
  1316. }
  1317.  
  1318. for ($i=0, $v_count=0; $i<sizeof($v_header_list); $i++)
  1319. {
  1320. if ($v_header_list[$i]['status'] == 'ok') {
  1321. if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  1322. fclose($v_zip_temp_fd);
  1323. $this->privCloseFd();
  1324. @unlink($v_zip_temp_name);
  1325. $this->privSwapBackMagicQuotes();
  1326.  
  1327. return $v_result;
  1328. }
  1329. $v_count++;
  1330. }
  1331.  
  1332. $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  1333. }
  1334.  
  1335. $v_comment = $v_central_dir['comment'];
  1336. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  1337. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  1338. }
  1339. if (isset($p_options[PCLZIP_OPT_ADD_COMMENT])) {
  1340. $v_comment = $v_comment.$p_options[PCLZIP_OPT_ADD_COMMENT];
  1341. }
  1342. if (isset($p_options[PCLZIP_OPT_PREPEND_COMMENT])) {
  1343. $v_comment = $p_options[PCLZIP_OPT_PREPEND_COMMENT].$v_comment;
  1344. }
  1345.  
  1346. $v_size = @ftell($this->zip_fd)-$v_offset;
  1347.  
  1348. if (($v_result = $this->privWriteCentralHeader($v_count+$v_central_dir['entries'], $v_size, $v_offset, $v_comment)) != 1)
  1349. {
  1350. unset($v_header_list);
  1351. $this->privSwapBackMagicQuotes();
  1352.  
  1353. return $v_result;
  1354. }
  1355.  
  1356. $v_swap = $this->zip_fd;
  1357. $this->zip_fd = $v_zip_temp_fd;
  1358. $v_zip_temp_fd = $v_swap;
  1359. $this->privCloseFd();
  1360. @fclose($v_zip_temp_fd);
  1361. $this->privSwapBackMagicQuotes();
  1362. @unlink($this->zipname);
  1363.  
  1364. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  1365.  
  1366. return $v_result;
  1367. }
  1368.  
  1369. function privOpenFd($p_mode)
  1370. {
  1371. $v_result=1;
  1372.  
  1373. if ($this->zip_fd != 0)
  1374. {
  1375.  
  1376. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Zip file \''.$this->zipname.'\' already open');
  1377.  
  1378. return PclZip::errorCode();
  1379. }
  1380.  
  1381. if (($this->zip_fd = @fopen($this->zipname, $p_mode)) == 0)
  1382. {
  1383.  
  1384. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in '.$p_mode.' mode');
  1385.  
  1386. return PclZip::errorCode();
  1387. }
  1388.  
  1389. return $v_result;
  1390. }
  1391.  
  1392. function privCloseFd()
  1393. {
  1394. $v_result=1;
  1395.  
  1396. if ($this->zip_fd != 0)
  1397. @fclose($this->zip_fd);
  1398. $this->zip_fd = 0;
  1399.  
  1400. return $v_result;
  1401. }
  1402.  
  1403. function privAddList($p_filedescr_list, &$p_result_list, &$p_options)
  1404. {
  1405. $v_result=1;
  1406.  
  1407. $v_header_list = array();
  1408. if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1)
  1409. {
  1410. return $v_result;
  1411. }
  1412.  
  1413. $v_offset = @ftell($this->zip_fd);
  1414.  
  1415. for ($i=0,$v_count=0; $i<sizeof($v_header_list); $i++)
  1416. {
  1417. if ($v_header_list[$i]['status'] == 'ok') {
  1418. if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  1419. return $v_result;
  1420. }
  1421. $v_count++;
  1422. }
  1423.  
  1424. $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  1425. }
  1426.  
  1427. $v_comment = '';
  1428. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  1429. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  1430. }
  1431.  
  1432. $v_size = @ftell($this->zip_fd)-$v_offset;
  1433.  
  1434. if (($v_result = $this->privWriteCentralHeader($v_count, $v_size, $v_offset, $v_comment)) != 1)
  1435. {
  1436. unset($v_header_list);
  1437.  
  1438. return $v_result;
  1439. }
  1440.  
  1441. return $v_result;
  1442. }
  1443.  
  1444. function privAddFileList($p_filedescr_list, &$p_result_list, &$p_options)
  1445. {
  1446. $v_result=1;
  1447. $v_header = array();
  1448.  
  1449. $v_nb = sizeof($p_result_list);
  1450.  
  1451. for ($j=0; ($j<sizeof($p_filedescr_list)) && ($v_result==1); $j++) {
  1452. $p_filedescr_list[$j]['filename']
  1453. = PclZipUtilTranslateWinPath($p_filedescr_list[$j]['filename'], false);
  1454.  
  1455. if ($p_filedescr_list[$j]['filename'] == "") {
  1456. continue;
  1457. }
  1458.  
  1459. if ( ($p_filedescr_list[$j]['type'] != 'virtual_file')
  1460. && (!file_exists($p_filedescr_list[$j]['filename']))) {
  1461. PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '".$p_filedescr_list[$j]['filename']."' does not exist");
  1462. return PclZip::errorCode();
  1463. }
  1464.  
  1465.  
  1466. if ( ($p_filedescr_list[$j]['type'] == 'file')
  1467. || ($p_filedescr_list[$j]['type'] == 'virtual_file')
  1468. || ( ($p_filedescr_list[$j]['type'] == 'folder')
  1469. && ( !isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])
  1470. || !$p_options[PCLZIP_OPT_REMOVE_ALL_PATH]))
  1471. ) {
  1472.  
  1473. $v_result = $this->privAddFile($p_filedescr_list[$j], $v_header,
  1474. $p_options);
  1475. if ($v_result != 1) {
  1476. return $v_result;
  1477. }
  1478.  
  1479. $p_result_list[$v_nb++] = $v_header;
  1480. }
  1481. }
  1482.  
  1483. return $v_result;
  1484. }
  1485.  
  1486. function privAddFile($p_filedescr, &$p_header, &$p_options)
  1487. {
  1488. $v_result=1;
  1489. $p_filename = $p_filedescr['filename'];
  1490.  
  1491. if ($p_filename == "") {
  1492. PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
  1493.  
  1494. return PclZip::errorCode();
  1495. }
  1496. clearstatcache();
  1497. $p_header['version'] = 20;
  1498. $p_header['version_extracted'] = 10;
  1499. $p_header['flag'] = 0;
  1500. $p_header['compression'] = 0;
  1501. $p_header['crc'] = 0;
  1502. $p_header['compressed_size'] = 0;
  1503. $p_header['filename_len'] = strlen($p_filename);
  1504. $p_header['extra_len'] = 0;
  1505. $p_header['disk'] = 0;
  1506. $p_header['internal'] = 0;
  1507. $p_header['offset'] = 0;
  1508. $p_header['filename'] = $p_filename;
  1509. $p_header['stored_filename'] = $p_filedescr['stored_filename'];
  1510. $p_header['extra'] = '';
  1511. $p_header['status'] = 'ok';
  1512. $p_header['index'] = -1;
  1513.  
  1514.  
  1515. /*
  1516. * ----
  1517. */
  1518.  
  1519. if ($p_filedescr['type'] == 'virtual_file') {
  1520. $p_header['external'] = 0x00000000;
  1521. $p_header['size'] = strlen($p_filedescr['content']);
  1522. }
  1523. else if ($p_filedescr['type']=='folder') {
  1524. $p_header['external'] = 0x00000010;
  1525. $p_header['mtime'] = filemtime($p_filename);
  1526. $p_header['size'] = filesize($p_filename);
  1527. }
  1528. else
  1529. {
  1530. $p_header['external'] = 0x00000000;
  1531. $p_header['size'] = filesize($p_filename);
  1532. }
  1533.  
  1534. if (isset($p_filedescr['mtime'])) {
  1535. $p_header['mtime'] = $p_filedescr['mtime'];
  1536. }
  1537. else if ($p_filedescr['type'] == 'virtual_file') {
  1538. $p_header['mtime'] = time();
  1539. }
  1540. else {
  1541. $p_header['mtime'] = filemtime($p_filename);
  1542. }
  1543.  
  1544. if (isset($p_filedescr['comment'])) {
  1545. $p_header['comment_len'] = strlen($p_filedescr['comment']);
  1546. $p_header['comment'] = $p_filedescr['comment'];
  1547. }
  1548. else {
  1549. $p_header['comment_len'] = 0;
  1550. $p_header['comment'] = '';
  1551. }
  1552.  
  1553. if (isset($p_options[PCLZIP_CB_PRE_ADD])) {
  1554.  
  1555. $v_local_header = array();
  1556. $this->privConvertHeader2FileInfo($p_header, $v_local_header);
  1557.  
  1558. $v_result = $p_options[PCLZIP_CB_PRE_ADD](PCLZIP_CB_PRE_ADD, $v_local_header);
  1559. if ($v_result == 0) {
  1560. $p_header['status'] = "skipped";
  1561. $v_result = 1;
  1562. }
  1563.  
  1564. if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
  1565. $p_header['stored_filename'] = PclZipUtilPathReduction($v_local_header['stored_filename']);
  1566. }
  1567. }
  1568.  
  1569. if ($p_header['stored_filename'] == "") {
  1570. $p_header['status'] = "filtered";
  1571. }
  1572. if (strlen($p_header['stored_filename']) > 0xFF) {
  1573. $p_header['status'] = 'filename_too_long';
  1574. }
  1575.  
  1576. if ($p_header['status'] == 'ok') {
  1577.  
  1578. if ($p_filedescr['type'] == 'file') {
  1579. if ( (!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF]))
  1580. && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON])
  1581. || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
  1582. && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_header['size'])) ) ) {
  1583. $v_result = $this->privAddFileUsingTempFile($p_filedescr, $p_header, $p_options);
  1584. if ($v_result < PCLZIP_ERR_NO_ERROR) {
  1585. return $v_result;
  1586. }
  1587. }
  1588. else {
  1589.  
  1590. if (($v_file = @fopen($p_filename, "rb")) == 0) {
  1591. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
  1592. return PclZip::errorCode();
  1593. }
  1594.  
  1595. $v_content = file_get_contents($p_filename);//fread($v_file, $p_header['size']);
  1596.  
  1597. @fclose($v_file);
  1598.  
  1599. $p_header['crc'] = @crc32($v_content);
  1600. if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
  1601. $p_header['compressed_size'] = $p_header['size'];
  1602. $p_header['compression'] = 0;
  1603. }
  1604. else {
  1605. $v_content = @gzdeflate($v_content);
  1606.  
  1607. $p_header['compressed_size'] = strlen($v_content);
  1608. $p_header['compression'] = 8;
  1609. }
  1610. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  1611. @fclose($v_file);
  1612. return $v_result;
  1613. }
  1614.  
  1615. @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
  1616.  
  1617. }
  1618.  
  1619. }
  1620.  
  1621. else if ($p_filedescr['type'] == 'virtual_file') {
  1622. $v_content = $p_filedescr['content'];
  1623.  
  1624. $p_header['crc'] = @crc32($v_content);
  1625. if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
  1626. $p_header['compressed_size'] = $p_header['size'];
  1627. $p_header['compression'] = 0;
  1628. }
  1629. else {
  1630. $v_content = @gzdeflate($v_content);
  1631.  
  1632. $p_header['compressed_size'] = strlen($v_content);
  1633. $p_header['compression'] = 8;
  1634. }
  1635. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  1636. @fclose($v_file);
  1637. return $v_result;
  1638. }
  1639.  
  1640. @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
  1641. }
  1642.  
  1643. else if ($p_filedescr['type'] == 'folder') {
  1644. if (@substr($p_header['stored_filename'], -1) != '/') {
  1645. $p_header['stored_filename'] .= '/';
  1646. }
  1647.  
  1648. $p_header['size'] = 0;
  1649. $p_header['external'] = 0x00000010;
  1650. if (($v_result = $this->privWriteFileHeader($p_header)) != 1)
  1651. {
  1652. return $v_result;
  1653. }
  1654. }
  1655. }
  1656.  
  1657. if (isset($p_options[PCLZIP_CB_POST_ADD])) {
  1658.  
  1659. $v_local_header = array();
  1660. $this->privConvertHeader2FileInfo($p_header, $v_local_header);
  1661.  
  1662. $v_result = $p_options[PCLZIP_CB_POST_ADD](PCLZIP_CB_POST_ADD, $v_local_header);
  1663. if ($v_result == 0) {
  1664. $v_result = 1;
  1665. }
  1666.  
  1667. }
  1668.  
  1669. return $v_result;
  1670. }
  1671.  
  1672. function privAddFileUsingTempFile($p_filedescr, &$p_header, &$p_options)
  1673. {
  1674. $v_result=PCLZIP_ERR_NO_ERROR;
  1675. $p_filename = $p_filedescr['filename'];
  1676.  
  1677.  
  1678. if (($v_file = @fopen($p_filename, "rb")) == 0) {
  1679. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
  1680. return PclZip::errorCode();
  1681. }
  1682.  
  1683. $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.gz';
  1684. if (($v_file_compressed = @gzopen($v_gzip_temp_name, "wb")) == 0) {
  1685. fclose($v_file);
  1686. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary write mode');
  1687. return PclZip::errorCode();
  1688. }
  1689.  
  1690. $v_size = filesize($p_filename);
  1691. while ($v_size != 0) {
  1692. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1693. $v_buffer = @fread($v_file, $v_read_size);
  1694. @gzputs($v_file_compressed, $v_buffer, $v_read_size);
  1695. $v_size -= $v_read_size;
  1696. }
  1697.  
  1698. @fclose($v_file);
  1699. @gzclose($v_file_compressed);
  1700.  
  1701. if (filesize($v_gzip_temp_name) < 18) {
  1702. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'gzip temporary file \''.$v_gzip_temp_name.'\' has invalid filesize - should be minimum 18 bytes');
  1703. return PclZip::errorCode();
  1704. }
  1705.  
  1706. if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0) {
  1707. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
  1708. return PclZip::errorCode();
  1709. }
  1710.  
  1711. $v_binary_data = @fread($v_file_compressed, 10);
  1712. $v_data_header = unpack('a1id1/a1id2/a1cm/a1flag/Vmtime/a1xfl/a1os', $v_binary_data);
  1713.  
  1714. $v_data_header['os'] = bin2hex($v_data_header['os']);
  1715.  
  1716. @fseek($v_file_compressed, filesize($v_gzip_temp_name)-8);
  1717. $v_binary_data = @fread($v_file_compressed, 8);
  1718. $v_data_footer = unpack('Vcrc/Vcompressed_size', $v_binary_data);
  1719.  
  1720. $p_header['compression'] = ord($v_data_header['cm']);
  1721. $p_header['crc'] = $v_data_footer['crc'];
  1722. $p_header['compressed_size'] = filesize($v_gzip_temp_name)-18;
  1723.  
  1724. @fclose($v_file_compressed);
  1725.  
  1726. if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
  1727. return $v_result;
  1728. }
  1729.  
  1730. if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0)
  1731. {
  1732. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
  1733. return PclZip::errorCode();
  1734. }
  1735.  
  1736. fseek($v_file_compressed, 10);
  1737. $v_size = $p_header['compressed_size'];
  1738. while ($v_size != 0)
  1739. {
  1740. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  1741. $v_buffer = @fread($v_file_compressed, $v_read_size);
  1742. @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  1743. $v_size -= $v_read_size;
  1744. }
  1745.  
  1746. @fclose($v_file_compressed);
  1747.  
  1748. @unlink($v_gzip_temp_name);
  1749. return $v_result;
  1750. }
  1751.  
  1752. function privCalculateStoredFilename(&$p_filedescr, &$p_options)
  1753. {
  1754. $v_result=1;
  1755. $p_filename = $p_filedescr['filename'];
  1756. if (isset($p_options[PCLZIP_OPT_ADD_PATH])) {
  1757. $p_add_dir = $p_options[PCLZIP_OPT_ADD_PATH];
  1758. }
  1759. else {
  1760. $p_add_dir = '';
  1761. }
  1762. if (isset($p_options[PCLZIP_OPT_REMOVE_PATH])) {
  1763. $p_remove_dir = $p_options[PCLZIP_OPT_REMOVE_PATH];
  1764. }
  1765. else {
  1766. $p_remove_dir = '';
  1767. }
  1768. if (isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
  1769. $p_remove_all_dir = $p_options[PCLZIP_OPT_REMOVE_ALL_PATH];
  1770. }
  1771. else {
  1772. $p_remove_all_dir = 0;
  1773. }
  1774.  
  1775.  
  1776. if (isset($p_filedescr['new_full_name'])) {
  1777. $v_stored_filename = PclZipUtilTranslateWinPath($p_filedescr['new_full_name']);
  1778. }
  1779. else {
  1780.  
  1781. if (isset($p_filedescr['new_short_name'])) {
  1782. $v_path_info = pathinfo($p_filename);
  1783. $v_dir = '';
  1784. if ($v_path_info['dirname'] != '') {
  1785. $v_dir = $v_path_info['dirname'].'/';
  1786. }
  1787. $v_stored_filename = $v_dir.$p_filedescr['new_short_name'];
  1788. }
  1789. else {
  1790. $v_stored_filename = $p_filename;
  1791. }
  1792.  
  1793. if ($p_remove_all_dir) {
  1794. $v_stored_filename = basename($p_filename);
  1795. }
  1796. else if ($p_remove_dir != "") {
  1797. if (substr($p_remove_dir, -1) != '/')
  1798. $p_remove_dir .= "/";
  1799.  
  1800. if ( (substr($p_filename, 0, 2) == "./")
  1801. || (substr($p_remove_dir, 0, 2) == "./")) {
  1802. if ( (substr($p_filename, 0, 2) == "./")
  1803. && (substr($p_remove_dir, 0, 2) != "./")) {
  1804. $p_remove_dir = "./".$p_remove_dir;
  1805. }
  1806. if ( (substr($p_filename, 0, 2) != "./")
  1807. && (substr($p_remove_dir, 0, 2) == "./")) {
  1808. $p_remove_dir = substr($p_remove_dir, 2);
  1809. }
  1810. }
  1811.  
  1812. $v_compare = PclZipUtilPathInclusion($p_remove_dir,
  1813. $v_stored_filename);
  1814. if ($v_compare > 0) {
  1815. if ($v_compare == 2) {
  1816. $v_stored_filename = "";
  1817. }
  1818. else {
  1819. $v_stored_filename = substr($v_stored_filename,
  1820. strlen($p_remove_dir));
  1821. }
  1822. }
  1823. }
  1824. $v_stored_filename = PclZipUtilTranslateWinPath($v_stored_filename);
  1825. if ($p_add_dir != "") {
  1826. if (substr($p_add_dir, -1) == "/")
  1827. $v_stored_filename = $p_add_dir.$v_stored_filename;
  1828. else
  1829. $v_stored_filename = $p_add_dir."/".$v_stored_filename;
  1830. }
  1831. }
  1832.  
  1833. $v_stored_filename = PclZipUtilPathReduction($v_stored_filename);
  1834. $p_filedescr['stored_filename'] = $v_stored_filename;
  1835. return $v_result;
  1836. }
  1837.  
  1838. function privWriteFileHeader(&$p_header)
  1839. {
  1840. $v_result=1;
  1841.  
  1842. $p_header['offset'] = ftell($this->zip_fd);
  1843.  
  1844. $v_date = getdate($p_header['mtime']);
  1845. $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
  1846. $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
  1847.  
  1848. $v_binary_data = pack("VvvvvvVVVvv", 0x04034b50,
  1849. $p_header['version_extracted'], $p_header['flag'],
  1850. $p_header['compression'], $v_mtime, $v_mdate,
  1851. $p_header['crc'], $p_header['compressed_size'],
  1852. $p_header['size'],
  1853. strlen($p_header['stored_filename']),
  1854. $p_header['extra_len']);
  1855.  
  1856. fputs($this->zip_fd, $v_binary_data, 30);
  1857.  
  1858. if (strlen($p_header['stored_filename']) != 0)
  1859. {
  1860. fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
  1861. }
  1862. if ($p_header['extra_len'] != 0)
  1863. {
  1864. fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
  1865. }
  1866.  
  1867. return $v_result;
  1868. }
  1869.  
  1870. function privWriteCentralFileHeader(&$p_header)
  1871. {
  1872. $v_result=1;
  1873.  
  1874. $v_date = getdate($p_header['mtime']);
  1875. $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
  1876. $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
  1877.  
  1878.  
  1879. $v_binary_data = pack("VvvvvvvVVVvvvvvVV", 0x02014b50,
  1880. $p_header['version'], $p_header['version_extracted'],
  1881. $p_header['flag'], $p_header['compression'],
  1882. $v_mtime, $v_mdate, $p_header['crc'],
  1883. $p_header['compressed_size'], $p_header['size'],
  1884. strlen($p_header['stored_filename']),
  1885. $p_header['extra_len'], $p_header['comment_len'],
  1886. $p_header['disk'], $p_header['internal'],
  1887. $p_header['external'], $p_header['offset']);
  1888.  
  1889. fputs($this->zip_fd, $v_binary_data, 46);
  1890.  
  1891. if (strlen($p_header['stored_filename']) != 0)
  1892. {
  1893. fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
  1894. }
  1895. if ($p_header['extra_len'] != 0)
  1896. {
  1897. fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
  1898. }
  1899. if ($p_header['comment_len'] != 0)
  1900. {
  1901. fputs($this->zip_fd, $p_header['comment'], $p_header['comment_len']);
  1902. }
  1903.  
  1904. return $v_result;
  1905. }
  1906.  
  1907. function privWriteCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
  1908. {
  1909. $v_result=1;
  1910.  
  1911. $v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries,
  1912. $p_nb_entries, $p_size,
  1913. $p_offset, strlen($p_comment));
  1914.  
  1915. fputs($this->zip_fd, $v_binary_data, 22);
  1916.  
  1917. if (strlen($p_comment) != 0)
  1918. {
  1919. fputs($this->zip_fd, $p_comment, strlen($p_comment));
  1920. }
  1921.  
  1922. return $v_result;
  1923. }
  1924.  
  1925. function privList(&$p_list)
  1926. {
  1927. $v_result=1;
  1928.  
  1929. $this->privDisableMagicQuotes();
  1930.  
  1931. if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
  1932. {
  1933. $this->privSwapBackMagicQuotes();
  1934. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
  1935.  
  1936. return PclZip::errorCode();
  1937. }
  1938.  
  1939. $v_central_dir = array();
  1940. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  1941. {
  1942. $this->privSwapBackMagicQuotes();
  1943. return $v_result;
  1944. }
  1945.  
  1946. @rewind($this->zip_fd);
  1947. if (@fseek($this->zip_fd, $v_central_dir['offset']))
  1948. {
  1949. $this->privSwapBackMagicQuotes();
  1950.  
  1951. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  1952.  
  1953. return PclZip::errorCode();
  1954. }
  1955.  
  1956. for ($i=0; $i<$v_central_dir['entries']; $i++)
  1957. {
  1958. if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
  1959. {
  1960. $this->privSwapBackMagicQuotes();
  1961. return $v_result;
  1962. }
  1963. $v_header['index'] = $i;
  1964.  
  1965. $this->privConvertHeader2FileInfo($v_header, $p_list[$v_header['stored_filename']]);
  1966. unset($v_header);
  1967. }
  1968.  
  1969. $this->privCloseFd();
  1970.  
  1971. $this->privSwapBackMagicQuotes();
  1972.  
  1973. return $v_result;
  1974. }
  1975.  
  1976. function privConvertHeader2FileInfo($p_header, &$p_info)
  1977. {
  1978. $v_result=1;
  1979.  
  1980. $v_temp_path = PclZipUtilPathReduction($p_header['filename']);
  1981. $p_info['filename'] = $v_temp_path;
  1982. $v_temp_path = PclZipUtilPathReduction($p_header['stored_filename']);
  1983. $p_info['stored_filename'] = $v_temp_path;
  1984. $p_info['size'] = $p_header['size'];
  1985. $p_info['compressed_size'] = $p_header['compressed_size'];
  1986. $p_info['mtime'] = $p_header['mtime'];
  1987. $p_info['comment'] = $p_header['comment'];
  1988. $p_info['folder'] = (($p_header['external']&0x00000010)==0x00000010);
  1989. $p_info['index'] = $p_header['index'];
  1990. $p_info['status'] = $p_header['status'];
  1991. $p_info['crc'] = $p_header['crc'];
  1992.  
  1993. return $v_result;
  1994. }
  1995.  
  1996. function privExtractByRule(&$p_file_list, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
  1997. {
  1998. $v_result=1;
  1999.  
  2000. $this->privDisableMagicQuotes();
  2001.  
  2002. if ( ($p_path == "")
  2003. || ( (substr($p_path, 0, 1) != "/")
  2004. && (substr($p_path, 0, 3) != "../")
  2005. && (substr($p_path,1,2)!=":/")))
  2006. $p_path = "./".$p_path;
  2007.  
  2008. if (($p_path != "./") && ($p_path != "/"))
  2009. {
  2010. while (substr($p_path, -1) == "/")
  2011. {
  2012. $p_path = substr($p_path, 0, strlen($p_path)-1);
  2013. }
  2014. }
  2015.  
  2016. if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/'))
  2017. {
  2018. $p_remove_path .= '/';
  2019. }
  2020. $p_remove_path_size = strlen($p_remove_path);
  2021.  
  2022. if (($v_result = $this->privOpenFd('rb')) != 1)
  2023. {
  2024. $this->privSwapBackMagicQuotes();
  2025. return $v_result;
  2026. }
  2027.  
  2028. $v_central_dir = array();
  2029. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  2030. {
  2031. $this->privCloseFd();
  2032. $this->privSwapBackMagicQuotes();
  2033.  
  2034. return $v_result;
  2035. }
  2036.  
  2037. $v_pos_entry = $v_central_dir['offset'];
  2038.  
  2039. $j_start = 0;
  2040. for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
  2041. {
  2042.  
  2043. @rewind($this->zip_fd);
  2044. if (@fseek($this->zip_fd, $v_pos_entry))
  2045. {
  2046. $this->privCloseFd();
  2047. $this->privSwapBackMagicQuotes();
  2048.  
  2049. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  2050.  
  2051. return PclZip::errorCode();
  2052. }
  2053.  
  2054. $v_header = array();
  2055. if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
  2056. {
  2057. $this->privCloseFd();
  2058. $this->privSwapBackMagicQuotes();
  2059.  
  2060. return $v_result;
  2061. }
  2062.  
  2063. $v_header['index'] = $i;
  2064.  
  2065. $v_pos_entry = ftell($this->zip_fd);
  2066.  
  2067. $v_extract = false;
  2068.  
  2069. if ( (isset($p_options[PCLZIP_OPT_BY_NAME]))
  2070. && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
  2071.  
  2072. for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_extract); $j++) {
  2073.  
  2074. if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
  2075.  
  2076. if ( (strlen($v_header['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
  2077. && (substr($v_header['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
  2078. $v_extract = true;
  2079. }
  2080. }
  2081. elseif ($v_header['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
  2082. $v_extract = true;
  2083. }
  2084. }
  2085. }
  2086.  
  2087. else if ( (isset($p_options[PCLZIP_OPT_BY_PREG]))
  2088. && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
  2089.  
  2090. if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header['stored_filename'])) {
  2091. $v_extract = true;
  2092. }
  2093. }
  2094.  
  2095. else if ( (isset($p_options[PCLZIP_OPT_BY_INDEX]))
  2096. && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
  2097. for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_extract); $j++) {
  2098.  
  2099. if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
  2100. $v_extract = true;
  2101. }
  2102. if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
  2103. $j_start = $j+1;
  2104. }
  2105.  
  2106. if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
  2107. break;
  2108. }
  2109. }
  2110. }
  2111.  
  2112. else {
  2113. $v_extract = true;
  2114. }
  2115.  
  2116. if ( ($v_extract)
  2117. && ( ($v_header['compression'] != 8)
  2118. && ($v_header['compression'] != 0))) {
  2119. $v_header['status'] = 'unsupported_compression';
  2120.  
  2121. if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  2122. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  2123.  
  2124. $this->privSwapBackMagicQuotes();
  2125. PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_COMPRESSION,
  2126. "Filename '".$v_header['stored_filename']."' is "
  2127. ."compressed by an unsupported compression "
  2128. ."method (".$v_header['compression'].") ");
  2129.  
  2130. return PclZip::errorCode();
  2131. }
  2132. }
  2133. if (($v_extract) && (($v_header['flag'] & 1) == 1)) {
  2134. $v_header['status'] = 'unsupported_encryption';
  2135.  
  2136. if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  2137. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  2138.  
  2139. $this->privSwapBackMagicQuotes();
  2140.  
  2141. PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION,
  2142. "Unsupported encryption for "
  2143. ." filename '".$v_header['stored_filename']
  2144. ."'");
  2145.  
  2146. return PclZip::errorCode();
  2147. }
  2148. }
  2149.  
  2150. if (($v_extract) && ($v_header['status'] != 'ok')) {
  2151. $v_result = $this->privConvertHeader2FileInfo($v_header,
  2152. $p_file_list[$v_nb_extracted++]);
  2153. if ($v_result != 1) {
  2154. $this->privCloseFd();
  2155. $this->privSwapBackMagicQuotes();
  2156. return $v_result;
  2157. }
  2158.  
  2159. $v_extract = false;
  2160. }
  2161. if ($v_extract)
  2162. {
  2163.  
  2164. @rewind($this->zip_fd);
  2165. if (@fseek($this->zip_fd, $v_header['offset']))
  2166. {
  2167. $this->privCloseFd();
  2168.  
  2169. $this->privSwapBackMagicQuotes();
  2170.  
  2171. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  2172.  
  2173. return PclZip::errorCode();
  2174. }
  2175.  
  2176. if ($p_options[PCLZIP_OPT_EXTRACT_AS_STRING]) {
  2177.  
  2178. $v_string = '';
  2179.  
  2180. $v_result1 = $this->privExtractFileAsString($v_header, $v_string, $p_options);
  2181. if ($v_result1 < 1) {
  2182. $this->privCloseFd();
  2183. $this->privSwapBackMagicQuotes();
  2184. return $v_result1;
  2185. }
  2186.  
  2187. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1)
  2188. {
  2189. $this->privCloseFd();
  2190. $this->privSwapBackMagicQuotes();
  2191.  
  2192. return $v_result;
  2193. }
  2194.  
  2195. $p_file_list[$v_nb_extracted]['content'] = $v_string;
  2196.  
  2197. $v_nb_extracted++;
  2198. if ($v_result1 == 2) {
  2199. break;
  2200. }
  2201. }
  2202. elseif ( (isset($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT]))
  2203. && ($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])) {
  2204. $v_result1 = $this->privExtractFileInOutput($v_header, $p_options);
  2205. if ($v_result1 < 1) {
  2206. $this->privCloseFd();
  2207. $this->privSwapBackMagicQuotes();
  2208. return $v_result1;
  2209. }
  2210.  
  2211. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
  2212. $this->privCloseFd();
  2213. $this->privSwapBackMagicQuotes();
  2214. return $v_result;
  2215. }
  2216.  
  2217. if ($v_result1 == 2) {
  2218. break;
  2219. }
  2220. }
  2221. else {
  2222. $v_result1 = $this->privExtractFile($v_header,
  2223. $p_path, $p_remove_path,
  2224. $p_remove_all_path,
  2225. $p_options);
  2226. if ($v_result1 < 1) {
  2227. $this->privCloseFd();
  2228. $this->privSwapBackMagicQuotes();
  2229. return $v_result1;
  2230. }
  2231.  
  2232. if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1)
  2233. {
  2234. $this->privCloseFd();
  2235. $this->privSwapBackMagicQuotes();
  2236.  
  2237. return $v_result;
  2238. }
  2239.  
  2240. if ($v_result1 == 2) {
  2241. break;
  2242. }
  2243. }
  2244. }
  2245. }
  2246.  
  2247. $this->privCloseFd();
  2248. $this->privSwapBackMagicQuotes();
  2249.  
  2250. return $v_result;
  2251. }
  2252.  
  2253. function privExtractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
  2254. {
  2255. $v_result=1;
  2256.  
  2257. if (($v_result = $this->privReadFileHeader($v_header)) != 1)
  2258. {
  2259. return $v_result;
  2260. }
  2261.  
  2262.  
  2263. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  2264. }
  2265.  
  2266. if ($p_remove_all_path == true) {
  2267. if (($p_entry['external']&0x00000010)==0x00000010) {
  2268.  
  2269. $p_entry['status'] = "filtered";
  2270.  
  2271. return $v_result;
  2272. }
  2273.  
  2274. $p_entry['filename'] = basename($p_entry['filename']);
  2275. }
  2276.  
  2277. else if ($p_remove_path != "")
  2278. {
  2279. if (PclZipUtilPathInclusion($p_remove_path, $p_entry['filename']) == 2)
  2280. {
  2281.  
  2282. $p_entry['status'] = "filtered";
  2283.  
  2284. return $v_result;
  2285. }
  2286.  
  2287. $p_remove_path_size = strlen($p_remove_path);
  2288. if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path)
  2289. {
  2290.  
  2291. $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
  2292.  
  2293. }
  2294. }
  2295.  
  2296. if ($p_path != '') {
  2297. $p_entry['filename'] = $p_path."/".$p_entry['filename'];
  2298. }
  2299. if (isset($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION])) {
  2300. $v_inclusion
  2301. = PclZipUtilPathInclusion($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION],
  2302. $p_entry['filename']);
  2303. if ($v_inclusion == 0) {
  2304.  
  2305. PclZip::privErrorLog(PCLZIP_ERR_DIRECTORY_RESTRICTION,
  2306. "Filename '".$p_entry['filename']."' is "
  2307. ."outside PCLZIP_OPT_EXTRACT_DIR_RESTRICTION");
  2308.  
  2309. return PclZip::errorCode();
  2310. }
  2311. }
  2312.  
  2313. if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  2314.  
  2315. $v_local_header = array();
  2316. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  2317. $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  2318. if ($v_result == 0) {
  2319. $p_entry['status'] = "skipped";
  2320. $v_result = 1;
  2321. }
  2322. if ($v_result == 2) {
  2323. $p_entry['status'] = "aborted";
  2324. $v_result = PCLZIP_ERR_USER_ABORTED;
  2325. }
  2326.  
  2327. $p_entry['filename'] = $v_local_header['filename'];
  2328. }
  2329.  
  2330.  
  2331. if ($p_entry['status'] == 'ok') {
  2332.  
  2333. if (file_exists($p_entry['filename']) && !$p_options[PCLZIP_OPT_SUBSTITUE_FILE])
  2334. {
  2335.  
  2336. if (is_dir($p_entry['filename']))
  2337. {
  2338.  
  2339. $p_entry['status'] = "already_a_directory";
  2340. if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  2341. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  2342.  
  2343. PclZip::privErrorLog(PCLZIP_ERR_ALREADY_A_DIRECTORY,
  2344. "Filename '".$p_entry['filename']."' is "
  2345. ."already used by an existing directory");
  2346.  
  2347. return PclZip::errorCode();
  2348. }
  2349. }
  2350. else if (!is_writeable($p_entry['filename']))
  2351. {
  2352.  
  2353. $p_entry['status'] = "write_protected";
  2354.  
  2355. if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  2356. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  2357.  
  2358. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
  2359. "Filename '".$p_entry['filename']."' exists "
  2360. ."and is write protected");
  2361.  
  2362. return PclZip::errorCode();
  2363. }
  2364. }
  2365.  
  2366. else if (filemtime($p_entry['filename']) > $p_entry['mtime'])
  2367. {
  2368. if ( (isset($p_options[PCLZIP_OPT_REPLACE_NEWER]))
  2369. && ($p_options[PCLZIP_OPT_REPLACE_NEWER]===true)) {
  2370. }
  2371. else {
  2372. $p_entry['status'] = "newer_exist";
  2373.  
  2374. if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
  2375. && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
  2376.  
  2377. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
  2378. "Newer version of '".$p_entry['filename']."' exists "
  2379. ."and option PCLZIP_OPT_REPLACE_NEWER is not selected");
  2380.  
  2381. return PclZip::errorCode();
  2382. }
  2383. }
  2384. }
  2385. else {
  2386. }
  2387. }
  2388.  
  2389. else {
  2390. if ((($p_entry['external']&0x00000010)==0x00000010) || (substr($p_entry['filename'], -1) == '/'))
  2391. $v_dir_to_check = $p_entry['filename'];
  2392. else if (!strstr($p_entry['filename'], "/"))
  2393. $v_dir_to_check = "";
  2394. else
  2395. $v_dir_to_check = dirname($p_entry['filename']);
  2396.  
  2397. if (($v_result = $this->privDirCheck($v_dir_to_check, (($p_entry['external']&0x00000010)==0x00000010))) != 1) {
  2398. $p_entry['status'] = "path_creation_fail";
  2399. $v_result = 1;
  2400. }
  2401. }
  2402. }
  2403.  
  2404. if ($p_entry['status'] == 'ok') {
  2405.  
  2406. if (!(($p_entry['external']&0x00000010)==0x00000010))
  2407. {
  2408. if ($p_entry['compression'] == 0) {
  2409.  
  2410. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0)
  2411. {
  2412.  
  2413. $p_entry['status'] = "write_error";
  2414.  
  2415. return $v_result;
  2416. }
  2417.  
  2418.  
  2419. $v_size = $p_entry['compressed_size'];
  2420. while ($v_size != 0)
  2421. {
  2422. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2423. $v_buffer = @fread($this->zip_fd, $v_read_size);
  2424.  
  2425. @fwrite($v_dest_file, $v_buffer, $v_read_size);
  2426. $v_size -= $v_read_size;
  2427. }
  2428.  
  2429. fclose($v_dest_file);
  2430.  
  2431. touch($p_entry['filename'], $p_entry['mtime']);
  2432.  
  2433. }
  2434. else {
  2435. if (($p_entry['flag'] & 1) == 1) {
  2436. PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION, 'File \''.$p_entry['filename'].'\' is encrypted. Encrypted files are not supported.');
  2437. return PclZip::errorCode();
  2438. }
  2439.  
  2440.  
  2441. if ( (!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF]))
  2442. && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON])
  2443. || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
  2444. && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_entry['size'])) ) ) {
  2445. $v_result = $this->privExtractFileUsingTempFile($p_entry, $p_options);
  2446. if ($v_result < PCLZIP_ERR_NO_ERROR) {
  2447. return $v_result;
  2448. }
  2449. }
  2450. else {
  2451.  
  2452. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  2453. $v_file_content = @gzinflate($v_buffer);
  2454. unset($v_buffer);
  2455. if ($v_file_content === FALSE) {
  2456. $p_entry['status'] = "error";
  2457. return $v_result;
  2458. }
  2459. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  2460. $p_entry['status'] = "write_error";
  2461. return $v_result;
  2462. }
  2463. @fwrite($v_dest_file, $v_file_content, $p_entry['size']);
  2464. unset($v_file_content);
  2465. @fclose($v_dest_file);
  2466. }
  2467.  
  2468. @touch($p_entry['filename'], $p_entry['mtime']);
  2469. }
  2470.  
  2471. if (isset($p_options[PCLZIP_OPT_SET_CHMOD])) {
  2472.  
  2473. @chmod($p_entry['filename'], $p_options[PCLZIP_OPT_SET_CHMOD]);
  2474. }
  2475.  
  2476. }
  2477. }
  2478.  
  2479. if ($p_entry['status'] == "aborted") {
  2480. $p_entry['status'] = "skipped";
  2481. }
  2482. elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  2483.  
  2484. $v_local_header = array();
  2485. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  2486.  
  2487. $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
  2488.  
  2489. if ($v_result == 2) {
  2490. $v_result = PCLZIP_ERR_USER_ABORTED;
  2491. }
  2492. }
  2493.  
  2494. return $v_result;
  2495. }
  2496.  
  2497. function privExtractFileUsingTempFile(&$p_entry, &$p_options)
  2498. {
  2499. $v_result=1;
  2500. $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.gz';
  2501. if (($v_dest_file = @fopen($v_gzip_temp_name, "wb")) == 0) {
  2502. fclose($v_file);
  2503. PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary write mode');
  2504. return PclZip::errorCode();
  2505. }
  2506.  
  2507.  
  2508. $v_binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($p_entry['compression']), Chr(0x00), time(), Chr(0x00), Chr(3));
  2509. @fwrite($v_dest_file, $v_binary_data, 10);
  2510.  
  2511. $v_size = $p_entry['compressed_size'];
  2512. while ($v_size != 0)
  2513. {
  2514. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2515. $v_buffer = @fread($this->zip_fd, $v_read_size);
  2516. @fwrite($v_dest_file, $v_buffer, $v_read_size);
  2517. $v_size -= $v_read_size;
  2518. }
  2519.  
  2520. $v_binary_data = pack('VV', $p_entry['crc'], $p_entry['size']);
  2521. @fwrite($v_dest_file, $v_binary_data, 8);
  2522.  
  2523. @fclose($v_dest_file);
  2524.  
  2525. if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
  2526. $p_entry['status'] = "write_error";
  2527. return $v_result;
  2528. }
  2529.  
  2530. if (($v_src_file = @gzopen($v_gzip_temp_name, 'rb')) == 0) {
  2531. @fclose($v_dest_file);
  2532. $p_entry['status'] = "read_error";
  2533. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
  2534. return PclZip::errorCode();
  2535. }
  2536.  
  2537.  
  2538. $v_size = $p_entry['size'];
  2539. while ($v_size != 0) {
  2540. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  2541. $v_buffer = @gzread($v_src_file, $v_read_size);
  2542. @fwrite($v_dest_file, $v_buffer, $v_read_size);
  2543. $v_size -= $v_read_size;
  2544. }
  2545. @fclose($v_dest_file);
  2546. @gzclose($v_src_file);
  2547.  
  2548. @unlink($v_gzip_temp_name);
  2549. return $v_result;
  2550. }
  2551.  
  2552. function privExtractFileInOutput(&$p_entry, &$p_options)
  2553. {
  2554. $v_result=1;
  2555.  
  2556. if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
  2557. return $v_result;
  2558. }
  2559.  
  2560.  
  2561. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  2562. }
  2563.  
  2564. if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  2565.  
  2566. $v_local_header = array();
  2567. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  2568.  
  2569. $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  2570. if ($v_result == 0) {
  2571. $p_entry['status'] = "skipped";
  2572. $v_result = 1;
  2573. }
  2574.  
  2575. if ($v_result == 2) {
  2576. $p_entry['status'] = "aborted";
  2577. $v_result = PCLZIP_ERR_USER_ABORTED;
  2578. }
  2579.  
  2580. $p_entry['filename'] = $v_local_header['filename'];
  2581. }
  2582.  
  2583. if ($p_entry['status'] == 'ok') {
  2584.  
  2585. if (!(($p_entry['external']&0x00000010)==0x00000010)) {
  2586. if ($p_entry['compressed_size'] == $p_entry['size']) {
  2587.  
  2588. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  2589.  
  2590. echo $v_buffer;
  2591. unset($v_buffer);
  2592. }
  2593. else {
  2594.  
  2595. $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
  2596. $v_file_content = gzinflate($v_buffer);
  2597. unset($v_buffer);
  2598.  
  2599. echo $v_file_content;
  2600. unset($v_file_content);
  2601. }
  2602. }
  2603. }
  2604.  
  2605. if ($p_entry['status'] == "aborted") {
  2606. $p_entry['status'] = "skipped";
  2607. }
  2608.  
  2609. elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  2610.  
  2611. $v_local_header = array();
  2612. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  2613.  
  2614. $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
  2615.  
  2616. if ($v_result == 2) {
  2617. $v_result = PCLZIP_ERR_USER_ABORTED;
  2618. }
  2619. }
  2620.  
  2621. return $v_result;
  2622. }
  2623.  
  2624. function privExtractFileAsString(&$p_entry, &$p_string, &$p_options)
  2625. {
  2626. $v_result=1;
  2627.  
  2628. $v_header = array();
  2629. if (($v_result = $this->privReadFileHeader($v_header)) != 1)
  2630. {
  2631. return $v_result;
  2632. }
  2633.  
  2634.  
  2635. if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
  2636. }
  2637.  
  2638. if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
  2639.  
  2640. $v_local_header = array();
  2641. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  2642.  
  2643. $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
  2644. if ($v_result == 0) {
  2645. $p_entry['status'] = "skipped";
  2646. $v_result = 1;
  2647. }
  2648. if ($v_result == 2) {
  2649. $p_entry['status'] = "aborted";
  2650. $v_result = PCLZIP_ERR_USER_ABORTED;
  2651. }
  2652.  
  2653. $p_entry['filename'] = $v_local_header['filename'];
  2654. }
  2655.  
  2656.  
  2657. if ($p_entry['status'] == 'ok') {
  2658.  
  2659. if (!(($p_entry['external']&0x00000010)==0x00000010)) {
  2660. if ($p_entry['compression'] == 0) {
  2661. $p_string = @fread($this->zip_fd, $p_entry['compressed_size']);
  2662. }
  2663. else {
  2664. $v_data = @fread($this->zip_fd, $p_entry['compressed_size']);
  2665. if (($p_string = @gzinflate($v_data)) === FALSE) {
  2666. }
  2667. }
  2668. }
  2669. else {
  2670. }
  2671. }
  2672.  
  2673. if ($p_entry['status'] == "aborted") {
  2674. $p_entry['status'] = "skipped";
  2675. }
  2676. elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
  2677.  
  2678. $v_local_header = array();
  2679. $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
  2680. $v_local_header['content'] = $p_string;
  2681. $p_string = '';
  2682.  
  2683. $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
  2684.  
  2685. $p_string = $v_local_header['content'];
  2686. unset($v_local_header['content']);
  2687.  
  2688. if ($v_result == 2) {
  2689. $v_result = PCLZIP_ERR_USER_ABORTED;
  2690. }
  2691. }
  2692.  
  2693. return $v_result;
  2694. }
  2695.  
  2696. function privReadFileHeader(&$p_header)
  2697. {
  2698. $v_result=1;
  2699.  
  2700. $v_binary_data = @fread($this->zip_fd, 4);
  2701. $v_data = unpack('Vid', $v_binary_data);
  2702.  
  2703. if ($v_data['id'] != 0x04034b50)
  2704. {
  2705.  
  2706. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  2707.  
  2708. return PclZip::errorCode();
  2709. }
  2710.  
  2711. $v_binary_data = fread($this->zip_fd, 26);
  2712.  
  2713. if (strlen($v_binary_data) != 26)
  2714. {
  2715. $p_header['filename'] = "";
  2716. $p_header['status'] = "invalid_header";
  2717.  
  2718. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
  2719.  
  2720. return PclZip::errorCode();
  2721. }
  2722.  
  2723. $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
  2724.  
  2725. $p_header['filename'] = fread($this->zip_fd, $v_data['filename_len']);
  2726.  
  2727. if ($v_data['extra_len'] != 0) {
  2728. $p_header['extra'] = fread($this->zip_fd, $v_data['extra_len']);
  2729. }
  2730. else {
  2731. $p_header['extra'] = '';
  2732. }
  2733.  
  2734. $p_header['version_extracted'] = $v_data['version'];
  2735. $p_header['compression'] = $v_data['compression'];
  2736. $p_header['size'] = $v_data['size'];
  2737. $p_header['compressed_size'] = $v_data['compressed_size'];
  2738. $p_header['crc'] = $v_data['crc'];
  2739. $p_header['flag'] = $v_data['flag'];
  2740. $p_header['filename_len'] = $v_data['filename_len'];
  2741.  
  2742. $p_header['mdate'] = $v_data['mdate'];
  2743. $p_header['mtime'] = $v_data['mtime'];
  2744. if ($p_header['mdate'] && $p_header['mtime'])
  2745. {
  2746. $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
  2747. $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
  2748. $v_seconde = ($p_header['mtime'] & 0x001F)*2;
  2749.  
  2750. $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
  2751. $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
  2752. $v_day = $p_header['mdate'] & 0x001F;
  2753.  
  2754. $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  2755.  
  2756. }
  2757. else
  2758. {
  2759. $p_header['mtime'] = time();
  2760. }
  2761.  
  2762. $p_header['stored_filename'] = $p_header['filename'];
  2763.  
  2764. $p_header['status'] = "ok";
  2765.  
  2766. return $v_result;
  2767. }
  2768.  
  2769. function privReadCentralFileHeader(&$p_header)
  2770. {
  2771. $v_result=1;
  2772.  
  2773. $v_binary_data = @fread($this->zip_fd, 4);
  2774. $v_data = unpack('Vid', $v_binary_data);
  2775.  
  2776. if ($v_data['id'] != 0x02014b50)
  2777. {
  2778.  
  2779. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
  2780.  
  2781. return PclZip::errorCode();
  2782. }
  2783.  
  2784. $v_binary_data = fread($this->zip_fd, 42);
  2785.  
  2786. if (strlen($v_binary_data) != 42)
  2787. {
  2788. $p_header['filename'] = "";
  2789. $p_header['status'] = "invalid_header";
  2790.  
  2791. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
  2792.  
  2793. return PclZip::errorCode();
  2794. }
  2795.  
  2796. $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);
  2797.  
  2798. if ($p_header['filename_len'] != 0)
  2799. $p_header['filename'] = fread($this->zip_fd, $p_header['filename_len']);
  2800. else
  2801. $p_header['filename'] = '';
  2802.  
  2803. if ($p_header['extra_len'] != 0)
  2804. $p_header['extra'] = fread($this->zip_fd, $p_header['extra_len']);
  2805. else
  2806. $p_header['extra'] = '';
  2807.  
  2808. if ($p_header['comment_len'] != 0)
  2809. $p_header['comment'] = fread($this->zip_fd, $p_header['comment_len']);
  2810. else
  2811. $p_header['comment'] = '';
  2812.  
  2813. $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
  2814. $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
  2815. $v_seconde = ($p_header['mtime'] & 0x001F)*2;
  2816.  
  2817. $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
  2818. $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
  2819. $v_day = $p_header['mdate'] & 0x001F;
  2820.  
  2821. $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
  2822.  
  2823.  
  2824. $p_header['stored_filename'] = $p_header['filename'];
  2825.  
  2826. $p_header['status'] = 'ok';
  2827.  
  2828. if (substr($p_header['filename'], -1) == '/') {
  2829. $p_header['external'] = 0x00000010;
  2830. }
  2831.  
  2832.  
  2833. return $v_result;
  2834. }
  2835.  
  2836. function privCheckFileHeaders(&$p_local_header, &$p_central_header)
  2837. {
  2838. $v_result=1;
  2839.  
  2840. if ($p_local_header['filename'] != $p_central_header['filename']) {
  2841. }
  2842. if ($p_local_header['version_extracted'] != $p_central_header['version_extracted']) {
  2843. }
  2844. if ($p_local_header['flag'] != $p_central_header['flag']) {
  2845. }
  2846. if ($p_local_header['compression'] != $p_central_header['compression']) {
  2847. }
  2848. if ($p_local_header['mtime'] != $p_central_header['mtime']) {
  2849. }
  2850. if ($p_local_header['filename_len'] != $p_central_header['filename_len']) {
  2851. }
  2852. if (($p_local_header['flag'] & 8) == 8) {
  2853. $p_local_header['size'] = $p_central_header['size'];
  2854. $p_local_header['compressed_size'] = $p_central_header['compressed_size'];
  2855. $p_local_header['crc'] = $p_central_header['crc'];
  2856. }
  2857.  
  2858. return $v_result;
  2859. }
  2860.  
  2861. function privReadEndCentralDir(&$p_central_dir)
  2862. {
  2863. $v_result=1;
  2864.  
  2865. $v_size = filesize($this->zipname);
  2866. @fseek($this->zip_fd, $v_size);
  2867. if (@ftell($this->zip_fd) != $v_size)
  2868. {
  2869. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \''.$this->zipname.'\'');
  2870.  
  2871. return PclZip::errorCode();
  2872. }
  2873.  
  2874. $v_found = 0;
  2875. if ($v_size > 26) {
  2876. @fseek($this->zip_fd, $v_size-22);
  2877. if (($v_pos = @ftell($this->zip_fd)) != ($v_size-22))
  2878. {
  2879. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
  2880.  
  2881. return PclZip::errorCode();
  2882. }
  2883.  
  2884. $v_binary_data = @fread($this->zip_fd, 4);
  2885. $v_data = @unpack('Vid', $v_binary_data);
  2886.  
  2887. if ($v_data['id'] == 0x06054b50) {
  2888. $v_found = 1;
  2889. }
  2890.  
  2891. $v_pos = ftell($this->zip_fd);
  2892. }
  2893.  
  2894. if (!$v_found) {
  2895. $v_maximum_size = 65557; if ($v_maximum_size > $v_size)
  2896. $v_maximum_size = $v_size;
  2897. @fseek($this->zip_fd, $v_size-$v_maximum_size);
  2898. if (@ftell($this->zip_fd) != ($v_size-$v_maximum_size))
  2899. {
  2900. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
  2901.  
  2902. return PclZip::errorCode();
  2903. }
  2904.  
  2905. $v_pos = ftell($this->zip_fd);
  2906. $v_bytes = 0x00000000;
  2907. while ($v_pos < $v_size)
  2908. {
  2909. $v_byte = @fread($this->zip_fd, 1);
  2910. $v_bytes = ( ($v_bytes & 0xFFFFFF) << 8) | Ord($v_byte);
  2911.  
  2912. if ($v_bytes == 0x504b0506)
  2913. {
  2914. $v_pos++;
  2915. break;
  2916. }
  2917.  
  2918. $v_pos++;
  2919. }
  2920.  
  2921. if ($v_pos == $v_size)
  2922. {
  2923.  
  2924. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
  2925.  
  2926. return PclZip::errorCode();
  2927. }
  2928. }
  2929.  
  2930. $v_binary_data = fread($this->zip_fd, 18);
  2931.  
  2932. if (strlen($v_binary_data) != 18)
  2933. {
  2934.  
  2935. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
  2936.  
  2937. return PclZip::errorCode();
  2938. }
  2939.  
  2940. $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
  2941.  
  2942. if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
  2943.  
  2944. if (0) {
  2945. PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT,
  2946. 'The central dir is not at the end of the archive.'
  2947. .' Some trailing bytes exists after the archive.');
  2948.  
  2949. return PclZip::errorCode();
  2950. }
  2951. }
  2952.  
  2953. if ($v_data['comment_size'] != 0) {
  2954. $p_central_dir['comment'] = fread($this->zip_fd, $v_data['comment_size']);
  2955. }
  2956. else
  2957. $p_central_dir['comment'] = '';
  2958.  
  2959. $p_central_dir['entries'] = $v_data['entries'];
  2960. $p_central_dir['disk_entries'] = $v_data['disk_entries'];
  2961. $p_central_dir['offset'] = $v_data['offset'];
  2962. $p_central_dir['size'] = $v_data['size'];
  2963. $p_central_dir['disk'] = $v_data['disk'];
  2964. $p_central_dir['disk_start'] = $v_data['disk_start'];
  2965.  
  2966. return $v_result;
  2967. }
  2968.  
  2969. function privDeleteByRule(&$p_result_list, &$p_options)
  2970. {
  2971. $v_result=1;
  2972. $v_list_detail = array();
  2973.  
  2974. if (($v_result=$this->privOpenFd('rb')) != 1)
  2975. {
  2976. return $v_result;
  2977. }
  2978.  
  2979. $v_central_dir = array();
  2980. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  2981. {
  2982. $this->privCloseFd();
  2983. return $v_result;
  2984. }
  2985.  
  2986. @rewind($this->zip_fd);
  2987.  
  2988. $v_pos_entry = $v_central_dir['offset'];
  2989. @rewind($this->zip_fd);
  2990. if (@fseek($this->zip_fd, $v_pos_entry))
  2991. {
  2992. $this->privCloseFd();
  2993.  
  2994. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  2995.  
  2996. return PclZip::errorCode();
  2997. }
  2998.  
  2999. $v_header_list = array();
  3000. $j_start = 0;
  3001. for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
  3002. {
  3003.  
  3004. $v_header_list[$v_nb_extracted] = array();
  3005. if (($v_result = $this->privReadCentralFileHeader($v_header_list[$v_nb_extracted])) != 1)
  3006. {
  3007. $this->privCloseFd();
  3008.  
  3009. return $v_result;
  3010. }
  3011.  
  3012.  
  3013. $v_header_list[$v_nb_extracted]['index'] = $i;
  3014.  
  3015. $v_found = false;
  3016.  
  3017. if ( (isset($p_options[PCLZIP_OPT_BY_NAME]))
  3018. && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
  3019.  
  3020. for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_found); $j++) {
  3021.  
  3022. if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
  3023.  
  3024. if ( (strlen($v_header_list[$v_nb_extracted]['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
  3025. && (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])) {
  3026. $v_found = true;
  3027. }
  3028. elseif ( (($v_header_list[$v_nb_extracted]['external']&0x00000010)==0x00000010)
  3029. && ($v_header_list[$v_nb_extracted]['stored_filename'].'/' == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
  3030. $v_found = true;
  3031. }
  3032. }
  3033. elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
  3034. $v_found = true;
  3035. }
  3036. }
  3037. }
  3038.  
  3039. else if ( (isset($p_options[PCLZIP_OPT_BY_PREG]))
  3040. && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
  3041.  
  3042. if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
  3043. $v_found = true;
  3044. }
  3045. }
  3046.  
  3047. else if ( (isset($p_options[PCLZIP_OPT_BY_INDEX]))
  3048. && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
  3049.  
  3050. for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_found); $j++) {
  3051.  
  3052. if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
  3053. $v_found = true;
  3054. }
  3055. if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
  3056. $j_start = $j+1;
  3057. }
  3058.  
  3059. if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
  3060. break;
  3061. }
  3062. }
  3063. }
  3064. else {
  3065. $v_found = true;
  3066. }
  3067.  
  3068. if ($v_found)
  3069. {
  3070. unset($v_header_list[$v_nb_extracted]);
  3071. }
  3072. else
  3073. {
  3074. $v_nb_extracted++;
  3075. }
  3076. }
  3077.  
  3078. if ($v_nb_extracted > 0) {
  3079.  
  3080. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
  3081.  
  3082. $v_temp_zip = new PclZip($v_zip_temp_name);
  3083.  
  3084. if (($v_result = $v_temp_zip->privOpenFd('wb')) != 1) {
  3085. $this->privCloseFd();
  3086.  
  3087. return $v_result;
  3088. }
  3089.  
  3090. for ($i=0; $i<sizeof($v_header_list); $i++) {
  3091.  
  3092. @rewind($this->zip_fd);
  3093. if (@fseek($this->zip_fd, $v_header_list[$i]['offset'])) {
  3094. $this->privCloseFd();
  3095. $v_temp_zip->privCloseFd();
  3096. @unlink($v_zip_temp_name);
  3097.  
  3098. PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
  3099.  
  3100. return PclZip::errorCode();
  3101. }
  3102.  
  3103. $v_local_header = array();
  3104. if (($v_result = $this->privReadFileHeader($v_local_header)) != 1) {
  3105. $this->privCloseFd();
  3106. $v_temp_zip->privCloseFd();
  3107. @unlink($v_zip_temp_name);
  3108.  
  3109. return $v_result;
  3110. }
  3111. if ($this->privCheckFileHeaders($v_local_header,
  3112. $v_header_list[$i]) != 1) {
  3113. }
  3114. unset($v_local_header);
  3115.  
  3116. if (($v_result = $v_temp_zip->privWriteFileHeader($v_header_list[$i])) != 1) {
  3117. $this->privCloseFd();
  3118. $v_temp_zip->privCloseFd();
  3119. @unlink($v_zip_temp_name);
  3120.  
  3121. return $v_result;
  3122. }
  3123.  
  3124. if (($v_result = PclZipUtilCopyBlock($this->zip_fd, $v_temp_zip->zip_fd, $v_header_list[$i]['compressed_size'])) != 1) {
  3125. $this->privCloseFd();
  3126. $v_temp_zip->privCloseFd();
  3127. @unlink($v_zip_temp_name);
  3128.  
  3129. return $v_result;
  3130. }
  3131. }
  3132.  
  3133. $v_offset = @ftell($v_temp_zip->zip_fd);
  3134.  
  3135. for ($i=0; $i<sizeof($v_header_list); $i++) {
  3136. if (($v_result = $v_temp_zip->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
  3137. $v_temp_zip->privCloseFd();
  3138. $this->privCloseFd();
  3139. @unlink($v_zip_temp_name);
  3140.  
  3141. return $v_result;
  3142. }
  3143.  
  3144. $v_temp_zip->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
  3145. }
  3146.  
  3147.  
  3148. $v_comment = '';
  3149. if (isset($p_options[PCLZIP_OPT_COMMENT])) {
  3150. $v_comment = $p_options[PCLZIP_OPT_COMMENT];
  3151. }
  3152.  
  3153. $v_size = @ftell($v_temp_zip->zip_fd)-$v_offset;
  3154.  
  3155. if (($v_result = $v_temp_zip->privWriteCentralHeader(sizeof($v_header_list), $v_size, $v_offset, $v_comment)) != 1) {
  3156. unset($v_header_list);
  3157. $v_temp_zip->privCloseFd();
  3158. $this->privCloseFd();
  3159. @unlink($v_zip_temp_name);
  3160.  
  3161. return $v_result;
  3162. }
  3163.  
  3164. $v_temp_zip->privCloseFd();
  3165. $this->privCloseFd();
  3166.  
  3167. @unlink($this->zipname);
  3168.  
  3169. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  3170. unset($v_temp_zip);
  3171. }
  3172. else if ($v_central_dir['entries'] != 0) {
  3173. $this->privCloseFd();
  3174.  
  3175. if (($v_result = $this->privOpenFd('wb')) != 1) {
  3176. return $v_result;
  3177. }
  3178.  
  3179. if (($v_result = $this->privWriteCentralHeader(0, 0, 0, '')) != 1) {
  3180. return $v_result;
  3181. }
  3182.  
  3183. $this->privCloseFd();
  3184. }
  3185.  
  3186. return $v_result;
  3187. }
  3188.  
  3189. function privDirCheck($p_dir, $p_is_dir=false)
  3190. {
  3191. $v_result = 1;
  3192.  
  3193.  
  3194. if (($p_is_dir) && (substr($p_dir, -1)=='/'))
  3195. {
  3196. $p_dir = substr($p_dir, 0, strlen($p_dir)-1);
  3197. }
  3198.  
  3199. if ((is_dir($p_dir)) || ($p_dir == ""))
  3200. {
  3201. return 1;
  3202. }
  3203.  
  3204. $p_parent_dir = dirname($p_dir);
  3205.  
  3206. if ($p_parent_dir != $p_dir)
  3207. {
  3208. if ($p_parent_dir != "")
  3209. {
  3210. if (($v_result = $this->privDirCheck($p_parent_dir)) != 1)
  3211. {
  3212. return $v_result;
  3213. }
  3214. }
  3215. }
  3216.  
  3217. if (!@mkdir($p_dir, 0777))
  3218. {
  3219. PclZip::privErrorLog(PCLZIP_ERR_DIR_CREATE_FAIL, "Unable to create directory '$p_dir'");
  3220.  
  3221. return PclZip::errorCode();
  3222. }
  3223.  
  3224. return $v_result;
  3225. }
  3226.  
  3227. function privMerge(&$p_archive_to_add)
  3228. {
  3229. $v_result=1;
  3230.  
  3231. if (!is_file($p_archive_to_add->zipname))
  3232. {
  3233.  
  3234. $v_result = 1;
  3235.  
  3236. return $v_result;
  3237. }
  3238.  
  3239. if (!is_file($this->zipname))
  3240. {
  3241.  
  3242. $v_result = $this->privDuplicate($p_archive_to_add->zipname);
  3243.  
  3244. return $v_result;
  3245. }
  3246.  
  3247. if (($v_result=$this->privOpenFd('rb')) != 1)
  3248. {
  3249. return $v_result;
  3250. }
  3251.  
  3252. $v_central_dir = array();
  3253. if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
  3254. {
  3255. $this->privCloseFd();
  3256. return $v_result;
  3257. }
  3258.  
  3259. @rewind($this->zip_fd);
  3260.  
  3261. if (($v_result=$p_archive_to_add->privOpenFd('rb')) != 1)
  3262. {
  3263. $this->privCloseFd();
  3264.  
  3265. return $v_result;
  3266. }
  3267.  
  3268. $v_central_dir_to_add = array();
  3269. if (($v_result = $p_archive_to_add->privReadEndCentralDir($v_central_dir_to_add)) != 1)
  3270. {
  3271. $this->privCloseFd();
  3272. $p_archive_to_add->privCloseFd();
  3273.  
  3274. return $v_result;
  3275. }
  3276.  
  3277. @rewind($p_archive_to_add->zip_fd);
  3278.  
  3279. $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
  3280.  
  3281. if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
  3282. {
  3283. $this->privCloseFd();
  3284. $p_archive_to_add->privCloseFd();
  3285.  
  3286. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
  3287.  
  3288. return PclZip::errorCode();
  3289. }
  3290.  
  3291. $v_size = $v_central_dir['offset'];
  3292. while ($v_size != 0)
  3293. {
  3294. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  3295. $v_buffer = fread($this->zip_fd, $v_read_size);
  3296. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  3297. $v_size -= $v_read_size;
  3298. }
  3299.  
  3300. $v_size = $v_central_dir_to_add['offset'];
  3301. while ($v_size != 0)
  3302. {
  3303. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  3304. $v_buffer = fread($p_archive_to_add->zip_fd, $v_read_size);
  3305. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  3306. $v_size -= $v_read_size;
  3307. }
  3308.  
  3309. $v_offset = @ftell($v_zip_temp_fd);
  3310.  
  3311. $v_size = $v_central_dir['size'];
  3312. while ($v_size != 0)
  3313. {
  3314. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  3315. $v_buffer = @fread($this->zip_fd, $v_read_size);
  3316. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  3317. $v_size -= $v_read_size;
  3318. }
  3319.  
  3320. $v_size = $v_central_dir_to_add['size'];
  3321. while ($v_size != 0)
  3322. {
  3323. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  3324. $v_buffer = @fread($p_archive_to_add->zip_fd, $v_read_size);
  3325. @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
  3326. $v_size -= $v_read_size;
  3327. }
  3328.  
  3329. $v_comment = $v_central_dir['comment'].' '.$v_central_dir_to_add['comment'];
  3330. $v_size = @ftell($v_zip_temp_fd)-$v_offset;
  3331. $v_swap = $this->zip_fd;
  3332. $this->zip_fd = $v_zip_temp_fd;
  3333. $v_zip_temp_fd = $v_swap;
  3334.  
  3335. if (($v_result = $this->privWriteCentralHeader($v_central_dir['entries']+$v_central_dir_to_add['entries'], $v_size, $v_offset, $v_comment)) != 1)
  3336. {
  3337. $this->privCloseFd();
  3338. $p_archive_to_add->privCloseFd();
  3339. @fclose($v_zip_temp_fd);
  3340. $this->zip_fd = null;
  3341.  
  3342. unset($v_header_list);
  3343. return $v_result;
  3344. }
  3345.  
  3346. $v_swap = $this->zip_fd;
  3347. $this->zip_fd = $v_zip_temp_fd;
  3348. $v_zip_temp_fd = $v_swap;
  3349.  
  3350. $this->privCloseFd();
  3351. $p_archive_to_add->privCloseFd();
  3352.  
  3353. @fclose($v_zip_temp_fd);
  3354.  
  3355. @unlink($this->zipname);
  3356.  
  3357. PclZipUtilRename($v_zip_temp_name, $this->zipname);
  3358.  
  3359. return $v_result;
  3360. }
  3361.  
  3362. function privDuplicate($p_archive_filename)
  3363. {
  3364. $v_result=1;
  3365.  
  3366. if (!is_file($p_archive_filename))
  3367. {
  3368. $v_result = 1;
  3369.  
  3370. return $v_result;
  3371. }
  3372.  
  3373. if (($v_result=$this->privOpenFd('wb')) != 1)
  3374. {
  3375. return $v_result;
  3376. }
  3377.  
  3378. if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0)
  3379. {
  3380. $this->privCloseFd();
  3381.  
  3382. PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive file \''.$p_archive_filename.'\' in binary write mode');
  3383.  
  3384. return PclZip::errorCode();
  3385. }
  3386.  
  3387. $v_size = filesize($p_archive_filename);
  3388. while ($v_size != 0)
  3389. {
  3390. $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
  3391. $v_buffer = fread($v_zip_temp_fd, $v_read_size);
  3392. @fwrite($this->zip_fd, $v_buffer, $v_read_size);
  3393. $v_size -= $v_read_size;
  3394. }
  3395.  
  3396. $this->privCloseFd();
  3397. @fclose($v_zip_temp_fd);
  3398.  
  3399. return $v_result;
  3400. }
  3401.  
  3402. function privErrorLog($p_error_code=0, $p_error_string='')
  3403. {
  3404. if (PCLZIP_ERROR_EXTERNAL == 1) {
  3405. PclError($p_error_code, $p_error_string);
  3406. }
  3407. else {
  3408. $this->error_code = $p_error_code;
  3409. $this->error_string = $p_error_string;
  3410. }
  3411. }
  3412.  
  3413. function privErrorReset()
  3414. {
  3415. if (PCLZIP_ERROR_EXTERNAL == 1) {
  3416. PclErrorReset();
  3417. }
  3418. else {
  3419. $this->error_code = 0;
  3420. $this->error_string = '';
  3421. }
  3422. }
  3423.  
  3424. function privDisableMagicQuotes()
  3425. {
  3426. $v_result=1;
  3427.  
  3428. if ( (!function_exists("get_magic_quotes_runtime"))
  3429. || (!function_exists("set_magic_quotes_runtime"))) {
  3430. return $v_result;
  3431. }
  3432.  
  3433. if ($this->magic_quotes_status != -1) {
  3434. return $v_result;
  3435. }
  3436.  
  3437. $this->magic_quotes_status = @get_magic_quotes_runtime();
  3438.  
  3439. if ($this->magic_quotes_status == 1) {
  3440. @set_magic_quotes_runtime(0);
  3441. }
  3442.  
  3443. return $v_result;
  3444. }
  3445.  
  3446. function privSwapBackMagicQuotes()
  3447. {
  3448. $v_result=1;
  3449.  
  3450. if ( (!function_exists("get_magic_quotes_runtime"))
  3451. || (!function_exists("set_magic_quotes_runtime"))) {
  3452. return $v_result;
  3453. }
  3454.  
  3455. if ($this->magic_quotes_status != -1) {
  3456. return $v_result;
  3457. }
  3458.  
  3459. if ($this->magic_quotes_status == 1) {
  3460. @set_magic_quotes_runtime($this->magic_quotes_status);
  3461. }
  3462.  
  3463. return $v_result;
  3464. }
  3465.  
  3466.  
  3467. }
  3468.  
  3469. function PclZipUtilPathReduction($p_dir)
  3470. {
  3471. $v_result = "";
  3472. if ($p_dir != "") {
  3473. $v_list = explode("/", $p_dir);
  3474.  
  3475. $v_skip = 0;
  3476. for ($i=sizeof($v_list)-1; $i>=0; $i--) {
  3477.  
  3478. if ($v_list[$i] == ".") {
  3479.  
  3480. }
  3481. else if ($v_list[$i] == "..") {
  3482. $v_skip++;
  3483. }
  3484. else if ($v_list[$i] == "") {
  3485. if ($i == 0) {
  3486. $v_result = "/".$v_result;
  3487. if ($v_skip > 0) {
  3488. $v_result = $p_dir;
  3489. $v_skip = 0;
  3490. }
  3491. }
  3492. else if ($i == (sizeof($v_list)-1)) {
  3493. $v_result = $v_list[$i];
  3494. }
  3495. else {
  3496. }
  3497. }
  3498. else {
  3499.  
  3500. if ($v_skip > 0) {
  3501. $v_skip--;
  3502. }
  3503. else {
  3504. $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?"/".$v_result:"");
  3505. }
  3506. }
  3507. }
  3508. if ($v_skip > 0) {
  3509. while ($v_skip > 0) {
  3510. $v_result = '../'.$v_result;
  3511. $v_skip--;
  3512. }
  3513. }
  3514. }
  3515.  
  3516. return $v_result;
  3517. }
  3518.  
  3519. function PclZipUtilPathInclusion($p_dir, $p_path)
  3520. {
  3521. $v_result = 1;
  3522. if ( ($p_dir == '.')
  3523. || ((strlen($p_dir) >=2) && (substr($p_dir, 0, 2) == './'))) {
  3524. $p_dir = PclZipUtilTranslateWinPath(getcwd(), FALSE).'/'.substr($p_dir, 1);
  3525. }
  3526. if ( ($p_path == '.')
  3527. || ((strlen($p_path) >=2) && (substr($p_path, 0, 2) == './'))) {
  3528. $p_path = PclZipUtilTranslateWinPath(getcwd(), FALSE).'/'.substr($p_path, 1);
  3529. }
  3530.  
  3531. $v_list_dir = explode("/", $p_dir);
  3532. $v_list_dir_size = sizeof($v_list_dir);
  3533. $v_list_path = explode("/", $p_path);
  3534. $v_list_path_size = sizeof($v_list_path);
  3535.  
  3536. $i = 0;
  3537. $j = 0;
  3538. while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
  3539.  
  3540. if ($v_list_dir[$i] == '') {
  3541. $i++;
  3542. continue;
  3543. }
  3544. if ($v_list_path[$j] == '') {
  3545. $j++;
  3546. continue;
  3547. }
  3548.  
  3549. if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ( $v_list_path[$j] != '')) {
  3550. $v_result = 0;
  3551. }
  3552.  
  3553. $i++;
  3554. $j++;
  3555. }
  3556.  
  3557. if ($v_result) {
  3558. while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) $j++;
  3559. while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) $i++;
  3560.  
  3561. if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
  3562. $v_result = 2;
  3563. }
  3564. else if ($i < $v_list_dir_size) {
  3565. $v_result = 0;
  3566. }
  3567. }
  3568.  
  3569. return $v_result;
  3570. }
  3571.  
  3572. function PclZipUtilCopyBlock($p_src, $p_dest, $p_size, $p_mode=0)
  3573. {
  3574. $v_result = 1;
  3575.  
  3576. if ($p_mode==0)
  3577. {
  3578. while ($p_size != 0)
  3579. {
  3580. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  3581. $v_buffer = @fread($p_src, $v_read_size);
  3582. @fwrite($p_dest, $v_buffer, $v_read_size);
  3583. $p_size -= $v_read_size;
  3584. }
  3585. }
  3586. else if ($p_mode==1)
  3587. {
  3588. while ($p_size != 0)
  3589. {
  3590. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  3591. $v_buffer = @gzread($p_src, $v_read_size);
  3592. @fwrite($p_dest, $v_buffer, $v_read_size);
  3593. $p_size -= $v_read_size;
  3594. }
  3595. }
  3596. else if ($p_mode==2)
  3597. {
  3598. while ($p_size != 0)
  3599. {
  3600. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  3601. $v_buffer = @fread($p_src, $v_read_size);
  3602. @gzwrite($p_dest, $v_buffer, $v_read_size);
  3603. $p_size -= $v_read_size;
  3604. }
  3605. }
  3606. else if ($p_mode==3)
  3607. {
  3608. while ($p_size != 0)
  3609. {
  3610. $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
  3611. $v_buffer = @gzread($p_src, $v_read_size);
  3612. @gzwrite($p_dest, $v_buffer, $v_read_size);
  3613. $p_size -= $v_read_size;
  3614. }
  3615. }
  3616.  
  3617. return $v_result;
  3618. }
  3619.  
  3620. function PclZipUtilRename($p_src, $p_dest)
  3621. {
  3622. $v_result = 1;
  3623.  
  3624. if (!@rename($p_src, $p_dest)) {
  3625.  
  3626. if (!@copy($p_src, $p_dest)) {
  3627. $v_result = 0;
  3628. }
  3629. else if (!@unlink($p_src)) {
  3630. $v_result = 0;
  3631. }
  3632. }
  3633.  
  3634. return $v_result;
  3635. }
  3636.  
  3637. function PclZipUtilOptionText($p_option)
  3638. {
  3639. $v_list = get_defined_constants();
  3640. for (reset($v_list); $v_key = key($v_list); next($v_list)) {
  3641. $v_prefix = substr($v_key, 0, 10);
  3642. if (( ($v_prefix == 'PCLZIP_OPT')
  3643. || ($v_prefix == 'PCLZIP_CB_')
  3644. || ($v_prefix == 'PCLZIP_ATT'))
  3645. && ($v_list[$v_key] == $p_option)) {
  3646. return $v_key;
  3647. }
  3648. }
  3649. $v_result = 'Unknown';
  3650.  
  3651. return $v_result;
  3652. }
  3653.  
  3654. function PclZipUtilTranslateWinPath($p_path, $p_remove_disk_letter=true)
  3655. {
  3656. if (stristr(php_uname(), 'windows')) {
  3657. if (($p_remove_disk_letter) && (($v_position = strpos($p_path, ':')) != false)) {
  3658. $p_path = substr($p_path, $v_position+1);
  3659. }
  3660. if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0,1) == '\\')) {
  3661. $p_path = strtr($p_path, '\\', '/');
  3662. }
  3663. }
  3664. return $p_path;
  3665. }