PageRenderTime 67ms CodeModel.GetById 31ms 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

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

  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_THROW

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