PageRenderTime 56ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/zip/php_zip.c

http://github.com/infusion/PHP
C | 2859 lines | 2144 code | 448 blank | 267 comment | 486 complexity | 07956452bfcd7c5c7c408c6cc44a0413 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause

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

  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2011 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt. |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Piere-Alain Joye <pierre@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id: php_zip.c 308107 2011-02-07 16:20:16Z pajoye $ */
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "php.h"
  23. #include "php_ini.h"
  24. #include "ext/standard/info.h"
  25. #include "ext/standard/file.h"
  26. #include "ext/standard/php_string.h"
  27. #include "ext/pcre/php_pcre.h"
  28. #include "php_zip.h"
  29. #include "lib/zip.h"
  30. #include "lib/zipint.h"
  31. /* zip_open is a macro for renaming libzip zipopen, so we need to use PHP_NAMED_FUNCTION */
  32. static PHP_NAMED_FUNCTION(zif_zip_open);
  33. static PHP_NAMED_FUNCTION(zif_zip_read);
  34. static PHP_NAMED_FUNCTION(zif_zip_close);
  35. static PHP_NAMED_FUNCTION(zif_zip_entry_read);
  36. static PHP_NAMED_FUNCTION(zif_zip_entry_filesize);
  37. static PHP_NAMED_FUNCTION(zif_zip_entry_name);
  38. static PHP_NAMED_FUNCTION(zif_zip_entry_compressedsize);
  39. static PHP_NAMED_FUNCTION(zif_zip_entry_compressionmethod);
  40. static PHP_NAMED_FUNCTION(zif_zip_entry_open);
  41. static PHP_NAMED_FUNCTION(zif_zip_entry_close);
  42. #ifdef HAVE_GLOB
  43. #ifndef PHP_WIN32
  44. #include <glob.h>
  45. #else
  46. #include "win32/glob.h"
  47. #endif
  48. #endif
  49. /* {{{ Resource le */
  50. static int le_zip_dir;
  51. #define le_zip_dir_name "Zip Directory"
  52. static int le_zip_entry;
  53. #define le_zip_entry_name "Zip Entry"
  54. /* }}} */
  55. /* {{{ PHP_ZIP_STAT_INDEX(za, index, flags, sb) */
  56. #define PHP_ZIP_STAT_INDEX(za, index, flags, sb) \
  57. if (zip_stat_index(za, index, flags, &sb) != 0) { \
  58. RETURN_FALSE; \
  59. }
  60. /* }}} */
  61. /* {{{ PHP_ZIP_STAT_PATH(za, path, path_len, flags, sb) */
  62. #define PHP_ZIP_STAT_PATH(za, path, path_len, flags, sb) \
  63. if (path_len < 1) { \
  64. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Empty string as entry name"); \
  65. RETURN_FALSE; \
  66. } \
  67. if (zip_stat(za, path, flags, &sb) != 0) { \
  68. RETURN_FALSE; \
  69. }
  70. /* }}} */
  71. /* {{{ PHP_ZIP_SET_FILE_COMMENT(za, index, comment, comment_len) */
  72. #define PHP_ZIP_SET_FILE_COMMENT(za, index, comment, comment_len) \
  73. if (comment_len == 0) { \
  74. /* Passing NULL remove the existing comment */ \
  75. if (zip_set_file_comment(intern, index, NULL, 0) < 0) { \
  76. RETURN_FALSE; \
  77. } \
  78. } else if (zip_set_file_comment(intern, index, comment, comment_len) < 0) { \
  79. RETURN_FALSE; \
  80. } \
  81. RETURN_TRUE;
  82. /* }}} */
  83. #if (PHP_MAJOR_VERSION < 6)
  84. # define add_ascii_assoc_string add_assoc_string
  85. # define add_ascii_assoc_long add_assoc_long
  86. #endif
  87. /* Flatten a path by making a relative path (to .)*/
  88. static char * php_zip_make_relative_path(char *path, int path_len) /* {{{ */
  89. {
  90. char *path_begin = path;
  91. size_t i;
  92. if (IS_SLASH(path[0])) {
  93. return path + 1;
  94. }
  95. if (path_len < 1 || path == NULL) {
  96. return NULL;
  97. }
  98. i = path_len;
  99. while (1) {
  100. while (i > 0 && !IS_SLASH(path[i])) {
  101. i--;
  102. }
  103. if (!i) {
  104. return path;
  105. }
  106. if (i >= 2 && (path[i -1] == '.' || path[i -1] == ':')) {
  107. /* i is the position of . or :, add 1 for / */
  108. path_begin = path + i + 1;
  109. break;
  110. }
  111. i--;
  112. }
  113. return path_begin;
  114. }
  115. /* }}} */
  116. #ifdef PHP_ZIP_USE_OO
  117. /* {{{ php_zip_extract_file */
  118. static int php_zip_extract_file(struct zip * za, char *dest, char *file, int file_len TSRMLS_DC)
  119. {
  120. php_stream_statbuf ssb;
  121. struct zip_file *zf;
  122. struct zip_stat sb;
  123. char b[8192];
  124. int n, len, ret;
  125. php_stream *stream;
  126. char *fullpath;
  127. char *file_dirname_fullpath;
  128. char file_dirname[MAXPATHLEN];
  129. size_t dir_len;
  130. char *file_basename;
  131. size_t file_basename_len;
  132. int is_dir_only = 0;
  133. char *path_cleaned;
  134. size_t path_cleaned_len;
  135. cwd_state new_state;
  136. new_state.cwd = (char*)malloc(1);
  137. new_state.cwd[0] = '\0';
  138. new_state.cwd_length = 0;
  139. /* Clean/normlize the path and then transform any path (absolute or relative)
  140. to a path relative to cwd (../../mydir/foo.txt > mydir/foo.txt)
  141. */
  142. virtual_file_ex(&new_state, file, NULL, CWD_EXPAND);
  143. path_cleaned = php_zip_make_relative_path(new_state.cwd, new_state.cwd_length);
  144. if(!path_cleaned) {
  145. return 0;
  146. }
  147. path_cleaned_len = strlen(path_cleaned);
  148. if (path_cleaned_len >= MAXPATHLEN || zip_stat(za, file, 0, &sb) != 0) {
  149. return 0;
  150. }
  151. /* it is a directory only, see #40228 */
  152. if (path_cleaned_len > 1 && IS_SLASH(path_cleaned[path_cleaned_len - 1])) {
  153. len = spprintf(&file_dirname_fullpath, 0, "%s/%s", dest, file);
  154. is_dir_only = 1;
  155. } else {
  156. memcpy(file_dirname, path_cleaned, path_cleaned_len);
  157. dir_len = php_dirname(file_dirname, path_cleaned_len);
  158. if (dir_len <= 0 || (dir_len == 1 && file_dirname[0] == '.')) {
  159. len = spprintf(&file_dirname_fullpath, 0, "%s", dest);
  160. } else {
  161. len = spprintf(&file_dirname_fullpath, 0, "%s/%s", dest, file_dirname);
  162. }
  163. php_basename(path_cleaned, path_cleaned_len, NULL, 0, &file_basename, (size_t *)&file_basename_len TSRMLS_CC);
  164. if (ZIP_OPENBASEDIR_CHECKPATH(file_dirname_fullpath)) {
  165. efree(file_dirname_fullpath);
  166. efree(file_basename);
  167. free(new_state.cwd);
  168. return 0;
  169. }
  170. }
  171. /* let see if the path already exists */
  172. if (php_stream_stat_path_ex(file_dirname_fullpath, PHP_STREAM_URL_STAT_QUIET, &ssb, NULL) < 0) {
  173. #if defined(PHP_WIN32) && (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 1)
  174. char *e;
  175. e = file_dirname_fullpath;
  176. while (*e) {
  177. if (*e == '/') {
  178. *e = DEFAULT_SLASH;
  179. }
  180. e++;
  181. }
  182. #endif
  183. ret = php_stream_mkdir(file_dirname_fullpath, 0777, PHP_STREAM_MKDIR_RECURSIVE|REPORT_ERRORS, NULL);
  184. if (!ret) {
  185. efree(file_dirname_fullpath);
  186. if (!is_dir_only) {
  187. efree(file_basename);
  188. free(new_state.cwd);
  189. }
  190. return 0;
  191. }
  192. }
  193. /* it is a standalone directory, job done */
  194. if (is_dir_only) {
  195. efree(file_dirname_fullpath);
  196. free(new_state.cwd);
  197. return 1;
  198. }
  199. len = spprintf(&fullpath, 0, "%s/%s", file_dirname_fullpath, file_basename);
  200. if (!len) {
  201. efree(file_dirname_fullpath);
  202. efree(file_basename);
  203. free(new_state.cwd);
  204. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot build full extract path");
  205. return 0;
  206. } else if (len > MAXPATHLEN) {
  207. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Full extraction path exceed MAXPATHLEN (%i)", MAXPATHLEN);
  208. efree(file_dirname_fullpath);
  209. efree(file_basename);
  210. free(new_state.cwd);
  211. return 0;
  212. }
  213. /* check again the full path, not sure if it
  214. * is required, does a file can have a different
  215. * safemode status as its parent folder?
  216. */
  217. if (ZIP_OPENBASEDIR_CHECKPATH(fullpath)) {
  218. efree(fullpath);
  219. efree(file_dirname_fullpath);
  220. efree(file_basename);
  221. free(new_state.cwd);
  222. return 0;
  223. }
  224. #if PHP_API_VERSION < 20100412
  225. stream = php_stream_open_wrapper(fullpath, "w+b", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
  226. #else
  227. stream = php_stream_open_wrapper(fullpath, "w+b", REPORT_ERRORS, NULL);
  228. #endif
  229. if (stream == NULL) {
  230. n = -1;
  231. goto done;
  232. }
  233. zf = zip_fopen(za, file, 0);
  234. if (zf == NULL) {
  235. n = -1;
  236. php_stream_close(stream);
  237. goto done;
  238. }
  239. n = 0;
  240. if (stream == NULL) {
  241. int ret = zip_fclose(zf);
  242. efree(fullpath);
  243. efree(file_basename);
  244. efree(file_dirname_fullpath);
  245. free(new_state.cwd);
  246. return 0;
  247. }
  248. while ((n=zip_fread(zf, b, sizeof(b))) > 0) {
  249. php_stream_write(stream, b, n);
  250. }
  251. php_stream_close(stream);
  252. n = zip_fclose(zf);
  253. done:
  254. efree(fullpath);
  255. efree(file_basename);
  256. efree(file_dirname_fullpath);
  257. free(new_state.cwd);
  258. if (n<0) {
  259. return 0;
  260. } else {
  261. return 1;
  262. }
  263. }
  264. /* }}} */
  265. static int php_zip_add_file(struct zip *za, const char *filename, size_t filename_len,
  266. char *entry_name, size_t entry_name_len, long offset_start, long offset_len TSRMLS_DC) /* {{{ */
  267. {
  268. struct zip_source *zs;
  269. int cur_idx;
  270. char resolved_path[MAXPATHLEN];
  271. if (ZIP_OPENBASEDIR_CHECKPATH(filename)) {
  272. return -1;
  273. }
  274. if (!expand_filepath(filename, resolved_path TSRMLS_CC)) {
  275. return -1;
  276. }
  277. zs = zip_source_file(za, resolved_path, offset_start, offset_len);
  278. if (!zs) {
  279. return -1;
  280. }
  281. cur_idx = zip_name_locate(za, (const char *)entry_name, 0);
  282. /* TODO: fix _zip_replace */
  283. if (cur_idx<0) {
  284. /* reset the error */
  285. if (za->error.str) {
  286. _zip_error_fini(&za->error);
  287. }
  288. _zip_error_init(&za->error);
  289. } else {
  290. if (zip_delete(za, cur_idx) == -1) {
  291. zip_source_free(zs);
  292. return -1;
  293. }
  294. }
  295. if (zip_add(za, entry_name, zs) == -1) {
  296. return -1;
  297. } else {
  298. return 1;
  299. }
  300. }
  301. /* }}} */
  302. static int php_zip_parse_options(zval *options, long *remove_all_path,
  303. char **remove_path, int *remove_path_len, char **add_path, int *add_path_len TSRMLS_DC) /* {{{ */
  304. {
  305. zval **option;
  306. if (zend_hash_find(HASH_OF(options), "remove_all_path", sizeof("remove_all_path"), (void **)&option) == SUCCESS) {
  307. long opt;
  308. if (Z_TYPE_PP(option) != IS_LONG) {
  309. zval tmp = **option;
  310. zval_copy_ctor(&tmp);
  311. convert_to_long(&tmp);
  312. opt = Z_LVAL(tmp);
  313. } else {
  314. opt = Z_LVAL_PP(option);
  315. }
  316. *remove_all_path = opt;
  317. }
  318. /* If I add more options, it would make sense to create a nice static struct and loop over it. */
  319. if (zend_hash_find(HASH_OF(options), "remove_path", sizeof("remove_path"), (void **)&option) == SUCCESS) {
  320. if (Z_TYPE_PP(option) != IS_STRING) {
  321. php_error_docref(NULL TSRMLS_CC, E_WARNING, "remove_path option expected to be a string");
  322. return -1;
  323. }
  324. if (Z_STRLEN_PP(option) < 1) {
  325. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Empty string given as remove_path option");
  326. return -1;
  327. }
  328. if (Z_STRLEN_PP(option) >= MAXPATHLEN) {
  329. php_error_docref(NULL TSRMLS_CC, E_WARNING, "remove_path string is too long (max: %i, %i given)",
  330. MAXPATHLEN - 1, Z_STRLEN_PP(option));
  331. return -1;
  332. }
  333. *remove_path_len = Z_STRLEN_PP(option);
  334. *remove_path = Z_STRVAL_PP(option);
  335. }
  336. if (zend_hash_find(HASH_OF(options), "add_path", sizeof("add_path"), (void **)&option) == SUCCESS) {
  337. if (Z_TYPE_PP(option) != IS_STRING) {
  338. php_error_docref(NULL TSRMLS_CC, E_WARNING, "add_path option expected to be a string");
  339. return -1;
  340. }
  341. if (Z_STRLEN_PP(option) < 1) {
  342. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Empty string given as the add_path option");
  343. return -1;
  344. }
  345. if (Z_STRLEN_PP(option) >= MAXPATHLEN) {
  346. php_error_docref(NULL TSRMLS_CC, E_WARNING, "add_path string too long (max: %i, %i given)",
  347. MAXPATHLEN - 1, Z_STRLEN_PP(option));
  348. return -1;
  349. }
  350. *add_path_len = Z_STRLEN_PP(option);
  351. *add_path = Z_STRVAL_PP(option);
  352. }
  353. return 1;
  354. }
  355. /* }}} */
  356. /* {{{ REGISTER_ZIP_CLASS_CONST_LONG */
  357. #define REGISTER_ZIP_CLASS_CONST_LONG(const_name, value) \
  358. zend_declare_class_constant_long(zip_class_entry, const_name, sizeof(const_name)-1, (long)value TSRMLS_CC);
  359. /* }}} */
  360. /* {{{ ZIP_FROM_OBJECT */
  361. #define ZIP_FROM_OBJECT(intern, object) \
  362. { \
  363. ze_zip_object *obj = (ze_zip_object*) zend_object_store_get_object(object TSRMLS_CC); \
  364. intern = obj->za; \
  365. if (!intern) { \
  366. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid or unitialized Zip object"); \
  367. RETURN_FALSE; \
  368. } \
  369. }
  370. /* }}} */
  371. /* {{{ RETURN_SB(sb) */
  372. #define RETURN_SB(sb) \
  373. { \
  374. array_init(return_value); \
  375. add_ascii_assoc_string(return_value, "name", (char *)(sb)->name, 1); \
  376. add_ascii_assoc_long(return_value, "index", (long) (sb)->index); \
  377. add_ascii_assoc_long(return_value, "crc", (long) (sb)->crc); \
  378. add_ascii_assoc_long(return_value, "size", (long) (sb)->size); \
  379. add_ascii_assoc_long(return_value, "mtime", (long) (sb)->mtime); \
  380. add_ascii_assoc_long(return_value, "comp_size", (long) (sb)->comp_size); \
  381. add_ascii_assoc_long(return_value, "comp_method", (long) (sb)->comp_method); \
  382. }
  383. /* }}} */
  384. static int php_zip_status(struct zip *za TSRMLS_DC) /* {{{ */
  385. {
  386. int zep, syp;
  387. zip_error_get(za, &zep, &syp);
  388. return zep;
  389. }
  390. /* }}} */
  391. static int php_zip_status_sys(struct zip *za TSRMLS_DC) /* {{{ */
  392. {
  393. int zep, syp;
  394. zip_error_get(za, &zep, &syp);
  395. return syp;
  396. }
  397. /* }}} */
  398. static int php_zip_get_num_files(struct zip *za TSRMLS_DC) /* {{{ */
  399. {
  400. return zip_get_num_files(za);
  401. }
  402. /* }}} */
  403. static char * php_zipobj_get_filename(ze_zip_object *obj TSRMLS_DC) /* {{{ */
  404. {
  405. if (!obj) {
  406. return NULL;
  407. }
  408. if (obj->filename) {
  409. return obj->filename;
  410. }
  411. return NULL;
  412. }
  413. /* }}} */
  414. static char * php_zipobj_get_zip_comment(struct zip *za, int *len TSRMLS_DC) /* {{{ */
  415. {
  416. if (za) {
  417. return (char *)zip_get_archive_comment(za, len, 0);
  418. }
  419. return NULL;
  420. }
  421. /* }}} */
  422. #ifdef HAVE_GLOB /* {{{ */
  423. #ifndef GLOB_ONLYDIR
  424. #define GLOB_ONLYDIR (1<<30)
  425. #define GLOB_EMULATE_ONLYDIR
  426. #define GLOB_FLAGMASK (~GLOB_ONLYDIR)
  427. #else
  428. #define GLOB_FLAGMASK (~0)
  429. #endif
  430. #endif /* }}} */
  431. int php_zip_glob(char *pattern, int pattern_len, long flags, zval *return_value TSRMLS_DC) /* {{{ */
  432. {
  433. #ifdef HAVE_GLOB
  434. char cwd[MAXPATHLEN];
  435. int cwd_skip = 0;
  436. #ifdef ZTS
  437. char work_pattern[MAXPATHLEN];
  438. char *result;
  439. #endif
  440. glob_t globbuf;
  441. int n;
  442. int ret;
  443. #ifdef ZTS
  444. if (!IS_ABSOLUTE_PATH(pattern, pattern_len)) {
  445. result = VCWD_GETCWD(cwd, MAXPATHLEN);
  446. if (!result) {
  447. cwd[0] = '\0';
  448. }
  449. #ifdef PHP_WIN32
  450. if (IS_SLASH(*pattern)) {
  451. cwd[2] = '\0';
  452. }
  453. #endif
  454. cwd_skip = strlen(cwd)+1;
  455. snprintf(work_pattern, MAXPATHLEN, "%s%c%s", cwd, DEFAULT_SLASH, pattern);
  456. pattern = work_pattern;
  457. }
  458. #endif
  459. globbuf.gl_offs = 0;
  460. if (0 != (ret = glob(pattern, flags & GLOB_FLAGMASK, NULL, &globbuf))) {
  461. #ifdef GLOB_NOMATCH
  462. if (GLOB_NOMATCH == ret) {
  463. /* Some glob implementation simply return no data if no matches
  464. were found, others return the GLOB_NOMATCH error code.
  465. We don't want to treat GLOB_NOMATCH as an error condition
  466. so that PHP glob() behaves the same on both types of
  467. implementations and so that 'foreach (glob() as ...'
  468. can be used for simple glob() calls without further error
  469. checking.
  470. */
  471. array_init(return_value);
  472. return 0;
  473. }
  474. #endif
  475. return 0;
  476. }
  477. /* now catch the FreeBSD style of "no matches" */
  478. if (!globbuf.gl_pathc || !globbuf.gl_pathv) {
  479. array_init(return_value);
  480. return 0;
  481. }
  482. /* we assume that any glob pattern will match files from one directory only
  483. so checking the dirname of the first match should be sufficient */
  484. strncpy(cwd, globbuf.gl_pathv[0], MAXPATHLEN);
  485. if (ZIP_OPENBASEDIR_CHECKPATH(cwd)) {
  486. return -1;
  487. }
  488. array_init(return_value);
  489. for (n = 0; n < globbuf.gl_pathc; n++) {
  490. /* we need to do this everytime since GLOB_ONLYDIR does not guarantee that
  491. * all directories will be filtered. GNU libc documentation states the
  492. * following:
  493. * If the information about the type of the file is easily available
  494. * non-directories will be rejected but no extra work will be done to
  495. * determine the information for each file. I.e., the caller must still be
  496. * able to filter directories out.
  497. */
  498. if (flags & GLOB_ONLYDIR) {
  499. struct stat s;
  500. if (0 != VCWD_STAT(globbuf.gl_pathv[n], &s)) {
  501. continue;
  502. }
  503. if (S_IFDIR != (s.st_mode & S_IFMT)) {
  504. continue;
  505. }
  506. }
  507. add_next_index_string(return_value, globbuf.gl_pathv[n]+cwd_skip, 1);
  508. }
  509. globfree(&globbuf);
  510. return globbuf.gl_pathc;
  511. #else
  512. php_error_docref(NULL TSRMLS_CC, E_ERROR, "Glob support is not available");
  513. return 0;
  514. #endif /* HAVE_GLOB */
  515. }
  516. /* }}} */
  517. int php_zip_pcre(char *regexp, int regexp_len, char *path, int path_len, zval *return_value TSRMLS_DC) /* {{{ */
  518. {
  519. #ifdef ZTS
  520. char cwd[MAXPATHLEN];
  521. int cwd_skip = 0;
  522. char work_path[MAXPATHLEN];
  523. char *result;
  524. #endif
  525. int files_cnt;
  526. char **namelist;
  527. #ifdef ZTS
  528. if (!IS_ABSOLUTE_PATH(path, path_len)) {
  529. result = VCWD_GETCWD(cwd, MAXPATHLEN);
  530. if (!result) {
  531. cwd[0] = '\0';
  532. }
  533. #ifdef PHP_WIN32
  534. if (IS_SLASH(*path)) {
  535. cwd[2] = '\0';
  536. }
  537. #endif
  538. cwd_skip = strlen(cwd)+1;
  539. snprintf(work_path, MAXPATHLEN, "%s%c%s", cwd, DEFAULT_SLASH, path);
  540. path = work_path;
  541. }
  542. #endif
  543. if (ZIP_OPENBASEDIR_CHECKPATH(path)) {
  544. return -1;
  545. }
  546. files_cnt = php_stream_scandir(path, &namelist, NULL, (void *) php_stream_dirent_alphasort);
  547. if (files_cnt > 0) {
  548. pcre *re = NULL;
  549. pcre_extra *pcre_extra = NULL;
  550. int preg_options = 0, i;
  551. re = pcre_get_compiled_regex(regexp, &pcre_extra, &preg_options TSRMLS_CC);
  552. if (!re) {
  553. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid expression");
  554. return -1;
  555. }
  556. array_init(return_value);
  557. /* only the files, directories are ignored */
  558. for (i = 0; i < files_cnt; i++) {
  559. struct stat s;
  560. char fullpath[MAXPATHLEN];
  561. int ovector[3];
  562. int matches;
  563. int namelist_len = strlen(namelist[i]);
  564. if ((namelist_len == 1 && namelist[i][0] == '.') ||
  565. (namelist_len == 2 && namelist[i][0] == '.' && namelist[i][1] == '.')) {
  566. efree(namelist[i]);
  567. continue;
  568. }
  569. if ((path_len + namelist_len + 1) >= MAXPATHLEN) {
  570. php_error_docref(NULL TSRMLS_CC, E_WARNING, "add_path string too long (max: %i, %i given)",
  571. MAXPATHLEN - 1, (path_len + namelist_len + 1));
  572. efree(namelist[i]);
  573. break;
  574. }
  575. snprintf(fullpath, MAXPATHLEN, "%s%c%s", path, DEFAULT_SLASH, namelist[i]);
  576. if (0 != VCWD_STAT(fullpath, &s)) {
  577. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot read <%s>", fullpath);
  578. efree(namelist[i]);
  579. continue;
  580. }
  581. if (S_IFDIR == (s.st_mode & S_IFMT)) {
  582. efree(namelist[i]);
  583. continue;
  584. }
  585. matches = pcre_exec(re, NULL, namelist[i], strlen(namelist[i]), 0, 0, ovector, 3);
  586. /* 0 means that the vector is too small to hold all the captured substring offsets */
  587. if (matches < 0) {
  588. efree(namelist[i]);
  589. continue;
  590. }
  591. add_next_index_string(return_value, fullpath, 1);
  592. efree(namelist[i]);
  593. }
  594. efree(namelist);
  595. }
  596. return files_cnt;
  597. }
  598. /* }}} */
  599. #endif
  600. /* {{{ arginfo */
  601. ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_open, 0, 0, 1)
  602. ZEND_ARG_INFO(0, filename)
  603. ZEND_END_ARG_INFO()
  604. ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_close, 0, 0, 1)
  605. ZEND_ARG_INFO(0, zip)
  606. ZEND_END_ARG_INFO()
  607. ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_read, 0, 0, 1)
  608. ZEND_ARG_INFO(0, zip)
  609. ZEND_END_ARG_INFO()
  610. ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_entry_open, 0, 0, 2)
  611. ZEND_ARG_INFO(0, zip_dp)
  612. ZEND_ARG_INFO(0, zip_entry)
  613. ZEND_ARG_INFO(0, mode)
  614. ZEND_END_ARG_INFO()
  615. ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_entry_close, 0, 0, 1)
  616. ZEND_ARG_INFO(0, zip_ent)
  617. ZEND_END_ARG_INFO()
  618. ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_entry_read, 0, 0, 1)
  619. ZEND_ARG_INFO(0, zip_entry)
  620. ZEND_ARG_INFO(0, len)
  621. ZEND_END_ARG_INFO()
  622. ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_entry_name, 0, 0, 1)
  623. ZEND_ARG_INFO(0, zip_entry)
  624. ZEND_END_ARG_INFO()
  625. ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_entry_compressedsize, 0, 0, 1)
  626. ZEND_ARG_INFO(0, zip_entry)
  627. ZEND_END_ARG_INFO()
  628. ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_entry_filesize, 0, 0, 1)
  629. ZEND_ARG_INFO(0, zip_entry)
  630. ZEND_END_ARG_INFO()
  631. ZEND_BEGIN_ARG_INFO_EX(arginfo_zip_entry_compressionmethod, 0, 0, 1)
  632. ZEND_ARG_INFO(0, zip_entry)
  633. ZEND_END_ARG_INFO()
  634. /* }}} */
  635. /* {{{ zend_function_entry */
  636. static const zend_function_entry zip_functions[] = {
  637. ZEND_RAW_FENTRY("zip_open", zif_zip_open, arginfo_zip_open, 0)
  638. ZEND_RAW_FENTRY("zip_close", zif_zip_close, arginfo_zip_close, 0)
  639. ZEND_RAW_FENTRY("zip_read", zif_zip_read, arginfo_zip_read, 0)
  640. PHP_FE(zip_entry_open, arginfo_zip_entry_open)
  641. PHP_FE(zip_entry_close, arginfo_zip_entry_close)
  642. PHP_FE(zip_entry_read, arginfo_zip_entry_read)
  643. PHP_FE(zip_entry_filesize, arginfo_zip_entry_filesize)
  644. PHP_FE(zip_entry_name, arginfo_zip_entry_name)
  645. PHP_FE(zip_entry_compressedsize, arginfo_zip_entry_compressedsize)
  646. PHP_FE(zip_entry_compressionmethod, arginfo_zip_entry_compressionmethod)
  647. {NULL, NULL, NULL}
  648. };
  649. /* }}} */
  650. /* {{{ ZE2 OO definitions */
  651. #ifdef PHP_ZIP_USE_OO
  652. static zend_class_entry *zip_class_entry;
  653. static zend_object_handlers zip_object_handlers;
  654. static HashTable zip_prop_handlers;
  655. typedef int (*zip_read_int_t)(struct zip *za TSRMLS_DC);
  656. typedef char *(*zip_read_const_char_t)(struct zip *za, int *len TSRMLS_DC);
  657. typedef char *(*zip_read_const_char_from_ze_t)(ze_zip_object *obj TSRMLS_DC);
  658. typedef struct _zip_prop_handler {
  659. zip_read_int_t read_int_func;
  660. zip_read_const_char_t read_const_char_func;
  661. zip_read_const_char_from_ze_t read_const_char_from_obj_func;
  662. int type;
  663. } zip_prop_handler;
  664. #endif
  665. /* }}} */
  666. #ifdef PHP_ZIP_USE_OO
  667. static void php_zip_register_prop_handler(HashTable *prop_handler, char *name, zip_read_int_t read_int_func, zip_read_const_char_t read_char_func, zip_read_const_char_from_ze_t read_char_from_obj_func, int rettype TSRMLS_DC) /* {{{ */
  668. {
  669. zip_prop_handler hnd;
  670. hnd.read_const_char_func = read_char_func;
  671. hnd.read_int_func = read_int_func;
  672. hnd.read_const_char_from_obj_func = read_char_from_obj_func;
  673. hnd.type = rettype;
  674. zend_hash_add(prop_handler, name, strlen(name)+1, &hnd, sizeof(zip_prop_handler), NULL);
  675. }
  676. /* }}} */
  677. static int php_zip_property_reader(ze_zip_object *obj, zip_prop_handler *hnd, zval **retval, int newzval TSRMLS_DC) /* {{{ */
  678. {
  679. const char *retchar = NULL;
  680. int retint = 0;
  681. int len = 0;
  682. if (obj && obj->za != NULL) {
  683. if (hnd->read_const_char_func) {
  684. retchar = hnd->read_const_char_func(obj->za, &len TSRMLS_CC);
  685. } else {
  686. if (hnd->read_int_func) {
  687. retint = hnd->read_int_func(obj->za TSRMLS_CC);
  688. if (retint == -1) {
  689. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Internal zip error returned");
  690. return FAILURE;
  691. }
  692. } else {
  693. if (hnd->read_const_char_from_obj_func) {
  694. retchar = hnd->read_const_char_from_obj_func(obj TSRMLS_CC);
  695. len = strlen(retchar);
  696. }
  697. }
  698. }
  699. }
  700. if (newzval) {
  701. ALLOC_ZVAL(*retval);
  702. }
  703. switch (hnd->type) {
  704. case IS_STRING:
  705. if (retchar) {
  706. ZVAL_STRINGL(*retval, (char *) retchar, len, 1);
  707. } else {
  708. ZVAL_EMPTY_STRING(*retval);
  709. }
  710. break;
  711. case IS_BOOL:
  712. ZVAL_BOOL(*retval, (long)retint);
  713. break;
  714. case IS_LONG:
  715. ZVAL_LONG(*retval, (long)retint);
  716. break;
  717. default:
  718. ZVAL_NULL(*retval);
  719. }
  720. return SUCCESS;
  721. }
  722. /* }}} */
  723. static zval **php_zip_get_property_ptr_ptr(zval *object, zval *member TSRMLS_DC) /* {{{ */
  724. {
  725. ze_zip_object *obj;
  726. zval tmp_member;
  727. zval **retval = NULL;
  728. zip_prop_handler *hnd;
  729. zend_object_handlers *std_hnd;
  730. int ret;
  731. if (member->type != IS_STRING) {
  732. tmp_member = *member;
  733. zval_copy_ctor(&tmp_member);
  734. convert_to_string(&tmp_member);
  735. member = &tmp_member;
  736. }
  737. ret = FAILURE;
  738. obj = (ze_zip_object *)zend_objects_get_address(object TSRMLS_CC);
  739. if (obj->prop_handler != NULL) {
  740. ret = zend_hash_find(obj->prop_handler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void **) &hnd);
  741. }
  742. if (ret == FAILURE) {
  743. std_hnd = zend_get_std_object_handlers();
  744. retval = std_hnd->get_property_ptr_ptr(object, member TSRMLS_CC);
  745. }
  746. if (member == &tmp_member) {
  747. zval_dtor(member);
  748. }
  749. return retval;
  750. }
  751. /* }}} */
  752. static zval* php_zip_read_property(zval *object, zval *member, int type TSRMLS_DC) /* {{{ */
  753. {
  754. ze_zip_object *obj;
  755. zval tmp_member;
  756. zval *retval;
  757. zip_prop_handler *hnd;
  758. zend_object_handlers *std_hnd;
  759. int ret;
  760. if (member->type != IS_STRING) {
  761. tmp_member = *member;
  762. zval_copy_ctor(&tmp_member);
  763. convert_to_string(&tmp_member);
  764. member = &tmp_member;
  765. }
  766. ret = FAILURE;
  767. obj = (ze_zip_object *)zend_objects_get_address(object TSRMLS_CC);
  768. if (obj->prop_handler != NULL) {
  769. ret = zend_hash_find(obj->prop_handler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void **) &hnd);
  770. }
  771. if (ret == SUCCESS) {
  772. ret = php_zip_property_reader(obj, hnd, &retval, 1 TSRMLS_CC);
  773. if (ret == SUCCESS) {
  774. /* ensure we're creating a temporary variable */
  775. Z_SET_REFCOUNT_P(retval, 0);
  776. } else {
  777. retval = EG(uninitialized_zval_ptr);
  778. }
  779. } else {
  780. std_hnd = zend_get_std_object_handlers();
  781. retval = std_hnd->read_property(object, member, type TSRMLS_CC);
  782. }
  783. if (member == &tmp_member) {
  784. zval_dtor(member);
  785. }
  786. return retval;
  787. }
  788. /* }}} */
  789. static int php_zip_has_property(zval *object, zval *member, int type TSRMLS_DC) /* {{{ */
  790. {
  791. ze_zip_object *obj;
  792. zval tmp_member;
  793. zip_prop_handler *hnd;
  794. zend_object_handlers *std_hnd;
  795. int ret, retval = 0;
  796. if (member->type != IS_STRING) {
  797. tmp_member = *member;
  798. zval_copy_ctor(&tmp_member);
  799. convert_to_string(&tmp_member);
  800. member = &tmp_member;
  801. }
  802. ret = FAILURE;
  803. obj = (ze_zip_object *)zend_objects_get_address(object TSRMLS_CC);
  804. if (obj->prop_handler != NULL) {
  805. ret = zend_hash_find(obj->prop_handler, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, (void **) &hnd);
  806. }
  807. if (ret == SUCCESS) {
  808. zval *tmp;
  809. ALLOC_INIT_ZVAL(tmp);
  810. if (type == 2) {
  811. retval = 1;
  812. } else if (php_zip_property_reader(obj, hnd, &tmp, 0 TSRMLS_CC) == SUCCESS) {
  813. Z_SET_REFCOUNT_P(tmp, 1);
  814. Z_UNSET_ISREF_P(tmp);
  815. if (type == 1) {
  816. retval = zend_is_true(tmp);
  817. } else if (type == 0) {
  818. retval = (Z_TYPE_P(tmp) != IS_NULL);
  819. }
  820. }
  821. zval_ptr_dtor(&tmp);
  822. } else {
  823. std_hnd = zend_get_std_object_handlers();
  824. retval = std_hnd->has_property(object, member, type TSRMLS_CC);
  825. }
  826. if (member == &tmp_member) {
  827. zval_dtor(member);
  828. }
  829. return retval;
  830. }
  831. /* }}} */
  832. static HashTable *php_zip_get_properties(zval *object TSRMLS_DC)/* {{{ */
  833. {
  834. ze_zip_object *obj;
  835. zip_prop_handler *hnd;
  836. HashTable *props;
  837. zval *val;
  838. int ret;
  839. char *key;
  840. uint key_len;
  841. HashPosition pos;
  842. ulong num_key;
  843. obj = (ze_zip_object *)zend_objects_get_address(object TSRMLS_CC);
  844. props = obj->zo.properties;
  845. if (obj->prop_handler == NULL) {
  846. return NULL;
  847. }
  848. zend_hash_internal_pointer_reset_ex(obj->prop_handler, &pos);
  849. while (zend_hash_get_current_data_ex(obj->prop_handler, (void**)&hnd, &pos) == SUCCESS) {
  850. zend_hash_get_current_key_ex(obj->prop_handler, &key, &key_len, &num_key, 0, &pos);
  851. MAKE_STD_ZVAL(val);
  852. ret = php_zip_property_reader(obj, hnd, &val, 0 TSRMLS_CC);
  853. if (ret != SUCCESS) {
  854. val = EG(uninitialized_zval_ptr);
  855. }
  856. zend_hash_update(props, key, key_len, (void *)&val, sizeof(zval *), NULL);
  857. zend_hash_move_forward_ex(obj->prop_handler, &pos);
  858. }
  859. return obj->zo.properties;
  860. }
  861. /* }}} */
  862. static void php_zip_object_free_storage(void *object TSRMLS_DC) /* {{{ */
  863. {
  864. ze_zip_object * intern = (ze_zip_object *) object;
  865. int i;
  866. if (!intern) {
  867. return;
  868. }
  869. if (intern->za) {
  870. if (zip_close(intern->za) != 0) {
  871. _zip_free(intern->za);
  872. }
  873. intern->za = NULL;
  874. }
  875. if (intern->buffers_cnt>0) {
  876. for (i=0; i<intern->buffers_cnt; i++) {
  877. efree(intern->buffers[i]);
  878. }
  879. efree(intern->buffers);
  880. }
  881. intern->za = NULL;
  882. #if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 1 && PHP_RELEASE_VERSION > 2) || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 1) || (PHP_MAJOR_VERSION > 5)
  883. zend_object_std_dtor(&intern->zo TSRMLS_CC);
  884. #else
  885. if (intern->zo.guards) {
  886. zend_hash_destroy(intern->zo.guards);
  887. FREE_HASHTABLE(intern->zo.guards);
  888. }
  889. if (intern->zo.properties) {
  890. zend_hash_destroy(intern->zo.properties);
  891. FREE_HASHTABLE(intern->zo.properties);
  892. }
  893. #endif
  894. if (intern->filename) {
  895. efree(intern->filename);
  896. }
  897. efree(intern);
  898. }
  899. /* }}} */
  900. static zend_object_value php_zip_object_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
  901. {
  902. ze_zip_object *intern;
  903. zval *tmp;
  904. zend_object_value retval;
  905. intern = emalloc(sizeof(ze_zip_object));
  906. memset(&intern->zo, 0, sizeof(zend_object));
  907. intern->za = NULL;
  908. intern->buffers = NULL;
  909. intern->filename = NULL;
  910. intern->buffers_cnt = 0;
  911. intern->prop_handler = &zip_prop_handlers;
  912. #if ((PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 1) || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 1 && PHP_RELEASE_VERSION > 2))
  913. zend_object_std_init(&intern->zo, class_type TSRMLS_CC);
  914. #else
  915. ALLOC_HASHTABLE(intern->zo.properties);
  916. zend_hash_init(intern->zo.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
  917. intern->zo.ce = class_type;
  918. #endif
  919. zend_hash_copy(intern->zo.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref,
  920. (void *) &tmp, sizeof(zval *));
  921. retval.handle = zend_objects_store_put(intern,
  922. NULL,
  923. (zend_objects_free_object_storage_t) php_zip_object_free_storage,
  924. NULL TSRMLS_CC);
  925. retval.handlers = (zend_object_handlers *) & zip_object_handlers;
  926. return retval;
  927. }
  928. /* }}} */
  929. #endif
  930. /* {{{ Resource dtors */
  931. /* {{{ php_zip_free_dir */
  932. static void php_zip_free_dir(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  933. {
  934. zip_rsrc * zip_int = (zip_rsrc *) rsrc->ptr;
  935. if (zip_int) {
  936. if (zip_int->za) {
  937. if (zip_close(zip_int->za) != 0) {
  938. _zip_free(zip_int->za);
  939. }
  940. zip_int->za = NULL;
  941. }
  942. efree(rsrc->ptr);
  943. rsrc->ptr = NULL;
  944. }
  945. }
  946. /* }}} */
  947. /* {{{ php_zip_free_entry */
  948. static void php_zip_free_entry(zend_rsrc_list_entry *rsrc TSRMLS_DC)
  949. {
  950. zip_read_rsrc *zr_rsrc = (zip_read_rsrc *) rsrc->ptr;
  951. if (zr_rsrc) {
  952. if (zr_rsrc->zf) {
  953. zip_fclose(zr_rsrc->zf);
  954. zr_rsrc->zf = NULL;
  955. }
  956. efree(zr_rsrc);
  957. rsrc->ptr = NULL;
  958. }
  959. }
  960. /* }}} */
  961. /* }}}*/
  962. /* reset macro */
  963. /* {{{ function prototypes */
  964. static PHP_MINIT_FUNCTION(zip);
  965. static PHP_MSHUTDOWN_FUNCTION(zip);
  966. static PHP_MINFO_FUNCTION(zip);
  967. /* }}} */
  968. /* {{{ zip_module_entry
  969. */
  970. zend_module_entry zip_module_entry = {
  971. STANDARD_MODULE_HEADER,
  972. "zip",
  973. zip_functions,
  974. PHP_MINIT(zip),
  975. PHP_MSHUTDOWN(zip),
  976. NULL,
  977. NULL,
  978. PHP_MINFO(zip),
  979. PHP_ZIP_VERSION_STRING,
  980. STANDARD_MODULE_PROPERTIES
  981. };
  982. /* }}} */
  983. #ifdef COMPILE_DL_ZIP
  984. ZEND_GET_MODULE(zip)
  985. #endif
  986. /* set macro */
  987. /* {{{ proto resource zip_open(string filename)
  988. Create new zip using source uri for output */
  989. static PHP_NAMED_FUNCTION(zif_zip_open)
  990. {
  991. char *filename;
  992. int filename_len;
  993. char resolved_path[MAXPATHLEN + 1];
  994. zip_rsrc *rsrc_int;
  995. int err = 0;
  996. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) {
  997. return;
  998. }
  999. if (filename_len == 0) {
  1000. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty string as source");
  1001. RETURN_FALSE;
  1002. }
  1003. if (strlen(filename) != filename_len) {
  1004. RETURN_FALSE;
  1005. }
  1006. if (ZIP_OPENBASEDIR_CHECKPATH(filename)) {
  1007. RETURN_FALSE;
  1008. }
  1009. if(!expand_filepath(filename, resolved_path TSRMLS_CC)) {
  1010. RETURN_FALSE;
  1011. }
  1012. rsrc_int = (zip_rsrc *)emalloc(sizeof(zip_rsrc));
  1013. rsrc_int->za = zip_open(resolved_path, 0, &err);
  1014. if (rsrc_int->za == NULL) {
  1015. efree(rsrc_int);
  1016. RETURN_LONG((long)err);
  1017. }
  1018. rsrc_int->index_current = 0;
  1019. rsrc_int->num_files = zip_get_num_files(rsrc_int->za);
  1020. ZEND_REGISTER_RESOURCE(return_value, rsrc_int, le_zip_dir);
  1021. }
  1022. /* }}} */
  1023. /* {{{ proto void zip_close(resource zip)
  1024. Close a Zip archive */
  1025. static PHP_NAMED_FUNCTION(zif_zip_close)
  1026. {
  1027. zval * zip;
  1028. zip_rsrc *z_rsrc = NULL;
  1029. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zip) == FAILURE) {
  1030. return;
  1031. }
  1032. ZEND_FETCH_RESOURCE(z_rsrc, zip_rsrc *, &zip, -1, le_zip_dir_name, le_zip_dir);
  1033. /* really close the zip will break BC :-D */
  1034. zend_list_delete(Z_LVAL_P(zip));
  1035. }
  1036. /* }}} */
  1037. /* {{{ proto resource zip_read(resource zip)
  1038. Returns the next file in the archive */
  1039. static PHP_NAMED_FUNCTION(zif_zip_read)
  1040. {
  1041. zval *zip_dp;
  1042. zip_read_rsrc *zr_rsrc;
  1043. int ret;
  1044. zip_rsrc *rsrc_int;
  1045. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zip_dp) == FAILURE) {
  1046. return;
  1047. }
  1048. ZEND_FETCH_RESOURCE(rsrc_int, zip_rsrc *, &zip_dp, -1, le_zip_dir_name, le_zip_dir);
  1049. if (rsrc_int && rsrc_int->za) {
  1050. if (rsrc_int->index_current >= rsrc_int->num_files) {
  1051. RETURN_FALSE;
  1052. }
  1053. zr_rsrc = emalloc(sizeof(zip_read_rsrc));
  1054. ret = zip_stat_index(rsrc_int->za, rsrc_int->index_current, 0, &zr_rsrc->sb);
  1055. if (ret != 0) {
  1056. efree(zr_rsrc);
  1057. RETURN_FALSE;
  1058. }
  1059. zr_rsrc->zf = zip_fopen_index(rsrc_int->za, rsrc_int->index_current, 0);
  1060. if (zr_rsrc->zf) {
  1061. rsrc_int->index_current++;
  1062. ZEND_REGISTER_RESOURCE(return_value, zr_rsrc, le_zip_entry);
  1063. } else {
  1064. efree(zr_rsrc);
  1065. RETURN_FALSE;
  1066. }
  1067. } else {
  1068. RETURN_FALSE;
  1069. }
  1070. }
  1071. /* }}} */
  1072. /* {{{ proto bool zip_entry_open(resource zip_dp, resource zip_entry [, string mode])
  1073. Open a Zip File, pointed by the resource entry */
  1074. /* Dummy function to follow the old API */
  1075. static PHP_NAMED_FUNCTION(zif_zip_entry_open)
  1076. {
  1077. zval * zip;
  1078. zval * zip_entry;
  1079. char *mode = NULL;
  1080. int mode_len = 0;
  1081. zip_read_rsrc * zr_rsrc;
  1082. zip_rsrc *z_rsrc;
  1083. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr|s", &zip, &zip_entry, &mode, &mode_len) == FAILURE) {
  1084. return;
  1085. }
  1086. ZEND_FETCH_RESOURCE(zr_rsrc, zip_read_rsrc *, &zip_entry, -1, le_zip_entry_name, le_zip_entry);
  1087. ZEND_FETCH_RESOURCE(z_rsrc, zip_rsrc *, &zip, -1, le_zip_dir_name, le_zip_dir);
  1088. if (zr_rsrc->zf != NULL) {
  1089. RETURN_TRUE;
  1090. } else {
  1091. RETURN_FALSE;
  1092. }
  1093. }
  1094. /* }}} */
  1095. /* {{{ proto void zip_entry_close(resource zip_ent)
  1096. Close a zip entry */
  1097. /* another dummy function to fit in the old api*/
  1098. static PHP_NAMED_FUNCTION(zif_zip_entry_close)
  1099. {
  1100. zval * zip_entry;
  1101. zip_read_rsrc * zr_rsrc;
  1102. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zip_entry) == FAILURE) {
  1103. return;
  1104. }
  1105. ZEND_FETCH_RESOURCE(zr_rsrc, zip_read_rsrc *, &zip_entry, -1, le_zip_entry_name, le_zip_entry);
  1106. /* we got a zip_entry resource, be happy */
  1107. RETURN_TRUE;
  1108. }
  1109. /* }}} */
  1110. /* {{{ proto mixed zip_entry_read(resource zip_entry [, int len])
  1111. Read from an open directory entry */
  1112. static PHP_NAMED_FUNCTION(zif_zip_entry_read)
  1113. {
  1114. zval * zip_entry;
  1115. long len = 0;
  1116. zip_read_rsrc * zr_rsrc;
  1117. char *buffer;
  1118. int n = 0;
  1119. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zip_entry, &len) == FAILURE) {
  1120. return;
  1121. }
  1122. ZEND_FETCH_RESOURCE(zr_rsrc, zip_read_rsrc *, &zip_entry, -1, le_zip_entry_name, le_zip_entry);
  1123. if (len <= 0) {
  1124. len = 1024;
  1125. }
  1126. if (zr_rsrc->zf) {
  1127. buffer = safe_emalloc(len, 1, 1);
  1128. n = zip_fread(zr_rsrc->zf, buffer, len);
  1129. if (n > 0) {
  1130. buffer[n] = 0;
  1131. RETURN_STRINGL(buffer, n, 0);
  1132. } else {
  1133. efree(buffer);
  1134. RETURN_EMPTY_STRING()
  1135. }
  1136. } else {
  1137. RETURN_FALSE;
  1138. }
  1139. }
  1140. /* }}} */
  1141. static void php_zip_entry_get_info(INTERNAL_FUNCTION_PARAMETERS, int opt) /* {{{ */
  1142. {
  1143. zval * zip_entry;
  1144. zip_read_rsrc * zr_rsrc;
  1145. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zip_entry) == FAILURE) {
  1146. return;
  1147. }
  1148. ZEND_FETCH_RESOURCE(zr_rsrc, zip_read_rsrc *, &zip_entry, -1, le_zip_entry_name, le_zip_entry);
  1149. if (!zr_rsrc->zf) {
  1150. RETURN_FALSE;
  1151. }
  1152. switch (opt) {
  1153. case 0:
  1154. RETURN_STRING((char *)zr_rsrc->sb.name, 1);
  1155. break;
  1156. case 1:
  1157. RETURN_LONG((long) (zr_rsrc->sb.comp_size));
  1158. break;
  1159. case 2:
  1160. RETURN_LONG((long) (zr_rsrc->sb.size));
  1161. break;
  1162. case 3:
  1163. switch (zr_rsrc->sb.comp_method) {
  1164. case 0:
  1165. RETURN_STRING("stored", 1);
  1166. break;
  1167. case 1:
  1168. RETURN_STRING("shrunk", 1);
  1169. break;
  1170. case 2:
  1171. case 3:
  1172. case 4:
  1173. case 5:
  1174. RETURN_STRING("reduced", 1);
  1175. break;
  1176. case 6:
  1177. RETURN_STRING("imploded", 1);
  1178. break;
  1179. case 7:
  1180. RETURN_STRING("tokenized", 1);
  1181. break;
  1182. case 8:
  1183. RETURN_STRING("deflated", 1);
  1184. break;
  1185. case 9:
  1186. RETURN_STRING("deflatedX", 1);
  1187. break;
  1188. case 10:
  1189. RETURN_STRING("implodedX", 1);
  1190. break;
  1191. default:
  1192. RETURN_FALSE;
  1193. }
  1194. RETURN_LONG((long) (zr_rsrc->sb.comp_method));
  1195. break;
  1196. }
  1197. }
  1198. /* }}} */
  1199. /* {{{ proto string zip_entry_name(resource zip_entry)
  1200. Return the name given a ZZip entry */
  1201. static PHP_NAMED_FUNCTION(zif_zip_entry_name)
  1202. {
  1203. php_zip_entry_get_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  1204. }
  1205. /* }}} */
  1206. /* {{{ proto int zip_entry_compressedsize(resource zip_entry)
  1207. Return the compressed size of a ZZip entry */
  1208. static PHP_NAMED_FUNCTION(zif_zip_entry_compressedsize)
  1209. {
  1210. php_zip_entry_get_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  1211. }
  1212. /* }}} */
  1213. /* {{{ proto int zip_entry_filesize(resource zip_entry)
  1214. Return the actual filesize of a ZZip entry */
  1215. static PHP_NAMED_FUNCTION(zif_zip_entry_filesize)
  1216. {
  1217. php_zip_entry_get_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2);
  1218. }
  1219. /* }}} */
  1220. /* {{{ proto string zip_entry_compressionmethod(resource zip_entry)
  1221. Return a string containing the compression method used on a particular entry */
  1222. static PHP_NAMED_FUNCTION(zif_zip_entry_compressionmethod)
  1223. {
  1224. php_zip_entry_get_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, 3);
  1225. }
  1226. /* }}} */
  1227. #ifdef PHP_ZIP_USE_OO
  1228. /* {{{ proto mixed ZipArchive::open(string source [, int flags])
  1229. Create new zip using source uri for output, return TRUE on success or the error code */
  1230. static ZIPARCHIVE_METHOD(open)
  1231. {
  1232. struct zip *intern;
  1233. char *filename;
  1234. int filename_len;
  1235. int err = 0;
  1236. long flags = 0;
  1237. char resolved_path[MAXPATHLEN];
  1238. zval *this = getThis();
  1239. ze_zip_object *ze_obj = NULL;
  1240. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &filename, &filename_len, &flags) == FAILURE) {
  1241. return;
  1242. }
  1243. if (this) {
  1244. /* We do not use ZIP_FROM_OBJECT, zip init function here */
  1245. ze_obj = (ze_zip_object*) zend_object_store_get_object(this TSRMLS_CC);
  1246. }
  1247. if (filename_len == 0) {
  1248. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty string as source");
  1249. RETURN_FALSE;
  1250. }
  1251. if (strlen(filename) != filename_len) {
  1252. RETURN_FALSE;
  1253. }
  1254. if (ZIP_OPENBASEDIR_CHECKPATH(filename)) {
  1255. RETURN_FALSE;
  1256. }
  1257. if (!expand_filepath(filename, resolved_path TSRMLS_CC)) {
  1258. RETURN_FALSE;
  1259. }
  1260. if (ze_obj->za) {
  1261. /* we already have an opened zip, free it */
  1262. if (zip_close(ze_obj->za) != 0) {
  1263. _zip_free(ze_obj->za);
  1264. }
  1265. ze_obj->za = NULL;
  1266. }
  1267. if (ze_obj->filename) {
  1268. efree(ze_obj->filename);
  1269. ze_obj->filename = NULL;
  1270. }
  1271. intern = zip_open(resolved_path, flags, &err);
  1272. if (!intern || err) {
  1273. RETURN_LONG((long)err);
  1274. }
  1275. ze_obj->filename = estrdup(resolved_path);
  1276. ze_obj->filename_len = filename_len;
  1277. ze_obj->za = intern;
  1278. RETURN_TRUE;
  1279. }
  1280. /* }}} */
  1281. /* {{{ proto bool ZipArchive::close()
  1282. close the zip archive */
  1283. static ZIPARCHIVE_METHOD(close)
  1284. {
  1285. struct zip *intern;
  1286. zval *this = getThis();
  1287. ze_zip_object *ze_obj;
  1288. if (!this) {
  1289. RETURN_FALSE;
  1290. }
  1291. ZIP_FROM_OBJECT(intern, this);
  1292. ze_obj = (ze_zip_object*) zend_object_store_get_object(this TSRMLS_CC);
  1293. if (zip_close(intern)) {
  1294. RETURN_FALSE;
  1295. }
  1296. efree(ze_obj->filename);
  1297. ze_obj->filename = NULL;
  1298. ze_obj->filename_len = 0;
  1299. ze_obj->za = NULL;
  1300. RETURN_TRUE;
  1301. }
  1302. /* }}} */
  1303. /* {{{ proto string ZipArchive::getStatusString()
  1304. * Returns the status error message, system and/or zip messages */
  1305. static ZIPARCHIVE_METHOD(getStatusString)
  1306. {
  1307. struct zip *intern;
  1308. zval *this = getThis();
  1309. int zep, syp, len;
  1310. char error_string[128];
  1311. if (!this) {
  1312. RETURN_FALSE;
  1313. }
  1314. ZIP_FROM_OBJECT(intern, this);
  1315. zip_error_get(intern, &zep, &syp);
  1316. len = zip_error_to_str(error_string, 128, zep, syp);
  1317. RETVAL_STRINGL(error_string, len, 1);
  1318. }
  1319. /* }}} */
  1320. /* {{{ proto bool ZipArchive::createEmptyDir(string dirname)
  1321. Returns the index of the entry named filename in the archive */
  1322. static ZIPARCHIVE_METHOD(addEmptyDir)
  1323. {
  1324. struct zip *intern;
  1325. zval *this = getThis();
  1326. char *dirname;
  1327. int dirname_len;
  1328. int idx;
  1329. struct zip_stat sb;
  1330. char *s;
  1331. if (!this) {
  1332. RETURN_FALSE;
  1333. }
  1334. ZIP_FROM_OBJECT(intern, this);
  1335. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
  1336. &dirname, &dirname_len) == FAILURE) {
  1337. return;
  1338. }
  1339. if (dirname_len<1) {
  1340. RETURN_FALSE;
  1341. }
  1342. if (dirname[dirname_len-1] != '/') {
  1343. s=(char *)emalloc(dirname_len+2);
  1344. strcpy(s, dirname);
  1345. s[dirname_len] = '/';
  1346. s[dirname_len+1] = '\0';
  1347. } else {
  1348. s = dirname;
  1349. }
  1350. idx = zip_stat(intern, s, 0, &sb);
  1351. if (idx >= 0) {
  1352. RETVAL_FALSE;
  1353. } else {
  1354. if (zip_add_dir(intern, (const char *)s) == -1) {
  1355. RETVAL_FALSE;
  1356. }
  1357. RETVAL_TRUE;
  1358. }
  1359. if (s != dirname) {
  1360. efree(s);
  1361. }
  1362. }
  1363. /* }}} */
  1364. static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /* {{{ */
  1365. {
  1366. struct zip *intern;
  1367. zval *this = getThis();
  1368. char *pattern;
  1369. char *path = NULL;
  1370. char *remove_path = NULL;
  1371. char *add_path = NULL;
  1372. int pattern_len, add_path_len, remove_path_len, path_len = 0;
  1373. long remove_all_path = 0;
  1374. long flags = 0;
  1375. zval *options = NULL;
  1376. int found;
  1377. if (!this) {
  1378. RETURN_FALSE;
  1379. }
  1380. ZIP_FROM_OBJECT(intern, this);
  1381. /* 1 == glob, 2==pcre */
  1382. if (type == 1) {
  1383. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|la",
  1384. &pattern, &pattern_len, &flags, &options) == FAILURE) {
  1385. return;
  1386. }
  1387. } else {
  1388. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sa",
  1389. &pattern, &pattern_len, &path, &path_len, &options) == FAILURE) {
  1390. return;
  1391. }
  1392. }
  1393. if (pattern_len == 0) {
  1394. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Empty string as pattern");
  1395. RETURN_FALSE;
  1396. }
  1397. if (options && (php_zip_parse_options(options, &remove_all_path, &remove_path, &remove_path_len,
  1398. &add_path, &add_path_len TSRMLS_CC) < 0)) {
  1399. RETURN_FALSE;
  1400. }
  1401. if (remove_path && remove_path_len > 1 && (remove_path[strlen(remove_path) - 1] == '/' ||
  1402. remove_path[strlen(remove_path) - 1] == '\\')) {
  1403. remove_path[strlen(remove_path) - 1] = '\0';
  1404. }
  1405. if (type == 1) {
  1406. found = php_zip_glob(pattern, pattern_len, flags, return_value TSRMLS_CC);
  1407. } else {
  1408. found = php_zip_pcre(pattern, pattern_len, path, path_len, return_value TSRMLS_CC);
  1409. }
  1410. if (found > 0) {
  1411. int i;
  1412. zval **zval_file = NULL;
  1413. for (i = 0; i < found; i++) {
  1414. char *file, *file_stripped, *entry_name;
  1415. size_t entry_name_len, file_stripped_len;
  1416. char entry_name_buf[MAXPATHLEN];
  1417. char *basename = NULL;
  1418. if (zend_hash_index_find(Z_ARRVAL_P(return_value), i, (void **) &zval_file) == SUCCESS) {
  1419. file = Z_STRVAL_PP(zval_file);
  1420. if (remove_all_path) {
  1421. php_basename(Z_STRVAL_PP(zval_file), Z_STRLEN_PP(zval_file), NULL, 0,
  1422. &basename, (size_t *)&file_stripped_len TSRMLS_CC);
  1423. file_stripped = basename;
  1424. } else if (remove_path && strstr(Z_STRVAL_PP(zval_file), remove_path) != NULL) {
  1425. file_stripped = Z_STRVAL_PP(zval_file) + remove_path_len + 1;
  1426. file_stripped_len = Z_STRLEN_PP(zval_file) - remove_path_len - 1;
  1427. } else {
  1428. file_stripped = Z_STRVAL_PP(zval_file);
  1429. file_stripped_len = Z_STRLEN_PP(zval_file);
  1430. }
  1431. if (add_path) {
  1432. if ((add_path_len + file_stripped_len) > MAXPATHLEN) {
  1433. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Entry name too long (max: %d, %ld given)",
  1434. MAXPATHLEN - 1, (add_path_len + file_stripped_len));
  1435. zval_dtor(return_value);
  1436. RETURN_FALSE;
  1437. }
  1438. snprintf(entry_name_buf, MAXPATHLEN, "%s%s", add_path, file_stripped);
  1439. entry_name = entry_name_buf;
  1440. entry_name_len = strlen(entry_name);
  1441. } else {
  1442. entry_name = Z_STRVAL_PP(zval_file);
  1443. entry_name_len = Z_STRLEN_PP(zval_file);
  1444. }
  1445. if (basename) {
  1446. efree(basename);
  1447. basename = NULL;
  1448. }
  1449. if (php_zip_add_file(intern, Z_STRVAL_PP(zval_file), Z_STRLEN_PP(zval_file),
  1450. entry_name, entry_name_len, 0, 0 TSRMLS_CC) < 0) {
  1451. zval_dtor(return_value);
  1452. RETURN_FALSE;
  1453. }
  1454. }
  1455. }
  1456. }
  1457. }
  1458. /* }}} */
  1459. /* {{{ proto bool ZipArchive::addGlob(string pattern[,int flags [, array options]])
  1460. Add files matching the glob pattern. See php's glob for the pattern syntax. */
  1461. static ZIPARCHIVE_METHOD(addGlob)
  1462. {
  1463. php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  1464. }
  1465. /* }}} */
  1466. /* {{{ proto bool ZipArchive::addPattern(string pattern[, string path [, array options]])
  1467. Add files matching the pcre pattern. See php's pcre for the pattern syntax. */
  1468. static ZIPARCHIVE_METHOD(addPattern)
  1469. {
  1470. php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2);
  1471. }
  1472. /* }}} */
  1473. /* {{{ proto bool ZipArchive::addFile(string filepath[, string entryname[, int start [, int length]]])
  1474. Add a file in a Zip archive using its path and the name to use. */
  1475. static ZIPARCHIVE_METHOD(addFile)
  1476. {
  1477. struct zip *intern;
  1478. zval *this = getThis();
  1479. char *filename;
  1480. int filename_len;
  1481. char *entry_name = NULL;
  1482. int entry_name_len = 0;
  1483. long offset_start = 0, offset_len = 0;
  1484. if (!this) {
  1485. RETURN_FALSE;
  1486. }
  1487. ZIP_FROM_OBJECT(intern, this);
  1488. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sll",
  1489. &filename, &filename_len, &entry_name, &entry_name_len, &offset_start, &offset_len) == FAILURE) {
  1490. return;
  1491. }
  1492. if (filename_len == 0) {
  1493. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Empty string as filename");
  1494. RETURN_FALSE;
  1495. }
  1496. if (entry_name_len == 0) {
  1497. entry_name = filename;
  1498. entry_name_len = filename_len;
  1499. }
  1500. if (php_zip_add_file(intern, filename, filename_len,
  1501. entry_name, entry_name_len, 0, 0 TSRMLS_CC) < 0) {
  1502. RETURN_FALSE;
  1503. } else {
  1504. RETURN_TRUE;
  1505. }
  1506. }
  1507. /* }}} */
  1508. /* {{{ proto bool ZipArchive::addFromString(string name, string content)
  1509. Add a file using content and the entry name */
  1510. static ZIPARCHIVE_METHOD(addFromString)
  1511. {
  1512. struct zip *intern;
  1513. zval *this = getThis();
  1514. char *buffer, *name;
  1515. int buffer_len, name_len;
  1516. ze_zip_object *ze_obj;
  1517. struct zip_source *zs;
  1518. int pos = 0;
  1519. int cur_idx;
  1520. if (!this) {
  1521. RETURN_FALSE;
  1522. }
  1523. ZIP_FROM_OBJECT(intern, this);
  1524. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss",
  1525. &name, &name_len, &buffer, &buffer_len) == FAILURE) {
  1526. return;
  1527. }
  1528. ze_obj = (ze_zip_object*) zend_object_store_get_object(this TSRMLS_CC);
  1529. if (ze_obj->buffers_cnt) {
  1530. ze_obj->buffers = (char **)erealloc(ze_obj->buffers, sizeof(char *) * (ze_obj->buffers_cnt+1));
  1531. pos = ze_obj->buffers_cnt++;
  1532. } else {
  1533. ze_obj->buffers = (char **)emalloc(sizeof(char *));
  1534. ze_obj->buffers_cnt++;
  1535. pos = 0;
  1536. }
  1537. ze_obj->buffers[pos] = (char *)emalloc(buffer_len + 1);
  1538. memcpy(ze_obj->buffers[pos], buffer, buffer_len + 1);
  1539. zs = zip_source_buffer(intern, ze_obj->buffers[pos], buffer_len, 0);
  1540. if (zs == NULL) {
  1541. RETURN_FALSE;
  1542. }
  1543. cur_idx = zip_name_locate(intern, (const char *)name, 0);
  1544. /* TODO: fix _zip_replace */
  1545. if (cur_idx >= 0) {
  1546. if (zip_delete(intern, cur_idx) == -1) {
  1547. RETURN_FALSE;
  1548. }
  1549. }
  1550. if (zip_add(intern, name, zs) == -1) {
  1551. RETURN_FALSE;
  1552. } else {
  1553. RETURN_TRUE;
  1554. }
  1555. }
  1556. /* }}} */
  1557. /* {{{ proto array ZipArchive::statName(string filename[, int flags])
  1558. Returns the information about a the zip entry filename */
  1559. static ZIPARCHIVE_METHOD(statName)
  1560. {
  1561. struct zip *intern;
  1562. zval *this = getThis();
  1563. char *name;
  1564. int name_len;
  1565. long flags = 0;
  1566. struct zip_stat sb;
  1567. if (!this) {
  1568. RETURN_FALSE;
  1569. }
  1570. ZIP_FROM_OBJECT(intern, this);
  1571. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l",
  1572. &name, &name_len, &flags) == FAILURE) {
  1573. return;
  1574. }
  1575. PHP_ZIP_STAT_PATH(intern, name, name_len, flags, sb);
  1576. RETURN_SB(&sb);
  1577. }
  1578. /* }}} */
  1579. /* {{{ proto resource ZipArchive::statIndex(int index[, int flags])
  1580. Returns the zip entry informations using its index */
  1581. static ZIPARCHIVE_METHOD(statIndex)
  1582. {
  1583. struct zip *intern;
  1584. zval *this = getThis();
  1585. long index, flags = 0;
  1586. struct zip_stat sb;
  1587. if (!this) {
  1588. RETURN_FALSE;
  1589. }
  1590. ZIP_FROM_OBJECT(intern, this);
  1591. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l",
  1592. &index, &flags) == FAILURE) {
  1593. return;
  1594. }
  1595. if (zip_stat_index(intern, index, flags, &sb) != 0) {
  1596. RETURN_FALSE;
  1597. }
  1598. RETURN_SB(&sb);
  1599. }
  1600. /* }}} */
  1601. /* {{{ proto int ZipArchive::locateName(string filename[, int flags])
  1602. Returns the index of the entry named filename in the archive */
  1603. static ZIPARCHIVE_METHOD(locateName)
  1604. {
  1605. struct zip *intern;
  1606. zval *this = getThis();
  1607. char *name;
  1608. int name_len;
  1609. long flags = 0;
  1610. long idx = -1;
  1611. if (!this) {
  1612. RETURN_FALSE;
  1613. }
  1614. ZIP_FROM_OBJECT(intern, this);
  1615. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l",
  1616. &name, &name_len, &flags) == FAILURE) {
  1617. return;
  1618. }
  1619. if (name_len<1) {
  1620. RETURN_FALSE;
  1621. }
  1622. idx = (long)zip_name_locate(intern, (const char *)name, flags);
  1623. if (idx >= 0) {
  1624. RETURN_LONG(idx);
  1625. } else {
  1626. RETURN_FALSE;
  1627. }
  1628. }
  1629. /* }}} */
  1630. /* {{{ proto string ZipArchive::getNameIndex(int index [, int flags])
  1631. Returns the name of the file at position index */
  1632. static ZIPARCHIVE_METHOD(getNameIndex)
  1633. {
  1634. struct zip *intern;
  1635. zval *this = getThis();
  1636. const char *name;
  1637. long flags = 0, index = 0;
  1638. if (!this) {
  1639. RETURN_FALSE;
  1640. }
  1641. ZIP_FROM_OBJECT(intern, this);
  1642. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l",
  1643. &index, &flags) == FAILURE) {
  1644. return;
  1645. }
  1646. name = zip_get_name(intern, (int) index, flags);
  1647. if (name) {
  1648. RETVAL_STRING((char *)name, 1);
  1649. } else {
  1650. RETURN_FALSE;
  1651. }
  1652. }
  1653. /* }}} */
  1654. /* {{{ proto bool ZipArchive::setArchiveComment(string comment)
  1655. Set or remove (NULL/'') the comment of the archive */
  1656. static ZIPARCHIVE_METHOD(setArchiveComment)
  1657. {
  1658. struct zip *intern;
  1659. zval *this = getThis();
  1660. int comment_len;
  1661. char * comment;
  1662. if (!this) {
  1663. RETURN_FALSE;
  1664. }
  1665. ZIP_FROM_OBJECT(intern, this);
  1666. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &comment, &comment_len) == FAILURE) {
  1667. return;
  1668. }
  1669. if (zip_set_archive_comment(intern, (const char *)comment, (int)comment_len)) {
  1670. RETURN_FALSE;
  1671. } else {
  1672. RETURN_TRUE;
  1673. }
  1674. }
  1675. /* }}} */
  1676. /* {{{ proto string ZipArchive::getArchiveComment([int flags])
  1677. Returns the comment of an entry using its index */
  1678. static ZIPARCHIVE_METHOD(getArchiveComment)
  1679. {
  1680. struct zip *intern;
  1681. zval *this = getThis();
  1682. long flags = 0;
  1683. const char * comment;
  1684. int comment_len = 0;
  1685. if (!this) {
  1686. RETURN_FALSE;
  1687. }
  1688. ZIP_FROM_OBJECT(intern, this);
  1689. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flags) == FAILURE) {
  1690. return;
  1691. }
  1692. comment = zip_get_archive_comment(intern, &comment_len, (int)flags);
  1693. if(comment==NULL) {
  1694. RETURN_FALSE;
  1695. }
  1696. RETURN_STRINGL((char *)comment, (long)comment_len, 1);
  1697. }
  1698. /* }}} */
  1699. /* {{{ proto bool ZipArchive::setCommentName(string name, string comment)
  1700. Set or remove (NULL/'') the comment of an entry using its Name */
  1701. static ZIPARCHIVE_METHOD(setCommentName)
  1702. {
  1703. struct zip *intern;
  1704. zval *this = getThis();
  1705. int comment_len, name_len;
  1706. char * comment, *name;
  1707. int idx;
  1708. if (!this) {
  1709. RETURN_FALSE;
  1710. }
  1711. ZIP_FROM_OBJECT(intern, this);
  1712. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss",
  1713. &name, &name_len, &comment, &comment_len) == FAILURE) {
  1714. return;
  1715. }
  1716. if (name_len < 1) {
  1717. php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Empty string as entry name");
  1718. }
  1719. idx = zip_name_locate(intern, name, 0);
  1720. if (idx < 0) {
  1721. RETURN_FALSE;
  1722. }
  1723. PHP_ZIP_SET_FILE_COMMENT(intern, idx, comment, comment_len);
  1724. }
  1725. /* }}} */
  1726. /* {{{ proto bool ZipArchive::setCommentIndex(int index, string comment)
  1727. Set or remove (NU…

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