PageRenderTime 65ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/phar/phar.c

http://github.com/infusion/PHP
C | 3739 lines | 2876 code | 521 blank | 342 comment | 960 complexity | e2d23dc05a55eb47f81518347ab21406 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. /*
  2. +----------------------------------------------------------------------+
  3. | phar php single-file executable PHP extension |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2005-2011 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt. |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Gregory Beaver <cellog@php.net> |
  16. | Marcus Boerger <helly@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id: phar.c 307915 2011-02-01 14:01:00Z iliaa $ */
  20. #define PHAR_MAIN 1
  21. #include "phar_internal.h"
  22. #include "SAPI.h"
  23. #include "func_interceptors.h"
  24. static void destroy_phar_data(void *pDest);
  25. ZEND_DECLARE_MODULE_GLOBALS(phar)
  26. #if PHP_VERSION_ID >= 50300
  27. char *(*phar_save_resolve_path)(const char *filename, int filename_len TSRMLS_DC);
  28. #endif
  29. /**
  30. * set's phar->is_writeable based on the current INI value
  31. */
  32. static int phar_set_writeable_bit(void *pDest, void *argument TSRMLS_DC) /* {{{ */
  33. {
  34. zend_bool keep = *(zend_bool *)argument;
  35. phar_archive_data *phar = *(phar_archive_data **)pDest;
  36. if (!phar->is_data) {
  37. phar->is_writeable = !keep;
  38. }
  39. return ZEND_HASH_APPLY_KEEP;
  40. }
  41. /* }}} */
  42. /* if the original value is 0 (disabled), then allow setting/unsetting at will. Otherwise only allow 1 (enabled), and error on disabling */
  43. ZEND_INI_MH(phar_ini_modify_handler) /* {{{ */
  44. {
  45. zend_bool old, ini;
  46. if (entry->name_length == 14) {
  47. old = PHAR_G(readonly_orig);
  48. } else {
  49. old = PHAR_G(require_hash_orig);
  50. }
  51. if (new_value_length == 2 && !strcasecmp("on", new_value)) {
  52. ini = (zend_bool) 1;
  53. }
  54. else if (new_value_length == 3 && !strcasecmp("yes", new_value)) {
  55. ini = (zend_bool) 1;
  56. }
  57. else if (new_value_length == 4 && !strcasecmp("true", new_value)) {
  58. ini = (zend_bool) 1;
  59. }
  60. else {
  61. ini = (zend_bool) atoi(new_value);
  62. }
  63. /* do not allow unsetting in runtime */
  64. if (stage == ZEND_INI_STAGE_STARTUP) {
  65. if (entry->name_length == 14) {
  66. PHAR_G(readonly_orig) = ini;
  67. } else {
  68. PHAR_G(require_hash_orig) = ini;
  69. }
  70. } else if (old && !ini) {
  71. return FAILURE;
  72. }
  73. if (entry->name_length == 14) {
  74. PHAR_G(readonly) = ini;
  75. if (PHAR_GLOBALS->request_init && PHAR_GLOBALS->phar_fname_map.arBuckets) {
  76. zend_hash_apply_with_argument(&(PHAR_GLOBALS->phar_fname_map), phar_set_writeable_bit, (void *)&ini TSRMLS_CC);
  77. }
  78. } else {
  79. PHAR_G(require_hash) = ini;
  80. }
  81. return SUCCESS;
  82. }
  83. /* }}}*/
  84. /* this global stores the global cached pre-parsed manifests */
  85. HashTable cached_phars;
  86. HashTable cached_alias;
  87. static void phar_split_cache_list(TSRMLS_D) /* {{{ */
  88. {
  89. char *tmp;
  90. char *key, *lasts, *end;
  91. char ds[2];
  92. phar_archive_data *phar;
  93. uint i = 0;
  94. if (!PHAR_GLOBALS->cache_list || !(PHAR_GLOBALS->cache_list[0])) {
  95. return;
  96. }
  97. ds[0] = DEFAULT_DIR_SEPARATOR;
  98. ds[1] = '\0';
  99. tmp = estrdup(PHAR_GLOBALS->cache_list);
  100. /* fake request startup */
  101. PHAR_GLOBALS->request_init = 1;
  102. if (zend_hash_init(&EG(regular_list), 0, NULL, NULL, 0) == SUCCESS) {
  103. EG(regular_list).nNextFreeElement=1; /* we don't want resource id 0 */
  104. }
  105. PHAR_G(has_bz2) = zend_hash_exists(&module_registry, "bz2", sizeof("bz2"));
  106. PHAR_G(has_zlib) = zend_hash_exists(&module_registry, "zlib", sizeof("zlib"));
  107. /* these two are dummies and will be destroyed later */
  108. zend_hash_init(&cached_phars, sizeof(phar_archive_data*), zend_get_hash_value, destroy_phar_data, 1);
  109. zend_hash_init(&cached_alias, sizeof(phar_archive_data*), zend_get_hash_value, NULL, 1);
  110. /* these two are real and will be copied over cached_phars/cached_alias later */
  111. zend_hash_init(&(PHAR_GLOBALS->phar_fname_map), sizeof(phar_archive_data*), zend_get_hash_value, destroy_phar_data, 1);
  112. zend_hash_init(&(PHAR_GLOBALS->phar_alias_map), sizeof(phar_archive_data*), zend_get_hash_value, NULL, 1);
  113. PHAR_GLOBALS->manifest_cached = 1;
  114. PHAR_GLOBALS->persist = 1;
  115. for (key = php_strtok_r(tmp, ds, &lasts);
  116. key;
  117. key = php_strtok_r(NULL, ds, &lasts)) {
  118. end = strchr(key, DEFAULT_DIR_SEPARATOR);
  119. if (end) {
  120. if (SUCCESS == phar_open_from_filename(key, end - key, NULL, 0, 0, &phar, NULL TSRMLS_CC)) {
  121. finish_up:
  122. phar->phar_pos = i++;
  123. php_stream_close(phar->fp);
  124. phar->fp = NULL;
  125. } else {
  126. finish_error:
  127. PHAR_GLOBALS->persist = 0;
  128. PHAR_GLOBALS->manifest_cached = 0;
  129. efree(tmp);
  130. zend_hash_destroy(&(PHAR_G(phar_fname_map)));
  131. PHAR_GLOBALS->phar_fname_map.arBuckets = 0;
  132. zend_hash_destroy(&(PHAR_G(phar_alias_map)));
  133. PHAR_GLOBALS->phar_alias_map.arBuckets = 0;
  134. zend_hash_destroy(&cached_phars);
  135. zend_hash_destroy(&cached_alias);
  136. zend_hash_graceful_reverse_destroy(&EG(regular_list));
  137. memset(&EG(regular_list), 0, sizeof(HashTable));
  138. /* free cached manifests */
  139. PHAR_GLOBALS->request_init = 0;
  140. return;
  141. }
  142. } else {
  143. if (SUCCESS == phar_open_from_filename(key, strlen(key), NULL, 0, 0, &phar, NULL TSRMLS_CC)) {
  144. goto finish_up;
  145. } else {
  146. goto finish_error;
  147. }
  148. }
  149. }
  150. PHAR_GLOBALS->persist = 0;
  151. PHAR_GLOBALS->request_init = 0;
  152. /* destroy dummy values from before */
  153. zend_hash_destroy(&cached_phars);
  154. zend_hash_destroy(&cached_alias);
  155. cached_phars = PHAR_GLOBALS->phar_fname_map;
  156. cached_alias = PHAR_GLOBALS->phar_alias_map;
  157. PHAR_GLOBALS->phar_fname_map.arBuckets = 0;
  158. PHAR_GLOBALS->phar_alias_map.arBuckets = 0;
  159. zend_hash_graceful_reverse_destroy(&EG(regular_list));
  160. memset(&EG(regular_list), 0, sizeof(HashTable));
  161. efree(tmp);
  162. }
  163. /* }}} */
  164. ZEND_INI_MH(phar_ini_cache_list) /* {{{ */
  165. {
  166. PHAR_G(cache_list) = new_value;
  167. if (stage == ZEND_INI_STAGE_STARTUP) {
  168. phar_split_cache_list(TSRMLS_C);
  169. }
  170. return SUCCESS;
  171. }
  172. /* }}} */
  173. PHP_INI_BEGIN()
  174. STD_PHP_INI_BOOLEAN( "phar.readonly", "1", PHP_INI_ALL, phar_ini_modify_handler, readonly, zend_phar_globals, phar_globals)
  175. STD_PHP_INI_BOOLEAN( "phar.require_hash", "1", PHP_INI_ALL, phar_ini_modify_handler, require_hash, zend_phar_globals, phar_globals)
  176. STD_PHP_INI_ENTRY("phar.cache_list", "", PHP_INI_SYSTEM, phar_ini_cache_list, cache_list, zend_phar_globals, phar_globals)
  177. PHP_INI_END()
  178. /**
  179. * When all uses of a phar have been concluded, this frees the manifest
  180. * and the phar slot
  181. */
  182. void phar_destroy_phar_data(phar_archive_data *phar TSRMLS_DC) /* {{{ */
  183. {
  184. if (phar->alias && phar->alias != phar->fname) {
  185. pefree(phar->alias, phar->is_persistent);
  186. phar->alias = NULL;
  187. }
  188. if (phar->fname) {
  189. pefree(phar->fname, phar->is_persistent);
  190. phar->fname = NULL;
  191. }
  192. if (phar->signature) {
  193. pefree(phar->signature, phar->is_persistent);
  194. phar->signature = NULL;
  195. }
  196. if (phar->manifest.arBuckets) {
  197. zend_hash_destroy(&phar->manifest);
  198. phar->manifest.arBuckets = NULL;
  199. }
  200. if (phar->mounted_dirs.arBuckets) {
  201. zend_hash_destroy(&phar->mounted_dirs);
  202. phar->mounted_dirs.arBuckets = NULL;
  203. }
  204. if (phar->virtual_dirs.arBuckets) {
  205. zend_hash_destroy(&phar->virtual_dirs);
  206. phar->virtual_dirs.arBuckets = NULL;
  207. }
  208. if (phar->metadata) {
  209. if (phar->is_persistent) {
  210. if (phar->metadata_len) {
  211. /* for zip comments that are strings */
  212. free(phar->metadata);
  213. } else {
  214. zval_internal_ptr_dtor(&phar->metadata);
  215. }
  216. } else {
  217. zval_ptr_dtor(&phar->metadata);
  218. }
  219. phar->metadata_len = 0;
  220. phar->metadata = 0;
  221. }
  222. if (phar->fp) {
  223. php_stream_close(phar->fp);
  224. phar->fp = 0;
  225. }
  226. if (phar->ufp) {
  227. php_stream_close(phar->ufp);
  228. phar->ufp = 0;
  229. }
  230. pefree(phar, phar->is_persistent);
  231. }
  232. /* }}}*/
  233. /**
  234. * Delete refcount and destruct if needed. On destruct return 1 else 0.
  235. */
  236. int phar_archive_delref(phar_archive_data *phar TSRMLS_DC) /* {{{ */
  237. {
  238. if (phar->is_persistent) {
  239. return 0;
  240. }
  241. if (--phar->refcount < 0) {
  242. if (PHAR_GLOBALS->request_done
  243. || zend_hash_del(&(PHAR_GLOBALS->phar_fname_map), phar->fname, phar->fname_len) != SUCCESS) {
  244. phar_destroy_phar_data(phar TSRMLS_CC);
  245. }
  246. return 1;
  247. } else if (!phar->refcount) {
  248. /* invalidate phar cache */
  249. PHAR_G(last_phar) = NULL;
  250. PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
  251. if (phar->fp && !(phar->flags & PHAR_FILE_COMPRESSION_MASK)) {
  252. /* close open file handle - allows removal or rename of
  253. the file on windows, which has greedy locking
  254. only close if the archive was not already compressed. If it
  255. was compressed, then the fp does not refer to the original file */
  256. php_stream_close(phar->fp);
  257. phar->fp = NULL;
  258. }
  259. if (!zend_hash_num_elements(&phar->manifest)) {
  260. /* this is a new phar that has perhaps had an alias/metadata set, but has never
  261. been flushed */
  262. if (zend_hash_del(&(PHAR_GLOBALS->phar_fname_map), phar->fname, phar->fname_len) != SUCCESS) {
  263. phar_destroy_phar_data(phar TSRMLS_CC);
  264. }
  265. return 1;
  266. }
  267. }
  268. return 0;
  269. }
  270. /* }}}*/
  271. /**
  272. * Destroy phar's in shutdown, here we don't care about aliases
  273. */
  274. static void destroy_phar_data_only(void *pDest) /* {{{ */
  275. {
  276. phar_archive_data *phar_data = *(phar_archive_data **) pDest;
  277. TSRMLS_FETCH();
  278. if (EG(exception) || --phar_data->refcount < 0) {
  279. phar_destroy_phar_data(phar_data TSRMLS_CC);
  280. }
  281. }
  282. /* }}}*/
  283. /**
  284. * Delete aliases to phar's that got kicked out of the global table
  285. */
  286. static int phar_unalias_apply(void *pDest, void *argument TSRMLS_DC) /* {{{ */
  287. {
  288. return *(void**)pDest == argument ? ZEND_HASH_APPLY_REMOVE : ZEND_HASH_APPLY_KEEP;
  289. }
  290. /* }}} */
  291. /**
  292. * Delete aliases to phar's that got kicked out of the global table
  293. */
  294. static int phar_tmpclose_apply(void *pDest TSRMLS_DC) /* {{{ */
  295. {
  296. phar_entry_info *entry = (phar_entry_info *) pDest;
  297. if (entry->fp_type != PHAR_TMP) {
  298. return ZEND_HASH_APPLY_KEEP;
  299. }
  300. if (entry->fp && !entry->fp_refcount) {
  301. php_stream_close(entry->fp);
  302. entry->fp = NULL;
  303. }
  304. return ZEND_HASH_APPLY_KEEP;
  305. }
  306. /* }}} */
  307. /**
  308. * Filename map destructor
  309. */
  310. static void destroy_phar_data(void *pDest) /* {{{ */
  311. {
  312. phar_archive_data *phar_data = *(phar_archive_data **) pDest;
  313. TSRMLS_FETCH();
  314. if (PHAR_GLOBALS->request_ends) {
  315. /* first, iterate over the manifest and close all PHAR_TMP entry fp handles,
  316. this prevents unnecessary unfreed stream resources */
  317. zend_hash_apply(&(phar_data->manifest), phar_tmpclose_apply TSRMLS_CC);
  318. destroy_phar_data_only(pDest);
  319. return;
  320. }
  321. zend_hash_apply_with_argument(&(PHAR_GLOBALS->phar_alias_map), phar_unalias_apply, phar_data TSRMLS_CC);
  322. if (--phar_data->refcount < 0) {
  323. phar_destroy_phar_data(phar_data TSRMLS_CC);
  324. }
  325. }
  326. /* }}}*/
  327. /**
  328. * destructor for the manifest hash, frees each file's entry
  329. */
  330. void destroy_phar_manifest_entry(void *pDest) /* {{{ */
  331. {
  332. phar_entry_info *entry = (phar_entry_info *)pDest;
  333. TSRMLS_FETCH();
  334. if (entry->cfp) {
  335. php_stream_close(entry->cfp);
  336. entry->cfp = 0;
  337. }
  338. if (entry->fp) {
  339. php_stream_close(entry->fp);
  340. entry->fp = 0;
  341. }
  342. if (entry->metadata) {
  343. if (entry->is_persistent) {
  344. if (entry->metadata_len) {
  345. /* for zip comments that are strings */
  346. free(entry->metadata);
  347. } else {
  348. zval_internal_ptr_dtor(&entry->metadata);
  349. }
  350. } else {
  351. zval_ptr_dtor(&entry->metadata);
  352. }
  353. entry->metadata_len = 0;
  354. entry->metadata = 0;
  355. }
  356. if (entry->metadata_str.c) {
  357. smart_str_free(&entry->metadata_str);
  358. entry->metadata_str.c = 0;
  359. }
  360. pefree(entry->filename, entry->is_persistent);
  361. if (entry->link) {
  362. pefree(entry->link, entry->is_persistent);
  363. entry->link = 0;
  364. }
  365. if (entry->tmp) {
  366. pefree(entry->tmp, entry->is_persistent);
  367. entry->tmp = 0;
  368. }
  369. }
  370. /* }}} */
  371. int phar_entry_delref(phar_entry_data *idata TSRMLS_DC) /* {{{ */
  372. {
  373. int ret = 0;
  374. if (idata->internal_file && !idata->internal_file->is_persistent) {
  375. if (--idata->internal_file->fp_refcount < 0) {
  376. idata->internal_file->fp_refcount = 0;
  377. }
  378. if (idata->fp && idata->fp != idata->phar->fp && idata->fp != idata->phar->ufp && idata->fp != idata->internal_file->fp) {
  379. php_stream_close(idata->fp);
  380. }
  381. /* if phar_get_or_create_entry_data returns a sub-directory, we have to free it */
  382. if (idata->internal_file->is_temp_dir) {
  383. destroy_phar_manifest_entry((void *)idata->internal_file);
  384. efree(idata->internal_file);
  385. }
  386. }
  387. phar_archive_delref(idata->phar TSRMLS_CC);
  388. efree(idata);
  389. return ret;
  390. }
  391. /* }}} */
  392. /**
  393. * Removes an entry, either by actually removing it or by marking it.
  394. */
  395. void phar_entry_remove(phar_entry_data *idata, char **error TSRMLS_DC) /* {{{ */
  396. {
  397. phar_archive_data *phar;
  398. phar = idata->phar;
  399. if (idata->internal_file->fp_refcount < 2) {
  400. if (idata->fp && idata->fp != idata->phar->fp && idata->fp != idata->phar->ufp && idata->fp != idata->internal_file->fp) {
  401. php_stream_close(idata->fp);
  402. }
  403. zend_hash_del(&idata->phar->manifest, idata->internal_file->filename, idata->internal_file->filename_len);
  404. idata->phar->refcount--;
  405. efree(idata);
  406. } else {
  407. idata->internal_file->is_deleted = 1;
  408. phar_entry_delref(idata TSRMLS_CC);
  409. }
  410. if (!phar->donotflush) {
  411. phar_flush(phar, 0, 0, 0, error TSRMLS_CC);
  412. }
  413. }
  414. /* }}} */
  415. #define MAPPHAR_ALLOC_FAIL(msg) \
  416. if (fp) {\
  417. php_stream_close(fp);\
  418. }\
  419. if (error) {\
  420. spprintf(error, 0, msg, fname);\
  421. }\
  422. return FAILURE;
  423. #define MAPPHAR_FAIL(msg) \
  424. efree(savebuf);\
  425. if (mydata) {\
  426. phar_destroy_phar_data(mydata TSRMLS_CC);\
  427. }\
  428. if (signature) {\
  429. pefree(signature, PHAR_G(persist));\
  430. }\
  431. MAPPHAR_ALLOC_FAIL(msg)
  432. #ifdef WORDS_BIGENDIAN
  433. # define PHAR_GET_32(buffer, var) \
  434. var = ((((unsigned char*)(buffer))[3]) << 24) \
  435. | ((((unsigned char*)(buffer))[2]) << 16) \
  436. | ((((unsigned char*)(buffer))[1]) << 8) \
  437. | (((unsigned char*)(buffer))[0]); \
  438. (buffer) += 4
  439. # define PHAR_GET_16(buffer, var) \
  440. var = ((((unsigned char*)(buffer))[1]) << 8) \
  441. | (((unsigned char*)(buffer))[0]); \
  442. (buffer) += 2
  443. #else
  444. # define PHAR_GET_32(buffer, var) \
  445. memcpy(&var, buffer, sizeof(var)); \
  446. buffer += 4
  447. # define PHAR_GET_16(buffer, var) \
  448. var = *(php_uint16*)(buffer); \
  449. buffer += 2
  450. #endif
  451. #define PHAR_ZIP_16(var) ((php_uint16)((((php_uint16)var[0]) & 0xff) | \
  452. (((php_uint16)var[1]) & 0xff) << 8))
  453. #define PHAR_ZIP_32(var) ((php_uint32)((((php_uint32)var[0]) & 0xff) | \
  454. (((php_uint32)var[1]) & 0xff) << 8 | \
  455. (((php_uint32)var[2]) & 0xff) << 16 | \
  456. (((php_uint32)var[3]) & 0xff) << 24))
  457. /**
  458. * Open an already loaded phar
  459. */
  460. int phar_open_parsed_phar(char *fname, int fname_len, char *alias, int alias_len, int is_data, int options, phar_archive_data** pphar, char **error TSRMLS_DC) /* {{{ */
  461. {
  462. phar_archive_data *phar;
  463. #ifdef PHP_WIN32
  464. char *unixfname;
  465. #endif
  466. if (error) {
  467. *error = NULL;
  468. }
  469. #ifdef PHP_WIN32
  470. unixfname = estrndup(fname, fname_len);
  471. phar_unixify_path_separators(unixfname, fname_len);
  472. if (SUCCESS == phar_get_archive(&phar, unixfname, fname_len, alias, alias_len, error TSRMLS_CC)
  473. && ((alias && fname_len == phar->fname_len
  474. && !strncmp(unixfname, phar->fname, fname_len)) || !alias)
  475. ) {
  476. phar_entry_info *stub;
  477. efree(unixfname);
  478. #else
  479. if (SUCCESS == phar_get_archive(&phar, fname, fname_len, alias, alias_len, error TSRMLS_CC)
  480. && ((alias && fname_len == phar->fname_len
  481. && !strncmp(fname, phar->fname, fname_len)) || !alias)
  482. ) {
  483. phar_entry_info *stub;
  484. #endif
  485. /* logic above is as follows:
  486. If an explicit alias was requested, ensure the filename passed in
  487. matches the phar's filename.
  488. If no alias was passed in, then it can match either and be valid
  489. */
  490. if (!is_data) {
  491. /* prevent any ".phar" without a stub getting through */
  492. if (!phar->halt_offset && !phar->is_brandnew && (phar->is_tar || phar->is_zip)) {
  493. if (PHAR_G(readonly) && FAILURE == zend_hash_find(&(phar->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1, (void **)&stub)) {
  494. if (error) {
  495. spprintf(error, 0, "'%s' is not a phar archive. Use PharData::__construct() for a standard zip or tar archive", fname);
  496. }
  497. return FAILURE;
  498. }
  499. }
  500. }
  501. if (pphar) {
  502. *pphar = phar;
  503. }
  504. return SUCCESS;
  505. } else {
  506. #ifdef PHP_WIN32
  507. efree(unixfname);
  508. #endif
  509. if (pphar) {
  510. *pphar = NULL;
  511. }
  512. if (phar && error && !(options & REPORT_ERRORS)) {
  513. efree(error);
  514. }
  515. return FAILURE;
  516. }
  517. }
  518. /* }}}*/
  519. /**
  520. * Parse out metadata from the manifest for a single file
  521. *
  522. * Meta-data is in this format:
  523. * [len32][data...]
  524. *
  525. * data is the serialized zval
  526. */
  527. int phar_parse_metadata(char **buffer, zval **metadata, int zip_metadata_len TSRMLS_DC) /* {{{ */
  528. {
  529. const unsigned char *p;
  530. php_uint32 buf_len;
  531. php_unserialize_data_t var_hash;
  532. if (!zip_metadata_len) {
  533. PHAR_GET_32(*buffer, buf_len);
  534. } else {
  535. buf_len = zip_metadata_len;
  536. }
  537. if (buf_len) {
  538. ALLOC_ZVAL(*metadata);
  539. INIT_ZVAL(**metadata);
  540. p = (const unsigned char*) *buffer;
  541. PHP_VAR_UNSERIALIZE_INIT(var_hash);
  542. if (!php_var_unserialize(metadata, &p, p + buf_len, &var_hash TSRMLS_CC)) {
  543. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  544. zval_ptr_dtor(metadata);
  545. *metadata = NULL;
  546. return FAILURE;
  547. }
  548. PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
  549. if (PHAR_G(persist)) {
  550. /* lazy init metadata */
  551. zval_ptr_dtor(metadata);
  552. *metadata = (zval *) pemalloc(buf_len, 1);
  553. memcpy(*metadata, *buffer, buf_len);
  554. *buffer += buf_len;
  555. return SUCCESS;
  556. }
  557. } else {
  558. *metadata = NULL;
  559. }
  560. if (!zip_metadata_len) {
  561. *buffer += buf_len;
  562. }
  563. return SUCCESS;
  564. }
  565. /* }}}*/
  566. /**
  567. * Does not check for a previously opened phar in the cache.
  568. *
  569. * Parse a new one and add it to the cache, returning either SUCCESS or
  570. * FAILURE, and setting pphar to the pointer to the manifest entry
  571. *
  572. * This is used by phar_open_from_filename to process the manifest, but can be called
  573. * directly.
  574. */
  575. static int phar_parse_pharfile(php_stream *fp, char *fname, int fname_len, char *alias, int alias_len, long halt_offset, phar_archive_data** pphar, php_uint32 compression, char **error TSRMLS_DC) /* {{{ */
  576. {
  577. char b32[4], *buffer, *endbuffer, *savebuf;
  578. phar_archive_data *mydata = NULL;
  579. phar_entry_info entry;
  580. php_uint32 manifest_len, manifest_count, manifest_flags, manifest_index, tmp_len, sig_flags;
  581. php_uint16 manifest_ver;
  582. long offset;
  583. int register_alias, sig_len, temp_alias = 0;
  584. char *signature = NULL;
  585. if (pphar) {
  586. *pphar = NULL;
  587. }
  588. if (error) {
  589. *error = NULL;
  590. }
  591. /* check for ?>\n and increment accordingly */
  592. if (-1 == php_stream_seek(fp, halt_offset, SEEK_SET)) {
  593. MAPPHAR_ALLOC_FAIL("cannot seek to __HALT_COMPILER(); location in phar \"%s\"")
  594. }
  595. buffer = b32;
  596. if (3 != php_stream_read(fp, buffer, 3)) {
  597. MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated manifest at stub end)")
  598. }
  599. if ((*buffer == ' ' || *buffer == '\n') && *(buffer + 1) == '?' && *(buffer + 2) == '>') {
  600. int nextchar;
  601. halt_offset += 3;
  602. if (EOF == (nextchar = php_stream_getc(fp))) {
  603. MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated manifest at stub end)")
  604. }
  605. if ((char) nextchar == '\r') {
  606. /* if we have an \r we require an \n as well */
  607. if (EOF == (nextchar = php_stream_getc(fp)) || (char)nextchar != '\n') {
  608. MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated manifest at stub end)")
  609. }
  610. ++halt_offset;
  611. }
  612. if ((char) nextchar == '\n') {
  613. ++halt_offset;
  614. }
  615. }
  616. /* make sure we are at the right location to read the manifest */
  617. if (-1 == php_stream_seek(fp, halt_offset, SEEK_SET)) {
  618. MAPPHAR_ALLOC_FAIL("cannot seek to __HALT_COMPILER(); location in phar \"%s\"")
  619. }
  620. /* read in manifest */
  621. buffer = b32;
  622. if (4 != php_stream_read(fp, buffer, 4)) {
  623. MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated manifest at manifest length)")
  624. }
  625. PHAR_GET_32(buffer, manifest_len);
  626. if (manifest_len > 1048576 * 100) {
  627. /* prevent serious memory issues by limiting manifest to at most 100 MB in length */
  628. MAPPHAR_ALLOC_FAIL("manifest cannot be larger than 100 MB in phar \"%s\"")
  629. }
  630. buffer = (char *)emalloc(manifest_len);
  631. savebuf = buffer;
  632. endbuffer = buffer + manifest_len;
  633. if (manifest_len < 10 || manifest_len != php_stream_read(fp, buffer, manifest_len)) {
  634. MAPPHAR_FAIL("internal corruption of phar \"%s\" (truncated manifest header)")
  635. }
  636. /* extract the number of entries */
  637. PHAR_GET_32(buffer, manifest_count);
  638. if (manifest_count == 0) {
  639. MAPPHAR_FAIL("in phar \"%s\", manifest claims to have zero entries. Phars must have at least 1 entry");
  640. }
  641. /* extract API version, lowest nibble currently unused */
  642. manifest_ver = (((unsigned char)buffer[0]) << 8)
  643. + ((unsigned char)buffer[1]);
  644. buffer += 2;
  645. if ((manifest_ver & PHAR_API_VER_MASK) < PHAR_API_MIN_READ) {
  646. efree(savebuf);
  647. php_stream_close(fp);
  648. if (error) {
  649. spprintf(error, 0, "phar \"%s\" is API version %1.u.%1.u.%1.u, and cannot be processed", fname, manifest_ver >> 12, (manifest_ver >> 8) & 0xF, (manifest_ver >> 4) & 0x0F);
  650. }
  651. return FAILURE;
  652. }
  653. PHAR_GET_32(buffer, manifest_flags);
  654. manifest_flags &= ~PHAR_HDR_COMPRESSION_MASK;
  655. manifest_flags &= ~PHAR_FILE_COMPRESSION_MASK;
  656. /* remember whether this entire phar was compressed with gz/bzip2 */
  657. manifest_flags |= compression;
  658. /* The lowest nibble contains the phar wide flags. The compression flags can */
  659. /* be ignored on reading because it is being generated anyways. */
  660. if (manifest_flags & PHAR_HDR_SIGNATURE) {
  661. char sig_buf[8], *sig_ptr = sig_buf;
  662. off_t read_len;
  663. size_t end_of_phar;
  664. if (-1 == php_stream_seek(fp, -8, SEEK_END)
  665. || (read_len = php_stream_tell(fp)) < 20
  666. || 8 != php_stream_read(fp, sig_buf, 8)
  667. || memcmp(sig_buf+4, "GBMB", 4)) {
  668. efree(savebuf);
  669. php_stream_close(fp);
  670. if (error) {
  671. spprintf(error, 0, "phar \"%s\" has a broken signature", fname);
  672. }
  673. return FAILURE;
  674. }
  675. PHAR_GET_32(sig_ptr, sig_flags);
  676. switch(sig_flags) {
  677. case PHAR_SIG_OPENSSL: {
  678. php_uint32 signature_len;
  679. char *sig;
  680. off_t whence;
  681. /* we store the signature followed by the signature length */
  682. if (-1 == php_stream_seek(fp, -12, SEEK_CUR)
  683. || 4 != php_stream_read(fp, sig_buf, 4)) {
  684. efree(savebuf);
  685. php_stream_close(fp);
  686. if (error) {
  687. spprintf(error, 0, "phar \"%s\" openssl signature length could not be read", fname);
  688. }
  689. return FAILURE;
  690. }
  691. sig_ptr = sig_buf;
  692. PHAR_GET_32(sig_ptr, signature_len);
  693. sig = (char *) emalloc(signature_len);
  694. whence = signature_len + 4;
  695. whence = -whence;
  696. if (-1 == php_stream_seek(fp, whence, SEEK_CUR)
  697. || !(end_of_phar = php_stream_tell(fp))
  698. || signature_len != php_stream_read(fp, sig, signature_len)) {
  699. efree(savebuf);
  700. efree(sig);
  701. php_stream_close(fp);
  702. if (error) {
  703. spprintf(error, 0, "phar \"%s\" openssl signature could not be read", fname);
  704. }
  705. return FAILURE;
  706. }
  707. if (FAILURE == phar_verify_signature(fp, end_of_phar, PHAR_SIG_OPENSSL, sig, signature_len, fname, &signature, &sig_len, error TSRMLS_CC)) {
  708. efree(savebuf);
  709. efree(sig);
  710. php_stream_close(fp);
  711. if (error) {
  712. char *save = *error;
  713. spprintf(error, 0, "phar \"%s\" openssl signature could not be verified: %s", fname, *error);
  714. efree(save);
  715. }
  716. return FAILURE;
  717. }
  718. efree(sig);
  719. }
  720. break;
  721. #if PHAR_HASH_OK
  722. case PHAR_SIG_SHA512: {
  723. unsigned char digest[64];
  724. php_stream_seek(fp, -(8 + 64), SEEK_END);
  725. read_len = php_stream_tell(fp);
  726. if (php_stream_read(fp, (char*)digest, sizeof(digest)) != sizeof(digest)) {
  727. efree(savebuf);
  728. php_stream_close(fp);
  729. if (error) {
  730. spprintf(error, 0, "phar \"%s\" has a broken signature", fname);
  731. }
  732. return FAILURE;
  733. }
  734. if (FAILURE == phar_verify_signature(fp, read_len, PHAR_SIG_SHA512, (char *)digest, 64, fname, &signature, &sig_len, error TSRMLS_CC)) {
  735. efree(savebuf);
  736. php_stream_close(fp);
  737. if (error) {
  738. char *save = *error;
  739. spprintf(error, 0, "phar \"%s\" SHA512 signature could not be verified: %s", fname, *error);
  740. efree(save);
  741. }
  742. return FAILURE;
  743. }
  744. break;
  745. }
  746. case PHAR_SIG_SHA256: {
  747. unsigned char digest[32];
  748. php_stream_seek(fp, -(8 + 32), SEEK_END);
  749. read_len = php_stream_tell(fp);
  750. if (php_stream_read(fp, (char*)digest, sizeof(digest)) != sizeof(digest)) {
  751. efree(savebuf);
  752. php_stream_close(fp);
  753. if (error) {
  754. spprintf(error, 0, "phar \"%s\" has a broken signature", fname);
  755. }
  756. return FAILURE;
  757. }
  758. if (FAILURE == phar_verify_signature(fp, read_len, PHAR_SIG_SHA256, (char *)digest, 32, fname, &signature, &sig_len, error TSRMLS_CC)) {
  759. efree(savebuf);
  760. php_stream_close(fp);
  761. if (error) {
  762. char *save = *error;
  763. spprintf(error, 0, "phar \"%s\" SHA256 signature could not be verified: %s", fname, *error);
  764. efree(save);
  765. }
  766. return FAILURE;
  767. }
  768. break;
  769. }
  770. #else
  771. case PHAR_SIG_SHA512:
  772. case PHAR_SIG_SHA256:
  773. efree(savebuf);
  774. php_stream_close(fp);
  775. if (error) {
  776. spprintf(error, 0, "phar \"%s\" has a unsupported signature", fname);
  777. }
  778. return FAILURE;
  779. #endif
  780. case PHAR_SIG_SHA1: {
  781. unsigned char digest[20];
  782. php_stream_seek(fp, -(8 + 20), SEEK_END);
  783. read_len = php_stream_tell(fp);
  784. if (php_stream_read(fp, (char*)digest, sizeof(digest)) != sizeof(digest)) {
  785. efree(savebuf);
  786. php_stream_close(fp);
  787. if (error) {
  788. spprintf(error, 0, "phar \"%s\" has a broken signature", fname);
  789. }
  790. return FAILURE;
  791. }
  792. if (FAILURE == phar_verify_signature(fp, read_len, PHAR_SIG_SHA1, (char *)digest, 20, fname, &signature, &sig_len, error TSRMLS_CC)) {
  793. efree(savebuf);
  794. php_stream_close(fp);
  795. if (error) {
  796. char *save = *error;
  797. spprintf(error, 0, "phar \"%s\" SHA1 signature could not be verified: %s", fname, *error);
  798. efree(save);
  799. }
  800. return FAILURE;
  801. }
  802. break;
  803. }
  804. case PHAR_SIG_MD5: {
  805. unsigned char digest[16];
  806. php_stream_seek(fp, -(8 + 16), SEEK_END);
  807. read_len = php_stream_tell(fp);
  808. if (php_stream_read(fp, (char*)digest, sizeof(digest)) != sizeof(digest)) {
  809. efree(savebuf);
  810. php_stream_close(fp);
  811. if (error) {
  812. spprintf(error, 0, "phar \"%s\" has a broken signature", fname);
  813. }
  814. return FAILURE;
  815. }
  816. if (FAILURE == phar_verify_signature(fp, read_len, PHAR_SIG_MD5, (char *)digest, 16, fname, &signature, &sig_len, error TSRMLS_CC)) {
  817. efree(savebuf);
  818. php_stream_close(fp);
  819. if (error) {
  820. char *save = *error;
  821. spprintf(error, 0, "phar \"%s\" MD5 signature could not be verified: %s", fname, *error);
  822. efree(save);
  823. }
  824. return FAILURE;
  825. }
  826. break;
  827. }
  828. default:
  829. efree(savebuf);
  830. php_stream_close(fp);
  831. if (error) {
  832. spprintf(error, 0, "phar \"%s\" has a broken or unsupported signature", fname);
  833. }
  834. return FAILURE;
  835. }
  836. } else if (PHAR_G(require_hash)) {
  837. efree(savebuf);
  838. php_stream_close(fp);
  839. if (error) {
  840. spprintf(error, 0, "phar \"%s\" does not have a signature", fname);
  841. }
  842. return FAILURE;
  843. } else {
  844. sig_flags = 0;
  845. sig_len = 0;
  846. }
  847. /* extract alias */
  848. PHAR_GET_32(buffer, tmp_len);
  849. if (buffer + tmp_len > endbuffer) {
  850. MAPPHAR_FAIL("internal corruption of phar \"%s\" (buffer overrun)");
  851. }
  852. if (manifest_len < 10 + tmp_len) {
  853. MAPPHAR_FAIL("internal corruption of phar \"%s\" (truncated manifest header)")
  854. }
  855. /* tmp_len = 0 says alias length is 0, which means the alias is not stored in the phar */
  856. if (tmp_len) {
  857. /* if the alias is stored we enforce it (implicit overrides explicit) */
  858. if (alias && alias_len && (alias_len != (int)tmp_len || strncmp(alias, buffer, tmp_len)))
  859. {
  860. buffer[tmp_len] = '\0';
  861. php_stream_close(fp);
  862. if (signature) {
  863. efree(signature);
  864. }
  865. if (error) {
  866. spprintf(error, 0, "cannot load phar \"%s\" with implicit alias \"%s\" under different alias \"%s\"", fname, buffer, alias);
  867. }
  868. efree(savebuf);
  869. return FAILURE;
  870. }
  871. alias_len = tmp_len;
  872. alias = buffer;
  873. buffer += tmp_len;
  874. register_alias = 1;
  875. } else if (!alias_len || !alias) {
  876. /* if we neither have an explicit nor an implicit alias, we use the filename */
  877. alias = NULL;
  878. alias_len = 0;
  879. register_alias = 0;
  880. } else if (alias_len) {
  881. register_alias = 1;
  882. temp_alias = 1;
  883. }
  884. /* we have 5 32-bit items plus 1 byte at least */
  885. if (manifest_count > ((manifest_len - 10 - tmp_len) / (5 * 4 + 1))) {
  886. /* prevent serious memory issues */
  887. MAPPHAR_FAIL("internal corruption of phar \"%s\" (too many manifest entries for size of manifest)")
  888. }
  889. mydata = pecalloc(1, sizeof(phar_archive_data), PHAR_G(persist));
  890. mydata->is_persistent = PHAR_G(persist);
  891. /* check whether we have meta data, zero check works regardless of byte order */
  892. if (mydata->is_persistent) {
  893. PHAR_GET_32(buffer, mydata->metadata_len);
  894. if (phar_parse_metadata(&buffer, &mydata->metadata, mydata->metadata_len TSRMLS_CC) == FAILURE) {
  895. MAPPHAR_FAIL("unable to read phar metadata in .phar file \"%s\"");
  896. }
  897. } else {
  898. if (phar_parse_metadata(&buffer, &mydata->metadata, 0 TSRMLS_CC) == FAILURE) {
  899. MAPPHAR_FAIL("unable to read phar metadata in .phar file \"%s\"");
  900. }
  901. }
  902. /* set up our manifest */
  903. zend_hash_init(&mydata->manifest, manifest_count,
  904. zend_get_hash_value, destroy_phar_manifest_entry, (zend_bool)mydata->is_persistent);
  905. zend_hash_init(&mydata->mounted_dirs, 5,
  906. zend_get_hash_value, NULL, (zend_bool)mydata->is_persistent);
  907. zend_hash_init(&mydata->virtual_dirs, manifest_count * 2,
  908. zend_get_hash_value, NULL, (zend_bool)mydata->is_persistent);
  909. mydata->fname = pestrndup(fname, fname_len, mydata->is_persistent);
  910. #ifdef PHP_WIN32
  911. phar_unixify_path_separators(mydata->fname, fname_len);
  912. #endif
  913. mydata->fname_len = fname_len;
  914. offset = halt_offset + manifest_len + 4;
  915. memset(&entry, 0, sizeof(phar_entry_info));
  916. entry.phar = mydata;
  917. entry.fp_type = PHAR_FP;
  918. entry.is_persistent = mydata->is_persistent;
  919. for (manifest_index = 0; manifest_index < manifest_count; ++manifest_index) {
  920. if (buffer + 4 > endbuffer) {
  921. MAPPHAR_FAIL("internal corruption of phar \"%s\" (truncated manifest entry)")
  922. }
  923. PHAR_GET_32(buffer, entry.filename_len);
  924. if (entry.filename_len == 0) {
  925. MAPPHAR_FAIL("zero-length filename encountered in phar \"%s\"");
  926. }
  927. if (entry.is_persistent) {
  928. entry.manifest_pos = manifest_index;
  929. }
  930. if (buffer + entry.filename_len + 20 > endbuffer) {
  931. MAPPHAR_FAIL("internal corruption of phar \"%s\" (truncated manifest entry)");
  932. }
  933. if ((manifest_ver & PHAR_API_VER_MASK) >= PHAR_API_MIN_DIR && buffer[entry.filename_len - 1] == '/') {
  934. entry.is_dir = 1;
  935. } else {
  936. entry.is_dir = 0;
  937. }
  938. phar_add_virtual_dirs(mydata, buffer, entry.filename_len TSRMLS_CC);
  939. entry.filename = pestrndup(buffer, entry.filename_len, entry.is_persistent);
  940. buffer += entry.filename_len;
  941. PHAR_GET_32(buffer, entry.uncompressed_filesize);
  942. PHAR_GET_32(buffer, entry.timestamp);
  943. if (offset == halt_offset + (int)manifest_len + 4) {
  944. mydata->min_timestamp = entry.timestamp;
  945. mydata->max_timestamp = entry.timestamp;
  946. } else {
  947. if (mydata->min_timestamp > entry.timestamp) {
  948. mydata->min_timestamp = entry.timestamp;
  949. } else if (mydata->max_timestamp < entry.timestamp) {
  950. mydata->max_timestamp = entry.timestamp;
  951. }
  952. }
  953. PHAR_GET_32(buffer, entry.compressed_filesize);
  954. PHAR_GET_32(buffer, entry.crc32);
  955. PHAR_GET_32(buffer, entry.flags);
  956. if (entry.is_dir) {
  957. entry.filename_len--;
  958. entry.flags |= PHAR_ENT_PERM_DEF_DIR;
  959. }
  960. if (entry.is_persistent) {
  961. PHAR_GET_32(buffer, entry.metadata_len);
  962. if (!entry.metadata_len) buffer -= 4;
  963. if (phar_parse_metadata(&buffer, &entry.metadata, entry.metadata_len TSRMLS_CC) == FAILURE) {
  964. pefree(entry.filename, entry.is_persistent);
  965. MAPPHAR_FAIL("unable to read file metadata in .phar file \"%s\"");
  966. }
  967. } else {
  968. if (phar_parse_metadata(&buffer, &entry.metadata, 0 TSRMLS_CC) == FAILURE) {
  969. pefree(entry.filename, entry.is_persistent);
  970. MAPPHAR_FAIL("unable to read file metadata in .phar file \"%s\"");
  971. }
  972. }
  973. entry.offset = entry.offset_abs = offset;
  974. offset += entry.compressed_filesize;
  975. switch (entry.flags & PHAR_ENT_COMPRESSION_MASK) {
  976. case PHAR_ENT_COMPRESSED_GZ:
  977. if (!PHAR_G(has_zlib)) {
  978. if (entry.metadata) {
  979. if (entry.is_persistent) {
  980. free(entry.metadata);
  981. } else {
  982. zval_ptr_dtor(&entry.metadata);
  983. }
  984. }
  985. pefree(entry.filename, entry.is_persistent);
  986. MAPPHAR_FAIL("zlib extension is required for gz compressed .phar file \"%s\"");
  987. }
  988. break;
  989. case PHAR_ENT_COMPRESSED_BZ2:
  990. if (!PHAR_G(has_bz2)) {
  991. if (entry.metadata) {
  992. if (entry.is_persistent) {
  993. free(entry.metadata);
  994. } else {
  995. zval_ptr_dtor(&entry.metadata);
  996. }
  997. }
  998. pefree(entry.filename, entry.is_persistent);
  999. MAPPHAR_FAIL("bz2 extension is required for bzip2 compressed .phar file \"%s\"");
  1000. }
  1001. break;
  1002. default:
  1003. if (entry.uncompressed_filesize != entry.compressed_filesize) {
  1004. if (entry.metadata) {
  1005. if (entry.is_persistent) {
  1006. free(entry.metadata);
  1007. } else {
  1008. zval_ptr_dtor(&entry.metadata);
  1009. }
  1010. }
  1011. pefree(entry.filename, entry.is_persistent);
  1012. MAPPHAR_FAIL("internal corruption of phar \"%s\" (compressed and uncompressed size does not match for uncompressed entry)");
  1013. }
  1014. break;
  1015. }
  1016. manifest_flags |= (entry.flags & PHAR_ENT_COMPRESSION_MASK);
  1017. /* if signature matched, no need to check CRC32 for each file */
  1018. entry.is_crc_checked = (manifest_flags & PHAR_HDR_SIGNATURE ? 1 : 0);
  1019. phar_set_inode(&entry TSRMLS_CC);
  1020. zend_hash_add(&mydata->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info), NULL);
  1021. }
  1022. snprintf(mydata->version, sizeof(mydata->version), "%u.%u.%u", manifest_ver >> 12, (manifest_ver >> 8) & 0xF, (manifest_ver >> 4) & 0xF);
  1023. mydata->internal_file_start = halt_offset + manifest_len + 4;
  1024. mydata->halt_offset = halt_offset;
  1025. mydata->flags = manifest_flags;
  1026. endbuffer = strrchr(mydata->fname, '/');
  1027. if (endbuffer) {
  1028. mydata->ext = memchr(endbuffer, '.', (mydata->fname + fname_len) - endbuffer);
  1029. if (mydata->ext == endbuffer) {
  1030. mydata->ext = memchr(endbuffer + 1, '.', (mydata->fname + fname_len) - endbuffer - 1);
  1031. }
  1032. if (mydata->ext) {
  1033. mydata->ext_len = (mydata->fname + mydata->fname_len) - mydata->ext;
  1034. }
  1035. }
  1036. mydata->alias = alias ?
  1037. pestrndup(alias, alias_len, mydata->is_persistent) :
  1038. pestrndup(mydata->fname, fname_len, mydata->is_persistent);
  1039. mydata->alias_len = alias ? alias_len : fname_len;
  1040. mydata->sig_flags = sig_flags;
  1041. mydata->fp = fp;
  1042. mydata->sig_len = sig_len;
  1043. mydata->signature = signature;
  1044. phar_request_initialize(TSRMLS_C);
  1045. if (register_alias) {
  1046. phar_archive_data **fd_ptr;
  1047. mydata->is_temporary_alias = temp_alias;
  1048. if (!phar_validate_alias(mydata->alias, mydata->alias_len)) {
  1049. signature = NULL;
  1050. fp = NULL;
  1051. MAPPHAR_FAIL("Cannot open archive \"%s\", invalid alias");
  1052. }
  1053. if (SUCCESS == zend_hash_find(&(PHAR_GLOBALS->phar_alias_map), alias, alias_len, (void **)&fd_ptr)) {
  1054. if (SUCCESS != phar_free_alias(*fd_ptr, alias, alias_len TSRMLS_CC)) {
  1055. signature = NULL;
  1056. fp = NULL;
  1057. MAPPHAR_FAIL("Cannot open archive \"%s\", alias is already in use by existing archive");
  1058. }
  1059. }
  1060. zend_hash_add(&(PHAR_GLOBALS->phar_alias_map), alias, alias_len, (void*)&mydata, sizeof(phar_archive_data*), NULL);
  1061. } else {
  1062. mydata->is_temporary_alias = 1;
  1063. }
  1064. zend_hash_add(&(PHAR_GLOBALS->phar_fname_map), mydata->fname, fname_len, (void*)&mydata, sizeof(phar_archive_data*), NULL);
  1065. efree(savebuf);
  1066. if (pphar) {
  1067. *pphar = mydata;
  1068. }
  1069. return SUCCESS;
  1070. }
  1071. /* }}} */
  1072. /**
  1073. * Create or open a phar for writing
  1074. */
  1075. int phar_open_or_create_filename(char *fname, int fname_len, char *alias, int alias_len, int is_data, int options, phar_archive_data** pphar, char **error TSRMLS_DC) /* {{{ */
  1076. {
  1077. const char *ext_str, *z;
  1078. char *my_error;
  1079. int ext_len;
  1080. phar_archive_data **test, *unused = NULL;
  1081. test = &unused;
  1082. if (error) {
  1083. *error = NULL;
  1084. }
  1085. /* first try to open an existing file */
  1086. if (phar_detect_phar_fname_ext(fname, fname_len, &ext_str, &ext_len, !is_data, 0, 1 TSRMLS_CC) == SUCCESS) {
  1087. goto check_file;
  1088. }
  1089. /* next try to create a new file */
  1090. if (FAILURE == phar_detect_phar_fname_ext(fname, fname_len, &ext_str, &ext_len, !is_data, 1, 1 TSRMLS_CC)) {
  1091. if (error) {
  1092. if (ext_len == -2) {
  1093. spprintf(error, 0, "Cannot create a phar archive from a URL like \"%s\". Phar objects can only be created from local files", fname);
  1094. } else {
  1095. spprintf(error, 0, "Cannot create phar '%s', file extension (or combination) not recognised or the directory does not exist", fname);
  1096. }
  1097. }
  1098. return FAILURE;
  1099. }
  1100. check_file:
  1101. if (phar_open_parsed_phar(fname, fname_len, alias, alias_len, is_data, options, test, &my_error TSRMLS_CC) == SUCCESS) {
  1102. if (pphar) {
  1103. *pphar = *test;
  1104. }
  1105. if ((*test)->is_data && !(*test)->is_tar && !(*test)->is_zip) {
  1106. if (error) {
  1107. spprintf(error, 0, "Cannot open '%s' as a PharData object. Use Phar::__construct() for executable archives", fname);
  1108. }
  1109. return FAILURE;
  1110. }
  1111. if (PHAR_G(readonly) && !(*test)->is_data && ((*test)->is_tar || (*test)->is_zip)) {
  1112. phar_entry_info *stub;
  1113. if (FAILURE == zend_hash_find(&((*test)->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1, (void **)&stub)) {
  1114. spprintf(error, 0, "'%s' is not a phar archive. Use PharData::__construct() for a standard zip or tar archive", fname);
  1115. return FAILURE;
  1116. }
  1117. }
  1118. if (!PHAR_G(readonly) || (*test)->is_data) {
  1119. (*test)->is_writeable = 1;
  1120. }
  1121. return SUCCESS;
  1122. } else if (my_error) {
  1123. if (error) {
  1124. *error = my_error;
  1125. } else {
  1126. efree(my_error);
  1127. }
  1128. return FAILURE;
  1129. }
  1130. if (ext_len > 3 && (z = memchr(ext_str, 'z', ext_len)) && ((ext_str + ext_len) - z >= 2) && !memcmp(z + 1, "ip", 2)) {
  1131. /* assume zip-based phar */
  1132. return phar_open_or_create_zip(fname, fname_len, alias, alias_len, is_data, options, pphar, error TSRMLS_CC);
  1133. }
  1134. if (ext_len > 3 && (z = memchr(ext_str, 't', ext_len)) && ((ext_str + ext_len) - z >= 2) && !memcmp(z + 1, "ar", 2)) {
  1135. /* assume tar-based phar */
  1136. return phar_open_or_create_tar(fname, fname_len, alias, alias_len, is_data, options, pphar, error TSRMLS_CC);
  1137. }
  1138. return phar_create_or_parse_filename(fname, fname_len, alias, alias_len, is_data, options, pphar, error TSRMLS_CC);
  1139. }
  1140. /* }}} */
  1141. int phar_create_or_parse_filename(char *fname, int fname_len, char *alias, int alias_len, int is_data, int options, phar_archive_data** pphar, char **error TSRMLS_DC) /* {{{ */
  1142. {
  1143. phar_archive_data *mydata;
  1144. php_stream *fp;
  1145. char *actual = NULL, *p;
  1146. if (!pphar) {
  1147. pphar = &mydata;
  1148. }
  1149. if (php_check_open_basedir(fname TSRMLS_CC)) {
  1150. return FAILURE;
  1151. }
  1152. /* first open readonly so it won't be created if not present */
  1153. fp = php_stream_open_wrapper(fname, "rb", IGNORE_URL|STREAM_MUST_SEEK|0, &actual);
  1154. if (actual) {
  1155. fname = actual;
  1156. fname_len = strlen(actual);
  1157. }
  1158. if (fp) {
  1159. if (phar_open_from_fp(fp, fname, fname_len, alias, alias_len, options, pphar, is_data, error TSRMLS_CC) == SUCCESS) {
  1160. if ((*pphar)->is_data || !PHAR_G(readonly)) {
  1161. (*pphar)->is_writeable = 1;
  1162. }
  1163. if (actual) {
  1164. efree(actual);
  1165. }
  1166. return SUCCESS;
  1167. } else {
  1168. /* file exists, but is either corrupt or not a phar archive */
  1169. if (actual) {
  1170. efree(actual);
  1171. }
  1172. return FAILURE;
  1173. }
  1174. }
  1175. if (actual) {
  1176. efree(actual);
  1177. }
  1178. if (PHAR_G(readonly) && !is_data) {
  1179. if (options & REPORT_ERRORS) {
  1180. if (error) {
  1181. spprintf(error, 0, "creating archive \"%s\" disabled by the php.ini setting phar.readonly", fname);
  1182. }
  1183. }
  1184. return FAILURE;
  1185. }
  1186. /* set up our manifest */
  1187. mydata = ecalloc(1, sizeof(phar_archive_data));
  1188. mydata->fname = expand_filepath(fname, NULL TSRMLS_CC);
  1189. fname_len = strlen(mydata->fname);
  1190. #ifdef PHP_WIN32
  1191. phar_unixify_path_separators(mydata->fname, fname_len);
  1192. #endif
  1193. p = strrchr(mydata->fname, '/');
  1194. if (p) {
  1195. mydata->ext = memchr(p, '.', (mydata->fname + fname_len) - p);
  1196. if (mydata->ext == p) {
  1197. mydata->ext = memchr(p + 1, '.', (mydata->fname + fname_len) - p - 1);
  1198. }
  1199. if (mydata->ext) {
  1200. mydata->ext_len = (mydata->fname + fname_len) - mydata->ext;
  1201. }
  1202. }
  1203. if (pphar) {
  1204. *pphar = mydata;
  1205. }
  1206. zend_hash_init(&mydata->manifest, sizeof(phar_entry_info),
  1207. zend_get_hash_value, destroy_phar_manifest_entry, 0);
  1208. zend_hash_init(&mydata->mounted_dirs, sizeof(char *),
  1209. zend_get_hash_value, NULL, 0);
  1210. zend_hash_init(&mydata->virtual_dirs, sizeof(char *),
  1211. zend_get_hash_value, NULL, (zend_bool)mydata->is_persistent);
  1212. mydata->fname_len = fname_len;
  1213. snprintf(mydata->version, sizeof(mydata->version), "%s", PHP_PHAR_API_VERSION);
  1214. mydata->is_temporary_alias = alias ? 0 : 1;
  1215. mydata->internal_file_start = -1;
  1216. mydata->fp = NULL;
  1217. mydata->is_writeable = 1;
  1218. mydata->is_brandnew = 1;
  1219. phar_request_initialize(TSRMLS_C);
  1220. zend_hash_add(&(PHAR_GLOBALS->phar_fname_map), mydata->fname, fname_len, (void*)&mydata, sizeof(phar_archive_data*), NULL);
  1221. if (is_data) {
  1222. alias = NULL;
  1223. alias_len = 0;
  1224. mydata->is_data = 1;
  1225. /* assume tar format, PharData can specify other */
  1226. mydata->is_tar = 1;
  1227. } else {
  1228. phar_archive_data **fd_ptr;
  1229. if (alias && SUCCESS == zend_hash_find(&(PHAR_GLOBALS->phar_alias_map), alias, alias_len, (void **)&fd_ptr)) {
  1230. if (SUCCESS != phar_free_alias(*fd_ptr, alias, alias_len TSRMLS_CC)) {
  1231. if (error) {
  1232. spprintf(error, 4096, "phar error: phar \"%s\" cannot set alias \"%s\", already in use by another phar archive", mydata->fname, alias);
  1233. }
  1234. zend_hash_del(&(PHAR_GLOBALS->phar_fname_map), mydata->fname, fname_len);
  1235. if (pphar) {
  1236. *pphar = NULL;
  1237. }
  1238. return FAILURE;
  1239. }
  1240. }
  1241. mydata->alias = alias ? estrndup(alias, alias_len) : estrndup(mydata->fname, fname_len);
  1242. mydata->alias_len = alias ? alias_len : fname_len;
  1243. }
  1244. if (alias_len && alias) {
  1245. if (FAILURE == zend_hash_add(&(PHAR_GLOBALS->phar_alias_map), alias, alias_len, (void*)&mydata, sizeof(phar_archive_data*), NULL)) {
  1246. if (options & REPORT_ERRORS) {
  1247. if (error) {
  1248. spprintf(error, 0, "archive \"%s\" cannot be associated with alias \"%s\", already in use", fname, alias);
  1249. }
  1250. }
  1251. zend_hash_del(&(PHAR_GLOBALS->phar_fname_map), mydata->fname, fname_len);
  1252. if (pphar) {
  1253. *pphar = NULL;
  1254. }
  1255. return FAILURE;
  1256. }
  1257. }
  1258. return SUCCESS;
  1259. }
  1260. /* }}}*/
  1261. /**
  1262. * Return an already opened filename.
  1263. *
  1264. * Or scan a phar file for the required __HALT_COMPILER(); ?> token and verify
  1265. * that the manifest is proper, then pass it to phar_parse_pharfile(). SUCCESS
  1266. * or FAILURE is returned and pphar is set to a pointer to the phar's manifest
  1267. */
  1268. int phar_open_from_filename(char *fname, int fname_len, char *alias, int alias_len, int options, phar_archive_data** pphar, char **error TSRMLS_DC) /* {{{ */
  1269. {
  1270. php_stream *fp;
  1271. char *actual;
  1272. int ret, is_data = 0;
  1273. if (error) {
  1274. *error = NULL;
  1275. }
  1276. if (!strstr(fname, ".phar")) {
  1277. is_data = 1;
  1278. }
  1279. if (phar_open_parsed_phar(fname, fname_len, alias, alias_len, is_data, options, pphar, error TSRMLS_CC) == SUCCESS) {
  1280. return SUCCESS;
  1281. } else if (error && *error) {
  1282. return FAILURE;
  1283. }
  1284. if (php_check_open_basedir(fname TSRMLS_CC)) {
  1285. return FAILURE;
  1286. }
  1287. fp = php_stream_open_wrapper(fname, "rb", IGNORE_URL|STREAM_MUST_SEEK, &actual);
  1288. if (!fp) {
  1289. if (options & REPORT_ERRORS) {
  1290. if (error) {
  1291. spprintf(error, 0, "unable to open phar for reading \"%s\"", fname);
  1292. }
  1293. }
  1294. if (actual) {
  1295. efree(actual);
  1296. }
  1297. return FAILURE;
  1298. }
  1299. if (actual) {
  1300. fname = actual;
  1301. fname_len = strlen(actual);
  1302. }
  1303. ret = phar_open_from_fp(fp, fname, fname_len, alias, alias_len, options, pphar, is_data, error TSRMLS_CC);
  1304. if (actual) {
  1305. efree(actual);
  1306. }
  1307. return ret;
  1308. }
  1309. /* }}}*/
  1310. static inline char *phar_strnstr(const char *buf, int buf_len, const char *search, int search_len) /* {{{ */
  1311. {
  1312. const char *c;
  1313. int so_far = 0;
  1314. if (buf_len < search_len) {
  1315. return NULL;
  1316. }
  1317. c = buf - 1;
  1318. do {
  1319. if (!(c = memchr(c + 1, search[0], buf_len - search_len - so_far))) {
  1320. return (char *) NULL;
  1321. }
  1322. so_far = c - buf;
  1323. if (so_far >= (buf_len - search_len)) {
  1324. return (char *) NULL;
  1325. }
  1326. if (!memcmp(c, search, search_len)) {
  1327. return (char *) c;
  1328. }
  1329. } while (1);
  1330. }
  1331. /* }}} */
  1332. /**
  1333. * Scan an open fp for the required __HALT_COMPILER(); ?> token and verify
  1334. * that the manifest is proper, then pass it to phar_parse_pharfile(). SUCCESS
  1335. * or FAILURE is returned and pphar is set to a pointer to the phar's manifest
  1336. */
  1337. static int phar_open_from_fp(php_stream* fp, char *fname, int fname_len, char *alias, int alias_len, int options, phar_archive_data** pphar, int is_data, char **error TSRMLS_DC) /* {{{ */
  1338. {
  1339. const char token[] = "__HALT_COMPILER();";
  1340. const char zip_magic[] = "PK\x03\x04";
  1341. const char gz_magic[] = "\x1f\x8b\x08";
  1342. const char bz_magic[] = "BZh";
  1343. char *pos, buffer[1024 + sizeof(token)], test = '\0';
  1344. const long readsize = sizeof(buffer) - sizeof(token);
  1345. const long tokenlen = sizeof(token) - 1;
  1346. long halt_offset;
  1347. size_t got;
  1348. php_uint32 compression = PHAR_FILE_COMPRESSED_NONE;
  1349. if (error) {
  1350. *error = NULL;
  1351. }
  1352. if (-1 == php_stream_rewind(fp)) {
  1353. MAPPHAR_ALLOC_FAIL("cannot rewind phar \"%s\"")
  1354. }
  1355. buffer[sizeof(buffer)-1] = '\0';
  1356. memset(buffer, 32, sizeof(token));
  1357. halt_offset = 0;
  1358. /* Maybe it's better to compile the file instead of just searching, */
  1359. /* but we only want the offset. So we want a .re scanner to find it. */
  1360. while(!php_stream_eof(fp)) {
  1361. if ((got = php_stream_read(fp, buffer+tokenlen, readsize)) < (size_t) tokenlen) {
  1362. MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated entry)")
  1363. }
  1364. if (!test) {
  1365. test = '\1';
  1366. pos = buffer+tokenlen;
  1367. if (!memcmp(pos, gz_magic, 3)) {
  1368. char err = 0;
  1369. php_stream_filter *filter;
  1370. php_stream *temp;
  1371. /* to properly decompress, we have to tell zlib to look for a zlib or gzip header */
  1372. zval filterparams;
  1373. if (!PHAR_G(has_zlib)) {
  1374. MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\" to temporary file, enable zlib extension in php.ini")
  1375. }
  1376. array_init(&filterparams);
  1377. /* this is defined in zlib's zconf.h */
  1378. #ifndef MAX_WBITS
  1379. #define MAX_WBITS 15
  1380. #endif
  1381. add_assoc_long(&filterparams, "window", MAX_WBITS + 32);
  1382. /* entire file is gzip-compressed, uncompress to temporary file */
  1383. if (!(temp = php_stream_fopen_tmpfile())) {
  1384. MAPPHAR_ALLOC_FAIL("unable to create temporary file for decompression of gzipped phar archive \"%s\"")
  1385. }
  1386. php_stream_rewind(fp);
  1387. filter = php_stream_filter_create("zlib.inflate", &filterparams, php_stream_is_persistent(fp) TSRMLS_CC);
  1388. if (!filter) {
  1389. err = 1;
  1390. add_assoc_long(&filterparams, "window", MAX_WBITS);
  1391. filter = php_stream_filter_create("zlib.inflate", &filterparams, php_stream_is_persistent(fp) TSRMLS_CC);
  1392. zval_dtor(&filterparams);
  1393. if (!filter) {
  1394. php_stream_close(temp);
  1395. MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\", ext/zlib is buggy in PHP versions older than 5.2.6")
  1396. }
  1397. } else {
  1398. zval_dtor(&filterparams);
  1399. }
  1400. php_stream_filter_append(&temp->writefilters, filter);
  1401. if (SUCCESS != phar_stream_copy_to_stream(fp, temp, PHP_STREAM_COPY_ALL, NULL)) {
  1402. if (err) {
  1403. php_stream_close(temp);
  1404. MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\", ext/zlib is buggy in PHP versions older than 5.2.6")
  1405. }
  1406. php_stream_close(temp);
  1407. MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\" to temporary file")
  1408. }
  1409. php_stream_filter_flush(filter, 1);
  1410. php_stream_filter_remove(filter, 1 TSRMLS_CC);
  1411. php_stream_close(fp);
  1412. fp = temp;
  1413. php_stream_rewind(fp);
  1414. compression = PHAR_FILE_COMPRESSED_GZ;
  1415. /* now, start over */
  1416. test = '\0';
  1417. continue;
  1418. } else if (!memcmp(pos, bz_magic, 3)) {
  1419. php_stream_filter *filter;
  1420. php_stream *temp;
  1421. if (!PHAR_G(has_bz2)) {
  1422. MAPPHAR_ALLOC_FAIL("unable to decompress bzipped phar archive \"%s\" to temporary file, enable bz2 extension in php.ini")
  1423. }
  1424. /* entire file is bzip-compressed, uncompress to temporary file */
  1425. if (!(temp = php_stream_fopen_tmpfile())) {
  1426. MAPPHAR_ALLOC_FAIL("unable to create temporary file for decompression of bzipped phar archive \"%s\"")
  1427. }
  1428. php_stream_rewind(fp);
  1429. filter = php_stream_filter_create("bzip2.decompress", NULL, php_stream_is_persistent(fp) TSRMLS_CC);
  1430. if (!filter) {
  1431. php_stream_close(temp);
  1432. MAPPHAR_ALLOC_FAIL("unable to decompress bzipped phar archive \"%s\", filter creation failed")
  1433. }
  1434. php_stream_filter_append(&temp->writefilters, filter);
  1435. if (SUCCESS != phar_stream_copy_to_stream(fp, temp, PHP_STREAM_COPY_ALL, NULL)) {
  1436. php_stream_close(temp);
  1437. MAPPHAR_ALLOC_FAIL("unable to decompress bzipped phar archive \"%s\" to temporary file")
  1438. }
  1439. php_stream_filter_flush(filter, 1);
  1440. php_stream_filter_remove(filter, 1 TSRMLS_CC);
  1441. php_stream_close(fp);
  1442. fp = temp;
  1443. php_stream_rewind(fp);
  1444. compression = PHAR_FILE_COMPRESSED_BZ2;
  1445. /* now, start over */
  1446. test = '\0';
  1447. continue;
  1448. }
  1449. if (!memcmp(pos, zip_magic, 4)) {
  1450. php_stream_seek(fp, 0, SEEK_END);
  1451. return phar_parse_zipfile(fp, fname, fname_len, alias, alias_len, pphar, error TSRMLS_CC);
  1452. }
  1453. if (got > 512) {
  1454. if (phar_is_tar(pos, fname)) {
  1455. php_stream_rewind(fp);
  1456. return phar_parse_tarfile(fp, fname, fname_len, alias, alias_len, pphar, is_data, compression, error TSRMLS_CC);
  1457. }
  1458. }
  1459. }
  1460. if (got > 0 && (pos = phar_strnstr(buffer, got + sizeof(token), token, sizeof(token)-1)) != NULL) {
  1461. halt_offset += (pos - buffer); /* no -tokenlen+tokenlen here */
  1462. return phar_parse_pharfile(fp, fname, fname_len, alias, alias_len, halt_offset, pphar, compression, error TSRMLS_CC);
  1463. }
  1464. halt_offset += got;
  1465. memmove(buffer, buffer + tokenlen, got + 1);
  1466. }
  1467. MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (__HALT_COMPILER(); not found)")
  1468. }
  1469. /* }}} */
  1470. /*
  1471. * given the location of the file extension and the start of the file path,
  1472. * determine the end of the portion of the path (i.e. /path/to/file.ext/blah
  1473. * grabs "/path/to/file.ext" as does the straight /path/to/file.ext),
  1474. * stat it to determine if it exists.
  1475. * if so, check to see if it is a directory and fail if so
  1476. * if not, check to see if its dirname() exists (i.e. "/path/to") and is a directory
  1477. * succeed if we are creating the file, otherwise fail.
  1478. */
  1479. static int phar_analyze_path(const char *fname, const char *ext, int ext_len, int for_create TSRMLS_DC) /* {{{ */
  1480. {
  1481. php_stream_statbuf ssb;
  1482. char *realpath, old, *a = (char *)(ext + ext_len);
  1483. old = *a;
  1484. *a = '\0';
  1485. if ((realpath = expand_filepath(fname, NULL TSRMLS_CC))) {
  1486. #ifdef PHP_WIN32
  1487. phar_unixify_path_separators(realpath, strlen(realpath));
  1488. #endif
  1489. if (zend_hash_exists(&(PHAR_GLOBALS->phar_fname_map), realpath, strlen(realpath))) {
  1490. *a = old;
  1491. efree(realpath);
  1492. return SUCCESS;
  1493. }
  1494. if (PHAR_G(manifest_cached) && zend_hash_exists(&cached_phars, realpath, strlen(realpath))) {
  1495. *a = old;
  1496. efree(realpath);
  1497. return SUCCESS;
  1498. }
  1499. efree(realpath);
  1500. }
  1501. if (SUCCESS == php_stream_stat_path((char *) fname, &ssb)) {
  1502. *a = old;
  1503. if (ssb.sb.st_mode & S_IFDIR) {
  1504. return FAILURE;
  1505. }
  1506. if (for_create == 1) {
  1507. return FAILURE;
  1508. }
  1509. return SUCCESS;
  1510. } else {
  1511. char *slash;
  1512. if (!for_create) {
  1513. *a = old;
  1514. return FAILURE;
  1515. }
  1516. slash = (char *) strrchr(fname, '/');
  1517. *a = old;
  1518. if (slash) {
  1519. old = *slash;
  1520. *slash = '\0';
  1521. }
  1522. if (SUCCESS != php_stream_stat_path((char *) fname, &ssb)) {
  1523. if (slash) {
  1524. *slash = old;
  1525. } else {
  1526. if (!(realpath = expand_filepath(fname, NULL TSRMLS_CC))) {
  1527. return FAILURE;
  1528. }
  1529. #ifdef PHP_WIN32
  1530. phar_unixify_path_separators(realpath, strlen(realpath));
  1531. #endif
  1532. a = strstr(realpath, fname) + ((ext - fname) + ext_len);
  1533. *a = '\0';
  1534. slash = strrchr(realpath, '/');
  1535. if (slash) {
  1536. *slash = '\0';
  1537. } else {
  1538. efree(realpath);
  1539. return FAILURE;
  1540. }
  1541. if (SUCCESS != php_stream_stat_path(realpath, &ssb)) {
  1542. efree(realpath);
  1543. return FAILURE;
  1544. }
  1545. efree(realpath);
  1546. if (ssb.sb.st_mode & S_IFDIR) {
  1547. return SUCCESS;
  1548. }
  1549. }
  1550. return FAILURE;
  1551. }
  1552. if (slash) {
  1553. *slash = old;
  1554. }
  1555. if (ssb.sb.st_mode & S_IFDIR) {
  1556. return SUCCESS;
  1557. }
  1558. return FAILURE;
  1559. }
  1560. }
  1561. /* }}} */
  1562. /* check for ".phar" in extension */
  1563. static int phar_check_str(const char *fname, const char *ext_str, int ext_len, int executable, int for_create TSRMLS_DC) /* {{{ */
  1564. {
  1565. char test[51];
  1566. const char *pos;
  1567. if (ext_len >= 50) {
  1568. return FAILURE;
  1569. }
  1570. if (executable == 1) {
  1571. /* copy "." as well */
  1572. memcpy(test, ext_str - 1, ext_len + 1);
  1573. test[ext_len + 1] = '\0';
  1574. /* executable phars must contain ".phar" as a valid extension (phar://.pharmy/oops is invalid) */
  1575. /* (phar://hi/there/.phar/oops is also invalid) */
  1576. pos = strstr(test, ".phar");
  1577. if (pos && (*(pos - 1) != '/')
  1578. && (pos += 5) && (*pos == '\0' || *pos == '/' || *pos == '.')) {
  1579. return phar_analyze_path(fname, ext_str, ext_len, for_create TSRMLS_CC);
  1580. } else {
  1581. return FAILURE;
  1582. }
  1583. }
  1584. /* data phars need only contain a single non-"." to be valid */
  1585. if (!executable) {
  1586. pos = strstr(ext_str, ".phar");
  1587. if (!(pos && (*(pos - 1) != '/')
  1588. && (pos += 5) && (*pos == '\0' || *pos == '/' || *pos == '.')) && *(ext_str + 1) != '.' && *(ext_str + 1) != '/' && *(ext_str + 1) != '\0') {
  1589. return phar_analyze_path(fname, ext_str, ext_len, for_create TSRMLS_CC);
  1590. }
  1591. } else {
  1592. if (*(ext_str + 1) != '.' && *(ext_str + 1) != '/' && *(ext_str + 1) != '\0') {
  1593. return phar_analyze_path(fname, ext_str, ext_len, for_create TSRMLS_CC);
  1594. }
  1595. }
  1596. return FAILURE;
  1597. }
  1598. /* }}} */
  1599. /*
  1600. * if executable is 1, only returns SUCCESS if the extension is one of the tar/zip .phar extensions
  1601. * if executable is 0, it returns SUCCESS only if the filename does *not* contain ".phar" anywhere, and treats
  1602. * the first extension as the filename extension
  1603. *
  1604. * if an extension is found, it sets ext_str to the location of the file extension in filename,
  1605. * and ext_len to the length of the extension.
  1606. * for urls like "phar://alias/oops" it instead sets ext_len to -1 and returns FAILURE, which tells
  1607. * the calling function to use "alias" as the phar alias
  1608. *
  1609. * the last parameter should be set to tell the thing to assume that filename is the full path, and only to check the
  1610. * extension rules, not to iterate.
  1611. */
  1612. int phar_detect_phar_fname_ext(const char *filename, int filename_len, const char **ext_str, int *ext_len, int executable, int for_create, int is_complete TSRMLS_DC) /* {{{ */
  1613. {
  1614. const char *pos, *slash;
  1615. *ext_str = NULL;
  1616. *ext_len = 0;
  1617. if (!filename_len || filename_len == 1) {
  1618. return FAILURE;
  1619. }
  1620. phar_request_initialize(TSRMLS_C);
  1621. /* first check for alias in first segment */
  1622. pos = memchr(filename, '/', filename_len);
  1623. if (pos && pos != filename) {
  1624. /* check for url like http:// or phar:// */
  1625. if (*(pos - 1) == ':' && (pos - filename) < filename_len - 1 && *(pos + 1) == '/') {
  1626. *ext_len = -2;
  1627. *ext_str = NULL;
  1628. return FAILURE;
  1629. }
  1630. if (zend_hash_exists(&(PHAR_GLOBALS->phar_alias_map), (char *) filename, pos - filename)) {
  1631. *ext_str = pos;
  1632. *ext_len = -1;
  1633. return FAILURE;
  1634. }
  1635. if (PHAR_G(manifest_cached) && zend_hash_exists(&cached_alias, (char *) filename, pos - filename)) {
  1636. *ext_str = pos;
  1637. *ext_len = -1;
  1638. return FAILURE;
  1639. }
  1640. }
  1641. if (zend_hash_num_elements(&(PHAR_GLOBALS->phar_fname_map)) || PHAR_G(manifest_cached)) {
  1642. phar_archive_data **pphar;
  1643. if (is_complete) {
  1644. if (SUCCESS == zend_hash_find(&(PHAR_GLOBALS->phar_fname_map), (char *) filename, filename_len, (void **)&pphar)) {
  1645. *ext_str = filename + (filename_len - (*pphar)->ext_len);
  1646. woohoo:
  1647. *ext_len = (*pphar)->ext_len;
  1648. if (executable == 2) {
  1649. return SUCCESS;
  1650. }
  1651. if (executable == 1 && !(*pphar)->is_data) {
  1652. return SUCCESS;
  1653. }
  1654. if (!executable && (*pphar)->is_data) {
  1655. return SUCCESS;
  1656. }
  1657. return FAILURE;
  1658. }
  1659. if (PHAR_G(manifest_cached) && SUCCESS == zend_hash_find(&cached_phars, (char *) filename, filename_len, (void **)&pphar)) {
  1660. *ext_str = filename + (filename_len - (*pphar)->ext_len);
  1661. goto woohoo;
  1662. }
  1663. } else {
  1664. phar_zstr key;
  1665. char *str_key;
  1666. uint keylen;
  1667. ulong unused;
  1668. zend_hash_internal_pointer_reset(&(PHAR_GLOBALS->phar_fname_map));
  1669. while (FAILURE != zend_hash_has_more_elements(&(PHAR_GLOBALS->phar_fname_map))) {
  1670. if (HASH_KEY_NON_EXISTANT == zend_hash_get_current_key_ex(&(PHAR_GLOBALS->phar_fname_map), &key, &keylen, &unused, 0, NULL)) {
  1671. break;
  1672. }
  1673. PHAR_STR(key, str_key);
  1674. if (keylen > (uint) filename_len) {
  1675. zend_hash_move_forward(&(PHAR_GLOBALS->phar_fname_map));
  1676. PHAR_STR_FREE(str_key);
  1677. continue;
  1678. }
  1679. if (!memcmp(filename, str_key, keylen) && ((uint)filename_len == keylen
  1680. || filename[keylen] == '/' || filename[keylen] == '\0')) {
  1681. PHAR_STR_FREE(str_key);
  1682. if (FAILURE == zend_hash_get_current_data(&(PHAR_GLOBALS->phar_fname_map), (void **) &pphar)) {
  1683. break;
  1684. }
  1685. *ext_str = filename + (keylen - (*pphar)->ext_len);
  1686. goto woohoo;
  1687. }
  1688. PHAR_STR_FREE(str_key);
  1689. zend_hash_move_forward(&(PHAR_GLOBALS->phar_fname_map));
  1690. }
  1691. if (PHAR_G(manifest_cached)) {
  1692. zend_hash_internal_pointer_reset(&cached_phars);
  1693. while (FAILURE != zend_hash_has_more_elements(&cached_phars)) {
  1694. if (HASH_KEY_NON_EXISTANT == zend_hash_get_current_key_ex(&cached_phars, &key, &keylen, &unused, 0, NULL)) {
  1695. break;
  1696. }
  1697. PHAR_STR(key, str_key);
  1698. if (keylen > (uint) filename_len) {
  1699. zend_hash_move_forward(&cached_phars);
  1700. PHAR_STR_FREE(str_key);
  1701. continue;
  1702. }
  1703. if (!memcmp(filename, str_key, keylen) && ((uint)filename_len == keylen
  1704. || filename[keylen] == '/' || filename[keylen] == '\0')) {
  1705. PHAR_STR_FREE(str_key);
  1706. if (FAILURE == zend_hash_get_current_data(&cached_phars, (void **) &pphar)) {
  1707. break;
  1708. }
  1709. *ext_str = filename + (keylen - (*pphar)->ext_len);
  1710. goto woohoo;
  1711. }
  1712. PHAR_STR_FREE(str_key);
  1713. zend_hash_move_forward(&cached_phars);
  1714. }
  1715. }
  1716. }
  1717. }
  1718. pos = memchr(filename + 1, '.', filename_len);
  1719. next_extension:
  1720. if (!pos) {
  1721. return FAILURE;
  1722. }
  1723. while (pos != filename && (*(pos - 1) == '/' || *(pos - 1) == '\0')) {
  1724. pos = memchr(pos + 1, '.', filename_len - (pos - filename) + 1);
  1725. if (!pos) {
  1726. return FAILURE;
  1727. }
  1728. }
  1729. slash = memchr(pos, '/', filename_len - (pos - filename));
  1730. if (!slash) {
  1731. /* this is a url like "phar://blah.phar" with no directory */
  1732. *ext_str = pos;
  1733. *ext_len = strlen(pos);
  1734. /* file extension must contain "phar" */
  1735. switch (phar_check_str(filename, *ext_str, *ext_len, executable, for_create TSRMLS_CC)) {
  1736. case SUCCESS:
  1737. return SUCCESS;
  1738. case FAILURE:
  1739. /* we are at the end of the string, so we fail */
  1740. return FAILURE;
  1741. }
  1742. }
  1743. /* we've found an extension that ends at a directory separator */
  1744. *ext_str = pos;
  1745. *ext_len = slash - pos;
  1746. switch (phar_check_str(filename, *ext_str, *ext_len, executable, for_create TSRMLS_CC)) {
  1747. case SUCCESS:
  1748. return SUCCESS;
  1749. case FAILURE:
  1750. /* look for more extensions */
  1751. pos = strchr(pos + 1, '.');
  1752. if (pos) {
  1753. *ext_str = NULL;
  1754. *ext_len = 0;
  1755. }
  1756. goto next_extension;
  1757. }
  1758. return FAILURE;
  1759. }
  1760. /* }}} */
  1761. static int php_check_dots(const char *element, int n) /* {{{ */
  1762. {
  1763. for(n--; n >= 0; --n) {
  1764. if (element[n] != '.') {
  1765. return 1;
  1766. }
  1767. }
  1768. return 0;
  1769. }
  1770. /* }}} */
  1771. #define IS_DIRECTORY_UP(element, len) \
  1772. (len >= 2 && !php_check_dots(element, len))
  1773. #define IS_DIRECTORY_CURRENT(element, len) \
  1774. (len == 1 && element[0] == '.')
  1775. #define IS_BACKSLASH(c) ((c) == '/')
  1776. #ifdef COMPILE_DL_PHAR
  1777. /* stupid-ass non-extern declaration in tsrm_strtok.h breaks dumbass MS compiler */
  1778. static inline int in_character_class(char ch, const char *delim) /* {{{ */
  1779. {
  1780. while (*delim) {
  1781. if (*delim == ch) {
  1782. return 1;
  1783. }
  1784. ++delim;
  1785. }
  1786. return 0;
  1787. }
  1788. /* }}} */
  1789. char *tsrm_strtok_r(char *s, const char *delim, char **last) /* {{{ */
  1790. {
  1791. char *token;
  1792. if (s == NULL) {
  1793. s = *last;
  1794. }
  1795. while (*s && in_character_class(*s, delim)) {
  1796. ++s;
  1797. }
  1798. if (!*s) {
  1799. return NULL;
  1800. }
  1801. token = s;
  1802. while (*s && !in_character_class(*s, delim)) {
  1803. ++s;
  1804. }
  1805. if (!*s) {
  1806. *last = s;
  1807. } else {
  1808. *s = '\0';
  1809. *last = s + 1;
  1810. }
  1811. return token;
  1812. }
  1813. /* }}} */
  1814. #endif
  1815. /**
  1816. * Remove .. and . references within a phar filename
  1817. */
  1818. char *phar_fix_filepath(char *path, int *new_len, int use_cwd TSRMLS_DC) /* {{{ */
  1819. {
  1820. char newpath[MAXPATHLEN];
  1821. int newpath_len;
  1822. char *ptr;
  1823. char *tok;
  1824. int ptr_length, path_length = *new_len;
  1825. if (PHAR_G(cwd_len) && use_cwd && path_length > 2 && path[0] == '.' && path[1] == '/') {
  1826. newpath_len = PHAR_G(cwd_len);
  1827. memcpy(newpath, PHAR_G(cwd), newpath_len);
  1828. } else {
  1829. newpath[0] = '/';
  1830. newpath_len = 1;
  1831. }
  1832. ptr = path;
  1833. if (*ptr == '/') {
  1834. ++ptr;
  1835. }
  1836. tok = ptr;
  1837. do {
  1838. ptr = memchr(ptr, '/', path_length - (ptr - path));
  1839. } while (ptr && ptr - tok == 0 && *ptr == '/' && ++ptr && ++tok);
  1840. if (!ptr && (path_length - (tok - path))) {
  1841. switch (path_length - (tok - path)) {
  1842. case 1:
  1843. if (*tok == '.') {
  1844. efree(path);
  1845. *new_len = 1;
  1846. return estrndup("/", 1);
  1847. }
  1848. break;
  1849. case 2:
  1850. if (tok[0] == '.' && tok[1] == '.') {
  1851. efree(path);
  1852. *new_len = 1;
  1853. return estrndup("/", 1);
  1854. }
  1855. }
  1856. return path;
  1857. }
  1858. while (ptr) {
  1859. ptr_length = ptr - tok;
  1860. last_time:
  1861. if (IS_DIRECTORY_UP(tok, ptr_length)) {
  1862. #define PREVIOUS newpath[newpath_len - 1]
  1863. while (newpath_len > 1 && !IS_BACKSLASH(PREVIOUS)) {
  1864. newpath_len--;
  1865. }
  1866. if (newpath[0] != '/') {
  1867. newpath[newpath_len] = '\0';
  1868. } else if (newpath_len > 1) {
  1869. --newpath_len;
  1870. }
  1871. } else if (!IS_DIRECTORY_CURRENT(tok, ptr_length)) {
  1872. if (newpath_len > 1) {
  1873. newpath[newpath_len++] = '/';
  1874. memcpy(newpath + newpath_len, tok, ptr_length+1);
  1875. } else {
  1876. memcpy(newpath + newpath_len, tok, ptr_length+1);
  1877. }
  1878. newpath_len += ptr_length;
  1879. }
  1880. if (ptr == path + path_length) {
  1881. break;
  1882. }
  1883. tok = ++ptr;
  1884. do {
  1885. ptr = memchr(ptr, '/', path_length - (ptr - path));
  1886. } while (ptr && ptr - tok == 0 && *ptr == '/' && ++ptr && ++tok);
  1887. if (!ptr && (path_length - (tok - path))) {
  1888. ptr_length = path_length - (tok - path);
  1889. ptr = path + path_length;
  1890. goto last_time;
  1891. }
  1892. }
  1893. efree(path);
  1894. *new_len = newpath_len;
  1895. return estrndup(newpath, newpath_len);
  1896. }
  1897. /* }}} */
  1898. /**
  1899. * Process a phar stream name, ensuring we can handle any of:
  1900. *
  1901. * - whatever.phar
  1902. * - whatever.phar.gz
  1903. * - whatever.phar.bz2
  1904. * - whatever.phar.php
  1905. *
  1906. * Optionally the name might start with 'phar://'
  1907. *
  1908. * This is used by phar_parse_url()
  1909. */
  1910. int phar_split_fname(char *filename, int filename_len, char **arch, int *arch_len, char **entry, int *entry_len, int executable, int for_create TSRMLS_DC) /* {{{ */
  1911. {
  1912. const char *ext_str;
  1913. #ifdef PHP_WIN32
  1914. char *save;
  1915. #endif
  1916. int ext_len, free_filename = 0;
  1917. if (!strncasecmp(filename, "phar://", 7)) {
  1918. filename += 7;
  1919. filename_len -= 7;
  1920. }
  1921. ext_len = 0;
  1922. #ifdef PHP_WIN32
  1923. free_filename = 1;
  1924. save = filename;
  1925. filename = estrndup(filename, filename_len);
  1926. phar_unixify_path_separators(filename, filename_len);
  1927. #endif
  1928. if (phar_detect_phar_fname_ext(filename, filename_len, &ext_str, &ext_len, executable, for_create, 0 TSRMLS_CC) == FAILURE) {
  1929. if (ext_len != -1) {
  1930. if (!ext_str) {
  1931. /* no / detected, restore arch for error message */
  1932. #ifdef PHP_WIN32
  1933. *arch = save;
  1934. #else
  1935. *arch = filename;
  1936. #endif
  1937. }
  1938. if (free_filename) {
  1939. efree(filename);
  1940. }
  1941. return FAILURE;
  1942. }
  1943. ext_len = 0;
  1944. /* no extension detected - instead we are dealing with an alias */
  1945. }
  1946. *arch_len = ext_str - filename + ext_len;
  1947. *arch = estrndup(filename, *arch_len);
  1948. if (ext_str[ext_len]) {
  1949. *entry_len = filename_len - *arch_len;
  1950. *entry = estrndup(ext_str+ext_len, *entry_len);
  1951. #ifdef PHP_WIN32
  1952. phar_unixify_path_separators(*entry, *entry_len);
  1953. #endif
  1954. *entry = phar_fix_filepath(*entry, entry_len, 0 TSRMLS_CC);
  1955. } else {
  1956. *entry_len = 1;
  1957. *entry = estrndup("/", 1);
  1958. }
  1959. if (free_filename) {
  1960. efree(filename);
  1961. }
  1962. return SUCCESS;
  1963. }
  1964. /* }}} */
  1965. /**
  1966. * Invoked when a user calls Phar::mapPhar() from within an executing .phar
  1967. * to set up its manifest directly
  1968. */
  1969. int phar_open_executed_filename(char *alias, int alias_len, char **error TSRMLS_DC) /* {{{ */
  1970. {
  1971. char *fname;
  1972. zval *halt_constant;
  1973. php_stream *fp;
  1974. int fname_len;
  1975. char *actual = NULL;
  1976. int ret;
  1977. if (error) {
  1978. *error = NULL;
  1979. }
  1980. fname = zend_get_executed_filename(TSRMLS_C);
  1981. fname_len = strlen(fname);
  1982. if (phar_open_parsed_phar(fname, fname_len, alias, alias_len, 0, REPORT_ERRORS, NULL, 0 TSRMLS_CC) == SUCCESS) {
  1983. return SUCCESS;
  1984. }
  1985. if (!strcmp(fname, "[no active file]")) {
  1986. if (error) {
  1987. spprintf(error, 0, "cannot initialize a phar outside of PHP execution");
  1988. }
  1989. return FAILURE;
  1990. }
  1991. MAKE_STD_ZVAL(halt_constant);
  1992. if (0 == zend_get_constant("__COMPILER_HALT_OFFSET__", 24, halt_constant TSRMLS_CC)) {
  1993. FREE_ZVAL(halt_constant);
  1994. if (error) {
  1995. spprintf(error, 0, "__HALT_COMPILER(); must be declared in a phar");
  1996. }
  1997. return FAILURE;
  1998. }
  1999. FREE_ZVAL(halt_constant);
  2000. if (php_check_open_basedir(fname TSRMLS_CC)) {
  2001. return FAILURE;
  2002. }
  2003. fp = php_stream_open_wrapper(fname, "rb", IGNORE_URL|STREAM_MUST_SEEK|REPORT_ERRORS, &actual);
  2004. if (!fp) {
  2005. if (error) {
  2006. spprintf(error, 0, "unable to open phar for reading \"%s\"", fname);
  2007. }
  2008. if (actual) {
  2009. efree(actual);
  2010. }
  2011. return FAILURE;
  2012. }
  2013. if (actual) {
  2014. fname = actual;
  2015. fname_len = strlen(actual);
  2016. }
  2017. ret = phar_open_from_fp(fp, fname, fname_len, alias, alias_len, REPORT_ERRORS, NULL, 0, error TSRMLS_CC);
  2018. if (actual) {
  2019. efree(actual);
  2020. }
  2021. return ret;
  2022. }
  2023. /* }}} */
  2024. /**
  2025. * Validate the CRC32 of a file opened from within the phar
  2026. */
  2027. int phar_postprocess_file(phar_entry_data *idata, php_uint32 crc32, char **error, int process_zip TSRMLS_DC) /* {{{ */
  2028. {
  2029. php_uint32 crc = ~0;
  2030. int len = idata->internal_file->uncompressed_filesize;
  2031. php_stream *fp = idata->fp;
  2032. phar_entry_info *entry = idata->internal_file;
  2033. if (error) {
  2034. *error = NULL;
  2035. }
  2036. if (entry->is_zip && process_zip > 0) {
  2037. /* verify local file header */
  2038. phar_zip_file_header local;
  2039. phar_zip_data_desc desc;
  2040. if (SUCCESS != phar_open_archive_fp(idata->phar TSRMLS_CC)) {
  2041. spprintf(error, 0, "phar error: unable to open zip-based phar archive \"%s\" to verify local file header for file \"%s\"", idata->phar->fname, entry->filename);
  2042. return FAILURE;
  2043. }
  2044. php_stream_seek(phar_get_entrypfp(idata->internal_file TSRMLS_CC), entry->header_offset, SEEK_SET);
  2045. if (sizeof(local) != php_stream_read(phar_get_entrypfp(idata->internal_file TSRMLS_CC), (char *) &local, sizeof(local))) {
  2046. spprintf(error, 0, "phar error: internal corruption of zip-based phar \"%s\" (cannot read local file header for file \"%s\")", idata->phar->fname, entry->filename);
  2047. return FAILURE;
  2048. }
  2049. /* check for data descriptor */
  2050. if (((PHAR_ZIP_16(local.flags)) & 0x8) == 0x8) {
  2051. php_stream_seek(phar_get_entrypfp(idata->internal_file TSRMLS_CC),
  2052. entry->header_offset + sizeof(local) +
  2053. PHAR_ZIP_16(local.filename_len) +
  2054. PHAR_ZIP_16(local.extra_len) +
  2055. entry->compressed_filesize, SEEK_SET);
  2056. if (sizeof(desc) != php_stream_read(phar_get_entrypfp(idata->internal_file TSRMLS_CC),
  2057. (char *) &desc, sizeof(desc))) {
  2058. spprintf(error, 0, "phar error: internal corruption of zip-based phar \"%s\" (cannot read local data descriptor for file \"%s\")", idata->phar->fname, entry->filename);
  2059. return FAILURE;
  2060. }
  2061. if (desc.signature[0] == 'P' && desc.signature[1] == 'K') {
  2062. memcpy(&(local.crc32), &(desc.crc32), 12);
  2063. } else {
  2064. /* old data descriptors have no signature */
  2065. memcpy(&(local.crc32), &desc, 12);
  2066. }
  2067. }
  2068. /* verify local header */
  2069. if (entry->filename_len != PHAR_ZIP_16(local.filename_len) || entry->crc32 != PHAR_ZIP_32(local.crc32) || entry->uncompressed_filesize != PHAR_ZIP_32(local.uncompsize) || entry->compressed_filesize != PHAR_ZIP_32(local.compsize)) {
  2070. spprintf(error, 0, "phar error: internal corruption of zip-based phar \"%s\" (local header of file \"%s\" does not match central directory)", idata->phar->fname, entry->filename);
  2071. return FAILURE;
  2072. }
  2073. /* construct actual offset to file start - local extra_len can be different from central extra_len */
  2074. entry->offset = entry->offset_abs =
  2075. sizeof(local) + entry->header_offset + PHAR_ZIP_16(local.filename_len) + PHAR_ZIP_16(local.extra_len);
  2076. if (idata->zero && idata->zero != entry->offset_abs) {
  2077. idata->zero = entry->offset_abs;
  2078. }
  2079. }
  2080. if (process_zip == 1) {
  2081. return SUCCESS;
  2082. }
  2083. php_stream_seek(fp, idata->zero, SEEK_SET);
  2084. while (len--) {
  2085. CRC32(crc, php_stream_getc(fp));
  2086. }
  2087. php_stream_seek(fp, idata->zero, SEEK_SET);
  2088. if (~crc == crc32) {
  2089. entry->is_crc_checked = 1;
  2090. return SUCCESS;
  2091. } else {
  2092. spprintf(error, 0, "phar error: internal corruption of phar \"%s\" (crc32 mismatch on file \"%s\")", idata->phar->fname, entry->filename);
  2093. return FAILURE;
  2094. }
  2095. }
  2096. /* }}} */
  2097. static inline void phar_set_32(char *buffer, int var) /* {{{ */
  2098. {
  2099. #ifdef WORDS_BIGENDIAN
  2100. *((buffer) + 3) = (unsigned char) (((var) >> 24) & 0xFF);
  2101. *((buffer) + 2) = (unsigned char) (((var) >> 16) & 0xFF);
  2102. *((buffer) + 1) = (unsigned char) (((var) >> 8) & 0xFF);
  2103. *((buffer) + 0) = (unsigned char) ((var) & 0xFF);
  2104. #else
  2105. memcpy(buffer, &var, sizeof(var));
  2106. #endif
  2107. } /* }}} */
  2108. static int phar_flush_clean_deleted_apply(void *data TSRMLS_DC) /* {{{ */
  2109. {
  2110. phar_entry_info *entry = (phar_entry_info *)data;
  2111. if (entry->fp_refcount <= 0 && entry->is_deleted) {
  2112. return ZEND_HASH_APPLY_REMOVE;
  2113. } else {
  2114. return ZEND_HASH_APPLY_KEEP;
  2115. }
  2116. }
  2117. /* }}} */
  2118. #include "stub.h"
  2119. char *phar_create_default_stub(const char *index_php, const char *web_index, size_t *len, char **error TSRMLS_DC) /* {{{ */
  2120. {
  2121. char *stub = NULL;
  2122. int index_len, web_len;
  2123. size_t dummy;
  2124. if (!len) {
  2125. len = &dummy;
  2126. }
  2127. if (error) {
  2128. *error = NULL;
  2129. }
  2130. if (!index_php) {
  2131. index_php = "index.php";
  2132. }
  2133. if (!web_index) {
  2134. web_index = "index.php";
  2135. }
  2136. index_len = strlen(index_php);
  2137. web_len = strlen(web_index);
  2138. if (index_len > 400) {
  2139. /* ridiculous size not allowed for index.php startup filename */
  2140. if (error) {
  2141. spprintf(error, 0, "Illegal filename passed in for stub creation, was %d characters long, and only 400 or less is allowed", index_len);
  2142. return NULL;
  2143. }
  2144. }
  2145. if (web_len > 400) {
  2146. /* ridiculous size not allowed for index.php startup filename */
  2147. if (error) {
  2148. spprintf(error, 0, "Illegal web filename passed in for stub creation, was %d characters long, and only 400 or less is allowed", web_len);
  2149. return NULL;
  2150. }
  2151. }
  2152. phar_get_stub(index_php, web_index, len, &stub, index_len+1, web_len+1 TSRMLS_CC);
  2153. return stub;
  2154. }
  2155. /* }}} */
  2156. /**
  2157. * Save phar contents to disk
  2158. *
  2159. * user_stub contains either a string, or a resource pointer, if len is a negative length.
  2160. * user_stub and len should be both 0 if the default or existing stub should be used
  2161. */
  2162. int phar_flush(phar_archive_data *phar, char *user_stub, long len, int convert, char **error TSRMLS_DC) /* {{{ */
  2163. {
  2164. char halt_stub[] = "__HALT_COMPILER();";
  2165. char *newstub, *tmp;
  2166. phar_entry_info *entry, *newentry;
  2167. int halt_offset, restore_alias_len, global_flags = 0, closeoldfile;
  2168. char *pos, has_dirs = 0;
  2169. char manifest[18], entry_buffer[24];
  2170. off_t manifest_ftell;
  2171. long offset;
  2172. size_t wrote;
  2173. php_uint32 manifest_len, mytime, loc, new_manifest_count;
  2174. php_uint32 newcrc32;
  2175. php_stream *file, *oldfile, *newfile, *stubfile;
  2176. php_stream_filter *filter;
  2177. php_serialize_data_t metadata_hash;
  2178. smart_str main_metadata_str = {0};
  2179. int free_user_stub, free_fp = 1, free_ufp = 1;
  2180. if (phar->is_persistent) {
  2181. if (error) {
  2182. spprintf(error, 0, "internal error: attempt to flush cached zip-based phar \"%s\"", phar->fname);
  2183. }
  2184. return EOF;
  2185. }
  2186. if (error) {
  2187. *error = NULL;
  2188. }
  2189. if (!zend_hash_num_elements(&phar->manifest) && !user_stub) {
  2190. return EOF;
  2191. }
  2192. zend_hash_clean(&phar->virtual_dirs);
  2193. if (phar->is_zip) {
  2194. return phar_zip_flush(phar, user_stub, len, convert, error TSRMLS_CC);
  2195. }
  2196. if (phar->is_tar) {
  2197. return phar_tar_flush(phar, user_stub, len, convert, error TSRMLS_CC);
  2198. }
  2199. if (PHAR_G(readonly)) {
  2200. return EOF;
  2201. }
  2202. if (phar->fp && !phar->is_brandnew) {
  2203. oldfile = phar->fp;
  2204. closeoldfile = 0;
  2205. php_stream_rewind(oldfile);
  2206. } else {
  2207. oldfile = php_stream_open_wrapper(phar->fname, "rb", 0, NULL);
  2208. closeoldfile = oldfile != NULL;
  2209. }
  2210. newfile = php_stream_fopen_tmpfile();
  2211. if (!newfile) {
  2212. if (error) {
  2213. spprintf(error, 0, "unable to create temporary file");
  2214. }
  2215. if (closeoldfile) {
  2216. php_stream_close(oldfile);
  2217. }
  2218. return EOF;
  2219. }
  2220. if (user_stub) {
  2221. if (len < 0) {
  2222. /* resource passed in */
  2223. if (!(php_stream_from_zval_no_verify(stubfile, (zval **)user_stub))) {
  2224. if (closeoldfile) {
  2225. php_stream_close(oldfile);
  2226. }
  2227. php_stream_close(newfile);
  2228. if (error) {
  2229. spprintf(error, 0, "unable to access resource to copy stub to new phar \"%s\"", phar->fname);
  2230. }
  2231. return EOF;
  2232. }
  2233. if (len == -1) {
  2234. len = PHP_STREAM_COPY_ALL;
  2235. } else {
  2236. len = -len;
  2237. }
  2238. user_stub = 0;
  2239. #if PHP_MAJOR_VERSION >= 6
  2240. if (!(len = php_stream_copy_to_mem(stubfile, (void **) &user_stub, len, 0)) || !user_stub) {
  2241. #else
  2242. if (!(len = php_stream_copy_to_mem(stubfile, &user_stub, len, 0)) || !user_stub) {
  2243. #endif
  2244. if (closeoldfile) {
  2245. php_stream_close(oldfile);
  2246. }
  2247. php_stream_close(newfile);
  2248. if (error) {
  2249. spprintf(error, 0, "unable to read resource to copy stub to new phar \"%s\"", phar->fname);
  2250. }
  2251. return EOF;
  2252. }
  2253. free_user_stub = 1;
  2254. } else {
  2255. free_user_stub = 0;
  2256. }
  2257. tmp = estrndup(user_stub, len);
  2258. if ((pos = php_stristr(tmp, halt_stub, len, sizeof(halt_stub) - 1)) == NULL) {
  2259. efree(tmp);
  2260. if (closeoldfile) {
  2261. php_stream_close(oldfile);
  2262. }
  2263. php_stream_close(newfile);
  2264. if (error) {
  2265. spprintf(error, 0, "illegal stub for phar \"%s\"", phar->fname);
  2266. }
  2267. if (free_user_stub) {
  2268. efree(user_stub);
  2269. }
  2270. return EOF;
  2271. }
  2272. pos = user_stub + (pos - tmp);
  2273. efree(tmp);
  2274. len = pos - user_stub + 18;
  2275. if ((size_t)len != php_stream_write(newfile, user_stub, len)
  2276. || 5 != php_stream_write(newfile, " ?>\r\n", 5)) {
  2277. if (closeoldfile) {
  2278. php_stream_close(oldfile);
  2279. }
  2280. php_stream_close(newfile);
  2281. if (error) {
  2282. spprintf(error, 0, "unable to create stub from string in new phar \"%s\"", phar->fname);
  2283. }
  2284. if (free_user_stub) {
  2285. efree(user_stub);
  2286. }
  2287. return EOF;
  2288. }
  2289. phar->halt_offset = len + 5;
  2290. if (free_user_stub) {
  2291. efree(user_stub);
  2292. }
  2293. } else {
  2294. size_t written;
  2295. if (!user_stub && phar->halt_offset && oldfile && !phar->is_brandnew) {
  2296. phar_stream_copy_to_stream(oldfile, newfile, phar->halt_offset, &written);
  2297. newstub = NULL;
  2298. } else {
  2299. /* this is either a brand new phar or a default stub overwrite */
  2300. newstub = phar_create_default_stub(NULL, NULL, &(phar->halt_offset), NULL TSRMLS_CC);
  2301. written = php_stream_write(newfile, newstub, phar->halt_offset);
  2302. }
  2303. if (phar->halt_offset != written) {
  2304. if (closeoldfile) {
  2305. php_stream_close(oldfile);
  2306. }
  2307. php_stream_close(newfile);
  2308. if (error) {
  2309. if (newstub) {
  2310. spprintf(error, 0, "unable to create stub in new phar \"%s\"", phar->fname);
  2311. } else {
  2312. spprintf(error, 0, "unable to copy stub of old phar to new phar \"%s\"", phar->fname);
  2313. }
  2314. }
  2315. if (newstub) {
  2316. efree(newstub);
  2317. }
  2318. return EOF;
  2319. }
  2320. if (newstub) {
  2321. efree(newstub);
  2322. }
  2323. }
  2324. manifest_ftell = php_stream_tell(newfile);
  2325. halt_offset = manifest_ftell;
  2326. /* Check whether we can get rid of some of the deleted entries which are
  2327. * unused. However some might still be in use so even after this clean-up
  2328. * we need to skip entries marked is_deleted. */
  2329. zend_hash_apply(&phar->manifest, phar_flush_clean_deleted_apply TSRMLS_CC);
  2330. /* compress as necessary, calculate crcs, serialize meta-data, manifest size, and file sizes */
  2331. main_metadata_str.c = 0;
  2332. if (phar->metadata) {
  2333. PHP_VAR_SERIALIZE_INIT(metadata_hash);
  2334. php_var_serialize(&main_metadata_str, &phar->metadata, &metadata_hash TSRMLS_CC);
  2335. PHP_VAR_SERIALIZE_DESTROY(metadata_hash);
  2336. } else {
  2337. main_metadata_str.len = 0;
  2338. }
  2339. new_manifest_count = 0;
  2340. offset = 0;
  2341. for (zend_hash_internal_pointer_reset(&phar->manifest);
  2342. zend_hash_has_more_elements(&phar->manifest) == SUCCESS;
  2343. zend_hash_move_forward(&phar->manifest)) {
  2344. if (zend_hash_get_current_data(&phar->manifest, (void **)&entry) == FAILURE) {
  2345. continue;
  2346. }
  2347. if (entry->cfp) {
  2348. /* did we forget to get rid of cfp last time? */
  2349. php_stream_close(entry->cfp);
  2350. entry->cfp = 0;
  2351. }
  2352. if (entry->is_deleted || entry->is_mounted) {
  2353. /* remove this from the new phar */
  2354. continue;
  2355. }
  2356. if (!entry->is_modified && entry->fp_refcount) {
  2357. /* open file pointers refer to this fp, do not free the stream */
  2358. switch (entry->fp_type) {
  2359. case PHAR_FP:
  2360. free_fp = 0;
  2361. break;
  2362. case PHAR_UFP:
  2363. free_ufp = 0;
  2364. default:
  2365. break;
  2366. }
  2367. }
  2368. /* after excluding deleted files, calculate manifest size in bytes and number of entries */
  2369. ++new_manifest_count;
  2370. phar_add_virtual_dirs(phar, entry->filename, entry->filename_len TSRMLS_CC);
  2371. if (entry->is_dir) {
  2372. /* we use this to calculate API version, 1.1.1 is used for phars with directories */
  2373. has_dirs = 1;
  2374. }
  2375. if (entry->metadata) {
  2376. if (entry->metadata_str.c) {
  2377. smart_str_free(&entry->metadata_str);
  2378. }
  2379. entry->metadata_str.c = 0;
  2380. entry->metadata_str.len = 0;
  2381. PHP_VAR_SERIALIZE_INIT(metadata_hash);
  2382. php_var_serialize(&entry->metadata_str, &entry->metadata, &metadata_hash TSRMLS_CC);
  2383. PHP_VAR_SERIALIZE_DESTROY(metadata_hash);
  2384. } else {
  2385. if (entry->metadata_str.c) {
  2386. smart_str_free(&entry->metadata_str);
  2387. }
  2388. entry->metadata_str.c = 0;
  2389. entry->metadata_str.len = 0;
  2390. }
  2391. /* 32 bits for filename length, length of filename, manifest + metadata, and add 1 for trailing / if a directory */
  2392. offset += 4 + entry->filename_len + sizeof(entry_buffer) + entry->metadata_str.len + (entry->is_dir ? 1 : 0);
  2393. /* compress and rehash as necessary */
  2394. if ((oldfile && !entry->is_modified) || entry->is_dir) {
  2395. if (entry->fp_type == PHAR_UFP) {
  2396. /* reset so we can copy the compressed data over */
  2397. entry->fp_type = PHAR_FP;
  2398. }
  2399. continue;
  2400. }
  2401. if (!phar_get_efp(entry, 0 TSRMLS_CC)) {
  2402. /* re-open internal file pointer just-in-time */
  2403. newentry = phar_open_jit(phar, entry, error TSRMLS_CC);
  2404. if (!newentry) {
  2405. /* major problem re-opening, so we ignore this file and the error */
  2406. efree(*error);
  2407. *error = NULL;
  2408. continue;
  2409. }
  2410. entry = newentry;
  2411. }
  2412. file = phar_get_efp(entry, 0 TSRMLS_CC);
  2413. if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 1 TSRMLS_CC)) {
  2414. if (closeoldfile) {
  2415. php_stream_close(oldfile);
  2416. }
  2417. php_stream_close(newfile);
  2418. if (error) {
  2419. spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname);
  2420. }
  2421. return EOF;
  2422. }
  2423. newcrc32 = ~0;
  2424. mytime = entry->uncompressed_filesize;
  2425. for (loc = 0;loc < mytime; ++loc) {
  2426. CRC32(newcrc32, php_stream_getc(file));
  2427. }
  2428. entry->crc32 = ~newcrc32;
  2429. entry->is_crc_checked = 1;
  2430. if (!(entry->flags & PHAR_ENT_COMPRESSION_MASK)) {
  2431. /* not compressed */
  2432. entry->compressed_filesize = entry->uncompressed_filesize;
  2433. continue;
  2434. }
  2435. filter = php_stream_filter_create(phar_compress_filter(entry, 0), NULL, 0 TSRMLS_CC);
  2436. if (!filter) {
  2437. if (closeoldfile) {
  2438. php_stream_close(oldfile);
  2439. }
  2440. php_stream_close(newfile);
  2441. if (entry->flags & PHAR_ENT_COMPRESSED_GZ) {
  2442. if (error) {
  2443. spprintf(error, 0, "unable to gzip compress file \"%s\" to new phar \"%s\"", entry->filename, phar->fname);
  2444. }
  2445. } else {
  2446. if (error) {
  2447. spprintf(error, 0, "unable to bzip2 compress file \"%s\" to new phar \"%s\"", entry->filename, phar->fname);
  2448. }
  2449. }
  2450. return EOF;
  2451. }
  2452. /* create new file that holds the compressed version */
  2453. /* work around inability to specify freedom in write and strictness
  2454. in read count */
  2455. entry->cfp = php_stream_fopen_tmpfile();
  2456. if (!entry->cfp) {
  2457. if (error) {
  2458. spprintf(error, 0, "unable to create temporary file");
  2459. }
  2460. if (closeoldfile) {
  2461. php_stream_close(oldfile);
  2462. }
  2463. php_stream_close(newfile);
  2464. return EOF;
  2465. }
  2466. php_stream_flush(file);
  2467. if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 0 TSRMLS_CC)) {
  2468. if (closeoldfile) {
  2469. php_stream_close(oldfile);
  2470. }
  2471. php_stream_close(newfile);
  2472. if (error) {
  2473. spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname);
  2474. }
  2475. return EOF;
  2476. }
  2477. php_stream_filter_append((&entry->cfp->writefilters), filter);
  2478. if (SUCCESS != phar_stream_copy_to_stream(file, entry->cfp, entry->uncompressed_filesize, NULL)) {
  2479. if (closeoldfile) {
  2480. php_stream_close(oldfile);
  2481. }
  2482. php_stream_close(newfile);
  2483. if (error) {
  2484. spprintf(error, 0, "unable to copy compressed file contents of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname);
  2485. }
  2486. return EOF;
  2487. }
  2488. php_stream_filter_flush(filter, 1);
  2489. php_stream_flush(entry->cfp);
  2490. php_stream_filter_remove(filter, 1 TSRMLS_CC);
  2491. php_stream_seek(entry->cfp, 0, SEEK_END);
  2492. entry->compressed_filesize = (php_uint32) php_stream_tell(entry->cfp);
  2493. /* generate crc on compressed file */
  2494. php_stream_rewind(entry->cfp);
  2495. entry->old_flags = entry->flags;
  2496. entry->is_modified = 1;
  2497. global_flags |= (entry->flags & PHAR_ENT_COMPRESSION_MASK);
  2498. }
  2499. global_flags |= PHAR_HDR_SIGNATURE;
  2500. /* write out manifest pre-header */
  2501. /* 4: manifest length
  2502. * 4: manifest entry count
  2503. * 2: phar version
  2504. * 4: phar global flags
  2505. * 4: alias length
  2506. * ?: the alias itself
  2507. * 4: phar metadata length
  2508. * ?: phar metadata
  2509. */
  2510. restore_alias_len = phar->alias_len;
  2511. if (phar->is_temporary_alias) {
  2512. phar->alias_len = 0;
  2513. }
  2514. manifest_len = offset + phar->alias_len + sizeof(manifest) + main_metadata_str.len;
  2515. phar_set_32(manifest, manifest_len);
  2516. phar_set_32(manifest+4, new_manifest_count);
  2517. if (has_dirs) {
  2518. *(manifest + 8) = (unsigned char) (((PHAR_API_VERSION) >> 8) & 0xFF);
  2519. *(manifest + 9) = (unsigned char) (((PHAR_API_VERSION) & 0xF0));
  2520. } else {
  2521. *(manifest + 8) = (unsigned char) (((PHAR_API_VERSION_NODIR) >> 8) & 0xFF);
  2522. *(manifest + 9) = (unsigned char) (((PHAR_API_VERSION_NODIR) & 0xF0));
  2523. }
  2524. phar_set_32(manifest+10, global_flags);
  2525. phar_set_32(manifest+14, phar->alias_len);
  2526. /* write the manifest header */
  2527. if (sizeof(manifest) != php_stream_write(newfile, manifest, sizeof(manifest))
  2528. || (size_t)phar->alias_len != php_stream_write(newfile, phar->alias, phar->alias_len)) {
  2529. if (closeoldfile) {
  2530. php_stream_close(oldfile);
  2531. }
  2532. php_stream_close(newfile);
  2533. phar->alias_len = restore_alias_len;
  2534. if (error) {
  2535. spprintf(error, 0, "unable to write manifest header of new phar \"%s\"", phar->fname);
  2536. }
  2537. return EOF;
  2538. }
  2539. phar->alias_len = restore_alias_len;
  2540. phar_set_32(manifest, main_metadata_str.len);
  2541. if (4 != php_stream_write(newfile, manifest, 4) || (main_metadata_str.len
  2542. && main_metadata_str.len != php_stream_write(newfile, main_metadata_str.c, main_metadata_str.len))) {
  2543. smart_str_free(&main_metadata_str);
  2544. if (closeoldfile) {
  2545. php_stream_close(oldfile);
  2546. }
  2547. php_stream_close(newfile);
  2548. phar->alias_len = restore_alias_len;
  2549. if (error) {
  2550. spprintf(error, 0, "unable to write manifest meta-data of new phar \"%s\"", phar->fname);
  2551. }
  2552. return EOF;
  2553. }
  2554. smart_str_free(&main_metadata_str);
  2555. /* re-calculate the manifest location to simplify later code */
  2556. manifest_ftell = php_stream_tell(newfile);
  2557. /* now write the manifest */
  2558. for (zend_hash_internal_pointer_reset(&phar->manifest);
  2559. zend_hash_has_more_elements(&phar->manifest) == SUCCESS;
  2560. zend_hash_move_forward(&phar->manifest)) {
  2561. if (zend_hash_get_current_data(&phar->manifest, (void **)&entry) == FAILURE) {
  2562. continue;
  2563. }
  2564. if (entry->is_deleted || entry->is_mounted) {
  2565. /* remove this from the new phar if deleted, ignore if mounted */
  2566. continue;
  2567. }
  2568. if (entry->is_dir) {
  2569. /* add 1 for trailing slash */
  2570. phar_set_32(entry_buffer, entry->filename_len + 1);
  2571. } else {
  2572. phar_set_32(entry_buffer, entry->filename_len);
  2573. }
  2574. if (4 != php_stream_write(newfile, entry_buffer, 4)
  2575. || entry->filename_len != php_stream_write(newfile, entry->filename, entry->filename_len)
  2576. || (entry->is_dir && 1 != php_stream_write(newfile, "/", 1))) {
  2577. if (closeoldfile) {
  2578. php_stream_close(oldfile);
  2579. }
  2580. php_stream_close(newfile);
  2581. if (error) {
  2582. if (entry->is_dir) {
  2583. spprintf(error, 0, "unable to write filename of directory \"%s\" to manifest of new phar \"%s\"", entry->filename, phar->fname);
  2584. } else {
  2585. spprintf(error, 0, "unable to write filename of file \"%s\" to manifest of new phar \"%s\"", entry->filename, phar->fname);
  2586. }
  2587. }
  2588. return EOF;
  2589. }
  2590. /* set the manifest meta-data:
  2591. 4: uncompressed filesize
  2592. 4: creation timestamp
  2593. 4: compressed filesize
  2594. 4: crc32
  2595. 4: flags
  2596. 4: metadata-len
  2597. +: metadata
  2598. */
  2599. mytime = sapi_get_request_time(TSRMLS_C);
  2600. phar_set_32(entry_buffer, entry->uncompressed_filesize);
  2601. phar_set_32(entry_buffer+4, mytime);
  2602. phar_set_32(entry_buffer+8, entry->compressed_filesize);
  2603. phar_set_32(entry_buffer+12, entry->crc32);
  2604. phar_set_32(entry_buffer+16, entry->flags);
  2605. phar_set_32(entry_buffer+20, entry->metadata_str.len);
  2606. if (sizeof(entry_buffer) != php_stream_write(newfile, entry_buffer, sizeof(entry_buffer))
  2607. || entry->metadata_str.len != php_stream_write(newfile, entry->metadata_str.c, entry->metadata_str.len)) {
  2608. if (closeoldfile) {
  2609. php_stream_close(oldfile);
  2610. }
  2611. php_stream_close(newfile);
  2612. if (error) {
  2613. spprintf(error, 0, "unable to write temporary manifest of file \"%s\" to manifest of new phar \"%s\"", entry->filename, phar->fname);
  2614. }
  2615. return EOF;
  2616. }
  2617. }
  2618. /* now copy the actual file data to the new phar */
  2619. offset = php_stream_tell(newfile);
  2620. for (zend_hash_internal_pointer_reset(&phar->manifest);
  2621. zend_hash_has_more_elements(&phar->manifest) == SUCCESS;
  2622. zend_hash_move_forward(&phar->manifest)) {
  2623. if (zend_hash_get_current_data(&phar->manifest, (void **)&entry) == FAILURE) {
  2624. continue;
  2625. }
  2626. if (entry->is_deleted || entry->is_dir || entry->is_mounted) {
  2627. continue;
  2628. }
  2629. if (entry->cfp) {
  2630. file = entry->cfp;
  2631. php_stream_rewind(file);
  2632. } else {
  2633. file = phar_get_efp(entry, 0 TSRMLS_CC);
  2634. if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 0 TSRMLS_CC)) {
  2635. if (closeoldfile) {
  2636. php_stream_close(oldfile);
  2637. }
  2638. php_stream_close(newfile);
  2639. if (error) {
  2640. spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname);
  2641. }
  2642. return EOF;
  2643. }
  2644. }
  2645. if (!file) {
  2646. if (closeoldfile) {
  2647. php_stream_close(oldfile);
  2648. }
  2649. php_stream_close(newfile);
  2650. if (error) {
  2651. spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname);
  2652. }
  2653. return EOF;
  2654. }
  2655. /* this will have changed for all files that have either changed compression or been modified */
  2656. entry->offset = entry->offset_abs = offset;
  2657. offset += entry->compressed_filesize;
  2658. phar_stream_copy_to_stream(file, newfile, entry->compressed_filesize, &wrote);
  2659. if (entry->compressed_filesize != wrote) {
  2660. if (closeoldfile) {
  2661. php_stream_close(oldfile);
  2662. }
  2663. php_stream_close(newfile);
  2664. if (error) {
  2665. spprintf(error, 0, "unable to write contents of file \"%s\" to new phar \"%s\"", entry->filename, phar->fname);
  2666. }
  2667. return EOF;
  2668. }
  2669. entry->is_modified = 0;
  2670. if (entry->cfp) {
  2671. php_stream_close(entry->cfp);
  2672. entry->cfp = NULL;
  2673. }
  2674. if (entry->fp_type == PHAR_MOD) {
  2675. /* this fp is in use by a phar_entry_data returned by phar_get_entry_data, it will be closed when the phar_entry_data is phar_entry_delref'ed */
  2676. if (entry->fp_refcount == 0 && entry->fp != phar->fp && entry->fp != phar->ufp) {
  2677. php_stream_close(entry->fp);
  2678. }
  2679. entry->fp = NULL;
  2680. entry->fp_type = PHAR_FP;
  2681. } else if (entry->fp_type == PHAR_UFP) {
  2682. entry->fp_type = PHAR_FP;
  2683. }
  2684. }
  2685. /* append signature */
  2686. if (global_flags & PHAR_HDR_SIGNATURE) {
  2687. char sig_buf[4];
  2688. php_stream_rewind(newfile);
  2689. if (phar->signature) {
  2690. efree(phar->signature);
  2691. phar->signature = NULL;
  2692. }
  2693. switch(phar->sig_flags) {
  2694. #ifndef PHAR_HASH_OK
  2695. case PHAR_SIG_SHA512:
  2696. case PHAR_SIG_SHA256:
  2697. if (closeoldfile) {
  2698. php_stream_close(oldfile);
  2699. }
  2700. php_stream_close(newfile);
  2701. if (error) {
  2702. spprintf(error, 0, "unable to write contents of file \"%s\" to new phar \"%s\" with requested hash type", entry->filename, phar->fname);
  2703. }
  2704. return EOF;
  2705. #endif
  2706. default: {
  2707. char *digest = NULL;
  2708. int digest_len;
  2709. if (FAILURE == phar_create_signature(phar, newfile, &digest, &digest_len, error TSRMLS_CC)) {
  2710. if (error) {
  2711. char *save = *error;
  2712. spprintf(error, 0, "phar error: unable to write signature: %s", save);
  2713. efree(save);
  2714. }
  2715. if (digest) {
  2716. efree(digest);
  2717. }
  2718. if (closeoldfile) {
  2719. php_stream_close(oldfile);
  2720. }
  2721. php_stream_close(newfile);
  2722. return EOF;
  2723. }
  2724. php_stream_write(newfile, digest, digest_len);
  2725. efree(digest);
  2726. if (phar->sig_flags == PHAR_SIG_OPENSSL) {
  2727. phar_set_32(sig_buf, digest_len);
  2728. php_stream_write(newfile, sig_buf, 4);
  2729. }
  2730. break;
  2731. }
  2732. }
  2733. phar_set_32(sig_buf, phar->sig_flags);
  2734. php_stream_write(newfile, sig_buf, 4);
  2735. php_stream_write(newfile, "GBMB", 4);
  2736. }
  2737. /* finally, close the temp file, rename the original phar,
  2738. move the temp to the old phar, unlink the old phar, and reload it into memory
  2739. */
  2740. if (phar->fp && free_fp) {
  2741. php_stream_close(phar->fp);
  2742. }
  2743. if (phar->ufp) {
  2744. if (free_ufp) {
  2745. php_stream_close(phar->ufp);
  2746. }
  2747. phar->ufp = NULL;
  2748. }
  2749. if (closeoldfile) {
  2750. php_stream_close(oldfile);
  2751. }
  2752. phar->internal_file_start = halt_offset + manifest_len + 4;
  2753. phar->halt_offset = halt_offset;
  2754. phar->is_brandnew = 0;
  2755. php_stream_rewind(newfile);
  2756. if (phar->donotflush) {
  2757. /* deferred flush */
  2758. phar->fp = newfile;
  2759. } else {
  2760. phar->fp = php_stream_open_wrapper(phar->fname, "w+b", IGNORE_URL|STREAM_MUST_SEEK|REPORT_ERRORS, NULL);
  2761. if (!phar->fp) {
  2762. phar->fp = newfile;
  2763. if (error) {
  2764. spprintf(error, 4096, "unable to open new phar \"%s\" for writing", phar->fname);
  2765. }
  2766. return EOF;
  2767. }
  2768. if (phar->flags & PHAR_FILE_COMPRESSED_GZ) {
  2769. /* to properly compress, we have to tell zlib to add a zlib header */
  2770. zval filterparams;
  2771. array_init(&filterparams);
  2772. add_assoc_long(&filterparams, "window", MAX_WBITS+16);
  2773. filter = php_stream_filter_create("zlib.deflate", &filterparams, php_stream_is_persistent(phar->fp) TSRMLS_CC);
  2774. zval_dtor(&filterparams);
  2775. if (!filter) {
  2776. if (error) {
  2777. spprintf(error, 4096, "unable to compress all contents of phar \"%s\" using zlib, PHP versions older than 5.2.6 have a buggy zlib", phar->fname);
  2778. }
  2779. return EOF;
  2780. }
  2781. php_stream_filter_append(&phar->fp->writefilters, filter);
  2782. phar_stream_copy_to_stream(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL);
  2783. php_stream_filter_flush(filter, 1);
  2784. php_stream_filter_remove(filter, 1 TSRMLS_CC);
  2785. php_stream_close(phar->fp);
  2786. /* use the temp stream as our base */
  2787. phar->fp = newfile;
  2788. } else if (phar->flags & PHAR_FILE_COMPRESSED_BZ2) {
  2789. filter = php_stream_filter_create("bzip2.compress", NULL, php_stream_is_persistent(phar->fp) TSRMLS_CC);
  2790. php_stream_filter_append(&phar->fp->writefilters, filter);
  2791. phar_stream_copy_to_stream(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL);
  2792. php_stream_filter_flush(filter, 1);
  2793. php_stream_filter_remove(filter, 1 TSRMLS_CC);
  2794. php_stream_close(phar->fp);
  2795. /* use the temp stream as our base */
  2796. phar->fp = newfile;
  2797. } else {
  2798. phar_stream_copy_to_stream(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL);
  2799. /* we could also reopen the file in "rb" mode but there is no need for that */
  2800. php_stream_close(newfile);
  2801. }
  2802. }
  2803. if (-1 == php_stream_seek(phar->fp, phar->halt_offset, SEEK_SET)) {
  2804. if (error) {
  2805. spprintf(error, 0, "unable to seek to __HALT_COMPILER(); in new phar \"%s\"", phar->fname);
  2806. }
  2807. return EOF;
  2808. }
  2809. return EOF;
  2810. }
  2811. /* }}} */
  2812. #ifdef COMPILE_DL_PHAR
  2813. ZEND_GET_MODULE(phar)
  2814. #endif
  2815. /* {{{ phar_functions[]
  2816. *
  2817. * Every user visible function must have an entry in phar_functions[].
  2818. */
  2819. function_entry phar_functions[] = {
  2820. {NULL, NULL, NULL} /* Must be the last line in phar_functions[] */
  2821. };
  2822. /* }}}*/
  2823. static size_t phar_zend_stream_reader(void *handle, char *buf, size_t len TSRMLS_DC) /* {{{ */
  2824. {
  2825. return php_stream_read(phar_get_pharfp((phar_archive_data*)handle TSRMLS_CC), buf, len);
  2826. }
  2827. /* }}} */
  2828. #if PHP_VERSION_ID >= 50300
  2829. static size_t phar_zend_stream_fsizer(void *handle TSRMLS_DC) /* {{{ */
  2830. {
  2831. return ((phar_archive_data*)handle)->halt_offset + 32;
  2832. } /* }}} */
  2833. #else /* PHP_VERSION_ID */
  2834. static long phar_stream_fteller_for_zend(void *handle TSRMLS_DC) /* {{{ */
  2835. {
  2836. return (long)php_stream_tell(phar_get_pharfp((phar_archive_data*)handle TSRMLS_CC));
  2837. }
  2838. /* }}} */
  2839. #endif
  2840. zend_op_array *(*phar_orig_compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC);
  2841. #if PHP_VERSION_ID >= 50300
  2842. #define phar_orig_zend_open zend_stream_open_function
  2843. static char *phar_resolve_path(const char *filename, int filename_len TSRMLS_DC)
  2844. {
  2845. return phar_find_in_include_path((char *) filename, filename_len, NULL TSRMLS_CC);
  2846. }
  2847. #else
  2848. int (*phar_orig_zend_open)(const char *filename, zend_file_handle *handle TSRMLS_DC);
  2849. #endif
  2850. static zend_op_array *phar_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC) /* {{{ */
  2851. {
  2852. zend_op_array *res;
  2853. char *name = NULL;
  2854. int failed;
  2855. phar_archive_data *phar;
  2856. if (!file_handle || !file_handle->filename) {
  2857. return phar_orig_compile_file(file_handle, type TSRMLS_CC);
  2858. }
  2859. if (strstr(file_handle->filename, ".phar") && !strstr(file_handle->filename, "://")) {
  2860. if (SUCCESS == phar_open_from_filename(file_handle->filename, strlen(file_handle->filename), NULL, 0, 0, &phar, NULL TSRMLS_CC)) {
  2861. if (phar->is_zip || phar->is_tar) {
  2862. zend_file_handle f = *file_handle;
  2863. /* zip or tar-based phar */
  2864. spprintf(&name, 4096, "phar://%s/%s", file_handle->filename, ".phar/stub.php");
  2865. if (SUCCESS == phar_orig_zend_open((const char *)name, file_handle TSRMLS_CC)) {
  2866. efree(name);
  2867. name = NULL;
  2868. file_handle->filename = f.filename;
  2869. if (file_handle->opened_path) {
  2870. efree(file_handle->opened_path);
  2871. }
  2872. file_handle->opened_path = f.opened_path;
  2873. file_handle->free_filename = f.free_filename;
  2874. } else {
  2875. *file_handle = f;
  2876. }
  2877. } else if (phar->flags & PHAR_FILE_COMPRESSION_MASK) {
  2878. /* compressed phar */
  2879. #if PHP_VERSION_ID >= 50300
  2880. file_handle->type = ZEND_HANDLE_STREAM;
  2881. /* we do our own reading directly from the phar, don't change the next line */
  2882. file_handle->handle.stream.handle = phar;
  2883. file_handle->handle.stream.reader = phar_zend_stream_reader;
  2884. file_handle->handle.stream.closer = NULL;
  2885. file_handle->handle.stream.fsizer = phar_zend_stream_fsizer;
  2886. file_handle->handle.stream.isatty = 0;
  2887. phar->is_persistent ?
  2888. php_stream_rewind(PHAR_GLOBALS->cached_fp[phar->phar_pos].fp) :
  2889. php_stream_rewind(phar->fp);
  2890. memset(&file_handle->handle.stream.mmap, 0, sizeof(file_handle->handle.stream.mmap));
  2891. #else /* PHP_VERSION_ID */
  2892. file_handle->type = ZEND_HANDLE_STREAM;
  2893. /* we do our own reading directly from the phar, don't change the next line */
  2894. file_handle->handle.stream.handle = phar;
  2895. file_handle->handle.stream.reader = phar_zend_stream_reader;
  2896. file_handle->handle.stream.closer = NULL; /* don't close - let phar handle this one */
  2897. file_handle->handle.stream.fteller = phar_stream_fteller_for_zend;
  2898. file_handle->handle.stream.interactive = 0;
  2899. phar->is_persistent ?
  2900. php_stream_rewind(PHAR_GLOBALS->cached_fp[phar->phar_pos].fp) :
  2901. php_stream_rewind(phar->fp);
  2902. #endif
  2903. }
  2904. }
  2905. }
  2906. zend_try {
  2907. failed = 0;
  2908. res = phar_orig_compile_file(file_handle, type TSRMLS_CC);
  2909. } zend_catch {
  2910. failed = 1;
  2911. } zend_end_try();
  2912. if (name) {
  2913. efree(name);
  2914. }
  2915. if (failed) {
  2916. zend_bailout();
  2917. }
  2918. return res;
  2919. }
  2920. /* }}} */
  2921. #if PHP_VERSION_ID < 50300
  2922. int phar_zend_open(const char *filename, zend_file_handle *handle TSRMLS_DC) /* {{{ */
  2923. {
  2924. char *arch, *entry;
  2925. int arch_len, entry_len;
  2926. /* this code is obsoleted in php 5.3 */
  2927. entry = (char *) filename;
  2928. if (!IS_ABSOLUTE_PATH(entry, strlen(entry)) && !strstr(entry, "://")) {
  2929. phar_archive_data **pphar = NULL;
  2930. char *fname;
  2931. int fname_len;
  2932. fname = zend_get_executed_filename(TSRMLS_C);
  2933. fname_len = strlen(fname);
  2934. if (fname_len > 7 && !strncasecmp(fname, "phar://", 7)) {
  2935. if (SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 1, 0 TSRMLS_CC)) {
  2936. zend_hash_find(&(PHAR_GLOBALS->phar_fname_map), arch, arch_len, (void **) &pphar);
  2937. if (!pphar && PHAR_G(manifest_cached)) {
  2938. zend_hash_find(&cached_phars, arch, arch_len, (void **) &pphar);
  2939. }
  2940. efree(arch);
  2941. efree(entry);
  2942. }
  2943. }
  2944. /* retrieving an include within the current directory, so use this if possible */
  2945. if (!(entry = phar_find_in_include_path((char *) filename, strlen(filename), NULL TSRMLS_CC))) {
  2946. /* this file is not in the phar, use the original path */
  2947. goto skip_phar;
  2948. }
  2949. if (SUCCESS == phar_orig_zend_open(entry, handle TSRMLS_CC)) {
  2950. if (!handle->opened_path) {
  2951. handle->opened_path = entry;
  2952. }
  2953. if (entry != filename) {
  2954. handle->free_filename = 1;
  2955. }
  2956. return SUCCESS;
  2957. }
  2958. if (entry != filename) {
  2959. efree(entry);
  2960. }
  2961. return FAILURE;
  2962. }
  2963. skip_phar:
  2964. return phar_orig_zend_open(filename, handle TSRMLS_CC);
  2965. }
  2966. /* }}} */
  2967. #endif
  2968. typedef zend_op_array* (zend_compile_t)(zend_file_handle*, int TSRMLS_DC);
  2969. typedef zend_compile_t* (compile_hook)(zend_compile_t *ptr);
  2970. PHP_GINIT_FUNCTION(phar) /* {{{ */
  2971. {
  2972. phar_mime_type mime;
  2973. memset(phar_globals, 0, sizeof(zend_phar_globals));
  2974. phar_globals->readonly = 1;
  2975. zend_hash_init(&phar_globals->mime_types, 0, NULL, NULL, 1);
  2976. #define PHAR_SET_MIME(mimetype, ret, fileext) \
  2977. mime.mime = mimetype; \
  2978. mime.len = sizeof((mimetype))+1; \
  2979. mime.type = ret; \
  2980. zend_hash_add(&phar_globals->mime_types, fileext, sizeof(fileext)-1, (void *)&mime, sizeof(phar_mime_type), NULL); \
  2981. PHAR_SET_MIME("text/html", PHAR_MIME_PHPS, "phps")
  2982. PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "c")
  2983. PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "cc")
  2984. PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "cpp")
  2985. PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "c++")
  2986. PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "dtd")
  2987. PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "h")
  2988. PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "log")
  2989. PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "rng")
  2990. PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "txt")
  2991. PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "xsd")
  2992. PHAR_SET_MIME("", PHAR_MIME_PHP, "php")
  2993. PHAR_SET_MIME("", PHAR_MIME_PHP, "inc")
  2994. PHAR_SET_MIME("video/avi", PHAR_MIME_OTHER, "avi")
  2995. PHAR_SET_MIME("image/bmp", PHAR_MIME_OTHER, "bmp")
  2996. PHAR_SET_MIME("text/css", PHAR_MIME_OTHER, "css")
  2997. PHAR_SET_MIME("image/gif", PHAR_MIME_OTHER, "gif")
  2998. PHAR_SET_MIME("text/html", PHAR_MIME_OTHER, "htm")
  2999. PHAR_SET_MIME("text/html", PHAR_MIME_OTHER, "html")
  3000. PHAR_SET_MIME("text/html", PHAR_MIME_OTHER, "htmls")
  3001. PHAR_SET_MIME("image/x-ico", PHAR_MIME_OTHER, "ico")
  3002. PHAR_SET_MIME("image/jpeg", PHAR_MIME_OTHER, "jpe")
  3003. PHAR_SET_MIME("image/jpeg", PHAR_MIME_OTHER, "jpg")
  3004. PHAR_SET_MIME("image/jpeg", PHAR_MIME_OTHER, "jpeg")
  3005. PHAR_SET_MIME("application/x-javascript", PHAR_MIME_OTHER, "js")
  3006. PHAR_SET_MIME("audio/midi", PHAR_MIME_OTHER, "midi")
  3007. PHAR_SET_MIME("audio/midi", PHAR_MIME_OTHER, "mid")
  3008. PHAR_SET_MIME("audio/mod", PHAR_MIME_OTHER, "mod")
  3009. PHAR_SET_MIME("movie/quicktime", PHAR_MIME_OTHER, "mov")
  3010. PHAR_SET_MIME("audio/mp3", PHAR_MIME_OTHER, "mp3")
  3011. PHAR_SET_MIME("video/mpeg", PHAR_MIME_OTHER, "mpg")
  3012. PHAR_SET_MIME("video/mpeg", PHAR_MIME_OTHER, "mpeg")
  3013. PHAR_SET_MIME("application/pdf", PHAR_MIME_OTHER, "pdf")
  3014. PHAR_SET_MIME("image/png", PHAR_MIME_OTHER, "png")
  3015. PHAR_SET_MIME("application/shockwave-flash", PHAR_MIME_OTHER, "swf")
  3016. PHAR_SET_MIME("image/tiff", PHAR_MIME_OTHER, "tif")
  3017. PHAR_SET_MIME("image/tiff", PHAR_MIME_OTHER, "tiff")
  3018. PHAR_SET_MIME("audio/wav", PHAR_MIME_OTHER, "wav")
  3019. PHAR_SET_MIME("image/xbm", PHAR_MIME_OTHER, "xbm")
  3020. PHAR_SET_MIME("text/xml", PHAR_MIME_OTHER, "xml")
  3021. phar_restore_orig_functions(TSRMLS_C);
  3022. }
  3023. /* }}} */
  3024. PHP_GSHUTDOWN_FUNCTION(phar) /* {{{ */
  3025. {
  3026. zend_hash_destroy(&phar_globals->mime_types);
  3027. }
  3028. /* }}} */
  3029. PHP_MINIT_FUNCTION(phar) /* {{{ */
  3030. {
  3031. REGISTER_INI_ENTRIES();
  3032. phar_orig_compile_file = zend_compile_file;
  3033. zend_compile_file = phar_compile_file;
  3034. #if PHP_VERSION_ID >= 50300
  3035. phar_save_resolve_path = zend_resolve_path;
  3036. zend_resolve_path = phar_resolve_path;
  3037. #else
  3038. phar_orig_zend_open = zend_stream_open_function;
  3039. zend_stream_open_function = phar_zend_open;
  3040. #endif
  3041. phar_object_init(TSRMLS_C);
  3042. phar_intercept_functions_init(TSRMLS_C);
  3043. phar_save_orig_functions(TSRMLS_C);
  3044. return php_register_url_stream_wrapper("phar", &php_stream_phar_wrapper TSRMLS_CC);
  3045. }
  3046. /* }}} */
  3047. PHP_MSHUTDOWN_FUNCTION(phar) /* {{{ */
  3048. {
  3049. php_unregister_url_stream_wrapper("phar" TSRMLS_CC);
  3050. phar_intercept_functions_shutdown(TSRMLS_C);
  3051. if (zend_compile_file == phar_compile_file) {
  3052. zend_compile_file = phar_orig_compile_file;
  3053. }
  3054. #if PHP_VERSION_ID < 50300
  3055. if (zend_stream_open_function == phar_zend_open) {
  3056. zend_stream_open_function = phar_orig_zend_open;
  3057. }
  3058. #endif
  3059. if (PHAR_G(manifest_cached)) {
  3060. zend_hash_destroy(&(cached_phars));
  3061. zend_hash_destroy(&(cached_alias));
  3062. }
  3063. return SUCCESS;
  3064. }
  3065. /* }}} */
  3066. void phar_request_initialize(TSRMLS_D) /* {{{ */
  3067. {
  3068. if (!PHAR_GLOBALS->request_init)
  3069. {
  3070. PHAR_G(last_phar) = NULL;
  3071. PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
  3072. PHAR_G(has_bz2) = zend_hash_exists(&module_registry, "bz2", sizeof("bz2"));
  3073. PHAR_G(has_zlib) = zend_hash_exists(&module_registry, "zlib", sizeof("zlib"));
  3074. PHAR_GLOBALS->request_init = 1;
  3075. PHAR_GLOBALS->request_ends = 0;
  3076. PHAR_GLOBALS->request_done = 0;
  3077. zend_hash_init(&(PHAR_GLOBALS->phar_fname_map), 5, zend_get_hash_value, destroy_phar_data, 0);
  3078. zend_hash_init(&(PHAR_GLOBALS->phar_persist_map), 5, zend_get_hash_value, NULL, 0);
  3079. zend_hash_init(&(PHAR_GLOBALS->phar_alias_map), 5, zend_get_hash_value, NULL, 0);
  3080. if (PHAR_G(manifest_cached)) {
  3081. phar_archive_data **pphar;
  3082. phar_entry_fp *stuff = (phar_entry_fp *) ecalloc(zend_hash_num_elements(&cached_phars), sizeof(phar_entry_fp));
  3083. for (zend_hash_internal_pointer_reset(&cached_phars);
  3084. zend_hash_get_current_data(&cached_phars, (void **)&pphar) == SUCCESS;
  3085. zend_hash_move_forward(&cached_phars)) {
  3086. stuff[pphar[0]->phar_pos].manifest = (phar_entry_fp_info *) ecalloc( zend_hash_num_elements(&(pphar[0]->manifest)), sizeof(phar_entry_fp_info));
  3087. }
  3088. PHAR_GLOBALS->cached_fp = stuff;
  3089. }
  3090. PHAR_GLOBALS->phar_SERVER_mung_list = 0;
  3091. PHAR_G(cwd) = NULL;
  3092. PHAR_G(cwd_len) = 0;
  3093. PHAR_G(cwd_init) = 0;
  3094. }
  3095. }
  3096. /* }}} */
  3097. PHP_RSHUTDOWN_FUNCTION(phar) /* {{{ */
  3098. {
  3099. int i;
  3100. PHAR_GLOBALS->request_ends = 1;
  3101. if (PHAR_GLOBALS->request_init)
  3102. {
  3103. phar_release_functions(TSRMLS_C);
  3104. zend_hash_destroy(&(PHAR_GLOBALS->phar_alias_map));
  3105. PHAR_GLOBALS->phar_alias_map.arBuckets = NULL;
  3106. zend_hash_destroy(&(PHAR_GLOBALS->phar_fname_map));
  3107. PHAR_GLOBALS->phar_fname_map.arBuckets = NULL;
  3108. zend_hash_destroy(&(PHAR_GLOBALS->phar_persist_map));
  3109. PHAR_GLOBALS->phar_persist_map.arBuckets = NULL;
  3110. PHAR_GLOBALS->phar_SERVER_mung_list = 0;
  3111. if (PHAR_GLOBALS->cached_fp) {
  3112. for (i = 0; i < zend_hash_num_elements(&cached_phars); ++i) {
  3113. if (PHAR_GLOBALS->cached_fp[i].fp) {
  3114. php_stream_close(PHAR_GLOBALS->cached_fp[i].fp);
  3115. }
  3116. if (PHAR_GLOBALS->cached_fp[i].ufp) {
  3117. php_stream_close(PHAR_GLOBALS->cached_fp[i].ufp);
  3118. }
  3119. efree(PHAR_GLOBALS->cached_fp[i].manifest);
  3120. }
  3121. efree(PHAR_GLOBALS->cached_fp);
  3122. PHAR_GLOBALS->cached_fp = 0;
  3123. }
  3124. PHAR_GLOBALS->request_init = 0;
  3125. if (PHAR_G(cwd)) {
  3126. efree(PHAR_G(cwd));
  3127. }
  3128. PHAR_G(cwd) = NULL;
  3129. PHAR_G(cwd_len) = 0;
  3130. PHAR_G(cwd_init) = 0;
  3131. }
  3132. PHAR_GLOBALS->request_done = 1;
  3133. return SUCCESS;
  3134. }
  3135. /* }}} */
  3136. PHP_MINFO_FUNCTION(phar) /* {{{ */
  3137. {
  3138. phar_request_initialize(TSRMLS_C);
  3139. php_info_print_table_start();
  3140. php_info_print_table_header(2, "Phar: PHP Archive support", "enabled");
  3141. php_info_print_table_row(2, "Phar EXT version", PHP_PHAR_VERSION);
  3142. php_info_print_table_row(2, "Phar API version", PHP_PHAR_API_VERSION);
  3143. php_info_print_table_row(2, "SVN revision", "$Revision: 307915 $");
  3144. php_info_print_table_row(2, "Phar-based phar archives", "enabled");
  3145. php_info_print_table_row(2, "Tar-based phar archives", "enabled");
  3146. php_info_print_table_row(2, "ZIP-based phar archives", "enabled");
  3147. if (PHAR_G(has_zlib)) {
  3148. php_info_print_table_row(2, "gzip compression", "enabled");
  3149. } else {
  3150. php_info_print_table_row(2, "gzip compression", "disabled (install ext/zlib)");
  3151. }
  3152. if (PHAR_G(has_bz2)) {
  3153. php_info_print_table_row(2, "bzip2 compression", "enabled");
  3154. } else {
  3155. php_info_print_table_row(2, "bzip2 compression", "disabled (install pecl/bz2)");
  3156. }
  3157. #ifdef PHAR_HAVE_OPENSSL
  3158. php_info_print_table_row(2, "Native OpenSSL support", "enabled");
  3159. #else
  3160. if (zend_hash_exists(&module_registry, "openssl", sizeof("openssl"))) {
  3161. php_info_print_table_row(2, "OpenSSL support", "enabled");
  3162. } else {
  3163. php_info_print_table_row(2, "OpenSSL support", "disabled (install ext/openssl)");
  3164. }
  3165. #endif
  3166. php_info_print_table_end();
  3167. php_info_print_box_start(0);
  3168. PUTS("Phar based on pear/PHP_Archive, original concept by Davey Shafik.");
  3169. PUTS(!sapi_module.phpinfo_as_text?"<br />":"\n");
  3170. PUTS("Phar fully realized by Gregory Beaver and Marcus Boerger.");
  3171. PUTS(!sapi_module.phpinfo_as_text?"<br />":"\n");
  3172. PUTS("Portions of tar implementation Copyright (c) 2003-2009 Tim Kientzle.");
  3173. php_info_print_box_end();
  3174. DISPLAY_INI_ENTRIES();
  3175. }
  3176. /* }}} */
  3177. /* {{{ phar_module_entry
  3178. */
  3179. static const zend_module_dep phar_deps[] = {
  3180. ZEND_MOD_OPTIONAL("apc")
  3181. ZEND_MOD_OPTIONAL("bz2")
  3182. ZEND_MOD_OPTIONAL("openssl")
  3183. ZEND_MOD_OPTIONAL("zlib")
  3184. ZEND_MOD_OPTIONAL("standard")
  3185. #if defined(HAVE_HASH) && !defined(COMPILE_DL_HASH)
  3186. ZEND_MOD_REQUIRED("hash")
  3187. #endif
  3188. #if HAVE_SPL
  3189. ZEND_MOD_REQUIRED("spl")
  3190. #endif
  3191. {NULL, NULL, NULL}
  3192. };
  3193. zend_module_entry phar_module_entry = {
  3194. STANDARD_MODULE_HEADER_EX, NULL,
  3195. phar_deps,
  3196. "Phar",
  3197. phar_functions,
  3198. PHP_MINIT(phar),
  3199. PHP_MSHUTDOWN(phar),
  3200. NULL,
  3201. PHP_RSHUTDOWN(phar),
  3202. PHP_MINFO(phar),
  3203. PHP_PHAR_VERSION,
  3204. PHP_MODULE_GLOBALS(phar), /* globals descriptor */
  3205. PHP_GINIT(phar), /* globals ctor */
  3206. PHP_GSHUTDOWN(phar), /* globals dtor */
  3207. NULL, /* post deactivate */
  3208. STANDARD_MODULE_PROPERTIES_EX
  3209. };
  3210. /* }}} */
  3211. /*
  3212. * Local variables:
  3213. * tab-width: 4
  3214. * c-basic-offset: 4
  3215. * End:
  3216. * vim600: noet sw=4 ts=4 fdm=marker
  3217. * vim<600: noet sw=4 ts=4
  3218. */