/ext/phar/phar_object.c
C | 5438 lines | 4157 code | 809 blank | 472 comment | 1052 complexity | f071aaab42bfcb662350e9d7403a9637 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
Large files files are truncated, but you can click here to view the full file
- /*
- +----------------------------------------------------------------------+
- | phar php single-file executable PHP extension |
- +----------------------------------------------------------------------+
- | Copyright (c) 2005-2011 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> |
- +----------------------------------------------------------------------+
- */
- /* $Id: phar_object.c 309222 2011-03-14 14:12:42Z felipe $ */
- #include "phar_internal.h"
- #include "func_interceptors.h"
- static zend_class_entry *phar_ce_archive;
- static zend_class_entry *phar_ce_data;
- static zend_class_entry *phar_ce_PharException;
- #if HAVE_SPL
- static zend_class_entry *phar_ce_entry;
- #endif
- #if PHP_MAJOR_VERSION > 5 || ((PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION >= 3))
- # define PHAR_ARG_INFO
- #else
- # define PHAR_ARG_INFO static
- #endif
- static int phar_file_type(HashTable *mimes, char *file, char **mime_type TSRMLS_DC) /* {{{ */
- {
- 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 (SUCCESS != zend_hash_find(mimes, ext, strlen(ext), (void **) &mime)) {
- *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, int entry_len, char *basename, int request_uri_len TSRMLS_DC) /* {{{ */
- {
- #if PHP_MAJOR_VERSION >= 6
- int is_unicode = 0;
- #endif
- HashTable *_SERVER;
- zval **stuff;
- char *path_info;
- int basename_len = strlen(basename);
- int code;
- zval *temp;
- /* "tweak" $_SERVER variables requested in earlier call to Phar::mungServer() */
- if (!PG(http_globals)[TRACK_VARS_SERVER]) {
- return;
- }
- _SERVER = Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]);
- /* PATH_INFO and PATH_TRANSLATED should always be munged */
- #if PHP_MAJOR_VERSION >= 6
- if (phar_find_key(_SERVER, "PATH_INFO", sizeof("PATH_INFO"), (void **) &stuff TSRMLS_CC)) {
- if (Z_TYPE_PP(stuff) == IS_UNICODE) {
- is_unicode = 1;
- zval_unicode_to_string(*stuff TSRMLS_CC);
- } else {
- is_unicode = 0;
- }
- #else
- if (SUCCESS == zend_hash_find(_SERVER, "PATH_INFO", sizeof("PATH_INFO"), (void **) &stuff)) {
- #endif
- path_info = Z_STRVAL_PP(stuff);
- code = Z_STRLEN_PP(stuff);
- if (Z_STRLEN_PP(stuff) > entry_len && !memcmp(Z_STRVAL_PP(stuff), entry, entry_len)) {
- ZVAL_STRINGL(*stuff, Z_STRVAL_PP(stuff) + entry_len, request_uri_len, 1);
- MAKE_STD_ZVAL(temp);
- ZVAL_STRINGL(temp, path_info, code, 0);
- #if PHP_MAJOR_VERSION >= 6
- if (is_unicode) {
- zval_string_to_unicode(*stuff TSRMLS_CC);
- }
- #endif
- zend_hash_update(_SERVER, "PHAR_PATH_INFO", sizeof("PHAR_PATH_INFO"), &temp, sizeof(zval **), NULL);
- }
- }
- #if PHP_MAJOR_VERSION >= 6
- if (phar_find_key(_SERVER, "PATH_TRANSLATED", sizeof("PATH_TRANSLATED"), (void **) &stuff TSRMLS_CC)) {
- if (Z_TYPE_PP(stuff) == IS_UNICODE) {
- is_unicode = 1;
- zval_unicode_to_string(*stuff TSRMLS_CC);
- } else {
- is_unicode = 0;
- }
- #else
- if (SUCCESS == zend_hash_find(_SERVER, "PATH_TRANSLATED", sizeof("PATH_TRANSLATED"), (void **) &stuff)) {
- #endif
- path_info = Z_STRVAL_PP(stuff);
- code = Z_STRLEN_PP(stuff);
- Z_STRLEN_PP(stuff) = spprintf(&(Z_STRVAL_PP(stuff)), 4096, "phar://%s%s", fname, entry);
- MAKE_STD_ZVAL(temp);
- ZVAL_STRINGL(temp, path_info, code, 0);
- #if PHP_MAJOR_VERSION >= 6
- if (is_unicode) {
- zval_string_to_unicode(*stuff TSRMLS_CC);
- }
- #endif
- zend_hash_update(_SERVER, "PHAR_PATH_TRANSLATED", sizeof("PHAR_PATH_TRANSLATED"), (void *) &temp, sizeof(zval **), NULL);
- }
- if (!PHAR_GLOBALS->phar_SERVER_mung_list) {
- return;
- }
- if (PHAR_GLOBALS->phar_SERVER_mung_list & PHAR_MUNG_REQUEST_URI) {
- #if PHP_MAJOR_VERSION >= 6
- if (phar_find_key(_SERVER, "REQUEST_URI", sizeof("REQUEST_URI"), (void **) &stuff TSRMLS_CC)) {
- if (Z_TYPE_PP(stuff) == IS_UNICODE) {
- is_unicode = 1;
- zval_unicode_to_string(*stuff TSRMLS_CC);
- } else {
- is_unicode = 0;
- }
- #else
- if (SUCCESS == zend_hash_find(_SERVER, "REQUEST_URI", sizeof("REQUEST_URI"), (void **) &stuff)) {
- #endif
- path_info = Z_STRVAL_PP(stuff);
- code = Z_STRLEN_PP(stuff);
- if (Z_STRLEN_PP(stuff) > basename_len && !memcmp(Z_STRVAL_PP(stuff), basename, basename_len)) {
- ZVAL_STRINGL(*stuff, Z_STRVAL_PP(stuff) + basename_len, Z_STRLEN_PP(stuff) - basename_len, 1);
- MAKE_STD_ZVAL(temp);
- ZVAL_STRINGL(temp, path_info, code, 0);
- #if PHP_MAJOR_VERSION >= 6
- if (is_unicode) {
- zval_string_to_unicode(*stuff TSRMLS_CC);
- }
- #endif
- zend_hash_update(_SERVER, "PHAR_REQUEST_URI", sizeof("PHAR_REQUEST_URI"), (void *) &temp, sizeof(zval **), NULL);
- }
- }
- }
- if (PHAR_GLOBALS->phar_SERVER_mung_list & PHAR_MUNG_PHP_SELF) {
- #if PHP_MAJOR_VERSION >= 6
- if (phar_find_key(_SERVER, "PHP_SELF", sizeof("PHP_SELF"), (void **) &stuff TSRMLS_CC)) {
- if (Z_TYPE_PP(stuff) == IS_UNICODE) {
- is_unicode = 1;
- zval_unicode_to_string(*stuff TSRMLS_CC);
- } else {
- is_unicode = 0;
- }
- #else
- if (SUCCESS == zend_hash_find(_SERVER, "PHP_SELF", sizeof("PHP_SELF"), (void **) &stuff)) {
- #endif
- path_info = Z_STRVAL_PP(stuff);
- code = Z_STRLEN_PP(stuff);
- if (Z_STRLEN_PP(stuff) > basename_len && !memcmp(Z_STRVAL_PP(stuff), basename, basename_len)) {
- ZVAL_STRINGL(*stuff, Z_STRVAL_PP(stuff) + basename_len, Z_STRLEN_PP(stuff) - basename_len, 1);
- MAKE_STD_ZVAL(temp);
- ZVAL_STRINGL(temp, path_info, code, 0);
- #if PHP_MAJOR_VERSION >= 6
- if (is_unicode) {
- zval_string_to_unicode(*stuff TSRMLS_CC);
- }
- #endif
- zend_hash_update(_SERVER, "PHAR_PHP_SELF", sizeof("PHAR_PHP_SELF"), (void *) &temp, sizeof(zval **), NULL);
- }
- }
- }
- if (PHAR_GLOBALS->phar_SERVER_mung_list & PHAR_MUNG_SCRIPT_NAME) {
- #if PHP_MAJOR_VERSION >= 6
- if (phar_find_key(_SERVER, "SCRIPT_NAME", sizeof("SCRIPT_NAME"), (void **) &stuff TSRMLS_CC)) {
- if (Z_TYPE_PP(stuff) == IS_UNICODE) {
- is_unicode = 1;
- zval_unicode_to_string(*stuff TSRMLS_CC);
- } else {
- is_unicode = 0;
- }
- #else
- if (SUCCESS == zend_hash_find(_SERVER, "SCRIPT_NAME", sizeof("SCRIPT_NAME"), (void **) &stuff)) {
- #endif
- path_info = Z_STRVAL_PP(stuff);
- code = Z_STRLEN_PP(stuff);
- ZVAL_STRINGL(*stuff, entry, entry_len, 1);
- MAKE_STD_ZVAL(temp);
- ZVAL_STRINGL(temp, path_info, code, 0);
- #if PHP_MAJOR_VERSION >= 6
- if (is_unicode) {
- zval_string_to_unicode(*stuff TSRMLS_CC);
- }
- #endif
- zend_hash_update(_SERVER, "PHAR_SCRIPT_NAME", sizeof("PHAR_SCRIPT_NAME"), (void *) &temp, sizeof(zval **), NULL);
- }
- }
- if (PHAR_GLOBALS->phar_SERVER_mung_list & PHAR_MUNG_SCRIPT_FILENAME) {
- #if PHP_MAJOR_VERSION >= 6
- if (phar_find_key(_SERVER, "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME"), (void **) &stuff TSRMLS_CC)) {
- if (Z_TYPE_PP(stuff) == IS_UNICODE) {
- is_unicode = 1;
- zval_unicode_to_string(*stuff TSRMLS_CC);
- } else {
- is_unicode = 0;
- }
- #else
- if (SUCCESS == zend_hash_find(_SERVER, "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME"), (void **) &stuff)) {
- #endif
- path_info = Z_STRVAL_PP(stuff);
- code = Z_STRLEN_PP(stuff);
- Z_STRLEN_PP(stuff) = spprintf(&(Z_STRVAL_PP(stuff)), 4096, "phar://%s%s", fname, entry);
- MAKE_STD_ZVAL(temp);
- ZVAL_STRINGL(temp, path_info, code, 0);
- #if PHP_MAJOR_VERSION >= 6
- if (is_unicode) {
- zval_string_to_unicode(*stuff TSRMLS_CC);
- }
- #endif
- zend_hash_update(_SERVER, "PHAR_SCRIPT_FILENAME", sizeof("PHAR_SCRIPT_FILENAME"), (void *) &temp, sizeof(zval **), NULL);
- }
- }
- }
- /* }}} */
- static int phar_file_action(phar_archive_data *phar, phar_entry_info *info, char *mime_type, int code, char *entry, int entry_len, char *arch, char *basename, char *ru, int ru_len TSRMLS_DC) /* {{{ */
- {
- char *name = NULL, buf[8192], *cwd;
- zend_syntax_highlighter_ini syntax_highlighter_ini;
- sapi_header_line ctr = {0};
- size_t got;
- int dummy = 1, name_len;
- zend_file_handle file_handle;
- zend_op_array *new_op_array;
- zval *result = NULL;
- php_stream *fp;
- off_t position;
- switch (code) {
- case PHAR_MIME_PHPS:
- efree(basename);
- /* highlight source */
- if (entry[0] == '/') {
- name_len = spprintf(&name, 4096, "phar://%s%s", arch, entry);
- } else {
- name_len = spprintf(&name, 4096, "phar://%s/%s", arch, entry);
- }
- php_get_highlight_struct(&syntax_highlighter_ini);
- highlight_file(name, &syntax_highlighter_ini TSRMLS_CC);
- 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 TSRMLS_CC);
- efree(ctr.line);
- ctr.line_len = spprintf(&(ctr.line), 0, "Content-length: %u", info->uncompressed_filesize);
- sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
- efree(ctr.line);
- if (FAILURE == sapi_send_headers(TSRMLS_C)) {
- zend_bailout();
- }
- /* prepare to output */
- fp = phar_get_efp(info, 1 TSRMLS_CC);
- if (!fp) {
- char *error;
- if (!phar_open_jit(phar, info, &error TSRMLS_CC)) {
- if (error) {
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
- efree(error);
- }
- return -1;
- }
- fp = phar_get_efp(info, 1 TSRMLS_CC);
- }
- position = 0;
- phar_seek_efp(info, 0, SEEK_SET, 0, 1 TSRMLS_CC);
- do {
- got = php_stream_read(fp, buf, MIN(8192, info->uncompressed_filesize - position));
- if (got > 0) {
- PHPWRITE(buf, got);
- position += got;
- if (position == (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 TSRMLS_CC);
- 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);
- }
- file_handle.type = ZEND_HANDLE_FILENAME;
- file_handle.handle.fd = 0;
- file_handle.filename = name;
- file_handle.opened_path = NULL;
- file_handle.free_filename = 0;
- PHAR_G(cwd) = NULL;
- PHAR_G(cwd_len) = 0;
- if (zend_hash_add(&EG(included_files), name, name_len+1, (void *)&dummy, sizeof(int), NULL) == SUCCESS) {
- 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 TSRMLS_CC);
- if (!new_op_array) {
- zend_hash_del(&EG(included_files), name, name_len+1);
- }
- zend_destroy_file_handle(&file_handle TSRMLS_CC);
- } else {
- efree(name);
- new_op_array = NULL;
- }
- #ifdef PHP_WIN32
- efree(arch);
- #endif
- if (new_op_array) {
- EG(return_value_ptr_ptr) = &result;
- EG(active_op_array) = new_op_array;
- zend_try {
- zend_execute(new_op_array TSRMLS_CC);
- 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 TSRMLS_CC);
- efree(new_op_array);
- if (EG(return_value_ptr_ptr) && *EG(return_value_ptr_ptr)) {
- zval_ptr_dtor(EG(return_value_ptr_ptr));
- }
- } 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, int entry_len TSRMLS_DC) /* {{{ */
- {
- sapi_header_line ctr = {0};
- ctr.response_code = 403;
- ctr.line_len = sizeof("HTTP/1.0 403 Access Denied");
- ctr.line = "HTTP/1.0 403 Access Denied";
- sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
- sapi_send_headers(TSRMLS_C);
- 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(entry, entry_len);
- 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, int fname_len, char *f404, int f404_len, char *entry, int entry_len TSRMLS_DC) /* {{{ */
- {
- sapi_header_line ctr = {0};
- phar_entry_info *info;
- if (phar && f404_len) {
- info = phar_get_entry_info(phar, f404, f404_len, NULL, 1 TSRMLS_CC);
- if (info) {
- phar_file_action(phar, info, "text/html", PHAR_MIME_PHP, f404, f404_len, fname, NULL, NULL, 0 TSRMLS_CC);
- 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 TSRMLS_CC);
- sapi_send_headers(TSRMLS_C);
- 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(entry, entry_len);
- 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, int fname_len, char **entry, int *entry_len, char **ru, int *ru_len TSRMLS_DC) /* {{{ */
- {
- char *e = *entry + 1, *u = NULL, *u1 = NULL, *saveu = NULL;
- int e_len = *entry_len - 1, u_len = 0;
- phar_archive_data **pphar = NULL;
- /* we already know we can retrieve the phar if we reach here */
- zend_hash_find(&(PHAR_GLOBALS->phar_fname_map), fname, fname_len, (void **) &pphar);
- if (!pphar && PHAR_G(manifest_cached)) {
- zend_hash_find(&cached_phars, fname, fname_len, (void **) &pphar);
- }
- do {
- if (zend_hash_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;
- if (e_len < 0) {
- if (saveu) {
- saveu[0] = '/';
- }
- return;
- }
- } 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;
- int fname_len, arch_len, entry_len;
- zend_bool retphar = 1;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &retphar) == FAILURE) {
- return;
- }
- fname = zend_get_executed_filename(TSRMLS_C);
- 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 TSRMLS_CC)) {
- efree(entry);
- if (retphar) {
- RETVAL_STRINGL(fname, arch_len + 7, 1);
- efree(arch);
- return;
- } else {
- RETURN_STRINGL(arch, arch_len, 0);
- }
- }
- RETURN_STRINGL("", 0, 1);
- }
- /* }}} */
- /* {{{ 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;
- int fname_len, arch_len, entry_len, path_len, actual_len;
- phar_archive_data **pphar;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &path, &path_len, &actual, &actual_len) == FAILURE) {
- return;
- }
- fname = zend_get_executed_filename(TSRMLS_C);
- fname_len = strlen(fname);
- #ifdef PHP_WIN32
- 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 TSRMLS_CC)) {
- efree(entry);
- entry = NULL;
- if (path_len > 7 && !memcmp(path, "phar://", 7)) {
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Can only mount internal paths within a phar archive, use a relative path instead of \"%s\"", path);
- efree(arch);
- return;
- }
- carry_on2:
- if (SUCCESS != zend_hash_find(&(PHAR_GLOBALS->phar_fname_map), arch, arch_len, (void **)&pphar)) {
- if (PHAR_G(manifest_cached) && SUCCESS == zend_hash_find(&cached_phars, arch, arch_len, (void **)&pphar)) {
- if (SUCCESS == phar_copy_on_write(pphar TSRMLS_CC)) {
- goto carry_on;
- }
- }
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s is not a phar archive, cannot mount", arch);
- if (arch) {
- efree(arch);
- }
- return;
- }
- carry_on:
- if (SUCCESS != phar_mount_entry(*pphar, actual, actual_len, path, path_len TSRMLS_CC)) {
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Mounting of %s to %s within phar %s failed", path, actual, arch);
- if (path && path == entry) {
- efree(entry);
- }
- if (arch) {
- efree(arch);
- }
- return;
- }
- if (entry && path && path == entry) {
- efree(entry);
- }
- if (arch) {
- efree(arch);
- }
- return;
- } else if (SUCCESS == zend_hash_find(&(PHAR_GLOBALS->phar_fname_map), fname, fname_len, (void **)&pphar)) {
- goto carry_on;
- } else if (PHAR_G(manifest_cached) && SUCCESS == zend_hash_find(&cached_phars, fname, fname_len, (void **)&pphar)) {
- if (SUCCESS == phar_copy_on_write(pphar TSRMLS_CC)) {
- goto carry_on;
- }
- goto carry_on;
- } else if (SUCCESS == phar_split_fname(path, path_len, &arch, &arch_len, &entry, &entry_len, 2, 0 TSRMLS_CC)) {
- path = entry;
- path_len = entry_len;
- goto carry_on2;
- }
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Mounting of %s to %s failed", path, actual);
- }
- /* }}} */
- /* {{{ 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;
- int alias_len = 0, ret, f404_len = 0, free_pathinfo = 0, ru_len = 0;
- char *fname, *basename, *path_info, *mime_type = NULL, *entry, *pt;
- int fname_len, entry_len, code, index_php_len = 0, not_cgi;
- phar_archive_data *phar = NULL;
- phar_entry_info *info;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!saz", &alias, &alias_len, &index_php, &index_php_len, &f404, &f404_len, &mimeoverride, &rewrite) == FAILURE) {
- return;
- }
- phar_request_initialize(TSRMLS_C);
- fname = zend_get_executed_filename(TSRMLS_C);
- fname_len = strlen(fname);
- if (phar_open_executed_filename(alias, alias_len, &error TSRMLS_CC) != SUCCESS) {
- if (error) {
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%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")))) {
- return;
- }
- #ifdef PHP_WIN32
- 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 ((strlen(sapi_module.name) == sizeof("cgi-fcgi")-1 && !strncmp(sapi_module.name, "cgi-fcgi", sizeof("cgi-fcgi")-1))
- || (strlen(sapi_module.name) == sizeof("cgi")-1 && !strncmp(sapi_module.name, "cgi", sizeof("cgi")-1))) {
- if (PG(http_globals)[TRACK_VARS_SERVER]) {
- HashTable *_server = Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]);
- zval **z_script_name, **z_path_info;
- if (SUCCESS != zend_hash_find(_server, "SCRIPT_NAME", sizeof("SCRIPT_NAME"), (void**)&z_script_name) ||
- IS_STRING != Z_TYPE_PP(z_script_name) ||
- !strstr(Z_STRVAL_PP(z_script_name), basename)) {
- return;
- }
- if (SUCCESS == zend_hash_find(_server, "PATH_INFO", sizeof("PATH_INFO"), (void**)&z_path_info) &&
- IS_STRING == Z_TYPE_PP(z_path_info)) {
- entry_len = Z_STRLEN_PP(z_path_info);
- entry = estrndup(Z_STRVAL_PP(z_path_info), entry_len);
- path_info = emalloc(Z_STRLEN_PP(z_script_name) + entry_len + 1);
- memcpy(path_info, Z_STRVAL_PP(z_script_name), Z_STRLEN_PP(z_script_name));
- memcpy(path_info + Z_STRLEN_PP(z_script_name), entry, entry_len + 1);
- free_pathinfo = 1;
- } else {
- entry_len = 0;
- entry = estrndup("", 0);
- path_info = Z_STRVAL_PP(z_script_name);
- }
- pt = estrndup(Z_STRVAL_PP(z_script_name), Z_STRLEN_PP(z_script_name));
- } else {
- char *testit;
- testit = sapi_getenv("SCRIPT_NAME", sizeof("SCRIPT_NAME")-1 TSRMLS_CC);
- if (!(pt = strstr(testit, basename))) {
- efree(testit);
- return;
- }
- path_info = sapi_getenv("PATH_INFO", sizeof("PATH_INFO")-1 TSRMLS_CC);
- 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_ptr, **zp[1];
- MAKE_STD_ZVAL(params);
- ZVAL_STRINGL(params, entry, entry_len, 1);
- zp[0] = ¶ms;
- #if PHP_VERSION_ID < 50300
- if (FAILURE == zend_fcall_info_init(rewrite, &fci, &fcc TSRMLS_CC)) {
- #else
- if (FAILURE == zend_fcall_info_init(rewrite, 0, &fci, &fcc, NULL, NULL TSRMLS_CC)) {
- #endif
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar error: invalid rewrite callback");
- if (free_pathinfo) {
- efree(path_info);
- }
- return;
- }
- fci.param_count = 1;
- fci.params = zp;
- #if PHP_VERSION_ID < 50300
- ++(params->refcount);
- #else
- Z_ADDREF_P(params);
- #endif
- fci.retval_ptr_ptr = &retval_ptr;
- if (FAILURE == zend_call_function(&fci, &fcc TSRMLS_CC)) {
- if (!EG(exception)) {
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar error: failed to call rewrite callback");
- }
- if (free_pathinfo) {
- efree(path_info);
- }
- return;
- }
- if (!fci.retval_ptr_ptr || !retval_ptr) {
- if (free_pathinfo) {
- efree(path_info);
- }
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar error: rewrite callback must return a string or false");
- return;
- }
- switch (Z_TYPE_P(retval_ptr)) {
- #if PHP_VERSION_ID >= 60000
- case IS_UNICODE:
- zval_unicode_to_string(retval_ptr TSRMLS_CC);
- /* break intentionally omitted */
- #endif
- case IS_STRING:
- efree(entry);
- if (fci.retval_ptr_ptr != &retval_ptr) {
- entry = estrndup(Z_STRVAL_PP(fci.retval_ptr_ptr), Z_STRLEN_PP(fci.retval_ptr_ptr));
- entry_len = Z_STRLEN_PP(fci.retval_ptr_ptr);
- } else {
- entry = Z_STRVAL_P(retval_ptr);
- entry_len = Z_STRLEN_P(retval_ptr);
- }
- break;
- case IS_BOOL:
- phar_do_403(entry, entry_len TSRMLS_CC);
- if (free_pathinfo) {
- efree(path_info);
- }
- zend_bailout();
- return;
- default:
- efree(retval_ptr);
- if (free_pathinfo) {
- efree(path_info);
- }
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar error: rewrite callback must return a string or false");
- return;
- }
- }
- if (entry_len) {
- phar_postprocess_ru_web(fname, fname_len, &entry, &entry_len, &ru, &ru_len TSRMLS_CC);
- }
- 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 TSRMLS_CC) ||
- (info = phar_get_entry_info(phar, entry, entry_len, NULL, 0 TSRMLS_CC)) == NULL) {
- phar_do_404(phar, fname, fname_len, f404, f404_len, entry, entry_len TSRMLS_CC);
- if (free_pathinfo) {
- efree(path_info);
- }
- zend_bailout();
- } else {
- char *tmp, sa;
- 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 TSRMLS_CC);
- 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 TSRMLS_CC);
- sapi_send_headers(TSRMLS_C);
- efree(ctr.line);
- zend_bailout();
- }
- }
- if (FAILURE == phar_get_archive(&phar, fname, fname_len, NULL, 0, NULL TSRMLS_CC) ||
- (info = phar_get_entry_info(phar, entry, entry_len, NULL, 0 TSRMLS_CC)) == NULL) {
- phar_do_404(phar, fname, fname_len, f404, f404_len, entry, entry_len TSRMLS_CC);
- #ifdef PHP_WIN32
- efree(fname);
- #endif
- zend_bailout();
- }
- if (mimeoverride && zend_hash_num_elements(Z_ARRVAL_P(mimeoverride))) {
- char *ext = zend_memrchr(entry, '.', entry_len);
- zval **val;
- if (ext) {
- ++ext;
- #if PHP_MAJOR_VERSION >= 6
- if (phar_find_key(Z_ARRVAL_P(mimeoverride), ext, strlen(ext)+1, (void **) &val TSRMLS_CC)) {
- #else
- if (SUCCESS == zend_hash_find(Z_ARRVAL_P(mimeoverride), ext, strlen(ext)+1, (void **) &val)) {
- #endif
- switch (Z_TYPE_PP(val)) {
- case IS_LONG:
- if (Z_LVAL_PP(val) == PHAR_MIME_PHP || Z_LVAL_PP(val) == PHAR_MIME_PHPS) {
- mime_type = "";
- code = Z_LVAL_PP(val);
- } else {
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Unknown mime type specifier used, only Phar::PHP, Phar::PHPS and a mime type string are allowed");
- #ifdef PHP_WIN32
- efree(fname);
- #endif
- RETURN_FALSE;
- }
- break;
- #if PHP_MAJOR_VERSION >= 6
- case IS_UNICODE:
- zval_unicode_to_string(*(val) TSRMLS_CC);
- /* break intentionally omitted */
- #endif
- case IS_STRING:
- mime_type = Z_STRVAL_PP(val);
- code = PHAR_MIME_OTHER;
- break;
- default:
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Unknown mime type specifier used (not a string or int), only Phar::PHP, Phar::PHPS and a mime type string are allowed");
- #ifdef PHP_WIN32
- efree(fname);
- #endif
- RETURN_FALSE;
- }
- }
- }
- }
- if (!mime_type) {
- code = phar_file_type(&PHAR_G(mime_types), entry, &mime_type TSRMLS_CC);
- }
- ret = phar_file_action(phar, info, mime_type, code, entry, entry_len, fname, pt, ru, ru_len TSRMLS_CC);
- }
- /* }}} */
- /* {{{ 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;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &mungvalues) == FAILURE) {
- return;
- }
- if (!zend_hash_num_elements(Z_ARRVAL_P(mungvalues))) {
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "No values passed to Phar::mungServer(), expecting an array of any of these strings: PHP_SELF, REQUEST_URI, SCRIPT_FILENAME, SCRIPT_NAME");
- return;
- }
- if (zend_hash_num_elements(Z_ARRVAL_P(mungvalues)) > 4) {
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Too many values passed to Phar::mungServer(), expecting an array of any of these strings: PHP_SELF, REQUEST_URI, SCRIPT_FILENAME, SCRIPT_NAME");
- return;
- }
- phar_request_initialize(TSRMLS_C);
- for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(mungvalues)); SUCCESS == zend_hash_has_more_elements(Z_ARRVAL_P(mungvalues)); zend_hash_move_forward(Z_ARRVAL_P(mungvalues))) {
- zval **data = NULL;
- #if PHP_MAJOR_VERSION >= 6
- zval *unicopy = NULL;
- #endif
- if (SUCCESS != zend_hash_get_current_data(Z_ARRVAL_P(mungvalues), (void **) &data)) {
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "unable to retrieve array value in Phar::mungServer()");
- return;
- }
- #if PHP_MAJOR_VERSION >= 6
- if (Z_TYPE_PP(data) == IS_UNICODE) {
- MAKE_STD_ZVAL(unicopy);
- *unicopy = **data;
- zval_copy_ctor(unicopy);
- INIT_PZVAL(unicopy);
- zval_unicode_to_string(unicopy TSRMLS_CC);
- data = &unicopy;
- }
- #endif
- if (Z_TYPE_PP(data) != IS_STRING) {
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Non-string value passed to Phar::mungServer(), expecting an array of any of these strings: PHP_SELF, REQUEST_URI, SCRIPT_FILENAME, SCRIPT_NAME");
- return;
- }
- if (Z_STRLEN_PP(data) == sizeof("PHP_SELF")-1 && !strncmp(Z_STRVAL_PP(data), "PHP_SELF", sizeof("PHP_SELF")-1)) {
- PHAR_GLOBALS->phar_SERVER_mung_list |= PHAR_MUNG_PHP_SELF;
- }
- if (Z_STRLEN_PP(data) == sizeof("REQUEST_URI")-1) {
- if (!strncmp(Z_STRVAL_PP(data), "REQUEST_URI", sizeof("REQUEST_URI")-1)) {
- PHAR_GLOBALS->phar_SERVER_mung_list |= PHAR_MUNG_REQUEST_URI;
- }
- if (!strncmp(Z_STRVAL_PP(data), "SCRIPT_NAME", sizeof("SCRIPT_NAME")-1)) {
- PHAR_GLOBALS->phar_SERVER_mung_list |= PHAR_MUNG_SCRIPT_NAME;
- }
- }
- if (Z_STRLEN_PP(data) == sizeof("SCRIPT_FILENAME")-1 && !strncmp(Z_STRVAL_PP(data), "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME")-1)) {
- PHAR_GLOBALS->phar_SERVER_mung_list |= PHAR_MUNG_SCRIPT_FILENAME;
- }
- #if PHP_MAJOR_VERSION >= 6
- if (unicopy) {
- zval_ptr_dtor(&unicopy);
- }
- #endif
- }
- }
- /* }}} */
- /* {{{ 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)
- {
- phar_intercept_functions(TSRMLS_C);
- }
- /* }}} */
- /* {{{ 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, *stub, *error;
- int index_len = 0, webindex_len = 0;
- size_t stub_len;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ss", &index, &index_len, &webindex, &webindex_len) == FAILURE) {
- return;
- }
- stub = phar_create_default_stub(index, webindex, &stub_len, &error TSRMLS_CC);
- if (error) {
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
- efree(error);
- return;
- }
- RETURN_STRINGL(stub, stub_len, 0);
- }
- /* }}} */
- /* {{{ 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;
- int alias_len = 0;
- long dataoffset = 0;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!l", &alias, &alias_len, &dataoffset) == FAILURE) {
- return;
- }
- phar_request_initialize(TSRMLS_C);
- RETVAL_BOOL(phar_open_executed_filename(alias, alias_len, &error TSRMLS_CC) == SUCCESS);
- if (error) {
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%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;
- int fname_len, alias_len = 0;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s!", &fname, &fname_len, &alias, &alias_len) == FAILURE) {
- return;
- }
- phar_request_initialize(TSRMLS_C);
- RETVAL_BOOL(phar_open_from_filename(fname, fname_len, alias, alias_len, REPORT_ERRORS, NULL, &error TSRMLS_CC) == SUCCESS);
- if (error) {
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
- efree(error);
- }
- } /* }}} */
- /* {{{ proto string Phar::apiVersion()
- * Returns the api version */
- PHP_METHOD(Phar, apiVersion)
- {
- RETURN_STRINGL(PHP_PHAR_API_VERSION, sizeof(PHP_PHAR_API_VERSION)-1, 1);
- }
- /* }}}*/
- /* {{{ proto bool Phar::canCompress([int method])
- * Returns whether phar extension supports compression using zlib/bzip2 */
- PHP_METHOD(Phar, canCompress)
- {
- long method = 0;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &method) == FAILURE) {
- return;
- }
- phar_request_initialize(TSRMLS_C);
- 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)
- {
- 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;
- int fname_len, ext_len, is_executable;
- zend_bool executable = 1;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &fname, &fname_len, &executable) == FAILURE) {
- return;
- }
- is_executable = executable;
- RETVAL_BOOL(phar_detect_phar_fname_ext(fname, fname_len, &ext_str, &ext_len, is_executable, 2, 1 TSRMLS_CC) == SUCCESS);
- }
- /* }}} */
- #if HAVE_SPL
- /**
- * from spl_directory
- */
- static void phar_spl_foreign_dtor(spl_filesystem_object *object TSRMLS_DC) /* {{{ */
- {
- phar_archive_data *phar = (phar_archive_data *) object->oth;
- if (!phar->is_persistent) {
- phar_archive_delref(phar TSRMLS_CC);
- }
- object->oth = NULL;
- }
- /* }}} */
- /**
- * from spl_directory
- */
- static void phar_spl_foreign_clone(spl_filesystem_object *src, spl_filesystem_object *dst TSRMLS_DC) /* {{{ */
- {
- phar_archive_data *phar_data = (phar_archive_data *) dst->oth;
- if (!phar_data->is_persistent) {
- ++(phar_data->refcount);
- }
- }
- /* }}} */
- static spl_other_handler phar_spl_foreign_handler = {
- phar_spl_foreign_dtor,
- phar_spl_foreign_clone
- };
- #endif /* HAVE_SPL */
- /* {{{ proto void Phar::__construct(string fname [, int flags [, string alias]])
- * Construct a Phar archive object
- *
- * proto void 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)
- {
- #if !HAVE_SPL
- zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Cannot instantiate Phar object without SPL extension");
- #else
- char *fname, *alias = NULL, *error, *arch = NULL, *entry = NULL, *save_fname;
- int fname_len, alias_len = 0, arch_len, entry_len, is_data;
- #if PHP_VERSION_ID < 50300
- long flags = 0;
- #else
- long flags = SPL_FILE_DIR_SKIPDOTS|SPL_FILE_DIR_UNIXPATHS;
- #endif
- long format = 0;
- phar_archive_object *phar_obj;
- phar_archive_data *phar_data;
- zval *zobj = getThis(), arg1, arg2;
- phar_obj = (phar_archive_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
- is_data = instanceof_function(Z_OBJCE_P(zobj), phar_ce_data TSRMLS_CC);
- if (is_data) {
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ls!l", &fname, &fname_len, &flags, &alias, &alias_len, &format) == FAILURE) {
- return;
- }
- } else {
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ls!", &fname, &fname_len, &flags, &alias, &alias_len) == FAILURE) {
- return;
- }
- }
- if (phar_obj->arc.archive) {
- zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Cannot call constructor twice");
- return;
- }
- save_fname = fname;
- if (SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, !is_data, 2 TSRMLS_CC)) {
- /* 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 TSRMLS_CC) == 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 TSRMLS_CC,
- "%s", error);
- efree(error);
- } else {
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
- "Phar creation or opening failed");
- }
- return;
- }
- 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 TSRMLS_CC,
- "PharData class can only be used for non-executable tar and zip archives");
- } else {
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
- "Phar class can only be used for executable tar and zip archives");
- }
- efree(entry);
- return;
- }
- is_data = phar_data->is_data;
- if (!phar_data->is_persistent) {
- ++(phar_data->refcount);
- }
- phar_obj->arc.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);
- }
- INIT_PZVAL(&arg1);
- ZVAL_STRINGL(&arg1, fname, fname_len, 0);
- INIT_PZVAL(&arg2);
- ZVAL_LONG(&arg2, flags);
- zend_call_method_with_2_params(&zobj, Z_OBJCE_P(zobj),
- &spl_ce_RecursiveDirectoryIterator->constructor, "__construct", NULL, &arg1, &arg2);
- if (!phar_data->is_persistent) {
- phar_obj->arc.archive->is_data = is_data;
- } else if (!EG(exception)) {
- /* register this guy so we can modify if necessary */
- zend_hash_add(&PHAR_GLOBALS->phar_persist_map, (const char *) phar_obj->arc.archive, sizeof(phar_obj->arc.archive), (void *) &phar_obj, sizeof(phar_archive_object **), NULL);
- }
- phar_obj->spl.info_class = phar_ce_entry;
- efree(fname);
- #endif /* HAVE_SPL */
- }
- /* }}} */
- /* {{{ proto array Phar::getSupportedSignatures()
- * Return array of supported signature types
- */
- PHP_METHOD(Phar, getSupportedSignatures)
- {
- array_init(return_value);
- add_next_index_stringl(return_value, "MD5", 3, 1);
- add_next_index_stringl(return_value, "SHA-1", 5, 1);
- #ifdef PHAR_HASH_OK
- add_next_index_stringl(return_value, "SHA-256", 7, 1);
- add_next_index_stringl(return_value, "SHA-512", 7, 1);
- #endif
- #if PHAR_HAVE_OPENSSL
- add_next_index_stringl(return_value, "OpenSSL", 7, 1);
- #else
- if (zend_hash_exists(&module_registry, "openssl", sizeof("openssl"))) {
- add_next_index_stringl(return_value, "OpenSSL", 7, 1);
- }
- #endif
- }
- /* }}} */
- /* {{{ proto array Phar::getSupportedCompression()
- * Return array of supported comparession algorithms
- */
- PHP_METHOD(Phar, getSupportedCompression)
- {
- array_init(return_value);
- phar_request_initialize(TSRMLS_C);
- if (PHAR_G(has_zlib)) {
- add_next_index_stringl(return_value, "GZ", 2, 1);
- }
- if (PHAR_G(has_bz2)) {
- add_next_index_stringl(return_value, "BZIP2", 5, 1);
- }
- }
- /* }}} */
- /* {{{ 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;
- int fname_len, zname_len, arch_len, entry_len;
- phar_archive_data *phar;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &fname, &fname_len) == FAILURE) {
- RETURN_FALSE;
- }
- if (!fname_len) {
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Unknown phar archive \"\"");
- return;
- }
- if (FAILURE == phar_open_from_filename(fname, fname_len, NULL, 0, REPORT_ERRORS, &phar, &error TSRMLS_CC)) {
- if (error) {
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Unknown phar archive \"%s\": %s", fname, error);
- efree(error);
- } else {
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Unknown phar archive \"%s\"", fname);
- }
- return;
- }
- zname = zend_get_executed_filename(TSRMLS_C);
- 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 TSRMLS_CC)) {
- if (arch_len == fname_len && !memcmp(arch, fname, arch_len)) {
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar archive \"%s\" cannot be unlinked from within itself", fname);
- efree(arch);
- efree(entry);
- return;
- }
- efree(arch);
- efree(entry);
- }
- if (phar->is_persistent) {
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar archive \"%s\" is in phar.cache_list, cannot unlinkArchive()", fname);
- return;
- }
- if (phar->refcount) {
- zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar archive \"%s\" has open file handles or objects. fclose() all file handles, and unset() all objects prior to calling unlinkArchive()", fname);
- return;
- }
- 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 TSRMLS_CC);
- unlink(fname);
- efree(fname);
- RETURN_TRUE;
- }
- /* }}} */
- #if HAVE_SPL
- #define PHAR_ARCHIVE_OBJECT() \
- phar_archive_object *phar_obj = (phar_archive_object*)zend_object_store_get_object(getThis() TSRMLS_CC); \
- if (!phar_obj->arc.archive) { \
- zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
- "Cannot call method on an uninitialized Phar object"); \
- return; \
- }
- /* {{{ proto void Phar::__destruct()
- * if persistent, remove from the cache
- */
- PHP_METHOD(Phar, __destruct)
- {
- phar_archive_object *phar_obj = (phar_archive_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
- if (phar_obj->arc.archive && phar_obj->arc.archive->is_persistent) {
- zend_hash_del(&PHAR_GLOBALS->phar_persist_map, (const char *) phar_obj->arc.archive, sizeof(phar_obj->arc.archive));
- }
- }
- /* }}} */
- struct _phar_t {
- phar_archive_object *p;
- zend_class_entry *c;
- char *b;
- uint l;
- zval *ret;
- int count;
- php_stream *fp;
- };
- static int phar_build(zend_object_iterator *iter, void *puser TSRMLS_DC) /* {{{ */
- {
- zval **value;
- zend_uchar key_type;
- zend_bool close_fp = 1;
- ulong int_key;
- struct _phar_t *p_obj = (struct _phar_t*) puser;
- uint str_key_len, base_len = p_obj->l, fname_len;
- phar_entry_data *data;
- php_stream *fp;
- size_t contents_len;
- char *fname, *error = NULL, *base = p_obj->b, *opened, *save = NULL, *temp = NULL;
- phar_zstr key;
- char *str_key;
- zend_class_entry *ce = p_obj->c;
- phar_archive_object *phar_obj = p_obj->p;
- char *str = "[stream]";
- iter->funcs->get_current_data(iter, &value TSRMLS_CC);
- if (EG(exception)) {
- return ZEND_HASH_APPLY_STOP;
- }
- if (!value) {
- /* failure in get_current_data */
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned no value", ce->name);
- return ZEND_HASH_APPLY_STOP;
- }
- switch (Z_TYPE_PP(value)) {
- #if PHP_VERSION_ID >= 60000
- case IS_UNICODE:
- zval_unicode_to_string(*(value) TSRMLS_CC);
- /* break intentionally omitted */
- #endif
- 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 TSRMLS_CC, "Iterator %v returned an invalid stream handle", ce->name);
- return ZEND_HASH_APPLY_STOP;
- }
- if (iter->funcs->get_current_key) {
- key_type = iter->funcs->get_current_key(iter, &key, &str_key_len, &int_key TSRMLS_CC);
- if (EG(exception)) {
- return ZEND_HASH_APPLY_STOP;
- }
- if (key_type == HASH_KEY_IS_LONG) {
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned an invalid key (must return a string)", ce->name);
- return ZEND_HASH_APPLY_STOP;
- }
- if (key_type > 9) { /* IS_UNICODE == 10 */
- #if PHP_VERSION_ID < 60000
- /* this can never happen, but fixes a compile warning */
- spprintf(&str_key, 0, "%s", key);
- #else
- spprintf(&str_key, 0, "%v", key);
- ezfree(key);
- #endif
- } else {
- PHAR_STR(key, str_key);
- }
- save = str_key;
- if (str_key[str_key_len - 1] == '\0') {
- str_key_len--;
- }
- } else {
- zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned an invalid key (must return a string)", ce->name);
- return ZEND_HASH_APPLY_STOP;
- }
- close_fp = 0;
- opened = (char *) estrndup(str, sizeof("[stream]") + 1);
- goto after_open_fp;
- case IS_OBJECT:
- if (instanceof_function(Z_OBJCE_PP(value), spl_ce_SplFileInfo TSRMLS_CC)) {
- char *test = NULL;
- zval dummy;
- spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(*value TSRMLS_CC);
- if (!base_len) {
- zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Iterator %v returns an SplFileInfo object, so base directory must be specified", ce->name);
- return ZEND_HASH_APPLY_STOP;
- }
- switch (intern->type) {
- case SPL_FS_DIR:
- #if PHP_VERSION_ID >= 60000
- test = spl_filesystem_object_get_path(intern, NULL, NULL TSRMLS_CC).s;
- #elif PHP_VERSION_ID >= 50300
- test = spl_filesystem_object_get_path(intern, NULL TSRMLS_CC);
- #else
- test = intern->path;
- #endif
- 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 TSRMLS_CC);
- if (Z_BVAL(dummy)) {
- /* ignore directories */
- efree(fname);
- return ZEND_HASH_APPLY_KEEP;
- }
- test = expand_filepath(fname, NULL TSRMLS_CC);
- if (test) {
- efree(fname);
- fname = test;
- fname_len = strlen(fname);
- }
- save = fname;
- goto phar_spl_fileinfo;
- case SPL_FS_INFO:
- case SPL_FS_FILE:
- #if PHP_VERSION_ID >= 60000
- if (intern->file_name_type == IS_UNICODE) {
- zval zv;
- INIT_ZVAL(zv);
- Z_UNIVAL(zv) = intern->file_name;
- Z_UNILEN(zv) = intern->file_name_len;
- Z_TYPE(zv) = IS_UNICODE;
- zval_copy_ctor(&zv);
- zval_unicode_to_string(&zv TSRMLS_CC);
- fname = expand_filepath(Z_STRVAL(zv), NULL TSRMLS_CC);
- ezfree(Z_UNIVAL(zv));
- } else {
- fname = expand_filepath(intern->file_name.s, NULL TSRMLS_CC);
- }
- #else
- fname = expand_filepath(intern->file_name, NULL TSRMLS_CC);
- #endif
- fname_len = strlen(fname);
- save = fname;
- goto phar_spl_fileinfo;…
Large files files are truncated, but you can click here to view the full file