PageRenderTime 87ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/phar/phar_object.c

http://github.com/infusion/PHP
C | 5438 lines | 4157 code | 809 blank | 472 comment | 1052 complexity | f071aaab42bfcb662350e9d7403a9637 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. /*
  2. +----------------------------------------------------------------------+
  3. | phar php single-file executable PHP extension |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2005-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. | Authors: Gregory Beaver <cellog@php.net> |
  16. | Marcus Boerger <helly@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id: phar_object.c 309222 2011-03-14 14:12:42Z felipe $ */
  20. #include "phar_internal.h"
  21. #include "func_interceptors.h"
  22. static zend_class_entry *phar_ce_archive;
  23. static zend_class_entry *phar_ce_data;
  24. static zend_class_entry *phar_ce_PharException;
  25. #if HAVE_SPL
  26. static zend_class_entry *phar_ce_entry;
  27. #endif
  28. #if PHP_MAJOR_VERSION > 5 || ((PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION >= 3))
  29. # define PHAR_ARG_INFO
  30. #else
  31. # define PHAR_ARG_INFO static
  32. #endif
  33. static int phar_file_type(HashTable *mimes, char *file, char **mime_type TSRMLS_DC) /* {{{ */
  34. {
  35. char *ext;
  36. phar_mime_type *mime;
  37. ext = strrchr(file, '.');
  38. if (!ext) {
  39. *mime_type = "text/plain";
  40. /* no file extension = assume text/plain */
  41. return PHAR_MIME_OTHER;
  42. }
  43. ++ext;
  44. if (SUCCESS != zend_hash_find(mimes, ext, strlen(ext), (void **) &mime)) {
  45. *mime_type = "application/octet-stream";
  46. return PHAR_MIME_OTHER;
  47. }
  48. *mime_type = mime->mime;
  49. return mime->type;
  50. }
  51. /* }}} */
  52. static void phar_mung_server_vars(char *fname, char *entry, int entry_len, char *basename, int request_uri_len TSRMLS_DC) /* {{{ */
  53. {
  54. #if PHP_MAJOR_VERSION >= 6
  55. int is_unicode = 0;
  56. #endif
  57. HashTable *_SERVER;
  58. zval **stuff;
  59. char *path_info;
  60. int basename_len = strlen(basename);
  61. int code;
  62. zval *temp;
  63. /* "tweak" $_SERVER variables requested in earlier call to Phar::mungServer() */
  64. if (!PG(http_globals)[TRACK_VARS_SERVER]) {
  65. return;
  66. }
  67. _SERVER = Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]);
  68. /* PATH_INFO and PATH_TRANSLATED should always be munged */
  69. #if PHP_MAJOR_VERSION >= 6
  70. if (phar_find_key(_SERVER, "PATH_INFO", sizeof("PATH_INFO"), (void **) &stuff TSRMLS_CC)) {
  71. if (Z_TYPE_PP(stuff) == IS_UNICODE) {
  72. is_unicode = 1;
  73. zval_unicode_to_string(*stuff TSRMLS_CC);
  74. } else {
  75. is_unicode = 0;
  76. }
  77. #else
  78. if (SUCCESS == zend_hash_find(_SERVER, "PATH_INFO", sizeof("PATH_INFO"), (void **) &stuff)) {
  79. #endif
  80. path_info = Z_STRVAL_PP(stuff);
  81. code = Z_STRLEN_PP(stuff);
  82. if (Z_STRLEN_PP(stuff) > entry_len && !memcmp(Z_STRVAL_PP(stuff), entry, entry_len)) {
  83. ZVAL_STRINGL(*stuff, Z_STRVAL_PP(stuff) + entry_len, request_uri_len, 1);
  84. MAKE_STD_ZVAL(temp);
  85. ZVAL_STRINGL(temp, path_info, code, 0);
  86. #if PHP_MAJOR_VERSION >= 6
  87. if (is_unicode) {
  88. zval_string_to_unicode(*stuff TSRMLS_CC);
  89. }
  90. #endif
  91. zend_hash_update(_SERVER, "PHAR_PATH_INFO", sizeof("PHAR_PATH_INFO"), &temp, sizeof(zval **), NULL);
  92. }
  93. }
  94. #if PHP_MAJOR_VERSION >= 6
  95. if (phar_find_key(_SERVER, "PATH_TRANSLATED", sizeof("PATH_TRANSLATED"), (void **) &stuff TSRMLS_CC)) {
  96. if (Z_TYPE_PP(stuff) == IS_UNICODE) {
  97. is_unicode = 1;
  98. zval_unicode_to_string(*stuff TSRMLS_CC);
  99. } else {
  100. is_unicode = 0;
  101. }
  102. #else
  103. if (SUCCESS == zend_hash_find(_SERVER, "PATH_TRANSLATED", sizeof("PATH_TRANSLATED"), (void **) &stuff)) {
  104. #endif
  105. path_info = Z_STRVAL_PP(stuff);
  106. code = Z_STRLEN_PP(stuff);
  107. Z_STRLEN_PP(stuff) = spprintf(&(Z_STRVAL_PP(stuff)), 4096, "phar://%s%s", fname, entry);
  108. MAKE_STD_ZVAL(temp);
  109. ZVAL_STRINGL(temp, path_info, code, 0);
  110. #if PHP_MAJOR_VERSION >= 6
  111. if (is_unicode) {
  112. zval_string_to_unicode(*stuff TSRMLS_CC);
  113. }
  114. #endif
  115. zend_hash_update(_SERVER, "PHAR_PATH_TRANSLATED", sizeof("PHAR_PATH_TRANSLATED"), (void *) &temp, sizeof(zval **), NULL);
  116. }
  117. if (!PHAR_GLOBALS->phar_SERVER_mung_list) {
  118. return;
  119. }
  120. if (PHAR_GLOBALS->phar_SERVER_mung_list & PHAR_MUNG_REQUEST_URI) {
  121. #if PHP_MAJOR_VERSION >= 6
  122. if (phar_find_key(_SERVER, "REQUEST_URI", sizeof("REQUEST_URI"), (void **) &stuff TSRMLS_CC)) {
  123. if (Z_TYPE_PP(stuff) == IS_UNICODE) {
  124. is_unicode = 1;
  125. zval_unicode_to_string(*stuff TSRMLS_CC);
  126. } else {
  127. is_unicode = 0;
  128. }
  129. #else
  130. if (SUCCESS == zend_hash_find(_SERVER, "REQUEST_URI", sizeof("REQUEST_URI"), (void **) &stuff)) {
  131. #endif
  132. path_info = Z_STRVAL_PP(stuff);
  133. code = Z_STRLEN_PP(stuff);
  134. if (Z_STRLEN_PP(stuff) > basename_len && !memcmp(Z_STRVAL_PP(stuff), basename, basename_len)) {
  135. ZVAL_STRINGL(*stuff, Z_STRVAL_PP(stuff) + basename_len, Z_STRLEN_PP(stuff) - basename_len, 1);
  136. MAKE_STD_ZVAL(temp);
  137. ZVAL_STRINGL(temp, path_info, code, 0);
  138. #if PHP_MAJOR_VERSION >= 6
  139. if (is_unicode) {
  140. zval_string_to_unicode(*stuff TSRMLS_CC);
  141. }
  142. #endif
  143. zend_hash_update(_SERVER, "PHAR_REQUEST_URI", sizeof("PHAR_REQUEST_URI"), (void *) &temp, sizeof(zval **), NULL);
  144. }
  145. }
  146. }
  147. if (PHAR_GLOBALS->phar_SERVER_mung_list & PHAR_MUNG_PHP_SELF) {
  148. #if PHP_MAJOR_VERSION >= 6
  149. if (phar_find_key(_SERVER, "PHP_SELF", sizeof("PHP_SELF"), (void **) &stuff TSRMLS_CC)) {
  150. if (Z_TYPE_PP(stuff) == IS_UNICODE) {
  151. is_unicode = 1;
  152. zval_unicode_to_string(*stuff TSRMLS_CC);
  153. } else {
  154. is_unicode = 0;
  155. }
  156. #else
  157. if (SUCCESS == zend_hash_find(_SERVER, "PHP_SELF", sizeof("PHP_SELF"), (void **) &stuff)) {
  158. #endif
  159. path_info = Z_STRVAL_PP(stuff);
  160. code = Z_STRLEN_PP(stuff);
  161. if (Z_STRLEN_PP(stuff) > basename_len && !memcmp(Z_STRVAL_PP(stuff), basename, basename_len)) {
  162. ZVAL_STRINGL(*stuff, Z_STRVAL_PP(stuff) + basename_len, Z_STRLEN_PP(stuff) - basename_len, 1);
  163. MAKE_STD_ZVAL(temp);
  164. ZVAL_STRINGL(temp, path_info, code, 0);
  165. #if PHP_MAJOR_VERSION >= 6
  166. if (is_unicode) {
  167. zval_string_to_unicode(*stuff TSRMLS_CC);
  168. }
  169. #endif
  170. zend_hash_update(_SERVER, "PHAR_PHP_SELF", sizeof("PHAR_PHP_SELF"), (void *) &temp, sizeof(zval **), NULL);
  171. }
  172. }
  173. }
  174. if (PHAR_GLOBALS->phar_SERVER_mung_list & PHAR_MUNG_SCRIPT_NAME) {
  175. #if PHP_MAJOR_VERSION >= 6
  176. if (phar_find_key(_SERVER, "SCRIPT_NAME", sizeof("SCRIPT_NAME"), (void **) &stuff TSRMLS_CC)) {
  177. if (Z_TYPE_PP(stuff) == IS_UNICODE) {
  178. is_unicode = 1;
  179. zval_unicode_to_string(*stuff TSRMLS_CC);
  180. } else {
  181. is_unicode = 0;
  182. }
  183. #else
  184. if (SUCCESS == zend_hash_find(_SERVER, "SCRIPT_NAME", sizeof("SCRIPT_NAME"), (void **) &stuff)) {
  185. #endif
  186. path_info = Z_STRVAL_PP(stuff);
  187. code = Z_STRLEN_PP(stuff);
  188. ZVAL_STRINGL(*stuff, entry, entry_len, 1);
  189. MAKE_STD_ZVAL(temp);
  190. ZVAL_STRINGL(temp, path_info, code, 0);
  191. #if PHP_MAJOR_VERSION >= 6
  192. if (is_unicode) {
  193. zval_string_to_unicode(*stuff TSRMLS_CC);
  194. }
  195. #endif
  196. zend_hash_update(_SERVER, "PHAR_SCRIPT_NAME", sizeof("PHAR_SCRIPT_NAME"), (void *) &temp, sizeof(zval **), NULL);
  197. }
  198. }
  199. if (PHAR_GLOBALS->phar_SERVER_mung_list & PHAR_MUNG_SCRIPT_FILENAME) {
  200. #if PHP_MAJOR_VERSION >= 6
  201. if (phar_find_key(_SERVER, "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME"), (void **) &stuff TSRMLS_CC)) {
  202. if (Z_TYPE_PP(stuff) == IS_UNICODE) {
  203. is_unicode = 1;
  204. zval_unicode_to_string(*stuff TSRMLS_CC);
  205. } else {
  206. is_unicode = 0;
  207. }
  208. #else
  209. if (SUCCESS == zend_hash_find(_SERVER, "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME"), (void **) &stuff)) {
  210. #endif
  211. path_info = Z_STRVAL_PP(stuff);
  212. code = Z_STRLEN_PP(stuff);
  213. Z_STRLEN_PP(stuff) = spprintf(&(Z_STRVAL_PP(stuff)), 4096, "phar://%s%s", fname, entry);
  214. MAKE_STD_ZVAL(temp);
  215. ZVAL_STRINGL(temp, path_info, code, 0);
  216. #if PHP_MAJOR_VERSION >= 6
  217. if (is_unicode) {
  218. zval_string_to_unicode(*stuff TSRMLS_CC);
  219. }
  220. #endif
  221. zend_hash_update(_SERVER, "PHAR_SCRIPT_FILENAME", sizeof("PHAR_SCRIPT_FILENAME"), (void *) &temp, sizeof(zval **), NULL);
  222. }
  223. }
  224. }
  225. /* }}} */
  226. static int phar_file_action(phar_archive_data *phar, phar_entry_info *info, char *mime_type, int code, char *entry, int entry_len, char *arch, char *basename, char *ru, int ru_len TSRMLS_DC) /* {{{ */
  227. {
  228. char *name = NULL, buf[8192], *cwd;
  229. zend_syntax_highlighter_ini syntax_highlighter_ini;
  230. sapi_header_line ctr = {0};
  231. size_t got;
  232. int dummy = 1, name_len;
  233. zend_file_handle file_handle;
  234. zend_op_array *new_op_array;
  235. zval *result = NULL;
  236. php_stream *fp;
  237. off_t position;
  238. switch (code) {
  239. case PHAR_MIME_PHPS:
  240. efree(basename);
  241. /* highlight source */
  242. if (entry[0] == '/') {
  243. name_len = spprintf(&name, 4096, "phar://%s%s", arch, entry);
  244. } else {
  245. name_len = spprintf(&name, 4096, "phar://%s/%s", arch, entry);
  246. }
  247. php_get_highlight_struct(&syntax_highlighter_ini);
  248. highlight_file(name, &syntax_highlighter_ini TSRMLS_CC);
  249. efree(name);
  250. #ifdef PHP_WIN32
  251. efree(arch);
  252. #endif
  253. zend_bailout();
  254. case PHAR_MIME_OTHER:
  255. /* send headers, output file contents */
  256. efree(basename);
  257. ctr.line_len = spprintf(&(ctr.line), 0, "Content-type: %s", mime_type);
  258. sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
  259. efree(ctr.line);
  260. ctr.line_len = spprintf(&(ctr.line), 0, "Content-length: %u", info->uncompressed_filesize);
  261. sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
  262. efree(ctr.line);
  263. if (FAILURE == sapi_send_headers(TSRMLS_C)) {
  264. zend_bailout();
  265. }
  266. /* prepare to output */
  267. fp = phar_get_efp(info, 1 TSRMLS_CC);
  268. if (!fp) {
  269. char *error;
  270. if (!phar_open_jit(phar, info, &error TSRMLS_CC)) {
  271. if (error) {
  272. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  273. efree(error);
  274. }
  275. return -1;
  276. }
  277. fp = phar_get_efp(info, 1 TSRMLS_CC);
  278. }
  279. position = 0;
  280. phar_seek_efp(info, 0, SEEK_SET, 0, 1 TSRMLS_CC);
  281. do {
  282. got = php_stream_read(fp, buf, MIN(8192, info->uncompressed_filesize - position));
  283. if (got > 0) {
  284. PHPWRITE(buf, got);
  285. position += got;
  286. if (position == (off_t) info->uncompressed_filesize) {
  287. break;
  288. }
  289. }
  290. } while (1);
  291. zend_bailout();
  292. case PHAR_MIME_PHP:
  293. if (basename) {
  294. phar_mung_server_vars(arch, entry, entry_len, basename, ru_len TSRMLS_CC);
  295. efree(basename);
  296. }
  297. if (entry[0] == '/') {
  298. name_len = spprintf(&name, 4096, "phar://%s%s", arch, entry);
  299. } else {
  300. name_len = spprintf(&name, 4096, "phar://%s/%s", arch, entry);
  301. }
  302. file_handle.type = ZEND_HANDLE_FILENAME;
  303. file_handle.handle.fd = 0;
  304. file_handle.filename = name;
  305. file_handle.opened_path = NULL;
  306. file_handle.free_filename = 0;
  307. PHAR_G(cwd) = NULL;
  308. PHAR_G(cwd_len) = 0;
  309. if (zend_hash_add(&EG(included_files), name, name_len+1, (void *)&dummy, sizeof(int), NULL) == SUCCESS) {
  310. if ((cwd = zend_memrchr(entry, '/', entry_len))) {
  311. PHAR_G(cwd_init) = 1;
  312. if (entry == cwd) {
  313. /* root directory */
  314. PHAR_G(cwd_len) = 0;
  315. PHAR_G(cwd) = NULL;
  316. } else if (entry[0] == '/') {
  317. PHAR_G(cwd_len) = cwd - (entry + 1);
  318. PHAR_G(cwd) = estrndup(entry + 1, PHAR_G(cwd_len));
  319. } else {
  320. PHAR_G(cwd_len) = cwd - entry;
  321. PHAR_G(cwd) = estrndup(entry, PHAR_G(cwd_len));
  322. }
  323. }
  324. new_op_array = zend_compile_file(&file_handle, ZEND_REQUIRE TSRMLS_CC);
  325. if (!new_op_array) {
  326. zend_hash_del(&EG(included_files), name, name_len+1);
  327. }
  328. zend_destroy_file_handle(&file_handle TSRMLS_CC);
  329. } else {
  330. efree(name);
  331. new_op_array = NULL;
  332. }
  333. #ifdef PHP_WIN32
  334. efree(arch);
  335. #endif
  336. if (new_op_array) {
  337. EG(return_value_ptr_ptr) = &result;
  338. EG(active_op_array) = new_op_array;
  339. zend_try {
  340. zend_execute(new_op_array TSRMLS_CC);
  341. if (PHAR_G(cwd)) {
  342. efree(PHAR_G(cwd));
  343. PHAR_G(cwd) = NULL;
  344. PHAR_G(cwd_len) = 0;
  345. }
  346. PHAR_G(cwd_init) = 0;
  347. efree(name);
  348. destroy_op_array(new_op_array TSRMLS_CC);
  349. efree(new_op_array);
  350. if (EG(return_value_ptr_ptr) && *EG(return_value_ptr_ptr)) {
  351. zval_ptr_dtor(EG(return_value_ptr_ptr));
  352. }
  353. } zend_catch {
  354. if (PHAR_G(cwd)) {
  355. efree(PHAR_G(cwd));
  356. PHAR_G(cwd) = NULL;
  357. PHAR_G(cwd_len) = 0;
  358. }
  359. PHAR_G(cwd_init) = 0;
  360. efree(name);
  361. } zend_end_try();
  362. zend_bailout();
  363. }
  364. return PHAR_MIME_PHP;
  365. }
  366. return -1;
  367. }
  368. /* }}} */
  369. static void phar_do_403(char *entry, int entry_len TSRMLS_DC) /* {{{ */
  370. {
  371. sapi_header_line ctr = {0};
  372. ctr.response_code = 403;
  373. ctr.line_len = sizeof("HTTP/1.0 403 Access Denied");
  374. ctr.line = "HTTP/1.0 403 Access Denied";
  375. sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
  376. sapi_send_headers(TSRMLS_C);
  377. PHPWRITE("<html>\n <head>\n <title>Access Denied</title>\n </head>\n <body>\n <h1>403 - File ", sizeof("<html>\n <head>\n <title>Access Denied</title>\n </head>\n <body>\n <h1>403 - File ") - 1);
  378. PHPWRITE(entry, entry_len);
  379. PHPWRITE(" Access Denied</h1>\n </body>\n</html>", sizeof(" Access Denied</h1>\n </body>\n</html>") - 1);
  380. }
  381. /* }}} */
  382. static void phar_do_404(phar_archive_data *phar, char *fname, int fname_len, char *f404, int f404_len, char *entry, int entry_len TSRMLS_DC) /* {{{ */
  383. {
  384. sapi_header_line ctr = {0};
  385. phar_entry_info *info;
  386. if (phar && f404_len) {
  387. info = phar_get_entry_info(phar, f404, f404_len, NULL, 1 TSRMLS_CC);
  388. if (info) {
  389. phar_file_action(phar, info, "text/html", PHAR_MIME_PHP, f404, f404_len, fname, NULL, NULL, 0 TSRMLS_CC);
  390. return;
  391. }
  392. }
  393. ctr.response_code = 404;
  394. ctr.line_len = sizeof("HTTP/1.0 404 Not Found")+1;
  395. ctr.line = "HTTP/1.0 404 Not Found";
  396. sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
  397. sapi_send_headers(TSRMLS_C);
  398. PHPWRITE("<html>\n <head>\n <title>File Not Found</title>\n </head>\n <body>\n <h1>404 - File ", sizeof("<html>\n <head>\n <title>File Not Found</title>\n </head>\n <body>\n <h1>404 - File ") - 1);
  399. PHPWRITE(entry, entry_len);
  400. PHPWRITE(" Not Found</h1>\n </body>\n</html>", sizeof(" Not Found</h1>\n </body>\n</html>") - 1);
  401. }
  402. /* }}} */
  403. /* post-process REQUEST_URI and retrieve the actual request URI. This is for
  404. cases like http://localhost/blah.phar/path/to/file.php/extra/stuff
  405. which calls "blah.phar" file "path/to/file.php" with PATH_INFO "/extra/stuff" */
  406. static void phar_postprocess_ru_web(char *fname, int fname_len, char **entry, int *entry_len, char **ru, int *ru_len TSRMLS_DC) /* {{{ */
  407. {
  408. char *e = *entry + 1, *u = NULL, *u1 = NULL, *saveu = NULL;
  409. int e_len = *entry_len - 1, u_len = 0;
  410. phar_archive_data **pphar = NULL;
  411. /* we already know we can retrieve the phar if we reach here */
  412. zend_hash_find(&(PHAR_GLOBALS->phar_fname_map), fname, fname_len, (void **) &pphar);
  413. if (!pphar && PHAR_G(manifest_cached)) {
  414. zend_hash_find(&cached_phars, fname, fname_len, (void **) &pphar);
  415. }
  416. do {
  417. if (zend_hash_exists(&((*pphar)->manifest), e, e_len)) {
  418. if (u) {
  419. u[0] = '/';
  420. *ru = estrndup(u, u_len+1);
  421. ++u_len;
  422. u[0] = '\0';
  423. } else {
  424. *ru = NULL;
  425. }
  426. *ru_len = u_len;
  427. *entry_len = e_len + 1;
  428. return;
  429. }
  430. if (u) {
  431. u1 = strrchr(e, '/');
  432. u[0] = '/';
  433. saveu = u;
  434. e_len += u_len + 1;
  435. u = u1;
  436. if (!u) {
  437. return;
  438. }
  439. } else {
  440. u = strrchr(e, '/');
  441. if (!u) {
  442. if (saveu) {
  443. saveu[0] = '/';
  444. }
  445. return;
  446. }
  447. }
  448. u[0] = '\0';
  449. u_len = strlen(u + 1);
  450. e_len -= u_len + 1;
  451. if (e_len < 0) {
  452. if (saveu) {
  453. saveu[0] = '/';
  454. }
  455. return;
  456. }
  457. } while (1);
  458. }
  459. /* }}} */
  460. /* {{{ proto void Phar::running([bool retphar = true])
  461. * return the name of the currently running phar archive. If the optional parameter
  462. * is set to true, return the phar:// URL to the currently running phar
  463. */
  464. PHP_METHOD(Phar, running)
  465. {
  466. char *fname, *arch, *entry;
  467. int fname_len, arch_len, entry_len;
  468. zend_bool retphar = 1;
  469. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &retphar) == FAILURE) {
  470. return;
  471. }
  472. fname = zend_get_executed_filename(TSRMLS_C);
  473. fname_len = strlen(fname);
  474. if (fname_len > 7 && !memcmp(fname, "phar://", 7) && SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0 TSRMLS_CC)) {
  475. efree(entry);
  476. if (retphar) {
  477. RETVAL_STRINGL(fname, arch_len + 7, 1);
  478. efree(arch);
  479. return;
  480. } else {
  481. RETURN_STRINGL(arch, arch_len, 0);
  482. }
  483. }
  484. RETURN_STRINGL("", 0, 1);
  485. }
  486. /* }}} */
  487. /* {{{ proto void Phar::mount(string pharpath, string externalfile)
  488. * mount an external file or path to a location within the phar. This maps
  489. * an external file or directory to a location within the phar archive, allowing
  490. * reference to an external location as if it were within the phar archive. This
  491. * is useful for writable temp files like databases
  492. */
  493. PHP_METHOD(Phar, mount)
  494. {
  495. char *fname, *arch = NULL, *entry = NULL, *path, *actual;
  496. int fname_len, arch_len, entry_len, path_len, actual_len;
  497. phar_archive_data **pphar;
  498. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &path, &path_len, &actual, &actual_len) == FAILURE) {
  499. return;
  500. }
  501. fname = zend_get_executed_filename(TSRMLS_C);
  502. fname_len = strlen(fname);
  503. #ifdef PHP_WIN32
  504. phar_unixify_path_separators(fname, fname_len);
  505. #endif
  506. if (fname_len > 7 && !memcmp(fname, "phar://", 7) && SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0 TSRMLS_CC)) {
  507. efree(entry);
  508. entry = NULL;
  509. if (path_len > 7 && !memcmp(path, "phar://", 7)) {
  510. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Can only mount internal paths within a phar archive, use a relative path instead of \"%s\"", path);
  511. efree(arch);
  512. return;
  513. }
  514. carry_on2:
  515. if (SUCCESS != zend_hash_find(&(PHAR_GLOBALS->phar_fname_map), arch, arch_len, (void **)&pphar)) {
  516. if (PHAR_G(manifest_cached) && SUCCESS == zend_hash_find(&cached_phars, arch, arch_len, (void **)&pphar)) {
  517. if (SUCCESS == phar_copy_on_write(pphar TSRMLS_CC)) {
  518. goto carry_on;
  519. }
  520. }
  521. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s is not a phar archive, cannot mount", arch);
  522. if (arch) {
  523. efree(arch);
  524. }
  525. return;
  526. }
  527. carry_on:
  528. if (SUCCESS != phar_mount_entry(*pphar, actual, actual_len, path, path_len TSRMLS_CC)) {
  529. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Mounting of %s to %s within phar %s failed", path, actual, arch);
  530. if (path && path == entry) {
  531. efree(entry);
  532. }
  533. if (arch) {
  534. efree(arch);
  535. }
  536. return;
  537. }
  538. if (entry && path && path == entry) {
  539. efree(entry);
  540. }
  541. if (arch) {
  542. efree(arch);
  543. }
  544. return;
  545. } else if (SUCCESS == zend_hash_find(&(PHAR_GLOBALS->phar_fname_map), fname, fname_len, (void **)&pphar)) {
  546. goto carry_on;
  547. } else if (PHAR_G(manifest_cached) && SUCCESS == zend_hash_find(&cached_phars, fname, fname_len, (void **)&pphar)) {
  548. if (SUCCESS == phar_copy_on_write(pphar TSRMLS_CC)) {
  549. goto carry_on;
  550. }
  551. goto carry_on;
  552. } else if (SUCCESS == phar_split_fname(path, path_len, &arch, &arch_len, &entry, &entry_len, 2, 0 TSRMLS_CC)) {
  553. path = entry;
  554. path_len = entry_len;
  555. goto carry_on2;
  556. }
  557. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Mounting of %s to %s failed", path, actual);
  558. }
  559. /* }}} */
  560. /* {{{ proto void Phar::webPhar([string alias, [string index, [string f404, [array mimetypes, [callback rewrites]]]]])
  561. * mapPhar for web-based phars. Reads the currently executed file (a phar)
  562. * and registers its manifest. When executed in the CLI or CGI command-line sapi,
  563. * this works exactly like mapPhar(). When executed by a web-based sapi, this
  564. * reads $_SERVER['REQUEST_URI'] (the actual original value) and parses out the
  565. * intended internal file.
  566. */
  567. PHP_METHOD(Phar, webPhar)
  568. {
  569. zval *mimeoverride = NULL, *rewrite = NULL;
  570. char *alias = NULL, *error, *index_php = NULL, *f404 = NULL, *ru = NULL;
  571. int alias_len = 0, ret, f404_len = 0, free_pathinfo = 0, ru_len = 0;
  572. char *fname, *basename, *path_info, *mime_type = NULL, *entry, *pt;
  573. int fname_len, entry_len, code, index_php_len = 0, not_cgi;
  574. phar_archive_data *phar = NULL;
  575. phar_entry_info *info;
  576. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!saz", &alias, &alias_len, &index_php, &index_php_len, &f404, &f404_len, &mimeoverride, &rewrite) == FAILURE) {
  577. return;
  578. }
  579. phar_request_initialize(TSRMLS_C);
  580. fname = zend_get_executed_filename(TSRMLS_C);
  581. fname_len = strlen(fname);
  582. if (phar_open_executed_filename(alias, alias_len, &error TSRMLS_CC) != SUCCESS) {
  583. if (error) {
  584. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  585. efree(error);
  586. }
  587. return;
  588. }
  589. /* retrieve requested file within phar */
  590. if (!(SG(request_info).request_method && SG(request_info).request_uri && (!strcmp(SG(request_info).request_method, "GET") || !strcmp(SG(request_info).request_method, "POST")))) {
  591. return;
  592. }
  593. #ifdef PHP_WIN32
  594. fname = estrndup(fname, fname_len);
  595. phar_unixify_path_separators(fname, fname_len);
  596. #endif
  597. basename = zend_memrchr(fname, '/', fname_len);
  598. if (!basename) {
  599. basename = fname;
  600. } else {
  601. ++basename;
  602. }
  603. if ((strlen(sapi_module.name) == sizeof("cgi-fcgi")-1 && !strncmp(sapi_module.name, "cgi-fcgi", sizeof("cgi-fcgi")-1))
  604. || (strlen(sapi_module.name) == sizeof("cgi")-1 && !strncmp(sapi_module.name, "cgi", sizeof("cgi")-1))) {
  605. if (PG(http_globals)[TRACK_VARS_SERVER]) {
  606. HashTable *_server = Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]);
  607. zval **z_script_name, **z_path_info;
  608. if (SUCCESS != zend_hash_find(_server, "SCRIPT_NAME", sizeof("SCRIPT_NAME"), (void**)&z_script_name) ||
  609. IS_STRING != Z_TYPE_PP(z_script_name) ||
  610. !strstr(Z_STRVAL_PP(z_script_name), basename)) {
  611. return;
  612. }
  613. if (SUCCESS == zend_hash_find(_server, "PATH_INFO", sizeof("PATH_INFO"), (void**)&z_path_info) &&
  614. IS_STRING == Z_TYPE_PP(z_path_info)) {
  615. entry_len = Z_STRLEN_PP(z_path_info);
  616. entry = estrndup(Z_STRVAL_PP(z_path_info), entry_len);
  617. path_info = emalloc(Z_STRLEN_PP(z_script_name) + entry_len + 1);
  618. memcpy(path_info, Z_STRVAL_PP(z_script_name), Z_STRLEN_PP(z_script_name));
  619. memcpy(path_info + Z_STRLEN_PP(z_script_name), entry, entry_len + 1);
  620. free_pathinfo = 1;
  621. } else {
  622. entry_len = 0;
  623. entry = estrndup("", 0);
  624. path_info = Z_STRVAL_PP(z_script_name);
  625. }
  626. pt = estrndup(Z_STRVAL_PP(z_script_name), Z_STRLEN_PP(z_script_name));
  627. } else {
  628. char *testit;
  629. testit = sapi_getenv("SCRIPT_NAME", sizeof("SCRIPT_NAME")-1 TSRMLS_CC);
  630. if (!(pt = strstr(testit, basename))) {
  631. efree(testit);
  632. return;
  633. }
  634. path_info = sapi_getenv("PATH_INFO", sizeof("PATH_INFO")-1 TSRMLS_CC);
  635. if (path_info) {
  636. entry = path_info;
  637. entry_len = strlen(entry);
  638. spprintf(&path_info, 0, "%s%s", testit, path_info);
  639. free_pathinfo = 1;
  640. } else {
  641. path_info = testit;
  642. free_pathinfo = 1;
  643. entry = estrndup("", 0);
  644. entry_len = 0;
  645. }
  646. pt = estrndup(testit, (pt - testit) + (fname_len - (basename - fname)));
  647. }
  648. not_cgi = 0;
  649. } else {
  650. path_info = SG(request_info).request_uri;
  651. if (!(pt = strstr(path_info, basename))) {
  652. /* this can happen with rewrite rules - and we have no idea what to do then, so return */
  653. return;
  654. }
  655. entry_len = strlen(path_info);
  656. entry_len -= (pt - path_info) + (fname_len - (basename - fname));
  657. entry = estrndup(pt + (fname_len - (basename - fname)), entry_len);
  658. pt = estrndup(path_info, (pt - path_info) + (fname_len - (basename - fname)));
  659. not_cgi = 1;
  660. }
  661. if (rewrite) {
  662. zend_fcall_info fci;
  663. zend_fcall_info_cache fcc;
  664. zval *params, *retval_ptr, **zp[1];
  665. MAKE_STD_ZVAL(params);
  666. ZVAL_STRINGL(params, entry, entry_len, 1);
  667. zp[0] = &params;
  668. #if PHP_VERSION_ID < 50300
  669. if (FAILURE == zend_fcall_info_init(rewrite, &fci, &fcc TSRMLS_CC)) {
  670. #else
  671. if (FAILURE == zend_fcall_info_init(rewrite, 0, &fci, &fcc, NULL, NULL TSRMLS_CC)) {
  672. #endif
  673. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar error: invalid rewrite callback");
  674. if (free_pathinfo) {
  675. efree(path_info);
  676. }
  677. return;
  678. }
  679. fci.param_count = 1;
  680. fci.params = zp;
  681. #if PHP_VERSION_ID < 50300
  682. ++(params->refcount);
  683. #else
  684. Z_ADDREF_P(params);
  685. #endif
  686. fci.retval_ptr_ptr = &retval_ptr;
  687. if (FAILURE == zend_call_function(&fci, &fcc TSRMLS_CC)) {
  688. if (!EG(exception)) {
  689. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar error: failed to call rewrite callback");
  690. }
  691. if (free_pathinfo) {
  692. efree(path_info);
  693. }
  694. return;
  695. }
  696. if (!fci.retval_ptr_ptr || !retval_ptr) {
  697. if (free_pathinfo) {
  698. efree(path_info);
  699. }
  700. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar error: rewrite callback must return a string or false");
  701. return;
  702. }
  703. switch (Z_TYPE_P(retval_ptr)) {
  704. #if PHP_VERSION_ID >= 60000
  705. case IS_UNICODE:
  706. zval_unicode_to_string(retval_ptr TSRMLS_CC);
  707. /* break intentionally omitted */
  708. #endif
  709. case IS_STRING:
  710. efree(entry);
  711. if (fci.retval_ptr_ptr != &retval_ptr) {
  712. entry = estrndup(Z_STRVAL_PP(fci.retval_ptr_ptr), Z_STRLEN_PP(fci.retval_ptr_ptr));
  713. entry_len = Z_STRLEN_PP(fci.retval_ptr_ptr);
  714. } else {
  715. entry = Z_STRVAL_P(retval_ptr);
  716. entry_len = Z_STRLEN_P(retval_ptr);
  717. }
  718. break;
  719. case IS_BOOL:
  720. phar_do_403(entry, entry_len TSRMLS_CC);
  721. if (free_pathinfo) {
  722. efree(path_info);
  723. }
  724. zend_bailout();
  725. return;
  726. default:
  727. efree(retval_ptr);
  728. if (free_pathinfo) {
  729. efree(path_info);
  730. }
  731. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar error: rewrite callback must return a string or false");
  732. return;
  733. }
  734. }
  735. if (entry_len) {
  736. phar_postprocess_ru_web(fname, fname_len, &entry, &entry_len, &ru, &ru_len TSRMLS_CC);
  737. }
  738. if (!entry_len || (entry_len == 1 && entry[0] == '/')) {
  739. efree(entry);
  740. /* direct request */
  741. if (index_php_len) {
  742. entry = index_php;
  743. entry_len = index_php_len;
  744. if (entry[0] != '/') {
  745. spprintf(&entry, 0, "/%s", index_php);
  746. ++entry_len;
  747. }
  748. } else {
  749. /* assume "index.php" is starting point */
  750. entry = estrndup("/index.php", sizeof("/index.php"));
  751. entry_len = sizeof("/index.php")-1;
  752. }
  753. if (FAILURE == phar_get_archive(&phar, fname, fname_len, NULL, 0, NULL TSRMLS_CC) ||
  754. (info = phar_get_entry_info(phar, entry, entry_len, NULL, 0 TSRMLS_CC)) == NULL) {
  755. phar_do_404(phar, fname, fname_len, f404, f404_len, entry, entry_len TSRMLS_CC);
  756. if (free_pathinfo) {
  757. efree(path_info);
  758. }
  759. zend_bailout();
  760. } else {
  761. char *tmp, sa;
  762. sapi_header_line ctr = {0};
  763. ctr.response_code = 301;
  764. ctr.line_len = sizeof("HTTP/1.1 301 Moved Permanently")+1;
  765. ctr.line = "HTTP/1.1 301 Moved Permanently";
  766. sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
  767. if (not_cgi) {
  768. tmp = strstr(path_info, basename) + fname_len;
  769. sa = *tmp;
  770. *tmp = '\0';
  771. }
  772. ctr.response_code = 0;
  773. if (path_info[strlen(path_info)-1] == '/') {
  774. ctr.line_len = spprintf(&(ctr.line), 4096, "Location: %s%s", path_info, entry + 1);
  775. } else {
  776. ctr.line_len = spprintf(&(ctr.line), 4096, "Location: %s%s", path_info, entry);
  777. }
  778. if (not_cgi) {
  779. *tmp = sa;
  780. }
  781. if (free_pathinfo) {
  782. efree(path_info);
  783. }
  784. sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
  785. sapi_send_headers(TSRMLS_C);
  786. efree(ctr.line);
  787. zend_bailout();
  788. }
  789. }
  790. if (FAILURE == phar_get_archive(&phar, fname, fname_len, NULL, 0, NULL TSRMLS_CC) ||
  791. (info = phar_get_entry_info(phar, entry, entry_len, NULL, 0 TSRMLS_CC)) == NULL) {
  792. phar_do_404(phar, fname, fname_len, f404, f404_len, entry, entry_len TSRMLS_CC);
  793. #ifdef PHP_WIN32
  794. efree(fname);
  795. #endif
  796. zend_bailout();
  797. }
  798. if (mimeoverride && zend_hash_num_elements(Z_ARRVAL_P(mimeoverride))) {
  799. char *ext = zend_memrchr(entry, '.', entry_len);
  800. zval **val;
  801. if (ext) {
  802. ++ext;
  803. #if PHP_MAJOR_VERSION >= 6
  804. if (phar_find_key(Z_ARRVAL_P(mimeoverride), ext, strlen(ext)+1, (void **) &val TSRMLS_CC)) {
  805. #else
  806. if (SUCCESS == zend_hash_find(Z_ARRVAL_P(mimeoverride), ext, strlen(ext)+1, (void **) &val)) {
  807. #endif
  808. switch (Z_TYPE_PP(val)) {
  809. case IS_LONG:
  810. if (Z_LVAL_PP(val) == PHAR_MIME_PHP || Z_LVAL_PP(val) == PHAR_MIME_PHPS) {
  811. mime_type = "";
  812. code = Z_LVAL_PP(val);
  813. } else {
  814. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Unknown mime type specifier used, only Phar::PHP, Phar::PHPS and a mime type string are allowed");
  815. #ifdef PHP_WIN32
  816. efree(fname);
  817. #endif
  818. RETURN_FALSE;
  819. }
  820. break;
  821. #if PHP_MAJOR_VERSION >= 6
  822. case IS_UNICODE:
  823. zval_unicode_to_string(*(val) TSRMLS_CC);
  824. /* break intentionally omitted */
  825. #endif
  826. case IS_STRING:
  827. mime_type = Z_STRVAL_PP(val);
  828. code = PHAR_MIME_OTHER;
  829. break;
  830. default:
  831. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Unknown mime type specifier used (not a string or int), only Phar::PHP, Phar::PHPS and a mime type string are allowed");
  832. #ifdef PHP_WIN32
  833. efree(fname);
  834. #endif
  835. RETURN_FALSE;
  836. }
  837. }
  838. }
  839. }
  840. if (!mime_type) {
  841. code = phar_file_type(&PHAR_G(mime_types), entry, &mime_type TSRMLS_CC);
  842. }
  843. ret = phar_file_action(phar, info, mime_type, code, entry, entry_len, fname, pt, ru, ru_len TSRMLS_CC);
  844. }
  845. /* }}} */
  846. /* {{{ proto void Phar::mungServer(array munglist)
  847. * Defines a list of up to 4 $_SERVER variables that should be modified for execution
  848. * to mask the presence of the phar archive. This should be used in conjunction with
  849. * Phar::webPhar(), and has no effect otherwise
  850. * SCRIPT_NAME, PHP_SELF, REQUEST_URI and SCRIPT_FILENAME
  851. */
  852. PHP_METHOD(Phar, mungServer)
  853. {
  854. zval *mungvalues;
  855. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &mungvalues) == FAILURE) {
  856. return;
  857. }
  858. if (!zend_hash_num_elements(Z_ARRVAL_P(mungvalues))) {
  859. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "No values passed to Phar::mungServer(), expecting an array of any of these strings: PHP_SELF, REQUEST_URI, SCRIPT_FILENAME, SCRIPT_NAME");
  860. return;
  861. }
  862. if (zend_hash_num_elements(Z_ARRVAL_P(mungvalues)) > 4) {
  863. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Too many values passed to Phar::mungServer(), expecting an array of any of these strings: PHP_SELF, REQUEST_URI, SCRIPT_FILENAME, SCRIPT_NAME");
  864. return;
  865. }
  866. phar_request_initialize(TSRMLS_C);
  867. for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(mungvalues)); SUCCESS == zend_hash_has_more_elements(Z_ARRVAL_P(mungvalues)); zend_hash_move_forward(Z_ARRVAL_P(mungvalues))) {
  868. zval **data = NULL;
  869. #if PHP_MAJOR_VERSION >= 6
  870. zval *unicopy = NULL;
  871. #endif
  872. if (SUCCESS != zend_hash_get_current_data(Z_ARRVAL_P(mungvalues), (void **) &data)) {
  873. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "unable to retrieve array value in Phar::mungServer()");
  874. return;
  875. }
  876. #if PHP_MAJOR_VERSION >= 6
  877. if (Z_TYPE_PP(data) == IS_UNICODE) {
  878. MAKE_STD_ZVAL(unicopy);
  879. *unicopy = **data;
  880. zval_copy_ctor(unicopy);
  881. INIT_PZVAL(unicopy);
  882. zval_unicode_to_string(unicopy TSRMLS_CC);
  883. data = &unicopy;
  884. }
  885. #endif
  886. if (Z_TYPE_PP(data) != IS_STRING) {
  887. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Non-string value passed to Phar::mungServer(), expecting an array of any of these strings: PHP_SELF, REQUEST_URI, SCRIPT_FILENAME, SCRIPT_NAME");
  888. return;
  889. }
  890. if (Z_STRLEN_PP(data) == sizeof("PHP_SELF")-1 && !strncmp(Z_STRVAL_PP(data), "PHP_SELF", sizeof("PHP_SELF")-1)) {
  891. PHAR_GLOBALS->phar_SERVER_mung_list |= PHAR_MUNG_PHP_SELF;
  892. }
  893. if (Z_STRLEN_PP(data) == sizeof("REQUEST_URI")-1) {
  894. if (!strncmp(Z_STRVAL_PP(data), "REQUEST_URI", sizeof("REQUEST_URI")-1)) {
  895. PHAR_GLOBALS->phar_SERVER_mung_list |= PHAR_MUNG_REQUEST_URI;
  896. }
  897. if (!strncmp(Z_STRVAL_PP(data), "SCRIPT_NAME", sizeof("SCRIPT_NAME")-1)) {
  898. PHAR_GLOBALS->phar_SERVER_mung_list |= PHAR_MUNG_SCRIPT_NAME;
  899. }
  900. }
  901. if (Z_STRLEN_PP(data) == sizeof("SCRIPT_FILENAME")-1 && !strncmp(Z_STRVAL_PP(data), "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME")-1)) {
  902. PHAR_GLOBALS->phar_SERVER_mung_list |= PHAR_MUNG_SCRIPT_FILENAME;
  903. }
  904. #if PHP_MAJOR_VERSION >= 6
  905. if (unicopy) {
  906. zval_ptr_dtor(&unicopy);
  907. }
  908. #endif
  909. }
  910. }
  911. /* }}} */
  912. /* {{{ proto void Phar::interceptFileFuncs()
  913. * instructs phar to intercept fopen, file_get_contents, opendir, and all of the stat-related functions
  914. * and return stat on files within the phar for relative paths
  915. *
  916. * Once called, this cannot be reversed, and continue until the end of the request.
  917. *
  918. * This allows legacy scripts to be pharred unmodified
  919. */
  920. PHP_METHOD(Phar, interceptFileFuncs)
  921. {
  922. phar_intercept_functions(TSRMLS_C);
  923. }
  924. /* }}} */
  925. /* {{{ proto array Phar::createDefaultStub([string indexfile[, string webindexfile]])
  926. * Return a stub that can be used to run a phar-based archive without the phar extension
  927. * indexfile is the CLI startup filename, which defaults to "index.php", webindexfile
  928. * is the web startup filename, and also defaults to "index.php"
  929. */
  930. PHP_METHOD(Phar, createDefaultStub)
  931. {
  932. char *index = NULL, *webindex = NULL, *stub, *error;
  933. int index_len = 0, webindex_len = 0;
  934. size_t stub_len;
  935. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ss", &index, &index_len, &webindex, &webindex_len) == FAILURE) {
  936. return;
  937. }
  938. stub = phar_create_default_stub(index, webindex, &stub_len, &error TSRMLS_CC);
  939. if (error) {
  940. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  941. efree(error);
  942. return;
  943. }
  944. RETURN_STRINGL(stub, stub_len, 0);
  945. }
  946. /* }}} */
  947. /* {{{ proto mixed Phar::mapPhar([string alias, [int dataoffset]])
  948. * Reads the currently executed file (a phar) and registers its manifest */
  949. PHP_METHOD(Phar, mapPhar)
  950. {
  951. char *alias = NULL, *error;
  952. int alias_len = 0;
  953. long dataoffset = 0;
  954. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!l", &alias, &alias_len, &dataoffset) == FAILURE) {
  955. return;
  956. }
  957. phar_request_initialize(TSRMLS_C);
  958. RETVAL_BOOL(phar_open_executed_filename(alias, alias_len, &error TSRMLS_CC) == SUCCESS);
  959. if (error) {
  960. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  961. efree(error);
  962. }
  963. } /* }}} */
  964. /* {{{ proto mixed Phar::loadPhar(string filename [, string alias])
  965. * Loads any phar archive with an alias */
  966. PHP_METHOD(Phar, loadPhar)
  967. {
  968. char *fname, *alias = NULL, *error;
  969. int fname_len, alias_len = 0;
  970. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s!", &fname, &fname_len, &alias, &alias_len) == FAILURE) {
  971. return;
  972. }
  973. phar_request_initialize(TSRMLS_C);
  974. RETVAL_BOOL(phar_open_from_filename(fname, fname_len, alias, alias_len, REPORT_ERRORS, NULL, &error TSRMLS_CC) == SUCCESS);
  975. if (error) {
  976. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  977. efree(error);
  978. }
  979. } /* }}} */
  980. /* {{{ proto string Phar::apiVersion()
  981. * Returns the api version */
  982. PHP_METHOD(Phar, apiVersion)
  983. {
  984. RETURN_STRINGL(PHP_PHAR_API_VERSION, sizeof(PHP_PHAR_API_VERSION)-1, 1);
  985. }
  986. /* }}}*/
  987. /* {{{ proto bool Phar::canCompress([int method])
  988. * Returns whether phar extension supports compression using zlib/bzip2 */
  989. PHP_METHOD(Phar, canCompress)
  990. {
  991. long method = 0;
  992. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &method) == FAILURE) {
  993. return;
  994. }
  995. phar_request_initialize(TSRMLS_C);
  996. switch (method) {
  997. case PHAR_ENT_COMPRESSED_GZ:
  998. if (PHAR_G(has_zlib)) {
  999. RETURN_TRUE;
  1000. } else {
  1001. RETURN_FALSE;
  1002. }
  1003. case PHAR_ENT_COMPRESSED_BZ2:
  1004. if (PHAR_G(has_bz2)) {
  1005. RETURN_TRUE;
  1006. } else {
  1007. RETURN_FALSE;
  1008. }
  1009. default:
  1010. if (PHAR_G(has_zlib) || PHAR_G(has_bz2)) {
  1011. RETURN_TRUE;
  1012. } else {
  1013. RETURN_FALSE;
  1014. }
  1015. }
  1016. }
  1017. /* }}} */
  1018. /* {{{ proto bool Phar::canWrite()
  1019. * Returns whether phar extension supports writing and creating phars */
  1020. PHP_METHOD(Phar, canWrite)
  1021. {
  1022. RETURN_BOOL(!PHAR_G(readonly));
  1023. }
  1024. /* }}} */
  1025. /* {{{ proto bool Phar::isValidPharFilename(string filename[, bool executable = true])
  1026. * Returns whether the given filename is a valid phar filename */
  1027. PHP_METHOD(Phar, isValidPharFilename)
  1028. {
  1029. char *fname;
  1030. const char *ext_str;
  1031. int fname_len, ext_len, is_executable;
  1032. zend_bool executable = 1;
  1033. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &fname, &fname_len, &executable) == FAILURE) {
  1034. return;
  1035. }
  1036. is_executable = executable;
  1037. RETVAL_BOOL(phar_detect_phar_fname_ext(fname, fname_len, &ext_str, &ext_len, is_executable, 2, 1 TSRMLS_CC) == SUCCESS);
  1038. }
  1039. /* }}} */
  1040. #if HAVE_SPL
  1041. /**
  1042. * from spl_directory
  1043. */
  1044. static void phar_spl_foreign_dtor(spl_filesystem_object *object TSRMLS_DC) /* {{{ */
  1045. {
  1046. phar_archive_data *phar = (phar_archive_data *) object->oth;
  1047. if (!phar->is_persistent) {
  1048. phar_archive_delref(phar TSRMLS_CC);
  1049. }
  1050. object->oth = NULL;
  1051. }
  1052. /* }}} */
  1053. /**
  1054. * from spl_directory
  1055. */
  1056. static void phar_spl_foreign_clone(spl_filesystem_object *src, spl_filesystem_object *dst TSRMLS_DC) /* {{{ */
  1057. {
  1058. phar_archive_data *phar_data = (phar_archive_data *) dst->oth;
  1059. if (!phar_data->is_persistent) {
  1060. ++(phar_data->refcount);
  1061. }
  1062. }
  1063. /* }}} */
  1064. static spl_other_handler phar_spl_foreign_handler = {
  1065. phar_spl_foreign_dtor,
  1066. phar_spl_foreign_clone
  1067. };
  1068. #endif /* HAVE_SPL */
  1069. /* {{{ proto void Phar::__construct(string fname [, int flags [, string alias]])
  1070. * Construct a Phar archive object
  1071. *
  1072. * proto void PharData::__construct(string fname [[, int flags [, string alias]], int file format = Phar::TAR])
  1073. * Construct a PharData archive object
  1074. *
  1075. * This function is used as the constructor for both the Phar and PharData
  1076. * classes, hence the two prototypes above.
  1077. */
  1078. PHP_METHOD(Phar, __construct)
  1079. {
  1080. #if !HAVE_SPL
  1081. zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Cannot instantiate Phar object without SPL extension");
  1082. #else
  1083. char *fname, *alias = NULL, *error, *arch = NULL, *entry = NULL, *save_fname;
  1084. int fname_len, alias_len = 0, arch_len, entry_len, is_data;
  1085. #if PHP_VERSION_ID < 50300
  1086. long flags = 0;
  1087. #else
  1088. long flags = SPL_FILE_DIR_SKIPDOTS|SPL_FILE_DIR_UNIXPATHS;
  1089. #endif
  1090. long format = 0;
  1091. phar_archive_object *phar_obj;
  1092. phar_archive_data *phar_data;
  1093. zval *zobj = getThis(), arg1, arg2;
  1094. phar_obj = (phar_archive_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  1095. is_data = instanceof_function(Z_OBJCE_P(zobj), phar_ce_data TSRMLS_CC);
  1096. if (is_data) {
  1097. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ls!l", &fname, &fname_len, &flags, &alias, &alias_len, &format) == FAILURE) {
  1098. return;
  1099. }
  1100. } else {
  1101. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ls!", &fname, &fname_len, &flags, &alias, &alias_len) == FAILURE) {
  1102. return;
  1103. }
  1104. }
  1105. if (phar_obj->arc.archive) {
  1106. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Cannot call constructor twice");
  1107. return;
  1108. }
  1109. save_fname = fname;
  1110. if (SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, !is_data, 2 TSRMLS_CC)) {
  1111. /* use arch (the basename for the archive) for fname instead of fname */
  1112. /* this allows support for RecursiveDirectoryIterator of subdirectories */
  1113. #ifdef PHP_WIN32
  1114. phar_unixify_path_separators(arch, arch_len);
  1115. #endif
  1116. fname = arch;
  1117. fname_len = arch_len;
  1118. #ifdef PHP_WIN32
  1119. } else {
  1120. arch = estrndup(fname, fname_len);
  1121. arch_len = fname_len;
  1122. fname = arch;
  1123. phar_unixify_path_separators(arch, arch_len);
  1124. #endif
  1125. }
  1126. if (phar_open_or_create_filename(fname, fname_len, alias, alias_len, is_data, REPORT_ERRORS, &phar_data, &error TSRMLS_CC) == FAILURE) {
  1127. if (fname == arch && fname != save_fname) {
  1128. efree(arch);
  1129. fname = save_fname;
  1130. }
  1131. if (entry) {
  1132. efree(entry);
  1133. }
  1134. if (error) {
  1135. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  1136. "%s", error);
  1137. efree(error);
  1138. } else {
  1139. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  1140. "Phar creation or opening failed");
  1141. }
  1142. return;
  1143. }
  1144. if (is_data && phar_data->is_tar && phar_data->is_brandnew && format == PHAR_FORMAT_ZIP) {
  1145. phar_data->is_zip = 1;
  1146. phar_data->is_tar = 0;
  1147. }
  1148. if (fname == arch) {
  1149. efree(arch);
  1150. fname = save_fname;
  1151. }
  1152. if ((is_data && !phar_data->is_data) || (!is_data && phar_data->is_data)) {
  1153. if (is_data) {
  1154. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  1155. "PharData class can only be used for non-executable tar and zip archives");
  1156. } else {
  1157. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  1158. "Phar class can only be used for executable tar and zip archives");
  1159. }
  1160. efree(entry);
  1161. return;
  1162. }
  1163. is_data = phar_data->is_data;
  1164. if (!phar_data->is_persistent) {
  1165. ++(phar_data->refcount);
  1166. }
  1167. phar_obj->arc.archive = phar_data;
  1168. phar_obj->spl.oth_handler = &phar_spl_foreign_handler;
  1169. if (entry) {
  1170. fname_len = spprintf(&fname, 0, "phar://%s%s", phar_data->fname, entry);
  1171. efree(entry);
  1172. } else {
  1173. fname_len = spprintf(&fname, 0, "phar://%s", phar_data->fname);
  1174. }
  1175. INIT_PZVAL(&arg1);
  1176. ZVAL_STRINGL(&arg1, fname, fname_len, 0);
  1177. INIT_PZVAL(&arg2);
  1178. ZVAL_LONG(&arg2, flags);
  1179. zend_call_method_with_2_params(&zobj, Z_OBJCE_P(zobj),
  1180. &spl_ce_RecursiveDirectoryIterator->constructor, "__construct", NULL, &arg1, &arg2);
  1181. if (!phar_data->is_persistent) {
  1182. phar_obj->arc.archive->is_data = is_data;
  1183. } else if (!EG(exception)) {
  1184. /* register this guy so we can modify if necessary */
  1185. zend_hash_add(&PHAR_GLOBALS->phar_persist_map, (const char *) phar_obj->arc.archive, sizeof(phar_obj->arc.archive), (void *) &phar_obj, sizeof(phar_archive_object **), NULL);
  1186. }
  1187. phar_obj->spl.info_class = phar_ce_entry;
  1188. efree(fname);
  1189. #endif /* HAVE_SPL */
  1190. }
  1191. /* }}} */
  1192. /* {{{ proto array Phar::getSupportedSignatures()
  1193. * Return array of supported signature types
  1194. */
  1195. PHP_METHOD(Phar, getSupportedSignatures)
  1196. {
  1197. array_init(return_value);
  1198. add_next_index_stringl(return_value, "MD5", 3, 1);
  1199. add_next_index_stringl(return_value, "SHA-1", 5, 1);
  1200. #ifdef PHAR_HASH_OK
  1201. add_next_index_stringl(return_value, "SHA-256", 7, 1);
  1202. add_next_index_stringl(return_value, "SHA-512", 7, 1);
  1203. #endif
  1204. #if PHAR_HAVE_OPENSSL
  1205. add_next_index_stringl(return_value, "OpenSSL", 7, 1);
  1206. #else
  1207. if (zend_hash_exists(&module_registry, "openssl", sizeof("openssl"))) {
  1208. add_next_index_stringl(return_value, "OpenSSL", 7, 1);
  1209. }
  1210. #endif
  1211. }
  1212. /* }}} */
  1213. /* {{{ proto array Phar::getSupportedCompression()
  1214. * Return array of supported comparession algorithms
  1215. */
  1216. PHP_METHOD(Phar, getSupportedCompression)
  1217. {
  1218. array_init(return_value);
  1219. phar_request_initialize(TSRMLS_C);
  1220. if (PHAR_G(has_zlib)) {
  1221. add_next_index_stringl(return_value, "GZ", 2, 1);
  1222. }
  1223. if (PHAR_G(has_bz2)) {
  1224. add_next_index_stringl(return_value, "BZIP2", 5, 1);
  1225. }
  1226. }
  1227. /* }}} */
  1228. /* {{{ proto array Phar::unlinkArchive(string archive)
  1229. * Completely remove a phar archive from memory and disk
  1230. */
  1231. PHP_METHOD(Phar, unlinkArchive)
  1232. {
  1233. char *fname, *error, *zname, *arch, *entry;
  1234. int fname_len, zname_len, arch_len, entry_len;
  1235. phar_archive_data *phar;
  1236. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &fname, &fname_len) == FAILURE) {
  1237. RETURN_FALSE;
  1238. }
  1239. if (!fname_len) {
  1240. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Unknown phar archive \"\"");
  1241. return;
  1242. }
  1243. if (FAILURE == phar_open_from_filename(fname, fname_len, NULL, 0, REPORT_ERRORS, &phar, &error TSRMLS_CC)) {
  1244. if (error) {
  1245. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Unknown phar archive \"%s\": %s", fname, error);
  1246. efree(error);
  1247. } else {
  1248. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Unknown phar archive \"%s\"", fname);
  1249. }
  1250. return;
  1251. }
  1252. zname = zend_get_executed_filename(TSRMLS_C);
  1253. zname_len = strlen(zname);
  1254. if (zname_len > 7 && !memcmp(zname, "phar://", 7) && SUCCESS == phar_split_fname(zname, zname_len, &arch, &arch_len, &entry, &entry_len, 2, 0 TSRMLS_CC)) {
  1255. if (arch_len == fname_len && !memcmp(arch, fname, arch_len)) {
  1256. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar archive \"%s\" cannot be unlinked from within itself", fname);
  1257. efree(arch);
  1258. efree(entry);
  1259. return;
  1260. }
  1261. efree(arch);
  1262. efree(entry);
  1263. }
  1264. if (phar->is_persistent) {
  1265. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar archive \"%s\" is in phar.cache_list, cannot unlinkArchive()", fname);
  1266. return;
  1267. }
  1268. if (phar->refcount) {
  1269. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar archive \"%s\" has open file handles or objects. fclose() all file handles, and unset() all objects prior to calling unlinkArchive()", fname);
  1270. return;
  1271. }
  1272. fname = estrndup(phar->fname, phar->fname_len);
  1273. /* invalidate phar cache */
  1274. PHAR_G(last_phar) = NULL;
  1275. PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
  1276. phar_archive_delref(phar TSRMLS_CC);
  1277. unlink(fname);
  1278. efree(fname);
  1279. RETURN_TRUE;
  1280. }
  1281. /* }}} */
  1282. #if HAVE_SPL
  1283. #define PHAR_ARCHIVE_OBJECT() \
  1284. phar_archive_object *phar_obj = (phar_archive_object*)zend_object_store_get_object(getThis() TSRMLS_CC); \
  1285. if (!phar_obj->arc.archive) { \
  1286. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
  1287. "Cannot call method on an uninitialized Phar object"); \
  1288. return; \
  1289. }
  1290. /* {{{ proto void Phar::__destruct()
  1291. * if persistent, remove from the cache
  1292. */
  1293. PHP_METHOD(Phar, __destruct)
  1294. {
  1295. phar_archive_object *phar_obj = (phar_archive_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  1296. if (phar_obj->arc.archive && phar_obj->arc.archive->is_persistent) {
  1297. zend_hash_del(&PHAR_GLOBALS->phar_persist_map, (const char *) phar_obj->arc.archive, sizeof(phar_obj->arc.archive));
  1298. }
  1299. }
  1300. /* }}} */
  1301. struct _phar_t {
  1302. phar_archive_object *p;
  1303. zend_class_entry *c;
  1304. char *b;
  1305. uint l;
  1306. zval *ret;
  1307. int count;
  1308. php_stream *fp;
  1309. };
  1310. static int phar_build(zend_object_iterator *iter, void *puser TSRMLS_DC) /* {{{ */
  1311. {
  1312. zval **value;
  1313. zend_uchar key_type;
  1314. zend_bool close_fp = 1;
  1315. ulong int_key;
  1316. struct _phar_t *p_obj = (struct _phar_t*) puser;
  1317. uint str_key_len, base_len = p_obj->l, fname_len;
  1318. phar_entry_data *data;
  1319. php_stream *fp;
  1320. size_t contents_len;
  1321. char *fname, *error = NULL, *base = p_obj->b, *opened, *save = NULL, *temp = NULL;
  1322. phar_zstr key;
  1323. char *str_key;
  1324. zend_class_entry *ce = p_obj->c;
  1325. phar_archive_object *phar_obj = p_obj->p;
  1326. char *str = "[stream]";
  1327. iter->funcs->get_current_data(iter, &value TSRMLS_CC);
  1328. if (EG(exception)) {
  1329. return ZEND_HASH_APPLY_STOP;
  1330. }
  1331. if (!value) {
  1332. /* failure in get_current_data */
  1333. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned no value", ce->name);
  1334. return ZEND_HASH_APPLY_STOP;
  1335. }
  1336. switch (Z_TYPE_PP(value)) {
  1337. #if PHP_VERSION_ID >= 60000
  1338. case IS_UNICODE:
  1339. zval_unicode_to_string(*(value) TSRMLS_CC);
  1340. /* break intentionally omitted */
  1341. #endif
  1342. case IS_STRING:
  1343. break;
  1344. case IS_RESOURCE:
  1345. php_stream_from_zval_no_verify(fp, value);
  1346. if (!fp) {
  1347. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Iterator %v returned an invalid stream handle", ce->name);
  1348. return ZEND_HASH_APPLY_STOP;
  1349. }
  1350. if (iter->funcs->get_current_key) {
  1351. key_type = iter->funcs->get_current_key(iter, &key, &str_key_len, &int_key TSRMLS_CC);
  1352. if (EG(exception)) {
  1353. return ZEND_HASH_APPLY_STOP;
  1354. }
  1355. if (key_type == HASH_KEY_IS_LONG) {
  1356. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned an invalid key (must return a string)", ce->name);
  1357. return ZEND_HASH_APPLY_STOP;
  1358. }
  1359. if (key_type > 9) { /* IS_UNICODE == 10 */
  1360. #if PHP_VERSION_ID < 60000
  1361. /* this can never happen, but fixes a compile warning */
  1362. spprintf(&str_key, 0, "%s", key);
  1363. #else
  1364. spprintf(&str_key, 0, "%v", key);
  1365. ezfree(key);
  1366. #endif
  1367. } else {
  1368. PHAR_STR(key, str_key);
  1369. }
  1370. save = str_key;
  1371. if (str_key[str_key_len - 1] == '\0') {
  1372. str_key_len--;
  1373. }
  1374. } else {
  1375. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned an invalid key (must return a string)", ce->name);
  1376. return ZEND_HASH_APPLY_STOP;
  1377. }
  1378. close_fp = 0;
  1379. opened = (char *) estrndup(str, sizeof("[stream]") + 1);
  1380. goto after_open_fp;
  1381. case IS_OBJECT:
  1382. if (instanceof_function(Z_OBJCE_PP(value), spl_ce_SplFileInfo TSRMLS_CC)) {
  1383. char *test = NULL;
  1384. zval dummy;
  1385. spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(*value TSRMLS_CC);
  1386. if (!base_len) {
  1387. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Iterator %v returns an SplFileInfo object, so base directory must be specified", ce->name);
  1388. return ZEND_HASH_APPLY_STOP;
  1389. }
  1390. switch (intern->type) {
  1391. case SPL_FS_DIR:
  1392. #if PHP_VERSION_ID >= 60000
  1393. test = spl_filesystem_object_get_path(intern, NULL, NULL TSRMLS_CC).s;
  1394. #elif PHP_VERSION_ID >= 50300
  1395. test = spl_filesystem_object_get_path(intern, NULL TSRMLS_CC);
  1396. #else
  1397. test = intern->path;
  1398. #endif
  1399. fname_len = spprintf(&fname, 0, "%s%c%s", test, DEFAULT_SLASH, intern->u.dir.entry.d_name);
  1400. php_stat(fname, fname_len, FS_IS_DIR, &dummy TSRMLS_CC);
  1401. if (Z_BVAL(dummy)) {
  1402. /* ignore directories */
  1403. efree(fname);
  1404. return ZEND_HASH_APPLY_KEEP;
  1405. }
  1406. test = expand_filepath(fname, NULL TSRMLS_CC);
  1407. if (test) {
  1408. efree(fname);
  1409. fname = test;
  1410. fname_len = strlen(fname);
  1411. }
  1412. save = fname;
  1413. goto phar_spl_fileinfo;
  1414. case SPL_FS_INFO:
  1415. case SPL_FS_FILE:
  1416. #if PHP_VERSION_ID >= 60000
  1417. if (intern->file_name_type == IS_UNICODE) {
  1418. zval zv;
  1419. INIT_ZVAL(zv);
  1420. Z_UNIVAL(zv) = intern->file_name;
  1421. Z_UNILEN(zv) = intern->file_name_len;
  1422. Z_TYPE(zv) = IS_UNICODE;
  1423. zval_copy_ctor(&zv);
  1424. zval_unicode_to_string(&zv TSRMLS_CC);
  1425. fname = expand_filepath(Z_STRVAL(zv), NULL TSRMLS_CC);
  1426. ezfree(Z_UNIVAL(zv));
  1427. } else {
  1428. fname = expand_filepath(intern->file_name.s, NULL TSRMLS_CC);
  1429. }
  1430. #else
  1431. fname = expand_filepath(intern->file_name, NULL TSRMLS_CC);
  1432. #endif
  1433. fname_len = strlen(fname);
  1434. save = fname;
  1435. goto phar_spl_fileinfo;
  1436. }
  1437. }
  1438. /* fall-through */
  1439. default:
  1440. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned an invalid value (must return a string)", ce->name);
  1441. return ZEND_HASH_APPLY_STOP;
  1442. }
  1443. fname = Z_STRVAL_PP(value);
  1444. fname_len = Z_STRLEN_PP(value);
  1445. phar_spl_fileinfo:
  1446. if (base_len) {
  1447. temp = expand_filepath(base, NULL TSRMLS_CC);
  1448. base = temp;
  1449. base_len = strlen(base);
  1450. if (strstr(fname, base)) {
  1451. str_key_len = fname_len - base_len;
  1452. if (str_key_len <= 0) {
  1453. if (save) {
  1454. efree(save);
  1455. efree(temp);
  1456. }
  1457. return ZEND_HASH_APPLY_KEEP;
  1458. }
  1459. str_key = fname + base_len;
  1460. if (*str_key == '/' || *str_key == '\\') {
  1461. str_key++;
  1462. str_key_len--;
  1463. }
  1464. } else {
  1465. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned a path \"%s\" that is not in the base directory \"%s\"", ce->name, fname, base);
  1466. if (save) {
  1467. efree(save);
  1468. efree(temp);
  1469. }
  1470. return ZEND_HASH_APPLY_STOP;
  1471. }
  1472. } else {
  1473. if (iter->funcs->get_current_key) {
  1474. key_type = iter->funcs->get_current_key(iter, &key, &str_key_len, &int_key TSRMLS_CC);
  1475. if (EG(exception)) {
  1476. return ZEND_HASH_APPLY_STOP;
  1477. }
  1478. if (key_type == HASH_KEY_IS_LONG) {
  1479. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned an invalid key (must return a string)", ce->name);
  1480. return ZEND_HASH_APPLY_STOP;
  1481. }
  1482. if (key_type > 9) { /* IS_UNICODE == 10 */
  1483. #if PHP_VERSION_ID < 60000
  1484. /* this can never happen, but fixes a compile warning */
  1485. spprintf(&str_key, 0, "%s", key);
  1486. #else
  1487. spprintf(&str_key, 0, "%v", key);
  1488. ezfree(key);
  1489. #endif
  1490. } else {
  1491. PHAR_STR(key, str_key);
  1492. }
  1493. save = str_key;
  1494. if (str_key[str_key_len - 1] == '\0') str_key_len--;
  1495. } else {
  1496. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned an invalid key (must return a string)", ce->name);
  1497. return ZEND_HASH_APPLY_STOP;
  1498. }
  1499. }
  1500. if (php_check_open_basedir(fname TSRMLS_CC)) {
  1501. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned a path \"%s\" that open_basedir prevents opening", ce->name, fname);
  1502. if (save) {
  1503. efree(save);
  1504. }
  1505. if (temp) {
  1506. efree(temp);
  1507. }
  1508. return ZEND_HASH_APPLY_STOP;
  1509. }
  1510. /* try to open source file, then create internal phar file and copy contents */
  1511. fp = php_stream_open_wrapper(fname, "rb", STREAM_MUST_SEEK|0, &opened);
  1512. if (!fp) {
  1513. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned a file that could not be opened \"%s\"", ce->name, fname);
  1514. if (save) {
  1515. efree(save);
  1516. }
  1517. if (temp) {
  1518. efree(temp);
  1519. }
  1520. return ZEND_HASH_APPLY_STOP;
  1521. }
  1522. after_open_fp:
  1523. if (str_key_len >= sizeof(".phar")-1 && !memcmp(str_key, ".phar", sizeof(".phar")-1)) {
  1524. /* silently skip any files that would be added to the magic .phar directory */
  1525. if (save) {
  1526. efree(save);
  1527. }
  1528. if (temp) {
  1529. efree(temp);
  1530. }
  1531. if (opened) {
  1532. efree(opened);
  1533. }
  1534. if (close_fp) {
  1535. php_stream_close(fp);
  1536. }
  1537. return ZEND_HASH_APPLY_KEEP;
  1538. }
  1539. if (!(data = phar_get_or_create_entry_data(phar_obj->arc.archive->fname, phar_obj->arc.archive->fname_len, str_key, str_key_len, "w+b", 0, &error, 1 TSRMLS_CC))) {
  1540. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Entry %s cannot be created: %s", str_key, error);
  1541. efree(error);
  1542. if (save) {
  1543. efree(save);
  1544. }
  1545. if (opened) {
  1546. efree(opened);
  1547. }
  1548. if (temp) {
  1549. efree(temp);
  1550. }
  1551. if (close_fp) {
  1552. php_stream_close(fp);
  1553. }
  1554. return ZEND_HASH_APPLY_STOP;
  1555. } else {
  1556. if (error) {
  1557. efree(error);
  1558. }
  1559. /* convert to PHAR_UFP */
  1560. if (data->internal_file->fp_type == PHAR_MOD) {
  1561. php_stream_close(data->internal_file->fp);
  1562. }
  1563. data->internal_file->fp = NULL;
  1564. data->internal_file->fp_type = PHAR_UFP;
  1565. data->internal_file->offset_abs = data->internal_file->offset = php_stream_tell(p_obj->fp);
  1566. data->fp = NULL;
  1567. phar_stream_copy_to_stream(fp, p_obj->fp, PHP_STREAM_COPY_ALL, &contents_len);
  1568. data->internal_file->uncompressed_filesize = data->internal_file->compressed_filesize =
  1569. php_stream_tell(p_obj->fp) - data->internal_file->offset;
  1570. }
  1571. if (close_fp) {
  1572. php_stream_close(fp);
  1573. }
  1574. add_assoc_string(p_obj->ret, str_key, opened, 0);
  1575. if (save) {
  1576. efree(save);
  1577. }
  1578. if (temp) {
  1579. efree(temp);
  1580. }
  1581. data->internal_file->compressed_filesize = data->internal_file->uncompressed_filesize = contents_len;
  1582. phar_entry_delref(data TSRMLS_CC);
  1583. return ZEND_HASH_APPLY_KEEP;
  1584. }
  1585. /* }}} */
  1586. /* {{{ proto array Phar::buildFromDirectory(string base_dir[, string regex])
  1587. * Construct a phar archive from an existing directory, recursively.
  1588. * Optional second parameter is a regular expression for filtering directory contents.
  1589. *
  1590. * Return value is an array mapping phar index to actual files added.
  1591. */
  1592. PHP_METHOD(Phar, buildFromDirectory)
  1593. {
  1594. char *dir, *error, *regex = NULL;
  1595. int dir_len, regex_len = 0;
  1596. zend_bool apply_reg = 0;
  1597. zval arg, arg2, *iter, *iteriter, *regexiter = NULL;
  1598. struct _phar_t pass;
  1599. PHAR_ARCHIVE_OBJECT();
  1600. if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
  1601. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  1602. "Cannot write to archive - write operations restricted by INI setting");
  1603. return;
  1604. }
  1605. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &dir, &dir_len, &regex, &regex_len) == FAILURE) {
  1606. RETURN_FALSE;
  1607. }
  1608. MAKE_STD_ZVAL(iter);
  1609. if (SUCCESS != object_init_ex(iter, spl_ce_RecursiveDirectoryIterator)) {
  1610. zval_ptr_dtor(&iter);
  1611. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Unable to instantiate directory iterator for %s", phar_obj->arc.archive->fname);
  1612. RETURN_FALSE;
  1613. }
  1614. INIT_PZVAL(&arg);
  1615. ZVAL_STRINGL(&arg, dir, dir_len, 0);
  1616. INIT_PZVAL(&arg2);
  1617. #if PHP_VERSION_ID < 50300
  1618. ZVAL_LONG(&arg2, 0);
  1619. #else
  1620. ZVAL_LONG(&arg2, SPL_FILE_DIR_SKIPDOTS|SPL_FILE_DIR_UNIXPATHS);
  1621. #endif
  1622. zend_call_method_with_2_params(&iter, spl_ce_RecursiveDirectoryIterator,
  1623. &spl_ce_RecursiveDirectoryIterator->constructor, "__construct", NULL, &arg, &arg2);
  1624. if (EG(exception)) {
  1625. zval_ptr_dtor(&iter);
  1626. RETURN_FALSE;
  1627. }
  1628. MAKE_STD_ZVAL(iteriter);
  1629. if (SUCCESS != object_init_ex(iteriter, spl_ce_RecursiveIteratorIterator)) {
  1630. zval_ptr_dtor(&iter);
  1631. zval_ptr_dtor(&iteriter);
  1632. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Unable to instantiate directory iterator for %s", phar_obj->arc.archive->fname);
  1633. RETURN_FALSE;
  1634. }
  1635. zend_call_method_with_1_params(&iteriter, spl_ce_RecursiveIteratorIterator,
  1636. &spl_ce_RecursiveIteratorIterator->constructor, "__construct", NULL, iter);
  1637. if (EG(exception)) {
  1638. zval_ptr_dtor(&iter);
  1639. zval_ptr_dtor(&iteriter);
  1640. RETURN_FALSE;
  1641. }
  1642. zval_ptr_dtor(&iter);
  1643. if (regex_len > 0) {
  1644. apply_reg = 1;
  1645. MAKE_STD_ZVAL(regexiter);
  1646. if (SUCCESS != object_init_ex(regexiter, spl_ce_RegexIterator)) {
  1647. zval_ptr_dtor(&iteriter);
  1648. zval_dtor(regexiter);
  1649. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Unable to instantiate regex iterator for %s", phar_obj->arc.archive->fname);
  1650. RETURN_FALSE;
  1651. }
  1652. INIT_PZVAL(&arg2);
  1653. ZVAL_STRINGL(&arg2, regex, regex_len, 0);
  1654. zend_call_method_with_2_params(&regexiter, spl_ce_RegexIterator,
  1655. &spl_ce_RegexIterator->constructor, "__construct", NULL, iteriter, &arg2);
  1656. }
  1657. array_init(return_value);
  1658. pass.c = apply_reg ? Z_OBJCE_P(regexiter) : Z_OBJCE_P(iteriter);
  1659. pass.p = phar_obj;
  1660. pass.b = dir;
  1661. pass.l = dir_len;
  1662. pass.count = 0;
  1663. pass.ret = return_value;
  1664. pass.fp = php_stream_fopen_tmpfile();
  1665. if (phar_obj->arc.archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->arc.archive) TSRMLS_CC)) {
  1666. zval_ptr_dtor(&iteriter);
  1667. if (apply_reg) {
  1668. zval_ptr_dtor(&regexiter);
  1669. }
  1670. php_stream_close(pass.fp);
  1671. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar_obj->arc.archive->fname);
  1672. return;
  1673. }
  1674. if (SUCCESS == spl_iterator_apply((apply_reg ? regexiter : iteriter), (spl_iterator_apply_func_t) phar_build, (void *) &pass TSRMLS_CC)) {
  1675. zval_ptr_dtor(&iteriter);
  1676. if (apply_reg) {
  1677. zval_ptr_dtor(&regexiter);
  1678. }
  1679. phar_obj->arc.archive->ufp = pass.fp;
  1680. phar_flush(phar_obj->arc.archive, 0, 0, 0, &error TSRMLS_CC);
  1681. if (error) {
  1682. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  1683. efree(error);
  1684. }
  1685. } else {
  1686. zval_ptr_dtor(&iteriter);
  1687. if (apply_reg) {
  1688. zval_ptr_dtor(&regexiter);
  1689. }
  1690. php_stream_close(pass.fp);
  1691. }
  1692. }
  1693. /* }}} */
  1694. /* {{{ proto array Phar::buildFromIterator(Iterator iter[, string base_directory])
  1695. * Construct a phar archive from an iterator. The iterator must return a series of strings
  1696. * that are full paths to files that should be added to the phar. The iterator key should
  1697. * be the path that the file will have within the phar archive.
  1698. *
  1699. * If base directory is specified, then the key will be ignored, and instead the portion of
  1700. * the current value minus the base directory will be used
  1701. *
  1702. * Returned is an array mapping phar index to actual file added
  1703. */
  1704. PHP_METHOD(Phar, buildFromIterator)
  1705. {
  1706. zval *obj;
  1707. char *error;
  1708. uint base_len = 0;
  1709. char *base = NULL;
  1710. struct _phar_t pass;
  1711. PHAR_ARCHIVE_OBJECT();
  1712. if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
  1713. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  1714. "Cannot write out phar archive, phar is read-only");
  1715. return;
  1716. }
  1717. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|s", &obj, zend_ce_traversable, &base, &base_len) == FAILURE) {
  1718. RETURN_FALSE;
  1719. }
  1720. if (phar_obj->arc.archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->arc.archive) TSRMLS_CC)) {
  1721. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar_obj->arc.archive->fname);
  1722. return;
  1723. }
  1724. array_init(return_value);
  1725. pass.c = Z_OBJCE_P(obj);
  1726. pass.p = phar_obj;
  1727. pass.b = base;
  1728. pass.l = base_len;
  1729. pass.ret = return_value;
  1730. pass.count = 0;
  1731. pass.fp = php_stream_fopen_tmpfile();
  1732. if (SUCCESS == spl_iterator_apply(obj, (spl_iterator_apply_func_t) phar_build, (void *) &pass TSRMLS_CC)) {
  1733. phar_obj->arc.archive->ufp = pass.fp;
  1734. phar_flush(phar_obj->arc.archive, 0, 0, 0, &error TSRMLS_CC);
  1735. if (error) {
  1736. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  1737. efree(error);
  1738. }
  1739. } else {
  1740. php_stream_close(pass.fp);
  1741. }
  1742. }
  1743. /* }}} */
  1744. /* {{{ proto int Phar::count()
  1745. * Returns the number of entries in the Phar archive
  1746. */
  1747. PHP_METHOD(Phar, count)
  1748. {
  1749. PHAR_ARCHIVE_OBJECT();
  1750. RETURN_LONG(zend_hash_num_elements(&phar_obj->arc.archive->manifest));
  1751. }
  1752. /* }}} */
  1753. /* {{{ proto bool Phar::isFileFormat(int format)
  1754. * Returns true if the phar archive is based on the tar/zip/phar file format depending
  1755. * on whether Phar::TAR, Phar::ZIP or Phar::PHAR was passed in
  1756. */
  1757. PHP_METHOD(Phar, isFileFormat)
  1758. {
  1759. long type;
  1760. PHAR_ARCHIVE_OBJECT();
  1761. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type) == FAILURE) {
  1762. RETURN_FALSE;
  1763. }
  1764. switch (type) {
  1765. case PHAR_FORMAT_TAR:
  1766. RETURN_BOOL(phar_obj->arc.archive->is_tar);
  1767. case PHAR_FORMAT_ZIP:
  1768. RETURN_BOOL(phar_obj->arc.archive->is_zip);
  1769. case PHAR_FORMAT_PHAR:
  1770. RETURN_BOOL(!phar_obj->arc.archive->is_tar && !phar_obj->arc.archive->is_zip);
  1771. default:
  1772. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Unknown file format specified");
  1773. }
  1774. }
  1775. /* }}} */
  1776. static int phar_copy_file_contents(phar_entry_info *entry, php_stream *fp TSRMLS_DC) /* {{{ */
  1777. {
  1778. char *error;
  1779. off_t offset;
  1780. phar_entry_info *link;
  1781. if (FAILURE == phar_open_entry_fp(entry, &error, 1 TSRMLS_CC)) {
  1782. if (error) {
  1783. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  1784. "Cannot convert phar archive \"%s\", unable to open entry \"%s\" contents: %s", entry->phar->fname, entry->filename, error);
  1785. efree(error);
  1786. } else {
  1787. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  1788. "Cannot convert phar archive \"%s\", unable to open entry \"%s\" contents", entry->phar->fname, entry->filename);
  1789. }
  1790. return FAILURE;
  1791. }
  1792. /* copy old contents in entirety */
  1793. phar_seek_efp(entry, 0, SEEK_SET, 0, 1 TSRMLS_CC);
  1794. offset = php_stream_tell(fp);
  1795. link = phar_get_link_source(entry TSRMLS_CC);
  1796. if (!link) {
  1797. link = entry;
  1798. }
  1799. if (SUCCESS != phar_stream_copy_to_stream(phar_get_efp(link, 0 TSRMLS_CC), fp, link->uncompressed_filesize, NULL)) {
  1800. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  1801. "Cannot convert phar archive \"%s\", unable to copy entry \"%s\" contents", entry->phar->fname, entry->filename);
  1802. return FAILURE;
  1803. }
  1804. if (entry->fp_type == PHAR_MOD) {
  1805. /* save for potential restore on error */
  1806. entry->cfp = entry->fp;
  1807. entry->fp = NULL;
  1808. }
  1809. /* set new location of file contents */
  1810. entry->fp_type = PHAR_FP;
  1811. entry->offset = offset;
  1812. return SUCCESS;
  1813. }
  1814. /* }}} */
  1815. static zval *phar_rename_archive(phar_archive_data *phar, char *ext, zend_bool compress TSRMLS_DC) /* {{{ */
  1816. {
  1817. char *oldname = NULL, *oldpath = NULL;
  1818. char *basename = NULL, *basepath = NULL;
  1819. char *newname = NULL, *newpath = NULL;
  1820. zval *ret, arg1;
  1821. zend_class_entry *ce;
  1822. char *error;
  1823. const char *pcr_error;
  1824. int ext_len = ext ? strlen(ext) : 0;
  1825. int oldname_len;
  1826. phar_archive_data **pphar = NULL;
  1827. php_stream_statbuf ssb;
  1828. if (!ext) {
  1829. if (phar->is_zip) {
  1830. if (phar->is_data) {
  1831. ext = "zip";
  1832. } else {
  1833. ext = "phar.zip";
  1834. }
  1835. } else if (phar->is_tar) {
  1836. switch (phar->flags) {
  1837. case PHAR_FILE_COMPRESSED_GZ:
  1838. if (phar->is_data) {
  1839. ext = "tar.gz";
  1840. } else {
  1841. ext = "phar.tar.gz";
  1842. }
  1843. break;
  1844. case PHAR_FILE_COMPRESSED_BZ2:
  1845. if (phar->is_data) {
  1846. ext = "tar.bz2";
  1847. } else {
  1848. ext = "phar.tar.bz2";
  1849. }
  1850. break;
  1851. default:
  1852. if (phar->is_data) {
  1853. ext = "tar";
  1854. } else {
  1855. ext = "phar.tar";
  1856. }
  1857. }
  1858. } else {
  1859. switch (phar->flags) {
  1860. case PHAR_FILE_COMPRESSED_GZ:
  1861. ext = "phar.gz";
  1862. break;
  1863. case PHAR_FILE_COMPRESSED_BZ2:
  1864. ext = "phar.bz2";
  1865. break;
  1866. default:
  1867. ext = "phar";
  1868. }
  1869. }
  1870. } else if (phar_path_check(&ext, &ext_len, &pcr_error) > pcr_is_ok) {
  1871. if (phar->is_data) {
  1872. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "data phar converted from \"%s\" has invalid extension %s", phar->fname, ext);
  1873. } else {
  1874. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "phar converted from \"%s\" has invalid extension %s", phar->fname, ext);
  1875. }
  1876. return NULL;
  1877. }
  1878. if (ext[0] == '.') {
  1879. ++ext;
  1880. }
  1881. oldpath = estrndup(phar->fname, phar->fname_len);
  1882. oldname = zend_memrchr(phar->fname, '/', phar->fname_len);
  1883. ++oldname;
  1884. oldname_len = strlen(oldname);
  1885. basename = estrndup(oldname, oldname_len);
  1886. spprintf(&newname, 0, "%s.%s", strtok(basename, "."), ext);
  1887. efree(basename);
  1888. basepath = estrndup(oldpath, (strlen(oldpath) - oldname_len));
  1889. phar->fname_len = spprintf(&newpath, 0, "%s%s", basepath, newname);
  1890. phar->fname = newpath;
  1891. phar->ext = newpath + phar->fname_len - strlen(ext) - 1;
  1892. efree(basepath);
  1893. efree(newname);
  1894. if (PHAR_G(manifest_cached) && SUCCESS == zend_hash_find(&cached_phars, newpath, phar->fname_len, (void **) &pphar)) {
  1895. efree(oldpath);
  1896. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Unable to add newly converted phar \"%s\" to the list of phars, new phar name is in phar.cache_list", phar->fname);
  1897. return NULL;
  1898. }
  1899. if (SUCCESS == zend_hash_find(&(PHAR_GLOBALS->phar_fname_map), newpath, phar->fname_len, (void **) &pphar)) {
  1900. if ((*pphar)->fname_len == phar->fname_len && !memcmp((*pphar)->fname, phar->fname, phar->fname_len)) {
  1901. if (!zend_hash_num_elements(&phar->manifest)) {
  1902. (*pphar)->is_tar = phar->is_tar;
  1903. (*pphar)->is_zip = phar->is_zip;
  1904. (*pphar)->is_data = phar->is_data;
  1905. (*pphar)->flags = phar->flags;
  1906. (*pphar)->fp = phar->fp;
  1907. phar->fp = NULL;
  1908. phar_destroy_phar_data(phar TSRMLS_CC);
  1909. phar = *pphar;
  1910. phar->refcount++;
  1911. newpath = oldpath;
  1912. goto its_ok;
  1913. }
  1914. }
  1915. efree(oldpath);
  1916. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Unable to add newly converted phar \"%s\" to the list of phars, a phar with that name already exists", phar->fname);
  1917. return NULL;
  1918. }
  1919. its_ok:
  1920. if (SUCCESS == php_stream_stat_path(newpath, &ssb)) {
  1921. efree(oldpath);
  1922. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "phar \"%s\" exists and must be unlinked prior to conversion", newpath);
  1923. return NULL;
  1924. }
  1925. if (!phar->is_data) {
  1926. if (SUCCESS != phar_detect_phar_fname_ext(newpath, phar->fname_len, (const char **) &(phar->ext), &(phar->ext_len), 1, 1, 1 TSRMLS_CC)) {
  1927. efree(oldpath);
  1928. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "phar \"%s\" has invalid extension %s", phar->fname, ext);
  1929. return NULL;
  1930. }
  1931. if (phar->alias) {
  1932. if (phar->is_temporary_alias) {
  1933. phar->alias = NULL;
  1934. phar->alias_len = 0;
  1935. } else {
  1936. phar->alias = estrndup(newpath, strlen(newpath));
  1937. phar->alias_len = strlen(newpath);
  1938. phar->is_temporary_alias = 1;
  1939. zend_hash_update(&(PHAR_GLOBALS->phar_alias_map), newpath, phar->fname_len, (void*)&phar, sizeof(phar_archive_data*), NULL);
  1940. }
  1941. }
  1942. } else {
  1943. if (SUCCESS != phar_detect_phar_fname_ext(newpath, phar->fname_len, (const char **) &(phar->ext), &(phar->ext_len), 0, 1, 1 TSRMLS_CC)) {
  1944. efree(oldpath);
  1945. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "data phar \"%s\" has invalid extension %s", phar->fname, ext);
  1946. return NULL;
  1947. }
  1948. phar->alias = NULL;
  1949. phar->alias_len = 0;
  1950. }
  1951. if ((!pphar || phar == *pphar) && SUCCESS != zend_hash_update(&(PHAR_GLOBALS->phar_fname_map), newpath, phar->fname_len, (void*)&phar, sizeof(phar_archive_data*), NULL)) {
  1952. efree(oldpath);
  1953. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Unable to add newly converted phar \"%s\" to the list of phars", phar->fname);
  1954. return NULL;
  1955. }
  1956. phar_flush(phar, 0, 0, 1, &error TSRMLS_CC);
  1957. if (error) {
  1958. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "%s", error);
  1959. efree(error);
  1960. efree(oldpath);
  1961. return NULL;
  1962. }
  1963. efree(oldpath);
  1964. if (phar->is_data) {
  1965. ce = phar_ce_data;
  1966. } else {
  1967. ce = phar_ce_archive;
  1968. }
  1969. MAKE_STD_ZVAL(ret);
  1970. if (SUCCESS != object_init_ex(ret, ce)) {
  1971. zval_dtor(ret);
  1972. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Unable to instantiate phar object when converting archive \"%s\"", phar->fname);
  1973. return NULL;
  1974. }
  1975. INIT_PZVAL(&arg1);
  1976. ZVAL_STRINGL(&arg1, phar->fname, phar->fname_len, 0);
  1977. zend_call_method_with_1_params(&ret, ce, &ce->constructor, "__construct", NULL, &arg1);
  1978. return ret;
  1979. }
  1980. /* }}} */
  1981. static zval *phar_convert_to_other(phar_archive_data *source, int convert, char *ext, php_uint32 flags TSRMLS_DC) /* {{{ */
  1982. {
  1983. phar_archive_data *phar;
  1984. phar_entry_info *entry, newentry;
  1985. zval *ret;
  1986. /* invalidate phar cache */
  1987. PHAR_G(last_phar) = NULL;
  1988. PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
  1989. phar = (phar_archive_data *) ecalloc(1, sizeof(phar_archive_data));
  1990. /* set whole-archive compression and type from parameter */
  1991. phar->flags = flags;
  1992. phar->is_data = source->is_data;
  1993. switch (convert) {
  1994. case PHAR_FORMAT_TAR:
  1995. phar->is_tar = 1;
  1996. break;
  1997. case PHAR_FORMAT_ZIP:
  1998. phar->is_zip = 1;
  1999. break;
  2000. default:
  2001. phar->is_data = 0;
  2002. break;
  2003. }
  2004. zend_hash_init(&(phar->manifest), sizeof(phar_entry_info),
  2005. zend_get_hash_value, destroy_phar_manifest_entry, 0);
  2006. zend_hash_init(&phar->mounted_dirs, sizeof(char *),
  2007. zend_get_hash_value, NULL, 0);
  2008. zend_hash_init(&phar->virtual_dirs, sizeof(char *),
  2009. zend_get_hash_value, NULL, 0);
  2010. phar->fp = php_stream_fopen_tmpfile();
  2011. phar->fname = source->fname;
  2012. phar->fname_len = source->fname_len;
  2013. phar->is_temporary_alias = source->is_temporary_alias;
  2014. phar->alias = source->alias;
  2015. if (source->metadata) {
  2016. zval *t;
  2017. t = source->metadata;
  2018. ALLOC_ZVAL(phar->metadata);
  2019. *phar->metadata = *t;
  2020. zval_copy_ctor(phar->metadata);
  2021. #if PHP_VERSION_ID < 50300
  2022. phar->metadata->refcount = 1;
  2023. #else
  2024. Z_SET_REFCOUNT_P(phar->metadata, 1);
  2025. #endif
  2026. phar->metadata_len = 0;
  2027. }
  2028. /* first copy each file's uncompressed contents to a temporary file and set per-file flags */
  2029. for (zend_hash_internal_pointer_reset(&source->manifest); SUCCESS == zend_hash_has_more_elements(&source->manifest); zend_hash_move_forward(&source->manifest)) {
  2030. if (FAILURE == zend_hash_get_current_data(&source->manifest, (void **) &entry)) {
  2031. zend_hash_destroy(&(phar->manifest));
  2032. php_stream_close(phar->fp);
  2033. efree(phar);
  2034. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2035. "Cannot convert phar archive \"%s\"", source->fname);
  2036. return NULL;
  2037. }
  2038. newentry = *entry;
  2039. if (newentry.link) {
  2040. newentry.link = estrdup(newentry.link);
  2041. goto no_copy;
  2042. }
  2043. if (newentry.tmp) {
  2044. newentry.tmp = estrdup(newentry.tmp);
  2045. goto no_copy;
  2046. }
  2047. newentry.metadata_str.c = 0;
  2048. if (FAILURE == phar_copy_file_contents(&newentry, phar->fp TSRMLS_CC)) {
  2049. zend_hash_destroy(&(phar->manifest));
  2050. php_stream_close(phar->fp);
  2051. efree(phar);
  2052. /* exception already thrown */
  2053. return NULL;
  2054. }
  2055. no_copy:
  2056. newentry.filename = estrndup(newentry.filename, newentry.filename_len);
  2057. if (newentry.metadata) {
  2058. zval *t;
  2059. t = newentry.metadata;
  2060. ALLOC_ZVAL(newentry.metadata);
  2061. *newentry.metadata = *t;
  2062. zval_copy_ctor(newentry.metadata);
  2063. #if PHP_VERSION_ID < 50300
  2064. newentry.metadata->refcount = 1;
  2065. #else
  2066. Z_SET_REFCOUNT_P(newentry.metadata, 1);
  2067. #endif
  2068. newentry.metadata_str.c = NULL;
  2069. newentry.metadata_str.len = 0;
  2070. }
  2071. newentry.is_zip = phar->is_zip;
  2072. newentry.is_tar = phar->is_tar;
  2073. if (newentry.is_tar) {
  2074. newentry.tar_type = (entry->is_dir ? TAR_DIR : TAR_FILE);
  2075. }
  2076. newentry.is_modified = 1;
  2077. newentry.phar = phar;
  2078. newentry.old_flags = newentry.flags & ~PHAR_ENT_COMPRESSION_MASK; /* remove compression from old_flags */
  2079. phar_set_inode(&newentry TSRMLS_CC);
  2080. zend_hash_add(&(phar->manifest), newentry.filename, newentry.filename_len, (void*)&newentry, sizeof(phar_entry_info), NULL);
  2081. phar_add_virtual_dirs(phar, newentry.filename, newentry.filename_len TSRMLS_CC);
  2082. }
  2083. if ((ret = phar_rename_archive(phar, ext, 0 TSRMLS_CC))) {
  2084. return ret;
  2085. } else {
  2086. zend_hash_destroy(&(phar->manifest));
  2087. zend_hash_destroy(&(phar->mounted_dirs));
  2088. zend_hash_destroy(&(phar->virtual_dirs));
  2089. php_stream_close(phar->fp);
  2090. efree(phar->fname);
  2091. efree(phar);
  2092. return NULL;
  2093. }
  2094. }
  2095. /* }}} */
  2096. /* {{{ proto object Phar::convertToExecutable([int format[, int compression [, string file_ext]]])
  2097. * Convert a phar.tar or phar.zip archive to the phar file format. The
  2098. * optional parameter allows the user to determine the new
  2099. * filename extension (default is phar).
  2100. */
  2101. PHP_METHOD(Phar, convertToExecutable)
  2102. {
  2103. char *ext = NULL;
  2104. int is_data, ext_len = 0;
  2105. php_uint32 flags;
  2106. zval *ret;
  2107. /* a number that is not 0, 1 or 2 (Which is also Greg's birthday, so there) */
  2108. long format = 9021976, method = 9021976;
  2109. PHAR_ARCHIVE_OBJECT();
  2110. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lls", &format, &method, &ext, &ext_len) == FAILURE) {
  2111. return;
  2112. }
  2113. if (PHAR_G(readonly)) {
  2114. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2115. "Cannot write out executable phar archive, phar is read-only");
  2116. return;
  2117. }
  2118. switch (format) {
  2119. case 9021976:
  2120. case PHAR_FORMAT_SAME: /* null is converted to 0 */
  2121. /* by default, use the existing format */
  2122. if (phar_obj->arc.archive->is_tar) {
  2123. format = PHAR_FORMAT_TAR;
  2124. } else if (phar_obj->arc.archive->is_zip) {
  2125. format = PHAR_FORMAT_ZIP;
  2126. } else {
  2127. format = PHAR_FORMAT_PHAR;
  2128. }
  2129. break;
  2130. case PHAR_FORMAT_PHAR:
  2131. case PHAR_FORMAT_TAR:
  2132. case PHAR_FORMAT_ZIP:
  2133. break;
  2134. default:
  2135. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2136. "Unknown file format specified, please pass one of Phar::PHAR, Phar::TAR or Phar::ZIP");
  2137. return;
  2138. }
  2139. switch (method) {
  2140. case 9021976:
  2141. flags = phar_obj->arc.archive->flags & PHAR_FILE_COMPRESSION_MASK;
  2142. break;
  2143. case 0:
  2144. flags = PHAR_FILE_COMPRESSED_NONE;
  2145. break;
  2146. case PHAR_ENT_COMPRESSED_GZ:
  2147. if (format == PHAR_FORMAT_ZIP) {
  2148. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2149. "Cannot compress entire archive with gzip, zip archives do not support whole-archive compression");
  2150. return;
  2151. }
  2152. if (!PHAR_G(has_zlib)) {
  2153. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2154. "Cannot compress entire archive with gzip, enable ext/zlib in php.ini");
  2155. return;
  2156. }
  2157. flags = PHAR_FILE_COMPRESSED_GZ;
  2158. break;
  2159. case PHAR_ENT_COMPRESSED_BZ2:
  2160. if (format == PHAR_FORMAT_ZIP) {
  2161. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2162. "Cannot compress entire archive with bz2, zip archives do not support whole-archive compression");
  2163. return;
  2164. }
  2165. if (!PHAR_G(has_bz2)) {
  2166. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2167. "Cannot compress entire archive with bz2, enable ext/bz2 in php.ini");
  2168. return;
  2169. }
  2170. flags = PHAR_FILE_COMPRESSED_BZ2;
  2171. break;
  2172. default:
  2173. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2174. "Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2");
  2175. return;
  2176. }
  2177. is_data = phar_obj->arc.archive->is_data;
  2178. phar_obj->arc.archive->is_data = 0;
  2179. ret = phar_convert_to_other(phar_obj->arc.archive, format, ext, flags TSRMLS_CC);
  2180. phar_obj->arc.archive->is_data = is_data;
  2181. if (ret) {
  2182. RETURN_ZVAL(ret, 1, 1);
  2183. } else {
  2184. RETURN_NULL();
  2185. }
  2186. }
  2187. /* }}} */
  2188. /* {{{ proto object Phar::convertToData([int format[, int compression [, string file_ext]]])
  2189. * Convert an archive to a non-executable .tar or .zip.
  2190. * The optional parameter allows the user to determine the new
  2191. * filename extension (default is .zip or .tar).
  2192. */
  2193. PHP_METHOD(Phar, convertToData)
  2194. {
  2195. char *ext = NULL;
  2196. int is_data, ext_len = 0;
  2197. php_uint32 flags;
  2198. zval *ret;
  2199. /* a number that is not 0, 1 or 2 (Which is also Greg's birthday so there) */
  2200. long format = 9021976, method = 9021976;
  2201. PHAR_ARCHIVE_OBJECT();
  2202. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lls", &format, &method, &ext, &ext_len) == FAILURE) {
  2203. return;
  2204. }
  2205. switch (format) {
  2206. case 9021976:
  2207. case PHAR_FORMAT_SAME: /* null is converted to 0 */
  2208. /* by default, use the existing format */
  2209. if (phar_obj->arc.archive->is_tar) {
  2210. format = PHAR_FORMAT_TAR;
  2211. } else if (phar_obj->arc.archive->is_zip) {
  2212. format = PHAR_FORMAT_ZIP;
  2213. } else {
  2214. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2215. "Cannot write out data phar archive, use Phar::TAR or Phar::ZIP");
  2216. return;
  2217. }
  2218. break;
  2219. case PHAR_FORMAT_PHAR:
  2220. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2221. "Cannot write out data phar archive, use Phar::TAR or Phar::ZIP");
  2222. return;
  2223. case PHAR_FORMAT_TAR:
  2224. case PHAR_FORMAT_ZIP:
  2225. break;
  2226. default:
  2227. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2228. "Unknown file format specified, please pass one of Phar::TAR or Phar::ZIP");
  2229. return;
  2230. }
  2231. switch (method) {
  2232. case 9021976:
  2233. flags = phar_obj->arc.archive->flags & PHAR_FILE_COMPRESSION_MASK;
  2234. break;
  2235. case 0:
  2236. flags = PHAR_FILE_COMPRESSED_NONE;
  2237. break;
  2238. case PHAR_ENT_COMPRESSED_GZ:
  2239. if (format == PHAR_FORMAT_ZIP) {
  2240. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2241. "Cannot compress entire archive with gzip, zip archives do not support whole-archive compression");
  2242. return;
  2243. }
  2244. if (!PHAR_G(has_zlib)) {
  2245. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2246. "Cannot compress entire archive with gzip, enable ext/zlib in php.ini");
  2247. return;
  2248. }
  2249. flags = PHAR_FILE_COMPRESSED_GZ;
  2250. break;
  2251. case PHAR_ENT_COMPRESSED_BZ2:
  2252. if (format == PHAR_FORMAT_ZIP) {
  2253. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2254. "Cannot compress entire archive with bz2, zip archives do not support whole-archive compression");
  2255. return;
  2256. }
  2257. if (!PHAR_G(has_bz2)) {
  2258. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2259. "Cannot compress entire archive with bz2, enable ext/bz2 in php.ini");
  2260. return;
  2261. }
  2262. flags = PHAR_FILE_COMPRESSED_BZ2;
  2263. break;
  2264. default:
  2265. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2266. "Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2");
  2267. return;
  2268. }
  2269. is_data = phar_obj->arc.archive->is_data;
  2270. phar_obj->arc.archive->is_data = 1;
  2271. ret = phar_convert_to_other(phar_obj->arc.archive, format, ext, flags TSRMLS_CC);
  2272. phar_obj->arc.archive->is_data = is_data;
  2273. if (ret) {
  2274. RETURN_ZVAL(ret, 1, 1);
  2275. } else {
  2276. RETURN_NULL();
  2277. }
  2278. }
  2279. /* }}} */
  2280. /* {{{ proto int|false Phar::isCompressed()
  2281. * Returns Phar::GZ or PHAR::BZ2 if the entire archive is compressed
  2282. * (.tar.gz/tar.bz2 and so on), or FALSE otherwise.
  2283. */
  2284. PHP_METHOD(Phar, isCompressed)
  2285. {
  2286. PHAR_ARCHIVE_OBJECT();
  2287. if (phar_obj->arc.archive->flags & PHAR_FILE_COMPRESSED_GZ) {
  2288. RETURN_LONG(PHAR_ENT_COMPRESSED_GZ);
  2289. }
  2290. if (phar_obj->arc.archive->flags & PHAR_FILE_COMPRESSED_BZ2) {
  2291. RETURN_LONG(PHAR_ENT_COMPRESSED_BZ2);
  2292. }
  2293. RETURN_FALSE;
  2294. }
  2295. /* }}} */
  2296. /* {{{ proto bool Phar::isWritable()
  2297. * Returns true if phar.readonly=0 or phar is a PharData AND the actual file is writable.
  2298. */
  2299. PHP_METHOD(Phar, isWritable)
  2300. {
  2301. php_stream_statbuf ssb;
  2302. PHAR_ARCHIVE_OBJECT();
  2303. if (!phar_obj->arc.archive->is_writeable) {
  2304. RETURN_FALSE;
  2305. }
  2306. if (SUCCESS != php_stream_stat_path(phar_obj->arc.archive->fname, &ssb)) {
  2307. if (phar_obj->arc.archive->is_brandnew) {
  2308. /* assume it works if the file doesn't exist yet */
  2309. RETURN_TRUE;
  2310. }
  2311. RETURN_FALSE;
  2312. }
  2313. RETURN_BOOL((ssb.sb.st_mode & (S_IWOTH | S_IWGRP | S_IWUSR)) != 0);
  2314. }
  2315. /* }}} */
  2316. /* {{{ proto bool Phar::delete(string entry)
  2317. * Deletes a named file within the archive.
  2318. */
  2319. PHP_METHOD(Phar, delete)
  2320. {
  2321. char *fname;
  2322. int fname_len;
  2323. char *error;
  2324. phar_entry_info *entry;
  2325. PHAR_ARCHIVE_OBJECT();
  2326. if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
  2327. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2328. "Cannot write out phar archive, phar is read-only");
  2329. return;
  2330. }
  2331. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &fname, &fname_len) == FAILURE) {
  2332. RETURN_FALSE;
  2333. }
  2334. if (phar_obj->arc.archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->arc.archive) TSRMLS_CC)) {
  2335. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar_obj->arc.archive->fname);
  2336. return;
  2337. }
  2338. if (zend_hash_exists(&phar_obj->arc.archive->manifest, fname, (uint) fname_len)) {
  2339. if (SUCCESS == zend_hash_find(&phar_obj->arc.archive->manifest, fname, (uint) fname_len, (void**)&entry)) {
  2340. if (entry->is_deleted) {
  2341. /* entry is deleted, but has not been flushed to disk yet */
  2342. RETURN_TRUE;
  2343. } else {
  2344. entry->is_deleted = 1;
  2345. entry->is_modified = 1;
  2346. phar_obj->arc.archive->is_modified = 1;
  2347. }
  2348. }
  2349. } else {
  2350. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Entry %s does not exist and cannot be deleted", fname);
  2351. RETURN_FALSE;
  2352. }
  2353. phar_flush(phar_obj->arc.archive, NULL, 0, 0, &error TSRMLS_CC);
  2354. if (error) {
  2355. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  2356. efree(error);
  2357. }
  2358. RETURN_TRUE;
  2359. }
  2360. /* }}} */
  2361. /* {{{ proto int Phar::getAlias()
  2362. * Returns the alias for the Phar or NULL.
  2363. */
  2364. PHP_METHOD(Phar, getAlias)
  2365. {
  2366. PHAR_ARCHIVE_OBJECT();
  2367. if (phar_obj->arc.archive->alias && phar_obj->arc.archive->alias != phar_obj->arc.archive->fname) {
  2368. RETURN_STRINGL(phar_obj->arc.archive->alias, phar_obj->arc.archive->alias_len, 1);
  2369. }
  2370. }
  2371. /* }}} */
  2372. /* {{{ proto int Phar::getPath()
  2373. * Returns the real path to the phar archive on disk
  2374. */
  2375. PHP_METHOD(Phar, getPath)
  2376. {
  2377. PHAR_ARCHIVE_OBJECT();
  2378. RETURN_STRINGL(phar_obj->arc.archive->fname, phar_obj->arc.archive->fname_len, 1);
  2379. }
  2380. /* }}} */
  2381. /* {{{ proto bool Phar::setAlias(string alias)
  2382. * Sets the alias for a Phar archive. The default value is the full path
  2383. * to the archive.
  2384. */
  2385. PHP_METHOD(Phar, setAlias)
  2386. {
  2387. char *alias, *error, *oldalias;
  2388. phar_archive_data **fd_ptr;
  2389. int alias_len, oldalias_len, old_temp, readd = 0;
  2390. PHAR_ARCHIVE_OBJECT();
  2391. if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
  2392. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2393. "Cannot write out phar archive, phar is read-only");
  2394. RETURN_FALSE;
  2395. }
  2396. /* invalidate phar cache */
  2397. PHAR_G(last_phar) = NULL;
  2398. PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
  2399. if (phar_obj->arc.archive->is_data) {
  2400. if (phar_obj->arc.archive->is_tar) {
  2401. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2402. "A Phar alias cannot be set in a plain tar archive");
  2403. } else {
  2404. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2405. "A Phar alias cannot be set in a plain zip archive");
  2406. }
  2407. RETURN_FALSE;
  2408. }
  2409. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &alias, &alias_len) == SUCCESS) {
  2410. if (alias_len == phar_obj->arc.archive->alias_len && memcmp(phar_obj->arc.archive->alias, alias, alias_len) == 0) {
  2411. RETURN_TRUE;
  2412. }
  2413. if (alias_len && SUCCESS == zend_hash_find(&(PHAR_GLOBALS->phar_alias_map), alias, alias_len, (void**)&fd_ptr)) {
  2414. spprintf(&error, 0, "alias \"%s\" is already used for archive \"%s\" and cannot be used for other archives", alias, (*fd_ptr)->fname);
  2415. if (SUCCESS == phar_free_alias(*fd_ptr, alias, alias_len TSRMLS_CC)) {
  2416. efree(error);
  2417. goto valid_alias;
  2418. }
  2419. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  2420. efree(error);
  2421. RETURN_FALSE;
  2422. }
  2423. if (!phar_validate_alias(alias, alias_len)) {
  2424. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2425. "Invalid alias \"%s\" specified for phar \"%s\"", alias, phar_obj->arc.archive->fname);
  2426. RETURN_FALSE;
  2427. }
  2428. valid_alias:
  2429. if (phar_obj->arc.archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->arc.archive) TSRMLS_CC)) {
  2430. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar_obj->arc.archive->fname);
  2431. return;
  2432. }
  2433. if (phar_obj->arc.archive->alias_len && SUCCESS == zend_hash_find(&(PHAR_GLOBALS->phar_alias_map), phar_obj->arc.archive->alias, phar_obj->arc.archive->alias_len, (void**)&fd_ptr)) {
  2434. zend_hash_del(&(PHAR_GLOBALS->phar_alias_map), phar_obj->arc.archive->alias, phar_obj->arc.archive->alias_len);
  2435. readd = 1;
  2436. }
  2437. oldalias = phar_obj->arc.archive->alias;
  2438. oldalias_len = phar_obj->arc.archive->alias_len;
  2439. old_temp = phar_obj->arc.archive->is_temporary_alias;
  2440. if (alias_len) {
  2441. phar_obj->arc.archive->alias = estrndup(alias, alias_len);
  2442. } else {
  2443. phar_obj->arc.archive->alias = NULL;
  2444. }
  2445. phar_obj->arc.archive->alias_len = alias_len;
  2446. phar_obj->arc.archive->is_temporary_alias = 0;
  2447. phar_flush(phar_obj->arc.archive, NULL, 0, 0, &error TSRMLS_CC);
  2448. if (error) {
  2449. phar_obj->arc.archive->alias = oldalias;
  2450. phar_obj->arc.archive->alias_len = oldalias_len;
  2451. phar_obj->arc.archive->is_temporary_alias = old_temp;
  2452. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  2453. if (readd) {
  2454. zend_hash_add(&(PHAR_GLOBALS->phar_alias_map), oldalias, oldalias_len, (void*)&(phar_obj->arc.archive), sizeof(phar_archive_data*), NULL);
  2455. }
  2456. efree(error);
  2457. RETURN_FALSE;
  2458. }
  2459. zend_hash_add(&(PHAR_GLOBALS->phar_alias_map), alias, alias_len, (void*)&(phar_obj->arc.archive), sizeof(phar_archive_data*), NULL);
  2460. if (oldalias) {
  2461. efree(oldalias);
  2462. }
  2463. RETURN_TRUE;
  2464. }
  2465. RETURN_FALSE;
  2466. }
  2467. /* }}} */
  2468. /* {{{ proto string Phar::getVersion()
  2469. * Return version info of Phar archive
  2470. */
  2471. PHP_METHOD(Phar, getVersion)
  2472. {
  2473. PHAR_ARCHIVE_OBJECT();
  2474. RETURN_STRING(phar_obj->arc.archive->version, 1);
  2475. }
  2476. /* }}} */
  2477. /* {{{ proto void Phar::startBuffering()
  2478. * Do not flush a writeable phar (save its contents) until explicitly requested
  2479. */
  2480. PHP_METHOD(Phar, startBuffering)
  2481. {
  2482. PHAR_ARCHIVE_OBJECT();
  2483. phar_obj->arc.archive->donotflush = 1;
  2484. }
  2485. /* }}} */
  2486. /* {{{ proto bool Phar::isBuffering()
  2487. * Returns whether write operations are flushing to disk immediately.
  2488. */
  2489. PHP_METHOD(Phar, isBuffering)
  2490. {
  2491. PHAR_ARCHIVE_OBJECT();
  2492. RETURN_BOOL(phar_obj->arc.archive->donotflush);
  2493. }
  2494. /* }}} */
  2495. /* {{{ proto bool Phar::stopBuffering()
  2496. * Saves the contents of a modified archive to disk.
  2497. */
  2498. PHP_METHOD(Phar, stopBuffering)
  2499. {
  2500. char *error;
  2501. PHAR_ARCHIVE_OBJECT();
  2502. if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
  2503. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2504. "Cannot write out phar archive, phar is read-only");
  2505. return;
  2506. }
  2507. phar_obj->arc.archive->donotflush = 0;
  2508. phar_flush(phar_obj->arc.archive, 0, 0, 0, &error TSRMLS_CC);
  2509. if (error) {
  2510. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  2511. efree(error);
  2512. }
  2513. }
  2514. /* }}} */
  2515. /* {{{ proto bool Phar::setStub(string|stream stub [, int len])
  2516. * Change the stub in a phar, phar.tar or phar.zip archive to something other
  2517. * than the default. The stub *must* end with a call to __HALT_COMPILER().
  2518. */
  2519. PHP_METHOD(Phar, setStub)
  2520. {
  2521. zval *zstub;
  2522. char *stub, *error;
  2523. int stub_len;
  2524. long len = -1;
  2525. php_stream *stream;
  2526. PHAR_ARCHIVE_OBJECT();
  2527. if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
  2528. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2529. "Cannot change stub, phar is read-only");
  2530. return;
  2531. }
  2532. if (phar_obj->arc.archive->is_data) {
  2533. if (phar_obj->arc.archive->is_tar) {
  2534. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2535. "A Phar stub cannot be set in a plain tar archive");
  2536. } else {
  2537. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2538. "A Phar stub cannot be set in a plain zip archive");
  2539. }
  2540. return;
  2541. }
  2542. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &zstub, &len) == SUCCESS) {
  2543. if ((php_stream_from_zval_no_verify(stream, &zstub)) != NULL) {
  2544. if (len > 0) {
  2545. len = -len;
  2546. } else {
  2547. len = -1;
  2548. }
  2549. if (phar_obj->arc.archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->arc.archive) TSRMLS_CC)) {
  2550. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar_obj->arc.archive->fname);
  2551. return;
  2552. }
  2553. phar_flush(phar_obj->arc.archive, (char *) &zstub, len, 0, &error TSRMLS_CC);
  2554. if (error) {
  2555. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  2556. efree(error);
  2557. }
  2558. RETURN_TRUE;
  2559. } else {
  2560. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2561. "Cannot change stub, unable to read from input stream");
  2562. }
  2563. } else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &stub, &stub_len) == SUCCESS) {
  2564. if (phar_obj->arc.archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->arc.archive) TSRMLS_CC)) {
  2565. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar_obj->arc.archive->fname);
  2566. return;
  2567. }
  2568. phar_flush(phar_obj->arc.archive, stub, stub_len, 0, &error TSRMLS_CC);
  2569. if (error) {
  2570. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  2571. efree(error);
  2572. }
  2573. RETURN_TRUE;
  2574. }
  2575. RETURN_FALSE;
  2576. }
  2577. /* }}} */
  2578. /* {{{ proto bool Phar::setDefaultStub([string index[, string webindex]])
  2579. * In a pure phar archive, sets a stub that can be used to run the archive
  2580. * regardless of whether the phar extension is available. The first parameter
  2581. * is the CLI startup filename, which defaults to "index.php". The second
  2582. * parameter is the web startup filename and also defaults to "index.php"
  2583. * (falling back to CLI behaviour).
  2584. * Both parameters are optional.
  2585. * In a phar.zip or phar.tar archive, the default stub is used only to
  2586. * identify the archive to the extension as a Phar object. This allows the
  2587. * extension to treat phar.zip and phar.tar types as honorary phars. Since
  2588. * files cannot be loaded via this kind of stub, no parameters are accepted
  2589. * when the Phar object is zip- or tar-based.
  2590. */
  2591. PHP_METHOD(Phar, setDefaultStub)
  2592. {
  2593. char *index = NULL, *webindex = NULL, *error = NULL, *stub = NULL;
  2594. int index_len = 0, webindex_len = 0, created_stub = 0;
  2595. size_t stub_len = 0;
  2596. PHAR_ARCHIVE_OBJECT();
  2597. if (phar_obj->arc.archive->is_data) {
  2598. if (phar_obj->arc.archive->is_tar) {
  2599. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2600. "A Phar stub cannot be set in a plain tar archive");
  2601. } else {
  2602. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2603. "A Phar stub cannot be set in a plain zip archive");
  2604. }
  2605. return;
  2606. }
  2607. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s", &index, &index_len, &webindex, &webindex_len) == FAILURE) {
  2608. RETURN_FALSE;
  2609. }
  2610. if (ZEND_NUM_ARGS() > 0 && (phar_obj->arc.archive->is_tar || phar_obj->arc.archive->is_zip)) {
  2611. php_error_docref(NULL TSRMLS_CC, E_WARNING, "method accepts no arguments for a tar- or zip-based phar stub, %d given", ZEND_NUM_ARGS());
  2612. RETURN_FALSE;
  2613. }
  2614. if (PHAR_G(readonly)) {
  2615. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2616. "Cannot change stub: phar.readonly=1");
  2617. RETURN_FALSE;
  2618. }
  2619. if (!phar_obj->arc.archive->is_tar && !phar_obj->arc.archive->is_zip) {
  2620. stub = phar_create_default_stub(index, webindex, &stub_len, &error TSRMLS_CC);
  2621. if (error) {
  2622. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "%s", error);
  2623. efree(error);
  2624. if (stub) {
  2625. efree(stub);
  2626. }
  2627. RETURN_FALSE;
  2628. }
  2629. created_stub = 1;
  2630. }
  2631. if (phar_obj->arc.archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->arc.archive) TSRMLS_CC)) {
  2632. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar_obj->arc.archive->fname);
  2633. return;
  2634. }
  2635. phar_flush(phar_obj->arc.archive, stub, stub_len, 1, &error TSRMLS_CC);
  2636. if (created_stub) {
  2637. efree(stub);
  2638. }
  2639. if (error) {
  2640. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  2641. efree(error);
  2642. RETURN_FALSE;
  2643. }
  2644. RETURN_TRUE;
  2645. }
  2646. /* }}} */
  2647. /* {{{ proto array Phar::setSignatureAlgorithm(int sigtype[, string privatekey])
  2648. * Sets the signature algorithm for a phar and applies it. The signature
  2649. * algorithm must be one of Phar::MD5, Phar::SHA1, Phar::SHA256,
  2650. * Phar::SHA512, or Phar::OPENSSL. Note that zip- based phar archives
  2651. * cannot support signatures.
  2652. */
  2653. PHP_METHOD(Phar, setSignatureAlgorithm)
  2654. {
  2655. long algo;
  2656. char *error, *key = NULL;
  2657. int key_len = 0;
  2658. PHAR_ARCHIVE_OBJECT();
  2659. if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
  2660. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2661. "Cannot set signature algorithm, phar is read-only");
  2662. return;
  2663. }
  2664. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "l|s", &algo, &key, &key_len) != SUCCESS) {
  2665. return;
  2666. }
  2667. switch (algo) {
  2668. case PHAR_SIG_SHA256:
  2669. case PHAR_SIG_SHA512:
  2670. #ifndef PHAR_HASH_OK
  2671. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2672. "SHA-256 and SHA-512 signatures are only supported if the hash extension is enabled and built non-shared");
  2673. return;
  2674. #endif
  2675. case PHAR_SIG_MD5:
  2676. case PHAR_SIG_SHA1:
  2677. case PHAR_SIG_OPENSSL:
  2678. if (phar_obj->arc.archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->arc.archive) TSRMLS_CC)) {
  2679. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar_obj->arc.archive->fname);
  2680. return;
  2681. }
  2682. phar_obj->arc.archive->sig_flags = algo;
  2683. phar_obj->arc.archive->is_modified = 1;
  2684. PHAR_G(openssl_privatekey) = key;
  2685. PHAR_G(openssl_privatekey_len) = key_len;
  2686. phar_flush(phar_obj->arc.archive, 0, 0, 0, &error TSRMLS_CC);
  2687. if (error) {
  2688. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  2689. efree(error);
  2690. }
  2691. break;
  2692. default:
  2693. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2694. "Unknown signature algorithm specified");
  2695. }
  2696. }
  2697. /* }}} */
  2698. /* {{{ proto array|false Phar::getSignature()
  2699. * Returns a hash signature, or FALSE if the archive is unsigned.
  2700. */
  2701. PHP_METHOD(Phar, getSignature)
  2702. {
  2703. PHAR_ARCHIVE_OBJECT();
  2704. if (phar_obj->arc.archive->signature) {
  2705. char *unknown;
  2706. int unknown_len;
  2707. array_init(return_value);
  2708. add_assoc_stringl(return_value, "hash", phar_obj->arc.archive->signature, phar_obj->arc.archive->sig_len, 1);
  2709. switch(phar_obj->arc.archive->sig_flags) {
  2710. case PHAR_SIG_MD5:
  2711. add_assoc_stringl(return_value, "hash_type", "MD5", 3, 1);
  2712. break;
  2713. case PHAR_SIG_SHA1:
  2714. add_assoc_stringl(return_value, "hash_type", "SHA-1", 5, 1);
  2715. break;
  2716. case PHAR_SIG_SHA256:
  2717. add_assoc_stringl(return_value, "hash_type", "SHA-256", 7, 1);
  2718. break;
  2719. case PHAR_SIG_SHA512:
  2720. add_assoc_stringl(return_value, "hash_type", "SHA-512", 7, 1);
  2721. break;
  2722. case PHAR_SIG_OPENSSL:
  2723. add_assoc_stringl(return_value, "hash_type", "OpenSSL", 7, 1);
  2724. break;
  2725. default:
  2726. unknown_len = spprintf(&unknown, 0, "Unknown (%u)", phar_obj->arc.archive->sig_flags);
  2727. add_assoc_stringl(return_value, "hash_type", unknown, unknown_len, 0);
  2728. break;
  2729. }
  2730. } else {
  2731. RETURN_FALSE;
  2732. }
  2733. }
  2734. /* }}} */
  2735. /* {{{ proto bool Phar::getModified()
  2736. * Return whether phar was modified
  2737. */
  2738. PHP_METHOD(Phar, getModified)
  2739. {
  2740. PHAR_ARCHIVE_OBJECT();
  2741. RETURN_BOOL(phar_obj->arc.archive->is_modified);
  2742. }
  2743. /* }}} */
  2744. static int phar_set_compression(void *pDest, void *argument TSRMLS_DC) /* {{{ */
  2745. {
  2746. phar_entry_info *entry = (phar_entry_info *)pDest;
  2747. php_uint32 compress = *(php_uint32 *)argument;
  2748. if (entry->is_deleted) {
  2749. return ZEND_HASH_APPLY_KEEP;
  2750. }
  2751. entry->old_flags = entry->flags;
  2752. entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
  2753. entry->flags |= compress;
  2754. entry->is_modified = 1;
  2755. return ZEND_HASH_APPLY_KEEP;
  2756. }
  2757. /* }}} */
  2758. static int phar_test_compression(void *pDest, void *argument TSRMLS_DC) /* {{{ */
  2759. {
  2760. phar_entry_info *entry = (phar_entry_info *)pDest;
  2761. if (entry->is_deleted) {
  2762. return ZEND_HASH_APPLY_KEEP;
  2763. }
  2764. if (!PHAR_G(has_bz2)) {
  2765. if (entry->flags & PHAR_ENT_COMPRESSED_BZ2) {
  2766. *(int *) argument = 0;
  2767. }
  2768. }
  2769. if (!PHAR_G(has_zlib)) {
  2770. if (entry->flags & PHAR_ENT_COMPRESSED_GZ) {
  2771. *(int *) argument = 0;
  2772. }
  2773. }
  2774. return ZEND_HASH_APPLY_KEEP;
  2775. }
  2776. /* }}} */
  2777. static void pharobj_set_compression(HashTable *manifest, php_uint32 compress TSRMLS_DC) /* {{{ */
  2778. {
  2779. zend_hash_apply_with_argument(manifest, phar_set_compression, &compress TSRMLS_CC);
  2780. }
  2781. /* }}} */
  2782. static int pharobj_cancompress(HashTable *manifest TSRMLS_DC) /* {{{ */
  2783. {
  2784. int test;
  2785. test = 1;
  2786. zend_hash_apply_with_argument(manifest, phar_test_compression, &test TSRMLS_CC);
  2787. return test;
  2788. }
  2789. /* }}} */
  2790. /* {{{ proto object Phar::compress(int method[, string extension])
  2791. * Compress a .tar, or .phar.tar with whole-file compression
  2792. * The parameter can be one of Phar::GZ or Phar::BZ2 to specify
  2793. * the kind of compression desired
  2794. */
  2795. PHP_METHOD(Phar, compress)
  2796. {
  2797. long method;
  2798. char *ext = NULL;
  2799. int ext_len = 0;
  2800. php_uint32 flags;
  2801. zval *ret;
  2802. PHAR_ARCHIVE_OBJECT();
  2803. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|s", &method, &ext, &ext_len) == FAILURE) {
  2804. return;
  2805. }
  2806. if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
  2807. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2808. "Cannot compress phar archive, phar is read-only");
  2809. return;
  2810. }
  2811. if (phar_obj->arc.archive->is_zip) {
  2812. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2813. "Cannot compress zip-based archives with whole-archive compression");
  2814. return;
  2815. }
  2816. switch (method) {
  2817. case 0:
  2818. flags = PHAR_FILE_COMPRESSED_NONE;
  2819. break;
  2820. case PHAR_ENT_COMPRESSED_GZ:
  2821. if (!PHAR_G(has_zlib)) {
  2822. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2823. "Cannot compress entire archive with gzip, enable ext/zlib in php.ini");
  2824. return;
  2825. }
  2826. flags = PHAR_FILE_COMPRESSED_GZ;
  2827. break;
  2828. case PHAR_ENT_COMPRESSED_BZ2:
  2829. if (!PHAR_G(has_bz2)) {
  2830. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2831. "Cannot compress entire archive with bz2, enable ext/bz2 in php.ini");
  2832. return;
  2833. }
  2834. flags = PHAR_FILE_COMPRESSED_BZ2;
  2835. break;
  2836. default:
  2837. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2838. "Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2");
  2839. return;
  2840. }
  2841. if (phar_obj->arc.archive->is_tar) {
  2842. ret = phar_convert_to_other(phar_obj->arc.archive, PHAR_FORMAT_TAR, ext, flags TSRMLS_CC);
  2843. } else {
  2844. ret = phar_convert_to_other(phar_obj->arc.archive, PHAR_FORMAT_PHAR, ext, flags TSRMLS_CC);
  2845. }
  2846. if (ret) {
  2847. RETURN_ZVAL(ret, 1, 1);
  2848. } else {
  2849. RETURN_NULL();
  2850. }
  2851. }
  2852. /* }}} */
  2853. /* {{{ proto object Phar::decompress([string extension])
  2854. * Decompress a .tar, or .phar.tar with whole-file compression
  2855. */
  2856. PHP_METHOD(Phar, decompress)
  2857. {
  2858. char *ext = NULL;
  2859. int ext_len = 0;
  2860. zval *ret;
  2861. PHAR_ARCHIVE_OBJECT();
  2862. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &ext, &ext_len) == FAILURE) {
  2863. return;
  2864. }
  2865. if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
  2866. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2867. "Cannot decompress phar archive, phar is read-only");
  2868. return;
  2869. }
  2870. if (phar_obj->arc.archive->is_zip) {
  2871. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  2872. "Cannot decompress zip-based archives with whole-archive compression");
  2873. return;
  2874. }
  2875. if (phar_obj->arc.archive->is_tar) {
  2876. ret = phar_convert_to_other(phar_obj->arc.archive, PHAR_FORMAT_TAR, ext, PHAR_FILE_COMPRESSED_NONE TSRMLS_CC);
  2877. } else {
  2878. ret = phar_convert_to_other(phar_obj->arc.archive, PHAR_FORMAT_PHAR, ext, PHAR_FILE_COMPRESSED_NONE TSRMLS_CC);
  2879. }
  2880. if (ret) {
  2881. RETURN_ZVAL(ret, 1, 1);
  2882. } else {
  2883. RETURN_NULL();
  2884. }
  2885. }
  2886. /* }}} */
  2887. /* {{{ proto object Phar::compressFiles(int method)
  2888. * Compress all files within a phar or zip archive using the specified compression
  2889. * The parameter can be one of Phar::GZ or Phar::BZ2 to specify
  2890. * the kind of compression desired
  2891. */
  2892. PHP_METHOD(Phar, compressFiles)
  2893. {
  2894. char *error;
  2895. php_uint32 flags;
  2896. long method;
  2897. PHAR_ARCHIVE_OBJECT();
  2898. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &method) == FAILURE) {
  2899. return;
  2900. }
  2901. if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
  2902. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2903. "Phar is readonly, cannot change compression");
  2904. return;
  2905. }
  2906. switch (method) {
  2907. case PHAR_ENT_COMPRESSED_GZ:
  2908. if (!PHAR_G(has_zlib)) {
  2909. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2910. "Cannot compress files within archive with gzip, enable ext/zlib in php.ini");
  2911. return;
  2912. }
  2913. flags = PHAR_ENT_COMPRESSED_GZ;
  2914. break;
  2915. case PHAR_ENT_COMPRESSED_BZ2:
  2916. if (!PHAR_G(has_bz2)) {
  2917. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2918. "Cannot compress files within archive with bz2, enable ext/bz2 in php.ini");
  2919. return;
  2920. }
  2921. flags = PHAR_ENT_COMPRESSED_BZ2;
  2922. break;
  2923. default:
  2924. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2925. "Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2");
  2926. return;
  2927. }
  2928. if (phar_obj->arc.archive->is_tar) {
  2929. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2930. "Cannot compress with Gzip compression, tar archives cannot compress individual files, use compress() to compress the whole archive");
  2931. return;
  2932. }
  2933. if (!pharobj_cancompress(&phar_obj->arc.archive->manifest TSRMLS_CC)) {
  2934. if (flags == PHAR_FILE_COMPRESSED_GZ) {
  2935. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2936. "Cannot compress all files as Gzip, some are compressed as bzip2 and cannot be decompressed");
  2937. } else {
  2938. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2939. "Cannot compress all files as Bzip2, some are compressed as gzip and cannot be decompressed");
  2940. }
  2941. return;
  2942. }
  2943. if (phar_obj->arc.archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->arc.archive) TSRMLS_CC)) {
  2944. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar_obj->arc.archive->fname);
  2945. return;
  2946. }
  2947. pharobj_set_compression(&phar_obj->arc.archive->manifest, flags TSRMLS_CC);
  2948. phar_obj->arc.archive->is_modified = 1;
  2949. phar_flush(phar_obj->arc.archive, 0, 0, 0, &error TSRMLS_CC);
  2950. if (error) {
  2951. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "%s", error);
  2952. efree(error);
  2953. }
  2954. }
  2955. /* }}} */
  2956. /* {{{ proto bool Phar::decompressFiles()
  2957. * decompress every file
  2958. */
  2959. PHP_METHOD(Phar, decompressFiles)
  2960. {
  2961. char *error;
  2962. PHAR_ARCHIVE_OBJECT();
  2963. if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
  2964. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2965. "Phar is readonly, cannot change compression");
  2966. return;
  2967. }
  2968. if (!pharobj_cancompress(&phar_obj->arc.archive->manifest TSRMLS_CC)) {
  2969. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  2970. "Cannot decompress all files, some are compressed as bzip2 or gzip and cannot be decompressed");
  2971. return;
  2972. }
  2973. if (phar_obj->arc.archive->is_tar) {
  2974. RETURN_TRUE;
  2975. } else {
  2976. if (phar_obj->arc.archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->arc.archive) TSRMLS_CC)) {
  2977. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar_obj->arc.archive->fname);
  2978. return;
  2979. }
  2980. pharobj_set_compression(&phar_obj->arc.archive->manifest, PHAR_ENT_COMPRESSED_NONE TSRMLS_CC);
  2981. }
  2982. phar_obj->arc.archive->is_modified = 1;
  2983. phar_flush(phar_obj->arc.archive, 0, 0, 0, &error TSRMLS_CC);
  2984. if (error) {
  2985. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "%s", error);
  2986. efree(error);
  2987. }
  2988. RETURN_TRUE;
  2989. }
  2990. /* }}} */
  2991. /* {{{ proto bool Phar::copy(string oldfile, string newfile)
  2992. * copy a file internal to the phar archive to another new file within the phar
  2993. */
  2994. PHP_METHOD(Phar, copy)
  2995. {
  2996. char *oldfile, *newfile, *error;
  2997. const char *pcr_error;
  2998. int oldfile_len, newfile_len;
  2999. phar_entry_info *oldentry, newentry = {0}, *temp;
  3000. PHAR_ARCHIVE_OBJECT();
  3001. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &oldfile, &oldfile_len, &newfile, &newfile_len) == FAILURE) {
  3002. return;
  3003. }
  3004. if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
  3005. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  3006. "Cannot copy \"%s\" to \"%s\", phar is read-only", oldfile, newfile);
  3007. RETURN_FALSE;
  3008. }
  3009. if (oldfile_len >= sizeof(".phar")-1 && !memcmp(oldfile, ".phar", sizeof(".phar")-1)) {
  3010. /* can't copy a meta file */
  3011. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  3012. "file \"%s\" cannot be copied to file \"%s\", cannot copy Phar meta-file in %s", oldfile, newfile, phar_obj->arc.archive->fname);
  3013. RETURN_FALSE;
  3014. }
  3015. if (newfile_len >= sizeof(".phar")-1 && !memcmp(newfile, ".phar", sizeof(".phar")-1)) {
  3016. /* can't copy a meta file */
  3017. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  3018. "file \"%s\" cannot be copied to file \"%s\", cannot copy to Phar meta-file in %s", oldfile, newfile, phar_obj->arc.archive->fname);
  3019. RETURN_FALSE;
  3020. }
  3021. if (!zend_hash_exists(&phar_obj->arc.archive->manifest, oldfile, (uint) oldfile_len) || SUCCESS != zend_hash_find(&phar_obj->arc.archive->manifest, oldfile, (uint) oldfile_len, (void**)&oldentry) || oldentry->is_deleted) {
  3022. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  3023. "file \"%s\" cannot be copied to file \"%s\", file does not exist in %s", oldfile, newfile, phar_obj->arc.archive->fname);
  3024. RETURN_FALSE;
  3025. }
  3026. if (zend_hash_exists(&phar_obj->arc.archive->manifest, newfile, (uint) newfile_len)) {
  3027. if (SUCCESS == zend_hash_find(&phar_obj->arc.archive->manifest, newfile, (uint) newfile_len, (void**)&temp) || !temp->is_deleted) {
  3028. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  3029. "file \"%s\" cannot be copied to file \"%s\", file must not already exist in phar %s", oldfile, newfile, phar_obj->arc.archive->fname);
  3030. RETURN_FALSE;
  3031. }
  3032. }
  3033. if (phar_path_check(&newfile, &newfile_len, &pcr_error) > pcr_is_ok) {
  3034. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
  3035. "file \"%s\" contains invalid characters %s, cannot be copied from \"%s\" in phar %s", newfile, pcr_error, oldfile, phar_obj->arc.archive->fname);
  3036. RETURN_FALSE;
  3037. }
  3038. if (phar_obj->arc.archive->is_persistent) {
  3039. if (FAILURE == phar_copy_on_write(&(phar_obj->arc.archive) TSRMLS_CC)) {
  3040. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar_obj->arc.archive->fname);
  3041. return;
  3042. }
  3043. /* re-populate with copied-on-write entry */
  3044. zend_hash_find(&phar_obj->arc.archive->manifest, oldfile, (uint) oldfile_len, (void**)&oldentry);
  3045. }
  3046. memcpy((void *) &newentry, oldentry, sizeof(phar_entry_info));
  3047. if (newentry.metadata) {
  3048. zval *t;
  3049. t = newentry.metadata;
  3050. ALLOC_ZVAL(newentry.metadata);
  3051. *newentry.metadata = *t;
  3052. zval_copy_ctor(newentry.metadata);
  3053. #if PHP_VERSION_ID < 50300
  3054. newentry.metadata->refcount = 1;
  3055. #else
  3056. Z_SET_REFCOUNT_P(newentry.metadata, 1);
  3057. #endif
  3058. newentry.metadata_str.c = NULL;
  3059. newentry.metadata_str.len = 0;
  3060. }
  3061. newentry.filename = estrndup(newfile, newfile_len);
  3062. newentry.filename_len = newfile_len;
  3063. newentry.fp_refcount = 0;
  3064. if (oldentry->fp_type != PHAR_FP) {
  3065. if (FAILURE == phar_copy_entry_fp(oldentry, &newentry, &error TSRMLS_CC)) {
  3066. efree(newentry.filename);
  3067. php_stream_close(newentry.fp);
  3068. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  3069. efree(error);
  3070. return;
  3071. }
  3072. }
  3073. zend_hash_add(&oldentry->phar->manifest, newfile, newfile_len, (void*)&newentry, sizeof(phar_entry_info), NULL);
  3074. phar_obj->arc.archive->is_modified = 1;
  3075. phar_flush(phar_obj->arc.archive, 0, 0, 0, &error TSRMLS_CC);
  3076. if (error) {
  3077. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  3078. efree(error);
  3079. }
  3080. RETURN_TRUE;
  3081. }
  3082. /* }}} */
  3083. /* {{{ proto int Phar::offsetExists(string entry)
  3084. * determines whether a file exists in the phar
  3085. */
  3086. PHP_METHOD(Phar, offsetExists)
  3087. {
  3088. char *fname;
  3089. int fname_len;
  3090. phar_entry_info *entry;
  3091. PHAR_ARCHIVE_OBJECT();
  3092. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &fname, &fname_len) == FAILURE) {
  3093. return;
  3094. }
  3095. if (zend_hash_exists(&phar_obj->arc.archive->manifest, fname, (uint) fname_len)) {
  3096. if (SUCCESS == zend_hash_find(&phar_obj->arc.archive->manifest, fname, (uint) fname_len, (void**)&entry)) {
  3097. if (entry->is_deleted) {
  3098. /* entry is deleted, but has not been flushed to disk yet */
  3099. RETURN_FALSE;
  3100. }
  3101. }
  3102. if (fname_len >= sizeof(".phar")-1 && !memcmp(fname, ".phar", sizeof(".phar")-1)) {
  3103. /* none of these are real files, so they don't exist */
  3104. RETURN_FALSE;
  3105. }
  3106. RETURN_TRUE;
  3107. } else {
  3108. if (zend_hash_exists(&phar_obj->arc.archive->virtual_dirs, fname, (uint) fname_len)) {
  3109. RETURN_TRUE;
  3110. }
  3111. RETURN_FALSE;
  3112. }
  3113. }
  3114. /* }}} */
  3115. /* {{{ proto int Phar::offsetGet(string entry)
  3116. * get a PharFileInfo object for a specific file
  3117. */
  3118. PHP_METHOD(Phar, offsetGet)
  3119. {
  3120. char *fname, *error;
  3121. int fname_len;
  3122. zval *zfname;
  3123. phar_entry_info *entry;
  3124. PHAR_ARCHIVE_OBJECT();
  3125. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &fname, &fname_len) == FAILURE) {
  3126. return;
  3127. }
  3128. /* security is 0 here so that we can get a better error message than "entry doesn't exist" */
  3129. if (!(entry = phar_get_entry_info_dir(phar_obj->arc.archive, fname, fname_len, 1, &error, 0 TSRMLS_CC))) {
  3130. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Entry %s does not exist%s%s", fname, error?", ":"", error?error:"");
  3131. } else {
  3132. if (fname_len == sizeof(".phar/stub.php")-1 && !memcmp(fname, ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
  3133. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Cannot get stub \".phar/stub.php\" directly in phar \"%s\", use getStub", phar_obj->arc.archive->fname);
  3134. return;
  3135. }
  3136. if (fname_len == sizeof(".phar/alias.txt")-1 && !memcmp(fname, ".phar/alias.txt", sizeof(".phar/alias.txt")-1)) {
  3137. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Cannot get alias \".phar/alias.txt\" directly in phar \"%s\", use getAlias", phar_obj->arc.archive->fname);
  3138. return;
  3139. }
  3140. if (fname_len >= sizeof(".phar")-1 && !memcmp(fname, ".phar", sizeof(".phar")-1)) {
  3141. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Cannot directly get any files or directories in magic \".phar\" directory", phar_obj->arc.archive->fname);
  3142. return;
  3143. }
  3144. if (entry->is_temp_dir) {
  3145. efree(entry->filename);
  3146. efree(entry);
  3147. }
  3148. fname_len = spprintf(&fname, 0, "phar://%s/%s", phar_obj->arc.archive->fname, fname);
  3149. MAKE_STD_ZVAL(zfname);
  3150. ZVAL_STRINGL(zfname, fname, fname_len, 0);
  3151. spl_instantiate_arg_ex1(phar_obj->spl.info_class, &return_value, 0, zfname TSRMLS_CC);
  3152. zval_ptr_dtor(&zfname);
  3153. }
  3154. }
  3155. /* }}} */
  3156. /* {{{ add a file within the phar archive from a string or resource
  3157. */
  3158. static void phar_add_file(phar_archive_data **pphar, char *filename, int filename_len, char *cont_str, int cont_len, zval *zresource TSRMLS_DC)
  3159. {
  3160. char *error;
  3161. size_t contents_len;
  3162. phar_entry_data *data;
  3163. php_stream *contents_file;
  3164. if (filename_len >= sizeof(".phar")-1 && !memcmp(filename, ".phar", sizeof(".phar")-1)) {
  3165. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Cannot create any files in magic \".phar\" directory", (*pphar)->fname);
  3166. return;
  3167. }
  3168. if (!(data = phar_get_or_create_entry_data((*pphar)->fname, (*pphar)->fname_len, filename, filename_len, "w+b", 0, &error, 1 TSRMLS_CC))) {
  3169. if (error) {
  3170. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Entry %s does not exist and cannot be created: %s", filename, error);
  3171. efree(error);
  3172. } else {
  3173. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Entry %s does not exist and cannot be created", filename);
  3174. }
  3175. return;
  3176. } else {
  3177. if (error) {
  3178. efree(error);
  3179. }
  3180. if (!data->internal_file->is_dir) {
  3181. if (cont_str) {
  3182. contents_len = php_stream_write(data->fp, cont_str, cont_len);
  3183. if (contents_len != cont_len) {
  3184. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Entry %s could not be written to", filename);
  3185. return;
  3186. }
  3187. } else {
  3188. if (!(php_stream_from_zval_no_verify(contents_file, &zresource))) {
  3189. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Entry %s could not be written to", filename);
  3190. return;
  3191. }
  3192. phar_stream_copy_to_stream(contents_file, data->fp, PHP_STREAM_COPY_ALL, &contents_len);
  3193. }
  3194. data->internal_file->compressed_filesize = data->internal_file->uncompressed_filesize = contents_len;
  3195. }
  3196. /* check for copy-on-write */
  3197. if (pphar[0] != data->phar) {
  3198. *pphar = data->phar;
  3199. }
  3200. phar_entry_delref(data TSRMLS_CC);
  3201. phar_flush(*pphar, 0, 0, 0, &error TSRMLS_CC);
  3202. if (error) {
  3203. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  3204. efree(error);
  3205. }
  3206. }
  3207. }
  3208. /* }}} */
  3209. /* {{{ create a directory within the phar archive
  3210. */
  3211. static void phar_mkdir(phar_archive_data **pphar, char *dirname, int dirname_len TSRMLS_DC)
  3212. {
  3213. char *error;
  3214. phar_entry_data *data;
  3215. if (!(data = phar_get_or_create_entry_data((*pphar)->fname, (*pphar)->fname_len, dirname, dirname_len, "w+b", 2, &error, 1 TSRMLS_CC))) {
  3216. if (error) {
  3217. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Directory %s does not exist and cannot be created: %s", dirname, error);
  3218. efree(error);
  3219. } else {
  3220. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Directory %s does not exist and cannot be created", dirname);
  3221. }
  3222. return;
  3223. } else {
  3224. if (error) {
  3225. efree(error);
  3226. }
  3227. /* check for copy on write */
  3228. if (data->phar != *pphar) {
  3229. *pphar = data->phar;
  3230. }
  3231. phar_entry_delref(data TSRMLS_CC);
  3232. phar_flush(*pphar, 0, 0, 0, &error TSRMLS_CC);
  3233. if (error) {
  3234. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  3235. efree(error);
  3236. }
  3237. }
  3238. }
  3239. /* }}} */
  3240. /* {{{ proto int Phar::offsetSet(string entry, string value)
  3241. * set the contents of an internal file to those of an external file
  3242. */
  3243. PHP_METHOD(Phar, offsetSet)
  3244. {
  3245. char *fname, *cont_str = NULL;
  3246. int fname_len, cont_len;
  3247. zval *zresource;
  3248. PHAR_ARCHIVE_OBJECT();
  3249. if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
  3250. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Write operations disabled by the php.ini setting phar.readonly");
  3251. return;
  3252. }
  3253. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "sr", &fname, &fname_len, &zresource) == FAILURE
  3254. && zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &fname, &fname_len, &cont_str, &cont_len) == FAILURE) {
  3255. return;
  3256. }
  3257. if (fname_len == sizeof(".phar/stub.php")-1 && !memcmp(fname, ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
  3258. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Cannot set stub \".phar/stub.php\" directly in phar \"%s\", use setStub", phar_obj->arc.archive->fname);
  3259. return;
  3260. }
  3261. if (fname_len == sizeof(".phar/alias.txt")-1 && !memcmp(fname, ".phar/alias.txt", sizeof(".phar/alias.txt")-1)) {
  3262. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Cannot set alias \".phar/alias.txt\" directly in phar \"%s\", use setAlias", phar_obj->arc.archive->fname);
  3263. return;
  3264. }
  3265. if (fname_len >= sizeof(".phar")-1 && !memcmp(fname, ".phar", sizeof(".phar")-1)) {
  3266. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Cannot set any files or directories in magic \".phar\" directory", phar_obj->arc.archive->fname);
  3267. return;
  3268. }
  3269. phar_add_file(&(phar_obj->arc.archive), fname, fname_len, cont_str, cont_len, zresource TSRMLS_CC);
  3270. }
  3271. /* }}} */
  3272. /* {{{ proto int Phar::offsetUnset(string entry)
  3273. * remove a file from a phar
  3274. */
  3275. PHP_METHOD(Phar, offsetUnset)
  3276. {
  3277. char *fname, *error;
  3278. int fname_len;
  3279. phar_entry_info *entry;
  3280. PHAR_ARCHIVE_OBJECT();
  3281. if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
  3282. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Write operations disabled by the php.ini setting phar.readonly");
  3283. return;
  3284. }
  3285. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &fname, &fname_len) == FAILURE) {
  3286. return;
  3287. }
  3288. if (zend_hash_exists(&phar_obj->arc.archive->manifest, fname, (uint) fname_len)) {
  3289. if (SUCCESS == zend_hash_find(&phar_obj->arc.archive->manifest, fname, (uint) fname_len, (void**)&entry)) {
  3290. if (entry->is_deleted) {
  3291. /* entry is deleted, but has not been flushed to disk yet */
  3292. return;
  3293. }
  3294. if (phar_obj->arc.archive->is_persistent) {
  3295. if (FAILURE == phar_copy_on_write(&(phar_obj->arc.archive) TSRMLS_CC)) {
  3296. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar_obj->arc.archive->fname);
  3297. return;
  3298. }
  3299. /* re-populate entry after copy on write */
  3300. zend_hash_find(&phar_obj->arc.archive->manifest, fname, (uint) fname_len, (void **)&entry);
  3301. }
  3302. entry->is_modified = 0;
  3303. entry->is_deleted = 1;
  3304. /* we need to "flush" the stream to save the newly deleted file on disk */
  3305. phar_flush(phar_obj->arc.archive, 0, 0, 0, &error TSRMLS_CC);
  3306. if (error) {
  3307. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  3308. efree(error);
  3309. }
  3310. RETURN_TRUE;
  3311. }
  3312. } else {
  3313. RETURN_FALSE;
  3314. }
  3315. }
  3316. /* }}} */
  3317. /* {{{ proto string Phar::addEmptyDir(string dirname)
  3318. * Adds an empty directory to the phar archive
  3319. */
  3320. PHP_METHOD(Phar, addEmptyDir)
  3321. {
  3322. char *dirname;
  3323. int dirname_len;
  3324. PHAR_ARCHIVE_OBJECT();
  3325. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &dirname, &dirname_len) == FAILURE) {
  3326. return;
  3327. }
  3328. if (dirname_len >= sizeof(".phar")-1 && !memcmp(dirname, ".phar", sizeof(".phar")-1)) {
  3329. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Cannot create a directory in magic \".phar\" directory");
  3330. return;
  3331. }
  3332. phar_mkdir(&phar_obj->arc.archive, dirname, dirname_len TSRMLS_CC);
  3333. }
  3334. /* }}} */
  3335. /* {{{ proto string Phar::addFile(string filename[, string localname])
  3336. * Adds a file to the archive using the filename, or the second parameter as the name within the archive
  3337. */
  3338. PHP_METHOD(Phar, addFile)
  3339. {
  3340. char *fname, *localname = NULL;
  3341. int fname_len, localname_len = 0;
  3342. php_stream *resource;
  3343. zval *zresource;
  3344. PHAR_ARCHIVE_OBJECT();
  3345. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &fname, &fname_len, &localname, &localname_len) == FAILURE) {
  3346. return;
  3347. }
  3348. if (!strstr(fname, "://") && php_check_open_basedir(fname TSRMLS_CC)) {
  3349. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "phar error: unable to open file \"%s\" to add to phar archive, open_basedir restrictions prevent this", fname);
  3350. return;
  3351. }
  3352. if (!(resource = php_stream_open_wrapper(fname, "rb", 0, NULL))) {
  3353. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "phar error: unable to open file \"%s\" to add to phar archive", fname);
  3354. return;
  3355. }
  3356. if (localname) {
  3357. fname = localname;
  3358. fname_len = localname_len;
  3359. }
  3360. MAKE_STD_ZVAL(zresource);
  3361. php_stream_to_zval(resource, zresource);
  3362. phar_add_file(&(phar_obj->arc.archive), fname, fname_len, NULL, 0, zresource TSRMLS_CC);
  3363. efree(zresource);
  3364. php_stream_close(resource);
  3365. }
  3366. /* }}} */
  3367. /* {{{ proto string Phar::addFromString(string localname, string contents)
  3368. * Adds a file to the archive using its contents as a string
  3369. */
  3370. PHP_METHOD(Phar, addFromString)
  3371. {
  3372. char *localname, *cont_str;
  3373. int localname_len, cont_len;
  3374. PHAR_ARCHIVE_OBJECT();
  3375. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &localname, &localname_len, &cont_str, &cont_len) == FAILURE) {
  3376. return;
  3377. }
  3378. phar_add_file(&(phar_obj->arc.archive), localname, localname_len, cont_str, cont_len, NULL TSRMLS_CC);
  3379. }
  3380. /* }}} */
  3381. /* {{{ proto string Phar::getStub()
  3382. * Returns the stub at the head of a phar archive as a string.
  3383. */
  3384. PHP_METHOD(Phar, getStub)
  3385. {
  3386. size_t len;
  3387. char *buf;
  3388. php_stream *fp;
  3389. php_stream_filter *filter = NULL;
  3390. phar_entry_info *stub;
  3391. PHAR_ARCHIVE_OBJECT();
  3392. if (phar_obj->arc.archive->is_tar || phar_obj->arc.archive->is_zip) {
  3393. if (SUCCESS == zend_hash_find(&(phar_obj->arc.archive->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1, (void **)&stub)) {
  3394. if (phar_obj->arc.archive->fp && !phar_obj->arc.archive->is_brandnew && !(stub->flags & PHAR_ENT_COMPRESSION_MASK)) {
  3395. fp = phar_obj->arc.archive->fp;
  3396. } else {
  3397. fp = php_stream_open_wrapper(phar_obj->arc.archive->fname, "rb", 0, NULL);
  3398. if (stub->flags & PHAR_ENT_COMPRESSION_MASK) {
  3399. char *filter_name;
  3400. if ((filter_name = phar_decompress_filter(stub, 0)) != NULL) {
  3401. filter = php_stream_filter_create(filter_name, NULL, php_stream_is_persistent(fp) TSRMLS_CC);
  3402. } else {
  3403. filter = NULL;
  3404. }
  3405. if (!filter) {
  3406. zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "phar error: unable to read stub of phar \"%s\" (cannot create %s filter)", phar_obj->arc.archive->fname, phar_decompress_filter(stub, 1));
  3407. return;
  3408. }
  3409. php_stream_filter_append(&fp->readfilters, filter);
  3410. }
  3411. }
  3412. if (!fp) {
  3413. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC,
  3414. "Unable to read stub");
  3415. return;
  3416. }
  3417. php_stream_seek(fp, stub->offset_abs, SEEK_SET);
  3418. len = stub->uncompressed_filesize;
  3419. goto carry_on;
  3420. } else {
  3421. RETURN_STRINGL("", 0, 1);
  3422. }
  3423. }
  3424. len = phar_obj->arc.archive->halt_offset;
  3425. if (phar_obj->arc.archive->fp && !phar_obj->arc.archive->is_brandnew) {
  3426. fp = phar_obj->arc.archive->fp;
  3427. } else {
  3428. fp = php_stream_open_wrapper(phar_obj->arc.archive->fname, "rb", 0, NULL);
  3429. }
  3430. if (!fp) {
  3431. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC,
  3432. "Unable to read stub");
  3433. return;
  3434. }
  3435. php_stream_rewind(fp);
  3436. carry_on:
  3437. buf = safe_emalloc(len, 1, 1);
  3438. if (len != php_stream_read(fp, buf, len)) {
  3439. if (fp != phar_obj->arc.archive->fp) {
  3440. php_stream_close(fp);
  3441. }
  3442. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC,
  3443. "Unable to read stub");
  3444. efree(buf);
  3445. return;
  3446. }
  3447. if (filter) {
  3448. php_stream_filter_flush(filter, 1);
  3449. php_stream_filter_remove(filter, 1 TSRMLS_CC);
  3450. }
  3451. if (fp != phar_obj->arc.archive->fp) {
  3452. php_stream_close(fp);
  3453. }
  3454. buf[len] = '\0';
  3455. RETURN_STRINGL(buf, len, 0);
  3456. }
  3457. /* }}}*/
  3458. /* {{{ proto int Phar::hasMetaData()
  3459. * Returns TRUE if the phar has global metadata, FALSE otherwise.
  3460. */
  3461. PHP_METHOD(Phar, hasMetadata)
  3462. {
  3463. PHAR_ARCHIVE_OBJECT();
  3464. RETURN_BOOL(phar_obj->arc.archive->metadata != NULL);
  3465. }
  3466. /* }}} */
  3467. /* {{{ proto int Phar::getMetaData()
  3468. * Returns the global metadata of the phar
  3469. */
  3470. PHP_METHOD(Phar, getMetadata)
  3471. {
  3472. PHAR_ARCHIVE_OBJECT();
  3473. if (phar_obj->arc.archive->metadata) {
  3474. if (phar_obj->arc.archive->is_persistent) {
  3475. zval *ret;
  3476. char *buf = estrndup((char *) phar_obj->arc.archive->metadata, phar_obj->arc.archive->metadata_len);
  3477. /* assume success, we would have failed before */
  3478. phar_parse_metadata(&buf, &ret, phar_obj->arc.archive->metadata_len TSRMLS_CC);
  3479. efree(buf);
  3480. RETURN_ZVAL(ret, 0, 1);
  3481. }
  3482. RETURN_ZVAL(phar_obj->arc.archive->metadata, 1, 0);
  3483. }
  3484. }
  3485. /* }}} */
  3486. /* {{{ proto int Phar::setMetaData(mixed $metadata)
  3487. * Sets the global metadata of the phar
  3488. */
  3489. PHP_METHOD(Phar, setMetadata)
  3490. {
  3491. char *error;
  3492. zval *metadata;
  3493. PHAR_ARCHIVE_OBJECT();
  3494. if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
  3495. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Write operations disabled by the php.ini setting phar.readonly");
  3496. return;
  3497. }
  3498. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &metadata) == FAILURE) {
  3499. return;
  3500. }
  3501. if (phar_obj->arc.archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->arc.archive) TSRMLS_CC)) {
  3502. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar_obj->arc.archive->fname);
  3503. return;
  3504. }
  3505. if (phar_obj->arc.archive->metadata) {
  3506. zval_ptr_dtor(&phar_obj->arc.archive->metadata);
  3507. phar_obj->arc.archive->metadata = NULL;
  3508. }
  3509. MAKE_STD_ZVAL(phar_obj->arc.archive->metadata);
  3510. ZVAL_ZVAL(phar_obj->arc.archive->metadata, metadata, 1, 0);
  3511. phar_obj->arc.archive->is_modified = 1;
  3512. phar_flush(phar_obj->arc.archive, 0, 0, 0, &error TSRMLS_CC);
  3513. if (error) {
  3514. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  3515. efree(error);
  3516. }
  3517. }
  3518. /* }}} */
  3519. /* {{{ proto int Phar::delMetadata()
  3520. * Deletes the global metadata of the phar
  3521. */
  3522. PHP_METHOD(Phar, delMetadata)
  3523. {
  3524. char *error;
  3525. PHAR_ARCHIVE_OBJECT();
  3526. if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
  3527. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Write operations disabled by the php.ini setting phar.readonly");
  3528. return;
  3529. }
  3530. if (phar_obj->arc.archive->metadata) {
  3531. zval_ptr_dtor(&phar_obj->arc.archive->metadata);
  3532. phar_obj->arc.archive->metadata = NULL;
  3533. phar_obj->arc.archive->is_modified = 1;
  3534. phar_flush(phar_obj->arc.archive, 0, 0, 0, &error TSRMLS_CC);
  3535. if (error) {
  3536. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  3537. efree(error);
  3538. RETURN_FALSE;
  3539. } else {
  3540. RETURN_TRUE;
  3541. }
  3542. } else {
  3543. RETURN_TRUE;
  3544. }
  3545. }
  3546. /* }}} */
  3547. #define PHAR_OPENBASEDIR_CHECKPATH(filename) \
  3548. php_check_open_basedir(filename TSRMLS_CC)
  3549. static int phar_extract_file(zend_bool overwrite, phar_entry_info *entry, char *dest, int dest_len, char **error TSRMLS_DC) /* {{{ */
  3550. {
  3551. php_stream_statbuf ssb;
  3552. int len;
  3553. php_stream *fp;
  3554. char *fullpath, *slash;
  3555. mode_t mode;
  3556. if (entry->is_mounted) {
  3557. /* silently ignore mounted entries */
  3558. return SUCCESS;
  3559. }
  3560. if (entry->filename_len >= sizeof(".phar")-1 && !memcmp(entry->filename, ".phar", sizeof(".phar")-1)) {
  3561. return SUCCESS;
  3562. }
  3563. len = spprintf(&fullpath, 0, "%s/%s", dest, entry->filename);
  3564. if (len >= MAXPATHLEN) {
  3565. char *tmp;
  3566. /* truncate for error message */
  3567. fullpath[50] = '\0';
  3568. if (entry->filename_len > 50) {
  3569. tmp = estrndup(entry->filename, 50);
  3570. spprintf(error, 4096, "Cannot extract \"%s...\" to \"%s...\", extracted filename is too long for filesystem", tmp, fullpath);
  3571. efree(tmp);
  3572. } else {
  3573. spprintf(error, 4096, "Cannot extract \"%s\" to \"%s...\", extracted filename is too long for filesystem", entry->filename, fullpath);
  3574. }
  3575. efree(fullpath);
  3576. return FAILURE;
  3577. }
  3578. if (!len) {
  3579. spprintf(error, 4096, "Cannot extract \"%s\", internal error", entry->filename);
  3580. efree(fullpath);
  3581. return FAILURE;
  3582. }
  3583. if (PHAR_OPENBASEDIR_CHECKPATH(fullpath)) {
  3584. spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", openbasedir/safe mode restrictions in effect", entry->filename, fullpath);
  3585. efree(fullpath);
  3586. return FAILURE;
  3587. }
  3588. /* let see if the path already exists */
  3589. if (!overwrite && SUCCESS == php_stream_stat_path(fullpath, &ssb)) {
  3590. spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", path already exists", entry->filename, fullpath);
  3591. efree(fullpath);
  3592. return FAILURE;
  3593. }
  3594. /* perform dirname */
  3595. slash = zend_memrchr(entry->filename, '/', entry->filename_len);
  3596. if (slash) {
  3597. fullpath[dest_len + (slash - entry->filename) + 1] = '\0';
  3598. } else {
  3599. fullpath[dest_len] = '\0';
  3600. }
  3601. if (FAILURE == php_stream_stat_path(fullpath, &ssb)) {
  3602. if (entry->is_dir) {
  3603. if (!php_stream_mkdir(fullpath, entry->flags & PHAR_ENT_PERM_MASK, PHP_STREAM_MKDIR_RECURSIVE, NULL)) {
  3604. spprintf(error, 4096, "Cannot extract \"%s\", could not create directory \"%s\"", entry->filename, fullpath);
  3605. efree(fullpath);
  3606. return FAILURE;
  3607. }
  3608. } else {
  3609. if (!php_stream_mkdir(fullpath, 0777, PHP_STREAM_MKDIR_RECURSIVE, NULL)) {
  3610. spprintf(error, 4096, "Cannot extract \"%s\", could not create directory \"%s\"", entry->filename, fullpath);
  3611. efree(fullpath);
  3612. return FAILURE;
  3613. }
  3614. }
  3615. }
  3616. if (slash) {
  3617. fullpath[dest_len + (slash - entry->filename) + 1] = '/';
  3618. } else {
  3619. fullpath[dest_len] = '/';
  3620. }
  3621. /* it is a standalone directory, job done */
  3622. if (entry->is_dir) {
  3623. efree(fullpath);
  3624. return SUCCESS;
  3625. }
  3626. #if PHP_API_VERSION < 20100412
  3627. fp = php_stream_open_wrapper(fullpath, "w+b", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
  3628. #else
  3629. fp = php_stream_open_wrapper(fullpath, "w+b", REPORT_ERRORS, NULL);
  3630. #endif
  3631. if (!fp) {
  3632. spprintf(error, 4096, "Cannot extract \"%s\", could not open for writing \"%s\"", entry->filename, fullpath);
  3633. efree(fullpath);
  3634. return FAILURE;
  3635. }
  3636. if (!phar_get_efp(entry, 0 TSRMLS_CC)) {
  3637. if (FAILURE == phar_open_entry_fp(entry, error, 1 TSRMLS_CC)) {
  3638. if (error) {
  3639. spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", unable to open internal file pointer: %s", entry->filename, fullpath, *error);
  3640. } else {
  3641. spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", unable to open internal file pointer", entry->filename, fullpath);
  3642. }
  3643. efree(fullpath);
  3644. php_stream_close(fp);
  3645. return FAILURE;
  3646. }
  3647. }
  3648. if (FAILURE == phar_seek_efp(entry, 0, SEEK_SET, 0, 0 TSRMLS_CC)) {
  3649. spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", unable to seek internal file pointer", entry->filename, fullpath);
  3650. efree(fullpath);
  3651. php_stream_close(fp);
  3652. return FAILURE;
  3653. }
  3654. if (SUCCESS != phar_stream_copy_to_stream(phar_get_efp(entry, 0 TSRMLS_CC), fp, entry->uncompressed_filesize, NULL)) {
  3655. spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", copying contents failed", entry->filename, fullpath);
  3656. efree(fullpath);
  3657. php_stream_close(fp);
  3658. return FAILURE;
  3659. }
  3660. php_stream_close(fp);
  3661. mode = (mode_t) entry->flags & PHAR_ENT_PERM_MASK;
  3662. if (FAILURE == VCWD_CHMOD(fullpath, mode)) {
  3663. spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", setting file permissions failed", entry->filename, fullpath);
  3664. efree(fullpath);
  3665. return FAILURE;
  3666. }
  3667. efree(fullpath);
  3668. return SUCCESS;
  3669. }
  3670. /* }}} */
  3671. /* {{{ proto bool Phar::extractTo(string pathto[[, mixed files], bool overwrite])
  3672. * Extract one or more file from a phar archive, optionally overwriting existing files
  3673. */
  3674. PHP_METHOD(Phar, extractTo)
  3675. {
  3676. char *error = NULL;
  3677. php_stream *fp;
  3678. php_stream_statbuf ssb;
  3679. phar_entry_info *entry;
  3680. char *pathto, *filename, *actual;
  3681. int pathto_len, filename_len;
  3682. int ret, i;
  3683. int nelems;
  3684. zval *zval_files = NULL;
  3685. zend_bool overwrite = 0;
  3686. PHAR_ARCHIVE_OBJECT();
  3687. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z!b", &pathto, &pathto_len, &zval_files, &overwrite) == FAILURE) {
  3688. return;
  3689. }
  3690. fp = php_stream_open_wrapper(phar_obj->arc.archive->fname, "rb", IGNORE_URL|STREAM_MUST_SEEK, &actual);
  3691. if (!fp) {
  3692. zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC,
  3693. "Invalid argument, %s cannot be found", phar_obj->arc.archive->fname);
  3694. return;
  3695. }
  3696. efree(actual);
  3697. php_stream_close(fp);
  3698. if (pathto_len < 1) {
  3699. zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC,
  3700. "Invalid argument, extraction path must be non-zero length");
  3701. return;
  3702. }
  3703. if (pathto_len >= MAXPATHLEN) {
  3704. char *tmp = estrndup(pathto, 50);
  3705. /* truncate for error message */
  3706. zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "Cannot extract to \"%s...\", destination directory is too long for filesystem", tmp);
  3707. efree(tmp);
  3708. return;
  3709. }
  3710. if (php_stream_stat_path(pathto, &ssb) < 0) {
  3711. ret = php_stream_mkdir(pathto, 0777, PHP_STREAM_MKDIR_RECURSIVE, NULL);
  3712. if (!ret) {
  3713. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC,
  3714. "Unable to create path \"%s\" for extraction", pathto);
  3715. return;
  3716. }
  3717. } else if (!(ssb.sb.st_mode & S_IFDIR)) {
  3718. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC,
  3719. "Unable to use path \"%s\" for extraction, it is a file, must be a directory", pathto);
  3720. return;
  3721. }
  3722. if (zval_files) {
  3723. switch (Z_TYPE_P(zval_files)) {
  3724. case IS_NULL:
  3725. goto all_files;
  3726. #if PHP_VERSION_ID >= 60000
  3727. case IS_UNICODE:
  3728. zval_unicode_to_string(zval_files TSRMLS_CC);
  3729. /* break intentionally omitted */
  3730. #endif
  3731. case IS_STRING:
  3732. filename = Z_STRVAL_P(zval_files);
  3733. filename_len = Z_STRLEN_P(zval_files);
  3734. break;
  3735. case IS_ARRAY:
  3736. nelems = zend_hash_num_elements(Z_ARRVAL_P(zval_files));
  3737. if (nelems == 0 ) {
  3738. RETURN_FALSE;
  3739. }
  3740. for (i = 0; i < nelems; i++) {
  3741. zval **zval_file;
  3742. if (zend_hash_index_find(Z_ARRVAL_P(zval_files), i, (void **) &zval_file) == SUCCESS) {
  3743. switch (Z_TYPE_PP(zval_file)) {
  3744. #if PHP_VERSION_ID >= 60000
  3745. case IS_UNICODE:
  3746. zval_unicode_to_string(*(zval_file) TSRMLS_CC);
  3747. /* break intentionally omitted */
  3748. #endif
  3749. case IS_STRING:
  3750. break;
  3751. default:
  3752. zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC,
  3753. "Invalid argument, array of filenames to extract contains non-string value");
  3754. return;
  3755. }
  3756. if (FAILURE == zend_hash_find(&phar_obj->arc.archive->manifest, Z_STRVAL_PP(zval_file), Z_STRLEN_PP(zval_file), (void **)&entry)) {
  3757. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC,
  3758. "Phar Error: attempted to extract non-existent file \"%s\" from phar \"%s\"", Z_STRVAL_PP(zval_file), phar_obj->arc.archive->fname);
  3759. }
  3760. if (FAILURE == phar_extract_file(overwrite, entry, pathto, pathto_len, &error TSRMLS_CC)) {
  3761. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC,
  3762. "Extraction from phar \"%s\" failed: %s", phar_obj->arc.archive->fname, error);
  3763. efree(error);
  3764. return;
  3765. }
  3766. }
  3767. }
  3768. RETURN_TRUE;
  3769. default:
  3770. zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC,
  3771. "Invalid argument, expected a filename (string) or array of filenames");
  3772. return;
  3773. }
  3774. if (FAILURE == zend_hash_find(&phar_obj->arc.archive->manifest, filename, filename_len, (void **)&entry)) {
  3775. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC,
  3776. "Phar Error: attempted to extract non-existent file \"%s\" from phar \"%s\"", filename, phar_obj->arc.archive->fname);
  3777. return;
  3778. }
  3779. if (FAILURE == phar_extract_file(overwrite, entry, pathto, pathto_len, &error TSRMLS_CC)) {
  3780. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC,
  3781. "Extraction from phar \"%s\" failed: %s", phar_obj->arc.archive->fname, error);
  3782. efree(error);
  3783. return;
  3784. }
  3785. } else {
  3786. phar_archive_data *phar;
  3787. all_files:
  3788. phar = phar_obj->arc.archive;
  3789. /* Extract all files */
  3790. if (!zend_hash_num_elements(&(phar->manifest))) {
  3791. RETURN_TRUE;
  3792. }
  3793. for (zend_hash_internal_pointer_reset(&phar->manifest);
  3794. zend_hash_has_more_elements(&phar->manifest) == SUCCESS;
  3795. zend_hash_move_forward(&phar->manifest)) {
  3796. if (zend_hash_get_current_data(&phar->manifest, (void **)&entry) == FAILURE) {
  3797. continue;
  3798. }
  3799. if (FAILURE == phar_extract_file(overwrite, entry, pathto, pathto_len, &error TSRMLS_CC)) {
  3800. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC,
  3801. "Extraction from phar \"%s\" failed: %s", phar->fname, error);
  3802. efree(error);
  3803. return;
  3804. }
  3805. }
  3806. }
  3807. RETURN_TRUE;
  3808. }
  3809. /* }}} */
  3810. /* {{{ proto void PharFileInfo::__construct(string entry)
  3811. * Construct a Phar entry object
  3812. */
  3813. PHP_METHOD(PharFileInfo, __construct)
  3814. {
  3815. char *fname, *arch, *entry, *error;
  3816. int fname_len, arch_len, entry_len;
  3817. phar_entry_object *entry_obj;
  3818. phar_entry_info *entry_info;
  3819. phar_archive_data *phar_data;
  3820. zval *zobj = getThis(), arg1;
  3821. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &fname, &fname_len) == FAILURE) {
  3822. return;
  3823. }
  3824. entry_obj = (phar_entry_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
  3825. if (entry_obj->ent.entry) {
  3826. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Cannot call constructor twice");
  3827. return;
  3828. }
  3829. if (fname_len < 7 || memcmp(fname, "phar://", 7) || phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0 TSRMLS_CC) == FAILURE) {
  3830. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC,
  3831. "'%s' is not a valid phar archive URL (must have at least phar://filename.phar)", fname);
  3832. return;
  3833. }
  3834. if (phar_open_from_filename(arch, arch_len, NULL, 0, REPORT_ERRORS, &phar_data, &error TSRMLS_CC) == FAILURE) {
  3835. efree(arch);
  3836. efree(entry);
  3837. if (error) {
  3838. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC,
  3839. "Cannot open phar file '%s': %s", fname, error);
  3840. efree(error);
  3841. } else {
  3842. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC,
  3843. "Cannot open phar file '%s'", fname);
  3844. }
  3845. return;
  3846. }
  3847. if ((entry_info = phar_get_entry_info_dir(phar_data, entry, entry_len, 1, &error, 1 TSRMLS_CC)) == NULL) {
  3848. zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC,
  3849. "Cannot access phar file entry '%s' in archive '%s'%s%s", entry, arch, error ? ", " : "", error ? error : "");
  3850. efree(arch);
  3851. efree(entry);
  3852. return;
  3853. }
  3854. efree(arch);
  3855. efree(entry);
  3856. entry_obj->ent.entry = entry_info;
  3857. INIT_PZVAL(&arg1);
  3858. ZVAL_STRINGL(&arg1, fname, fname_len, 0);
  3859. zend_call_method_with_1_params(&zobj, Z_OBJCE_P(zobj),
  3860. &spl_ce_SplFileInfo->constructor, "__construct", NULL, &arg1);
  3861. }
  3862. /* }}} */
  3863. #define PHAR_ENTRY_OBJECT() \
  3864. phar_entry_object *entry_obj = (phar_entry_object*)zend_object_store_get_object(getThis() TSRMLS_CC); \
  3865. if (!entry_obj->ent.entry) { \
  3866. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
  3867. "Cannot call method on an uninitialized PharFileInfo object"); \
  3868. return; \
  3869. }
  3870. /* {{{ proto void PharFileInfo::__destruct()
  3871. * clean up directory-based entry objects
  3872. */
  3873. PHP_METHOD(PharFileInfo, __destruct)
  3874. {
  3875. phar_entry_object *entry_obj = (phar_entry_object*)zend_object_store_get_object(getThis() TSRMLS_CC); \
  3876. if (entry_obj->ent.entry && entry_obj->ent.entry->is_temp_dir) {
  3877. if (entry_obj->ent.entry->filename) {
  3878. efree(entry_obj->ent.entry->filename);
  3879. entry_obj->ent.entry->filename = NULL;
  3880. }
  3881. efree(entry_obj->ent.entry);
  3882. entry_obj->ent.entry = NULL;
  3883. }
  3884. }
  3885. /* }}} */
  3886. /* {{{ proto int PharFileInfo::getCompressedSize()
  3887. * Returns the compressed size
  3888. */
  3889. PHP_METHOD(PharFileInfo, getCompressedSize)
  3890. {
  3891. PHAR_ENTRY_OBJECT();
  3892. RETURN_LONG(entry_obj->ent.entry->compressed_filesize);
  3893. }
  3894. /* }}} */
  3895. /* {{{ proto bool PharFileInfo::isCompressed([int compression_type])
  3896. * Returns whether the entry is compressed, and whether it is compressed with Phar::GZ or Phar::BZ2 if specified
  3897. */
  3898. PHP_METHOD(PharFileInfo, isCompressed)
  3899. {
  3900. /* a number that is not Phar::GZ or Phar::BZ2 */
  3901. long method = 9021976;
  3902. PHAR_ENTRY_OBJECT();
  3903. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &method) == FAILURE) {
  3904. return;
  3905. }
  3906. switch (method) {
  3907. case 9021976:
  3908. RETURN_BOOL(entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSION_MASK);
  3909. case PHAR_ENT_COMPRESSED_GZ:
  3910. RETURN_BOOL(entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_GZ);
  3911. case PHAR_ENT_COMPRESSED_BZ2:
  3912. RETURN_BOOL(entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_BZ2);
  3913. default:
  3914. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
  3915. "Unknown compression type specified"); \
  3916. }
  3917. }
  3918. /* }}} */
  3919. /* {{{ proto int PharFileInfo::getCRC32()
  3920. * Returns CRC32 code or throws an exception if not CRC checked
  3921. */
  3922. PHP_METHOD(PharFileInfo, getCRC32)
  3923. {
  3924. PHAR_ENTRY_OBJECT();
  3925. if (entry_obj->ent.entry->is_dir) {
  3926. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
  3927. "Phar entry is a directory, does not have a CRC"); \
  3928. return;
  3929. }
  3930. if (entry_obj->ent.entry->is_crc_checked) {
  3931. RETURN_LONG(entry_obj->ent.entry->crc32);
  3932. } else {
  3933. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
  3934. "Phar entry was not CRC checked"); \
  3935. }
  3936. }
  3937. /* }}} */
  3938. /* {{{ proto int PharFileInfo::isCRCChecked()
  3939. * Returns whether file entry is CRC checked
  3940. */
  3941. PHP_METHOD(PharFileInfo, isCRCChecked)
  3942. {
  3943. PHAR_ENTRY_OBJECT();
  3944. RETURN_BOOL(entry_obj->ent.entry->is_crc_checked);
  3945. }
  3946. /* }}} */
  3947. /* {{{ proto int PharFileInfo::getPharFlags()
  3948. * Returns the Phar file entry flags
  3949. */
  3950. PHP_METHOD(PharFileInfo, getPharFlags)
  3951. {
  3952. PHAR_ENTRY_OBJECT();
  3953. RETURN_LONG(entry_obj->ent.entry->flags & ~(PHAR_ENT_PERM_MASK|PHAR_ENT_COMPRESSION_MASK));
  3954. }
  3955. /* }}} */
  3956. /* {{{ proto int PharFileInfo::chmod()
  3957. * set the file permissions for the Phar. This only allows setting execution bit, read/write
  3958. */
  3959. PHP_METHOD(PharFileInfo, chmod)
  3960. {
  3961. char *error;
  3962. long perms;
  3963. PHAR_ENTRY_OBJECT();
  3964. if (entry_obj->ent.entry->is_temp_dir) {
  3965. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
  3966. "Phar entry \"%s\" is a temporary directory (not an actual entry in the archive), cannot chmod", entry_obj->ent.entry->filename); \
  3967. return;
  3968. }
  3969. if (PHAR_G(readonly) && !entry_obj->ent.entry->phar->is_data) {
  3970. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Cannot modify permissions for file \"%s\" in phar \"%s\", write operations are prohibited", entry_obj->ent.entry->filename, entry_obj->ent.entry->phar->fname);
  3971. return;
  3972. }
  3973. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &perms) == FAILURE) {
  3974. return;
  3975. }
  3976. if (entry_obj->ent.entry->is_persistent) {
  3977. phar_archive_data *phar = entry_obj->ent.entry->phar;
  3978. if (FAILURE == phar_copy_on_write(&phar TSRMLS_CC)) {
  3979. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
  3980. return;
  3981. }
  3982. /* re-populate after copy-on-write */
  3983. zend_hash_find(&phar->manifest, entry_obj->ent.entry->filename, entry_obj->ent.entry->filename_len, (void **)&entry_obj->ent.entry);
  3984. }
  3985. /* clear permissions */
  3986. entry_obj->ent.entry->flags &= ~PHAR_ENT_PERM_MASK;
  3987. perms &= 0777;
  3988. entry_obj->ent.entry->flags |= perms;
  3989. entry_obj->ent.entry->old_flags = entry_obj->ent.entry->flags;
  3990. entry_obj->ent.entry->phar->is_modified = 1;
  3991. entry_obj->ent.entry->is_modified = 1;
  3992. /* hackish cache in php_stat needs to be cleared */
  3993. /* if this code fails to work, check main/streams/streams.c, _php_stream_stat_path */
  3994. if (BG(CurrentLStatFile)) {
  3995. efree(BG(CurrentLStatFile));
  3996. }
  3997. if (BG(CurrentStatFile)) {
  3998. efree(BG(CurrentStatFile));
  3999. }
  4000. BG(CurrentLStatFile) = NULL;
  4001. BG(CurrentStatFile) = NULL;
  4002. phar_flush(entry_obj->ent.entry->phar, 0, 0, 0, &error TSRMLS_CC);
  4003. if (error) {
  4004. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  4005. efree(error);
  4006. }
  4007. }
  4008. /* }}} */
  4009. /* {{{ proto int PharFileInfo::hasMetaData()
  4010. * Returns the metadata of the entry
  4011. */
  4012. PHP_METHOD(PharFileInfo, hasMetadata)
  4013. {
  4014. PHAR_ENTRY_OBJECT();
  4015. RETURN_BOOL(entry_obj->ent.entry->metadata != NULL);
  4016. }
  4017. /* }}} */
  4018. /* {{{ proto int PharFileInfo::getMetaData()
  4019. * Returns the metadata of the entry
  4020. */
  4021. PHP_METHOD(PharFileInfo, getMetadata)
  4022. {
  4023. PHAR_ENTRY_OBJECT();
  4024. if (entry_obj->ent.entry->metadata) {
  4025. if (entry_obj->ent.entry->is_persistent) {
  4026. zval *ret;
  4027. char *buf = estrndup((char *) entry_obj->ent.entry->metadata, entry_obj->ent.entry->metadata_len);
  4028. /* assume success, we would have failed before */
  4029. phar_parse_metadata(&buf, &ret, entry_obj->ent.entry->metadata_len TSRMLS_CC);
  4030. efree(buf);
  4031. RETURN_ZVAL(ret, 0, 1);
  4032. }
  4033. RETURN_ZVAL(entry_obj->ent.entry->metadata, 1, 0);
  4034. }
  4035. }
  4036. /* }}} */
  4037. /* {{{ proto int PharFileInfo::setMetaData(mixed $metadata)
  4038. * Sets the metadata of the entry
  4039. */
  4040. PHP_METHOD(PharFileInfo, setMetadata)
  4041. {
  4042. char *error;
  4043. zval *metadata;
  4044. PHAR_ENTRY_OBJECT();
  4045. if (PHAR_G(readonly) && !entry_obj->ent.entry->phar->is_data) {
  4046. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Write operations disabled by the php.ini setting phar.readonly");
  4047. return;
  4048. }
  4049. if (entry_obj->ent.entry->is_temp_dir) {
  4050. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
  4051. "Phar entry is a temporary directory (not an actual entry in the archive), cannot set metadata"); \
  4052. return;
  4053. }
  4054. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &metadata) == FAILURE) {
  4055. return;
  4056. }
  4057. if (entry_obj->ent.entry->is_persistent) {
  4058. phar_archive_data *phar = entry_obj->ent.entry->phar;
  4059. if (FAILURE == phar_copy_on_write(&phar TSRMLS_CC)) {
  4060. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
  4061. return;
  4062. }
  4063. /* re-populate after copy-on-write */
  4064. zend_hash_find(&phar->manifest, entry_obj->ent.entry->filename, entry_obj->ent.entry->filename_len, (void **)&entry_obj->ent.entry);
  4065. }
  4066. if (entry_obj->ent.entry->metadata) {
  4067. zval_ptr_dtor(&entry_obj->ent.entry->metadata);
  4068. entry_obj->ent.entry->metadata = NULL;
  4069. }
  4070. MAKE_STD_ZVAL(entry_obj->ent.entry->metadata);
  4071. ZVAL_ZVAL(entry_obj->ent.entry->metadata, metadata, 1, 0);
  4072. entry_obj->ent.entry->is_modified = 1;
  4073. entry_obj->ent.entry->phar->is_modified = 1;
  4074. phar_flush(entry_obj->ent.entry->phar, 0, 0, 0, &error TSRMLS_CC);
  4075. if (error) {
  4076. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  4077. efree(error);
  4078. }
  4079. }
  4080. /* }}} */
  4081. /* {{{ proto bool PharFileInfo::delMetaData()
  4082. * Deletes the metadata of the entry
  4083. */
  4084. PHP_METHOD(PharFileInfo, delMetadata)
  4085. {
  4086. char *error;
  4087. PHAR_ENTRY_OBJECT();
  4088. if (PHAR_G(readonly) && !entry_obj->ent.entry->phar->is_data) {
  4089. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Write operations disabled by the php.ini setting phar.readonly");
  4090. return;
  4091. }
  4092. if (entry_obj->ent.entry->is_temp_dir) {
  4093. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
  4094. "Phar entry is a temporary directory (not an actual entry in the archive), cannot delete metadata"); \
  4095. return;
  4096. }
  4097. if (entry_obj->ent.entry->metadata) {
  4098. if (entry_obj->ent.entry->is_persistent) {
  4099. phar_archive_data *phar = entry_obj->ent.entry->phar;
  4100. if (FAILURE == phar_copy_on_write(&phar TSRMLS_CC)) {
  4101. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
  4102. return;
  4103. }
  4104. /* re-populate after copy-on-write */
  4105. zend_hash_find(&phar->manifest, entry_obj->ent.entry->filename, entry_obj->ent.entry->filename_len, (void **)&entry_obj->ent.entry);
  4106. }
  4107. zval_ptr_dtor(&entry_obj->ent.entry->metadata);
  4108. entry_obj->ent.entry->metadata = NULL;
  4109. entry_obj->ent.entry->is_modified = 1;
  4110. entry_obj->ent.entry->phar->is_modified = 1;
  4111. phar_flush(entry_obj->ent.entry->phar, 0, 0, 0, &error TSRMLS_CC);
  4112. if (error) {
  4113. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  4114. efree(error);
  4115. RETURN_FALSE;
  4116. } else {
  4117. RETURN_TRUE;
  4118. }
  4119. } else {
  4120. RETURN_TRUE;
  4121. }
  4122. }
  4123. /* }}} */
  4124. /* {{{ proto string PharFileInfo::getContent()
  4125. * return the complete file contents of the entry (like file_get_contents)
  4126. */
  4127. PHP_METHOD(PharFileInfo, getContent)
  4128. {
  4129. char *error;
  4130. php_stream *fp;
  4131. phar_entry_info *link;
  4132. PHAR_ENTRY_OBJECT();
  4133. if (entry_obj->ent.entry->is_dir) {
  4134. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  4135. "Phar error: Cannot retrieve contents, \"%s\" in phar \"%s\" is a directory", entry_obj->ent.entry->filename, entry_obj->ent.entry->phar->fname);
  4136. return;
  4137. }
  4138. link = phar_get_link_source(entry_obj->ent.entry TSRMLS_CC);
  4139. if (!link) {
  4140. link = entry_obj->ent.entry;
  4141. }
  4142. if (SUCCESS != phar_open_entry_fp(link, &error, 0 TSRMLS_CC)) {
  4143. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  4144. "Phar error: Cannot retrieve contents, \"%s\" in phar \"%s\": %s", entry_obj->ent.entry->filename, entry_obj->ent.entry->phar->fname, error);
  4145. efree(error);
  4146. return;
  4147. }
  4148. if (!(fp = phar_get_efp(link, 0 TSRMLS_CC))) {
  4149. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  4150. "Phar error: Cannot retrieve contents of \"%s\" in phar \"%s\"", entry_obj->ent.entry->filename, entry_obj->ent.entry->phar->fname);
  4151. return;
  4152. }
  4153. phar_seek_efp(link, 0, SEEK_SET, 0, 0 TSRMLS_CC);
  4154. Z_TYPE_P(return_value) = IS_STRING;
  4155. #if PHP_MAJOR_VERSION >= 6
  4156. Z_STRLEN_P(return_value) = php_stream_copy_to_mem(fp, (void **) &(Z_STRVAL_P(return_value)), link->uncompressed_filesize, 0);
  4157. #else
  4158. Z_STRLEN_P(return_value) = php_stream_copy_to_mem(fp, &(Z_STRVAL_P(return_value)), link->uncompressed_filesize, 0);
  4159. #endif
  4160. if (!Z_STRVAL_P(return_value)) {
  4161. Z_STRVAL_P(return_value) = estrndup("", 0);
  4162. }
  4163. }
  4164. /* }}} */
  4165. /* {{{ proto int PharFileInfo::compress(int compression_type)
  4166. * Instructs the Phar class to compress the current file using zlib or bzip2 compression
  4167. */
  4168. PHP_METHOD(PharFileInfo, compress)
  4169. {
  4170. long method;
  4171. char *error;
  4172. PHAR_ENTRY_OBJECT();
  4173. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &method) == FAILURE) {
  4174. return;
  4175. }
  4176. if (entry_obj->ent.entry->is_tar) {
  4177. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  4178. "Cannot compress with Gzip compression, not possible with tar-based phar archives");
  4179. return;
  4180. }
  4181. if (entry_obj->ent.entry->is_dir) {
  4182. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
  4183. "Phar entry is a directory, cannot set compression"); \
  4184. return;
  4185. }
  4186. if (PHAR_G(readonly) && !entry_obj->ent.entry->phar->is_data) {
  4187. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  4188. "Phar is readonly, cannot change compression");
  4189. return;
  4190. }
  4191. if (entry_obj->ent.entry->is_deleted) {
  4192. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  4193. "Cannot compress deleted file");
  4194. return;
  4195. }
  4196. if (entry_obj->ent.entry->is_persistent) {
  4197. phar_archive_data *phar = entry_obj->ent.entry->phar;
  4198. if (FAILURE == phar_copy_on_write(&phar TSRMLS_CC)) {
  4199. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
  4200. return;
  4201. }
  4202. /* re-populate after copy-on-write */
  4203. zend_hash_find(&phar->manifest, entry_obj->ent.entry->filename, entry_obj->ent.entry->filename_len, (void **)&entry_obj->ent.entry);
  4204. }
  4205. switch (method) {
  4206. case PHAR_ENT_COMPRESSED_GZ:
  4207. if (entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_GZ) {
  4208. RETURN_TRUE;
  4209. }
  4210. if ((entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_BZ2) != 0) {
  4211. if (!PHAR_G(has_bz2)) {
  4212. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  4213. "Cannot compress with gzip compression, file is already compressed with bzip2 compression and bz2 extension is not enabled, cannot decompress");
  4214. return;
  4215. }
  4216. /* decompress this file indirectly */
  4217. if (SUCCESS != phar_open_entry_fp(entry_obj->ent.entry, &error, 1 TSRMLS_CC)) {
  4218. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  4219. "Phar error: Cannot decompress bzip2-compressed file \"%s\" in phar \"%s\" in order to compress with gzip: %s", entry_obj->ent.entry->filename, entry_obj->ent.entry->phar->fname, error);
  4220. efree(error);
  4221. return;
  4222. }
  4223. }
  4224. if (!PHAR_G(has_zlib)) {
  4225. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  4226. "Cannot compress with gzip compression, zlib extension is not enabled");
  4227. return;
  4228. }
  4229. entry_obj->ent.entry->old_flags = entry_obj->ent.entry->flags;
  4230. entry_obj->ent.entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
  4231. entry_obj->ent.entry->flags |= PHAR_ENT_COMPRESSED_GZ;
  4232. break;
  4233. case PHAR_ENT_COMPRESSED_BZ2:
  4234. if (entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_BZ2) {
  4235. RETURN_TRUE;
  4236. }
  4237. if ((entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_GZ) != 0) {
  4238. if (!PHAR_G(has_zlib)) {
  4239. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  4240. "Cannot compress with bzip2 compression, file is already compressed with gzip compression and zlib extension is not enabled, cannot decompress");
  4241. return;
  4242. }
  4243. /* decompress this file indirectly */
  4244. if (SUCCESS != phar_open_entry_fp(entry_obj->ent.entry, &error, 1 TSRMLS_CC)) {
  4245. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  4246. "Phar error: Cannot decompress gzip-compressed file \"%s\" in phar \"%s\" in order to compress with bzip2: %s", entry_obj->ent.entry->filename, entry_obj->ent.entry->phar->fname, error);
  4247. efree(error);
  4248. return;
  4249. }
  4250. }
  4251. if (!PHAR_G(has_bz2)) {
  4252. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  4253. "Cannot compress with bzip2 compression, bz2 extension is not enabled");
  4254. return;
  4255. }
  4256. entry_obj->ent.entry->old_flags = entry_obj->ent.entry->flags;
  4257. entry_obj->ent.entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
  4258. entry_obj->ent.entry->flags |= PHAR_ENT_COMPRESSED_BZ2;
  4259. break;
  4260. default:
  4261. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
  4262. "Unknown compression type specified"); \
  4263. }
  4264. entry_obj->ent.entry->phar->is_modified = 1;
  4265. entry_obj->ent.entry->is_modified = 1;
  4266. phar_flush(entry_obj->ent.entry->phar, 0, 0, 0, &error TSRMLS_CC);
  4267. if (error) {
  4268. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  4269. efree(error);
  4270. }
  4271. RETURN_TRUE;
  4272. }
  4273. /* }}} */
  4274. /* {{{ proto int PharFileInfo::decompress()
  4275. * Instructs the Phar class to decompress the current file
  4276. */
  4277. PHP_METHOD(PharFileInfo, decompress)
  4278. {
  4279. char *error;
  4280. PHAR_ENTRY_OBJECT();
  4281. if (entry_obj->ent.entry->is_dir) {
  4282. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
  4283. "Phar entry is a directory, cannot set compression"); \
  4284. return;
  4285. }
  4286. if ((entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSION_MASK) == 0) {
  4287. RETURN_TRUE;
  4288. }
  4289. if (PHAR_G(readonly) && !entry_obj->ent.entry->phar->is_data) {
  4290. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  4291. "Phar is readonly, cannot decompress");
  4292. return;
  4293. }
  4294. if (entry_obj->ent.entry->is_deleted) {
  4295. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  4296. "Cannot compress deleted file");
  4297. return;
  4298. }
  4299. if ((entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_GZ) != 0 && !PHAR_G(has_zlib)) {
  4300. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  4301. "Cannot decompress Gzip-compressed file, zlib extension is not enabled");
  4302. return;
  4303. }
  4304. if ((entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_BZ2) != 0 && !PHAR_G(has_bz2)) {
  4305. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
  4306. "Cannot decompress Bzip2-compressed file, bz2 extension is not enabled");
  4307. return;
  4308. }
  4309. if (entry_obj->ent.entry->is_persistent) {
  4310. phar_archive_data *phar = entry_obj->ent.entry->phar;
  4311. if (FAILURE == phar_copy_on_write(&phar TSRMLS_CC)) {
  4312. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
  4313. return;
  4314. }
  4315. /* re-populate after copy-on-write */
  4316. zend_hash_find(&phar->manifest, entry_obj->ent.entry->filename, entry_obj->ent.entry->filename_len, (void **)&entry_obj->ent.entry);
  4317. }
  4318. if (!entry_obj->ent.entry->fp) {
  4319. if (FAILURE == phar_open_archive_fp(entry_obj->ent.entry->phar TSRMLS_CC)) {
  4320. zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Cannot decompress entry \"%s\", phar error: Cannot open phar archive \"%s\" for reading", entry_obj->ent.entry->filename, entry_obj->ent.entry->phar->fname);
  4321. return;
  4322. }
  4323. entry_obj->ent.entry->fp_type = PHAR_FP;
  4324. }
  4325. entry_obj->ent.entry->old_flags = entry_obj->ent.entry->flags;
  4326. entry_obj->ent.entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
  4327. entry_obj->ent.entry->phar->is_modified = 1;
  4328. entry_obj->ent.entry->is_modified = 1;
  4329. phar_flush(entry_obj->ent.entry->phar, 0, 0, 0, &error TSRMLS_CC);
  4330. if (error) {
  4331. zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
  4332. efree(error);
  4333. }
  4334. RETURN_TRUE;
  4335. }
  4336. /* }}} */
  4337. #endif /* HAVE_SPL */
  4338. /* {{{ phar methods */
  4339. PHAR_ARG_INFO
  4340. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar___construct, 0, 0, 1)
  4341. ZEND_ARG_INFO(0, filename)
  4342. ZEND_ARG_INFO(0, flags)
  4343. ZEND_ARG_INFO(0, alias)
  4344. ZEND_ARG_INFO(0, fileformat)
  4345. ZEND_END_ARG_INFO()
  4346. PHAR_ARG_INFO
  4347. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_createDS, 0, 0, 0)
  4348. ZEND_ARG_INFO(0, index)
  4349. ZEND_ARG_INFO(0, webindex)
  4350. ZEND_END_ARG_INFO()
  4351. PHAR_ARG_INFO
  4352. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_loadPhar, 0, 0, 1)
  4353. ZEND_ARG_INFO(0, filename)
  4354. ZEND_ARG_INFO(0, alias)
  4355. ZEND_END_ARG_INFO()
  4356. PHAR_ARG_INFO
  4357. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_mapPhar, 0, 0, 0)
  4358. ZEND_ARG_INFO(0, alias)
  4359. ZEND_ARG_INFO(0, offset)
  4360. ZEND_END_ARG_INFO()
  4361. PHAR_ARG_INFO
  4362. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_mount, 0, 0, 2)
  4363. ZEND_ARG_INFO(0, inphar)
  4364. ZEND_ARG_INFO(0, externalfile)
  4365. ZEND_END_ARG_INFO()
  4366. PHAR_ARG_INFO
  4367. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_mungServer, 0, 0, 1)
  4368. ZEND_ARG_INFO(0, munglist)
  4369. ZEND_END_ARG_INFO()
  4370. PHAR_ARG_INFO
  4371. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_webPhar, 0, 0, 0)
  4372. ZEND_ARG_INFO(0, alias)
  4373. ZEND_ARG_INFO(0, index)
  4374. ZEND_ARG_INFO(0, f404)
  4375. ZEND_ARG_INFO(0, mimetypes)
  4376. ZEND_ARG_INFO(0, rewrites)
  4377. ZEND_END_ARG_INFO()
  4378. PHAR_ARG_INFO
  4379. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_running, 0, 0, 1)
  4380. ZEND_ARG_INFO(0, retphar)
  4381. ZEND_END_ARG_INFO()
  4382. PHAR_ARG_INFO
  4383. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_ua, 0, 0, 1)
  4384. ZEND_ARG_INFO(0, archive)
  4385. ZEND_END_ARG_INFO()
  4386. #if HAVE_SPL
  4387. PHAR_ARG_INFO
  4388. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_build, 0, 0, 1)
  4389. ZEND_ARG_INFO(0, iterator)
  4390. ZEND_ARG_INFO(0, base_directory)
  4391. ZEND_END_ARG_INFO()
  4392. PHAR_ARG_INFO
  4393. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_conv, 0, 0, 0)
  4394. ZEND_ARG_INFO(0, format)
  4395. ZEND_ARG_INFO(0, compression_type)
  4396. ZEND_ARG_INFO(0, file_ext)
  4397. ZEND_END_ARG_INFO()
  4398. PHAR_ARG_INFO
  4399. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_comps, 0, 0, 1)
  4400. ZEND_ARG_INFO(0, compression_type)
  4401. ZEND_ARG_INFO(0, file_ext)
  4402. ZEND_END_ARG_INFO()
  4403. PHAR_ARG_INFO
  4404. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_decomp, 0, 0, 0)
  4405. ZEND_ARG_INFO(0, file_ext)
  4406. ZEND_END_ARG_INFO()
  4407. PHAR_ARG_INFO
  4408. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_comp, 0, 0, 1)
  4409. ZEND_ARG_INFO(0, compression_type)
  4410. ZEND_END_ARG_INFO()
  4411. PHAR_ARG_INFO
  4412. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_compo, 0, 0, 0)
  4413. ZEND_ARG_INFO(0, compression_type)
  4414. ZEND_END_ARG_INFO()
  4415. PHAR_ARG_INFO
  4416. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_copy, 0, 0, 2)
  4417. ZEND_ARG_INFO(0, newfile)
  4418. ZEND_ARG_INFO(0, oldfile)
  4419. ZEND_END_ARG_INFO()
  4420. PHAR_ARG_INFO
  4421. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_delete, 0, 0, 1)
  4422. ZEND_ARG_INFO(0, entry)
  4423. ZEND_END_ARG_INFO()
  4424. PHAR_ARG_INFO
  4425. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_fromdir, 0, 0, 1)
  4426. ZEND_ARG_INFO(0, base_dir)
  4427. ZEND_ARG_INFO(0, regex)
  4428. ZEND_END_ARG_INFO()
  4429. PHAR_ARG_INFO
  4430. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_offsetExists, 0, 0, 1)
  4431. ZEND_ARG_INFO(0, entry)
  4432. ZEND_END_ARG_INFO()
  4433. PHAR_ARG_INFO
  4434. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_offsetSet, 0, 0, 2)
  4435. ZEND_ARG_INFO(0, entry)
  4436. ZEND_ARG_INFO(0, value)
  4437. ZEND_END_ARG_INFO()
  4438. PHAR_ARG_INFO
  4439. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_setAlias, 0, 0, 1)
  4440. ZEND_ARG_INFO(0, alias)
  4441. ZEND_END_ARG_INFO()
  4442. PHAR_ARG_INFO
  4443. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_setMetadata, 0, 0, 1)
  4444. ZEND_ARG_INFO(0, metadata)
  4445. ZEND_END_ARG_INFO()
  4446. PHAR_ARG_INFO
  4447. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_setSigAlgo, 0, 0, 1)
  4448. ZEND_ARG_INFO(0, algorithm)
  4449. ZEND_ARG_INFO(0, privatekey)
  4450. ZEND_END_ARG_INFO()
  4451. PHAR_ARG_INFO
  4452. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_setStub, 0, 0, 1)
  4453. ZEND_ARG_INFO(0, newstub)
  4454. ZEND_ARG_INFO(0, maxlen)
  4455. ZEND_END_ARG_INFO()
  4456. PHAR_ARG_INFO
  4457. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_emptydir, 0, 0, 0)
  4458. ZEND_ARG_INFO(0, dirname)
  4459. ZEND_END_ARG_INFO()
  4460. PHAR_ARG_INFO
  4461. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_extract, 0, 0, 1)
  4462. ZEND_ARG_INFO(0, pathto)
  4463. ZEND_ARG_INFO(0, files)
  4464. ZEND_ARG_INFO(0, overwrite)
  4465. ZEND_END_ARG_INFO()
  4466. PHAR_ARG_INFO
  4467. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_addfile, 0, 0, 1)
  4468. ZEND_ARG_INFO(0, filename)
  4469. ZEND_ARG_INFO(0, localname)
  4470. ZEND_END_ARG_INFO()
  4471. PHAR_ARG_INFO
  4472. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_fromstring, 0, 0, 1)
  4473. ZEND_ARG_INFO(0, localname)
  4474. ZEND_ARG_INFO(0, contents)
  4475. ZEND_END_ARG_INFO()
  4476. PHAR_ARG_INFO
  4477. ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_isff, 0, 0, 1)
  4478. ZEND_ARG_INFO(0, fileformat)
  4479. ZEND_END_ARG_INFO()
  4480. #endif /* HAVE_SPL */
  4481. zend_function_entry php_archive_methods[] = {
  4482. #if !HAVE_SPL
  4483. PHP_ME(Phar, __construct, arginfo_phar___construct, ZEND_ACC_PRIVATE)
  4484. #else
  4485. PHP_ME(Phar, __construct, arginfo_phar___construct, ZEND_ACC_PUBLIC)
  4486. PHP_ME(Phar, __destruct, NULL, ZEND_ACC_PUBLIC)
  4487. PHP_ME(Phar, addEmptyDir, arginfo_phar_emptydir, ZEND_ACC_PUBLIC)
  4488. PHP_ME(Phar, addFile, arginfo_phar_addfile, ZEND_ACC_PUBLIC)
  4489. PHP_ME(Phar, addFromString, arginfo_phar_fromstring, ZEND_ACC_PUBLIC)
  4490. PHP_ME(Phar, buildFromDirectory, arginfo_phar_fromdir, ZEND_ACC_PUBLIC)
  4491. PHP_ME(Phar, buildFromIterator, arginfo_phar_build, ZEND_ACC_PUBLIC)
  4492. PHP_ME(Phar, compressFiles, arginfo_phar_comp, ZEND_ACC_PUBLIC)
  4493. PHP_ME(Phar, decompressFiles, NULL, ZEND_ACC_PUBLIC)
  4494. PHP_ME(Phar, compress, arginfo_phar_comps, ZEND_ACC_PUBLIC)
  4495. PHP_ME(Phar, decompress, arginfo_phar_decomp, ZEND_ACC_PUBLIC)
  4496. PHP_ME(Phar, convertToExecutable, arginfo_phar_conv, ZEND_ACC_PUBLIC)
  4497. PHP_ME(Phar, convertToData, arginfo_phar_conv, ZEND_ACC_PUBLIC)
  4498. PHP_ME(Phar, copy, arginfo_phar_copy, ZEND_ACC_PUBLIC)
  4499. PHP_ME(Phar, count, NULL, ZEND_ACC_PUBLIC)
  4500. PHP_ME(Phar, delete, arginfo_phar_delete, ZEND_ACC_PUBLIC)
  4501. PHP_ME(Phar, delMetadata, NULL, ZEND_ACC_PUBLIC)
  4502. PHP_ME(Phar, extractTo, arginfo_phar_extract, ZEND_ACC_PUBLIC)
  4503. PHP_ME(Phar, getAlias, NULL, ZEND_ACC_PUBLIC)
  4504. PHP_ME(Phar, getPath, NULL, ZEND_ACC_PUBLIC)
  4505. PHP_ME(Phar, getMetadata, NULL, ZEND_ACC_PUBLIC)
  4506. PHP_ME(Phar, getModified, NULL, ZEND_ACC_PUBLIC)
  4507. PHP_ME(Phar, getSignature, NULL, ZEND_ACC_PUBLIC)
  4508. PHP_ME(Phar, getStub, NULL, ZEND_ACC_PUBLIC)
  4509. PHP_ME(Phar, getVersion, NULL, ZEND_ACC_PUBLIC)
  4510. PHP_ME(Phar, hasMetadata, NULL, ZEND_ACC_PUBLIC)
  4511. PHP_ME(Phar, isBuffering, NULL, ZEND_ACC_PUBLIC)
  4512. PHP_ME(Phar, isCompressed, NULL, ZEND_ACC_PUBLIC)
  4513. PHP_ME(Phar, isFileFormat, arginfo_phar_isff, ZEND_ACC_PUBLIC)
  4514. PHP_ME(Phar, isWritable, NULL, ZEND_ACC_PUBLIC)
  4515. PHP_ME(Phar, offsetExists, arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
  4516. PHP_ME(Phar, offsetGet, arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
  4517. PHP_ME(Phar, offsetSet, arginfo_phar_offsetSet, ZEND_ACC_PUBLIC)
  4518. PHP_ME(Phar, offsetUnset, arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
  4519. PHP_ME(Phar, setAlias, arginfo_phar_setAlias, ZEND_ACC_PUBLIC)
  4520. PHP_ME(Phar, setDefaultStub, arginfo_phar_createDS, ZEND_ACC_PUBLIC)
  4521. PHP_ME(Phar, setMetadata, arginfo_phar_setMetadata, ZEND_ACC_PUBLIC)
  4522. PHP_ME(Phar, setSignatureAlgorithm, arginfo_phar_setSigAlgo, ZEND_ACC_PUBLIC)
  4523. PHP_ME(Phar, setStub, arginfo_phar_setStub, ZEND_ACC_PUBLIC)
  4524. PHP_ME(Phar, startBuffering, NULL, ZEND_ACC_PUBLIC)
  4525. PHP_ME(Phar, stopBuffering, NULL, ZEND_ACC_PUBLIC)
  4526. #endif
  4527. /* static member functions */
  4528. PHP_ME(Phar, apiVersion, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
  4529. PHP_ME(Phar, canCompress, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
  4530. PHP_ME(Phar, canWrite, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
  4531. PHP_ME(Phar, createDefaultStub, arginfo_phar_createDS, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
  4532. PHP_ME(Phar, getSupportedCompression,NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
  4533. PHP_ME(Phar, getSupportedSignatures,NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
  4534. PHP_ME(Phar, interceptFileFuncs, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
  4535. PHP_ME(Phar, isValidPharFilename, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
  4536. PHP_ME(Phar, loadPhar, arginfo_phar_loadPhar, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
  4537. PHP_ME(Phar, mapPhar, arginfo_phar_mapPhar, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
  4538. PHP_ME(Phar, running, arginfo_phar_running, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
  4539. PHP_ME(Phar, mount, arginfo_phar_mount, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
  4540. PHP_ME(Phar, mungServer, arginfo_phar_mungServer, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
  4541. PHP_ME(Phar, unlinkArchive, arginfo_phar_ua, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
  4542. PHP_ME(Phar, webPhar, arginfo_phar_webPhar, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
  4543. {NULL, NULL, NULL}
  4544. };
  4545. #if HAVE_SPL
  4546. PHAR_ARG_INFO
  4547. ZEND_BEGIN_ARG_INFO_EX(arginfo_entry___construct, 0, 0, 1)
  4548. ZEND_ARG_INFO(0, filename)
  4549. ZEND_END_ARG_INFO()
  4550. PHAR_ARG_INFO
  4551. ZEND_BEGIN_ARG_INFO_EX(arginfo_entry_chmod, 0, 0, 1)
  4552. ZEND_ARG_INFO(0, perms)
  4553. ZEND_END_ARG_INFO()
  4554. zend_function_entry php_entry_methods[] = {
  4555. PHP_ME(PharFileInfo, __construct, arginfo_entry___construct, ZEND_ACC_PUBLIC)
  4556. PHP_ME(PharFileInfo, __destruct, NULL, ZEND_ACC_PUBLIC)
  4557. PHP_ME(PharFileInfo, chmod, arginfo_entry_chmod, ZEND_ACC_PUBLIC)
  4558. PHP_ME(PharFileInfo, compress, arginfo_phar_comp, ZEND_ACC_PUBLIC)
  4559. PHP_ME(PharFileInfo, decompress, NULL, ZEND_ACC_PUBLIC)
  4560. PHP_ME(PharFileInfo, delMetadata, NULL, ZEND_ACC_PUBLIC)
  4561. PHP_ME(PharFileInfo, getCompressedSize, NULL, ZEND_ACC_PUBLIC)
  4562. PHP_ME(PharFileInfo, getCRC32, NULL, ZEND_ACC_PUBLIC)
  4563. PHP_ME(PharFileInfo, getContent, NULL, ZEND_ACC_PUBLIC)
  4564. PHP_ME(PharFileInfo, getMetadata, NULL, ZEND_ACC_PUBLIC)
  4565. PHP_ME(PharFileInfo, getPharFlags, NULL, ZEND_ACC_PUBLIC)
  4566. PHP_ME(PharFileInfo, hasMetadata, NULL, ZEND_ACC_PUBLIC)
  4567. PHP_ME(PharFileInfo, isCompressed, arginfo_phar_compo, ZEND_ACC_PUBLIC)
  4568. PHP_ME(PharFileInfo, isCRCChecked, NULL, ZEND_ACC_PUBLIC)
  4569. PHP_ME(PharFileInfo, setMetadata, arginfo_phar_setMetadata, ZEND_ACC_PUBLIC)
  4570. {NULL, NULL, NULL}
  4571. };
  4572. #endif /* HAVE_SPL */
  4573. zend_function_entry phar_exception_methods[] = {
  4574. {NULL, NULL, NULL}
  4575. };
  4576. /* }}} */
  4577. #define REGISTER_PHAR_CLASS_CONST_LONG(class_name, const_name, value) \
  4578. zend_declare_class_constant_long(class_name, const_name, sizeof(const_name)-1, (long)value TSRMLS_CC);
  4579. #if PHP_VERSION_ID < 50200
  4580. # define phar_exception_get_default() zend_exception_get_default()
  4581. #else
  4582. # define phar_exception_get_default() zend_exception_get_default(TSRMLS_C)
  4583. #endif
  4584. void phar_object_init(TSRMLS_D) /* {{{ */
  4585. {
  4586. zend_class_entry ce;
  4587. INIT_CLASS_ENTRY(ce, "PharException", phar_exception_methods);
  4588. phar_ce_PharException = zend_register_internal_class_ex(&ce, phar_exception_get_default(), NULL TSRMLS_CC);
  4589. #if HAVE_SPL
  4590. INIT_CLASS_ENTRY(ce, "Phar", php_archive_methods);
  4591. phar_ce_archive = zend_register_internal_class_ex(&ce, spl_ce_RecursiveDirectoryIterator, NULL TSRMLS_CC);
  4592. zend_class_implements(phar_ce_archive TSRMLS_CC, 2, spl_ce_Countable, zend_ce_arrayaccess);
  4593. INIT_CLASS_ENTRY(ce, "PharData", php_archive_methods);
  4594. phar_ce_data = zend_register_internal_class_ex(&ce, spl_ce_RecursiveDirectoryIterator, NULL TSRMLS_CC);
  4595. zend_class_implements(phar_ce_data TSRMLS_CC, 2, spl_ce_Countable, zend_ce_arrayaccess);
  4596. INIT_CLASS_ENTRY(ce, "PharFileInfo", php_entry_methods);
  4597. phar_ce_entry = zend_register_internal_class_ex(&ce, spl_ce_SplFileInfo, NULL TSRMLS_CC);
  4598. #else
  4599. INIT_CLASS_ENTRY(ce, "Phar", php_archive_methods);
  4600. phar_ce_archive = zend_register_internal_class(&ce TSRMLS_CC);
  4601. phar_ce_archive->ce_flags |= ZEND_ACC_FINAL_CLASS;
  4602. INIT_CLASS_ENTRY(ce, "PharData", php_archive_methods);
  4603. phar_ce_data = zend_register_internal_class(&ce TSRMLS_CC);
  4604. phar_ce_data->ce_flags |= ZEND_ACC_FINAL_CLASS;
  4605. #endif
  4606. REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "BZ2", PHAR_ENT_COMPRESSED_BZ2)
  4607. REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "GZ", PHAR_ENT_COMPRESSED_GZ)
  4608. REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "NONE", PHAR_ENT_COMPRESSED_NONE)
  4609. REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "PHAR", PHAR_FORMAT_PHAR)
  4610. REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "TAR", PHAR_FORMAT_TAR)
  4611. REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "ZIP", PHAR_FORMAT_ZIP)
  4612. REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "COMPRESSED", PHAR_ENT_COMPRESSION_MASK)
  4613. REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "PHP", PHAR_MIME_PHP)
  4614. REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "PHPS", PHAR_MIME_PHPS)
  4615. REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "MD5", PHAR_SIG_MD5)
  4616. REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "OPENSSL", PHAR_SIG_OPENSSL)
  4617. REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA1", PHAR_SIG_SHA1)
  4618. REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA256", PHAR_SIG_SHA256)
  4619. REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA512", PHAR_SIG_SHA512)
  4620. }
  4621. /* }}} */
  4622. /*
  4623. * Local variables:
  4624. * tab-width: 4
  4625. * c-basic-offset: 4
  4626. * End:
  4627. * vim600: noet sw=4 ts=4 fdm=marker
  4628. * vim<600: noet sw=4 ts=4
  4629. */