Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions ext/phar/dirstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,21 +345,21 @@ int phar_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url_from, int mo
{
phar_entry_info entry, *e;
phar_archive_data *phar = NULL;
char *error, *arch;
size_t arch_len;
char *error;
php_url *resource = NULL;

/* pre-readonly check, we need to know if this is a data phar */
if (FAILURE == phar_split_fname(url_from, strlen(url_from), &arch, &arch_len, NULL, 2, 2)) {
zend_string *arch = phar_split_fname(url_from, strlen(url_from), NULL, 2, 2);
if (!arch) {
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot create directory \"%s\", no phar archive specified", url_from);
return 0;
}

if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, NULL)) {
phar = NULL;
}

efree(arch);
zend_string_release_ex(arch, false);

if (PHAR_G(readonly) && (!phar || !phar->is_data)) {
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot create directory \"%s\", write operations disabled", url_from);
Expand Down Expand Up @@ -471,21 +471,21 @@ int phar_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options
{
phar_entry_info *entry;
phar_archive_data *phar = NULL;
char *error, *arch;
size_t arch_len;
char *error;
php_url *resource = NULL;

/* pre-readonly check, we need to know if this is a data phar */
if (FAILURE == phar_split_fname(url, strlen(url), &arch, &arch_len, NULL, 2, 2)) {
zend_string *arch = phar_split_fname(url, strlen(url), NULL, 2, 2);
if (!arch) {
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot remove directory \"%s\", no phar archive specified, or phar archive does not exist", url);
return 0;
}

if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, NULL)) {
phar = NULL;
}

efree(arch);
zend_string_release_ex(arch, false);

if (PHAR_G(readonly) && (!phar || !phar->is_data)) {
php_stream_wrapper_log_error(wrapper, options, "phar error: cannot rmdir directory \"%s\", write operations disabled", url);
Expand Down
64 changes: 30 additions & 34 deletions ext/phar/func_interceptors.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ PHP_FUNCTION(phar_opendir) /* {{{ */
}

if (!IS_ABSOLUTE_PATH(filename, filename_len) && !strstr(filename, "://")) {
char *arch;
size_t arch_len;
zend_string *fname = zend_get_executed_filename_ex();

/* we are checking for existence of a file within the relative path. Chances are good that this is
Expand All @@ -48,7 +46,8 @@ PHP_FUNCTION(phar_opendir) /* {{{ */
goto skip_phar;
}

if (SUCCESS == phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), &arch, &arch_len, NULL, 2, 0)) {
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, 2, 0);
if (arch) {
php_stream_context *context = NULL;
php_stream *stream;
char *name;
Expand All @@ -58,12 +57,12 @@ PHP_FUNCTION(phar_opendir) /* {{{ */
zend_string *entry = phar_fix_filepath(filename, filename_len, true);

if (ZSTR_VAL(entry)[0] == '/') {
spprintf(&name, 4096, "phar://%s%s", arch, ZSTR_VAL(entry));
spprintf(&name, 4096, "phar://%s%s", ZSTR_VAL(arch), ZSTR_VAL(entry));
} else {
spprintf(&name, 4096, "phar://%s/%s", arch, ZSTR_VAL(entry));
spprintf(&name, 4096, "phar://%s/%s", ZSTR_VAL(arch), ZSTR_VAL(entry));
}
zend_string_release_ex(entry, false);
efree(arch);
zend_string_release_ex(arch, false);
if (zcontext) {
context = php_stream_context_from_zval(zcontext, 0);
}
Expand All @@ -84,8 +83,6 @@ PHP_FUNCTION(phar_opendir) /* {{{ */

static zend_string* phar_get_name_for_relative_paths(zend_string *filename, bool using_include_path)
{
char *arch;
size_t arch_len;
zend_string *fname = zend_get_executed_filename_ex();

/* we are checking for existence of a file within the relative path. Chances are good that this is
Expand All @@ -94,23 +91,24 @@ static zend_string* phar_get_name_for_relative_paths(zend_string *filename, bool
return NULL;
}

if (FAILURE == phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), &arch, &arch_len, NULL, 2, 0)) {
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, 2, 0);
if (!arch) {
return NULL;
}

/* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
/* retrieving a file defaults to within the current directory, so use this if possible */
phar_archive_data *phar;
if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
efree(arch);
if (FAILURE == phar_get_archive(&phar, ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, NULL)) {
zend_string_release_ex(arch, false);
return NULL;
}

zend_string *name = NULL;
if (using_include_path) {
if (!(name = phar_find_in_include_path(filename, NULL))) {
/* this file is not in the phar, use the original path */
efree(arch);
zend_string_release_ex(arch, false);
return NULL;
}
} else {
Expand All @@ -124,24 +122,24 @@ static zend_string* phar_get_name_for_relative_paths(zend_string *filename, bool
/* this file is not in the phar, use the original path */
if (!is_in_phar) {
zend_string_release_ex(entry, false);
efree(arch);
zend_string_release_ex(arch, false);
return NULL;
}
/* auto-convert to phar:// */
if (ZSTR_VAL(entry)[0] == '/') {
ZEND_ASSERT(strlen("phar://") + arch_len + ZSTR_LEN(entry) < 4096);
ZEND_ASSERT(strlen("phar://") + ZSTR_LEN(arch) + ZSTR_LEN(entry) < 4096);
name = zend_string_concat3(
"phar://", strlen("phar://"),
arch, arch_len,
ZSTR_VAL(arch), ZSTR_LEN(arch),
ZSTR_VAL(entry), ZSTR_LEN(entry)
);
} else {
name = strpprintf(4096, "phar://%s/%s", arch, ZSTR_VAL(entry));
name = strpprintf(4096, "phar://%s/%s", ZSTR_VAL(arch), ZSTR_VAL(entry));
}
zend_string_release_ex(entry, false);
}

efree(arch);
zend_string_release_ex(arch, false);
return name;
}

Expand Down Expand Up @@ -492,12 +490,12 @@ static void phar_file_stat(const char *filename, size_t filename_length, int typ
phar = PHAR_G(last_phar);
goto splitted;
}
char *arch;
size_t arch_len;
if (SUCCESS == phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), &arch, &arch_len, NULL, 2, 0)) {

zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, 2, 0);
if (arch) {
/* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
zend_result has_archive = phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL);
efree(arch);
zend_result has_archive = phar_get_archive(&phar, ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, NULL);
zend_string_release_ex(arch, false);
if (FAILURE == has_archive) {
goto skip_phar;
}
Expand Down Expand Up @@ -721,8 +719,6 @@ PHP_FUNCTION(phar_is_file) /* {{{ */
goto skip_phar;
}
if (!IS_ABSOLUTE_PATH(filename, filename_len) && !strstr(filename, "://")) {
char *arch;
size_t arch_len;
zend_string *fname = zend_get_executed_filename_ex();

/* we are checking for existence of a file within the relative path. Chances are good that this is
Expand All @@ -731,12 +727,15 @@ PHP_FUNCTION(phar_is_file) /* {{{ */
goto skip_phar;
}

if (SUCCESS == phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), &arch, &arch_len, NULL, 2, 0)) {
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, 2, 0);
if (arch) {
phar_archive_data *phar;

/* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
/* retrieving a file within the current directory, so use this if possible */
if (SUCCESS == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
zend_result has_archive = phar_get_archive(&phar, ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, NULL);
zend_string_release_ex(arch, false);
if (has_archive == SUCCESS) {
phar_entry_info *etemp;

zend_string *entry = phar_fix_filepath(filename, filename_len, true);
Expand All @@ -747,12 +746,10 @@ PHP_FUNCTION(phar_is_file) /* {{{ */
}
zend_string_release_ex(entry, false);
if (etemp) {
efree(arch);
RETURN_BOOL(!etemp->is_dir);
}
/* this file is not in the current directory, use the original path */
}
efree(arch);
RETURN_FALSE;
}
}
Expand All @@ -779,8 +776,6 @@ PHP_FUNCTION(phar_is_link) /* {{{ */
goto skip_phar;
}
if (!IS_ABSOLUTE_PATH(filename, filename_len) && !strstr(filename, "://")) {
char *arch;
size_t arch_len;
zend_string *fname = zend_get_executed_filename_ex();

/* we are checking for existence of a file within the relative path. Chances are good that this is
Expand All @@ -789,12 +784,15 @@ PHP_FUNCTION(phar_is_link) /* {{{ */
goto skip_phar;
}

if (SUCCESS == phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), &arch, &arch_len, NULL, 2, 0)) {
zend_string *arch = phar_split_fname(ZSTR_VAL(fname), ZSTR_LEN(fname), NULL, 2, 0);
if (arch) {
phar_archive_data *phar;

/* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */
/* retrieving a file within the current directory, so use this if possible */
if (SUCCESS == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) {
zend_result has_archive = phar_get_archive(&phar, ZSTR_VAL(arch), ZSTR_LEN(arch), NULL, 0, NULL);
zend_string_release_ex(arch, false);
if (has_archive == SUCCESS) {
phar_entry_info *etemp;

zend_string *entry = phar_fix_filepath(filename, filename_len, true);
Expand All @@ -805,11 +803,9 @@ PHP_FUNCTION(phar_is_link) /* {{{ */
}
zend_string_release_ex(entry, false);
if (etemp) {
efree(arch);
RETURN_BOOL(etemp->link);
}
}
efree(arch);
RETURN_FALSE;
}
}
Expand Down
27 changes: 17 additions & 10 deletions ext/phar/phar.c
Original file line number Diff line number Diff line change
Expand Up @@ -2164,16 +2164,19 @@ zend_string* phar_fix_filepath(const char *path, size_t path_length, bool use_cw
*
* This is used by phar_parse_url()
*/
zend_result phar_split_fname(const char *filename, size_t filename_len, char **arch, size_t *arch_len, zend_string **entry, int executable, int for_create) /* {{{ */
zend_string* phar_split_fname_ex(const char *filename, size_t filename_len, zend_string **entry, int executable, int for_create, const char **error) /* {{{ */
{
const char *ext_str;
#ifdef PHP_WIN32
char *save;
#endif
size_t ext_len;

if (error) {
*error = NULL;
}
if (zend_char_has_nul_byte(filename, filename_len)) {
return FAILURE;
return NULL;
}

if (!strncasecmp(filename, "phar://", 7)) {
Expand All @@ -2191,12 +2194,12 @@ zend_result phar_split_fname(const char *filename, size_t filename_len, char **a
#endif
if (phar_detect_phar_fname_ext(filename, filename_len, &ext_str, &ext_len, executable, for_create, false) == FAILURE) {
if (ext_len != -1) {
if (!ext_str) {
if (!ext_str && error) {
/* no / detected, restore arch for error message */
#ifdef PHP_WIN32
*arch = save;
*error = save;
#else
*arch = (char*)filename;
*error = filename;
#endif
}

Expand All @@ -2205,19 +2208,19 @@ zend_result phar_split_fname(const char *filename, size_t filename_len, char **a
efree((char *)filename);
}
#endif
return FAILURE;
return NULL;
}

ext_len = 0;
/* no extension detected - instead we are dealing with an alias */
}

*arch_len = ext_str - filename + ext_len;
*arch = estrndup(filename, *arch_len);
size_t arch_len = ext_str - filename + ext_len;
zend_string *arch = zend_string_init(filename, arch_len, false);

if (entry) {
if (ext_str[ext_len]) {
size_t computed_entry_len = filename_len - *arch_len;
size_t computed_entry_len = filename_len - arch_len;
/* We don't need to unixify the path on Windows,
* as ext_str is derived from filename that was already unixify */
*entry = phar_fix_filepath(ext_str+ext_len, computed_entry_len, false);
Expand All @@ -2232,10 +2235,14 @@ zend_result phar_split_fname(const char *filename, size_t filename_len, char **a
}
#endif

return SUCCESS;
return arch;
}
/* }}} */

zend_string* phar_split_fname(const char *filename, size_t filename_len, zend_string **entry, int executable, int for_create) {
return phar_split_fname_ex(filename, filename_len, entry, executable, for_create, NULL);
}

/**
* Invoked when a user calls Phar::mapPhar() from within an executing .phar
* to set up its manifest directly
Expand Down
3 changes: 2 additions & 1 deletion ext/phar/phar_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,8 @@ ZEND_ATTRIBUTE_NONNULL zend_result phar_get_entry_data(phar_entry_data **ret, co
ZEND_ATTRIBUTE_NONNULL_ARGS(1, 4) int phar_flush_ex(phar_archive_data *archive, zend_string *user_stub, bool is_default_stub, char **error);
ZEND_ATTRIBUTE_NONNULL int phar_flush(phar_archive_data *archive, char **error);
zend_result phar_detect_phar_fname_ext(const char *filename, size_t filename_len, const char **ext_str, size_t *ext_len, int executable, int for_create, bool is_complete);
zend_result phar_split_fname(const char *filename, size_t filename_len, char **arch, size_t *arch_len, zend_string **entry, int executable, int for_create);
zend_string* phar_split_fname_ex(const char *filename, size_t filename_len, zend_string **entry, int executable, int for_create, const char **error);
zend_string* phar_split_fname(const char *filename, size_t filename_len, zend_string **entry, int executable, int for_create);

typedef enum {
pcr_use_query,
Expand Down
Loading