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

/ext/phar/phar_object.c

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