Skip to content

Commit 688aef5

Browse files
author
Greg Beaver
committed
add the ability to automatically resolve includes inside a phar to files within that phar, so
no code modification is needed to include/require
1 parent 5a14715 commit 688aef5

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

ext/phar/phar.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3757,6 +3757,51 @@ static void php_phar_init_globals_module(zend_phar_globals *phar_globals)
37573757
}
37583758
/* }}} */
37593759

3760+
static zend_op_array *(*orig_compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC);
3761+
3762+
static zend_op_array *phar_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC) /* {{{ */
3763+
{
3764+
zend_op_array *res;
3765+
char *fname = NULL;
3766+
int fname_len;
3767+
3768+
zend_compile_file = orig_compile_file;
3769+
if (zend_hash_num_elements(&(PHAR_GLOBALS->phar_fname_map))) {
3770+
char *arch, *entry;
3771+
int arch_len, entry_len;
3772+
fname = zend_get_executed_filename(TSRMLS_C);
3773+
if (strncasecmp(fname, "phar://", 7)) {
3774+
goto skip_phar;
3775+
}
3776+
fname_len = strlen(fname);
3777+
if (SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len TSRMLS_CC)) {
3778+
char *s, *name;
3779+
3780+
efree(entry);
3781+
entry = file_handle->filename;
3782+
/* include within phar, if :// is not in the url, then prepend phar://<archive>/ */
3783+
entry_len = strlen(entry);
3784+
for (s = entry; s < (entry + entry_len - 4); s++) {
3785+
if (*s == ':' && *(s + 1) == '/' && *(s + 2) == '/') {
3786+
efree(arch);
3787+
goto skip_phar;
3788+
}
3789+
}
3790+
/* auto-convert to phar:// */
3791+
spprintf(&name, 4096, "phar://%s/%s", arch, entry);
3792+
efree(arch);
3793+
file_handle->type = ZEND_HANDLE_FILENAME;
3794+
file_handle->free_filename = 1;
3795+
file_handle->filename = name;
3796+
}
3797+
}
3798+
skip_phar:
3799+
res = orig_compile_file(file_handle, type TSRMLS_CC);
3800+
zend_compile_file = phar_compile_file;
3801+
return res;
3802+
}
3803+
/* }}} */
3804+
37603805
PHP_MINIT_FUNCTION(phar) /* {{{ */
37613806
{
37623807
ZEND_INIT_MODULE_GLOBALS(phar, php_phar_init_globals_module, NULL);
@@ -3765,6 +3810,8 @@ PHP_MINIT_FUNCTION(phar) /* {{{ */
37653810
PHAR_G(has_gnupg) = zend_hash_exists(&module_registry, "gnupg", sizeof("gnupg"));
37663811
PHAR_G(has_bz2) = zend_hash_exists(&module_registry, "bz2", sizeof("bz2"));
37673812
PHAR_G(has_zlib) = zend_hash_exists(&module_registry, "zlib", sizeof("zlib"));
3813+
orig_compile_file = zend_compile_file;
3814+
zend_compile_file = phar_compile_file;
37683815
phar_object_init(TSRMLS_C);
37693816

37703817
return php_register_url_stream_wrapper("phar", &php_stream_phar_wrapper TSRMLS_CC);
@@ -3774,6 +3821,7 @@ PHP_MINIT_FUNCTION(phar) /* {{{ */
37743821
PHP_MSHUTDOWN_FUNCTION(phar) /* {{{ */
37753822
{
37763823
return php_unregister_url_stream_wrapper("phar" TSRMLS_CC);
3824+
zend_compile_file = orig_compile_file;
37773825
}
37783826
/* }}} */
37793827

0 commit comments

Comments
 (0)