/* {{{ void phar_intercept_functions(void) */ void phar_intercept_functions(void) { if (!PHAR_G(request_init)) { PHAR_G(cwd) = NULL; PHAR_G(cwd_len) = 0; } PHAR_G(intercepted) = 1; }
void phar_intercept_functions_shutdown(void) { zend_function *orig; PHAR_RELEASE(fopen); PHAR_RELEASE(file_get_contents); PHAR_RELEASE(is_file); PHAR_RELEASE(is_dir); PHAR_RELEASE(opendir); PHAR_RELEASE(file_exists); PHAR_RELEASE(fileperms); PHAR_RELEASE(fileinode); PHAR_RELEASE(filesize); PHAR_RELEASE(fileowner); PHAR_RELEASE(filegroup); PHAR_RELEASE(fileatime); PHAR_RELEASE(filemtime); PHAR_RELEASE(filectime); PHAR_RELEASE(filetype); PHAR_RELEASE(is_writable); PHAR_RELEASE(is_readable); PHAR_RELEASE(is_executable); PHAR_RELEASE(lstat); PHAR_RELEASE(stat); PHAR_RELEASE(readfile); PHAR_G(intercepted) = 0; }
void phar_intercept_functions_init(void) { zend_function *orig; PHAR_INTERCEPT(fopen); PHAR_INTERCEPT(file_get_contents); PHAR_INTERCEPT(is_file); PHAR_INTERCEPT(is_link); PHAR_INTERCEPT(is_dir); PHAR_INTERCEPT(opendir); PHAR_INTERCEPT(file_exists); PHAR_INTERCEPT(fileperms); PHAR_INTERCEPT(fileinode); PHAR_INTERCEPT(filesize); PHAR_INTERCEPT(fileowner); PHAR_INTERCEPT(filegroup); PHAR_INTERCEPT(fileatime); PHAR_INTERCEPT(filemtime); PHAR_INTERCEPT(filectime); PHAR_INTERCEPT(filetype); PHAR_INTERCEPT(is_writable); PHAR_INTERCEPT(is_readable); PHAR_INTERCEPT(is_executable); PHAR_INTERCEPT(lstat); PHAR_INTERCEPT(stat); PHAR_INTERCEPT(readfile); PHAR_G(intercepted) = 0; }
static void phar_file_stat(const char *filename, php_stat_len filename_length, int type, void (*orig_stat_func)(INTERNAL_FUNCTION_PARAMETERS), INTERNAL_FUNCTION_PARAMETERS) /* {{{ */ { if (!filename_length) { RETURN_FALSE; } if (!IS_ABSOLUTE_PATH(filename, filename_length) && !strstr(filename, "://")) { char *arch, *entry, *fname; int arch_len, entry_len, fname_len; zend_stat_t sb = {0}; phar_entry_info *data = NULL; phar_archive_data *phar; fname = (char*)zend_get_executed_filename(); /* we are checking for existence of a file within the relative path. Chances are good that this is retrieving something from within the phar archive */ if (strncasecmp(fname, "phar://", 7)) { goto skip_phar; } fname_len = strlen(fname); if (PHAR_G(last_phar) && fname_len - 7 >= PHAR_G(last_phar_name_len) && !memcmp(fname + 7, PHAR_G(last_phar_name), PHAR_G(last_phar_name_len))) { arch = estrndup(PHAR_G(last_phar_name), PHAR_G(last_phar_name_len)); arch_len = PHAR_G(last_phar_name_len); entry = estrndup(filename, filename_length); /* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */ entry_len = (int) filename_length; phar = PHAR_G(last_phar); goto splitted; } if (SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0)) { efree(entry); entry = estrndup(filename, filename_length); /* fopen within phar, if :// is not in the url, then prepend phar://<archive>/ */ entry_len = (int) filename_length; if (FAILURE == phar_get_archive(&phar, arch, arch_len, NULL, 0, NULL)) { efree(arch); efree(entry); goto skip_phar; } splitted: entry = phar_fix_filepath(entry, &entry_len, 1); if (entry[0] == '/') { if (NULL != (data = zend_hash_str_find_ptr(&(phar->manifest), entry + 1, entry_len - 1))) { efree(entry); goto stat_entry; } goto notfound; } if (NULL != (data = zend_hash_str_find_ptr(&(phar->manifest), entry, entry_len))) { efree(entry); goto stat_entry; } if (zend_hash_str_exists(&(phar->virtual_dirs), entry, entry_len)) { efree(entry); efree(arch); if (IS_EXISTS_CHECK(type)) { RETURN_TRUE; } sb.st_size = 0; sb.st_mode = 0777; sb.st_mode |= S_IFDIR; /* regular directory */ #ifdef NETWARE sb.st_mtime.tv_sec = phar->max_timestamp; sb.st_atime.tv_sec = phar->max_timestamp; sb.st_ctime.tv_sec = phar->max_timestamp; #else sb.st_mtime = phar->max_timestamp; sb.st_atime = phar->max_timestamp; sb.st_ctime = phar->max_timestamp; #endif goto statme_baby; } else { char *save; int save_len; notfound: efree(entry); save = PHAR_G(cwd); save_len = PHAR_G(cwd_len); /* this file is not in the current directory, use the original path */ entry = estrndup(filename, filename_length); entry_len = filename_length; PHAR_G(cwd) = "/"; PHAR_G(cwd_len) = 0; /* clean path without cwd */ entry = phar_fix_filepath(entry, &entry_len, 1); if (NULL != (data = zend_hash_str_find_ptr(&(phar->manifest), entry + 1, entry_len - 1))) { PHAR_G(cwd) = save; PHAR_G(cwd_len) = save_len; efree(entry); if (IS_EXISTS_CHECK(type)) { efree(arch); RETURN_TRUE; } goto stat_entry; } if (zend_hash_str_exists(&(phar->virtual_dirs), entry + 1, entry_len - 1)) { PHAR_G(cwd) = save; PHAR_G(cwd_len) = save_len; efree(entry); efree(arch); if (IS_EXISTS_CHECK(type)) { RETURN_TRUE; } sb.st_size = 0; sb.st_mode = 0777; sb.st_mode |= S_IFDIR; /* regular directory */ #ifdef NETWARE sb.st_mtime.tv_sec = phar->max_timestamp; sb.st_atime.tv_sec = phar->max_timestamp; sb.st_ctime.tv_sec = phar->max_timestamp; #else sb.st_mtime = phar->max_timestamp; sb.st_atime = phar->max_timestamp; sb.st_ctime = phar->max_timestamp; #endif goto statme_baby; } PHAR_G(cwd) = save; PHAR_G(cwd_len) = save_len; efree(entry); efree(arch); /* Error Occurred */ if (!IS_EXISTS_CHECK(type)) { php_error_docref(NULL, E_WARNING, "%sstat failed for %s", IS_LINK_OPERATION(type) ? "L" : "", filename); } RETURN_FALSE; } stat_entry: efree(arch); if (!data->is_dir) { sb.st_size = data->uncompressed_filesize; sb.st_mode = data->flags & PHAR_ENT_PERM_MASK; if (data->link) { sb.st_mode |= S_IFREG|S_IFLNK; /* regular file */ } else { sb.st_mode |= S_IFREG; /* regular file */ } /* timestamp is just the timestamp when this was added to the phar */ #ifdef NETWARE sb.st_mtime.tv_sec = data->timestamp; sb.st_atime.tv_sec = data->timestamp; sb.st_ctime.tv_sec = data->timestamp; #else sb.st_mtime = data->timestamp; sb.st_atime = data->timestamp; sb.st_ctime = data->timestamp; #endif } else { sb.st_size = 0; sb.st_mode = data->flags & PHAR_ENT_PERM_MASK; sb.st_mode |= S_IFDIR; /* regular directory */ if (data->link) { sb.st_mode |= S_IFLNK; } /* timestamp is just the timestamp when this was added to the phar */ #ifdef NETWARE sb.st_mtime.tv_sec = data->timestamp; sb.st_atime.tv_sec = data->timestamp; sb.st_ctime.tv_sec = data->timestamp; #else sb.st_mtime = data->timestamp; sb.st_atime = data->timestamp; sb.st_ctime = data->timestamp; #endif } statme_baby: if (!phar->is_writeable) { sb.st_mode = (sb.st_mode & 0555) | (sb.st_mode & ~0777); } sb.st_nlink = 1; sb.st_rdev = -1; /* this is only for APC, so use /dev/null device - no chance of conflict there! */ sb.st_dev = 0xc; /* generate unique inode number for alias/filename, so no phars will conflict */ if (data) { sb.st_ino = data->inode; } #ifndef PHP_WIN32 sb.st_blksize = -1; sb.st_blocks = -1; #endif phar_fancy_stat(&sb, type, return_value); return; } } skip_phar: orig_stat_func(INTERNAL_FUNCTION_PARAM_PASSTHRU); return; }
void phar_restore_orig_functions(void) /* {{{ */ { PHAR_G(orig_fopen) = phar_orig_functions.orig_fopen; PHAR_G(orig_file_get_contents) = phar_orig_functions.orig_file_get_contents; PHAR_G(orig_is_file) = phar_orig_functions.orig_is_file; PHAR_G(orig_is_link) = phar_orig_functions.orig_is_link; PHAR_G(orig_is_dir) = phar_orig_functions.orig_is_dir; PHAR_G(orig_opendir) = phar_orig_functions.orig_opendir; PHAR_G(orig_file_exists) = phar_orig_functions.orig_file_exists; PHAR_G(orig_fileperms) = phar_orig_functions.orig_fileperms; PHAR_G(orig_fileinode) = phar_orig_functions.orig_fileinode; PHAR_G(orig_filesize) = phar_orig_functions.orig_filesize; PHAR_G(orig_fileowner) = phar_orig_functions.orig_fileowner; PHAR_G(orig_filegroup) = phar_orig_functions.orig_filegroup; PHAR_G(orig_fileatime) = phar_orig_functions.orig_fileatime; PHAR_G(orig_filemtime) = phar_orig_functions.orig_filemtime; PHAR_G(orig_filectime) = phar_orig_functions.orig_filectime; PHAR_G(orig_filetype) = phar_orig_functions.orig_filetype; PHAR_G(orig_is_writable) = phar_orig_functions.orig_is_writable; PHAR_G(orig_is_readable) = phar_orig_functions.orig_is_readable; PHAR_G(orig_is_executable) = phar_orig_functions.orig_is_executable; PHAR_G(orig_lstat) = phar_orig_functions.orig_lstat; PHAR_G(orig_readfile) = phar_orig_functions.orig_readfile; PHAR_G(orig_stat) = phar_orig_functions.orig_stat; }
void phar_save_orig_functions(void) /* {{{ */ { phar_orig_functions.orig_fopen = PHAR_G(orig_fopen); phar_orig_functions.orig_file_get_contents = PHAR_G(orig_file_get_contents); phar_orig_functions.orig_is_file = PHAR_G(orig_is_file); phar_orig_functions.orig_is_link = PHAR_G(orig_is_link); phar_orig_functions.orig_is_dir = PHAR_G(orig_is_dir); phar_orig_functions.orig_opendir = PHAR_G(orig_opendir); phar_orig_functions.orig_file_exists = PHAR_G(orig_file_exists); phar_orig_functions.orig_fileperms = PHAR_G(orig_fileperms); phar_orig_functions.orig_fileinode = PHAR_G(orig_fileinode); phar_orig_functions.orig_filesize = PHAR_G(orig_filesize); phar_orig_functions.orig_fileowner = PHAR_G(orig_fileowner); phar_orig_functions.orig_filegroup = PHAR_G(orig_filegroup); phar_orig_functions.orig_fileatime = PHAR_G(orig_fileatime); phar_orig_functions.orig_filemtime = PHAR_G(orig_filemtime); phar_orig_functions.orig_filectime = PHAR_G(orig_filectime); phar_orig_functions.orig_filetype = PHAR_G(orig_filetype); phar_orig_functions.orig_is_writable = PHAR_G(orig_is_writable); phar_orig_functions.orig_is_readable = PHAR_G(orig_is_readable); phar_orig_functions.orig_is_executable = PHAR_G(orig_is_executable); phar_orig_functions.orig_lstat = PHAR_G(orig_lstat); phar_orig_functions.orig_readfile = PHAR_G(orig_readfile); phar_orig_functions.orig_stat = PHAR_G(orig_stat); }
/* {{{ void phar_release_functions(void) */ void phar_release_functions(void) { PHAR_G(intercepted) = 0; }
/** * Open a phar file for streams API */ php_url* phar_parse_url(php_stream_wrapper *wrapper, const char *filename, const char *mode, int options) /* {{{ */ { php_url *resource; char *arch = NULL, *entry = NULL, *error; int arch_len, entry_len; if (strlen(filename) < 7 || strncasecmp(filename, "phar://", 7)) { return NULL; } if (mode[0] == 'a') { if (!(options & PHP_STREAM_URL_STAT_QUIET)) { php_stream_wrapper_log_error(wrapper, options, "phar error: open mode append not supported"); } return NULL; } if (phar_split_fname(filename, strlen(filename), &arch, &arch_len, &entry, &entry_len, 2, (mode[0] == 'w' ? 2 : 0)) == FAILURE) { if (!(options & PHP_STREAM_URL_STAT_QUIET)) { if (arch && !entry) { php_stream_wrapper_log_error(wrapper, options, "phar error: no directory in \"%s\", must have at least phar://%s/ for root directory (always use full path to a new phar)", filename, arch); arch = NULL; } else { php_stream_wrapper_log_error(wrapper, options, "phar error: invalid url or non-existent phar \"%s\"", filename); } } return NULL; } resource = ecalloc(1, sizeof(php_url)); resource->scheme = estrndup("phar", 4); resource->host = arch; resource->path = entry; #if MBO_0 if (resource) { fprintf(stderr, "Alias: %s\n", alias); fprintf(stderr, "Scheme: %s\n", resource->scheme); /* fprintf(stderr, "User: %s\n", resource->user);*/ /* fprintf(stderr, "Pass: %s\n", resource->pass ? "***" : NULL);*/ fprintf(stderr, "Host: %s\n", resource->host); /* fprintf(stderr, "Port: %d\n", resource->port);*/ fprintf(stderr, "Path: %s\n", resource->path); /* fprintf(stderr, "Query: %s\n", resource->query);*/ /* fprintf(stderr, "Fragment: %s\n", resource->fragment);*/ } #endif if (mode[0] == 'w' || (mode[0] == 'r' && mode[1] == '+')) { phar_archive_data *pphar = NULL, *phar; if (PHAR_G(request_init) && PHAR_G(phar_fname_map.u.flags) && NULL == (pphar = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), arch, arch_len))) { pphar = NULL; } if (PHAR_G(readonly) && (!pphar || !pphar->is_data)) { if (!(options & PHP_STREAM_URL_STAT_QUIET)) { php_stream_wrapper_log_error(wrapper, options, "phar error: write operations disabled by the php.ini setting phar.readonly"); } php_url_free(resource); return NULL; } if (phar_open_or_create_filename(resource->host, arch_len, NULL, 0, 0, options, &phar, &error) == FAILURE) { if (error) { if (!(options & PHP_STREAM_URL_STAT_QUIET)) { php_stream_wrapper_log_error(wrapper, options, "%s", error); } efree(error); } php_url_free(resource); return NULL; } if (phar->is_persistent && FAILURE == phar_copy_on_write(&phar)) { if (error) { spprintf(&error, 0, "Cannot open cached phar '%s' as writeable, copy on write failed", resource->host); if (!(options & PHP_STREAM_URL_STAT_QUIET)) { php_stream_wrapper_log_error(wrapper, options, "%s", error); } efree(error); } php_url_free(resource); return NULL; } } else { if (phar_open_from_filename(resource->host, arch_len, NULL, 0, options, NULL, &error) == FAILURE) { if (error) { if (!(options & PHP_STREAM_URL_STAT_QUIET)) { php_stream_wrapper_log_error(wrapper, options, "%s", error); } efree(error); } php_url_free(resource); return NULL; } } return resource; }