/ext/phar/phar_object.c
C | 5199 lines | 3957 code | 772 blank | 470 comment | 1140 complexity | d61464bec3a114a57f5b0e28525667ba MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
Large files files are truncated, but you can click here to view the full file
- /*
- +----------------------------------------------------------------------+
- | phar php single-file executable PHP extension |
- +----------------------------------------------------------------------+
- | Copyright (c) The PHP Group |
- +----------------------------------------------------------------------+
- | This source file is subject to version 3.01 of the PHP license, |
- | that is bundled with this package in the file LICENSE, and is |
- | available through the world-wide-web at the following url: |
- | http://www.php.net/license/3_01.txt. |
- | If you did not receive a copy of the PHP license and are unable to |
- | obtain it through the world-wide-web, please send a note to |
- | license@php.net so we can mail you a copy immediately. |
- +----------------------------------------------------------------------+
- | Authors: Gregory Beaver <cellog@php.net> |
- | Marcus Boerger <helly@php.net> |
- +----------------------------------------------------------------------+
- */
- #include "phar_internal.h"
- #include "func_interceptors.h"
- #include "phar_object_arginfo.h"
- static zend_class_entry *phar_ce_archive;
- static zend_class_entry *phar_ce_data;
- static zend_class_entry *phar_ce_PharException;
- static zend_class_entry *phar_ce_entry;
- static int phar_file_type(HashTable *mimes, char *file, char **mime_type) /* {{{ */
- {
- char *ext;
- phar_mime_type *mime;
- ext = strrchr(file, '.');
- if (!ext) {
- *mime_type = "text/plain";
- /* no file extension = assume text/plain */
- return PHAR_MIME_OTHER;
- }
- ++ext;
- if (NULL == (mime = zend_hash_str_find_ptr(mimes, ext, strlen(ext)))) {
- *mime_type = "application/octet-stream";
- return PHAR_MIME_OTHER;
- }
- *mime_type = mime->mime;
- return mime->type;
- }
- /* }}} */
- static void phar_mung_server_vars(char *fname, char *entry, size_t entry_len, char *basename, size_t request_uri_len) /* {{{ */
- {
- HashTable *_SERVER;
- zval *stuff;
- char *path_info;
- size_t basename_len = strlen(basename);
- size_t code;
- zval temp;
- /* "tweak" $_SERVER variables requested in earlier call to Phar::mungServer() */
- if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_UNDEF) {
- return;
- }
- _SERVER = Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]);
- /* PATH_INFO and PATH_TRANSLATED should always be munged */
- if (NULL != (stuff = zend_hash_str_find(_SERVER, "PATH_INFO", sizeof("PATH_INFO")-1))) {
- path_info = Z_STRVAL_P(stuff);
- code = Z_STRLEN_P(stuff);
- if (code > (size_t)entry_len && !memcmp(path_info, entry, entry_len)) {
- ZVAL_STR(&temp, Z_STR_P(stuff));
- ZVAL_STRINGL(stuff, path_info + entry_len, request_uri_len);
- zend_hash_str_update(_SERVER, "PHAR_PATH_INFO", sizeof("PHAR_PATH_INFO")-1, &temp);
- }
- }
- if (NULL != (stuff = zend_hash_str_find(_SERVER, "PATH_TRANSLATED", sizeof("PATH_TRANSLATED")-1))) {
- zend_string *str = strpprintf(4096, "phar://%s%s", fname, entry);
- ZVAL_STR(&temp, Z_STR_P(stuff));
- ZVAL_NEW_STR(stuff, str);
- zend_hash_str_update(_SERVER, "PHAR_PATH_TRANSLATED", sizeof("PHAR_PATH_TRANSLATED")-1, &temp);
- }
- if (!PHAR_G(phar_SERVER_mung_list)) {
- return;
- }
- if (PHAR_G(phar_SERVER_mung_list) & PHAR_MUNG_REQUEST_URI) {
- if (NULL != (stuff = zend_hash_str_find(_SERVER, "REQUEST_URI", sizeof("REQUEST_URI")-1))) {
- path_info = Z_STRVAL_P(stuff);
- code = Z_STRLEN_P(stuff);
- if (code > basename_len && !memcmp(path_info, basename, basename_len)) {
- ZVAL_STR(&temp, Z_STR_P(stuff));
- ZVAL_STRINGL(stuff, path_info + basename_len, code - basename_len);
- zend_hash_str_update(_SERVER, "PHAR_REQUEST_URI", sizeof("PHAR_REQUEST_URI")-1, &temp);
- }
- }
- }
- if (PHAR_G(phar_SERVER_mung_list) & PHAR_MUNG_PHP_SELF) {
- if (NULL != (stuff = zend_hash_str_find(_SERVER, "PHP_SELF", sizeof("PHP_SELF")-1))) {
- path_info = Z_STRVAL_P(stuff);
- code = Z_STRLEN_P(stuff);
- if (code > basename_len && !memcmp(path_info, basename, basename_len)) {
- ZVAL_STR(&temp, Z_STR_P(stuff));
- ZVAL_STRINGL(stuff, path_info + basename_len, code - basename_len);
- zend_hash_str_update(_SERVER, "PHAR_PHP_SELF", sizeof("PHAR_PHP_SELF")-1, &temp);
- }
- }
- }
- if (PHAR_G(phar_SERVER_mung_list) & PHAR_MUNG_SCRIPT_NAME) {
- if (NULL != (stuff = zend_hash_str_find(_SERVER, "SCRIPT_NAME", sizeof("SCRIPT_NAME")-1))) {
- ZVAL_STR(&temp, Z_STR_P(stuff));
- ZVAL_STRINGL(stuff, entry, entry_len);
- zend_hash_str_update(_SERVER, "PHAR_SCRIPT_NAME", sizeof("PHAR_SCRIPT_NAME")-1, &temp);
- }
- }
- if (PHAR_G(phar_SERVER_mung_list) & PHAR_MUNG_SCRIPT_FILENAME) {
- if (NULL != (stuff = zend_hash_str_find(_SERVER, "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME")-1))) {
- zend_string *str = strpprintf(4096, "phar://%s%s", fname, entry);
- ZVAL_STR(&temp, Z_STR_P(stuff));
- ZVAL_NEW_STR(stuff, str);
- zend_hash_str_update(_SERVER, "PHAR_SCRIPT_FILENAME", sizeof("PHAR_SCRIPT_FILENAME")-1, &temp);
- }
- }
- }
- /* }}} */
- static int phar_file_action(phar_archive_data *phar, phar_entry_info *info, char *mime_type, int code, char *entry, size_t entry_len, char *arch, char *basename, char *ru, size_t ru_len) /* {{{ */
- {
- char *name = NULL, buf[8192];
- const char *cwd;
- zend_syntax_highlighter_ini syntax_highlighter_ini;
- sapi_header_line ctr = {0};
- size_t got;
- zval dummy;
- size_t name_len;
- zend_file_handle file_handle;
- zend_op_array *new_op_array;
- zval result;
- php_stream *fp;
- zend_off_t position;
- switch (code) {
- case PHAR_MIME_PHPS:
- efree(basename);
- /* highlight source */
- if (entry[0] == '/') {
- spprintf(&name, 4096, "phar://%s%s", arch, entry);
- } else {
- spprintf(&name, 4096, "phar://%s/%s", arch, entry);
- }
- php_get_highlight_struct(&syntax_highlighter_ini);
- highlight_file(name, &syntax_highlighter_ini);
- efree(name);
- #ifdef PHP_WIN32
- efree(arch);
- #endif
- zend_bailout();
- case PHAR_MIME_OTHER:
- /* send headers, output file contents */
- efree(basename);
- ctr.line_len = spprintf(&(ctr.line), 0, "Content-type: %s", mime_type);
- sapi_header_op(SAPI_HEADER_REPLACE, &ctr);
- efree(ctr.line);
- ctr.line_len = spprintf(&(ctr.line), 0, "Content-length: %u", info->uncompressed_filesize);
- sapi_header_op(SAPI_HEADER_REPLACE, &ctr);
- efree(ctr.line);
- if (FAILURE == sapi_send_headers()) {
- zend_bailout();
- }
- /* prepare to output */
- fp = phar_get_efp(info, 1);
- if (!fp) {
- char *error;
- if (!phar_open_jit(phar, info, &error)) {
- if (error) {
- zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
- efree(error);
- }
- return -1;
- }
- fp = phar_get_efp(info, 1);
- }
- position = 0;
- phar_seek_efp(info, 0, SEEK_SET, 0, 1);
- do {
- got = php_stream_read(fp, buf, MIN(8192, info->uncompressed_filesize - position));
- if (got > 0) {
- PHPWRITE(buf, got);
- position += got;
- if (position == (zend_off_t) info->uncompressed_filesize) {
- break;
- }
- }
- } while (1);
- zend_bailout();
- case PHAR_MIME_PHP:
- if (basename) {
- phar_mung_server_vars(arch, entry, entry_len, basename, ru_len);
- efree(basename);
- }
- if (entry[0] == '/') {
- name_len = spprintf(&name, 4096, "phar://%s%s", arch, entry);
- } else {
- name_len = spprintf(&name, 4096, "phar://%s/%s", arch, entry);
- }
- zend_stream_init_filename(&file_handle, name);
- PHAR_G(cwd) = NULL;
- PHAR_G(cwd_len) = 0;
- ZVAL_NULL(&dummy);
- if (zend_hash_str_add(&EG(included_files), name, name_len, &dummy) != NULL) {
- if ((cwd = zend_memrchr(entry, '/', entry_len))) {
- PHAR_G(cwd_init) = 1;
- if (entry == cwd) {
- /* root directory */
- PHAR_G(cwd_len) = 0;
- PHAR_G(cwd) = NULL;
- } else if (entry[0] == '/') {
- PHAR_G(cwd_len) = (cwd - (entry + 1));
- PHAR_G(cwd) = estrndup(entry + 1, PHAR_G(cwd_len));
- } else {
- PHAR_G(cwd_len) = (cwd - entry);
- PHAR_G(cwd) = estrndup(entry, PHAR_G(cwd_len));
- }
- }
- new_op_array = zend_compile_file(&file_handle, ZEND_REQUIRE);
- if (!new_op_array) {
- zend_hash_str_del(&EG(included_files), name, name_len);
- }
- zend_destroy_file_handle(&file_handle);
- } else {
- efree(name);
- new_op_array = NULL;
- }
- #ifdef PHP_WIN32
- efree(arch);
- #endif
- if (new_op_array) {
- ZVAL_UNDEF(&result);
- zend_try {
- zend_execute(new_op_array, &result);
- if (PHAR_G(cwd)) {
- efree(PHAR_G(cwd));
- PHAR_G(cwd) = NULL;
- PHAR_G(cwd_len) = 0;
- }
- PHAR_G(cwd_init) = 0;
- efree(name);
- destroy_op_array(new_op_array);
- efree(new_op_array);
- zval_ptr_dtor(&result);
- } zend_catch {
- if (PHAR_G(cwd)) {
- efree(PHAR_G(cwd));
- PHAR_G(cwd) = NULL;
- PHAR_G(cwd_len) = 0;
- }
- PHAR_G(cwd_init) = 0;
- efree(name);
- } zend_end_try();
- zend_bailout();
- }
- return PHAR_MIME_PHP;
- }
- return -1;
- }
- /* }}} */
- static void phar_do_403(char *entry, size_t entry_len) /* {{{ */
- {
- sapi_header_line ctr = {0};
- ctr.response_code = 403;
- ctr.line_len = sizeof("HTTP/1.0 403 Access Denied")-1;
- ctr.line = "HTTP/1.0 403 Access Denied";
- sapi_header_op(SAPI_HEADER_REPLACE, &ctr);
- sapi_send_headers();
- PHPWRITE("<html>\n <head>\n <title>Access Denied</title>\n </head>\n <body>\n <h1>403 - File ", sizeof("<html>\n <head>\n <title>Access Denied</title>\n </head>\n <body>\n <h1>403 - File ") - 1);
- PHPWRITE("Access Denied</h1>\n </body>\n</html>", sizeof("Access Denied</h1>\n </body>\n</html>") - 1);
- }
- /* }}} */
- static void phar_do_404(phar_archive_data *phar, char *fname, size_t fname_len, char *f404, size_t f404_len, char *entry, size_t entry_len) /* {{{ */
- {
- sapi_header_line ctr = {0};
- phar_entry_info *info;
- if (phar && f404_len) {
- info = phar_get_entry_info(phar, f404, f404_len, NULL, 1);
- if (info) {
- phar_file_action(phar, info, "text/html", PHAR_MIME_PHP, f404, f404_len, fname, NULL, NULL, 0);
- return;
- }
- }
- ctr.response_code = 404;
- ctr.line_len = sizeof("HTTP/1.0 404 Not Found")-1;
- ctr.line = "HTTP/1.0 404 Not Found";
- sapi_header_op(SAPI_HEADER_REPLACE, &ctr);
- sapi_send_headers();
- PHPWRITE("<html>\n <head>\n <title>File Not Found</title>\n </head>\n <body>\n <h1>404 - File ", sizeof("<html>\n <head>\n <title>File Not Found</title>\n </head>\n <body>\n <h1>404 - File ") - 1);
- PHPWRITE("Not Found</h1>\n </body>\n</html>", sizeof("Not Found</h1>\n </body>\n</html>") - 1);
- }
- /* }}} */
- /* post-process REQUEST_URI and retrieve the actual request URI. This is for
- cases like http://localhost/blah.phar/path/to/file.php/extra/stuff
- which calls "blah.phar" file "path/to/file.php" with PATH_INFO "/extra/stuff" */
- static void phar_postprocess_ru_web(char *fname, size_t fname_len, char **entry, size_t *entry_len, char **ru, size_t *ru_len) /* {{{ */
- {
- char *e = *entry + 1, *u = NULL, *u1 = NULL, *saveu = NULL;
- size_t e_len = *entry_len - 1, u_len = 0;
- phar_archive_data *pphar;
- /* we already know we can retrieve the phar if we reach here */
- pphar = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), fname, fname_len);
- if (!pphar && PHAR_G(manifest_cached)) {
- pphar = zend_hash_str_find_ptr(&cached_phars, fname, fname_len);
- }
- do {
- if (zend_hash_str_exists(&(pphar->manifest), e, e_len)) {
- if (u) {
- u[0] = '/';
- *ru = estrndup(u, u_len+1);
- ++u_len;
- u[0] = '\0';
- } else {
- *ru = NULL;
- }
- *ru_len = u_len;
- *entry_len = e_len + 1;
- return;
- }
- if (u) {
- u1 = strrchr(e, '/');
- u[0] = '/';
- saveu = u;
- e_len += u_len + 1;
- u = u1;
- if (!u) {
- return;
- }
- } else {
- u = strrchr(e, '/');
- if (!u) {
- if (saveu) {
- saveu[0] = '/';
- }
- return;
- }
- }
- u[0] = '\0';
- u_len = strlen(u + 1);
- e_len -= u_len + 1;
- } while (1);
- }
- /* }}} */
- /* {{{ proto void Phar::running([bool retphar = true])
- * return the name of the currently running phar archive. If the optional parameter
- * is set to true, return the phar:// URL to the currently running phar
- */
- PHP_METHOD(Phar, running)
- {
- char *fname, *arch, *entry;
- size_t fname_len, arch_len, entry_len;
- zend_bool retphar = 1;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &retphar) == FAILURE) {
- RETURN_THROWS();
- }
- fname = (char*)zend_get_executed_filename();
- fname_len = strlen(fname);
- if (fname_len > 7 && !memcmp(fname, "phar://", 7) && SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0)) {
- efree(entry);
- if (retphar) {
- RETVAL_STRINGL(fname, arch_len + 7);
- efree(arch);
- return;
- } else {
- // TODO: avoid reallocation ???
- RETVAL_STRINGL(arch, arch_len);
- efree(arch);
- return;
- }
- }
- RETURN_EMPTY_STRING();
- }
- /* }}} */
- /* {{{ proto void Phar::mount(string pharpath, string externalfile)
- * mount an external file or path to a location within the phar. This maps
- * an external file or directory to a location within the phar archive, allowing
- * reference to an external location as if it were within the phar archive. This
- * is useful for writable temp files like databases
- */
- PHP_METHOD(Phar, mount)
- {
- char *fname, *arch = NULL, *entry = NULL, *path, *actual;
- size_t fname_len, arch_len, entry_len;
- size_t path_len, actual_len;
- phar_archive_data *pphar;
- #ifdef PHP_WIN32
- char *save_fname;
- ALLOCA_FLAG(fname_use_heap)
- #endif
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "pp", &path, &path_len, &actual, &actual_len) == FAILURE) {
- RETURN_THROWS();
- }
- fname = (char*)zend_get_executed_filename();
- fname_len = strlen(fname);
- #ifdef PHP_WIN32
- save_fname = fname;
- if (memchr(fname, '\\', fname_len)) {
- fname = do_alloca(fname_len + 1, fname_use_heap);
- memcpy(fname, save_fname, fname_len);
- fname[fname_len] = '\0';
- phar_unixify_path_separators(fname, fname_len);
- }
- #endif
- if (fname_len > 7 && !memcmp(fname, "phar://", 7) && SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0)) {
- efree(entry);
- entry = NULL;
- if (path_len > 7 && !memcmp(path, "phar://", 7)) {
- zend_throw_exception_ex(phar_ce_PharException, 0, "Can only mount internal paths within a phar archive, use a relative path instead of \"%s\"", path);
- efree(arch);
- goto finish;
- }
- carry_on2:
- if (NULL == (pphar = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), arch, arch_len))) {
- if (PHAR_G(manifest_cached) && NULL != (pphar = zend_hash_str_find_ptr(&cached_phars, arch, arch_len))) {
- if (SUCCESS == phar_copy_on_write(&pphar)) {
- goto carry_on;
- }
- }
- zend_throw_exception_ex(phar_ce_PharException, 0, "%s is not a phar archive, cannot mount", arch);
- if (arch) {
- efree(arch);
- }
- goto finish;
- }
- carry_on:
- if (SUCCESS != phar_mount_entry(pphar, actual, actual_len, path, path_len)) {
- zend_throw_exception_ex(phar_ce_PharException, 0, "Mounting of %s to %s within phar %s failed", path, actual, arch);
- if (path && path == entry) {
- efree(entry);
- }
- if (arch) {
- efree(arch);
- }
- goto finish;
- }
- if (entry && path && path == entry) {
- efree(entry);
- }
- if (arch) {
- efree(arch);
- }
- goto finish;
- } else if (HT_IS_INITIALIZED(&PHAR_G(phar_fname_map)) && NULL != (pphar = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), fname, fname_len))) {
- goto carry_on;
- } else if (PHAR_G(manifest_cached) && NULL != (pphar = zend_hash_str_find_ptr(&cached_phars, fname, fname_len))) {
- if (SUCCESS == phar_copy_on_write(&pphar)) {
- goto carry_on;
- }
- goto carry_on;
- } else if (SUCCESS == phar_split_fname(path, path_len, &arch, &arch_len, &entry, &entry_len, 2, 0)) {
- path = entry;
- path_len = entry_len;
- goto carry_on2;
- }
- zend_throw_exception_ex(phar_ce_PharException, 0, "Mounting of %s to %s failed", path, actual);
- finish: ;
- #ifdef PHP_WIN32
- if (fname != save_fname) {
- free_alloca(fname, fname_use_heap);
- fname = save_fname;
- }
- #endif
- }
- /* }}} */
- /* {{{ proto void Phar::webPhar([string alias, [string index, [string f404, [array mimetypes, [callback rewrites]]]]])
- * mapPhar for web-based phars. Reads the currently executed file (a phar)
- * and registers its manifest. When executed in the CLI or CGI command-line sapi,
- * this works exactly like mapPhar(). When executed by a web-based sapi, this
- * reads $_SERVER['REQUEST_URI'] (the actual original value) and parses out the
- * intended internal file.
- */
- PHP_METHOD(Phar, webPhar)
- {
- zval *mimeoverride = NULL, *rewrite = NULL;
- char *alias = NULL, *error, *index_php = NULL, *f404 = NULL, *ru = NULL;
- size_t alias_len = 0, f404_len = 0, free_pathinfo = 0;
- size_t ru_len = 0;
- char *fname, *path_info, *mime_type = NULL, *entry, *pt;
- const char *basename;
- size_t fname_len, index_php_len = 0;
- size_t entry_len;
- int code, not_cgi;
- phar_archive_data *phar = NULL;
- phar_entry_info *info = NULL;
- size_t sapi_mod_name_len = strlen(sapi_module.name);
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s!saz", &alias, &alias_len, &index_php, &index_php_len, &f404, &f404_len, &mimeoverride, &rewrite) == FAILURE) {
- RETURN_THROWS();
- }
- phar_request_initialize();
- fname = (char*)zend_get_executed_filename();
- fname_len = strlen(fname);
- if (phar_open_executed_filename(alias, alias_len, &error) != SUCCESS) {
- if (error) {
- zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
- efree(error);
- }
- return;
- }
- /* retrieve requested file within phar */
- if (!(SG(request_info).request_method
- && SG(request_info).request_uri
- && (!strcmp(SG(request_info).request_method, "GET")
- || !strcmp(SG(request_info).request_method, "POST")
- || !strcmp(SG(request_info).request_method, "DELETE")
- || !strcmp(SG(request_info).request_method, "HEAD")
- || !strcmp(SG(request_info).request_method, "OPTIONS")
- || !strcmp(SG(request_info).request_method, "PATCH")
- || !strcmp(SG(request_info).request_method, "PUT")
- )
- )
- ) {
- return;
- }
- #ifdef PHP_WIN32
- if (memchr(fname, '\\', fname_len)) {
- fname = estrndup(fname, fname_len);
- phar_unixify_path_separators(fname, fname_len);
- }
- #endif
- basename = zend_memrchr(fname, '/', fname_len);
- if (!basename) {
- basename = fname;
- } else {
- ++basename;
- }
- if ((sapi_mod_name_len == sizeof("cgi-fcgi") - 1 && !strncmp(sapi_module.name, "cgi-fcgi", sizeof("cgi-fcgi") - 1))
- || (sapi_mod_name_len == sizeof("fpm-fcgi") - 1 && !strncmp(sapi_module.name, "fpm-fcgi", sizeof("fpm-fcgi") - 1))
- || (sapi_mod_name_len == sizeof("cgi") - 1 && !strncmp(sapi_module.name, "cgi", sizeof("cgi") - 1))) {
- if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) != IS_UNDEF) {
- HashTable *_server = Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]);
- zval *z_script_name, *z_path_info;
- if (NULL == (z_script_name = zend_hash_str_find(_server, "SCRIPT_NAME", sizeof("SCRIPT_NAME")-1)) ||
- IS_STRING != Z_TYPE_P(z_script_name) ||
- !strstr(Z_STRVAL_P(z_script_name), basename)) {
- return;
- }
- if (NULL != (z_path_info = zend_hash_str_find(_server, "PATH_INFO", sizeof("PATH_INFO")-1)) &&
- IS_STRING == Z_TYPE_P(z_path_info)) {
- entry_len = Z_STRLEN_P(z_path_info);
- entry = estrndup(Z_STRVAL_P(z_path_info), entry_len);
- path_info = emalloc(Z_STRLEN_P(z_script_name) + entry_len + 1);
- memcpy(path_info, Z_STRVAL_P(z_script_name), Z_STRLEN_P(z_script_name));
- memcpy(path_info + Z_STRLEN_P(z_script_name), entry, entry_len + 1);
- free_pathinfo = 1;
- } else {
- entry_len = 0;
- entry = estrndup("", 0);
- path_info = Z_STRVAL_P(z_script_name);
- }
- pt = estrndup(Z_STRVAL_P(z_script_name), Z_STRLEN_P(z_script_name));
- } else {
- char *testit;
- testit = sapi_getenv("SCRIPT_NAME", sizeof("SCRIPT_NAME")-1);
- if (!(pt = strstr(testit, basename))) {
- efree(testit);
- return;
- }
- path_info = sapi_getenv("PATH_INFO", sizeof("PATH_INFO")-1);
- if (path_info) {
- entry = path_info;
- entry_len = strlen(entry);
- spprintf(&path_info, 0, "%s%s", testit, path_info);
- free_pathinfo = 1;
- } else {
- path_info = testit;
- free_pathinfo = 1;
- entry = estrndup("", 0);
- entry_len = 0;
- }
- pt = estrndup(testit, (pt - testit) + (fname_len - (basename - fname)));
- }
- not_cgi = 0;
- } else {
- path_info = SG(request_info).request_uri;
- if (!(pt = strstr(path_info, basename))) {
- /* this can happen with rewrite rules - and we have no idea what to do then, so return */
- return;
- }
- entry_len = strlen(path_info);
- entry_len -= (pt - path_info) + (fname_len - (basename - fname));
- entry = estrndup(pt + (fname_len - (basename - fname)), entry_len);
- pt = estrndup(path_info, (pt - path_info) + (fname_len - (basename - fname)));
- not_cgi = 1;
- }
- if (rewrite) {
- zend_fcall_info fci;
- zend_fcall_info_cache fcc;
- zval params, retval;
- ZVAL_STRINGL(¶ms, entry, entry_len);
- if (FAILURE == zend_fcall_info_init(rewrite, 0, &fci, &fcc, NULL, NULL)) {
- zend_throw_exception_ex(phar_ce_PharException, 0, "phar error: invalid rewrite callback");
- if (free_pathinfo) {
- efree(path_info);
- }
- efree(pt);
- RETURN_THROWS();
- }
- fci.param_count = 1;
- fci.params = ¶ms;
- Z_ADDREF(params);
- fci.retval = &retval;
- if (FAILURE == zend_call_function(&fci, &fcc)) {
- if (!EG(exception)) {
- zend_throw_exception_ex(phar_ce_PharException, 0, "phar error: failed to call rewrite callback");
- }
- if (free_pathinfo) {
- efree(path_info);
- }
- efree(pt);
- RETURN_THROWS();
- }
- if (Z_TYPE_P(fci.retval) == IS_UNDEF || Z_TYPE(retval) == IS_UNDEF) {
- if (free_pathinfo) {
- efree(path_info);
- }
- zend_throw_exception_ex(phar_ce_PharException, 0, "phar error: rewrite callback must return a string or false");
- efree(pt);
- RETURN_THROWS();
- }
- switch (Z_TYPE(retval)) {
- case IS_STRING:
- efree(entry);
- entry = estrndup(Z_STRVAL_P(fci.retval), Z_STRLEN_P(fci.retval));
- entry_len = Z_STRLEN_P(fci.retval);
- break;
- case IS_TRUE:
- case IS_FALSE:
- phar_do_403(entry, entry_len);
- if (free_pathinfo) {
- efree(path_info);
- }
- efree(pt);
- zend_bailout();
- return;
- default:
- if (free_pathinfo) {
- efree(path_info);
- }
- efree(pt);
- zend_throw_exception_ex(phar_ce_PharException, 0, "phar error: rewrite callback must return a string or false");
- RETURN_THROWS();
- }
- }
- if (entry_len) {
- phar_postprocess_ru_web(fname, fname_len, &entry, &entry_len, &ru, &ru_len);
- }
- if (!entry_len || (entry_len == 1 && entry[0] == '/')) {
- efree(entry);
- /* direct request */
- if (index_php_len) {
- entry = index_php;
- entry_len = index_php_len;
- if (entry[0] != '/') {
- spprintf(&entry, 0, "/%s", index_php);
- ++entry_len;
- }
- } else {
- /* assume "index.php" is starting point */
- entry = estrndup("/index.php", sizeof("/index.php"));
- entry_len = sizeof("/index.php")-1;
- }
- if (FAILURE == phar_get_archive(&phar, fname, fname_len, NULL, 0, NULL) ||
- (info = phar_get_entry_info(phar, entry, entry_len, NULL, 0)) == NULL) {
- phar_do_404(phar, fname, fname_len, f404, f404_len, entry, entry_len);
- if (free_pathinfo) {
- efree(path_info);
- }
- zend_bailout();
- } else {
- char *tmp = NULL, sa = '\0';
- sapi_header_line ctr = {0};
- ctr.response_code = 301;
- ctr.line_len = sizeof("HTTP/1.1 301 Moved Permanently")-1;
- ctr.line = "HTTP/1.1 301 Moved Permanently";
- sapi_header_op(SAPI_HEADER_REPLACE, &ctr);
- if (not_cgi) {
- tmp = strstr(path_info, basename) + fname_len;
- sa = *tmp;
- *tmp = '\0';
- }
- ctr.response_code = 0;
- if (path_info[strlen(path_info)-1] == '/') {
- ctr.line_len = spprintf(&(ctr.line), 4096, "Location: %s%s", path_info, entry + 1);
- } else {
- ctr.line_len = spprintf(&(ctr.line), 4096, "Location: %s%s", path_info, entry);
- }
- if (not_cgi) {
- *tmp = sa;
- }
- if (free_pathinfo) {
- efree(path_info);
- }
- sapi_header_op(SAPI_HEADER_REPLACE, &ctr);
- sapi_send_headers();
- efree(ctr.line);
- zend_bailout();
- }
- }
- if (FAILURE == phar_get_archive(&phar, fname, fname_len, NULL, 0, NULL) ||
- (info = phar_get_entry_info(phar, entry, entry_len, NULL, 0)) == NULL) {
- phar_do_404(phar, fname, fname_len, f404, f404_len, entry, entry_len);
- #ifdef PHP_WIN32
- efree(fname);
- #endif
- zend_bailout();
- }
- if (mimeoverride && zend_hash_num_elements(Z_ARRVAL_P(mimeoverride))) {
- const char *ext = zend_memrchr(entry, '.', entry_len);
- zval *val;
- if (ext) {
- ++ext;
- if (NULL != (val = zend_hash_str_find(Z_ARRVAL_P(mimeoverride), ext, strlen(ext)))) {
- switch (Z_TYPE_P(val)) {
- case IS_LONG:
- if (Z_LVAL_P(val) == PHAR_MIME_PHP || Z_LVAL_P(val) == PHAR_MIME_PHPS) {
- mime_type = "";
- code = Z_LVAL_P(val);
- } else {
- zend_throw_exception_ex(phar_ce_PharException, 0, "Unknown mime type specifier used, only Phar::PHP, Phar::PHPS and a mime type string are allowed");
- if (free_pathinfo) {
- efree(path_info);
- }
- efree(pt);
- efree(entry);
- #ifdef PHP_WIN32
- efree(fname);
- #endif
- RETURN_THROWS();
- }
- break;
- case IS_STRING:
- mime_type = Z_STRVAL_P(val);
- code = PHAR_MIME_OTHER;
- break;
- default:
- zend_throw_exception_ex(phar_ce_PharException, 0, "Unknown mime type specifier used (not a string or int), only Phar::PHP, Phar::PHPS and a mime type string are allowed");
- if (free_pathinfo) {
- efree(path_info);
- }
- efree(pt);
- efree(entry);
- #ifdef PHP_WIN32
- efree(fname);
- #endif
- RETURN_THROWS();
- }
- }
- }
- }
- if (!mime_type) {
- code = phar_file_type(&PHAR_G(mime_types), entry, &mime_type);
- }
- phar_file_action(phar, info, mime_type, code, entry, entry_len, fname, pt, ru, ru_len);
- }
- /* }}} */
- /* {{{ proto void Phar::mungServer(array munglist)
- * Defines a list of up to 4 $_SERVER variables that should be modified for execution
- * to mask the presence of the phar archive. This should be used in conjunction with
- * Phar::webPhar(), and has no effect otherwise
- * SCRIPT_NAME, PHP_SELF, REQUEST_URI and SCRIPT_FILENAME
- */
- PHP_METHOD(Phar, mungServer)
- {
- zval *mungvalues, *data;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &mungvalues) == FAILURE) {
- RETURN_THROWS();
- }
- if (!zend_hash_num_elements(Z_ARRVAL_P(mungvalues))) {
- zend_throw_exception_ex(phar_ce_PharException, 0, "No values passed to Phar::mungServer(), expecting an array of any of these strings: PHP_SELF, REQUEST_URI, SCRIPT_FILENAME, SCRIPT_NAME");
- RETURN_THROWS();
- }
- if (zend_hash_num_elements(Z_ARRVAL_P(mungvalues)) > 4) {
- zend_throw_exception_ex(phar_ce_PharException, 0, "Too many values passed to Phar::mungServer(), expecting an array of any of these strings: PHP_SELF, REQUEST_URI, SCRIPT_FILENAME, SCRIPT_NAME");
- RETURN_THROWS();
- }
- phar_request_initialize();
- ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(mungvalues), data) {
- if (Z_TYPE_P(data) != IS_STRING) {
- zend_throw_exception_ex(phar_ce_PharException, 0, "Non-string value passed to Phar::mungServer(), expecting an array of any of these strings: PHP_SELF, REQUEST_URI, SCRIPT_FILENAME, SCRIPT_NAME");
- RETURN_THROWS();
- }
- if (Z_STRLEN_P(data) == sizeof("PHP_SELF")-1 && !strncmp(Z_STRVAL_P(data), "PHP_SELF", sizeof("PHP_SELF")-1)) {
- PHAR_G(phar_SERVER_mung_list) |= PHAR_MUNG_PHP_SELF;
- }
- if (Z_STRLEN_P(data) == sizeof("REQUEST_URI")-1) {
- if (!strncmp(Z_STRVAL_P(data), "REQUEST_URI", sizeof("REQUEST_URI")-1)) {
- PHAR_G(phar_SERVER_mung_list) |= PHAR_MUNG_REQUEST_URI;
- }
- if (!strncmp(Z_STRVAL_P(data), "SCRIPT_NAME", sizeof("SCRIPT_NAME")-1)) {
- PHAR_G(phar_SERVER_mung_list) |= PHAR_MUNG_SCRIPT_NAME;
- }
- }
- if (Z_STRLEN_P(data) == sizeof("SCRIPT_FILENAME")-1 && !strncmp(Z_STRVAL_P(data), "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME")-1)) {
- PHAR_G(phar_SERVER_mung_list) |= PHAR_MUNG_SCRIPT_FILENAME;
- }
- } ZEND_HASH_FOREACH_END();
- }
- /* }}} */
- /* {{{ proto void Phar::interceptFileFuncs()
- * instructs phar to intercept fopen, file_get_contents, opendir, and all of the stat-related functions
- * and return stat on files within the phar for relative paths
- *
- * Once called, this cannot be reversed, and continue until the end of the request.
- *
- * This allows legacy scripts to be pharred unmodified
- */
- PHP_METHOD(Phar, interceptFileFuncs)
- {
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- phar_intercept_functions();
- }
- /* }}} */
- /* {{{ proto array Phar::createDefaultStub([string indexfile[, string webindexfile]])
- * Return a stub that can be used to run a phar-based archive without the phar extension
- * indexfile is the CLI startup filename, which defaults to "index.php", webindexfile
- * is the web startup filename, and also defaults to "index.php"
- */
- PHP_METHOD(Phar, createDefaultStub)
- {
- char *index = NULL, *webindex = NULL, *error;
- zend_string *stub;
- size_t index_len = 0, webindex_len = 0;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "|pp", &index, &index_len, &webindex, &webindex_len) == FAILURE) {
- RETURN_THROWS();
- }
- stub = phar_create_default_stub(index, webindex, &error);
- if (error) {
- zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
- efree(error);
- RETURN_THROWS();
- }
- RETURN_NEW_STR(stub);
- }
- /* }}} */
- /* {{{ proto mixed Phar::mapPhar([string alias, [int dataoffset]])
- * Reads the currently executed file (a phar) and registers its manifest */
- PHP_METHOD(Phar, mapPhar)
- {
- char *alias = NULL, *error;
- size_t alias_len = 0;
- zend_long dataoffset = 0;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!l", &alias, &alias_len, &dataoffset) == FAILURE) {
- RETURN_THROWS();
- }
- phar_request_initialize();
- RETVAL_BOOL(phar_open_executed_filename(alias, alias_len, &error) == SUCCESS);
- if (error) {
- zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
- efree(error);
- }
- } /* }}} */
- /* {{{ proto mixed Phar::loadPhar(string filename [, string alias])
- * Loads any phar archive with an alias */
- PHP_METHOD(Phar, loadPhar)
- {
- char *fname, *alias = NULL, *error;
- size_t fname_len, alias_len = 0;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|s!", &fname, &fname_len, &alias, &alias_len) == FAILURE) {
- RETURN_THROWS();
- }
- phar_request_initialize();
- RETVAL_BOOL(phar_open_from_filename(fname, fname_len, alias, alias_len, REPORT_ERRORS, NULL, &error) == SUCCESS);
- if (error) {
- zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
- efree(error);
- }
- } /* }}} */
- /* {{{ proto string Phar::apiVersion()
- * Returns the api version */
- PHP_METHOD(Phar, apiVersion)
- {
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- RETURN_STRINGL(PHP_PHAR_API_VERSION, sizeof(PHP_PHAR_API_VERSION)-1);
- }
- /* }}}*/
- /* {{{ proto bool Phar::canCompress([int method])
- * Returns whether phar extension supports compression using zlib/bzip2 */
- PHP_METHOD(Phar, canCompress)
- {
- zend_long method = 0;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &method) == FAILURE) {
- RETURN_THROWS();
- }
- phar_request_initialize();
- switch (method) {
- case PHAR_ENT_COMPRESSED_GZ:
- if (PHAR_G(has_zlib)) {
- RETURN_TRUE;
- } else {
- RETURN_FALSE;
- }
- case PHAR_ENT_COMPRESSED_BZ2:
- if (PHAR_G(has_bz2)) {
- RETURN_TRUE;
- } else {
- RETURN_FALSE;
- }
- default:
- if (PHAR_G(has_zlib) || PHAR_G(has_bz2)) {
- RETURN_TRUE;
- } else {
- RETURN_FALSE;
- }
- }
- }
- /* }}} */
- /* {{{ proto bool Phar::canWrite()
- * Returns whether phar extension supports writing and creating phars */
- PHP_METHOD(Phar, canWrite)
- {
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- RETURN_BOOL(!PHAR_G(readonly));
- }
- /* }}} */
- /* {{{ proto bool Phar::isValidPharFilename(string filename[, bool executable = true])
- * Returns whether the given filename is a valid phar filename */
- PHP_METHOD(Phar, isValidPharFilename)
- {
- char *fname;
- const char *ext_str;
- size_t fname_len;
- size_t ext_len;
- int is_executable;
- zend_bool executable = 1;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|b", &fname, &fname_len, &executable) == FAILURE) {
- RETURN_THROWS();
- }
- is_executable = executable;
- RETVAL_BOOL(phar_detect_phar_fname_ext(fname, fname_len, &ext_str, &ext_len, is_executable, 2, 1) == SUCCESS);
- }
- /* }}} */
- /**
- * from spl_directory
- */
- static void phar_spl_foreign_dtor(spl_filesystem_object *object) /* {{{ */
- {
- phar_archive_data *phar = (phar_archive_data *) object->oth;
- if (!phar->is_persistent) {
- phar_archive_delref(phar);
- }
- object->oth = NULL;
- }
- /* }}} */
- /**
- * from spl_directory
- */
- static void phar_spl_foreign_clone(spl_filesystem_object *src, spl_filesystem_object *dst) /* {{{ */
- {
- phar_archive_data *phar_data = (phar_archive_data *) dst->oth;
- if (!phar_data->is_persistent) {
- ++(phar_data->refcount);
- }
- }
- /* }}} */
- static const spl_other_handler phar_spl_foreign_handler = {
- phar_spl_foreign_dtor,
- phar_spl_foreign_clone
- };
- /* {{{ proto Phar::__construct(string fname [, int flags [, string alias]])
- * Construct a Phar archive object
- *
- * proto PharData::__construct(string fname [[, int flags [, string alias]], int file format = Phar::TAR])
- * Construct a PharData archive object
- *
- * This function is used as the constructor for both the Phar and PharData
- * classes, hence the two prototypes above.
- */
- PHP_METHOD(Phar, __construct)
- {
- char *fname, *alias = NULL, *error, *arch = NULL, *entry = NULL, *save_fname;
- size_t fname_len, alias_len = 0;
- size_t arch_len, entry_len;
- zend_bool is_data;
- zend_long flags = SPL_FILE_DIR_SKIPDOTS|SPL_FILE_DIR_UNIXPATHS;
- zend_long format = 0;
- phar_archive_object *phar_obj;
- phar_archive_data *phar_data;
- zval *zobj = ZEND_THIS, arg1, arg2;
- phar_obj = (phar_archive_object*)((char*)Z_OBJ_P(zobj) - Z_OBJ_P(zobj)->handlers->offset);
- is_data = instanceof_function(Z_OBJCE_P(zobj), phar_ce_data);
- if (is_data) {
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|ls!l", &fname, &fname_len, &flags, &alias, &alias_len, &format) == FAILURE) {
- RETURN_THROWS();
- }
- } else {
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|ls!", &fname, &fname_len, &flags, &alias, &alias_len) == FAILURE) {
- RETURN_THROWS();
- }
- }
- if (phar_obj->archive) {
- zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot call constructor twice");
- RETURN_THROWS();
- }
- save_fname = fname;
- if (SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, !is_data, 2)) {
- /* use arch (the basename for the archive) for fname instead of fname */
- /* this allows support for RecursiveDirectoryIterator of subdirectories */
- #ifdef PHP_WIN32
- phar_unixify_path_separators(arch, arch_len);
- #endif
- fname = arch;
- fname_len = arch_len;
- #ifdef PHP_WIN32
- } else {
- arch = estrndup(fname, fname_len);
- arch_len = fname_len;
- fname = arch;
- phar_unixify_path_separators(arch, arch_len);
- #endif
- }
- if (phar_open_or_create_filename(fname, fname_len, alias, alias_len, is_data, REPORT_ERRORS, &phar_data, &error) == FAILURE) {
- if (fname == arch && fname != save_fname) {
- efree(arch);
- fname = save_fname;
- }
- if (entry) {
- efree(entry);
- }
- if (error) {
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
- "%s", error);
- efree(error);
- } else {
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
- "Phar creation or opening failed");
- }
- RETURN_THROWS();
- }
- if (is_data && phar_data->is_tar && phar_data->is_brandnew && format == PHAR_FORMAT_ZIP) {
- phar_data->is_zip = 1;
- phar_data->is_tar = 0;
- }
- if (fname == arch) {
- efree(arch);
- fname = save_fname;
- }
- if ((is_data && !phar_data->is_data) || (!is_data && phar_data->is_data)) {
- if (is_data) {
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
- "PharData class can only be used for non-executable tar and zip archives");
- } else {
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
- "Phar class can only be used for executable tar and zip archives");
- }
- efree(entry);
- RETURN_THROWS();
- }
- is_data = phar_data->is_data;
- if (!phar_data->is_persistent) {
- ++(phar_data->refcount);
- }
- phar_obj->archive = phar_data;
- phar_obj->spl.oth_handler = &phar_spl_foreign_handler;
- if (entry) {
- fname_len = spprintf(&fname, 0, "phar://%s%s", phar_data->fname, entry);
- efree(entry);
- } else {
- fname_len = spprintf(&fname, 0, "phar://%s", phar_data->fname);
- }
- ZVAL_STRINGL(&arg1, fname, fname_len);
- ZVAL_LONG(&arg2, flags);
- zend_call_method_with_2_params(Z_OBJ_P(zobj), Z_OBJCE_P(zobj),
- &spl_ce_RecursiveDirectoryIterator->constructor, "__construct", NULL, &arg1, &arg2);
- zval_ptr_dtor(&arg1);
- if (!phar_data->is_persistent) {
- phar_obj->archive->is_data = is_data;
- } else if (!EG(exception)) {
- /* register this guy so we can modify if necessary */
- zend_hash_str_add_ptr(&PHAR_G(phar_persist_map), (const char *) phar_obj->archive, sizeof(phar_obj->archive), phar_obj);
- }
- phar_obj->spl.info_class = phar_ce_entry;
- efree(fname);
- }
- /* }}} */
- /* {{{ proto array Phar::getSupportedSignatures()
- * Return array of supported signature types
- */
- PHP_METHOD(Phar, getSupportedSignatures)
- {
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- array_init(return_value);
- add_next_index_stringl(return_value, "MD5", 3);
- add_next_index_stringl(return_value, "SHA-1", 5);
- add_next_index_stringl(return_value, "SHA-256", 7);
- add_next_index_stringl(return_value, "SHA-512", 7);
- #if PHAR_HAVE_OPENSSL
- add_next_index_stringl(return_value, "OpenSSL", 7);
- #else
- if (zend_hash_str_exists(&module_registry, "openssl", sizeof("openssl")-1)) {
- add_next_index_stringl(return_value, "OpenSSL", 7);
- }
- #endif
- }
- /* }}} */
- /* {{{ proto array Phar::getSupportedCompression()
- * Return array of supported comparession algorithms
- */
- PHP_METHOD(Phar, getSupportedCompression)
- {
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- array_init(return_value);
- phar_request_initialize();
- if (PHAR_G(has_zlib)) {
- add_next_index_stringl(return_value, "GZ", 2);
- }
- if (PHAR_G(has_bz2)) {
- add_next_index_stringl(return_value, "BZIP2", 5);
- }
- }
- /* }}} */
- /* {{{ proto array Phar::unlinkArchive(string archive)
- * Completely remove a phar archive from memory and disk
- */
- PHP_METHOD(Phar, unlinkArchive)
- {
- char *fname, *error, *zname, *arch, *entry;
- size_t fname_len;
- size_t zname_len, arch_len, entry_len;
- phar_archive_data *phar;
- if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) {
- RETURN_THROWS();
- }
- if (!fname_len) {
- zend_throw_exception_ex(phar_ce_PharException, 0, "Unknown phar archive \"\"");
- RETURN_THROWS();
- }
- if (FAILURE == phar_open_from_filename(fname, fname_len, NULL, 0, REPORT_ERRORS, &phar, &error)) {
- if (error) {
- zend_throw_exception_ex(phar_ce_PharException, 0, "Unknown phar archive \"%s\": %s", fname, error);
- efree(error);
- } else {
- zend_throw_exception_ex(phar_ce_PharException, 0, "Unknown phar archive \"%s\"", fname);
- }
- RETURN_THROWS();
- }
- zname = (char*)zend_get_executed_filename();
- zname_len = strlen(zname);
- if (zname_len > 7 && !memcmp(zname, "phar://", 7) && SUCCESS == phar_split_fname(zname, zname_len, &arch, &arch_len, &entry, &entry_len, 2, 0)) {
- if ((size_t)arch_len == fname_len && !memcmp(arch, fname, arch_len)) {
- zend_throw_exception_ex(phar_ce_PharException, 0, "phar archive \"%s\" cannot be unlinked from within itself", fname);
- efree(arch);
- efree(entry);
- RETURN_THROWS();
- }
- efree(arch);
- efree(entry);
- }
- if (phar->is_persistent) {
- zend_throw_exception_ex(phar_ce_PharException, 0, "phar archive \"%s\" is in phar.cache_list, cannot unlinkArchive()", fname);
- RETURN_THROWS();
- }
- if (phar->refcount) {
- zend_throw_exception_ex(phar_ce_PharException, 0, "phar archive \"%s\" has open file handles or objects. fclose() all file handles, and unset() all objects prior to calling unlinkArchive()", fname);
- RETURN_THROWS();
- }
- fname = estrndup(phar->fname, phar->fname_len);
- /* invalidate phar cache */
- PHAR_G(last_phar) = NULL;
- PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
- phar_archive_delref(phar);
- unlink(fname);
- efree(fname);
- RETURN_TRUE;
- }
- /* }}} */
- #define PHAR_ARCHIVE_OBJECT() \
- zval *zobj = ZEND_THIS; \
- phar_archive_object *phar_obj = (phar_archive_object*)((char*)Z_OBJ_P(zobj) - Z_OBJ_P(zobj)->handlers->offset); \
- if (!phar_obj->archive) { \
- zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
- "Cannot call method on an uninitialized Phar object"); \
- RETURN_THROWS(); \
- }
- /* {{{ proto Phar::__destruct()
- * if persistent, remove from the cache
- */
- PHP_METHOD(Phar, __destruct)
- {
- zval *zobj = ZEND_THIS;
- phar_archive_object *phar_obj = (phar_archive_object*)((char*)Z_OBJ_P(zobj) - Z_OBJ_P(zobj)->handlers->offset);
- if (zend_parse_parameters_none() == FAILURE) {
- RETURN_THROWS();
- }
- if (phar_obj->archive && phar_obj->archive->is_persistent) {
- zend_hash_str_del(&PHAR_G(phar_persist_map), (const char *) phar_obj->archive, sizeof(phar_obj->archive));
- }
- }
- /* }}} */
- struct _phar_t {
- phar_archive_object *p;
- zend_class_entry *c;
- char *b;
- zval *ret;
- php_stream *fp;
- uint32_t l;
- int count;
- };
- static int phar_build(zend_object_iterator *iter, void *puser) /* {{{ */
- {
- zval *value;
- zend_bool close_fp = 1;
- struct _phar_t *p_obj = (struct _phar_t*) puser;
- size_t str_key_len, base_len = p_obj->l;
- phar_entry_data *data;
- php_stream *fp;
- size_t fname_len;
- size_t contents_len;
- char *fname, *error = NULL, *base = p_obj->b, *save = NULL, *temp = NULL;
- zend_string *opened;
- char *str_key;
- zend_class_entry *ce = p_obj->c;
- phar_archive_object *phar_obj = p_obj->p;
- php_stream_statbuf ssb;
- value = iter->funcs->get_current_data(iter);
- if (EG(exception)) {
- return ZEND_HASH_APPLY_STOP;
- }
- if (!value) {
- /* failure in get_current_data */
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned no value", ZSTR_VAL(ce->name));
- return ZEND_HASH_APPLY_STOP;
- }
- switch (Z_TYPE_P(value)) {
- case IS_STRING:
- break;
- case IS_RESOURCE:
- php_stream_from_zval_no_verify(fp, value);
- if (!fp) {
- zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Iterator %s returned an invalid stream handle", ZSTR_VAL(ce->name));
- return ZEND_HASH_APPLY_STOP;
- }
- if (iter->funcs->get_current_key) {
- zval key;
- iter->funcs->get_current_key(iter, &key);
- if (EG(exception)) {
- return ZEND_HASH_APPLY_STOP;
- }
- if (Z_TYPE(key) != IS_STRING) {
- zval_ptr_dtor(&key);
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned an invalid key (must return a string)", ZSTR_VAL(ce->name));
- return ZEND_HASH_APPLY_STOP;
- }
- str_key_len = Z_STRLEN(key);
- str_key = estrndup(Z_STRVAL(key), str_key_len);
- save = str_key;
- zval_ptr_dtor_str(&key);
- } else {
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned an invalid key (must return a string)", ZSTR_VAL(ce->name));
- return ZEND_HASH_APPLY_STOP;
- }
- close_fp = 0;
- opened = zend_string_init("[stream]", sizeof("[stream]") - 1, 0);
- goto after_open_fp;
- case IS_OBJECT:
- if (instanceof_function(Z_OBJCE_P(value), spl_ce_SplFileInfo)) {
- char *test = NULL;
- zval dummy;
- spl_filesystem_object *intern = (spl_filesystem_object*)((char*)Z_OBJ_P(value) - Z_OBJ_P(value)->handlers->offset);
- if (!base_len) {
- zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Iterator %s returns an SplFileInfo object, so base directory must be specified", ZSTR_VAL(ce->name));
- return ZEND_HASH_APPLY_STOP;
- }
- switch (intern->type) {
- case SPL_FS_DIR:
- test = spl_filesystem_object_get_path(intern, NULL);
- fname_len = spprintf(&fname, 0, "%s%c%s", test, DEFAULT_SLASH, intern->u.dir.entry.d_name);
- php_stat(fname, fname_len, FS_IS_DIR, &dummy);
- if (Z_TYPE(dummy) == IS_TRUE) {
- /* ignore directories */
- efree(fname);
- return ZEND_HASH_APPLY_KEEP;
- }
- test = expand_filepath(fname, NULL);
- efree(fname);
- if (test) {
- fname = test;
- fname_len = strlen(fname);
- } else {
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Could not resolve file path");
- return ZEND_HASH_APPLY_STOP;
- }
- save = fname;
- goto phar_spl_fileinfo;
- case SPL_FS_INFO:
- case SPL_FS_FILE:
- fname = expand_filepath(intern->file_name, NULL);
- if (!fname) {
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Could not resolve file path");
- return ZEND_HASH_APPLY_STOP;
- }
- fname_len = strlen(fname);
- save = fname;
- goto phar_spl_fileinfo;
- }
- }
- /* fall-through */
- default:
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned an invalid value (must return a string)", ZSTR_VAL(ce->name));
- return ZEND_HASH_APPLY_STOP;
- }
- fname = Z_STRVAL_P(value);
- fname_len = Z_STRLEN_P(value);
- phar_spl_fileinfo:
- if (base_len) {
- temp = expand_filepath(base, NULL);
- if (!temp) {
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Could not resolve file path");
- if (save) {
- efree(save);
- }
- return ZEND_HASH_APPLY_STOP;
- }
- base = temp;
- base_len = strlen(base);
- if (strstr(fname, base)) {
- str_key_len = fname_len - base_len;
- if (str_key_len <= 0) {
- if (save) {
- efree(save);
- efree(temp);
- }
- return ZEND_HASH_APPLY_KEEP;
- }
- str_key = fname + base_len;
- if (*str_key == '/' || *str_key == '\\') {
- str_key++;
- str_key_len--;
- }
- } else {
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned a path \"%s\" that is not in the base directory \"%s\"", ZSTR_VAL(ce->name), fname, base);
- if (save) {
- efree(save);
- efree(temp);
- }
- return ZEND_HASH_APPLY_STOP;
- }
- } else {
- if (iter->funcs->get_current_key) {
- zval key;
- iter->funcs->get_current_key(iter, &key);
- if (EG(exception)) {
- return ZEND_HASH_APPLY_STOP;
- }
- if (Z_TYPE(key) != IS_STRING) {
- zval_ptr_dtor(&key);
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned an invalid key (must return a string)", ZSTR_VAL(ce->name));
- return ZEND_HASH_APPLY_STOP;
- }
- str_key_len = Z_STRLEN(key);
- str_key = estrndup(Z_STRVAL(key), str_key_len);
- save = str_key;
- zval_ptr_dtor_str(&key);
- } else {
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned an invalid key (must return a string)", ZSTR_VAL(ce->name));
- return ZEND_HASH_APPLY_STOP;
- }
- }
- if (php_check_open_basedir(fname)) {
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned a path \"%s\" that open_basedir prevents opening", ZSTR_VAL(ce->name), fname);
- if (save) {
- efree(save);
- }
- if (temp) {
- efree(temp);
- }
- return ZEND_HASH_APPLY_STOP;
- }
- /* try to open source file, then create internal phar file and copy contents */
- fp = php_stream_open_wrapper(fname, "rb", STREAM_MUST_SEEK|0, &opened);
- if (!fp) {
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned a file that could not be opened \"%s\"", ZSTR_VAL(ce->name), fname);
- if (save) {
- efree(save);
- }
- if (temp) {
- efree(temp);
- }
- return ZEND_HASH_APPLY_STOP;
- }
- after_open_fp:
- if (str_key_len >= sizeof(".phar")-1 && !memcmp(str_key, ".phar", sizeof(".phar")-1)) {
- /* silently skip any files that would be added to the magic .phar directory */
- if (save) {
- efree(save);
- }
- if (temp) {
- efree(temp);
- }
- if (opened) {
- zend_string_release_ex(opened, 0);
- }
- if (close_fp) {
- php_stream_close(fp);
- }
- return ZEND_HASH_APPLY_KEEP;
- }
- if (!(data = phar_get_or_create_entry_data(phar_obj->archive->fname, phar_obj->archive->fname_len, str_key, str_key_len, "w+b", 0, &error, 1))) {
- zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s cannot be created: %s", str_key, error);
- efree(error);
- if (save) {
- efree(save);
- }
- if (opened) {
- zend_string_release_ex(opened, 0);
- }
- if (temp) {
- efree(temp);
- }
- if (close_fp) {
- php_stream_close(fp);
- }
- return ZEND_HASH_APPLY_STOP;
- } else {
- if (error) {
- efree(error);
- }
- /* convert to PHAR_UFP */
- if (data->internal_file->fp_type == PHAR_MOD) {
- php_stream_close(data->internal_file->fp);
- }
- data->internal_file->fp = NULL;
- data->internal_file->fp_type = PHAR_UFP;
- data->internal_file->offset_abs = data->internal_file->offset = php_stream_tell(p_obj->fp);
- data->fp = NULL;
- php_stream_copy_to_stream_ex(fp, p_obj->fp, PHP_STREAM_COPY_ALL, &contents_len);
- data->internal_file->uncompressed_filesize = data->internal_file->compressed_filesize =
- php_stream_tell(p_obj->fp) - data->internal_file->offset;
- if (php_stream_stat(fp, &ssb) != -1) {
- data->internal_file->flags = ssb.sb.st_mode & PHAR_ENT_PERM_MASK ;
- } else {
- #ifndef _WIN32
- mode_t mask;
- mask = umask(0);
- umask(mask);
- data->internal_file->flags &= ~mask;
- #endif
- }
- }
- if (close_fp) {
- php_stream_close(fp);
- }
- add_assoc_str(p_obj->ret, str_key, opened);
- if (save) {
- efree(save);
- }
- if (temp) {
- efree(temp);
- }
- data->internal_file->compressed_filesize = data->internal_file->uncompressed_filesize = contents_len;
- phar_entry_delref(data);
- return ZEND_HASH_APPLY_KEEP;
- }
- /* }}} */
- /* {{{ proto array Phar::buildFromDirectory(string base_dir[, string regex])
- * Construct a phar archive from an existing directory, recursively.
- * Optional second parameter is a regular expression for filtering directory contents.
- *
- * Return value is an array mapping phar index to actual files added.
- */
- PHP_METHOD(Phar, buildFromDirectory)
- {
- char *dir, *error, *regex = NULL;
- size_t dir_len, regex_len = 0;
- zend_bool apply_reg = 0;
- zval arg, arg2, iter, iteriter, regexiter;
- struct _phar_t pass;
- PHAR_ARCHIVE_OBJECT();
- if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
- "Cannot write to…
Large files files are truncated, but you can click here to view the full file