PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/phar - phar_object.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 1936 2479 78.1 %
Date: 2016-09-27 Functions: 90 91 98.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | phar php single-file executable PHP extension                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 2005-2016 The PHP Group                                |
       6             :   +----------------------------------------------------------------------+
       7             :   | This source file is subject to version 3.01 of the PHP license,      |
       8             :   | that is bundled with this package in the file LICENSE, and is        |
       9             :   | available through the world-wide-web at the following url:           |
      10             :   | http://www.php.net/license/3_01.txt.                                 |
      11             :   | If you did not receive a copy of the PHP license and are unable to   |
      12             :   | obtain it through the world-wide-web, please send a note to          |
      13             :   | license@php.net so we can mail you a copy immediately.               |
      14             :   +----------------------------------------------------------------------+
      15             :   | Authors: Gregory Beaver <cellog@php.net>                             |
      16             :   |          Marcus Boerger <helly@php.net>                              |
      17             :   +----------------------------------------------------------------------+
      18             : */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : #include "phar_internal.h"
      23             : #include "func_interceptors.h"
      24             : 
      25             : static zend_class_entry *phar_ce_archive;
      26             : static zend_class_entry *phar_ce_data;
      27             : static zend_class_entry *phar_ce_PharException;
      28             : static zend_class_entry *phar_ce_entry;
      29             : 
      30          39 : static int phar_file_type(HashTable *mimes, char *file, char **mime_type) /* {{{ */
      31             : {
      32             :         char *ext;
      33             :         phar_mime_type *mime;
      34          39 :         ext = strrchr(file, '.');
      35          39 :         if (!ext) {
      36           2 :                 *mime_type = "text/plain";
      37             :                 /* no file extension = assume text/plain */
      38           2 :                 return PHAR_MIME_OTHER;
      39             :         }
      40          37 :         ++ext;
      41          74 :         if (NULL == (mime = zend_hash_str_find_ptr(mimes, ext, strlen(ext)))) {
      42           6 :                 *mime_type = "application/octet-stream";
      43           6 :                 return PHAR_MIME_OTHER;
      44             :         }
      45          31 :         *mime_type = mime->mime;
      46          31 :         return mime->type;
      47             : }
      48             : /* }}} */
      49             : 
      50          25 : static void phar_mung_server_vars(char *fname, char *entry, int entry_len, char *basename, int request_uri_len) /* {{{ */
      51             : {
      52             :         HashTable *_SERVER;
      53             :         zval *stuff;
      54             :         char *path_info;
      55          25 :         int basename_len = strlen(basename);
      56             :         int code;
      57             :         zval temp;
      58             : 
      59             :         /* "tweak" $_SERVER variables requested in earlier call to Phar::mungServer() */
      60          25 :         if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_UNDEF) {
      61           4 :                 return;
      62             :         }
      63             : 
      64          21 :         _SERVER = Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]);
      65             : 
      66             :         /* PATH_INFO and PATH_TRANSLATED should always be munged */
      67          21 :         if (NULL != (stuff = zend_hash_str_find(_SERVER, "PATH_INFO", sizeof("PATH_INFO")-1))) {
      68          21 :                 path_info = Z_STRVAL_P(stuff);
      69          21 :                 code = Z_STRLEN_P(stuff);
      70          21 :                 if (code > entry_len && !memcmp(path_info, entry, entry_len)) {
      71           2 :                         ZVAL_STR(&temp, Z_STR_P(stuff));
      72           4 :                         ZVAL_STRINGL(stuff, path_info + entry_len, request_uri_len);
      73           2 :                         zend_hash_str_update(_SERVER, "PHAR_PATH_INFO", sizeof("PHAR_PATH_INFO")-1, &temp);
      74             :                 }
      75             :         }
      76             : 
      77          21 :         if (NULL != (stuff = zend_hash_str_find(_SERVER, "PATH_TRANSLATED", sizeof("PATH_TRANSLATED")-1))) {
      78          21 :                 zend_string *str = strpprintf(4096, "phar://%s%s", fname, entry);
      79             : 
      80          21 :                 ZVAL_STR(&temp, Z_STR_P(stuff));
      81          21 :                 ZVAL_NEW_STR(stuff, str);
      82             : 
      83          21 :                 zend_hash_str_update(_SERVER, "PHAR_PATH_TRANSLATED", sizeof("PHAR_PATH_TRANSLATED")-1, &temp);
      84             :         }
      85             : 
      86          21 :         if (!PHAR_G(phar_SERVER_mung_list)) {
      87          15 :                 return;
      88             :         }
      89             : 
      90           6 :         if (PHAR_G(phar_SERVER_mung_list) & PHAR_MUNG_REQUEST_URI) {
      91           6 :                 if (NULL != (stuff = zend_hash_str_find(_SERVER, "REQUEST_URI", sizeof("REQUEST_URI")-1))) {
      92           6 :                         path_info = Z_STRVAL_P(stuff);
      93           6 :                         code = Z_STRLEN_P(stuff);
      94           6 :                         if (code > basename_len && !memcmp(path_info, basename, basename_len)) {
      95           6 :                                 ZVAL_STR(&temp, Z_STR_P(stuff));
      96          12 :                                 ZVAL_STRINGL(stuff, path_info + basename_len, code - basename_len);
      97           6 :                                 zend_hash_str_update(_SERVER, "PHAR_REQUEST_URI", sizeof("PHAR_REQUEST_URI")-1, &temp);
      98             :                         }
      99             :                 }
     100             :         }
     101             : 
     102           6 :         if (PHAR_G(phar_SERVER_mung_list) & PHAR_MUNG_PHP_SELF) {
     103           6 :                 if (NULL != (stuff = zend_hash_str_find(_SERVER, "PHP_SELF", sizeof("PHP_SELF")-1))) {
     104           6 :                         path_info = Z_STRVAL_P(stuff);
     105           6 :                         code = Z_STRLEN_P(stuff);
     106             : 
     107           6 :                         if (code > basename_len && !memcmp(path_info, basename, basename_len)) {
     108           6 :                                 ZVAL_STR(&temp, Z_STR_P(stuff));
     109          12 :                                 ZVAL_STRINGL(stuff, path_info + basename_len, code - basename_len);
     110           6 :                                 zend_hash_str_update(_SERVER, "PHAR_PHP_SELF", sizeof("PHAR_PHP_SELF")-1, &temp);
     111             :                         }
     112             :                 }
     113             :         }
     114             : 
     115           6 :         if (PHAR_G(phar_SERVER_mung_list) & PHAR_MUNG_SCRIPT_NAME) {
     116           4 :                 if (NULL != (stuff = zend_hash_str_find(_SERVER, "SCRIPT_NAME", sizeof("SCRIPT_NAME")-1))) {
     117           4 :                         ZVAL_STR(&temp, Z_STR_P(stuff));
     118           8 :                         ZVAL_STRINGL(stuff, entry, entry_len);
     119           4 :                         zend_hash_str_update(_SERVER, "PHAR_SCRIPT_NAME", sizeof("PHAR_SCRIPT_NAME")-1, &temp);
     120             :                 }
     121             :         }
     122             : 
     123           6 :         if (PHAR_G(phar_SERVER_mung_list) & PHAR_MUNG_SCRIPT_FILENAME) {
     124           4 :                 if (NULL != (stuff = zend_hash_str_find(_SERVER, "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME")-1))) {
     125           4 :                         zend_string *str = strpprintf(4096, "phar://%s%s", fname, entry);
     126             : 
     127           4 :                         ZVAL_STR(&temp, Z_STR_P(stuff));
     128           4 :                         ZVAL_NEW_STR(stuff, str);
     129             : 
     130           4 :                         zend_hash_str_update(_SERVER, "PHAR_SCRIPT_FILENAME", sizeof("PHAR_SCRIPT_FILENAME")-1, &temp);
     131             :                 }
     132             :         }
     133             : }
     134             : /* }}} */
     135             : 
     136          57 : 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) /* {{{ */
     137             : {
     138          57 :         char *name = NULL, buf[8192];
     139             :         const char *cwd;
     140             :         zend_syntax_highlighter_ini syntax_highlighter_ini;
     141          57 :         sapi_header_line ctr = {0};
     142             :         size_t got;
     143             :         zval dummy;
     144             :         int name_len;
     145             :         zend_file_handle file_handle;
     146             :         zend_op_array *new_op_array;
     147             :         zval result;
     148             :         php_stream *fp;
     149             :         zend_off_t position;
     150             : 
     151          57 :         switch (code) {
     152             :                 case PHAR_MIME_PHPS:
     153          12 :                         efree(basename);
     154             :                         /* highlight source */
     155          12 :                         if (entry[0] == '/') {
     156           8 :                                 name_len = spprintf(&name, 4096, "phar://%s%s", arch, entry);
     157             :                         } else {
     158           4 :                                 name_len = spprintf(&name, 4096, "phar://%s/%s", arch, entry);
     159             :                         }
     160          12 :                         php_get_highlight_struct(&syntax_highlighter_ini);
     161             : 
     162          12 :                         highlight_file(name, &syntax_highlighter_ini);
     163             : 
     164          12 :                         efree(name);
     165             : #ifdef PHP_WIN32
     166             :                         efree(arch);
     167             : #endif
     168          12 :                         zend_bailout();
     169             :                 case PHAR_MIME_OTHER:
     170             :                         /* send headers, output file contents */
     171          18 :                         efree(basename);
     172          18 :                         ctr.line_len = spprintf(&(ctr.line), 0, "Content-type: %s", mime_type);
     173          18 :                         sapi_header_op(SAPI_HEADER_REPLACE, &ctr);
     174          18 :                         efree(ctr.line);
     175          18 :                         ctr.line_len = spprintf(&(ctr.line), 0, "Content-length: %u", info->uncompressed_filesize);
     176          18 :                         sapi_header_op(SAPI_HEADER_REPLACE, &ctr);
     177          18 :                         efree(ctr.line);
     178             : 
     179          18 :                         if (FAILURE == sapi_send_headers()) {
     180           0 :                                 zend_bailout();
     181             :                         }
     182             : 
     183             :                         /* prepare to output  */
     184          18 :                         fp = phar_get_efp(info, 1);
     185             : 
     186          18 :                         if (!fp) {
     187             :                                 char *error;
     188           0 :                                 if (!phar_open_jit(phar, info, &error)) {
     189           0 :                                         if (error) {
     190           0 :                                                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
     191           0 :                                                 efree(error);
     192             :                                         }
     193           0 :                                         return -1;
     194             :                                 }
     195           0 :                                 fp = phar_get_efp(info, 1);
     196             :                         }
     197          18 :                         position = 0;
     198          18 :                         phar_seek_efp(info, 0, SEEK_SET, 0, 1);
     199             : 
     200             :                         do {
     201          20 :                                 got = php_stream_read(fp, buf, MIN(8192, info->uncompressed_filesize - position));
     202          20 :                                 if (got > 0) {
     203          20 :                                         PHPWRITE(buf, got);
     204          20 :                                         position += got;
     205          20 :                                         if (position == (zend_off_t) info->uncompressed_filesize) {
     206          18 :                                                 break;
     207             :                                         }
     208             :                                 }
     209           2 :                         } while (1);
     210             : 
     211          18 :                         zend_bailout();
     212             :                 case PHAR_MIME_PHP:
     213          27 :                         if (basename) {
     214          25 :                                 phar_mung_server_vars(arch, entry, entry_len, basename, ru_len);
     215          25 :                                 efree(basename);
     216             :                         }
     217             : 
     218          27 :                         if (entry[0] == '/') {
     219          25 :                                 name_len = spprintf(&name, 4096, "phar://%s%s", arch, entry);
     220             :                         } else {
     221           2 :                                 name_len = spprintf(&name, 4096, "phar://%s/%s", arch, entry);
     222             :                         }
     223             : 
     224          27 :                         file_handle.type = ZEND_HANDLE_FILENAME;
     225          27 :                         file_handle.handle.fd = 0;
     226          27 :                         file_handle.filename = name;
     227          27 :                         file_handle.opened_path = NULL;
     228          27 :                         file_handle.free_filename = 0;
     229             : 
     230          27 :                         PHAR_G(cwd) = NULL;
     231          27 :                         PHAR_G(cwd_len) = 0;
     232             : 
     233          27 :                         ZVAL_NULL(&dummy);
     234          27 :                         if (zend_hash_str_add(&EG(included_files), name, name_len, &dummy) != NULL) {
     235          54 :                                 if ((cwd = zend_memrchr(entry, '/', entry_len))) {
     236          25 :                                         PHAR_G(cwd_init) = 1;
     237          25 :                                         if (entry == cwd) {
     238             :                                                 /* root directory */
     239          21 :                                                 PHAR_G(cwd_len) = 0;
     240          21 :                                                 PHAR_G(cwd) = NULL;
     241           4 :                                         } else if (entry[0] == '/') {
     242           4 :                                                 PHAR_G(cwd_len) = cwd - (entry + 1);
     243           4 :                                                 PHAR_G(cwd) = estrndup(entry + 1, PHAR_G(cwd_len));
     244             :                                         } else {
     245           0 :                                                 PHAR_G(cwd_len) = cwd - entry;
     246           0 :                                                 PHAR_G(cwd) = estrndup(entry, PHAR_G(cwd_len));
     247             :                                         }
     248             :                                 }
     249             : 
     250          27 :                                 new_op_array = zend_compile_file(&file_handle, ZEND_REQUIRE);
     251             : 
     252          27 :                                 if (!new_op_array) {
     253           0 :                                         zend_hash_str_del(&EG(included_files), name, name_len);
     254             :                                 }
     255             : 
     256          27 :                                 zend_destroy_file_handle(&file_handle);
     257             : 
     258             :                         } else {
     259           0 :                                 efree(name);
     260           0 :                                 new_op_array = NULL;
     261             :                         }
     262             : #ifdef PHP_WIN32
     263             :                         efree(arch);
     264             : #endif
     265          27 :                         if (new_op_array) {
     266          27 :                                 ZVAL_UNDEF(&result);
     267             : 
     268          27 :                                 zend_try {
     269          27 :                                         zend_execute(new_op_array, &result);
     270          27 :                                         if (PHAR_G(cwd)) {
     271           4 :                                                 efree(PHAR_G(cwd));
     272           4 :                                                 PHAR_G(cwd) = NULL;
     273           4 :                                                 PHAR_G(cwd_len) = 0;
     274             :                                         }
     275             : 
     276          27 :                                         PHAR_G(cwd_init) = 0;
     277          27 :                                         efree(name);
     278          27 :                                         destroy_op_array(new_op_array);
     279          27 :                                         efree(new_op_array);
     280          27 :                                         zval_ptr_dtor(&result);
     281           0 :                                 } zend_catch {
     282           0 :                                         if (PHAR_G(cwd)) {
     283           0 :                                                 efree(PHAR_G(cwd));
     284           0 :                                                 PHAR_G(cwd) = NULL;
     285           0 :                                                 PHAR_G(cwd_len) = 0;
     286             :                                         }
     287             : 
     288           0 :                                         PHAR_G(cwd_init) = 0;
     289           0 :                                         efree(name);
     290          27 :                                 } zend_end_try();
     291             : 
     292          27 :                                 zend_bailout();
     293             :                         }
     294             : 
     295           0 :                         return PHAR_MIME_PHP;
     296             :         }
     297           0 :         return -1;
     298             : }
     299             : /* }}} */
     300             : 
     301           4 : static void phar_do_403(char *entry, int entry_len) /* {{{ */
     302             : {
     303           4 :         sapi_header_line ctr = {0};
     304             : 
     305           4 :         ctr.response_code = 403;
     306           4 :         ctr.line_len = sizeof("HTTP/1.0 403 Access Denied")-1;
     307           4 :         ctr.line = "HTTP/1.0 403 Access Denied";
     308           4 :         sapi_header_op(SAPI_HEADER_REPLACE, &ctr);
     309           4 :         sapi_send_headers();
     310           4 :         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);
     311           4 :         PHPWRITE(entry, entry_len);
     312           4 :         PHPWRITE(" Access Denied</h1>\n </body>\n</html>", sizeof(" Access Denied</h1>\n </body>\n</html>") - 1);
     313           4 : }
     314             : /* }}} */
     315             : 
     316          10 : static void phar_do_404(phar_archive_data *phar, char *fname, int fname_len, char *f404, size_t f404_len, char *entry, size_t entry_len) /* {{{ */
     317             : {
     318          10 :         sapi_header_line ctr = {0};
     319             :         phar_entry_info *info;
     320             : 
     321          10 :         if (phar && f404_len) {
     322           2 :                 info = phar_get_entry_info(phar, f404, f404_len, NULL, 1);
     323             : 
     324           2 :                 if (info) {
     325           2 :                         phar_file_action(phar, info, "text/html", PHAR_MIME_PHP, f404, f404_len, fname, NULL, NULL, 0);
     326           0 :                         return;
     327             :                 }
     328             :         }
     329             : 
     330           8 :         ctr.response_code = 404;
     331           8 :         ctr.line_len = sizeof("HTTP/1.0 404 Not Found")-1;
     332           8 :         ctr.line = "HTTP/1.0 404 Not Found";
     333           8 :         sapi_header_op(SAPI_HEADER_REPLACE, &ctr);
     334           8 :         sapi_send_headers();
     335           8 :         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);
     336           8 :         PHPWRITE(entry, entry_len);
     337           8 :         PHPWRITE(" Not Found</h1>\n </body>\n</html>",  sizeof(" Not Found</h1>\n </body>\n</html>") - 1);
     338             : }
     339             : /* }}} */
     340             : 
     341             : /* post-process REQUEST_URI and retrieve the actual request URI.  This is for
     342             :    cases like http://localhost/blah.phar/path/to/file.php/extra/stuff
     343             :    which calls "blah.phar" file "path/to/file.php" with PATH_INFO "/extra/stuff" */
     344          81 : static void phar_postprocess_ru_web(char *fname, int fname_len, char **entry, int *entry_len, char **ru, int *ru_len) /* {{{ */
     345             : {
     346          81 :         char *e = *entry + 1, *u = NULL, *u1 = NULL, *saveu = NULL;
     347          81 :         int e_len = *entry_len - 1, u_len = 0;
     348             :         phar_archive_data *pphar;
     349             : 
     350             :         /* we already know we can retrieve the phar if we reach here */
     351         162 :         pphar = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), fname, fname_len);
     352             : 
     353          81 :         if (!pphar && PHAR_G(manifest_cached)) {
     354           0 :                 pphar = zend_hash_str_find_ptr(&cached_phars, fname, fname_len);
     355             :         }
     356             : 
     357             :         do {
     358          87 :                 if (zend_hash_str_exists(&(pphar->manifest), e, e_len)) {
     359          59 :                         if (u) {
     360           2 :                                 u[0] = '/';
     361           2 :                                 *ru = estrndup(u, u_len+1);
     362           2 :                                 ++u_len;
     363           2 :                                 u[0] = '\0';
     364             :                         } else {
     365          57 :                                 *ru = NULL;
     366             :                         }
     367          59 :                         *ru_len = u_len;
     368          59 :                         *entry_len = e_len + 1;
     369          59 :                         return;
     370             :                 }
     371             : 
     372          28 :                 if (u) {
     373           4 :                         u1 = strrchr(e, '/');
     374           4 :                         u[0] = '/';
     375           4 :                         saveu = u;
     376           4 :                         e_len += u_len + 1;
     377           4 :                         u = u1;
     378           4 :                         if (!u) {
     379           2 :                                 return;
     380             :                         }
     381             :                 } else {
     382          24 :                         u = strrchr(e, '/');
     383          24 :                         if (!u) {
     384          20 :                                 if (saveu) {
     385           0 :                                         saveu[0] = '/';
     386             :                                 }
     387          20 :                                 return;
     388             :                         }
     389             :                 }
     390             : 
     391           6 :                 u[0] = '\0';
     392           6 :                 u_len = strlen(u + 1);
     393           6 :                 e_len -= u_len + 1;
     394             : 
     395           6 :                 if (e_len < 0) {
     396           0 :                         if (saveu) {
     397           0 :                                 saveu[0] = '/';
     398             :                         }
     399           0 :                         return;
     400             :                 }
     401           6 :         } while (1);
     402             : }
     403             : /* }}} */
     404             : 
     405             : /* {{{ proto void Phar::running([bool retphar = true])
     406             :  * return the name of the currently running phar archive.  If the optional parameter
     407             :  * is set to true, return the phar:// URL to the currently running phar
     408             :  */
     409          12 : PHP_METHOD(Phar, running)
     410             : {
     411             :         char *fname, *arch, *entry;
     412             :         int fname_len, arch_len, entry_len;
     413          12 :         zend_bool retphar = 1;
     414             : 
     415          12 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &retphar) == FAILURE) {
     416           1 :                 return;
     417             :         }
     418             : 
     419          11 :         fname = (char*)zend_get_executed_filename();
     420          11 :         fname_len = strlen(fname);
     421             : 
     422          11 :         if (fname_len > 7 && !memcmp(fname, "phar://", 7) && SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0)) {
     423          10 :                 efree(entry);
     424          10 :                 if (retphar) {
     425          10 :                         RETVAL_STRINGL(fname, arch_len + 7);
     426           5 :                         efree(arch);
     427           5 :                         return;
     428             :                 } else {
     429             :                         // TODO: avoid reallocation ???
     430          10 :                         RETVAL_STRINGL(arch, arch_len);
     431           5 :                         efree(arch);
     432           5 :                         return;
     433             :                 }
     434             :         }
     435             : 
     436           1 :         RETURN_EMPTY_STRING();
     437             : }
     438             : /* }}} */
     439             : 
     440             : /* {{{ proto void Phar::mount(string pharpath, string externalfile)
     441             :  * mount an external file or path to a location within the phar.  This maps
     442             :  * an external file or directory to a location within the phar archive, allowing
     443             :  * reference to an external location as if it were within the phar archive.  This
     444             :  * is useful for writable temp files like databases
     445             :  */
     446          21 : PHP_METHOD(Phar, mount)
     447             : {
     448          21 :         char *fname, *arch = NULL, *entry = NULL, *path, *actual;
     449             :         int fname_len, arch_len, entry_len;
     450             :         size_t path_len, actual_len;
     451             :         phar_archive_data *pphar;
     452             : 
     453          21 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "pp", &path, &path_len, &actual, &actual_len) == FAILURE) {
     454           0 :                 return;
     455             :         }
     456             : 
     457          21 :         fname = (char*)zend_get_executed_filename();
     458          21 :         fname_len = strlen(fname);
     459             : 
     460             : #ifdef PHP_WIN32
     461             :         phar_unixify_path_separators(fname, fname_len);
     462             : #endif
     463             : 
     464          21 :         if (fname_len > 7 && !memcmp(fname, "phar://", 7) && SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0)) {
     465           6 :                 efree(entry);
     466           6 :                 entry = NULL;
     467             : 
     468           6 :                 if (path_len > 7 && !memcmp(path, "phar://", 7)) {
     469           1 :                         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);
     470           1 :                         efree(arch);
     471           1 :                         return;
     472             :                 }
     473             : carry_on2:
     474          32 :                 if (NULL == (pphar = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), arch, arch_len))) {
     475           0 :                         if (PHAR_G(manifest_cached) && NULL != (pphar = zend_hash_str_find_ptr(&cached_phars, arch, arch_len))) {
     476           0 :                                 if (SUCCESS == phar_copy_on_write(&pphar)) {
     477           0 :                                         goto carry_on;
     478             :                                 }
     479             :                         }
     480             : 
     481           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "%s is not a phar archive, cannot mount", arch);
     482             : 
     483           0 :                         if (arch) {
     484           0 :                                 efree(arch);
     485             :                         }
     486           0 :                         return;
     487             :                 }
     488             : carry_on:
     489          19 :                 if (SUCCESS != phar_mount_entry(pphar, actual, actual_len, path, path_len)) {
     490           8 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "Mounting of %s to %s within phar %s failed", path, actual, arch);
     491           8 :                         if (path && path == entry) {
     492           5 :                                 efree(entry);
     493             :                         }
     494             : 
     495           8 :                         if (arch) {
     496           8 :                                 efree(arch);
     497             :                         }
     498             : 
     499           8 :                         return;
     500             :                 }
     501             : 
     502          11 :                 if (entry && path && path == entry) {
     503           6 :                         efree(entry);
     504             :                 }
     505             : 
     506          11 :                 if (arch) {
     507           8 :                         efree(arch);
     508             :                 }
     509             : 
     510          11 :                 return;
     511          29 :         } else if (PHAR_G(phar_fname_map.u.flags) && NULL != (pphar = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), fname, fname_len))) {
     512             :                 goto carry_on;
     513          12 :         } else if (PHAR_G(manifest_cached) && NULL != (pphar = zend_hash_str_find_ptr(&cached_phars, fname, fname_len))) {
     514           0 :                 if (SUCCESS == phar_copy_on_write(&pphar)) {
     515           0 :                         goto carry_on;
     516             :                 }
     517             : 
     518           0 :                 goto carry_on;
     519          12 :         } else if (SUCCESS == phar_split_fname(path, path_len, &arch, &arch_len, &entry, &entry_len, 2, 0)) {
     520          11 :                 path = entry;
     521          11 :                 path_len = entry_len;
     522          11 :                 goto carry_on2;
     523             :         }
     524             : 
     525           1 :         zend_throw_exception_ex(phar_ce_PharException, 0, "Mounting of %s to %s failed", path, actual);
     526             : }
     527             : /* }}} */
     528             : 
     529             : /* {{{ proto void Phar::webPhar([string alias, [string index, [string f404, [array mimetypes, [callback rewrites]]]]])
     530             :  * mapPhar for web-based phars. Reads the currently executed file (a phar)
     531             :  * and registers its manifest. When executed in the CLI or CGI command-line sapi,
     532             :  * this works exactly like mapPhar().  When executed by a web-based sapi, this
     533             :  * reads $_SERVER['REQUEST_URI'] (the actual original value) and parses out the
     534             :  * intended internal file.
     535             :  */
     536          99 : PHP_METHOD(Phar, webPhar)
     537             : {
     538          99 :         zval *mimeoverride = NULL, *rewrite = NULL;
     539          99 :         char *alias = NULL, *error, *index_php = NULL, *f404 = NULL, *ru = NULL;
     540          99 :         size_t alias_len = 0, f404_len = 0, free_pathinfo = 0;
     541          99 :         int  ru_len = 0;
     542          99 :         char *fname, *path_info, *mime_type = NULL, *entry, *pt;
     543             :         const char *basename;
     544          99 :         size_t fname_len, index_php_len = 0;
     545             :         int entry_len, code, not_cgi;
     546          99 :         phar_archive_data *phar = NULL;
     547          99 :         phar_entry_info *info = NULL;
     548          99 :         size_t sapi_mod_name_len = strlen(sapi_module.name);
     549             : 
     550          99 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s!saz", &alias, &alias_len, &index_php, &index_php_len, &f404, &f404_len, &mimeoverride, &rewrite) == FAILURE) {
     551           0 :                 return;
     552             :         }
     553             : 
     554          99 :         phar_request_initialize();
     555          99 :         fname = (char*)zend_get_executed_filename();
     556          99 :         fname_len = strlen(fname);
     557             : 
     558          99 :         if (phar_open_executed_filename(alias, alias_len, &error) != SUCCESS) {
     559           1 :                 if (error) {
     560           1 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
     561           1 :                         efree(error);
     562             :                 }
     563           1 :                 return;
     564             :         }
     565             : 
     566             :         /* retrieve requested file within phar */
     567          98 :         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")))) {
     568           3 :                 return;
     569             :         }
     570             : 
     571             : #ifdef PHP_WIN32
     572             :         fname = estrndup(fname, fname_len);
     573             :         phar_unixify_path_separators(fname, fname_len);
     574             : #endif
     575          95 :         basename = zend_memrchr(fname, '/', fname_len);
     576             : 
     577          95 :         if (!basename) {
     578           0 :                 basename = fname;
     579             :         } else {
     580          95 :                 ++basename;
     581             :         }
     582             : 
     583         190 :         if ((sapi_mod_name_len == sizeof("cgi-fcgi") - 1 && !strncmp(sapi_module.name, "cgi-fcgi", sizeof("cgi-fcgi") - 1))
     584           0 :                 || (sapi_mod_name_len == sizeof("fpm-fcgi") - 1 && !strncmp(sapi_module.name, "fpm-fcgi", sizeof("fpm-fcgi") - 1))
     585           0 :                 || (sapi_mod_name_len == sizeof("cgi") - 1 && !strncmp(sapi_module.name, "cgi", sizeof("cgi") - 1))) {
     586             : 
     587          95 :                 if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) != IS_UNDEF) {
     588          91 :                         HashTable *_server = Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]);
     589             :                         zval *z_script_name, *z_path_info;
     590             : 
     591         273 :                         if (NULL == (z_script_name = zend_hash_str_find(_server, "SCRIPT_NAME", sizeof("SCRIPT_NAME")-1)) ||
     592             :                                 IS_STRING != Z_TYPE_P(z_script_name) ||
     593          91 :                                 !strstr(Z_STRVAL_P(z_script_name), basename)) {
     594           0 :                                 return;
     595             :                         }
     596             : 
     597         253 :                         if (NULL != (z_path_info = zend_hash_str_find(_server, "PATH_INFO", sizeof("PATH_INFO")-1)) &&
     598             :                                 IS_STRING == Z_TYPE_P(z_path_info)) {
     599          81 :                                 entry_len = Z_STRLEN_P(z_path_info);
     600          81 :                                 entry = estrndup(Z_STRVAL_P(z_path_info), entry_len);
     601          81 :                                 path_info = emalloc(Z_STRLEN_P(z_script_name) + entry_len + 1);
     602          81 :                                 memcpy(path_info, Z_STRVAL_P(z_script_name), Z_STRLEN_P(z_script_name));
     603          81 :                                 memcpy(path_info + Z_STRLEN_P(z_script_name), entry, entry_len + 1);
     604          81 :                                 free_pathinfo = 1;
     605             :                         } else {
     606          10 :                                 entry_len = 0;
     607          10 :                                 entry = estrndup("", 0);
     608          10 :                                 path_info = Z_STRVAL_P(z_script_name);
     609             :                         }
     610             : 
     611          91 :                         pt = estrndup(Z_STRVAL_P(z_script_name), Z_STRLEN_P(z_script_name));
     612             : 
     613             :                 } else {
     614             :                         char *testit;
     615             : 
     616           4 :                         testit = sapi_getenv("SCRIPT_NAME", sizeof("SCRIPT_NAME")-1);
     617           4 :                         if (!(pt = strstr(testit, basename))) {
     618           0 :                                 efree(testit);
     619           0 :                                 return;
     620             :                         }
     621             : 
     622           4 :                         path_info = sapi_getenv("PATH_INFO", sizeof("PATH_INFO")-1);
     623             : 
     624           4 :                         if (path_info) {
     625           4 :                                 entry = path_info;
     626           4 :                                 entry_len = strlen(entry);
     627           4 :                                 spprintf(&path_info, 0, "%s%s", testit, path_info);
     628           4 :                                 free_pathinfo = 1;
     629             :                         } else {
     630           0 :                                 path_info = testit;
     631           0 :                                 free_pathinfo = 1;
     632           0 :                                 entry = estrndup("", 0);
     633           0 :                                 entry_len = 0;
     634             :                         }
     635             : 
     636           4 :                         pt = estrndup(testit, (pt - testit) + (fname_len - (basename - fname)));
     637             :                 }
     638          95 :                 not_cgi = 0;
     639             :         } else {
     640           0 :                 path_info = SG(request_info).request_uri;
     641             : 
     642           0 :                 if (!(pt = strstr(path_info, basename))) {
     643             :                         /* this can happen with rewrite rules - and we have no idea what to do then, so return */
     644           0 :                         return;
     645             :                 }
     646             : 
     647           0 :                 entry_len = strlen(path_info);
     648           0 :                 entry_len -= (pt - path_info) + (fname_len - (basename - fname));
     649           0 :                 entry = estrndup(pt + (fname_len - (basename - fname)), entry_len);
     650             : 
     651           0 :                 pt = estrndup(path_info, (pt - path_info) + (fname_len - (basename - fname)));
     652           0 :                 not_cgi = 1;
     653             :         }
     654             : 
     655          95 :         if (rewrite) {
     656             :                 zend_fcall_info fci;
     657             :                 zend_fcall_info_cache fcc;
     658             :                 zval params, retval;
     659             : 
     660          40 :                 ZVAL_STRINGL(&params, entry, entry_len);
     661             : 
     662          20 :                 if (FAILURE == zend_fcall_info_init(rewrite, 0, &fci, &fcc, NULL, NULL)) {
     663           2 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar error: invalid rewrite callback");
     664             : 
     665           2 :                         if (free_pathinfo) {
     666           0 :                                 efree(path_info);
     667             :                         }
     668           2 :                         efree(pt);
     669             : 
     670           2 :                         return;
     671             :                 }
     672             : 
     673          18 :                 fci.param_count = 1;
     674          18 :                 fci.params = &params;
     675             :                 Z_ADDREF(params);
     676          18 :                 fci.retval = &retval;
     677             : 
     678          18 :                 if (FAILURE == zend_call_function(&fci, &fcc)) {
     679           0 :                         if (!EG(exception)) {
     680           0 :                                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar error: failed to call rewrite callback");
     681             :                         }
     682             : 
     683           0 :                         if (free_pathinfo) {
     684           0 :                                 efree(path_info);
     685             :                         }
     686           0 :                         efree(pt);
     687             : 
     688           0 :                         return;
     689             :                 }
     690             : 
     691          52 :                 if (Z_TYPE_P(fci.retval) == IS_UNDEF || Z_TYPE(retval) == IS_UNDEF) {
     692           2 :                         if (free_pathinfo) {
     693           0 :                                 efree(path_info);
     694             :                         }
     695           2 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar error: rewrite callback must return a string or false");
     696           2 :                         efree(pt);
     697           2 :                         return;
     698             :                 }
     699             : 
     700          16 :                 switch (Z_TYPE(retval)) {
     701             :                         case IS_STRING:
     702          10 :                                 efree(entry);
     703          10 :                                 entry = estrndup(Z_STRVAL_P(fci.retval), Z_STRLEN_P(fci.retval));
     704          10 :                                 entry_len = Z_STRLEN_P(fci.retval);
     705          10 :                                 break;
     706             :                         case IS_TRUE:
     707             :                         case IS_FALSE:
     708           4 :                                 phar_do_403(entry, entry_len);
     709             : 
     710           4 :                                 if (free_pathinfo) {
     711           4 :                                         efree(path_info);
     712             :                                 }
     713           4 :                                 efree(pt);
     714             : 
     715           4 :                                 zend_bailout();
     716           0 :                                 return;
     717             :                         default:
     718           2 :                                 if (free_pathinfo) {
     719           0 :                                         efree(path_info);
     720             :                                 }
     721           2 :                                 efree(pt);
     722             : 
     723           2 :                                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar error: rewrite callback must return a string or false");
     724           2 :                                 return;
     725             :                 }
     726             :         }
     727             : 
     728          85 :         if (entry_len) {
     729          81 :                 phar_postprocess_ru_web(fname, fname_len, &entry, &entry_len, &ru, &ru_len);
     730             :         }
     731             : 
     732          85 :         if (!entry_len || (entry_len == 1 && entry[0] == '/')) {
     733          16 :                 efree(entry);
     734             :                 /* direct request */
     735          16 :                 if (index_php_len) {
     736           8 :                         entry = index_php;
     737           8 :                         entry_len = index_php_len;
     738           8 :                         if (entry[0] != '/') {
     739           4 :                                 spprintf(&entry, 0, "/%s", index_php);
     740           4 :                                 ++entry_len;
     741             :                         }
     742             :                 } else {
     743             :                         /* assume "index.php" is starting point */
     744           8 :                         entry = estrndup("/index.php", sizeof("/index.php"));
     745           8 :                         entry_len = sizeof("/index.php")-1;
     746             :                 }
     747             : 
     748          32 :                 if (FAILURE == phar_get_archive(&phar, fname, fname_len, NULL, 0, NULL) ||
     749          16 :                         (info = phar_get_entry_info(phar, entry, entry_len, NULL, 0)) == NULL) {
     750           4 :                         phar_do_404(phar, fname, fname_len, f404, f404_len, entry, entry_len);
     751             : 
     752           4 :                         if (free_pathinfo) {
     753           4 :                                 efree(path_info);
     754             :                         }
     755             : 
     756           4 :                         zend_bailout();
     757             :                 } else {
     758          12 :                         char *tmp = NULL, sa = '\0';
     759          12 :                         sapi_header_line ctr = {0};
     760          12 :                         ctr.response_code = 301;
     761          12 :                         ctr.line_len = sizeof("HTTP/1.1 301 Moved Permanently")-1;
     762          12 :                         ctr.line = "HTTP/1.1 301 Moved Permanently";
     763          12 :                         sapi_header_op(SAPI_HEADER_REPLACE, &ctr);
     764             : 
     765          12 :                         if (not_cgi) {
     766           0 :                                 tmp = strstr(path_info, basename) + fname_len;
     767           0 :                                 sa = *tmp;
     768           0 :                                 *tmp = '\0';
     769             :                         }
     770             : 
     771          12 :                         ctr.response_code = 0;
     772             : 
     773          12 :                         if (path_info[strlen(path_info)-1] == '/') {
     774           8 :                                 ctr.line_len = spprintf(&(ctr.line), 4096, "Location: %s%s", path_info, entry + 1);
     775             :                         } else {
     776           4 :                                 ctr.line_len = spprintf(&(ctr.line), 4096, "Location: %s%s", path_info, entry);
     777             :                         }
     778             : 
     779          12 :                         if (not_cgi) {
     780           0 :                                 *tmp = sa;
     781             :                         }
     782             : 
     783          12 :                         if (free_pathinfo) {
     784           8 :                                 efree(path_info);
     785             :                         }
     786             : 
     787          12 :                         sapi_header_op(SAPI_HEADER_REPLACE, &ctr);
     788          12 :                         sapi_send_headers();
     789          12 :                         efree(ctr.line);
     790          12 :                         zend_bailout();
     791             :                 }
     792             :         }
     793             : 
     794         138 :         if (FAILURE == phar_get_archive(&phar, fname, fname_len, NULL, 0, NULL) ||
     795          69 :                 (info = phar_get_entry_info(phar, entry, entry_len, NULL, 0)) == NULL) {
     796           6 :                 phar_do_404(phar, fname, fname_len, f404, f404_len, entry, entry_len);
     797             : #ifdef PHP_WIN32
     798             :                 efree(fname);
     799             : #endif
     800           4 :                 zend_bailout();
     801             :         }
     802             : 
     803          63 :         if (mimeoverride && zend_hash_num_elements(Z_ARRVAL_P(mimeoverride))) {
     804          68 :                 const char *ext = zend_memrchr(entry, '.', entry_len);
     805             :                 zval *val;
     806             : 
     807          34 :                 if (ext) {
     808          32 :                         ++ext;
     809             : 
     810          32 :                         if (NULL != (val = zend_hash_str_find(Z_ARRVAL_P(mimeoverride), ext, strlen(ext)))) {
     811          24 :                                 switch (Z_TYPE_P(val)) {
     812             :                                         case IS_LONG:
     813          28 :                                                 if (Z_LVAL_P(val) == PHAR_MIME_PHP || Z_LVAL_P(val) == PHAR_MIME_PHPS) {
     814          12 :                                                         mime_type = "";
     815          12 :                                                         code = Z_LVAL_P(val);
     816             :                                                 } else {
     817           4 :                                                         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");
     818           4 :                                                         if (free_pathinfo) {
     819           4 :                                                                 efree(path_info);
     820             :                                                         }
     821           4 :                                                         efree(pt);
     822           4 :                                                         efree(entry);
     823             : #ifdef PHP_WIN32
     824             :                                                         efree(fname);
     825             : #endif
     826           4 :                                                         RETURN_FALSE;
     827             :                                                 }
     828          12 :                                                 break;
     829             :                                         case IS_STRING:
     830           4 :                                                 mime_type = Z_STRVAL_P(val);
     831           4 :                                                 code = PHAR_MIME_OTHER;
     832           4 :                                                 break;
     833             :                                         default:
     834           4 :                                                 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");
     835           4 :                                                 if (free_pathinfo) {
     836           4 :                                                         efree(path_info);
     837             :                                                 }
     838           4 :                                                 efree(pt);
     839           4 :                                                 efree(entry);
     840             : #ifdef PHP_WIN32
     841             :                                                 efree(fname);
     842             : #endif
     843           4 :                                                 RETURN_FALSE;
     844             :                                 }
     845             :                         }
     846             :                 }
     847             :         }
     848             : 
     849          55 :         if (!mime_type) {
     850          39 :                 code = phar_file_type(&PHAR_G(mime_types), entry, &mime_type);
     851             :         }
     852          55 :         phar_file_action(phar, info, mime_type, code, entry, entry_len, fname, pt, ru, ru_len);
     853             : }
     854             : /* }}} */
     855             : 
     856             : /* {{{ proto void Phar::mungServer(array munglist)
     857             :  * Defines a list of up to 4 $_SERVER variables that should be modified for execution
     858             :  * to mask the presence of the phar archive.  This should be used in conjunction with
     859             :  * Phar::webPhar(), and has no effect otherwise
     860             :  * SCRIPT_NAME, PHP_SELF, REQUEST_URI and SCRIPT_FILENAME
     861             :  */
     862          19 : PHP_METHOD(Phar, mungServer)
     863             : {
     864             :         zval *mungvalues, *data;
     865             : 
     866          19 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &mungvalues) == FAILURE) {
     867           1 :                 return;
     868             :         }
     869             : 
     870          18 :         if (!zend_hash_num_elements(Z_ARRVAL_P(mungvalues))) {
     871           4 :                 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");
     872           4 :                 return;
     873             :         }
     874             : 
     875          14 :         if (zend_hash_num_elements(Z_ARRVAL_P(mungvalues)) > 4) {
     876           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");
     877           4 :                 return;
     878             :         }
     879             : 
     880          10 :         phar_request_initialize();
     881             : 
     882          54 :         ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(mungvalues), data) {
     883             : 
     884          24 :                 if (Z_TYPE_P(data) != IS_STRING) {
     885           4 :                         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");
     886           4 :                         return;
     887             :                 }
     888             : 
     889          20 :                 if (Z_STRLEN_P(data) == sizeof("PHP_SELF")-1 && !strncmp(Z_STRVAL_P(data), "PHP_SELF", sizeof("PHP_SELF")-1)) {
     890           6 :                         PHAR_G(phar_SERVER_mung_list) |= PHAR_MUNG_PHP_SELF;
     891             :                 }
     892             : 
     893          20 :                 if (Z_STRLEN_P(data) == sizeof("REQUEST_URI")-1) {
     894          10 :                         if (!strncmp(Z_STRVAL_P(data), "REQUEST_URI", sizeof("REQUEST_URI")-1)) {
     895           6 :                                 PHAR_G(phar_SERVER_mung_list) |= PHAR_MUNG_REQUEST_URI;
     896             :                         }
     897          10 :                         if (!strncmp(Z_STRVAL_P(data), "SCRIPT_NAME", sizeof("SCRIPT_NAME")-1)) {
     898           4 :                                 PHAR_G(phar_SERVER_mung_list) |= PHAR_MUNG_SCRIPT_NAME;
     899             :                         }
     900             :                 }
     901             : 
     902          20 :                 if (Z_STRLEN_P(data) == sizeof("SCRIPT_FILENAME")-1 && !strncmp(Z_STRVAL_P(data), "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME")-1)) {
     903           4 :                         PHAR_G(phar_SERVER_mung_list) |= PHAR_MUNG_SCRIPT_FILENAME;
     904             :                 }
     905             :         } ZEND_HASH_FOREACH_END();
     906             : }
     907             : /* }}} */
     908             : 
     909             : /* {{{ proto void Phar::interceptFileFuncs()
     910             :  * instructs phar to intercept fopen, file_get_contents, opendir, and all of the stat-related functions
     911             :  * and return stat on files within the phar for relative paths
     912             :  *
     913             :  * Once called, this cannot be reversed, and continue until the end of the request.
     914             :  *
     915             :  * This allows legacy scripts to be pharred unmodified
     916             :  */
     917          15 : PHP_METHOD(Phar, interceptFileFuncs)
     918             : {
     919          15 :         if (zend_parse_parameters_none() == FAILURE) {
     920           0 :                 return;
     921             :         }
     922          15 :         phar_intercept_functions();
     923             : }
     924             : /* }}} */
     925             : 
     926             : /* {{{ proto array Phar::createDefaultStub([string indexfile[, string webindexfile]])
     927             :  * Return a stub that can be used to run a phar-based archive without the phar extension
     928             :  * indexfile is the CLI startup filename, which defaults to "index.php", webindexfile
     929             :  * is the web startup filename, and also defaults to "index.php"
     930             :  */
     931           9 : PHP_METHOD(Phar, createDefaultStub)
     932             : {
     933           9 :         char *index = NULL, *webindex = NULL, *error;
     934             :         zend_string *stub;
     935           9 :         size_t index_len = 0, webindex_len = 0;
     936             : 
     937           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|pp", &index, &index_len, &webindex, &webindex_len) == FAILURE) {
     938           1 :                 return;
     939             :         }
     940             : 
     941           8 :         stub = phar_create_default_stub(index, webindex, &error);
     942             : 
     943           8 :         if (error) {
     944           2 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
     945           2 :                 efree(error);
     946           2 :                 return;
     947             :         }
     948           6 :         RETURN_NEW_STR(stub);
     949             : }
     950             : /* }}} */
     951             : 
     952             : /* {{{ proto mixed Phar::mapPhar([string alias, [int dataoffset]])
     953             :  * Reads the currently executed file (a phar) and registers its manifest */
     954          37 : PHP_METHOD(Phar, mapPhar)
     955             : {
     956          37 :         char *alias = NULL, *error;
     957          37 :         size_t alias_len = 0;
     958          37 :         zend_long dataoffset = 0;
     959             : 
     960          37 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!l", &alias, &alias_len, &dataoffset) == FAILURE) {
     961           1 :                 return;
     962             :         }
     963             : 
     964          36 :         phar_request_initialize();
     965             : 
     966          36 :         RETVAL_BOOL(phar_open_executed_filename(alias, alias_len, &error) == SUCCESS);
     967             : 
     968          36 :         if (error) {
     969           9 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
     970           9 :                 efree(error);
     971             :         }
     972             : } /* }}} */
     973             : 
     974             : /* {{{ proto mixed Phar::loadPhar(string filename [, string alias])
     975             :  * Loads any phar archive with an alias */
     976           8 : PHP_METHOD(Phar, loadPhar)
     977             : {
     978           8 :         char *fname, *alias = NULL, *error;
     979           8 :         size_t fname_len, alias_len = 0;
     980             : 
     981           8 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|s!", &fname, &fname_len, &alias, &alias_len) == FAILURE) {
     982           1 :                 return;
     983             :         }
     984             : 
     985           7 :         phar_request_initialize();
     986             : 
     987           7 :         RETVAL_BOOL(phar_open_from_filename(fname, fname_len, alias, alias_len, REPORT_ERRORS, NULL, &error) == SUCCESS);
     988             : 
     989           7 :         if (error) {
     990           2 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
     991           2 :                 efree(error);
     992             :         }
     993             : } /* }}} */
     994             : 
     995             : /* {{{ proto string Phar::apiVersion()
     996             :  * Returns the api version */
     997           1 : PHP_METHOD(Phar, apiVersion)
     998             : {
     999           1 :         if (zend_parse_parameters_none() == FAILURE) {
    1000           0 :                 return;
    1001             :         }
    1002           2 :         RETURN_STRINGL(PHP_PHAR_API_VERSION, sizeof(PHP_PHAR_API_VERSION)-1);
    1003             : }
    1004             : /* }}}*/
    1005             : 
    1006             : /* {{{ proto bool Phar::canCompress([int method])
    1007             :  * Returns whether phar extension supports compression using zlib/bzip2 */
    1008           4 : PHP_METHOD(Phar, canCompress)
    1009             : {
    1010           4 :         zend_long method = 0;
    1011             : 
    1012           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &method) == FAILURE) {
    1013           1 :                 return;
    1014             :         }
    1015             : 
    1016           3 :         phar_request_initialize();
    1017           3 :         switch (method) {
    1018             :         case PHAR_ENT_COMPRESSED_GZ:
    1019           1 :                 if (PHAR_G(has_zlib)) {
    1020           1 :                         RETURN_TRUE;
    1021             :                 } else {
    1022           0 :                         RETURN_FALSE;
    1023             :                 }
    1024             :         case PHAR_ENT_COMPRESSED_BZ2:
    1025           1 :                 if (PHAR_G(has_bz2)) {
    1026           1 :                         RETURN_TRUE;
    1027             :                 } else {
    1028           0 :                         RETURN_FALSE;
    1029             :                 }
    1030             :         default:
    1031           1 :                 if (PHAR_G(has_zlib) || PHAR_G(has_bz2)) {
    1032           1 :                         RETURN_TRUE;
    1033             :                 } else {
    1034           0 :                         RETURN_FALSE;
    1035             :                 }
    1036             :         }
    1037             : }
    1038             : /* }}} */
    1039             : 
    1040             : /* {{{ proto bool Phar::canWrite()
    1041             :  * Returns whether phar extension supports writing and creating phars */
    1042           5 : PHP_METHOD(Phar, canWrite)
    1043             : {
    1044           5 :         if (zend_parse_parameters_none() == FAILURE) {
    1045           0 :                 return;
    1046             :         }
    1047           5 :         RETURN_BOOL(!PHAR_G(readonly));
    1048             : }
    1049             : /* }}} */
    1050             : 
    1051             : /* {{{ proto bool Phar::isValidPharFilename(string filename[, bool executable = true])
    1052             :  * Returns whether the given filename is a valid phar filename */
    1053          37 : PHP_METHOD(Phar, isValidPharFilename)
    1054             : {
    1055             :         char *fname;
    1056             :         const char *ext_str;
    1057             :         size_t fname_len;
    1058             :         int ext_len, is_executable;
    1059          37 :         zend_bool executable = 1;
    1060             : 
    1061          37 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|b", &fname, &fname_len, &executable) == FAILURE) {
    1062           1 :                 return;
    1063             :         }
    1064             : 
    1065          36 :         is_executable = executable;
    1066          36 :         RETVAL_BOOL(phar_detect_phar_fname_ext(fname, fname_len, &ext_str, &ext_len, is_executable, 2, 1) == SUCCESS);
    1067             : }
    1068             : /* }}} */
    1069             : 
    1070             : /**
    1071             :  * from spl_directory
    1072             :  */
    1073         451 : static void phar_spl_foreign_dtor(spl_filesystem_object *object) /* {{{ */
    1074             : {
    1075         451 :         phar_archive_data *phar = (phar_archive_data *) object->oth;
    1076             : 
    1077         451 :         if (!phar->is_persistent) {
    1078         451 :                 phar_archive_delref(phar);
    1079             :         }
    1080             : 
    1081         451 :         object->oth = NULL;
    1082         451 : }
    1083             : /* }}} */
    1084             : 
    1085             : /**
    1086             :  * from spl_directory
    1087             :  */
    1088           0 : static void phar_spl_foreign_clone(spl_filesystem_object *src, spl_filesystem_object *dst) /* {{{ */
    1089             : {
    1090           0 :         phar_archive_data *phar_data = (phar_archive_data *) dst->oth;
    1091             : 
    1092           0 :         if (!phar_data->is_persistent) {
    1093           0 :                 ++(phar_data->refcount);
    1094             :         }
    1095           0 : }
    1096             : /* }}} */
    1097             : 
    1098             : static spl_other_handler phar_spl_foreign_handler = {
    1099             :         phar_spl_foreign_dtor,
    1100             :         phar_spl_foreign_clone
    1101             : };
    1102             : 
    1103             : /* {{{ proto void Phar::__construct(string fname [, int flags [, string alias]])
    1104             :  * Construct a Phar archive object
    1105             :  *
    1106             :  * proto void PharData::__construct(string fname [[, int flags [, string alias]], int file format = Phar::TAR])
    1107             :  * Construct a PharData archive object
    1108             :  *
    1109             :  * This function is used as the constructor for both the Phar and PharData
    1110             :  * classes, hence the two prototypes above.
    1111             :  */
    1112         522 : PHP_METHOD(Phar, __construct)
    1113             : {
    1114         522 :         char *fname, *alias = NULL, *error, *arch = NULL, *entry = NULL, *save_fname;
    1115         522 :         size_t fname_len, alias_len = 0;
    1116             :         int arch_len, entry_len, is_data;
    1117         522 :         zend_long flags = SPL_FILE_DIR_SKIPDOTS|SPL_FILE_DIR_UNIXPATHS;
    1118         522 :         zend_long format = 0;
    1119             :         phar_archive_object *phar_obj;
    1120             :         phar_archive_data   *phar_data;
    1121        1044 :         zval *zobj = getThis(), arg1, arg2;
    1122             : 
    1123         522 :         phar_obj = (phar_archive_object*)((char*)Z_OBJ_P(zobj) - Z_OBJ_P(zobj)->handlers->offset);
    1124             : 
    1125         522 :         is_data = instanceof_function(Z_OBJCE_P(zobj), phar_ce_data);
    1126             : 
    1127         522 :         if (is_data) {
    1128         109 :                 if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "p|ls!l", &fname, &fname_len, &flags, &alias, &alias_len, &format) == FAILURE) {
    1129           0 :                         return;
    1130             :                 }
    1131             :         } else {
    1132         413 :                 if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "p|ls!", &fname, &fname_len, &flags, &alias, &alias_len) == FAILURE) {
    1133           2 :                         return;
    1134             :                 }
    1135             :         }
    1136             : 
    1137         520 :         if (phar_obj->archive) {
    1138           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot call constructor twice");
    1139           1 :                 return;
    1140             :         }
    1141             : 
    1142         519 :         save_fname = fname;
    1143         519 :         if (SUCCESS == phar_split_fname(fname, (int)fname_len, &arch, &arch_len, &entry, &entry_len, !is_data, 2)) {
    1144             :                 /* use arch (the basename for the archive) for fname instead of fname */
    1145             :                 /* this allows support for RecursiveDirectoryIterator of subdirectories */
    1146             : #ifdef PHP_WIN32
    1147             :                 phar_unixify_path_separators(arch, arch_len);
    1148             : #endif
    1149         512 :                 fname = arch;
    1150         512 :                 fname_len = arch_len;
    1151             : #ifdef PHP_WIN32
    1152             :         } else {
    1153             :                 arch = estrndup(fname, fname_len);
    1154             :                 arch_len = fname_len;
    1155             :                 fname = arch;
    1156             :                 phar_unixify_path_separators(arch, arch_len);
    1157             : #endif
    1158             :         }
    1159             : 
    1160         519 :         if (phar_open_or_create_filename(fname, fname_len, alias, alias_len, is_data, REPORT_ERRORS, &phar_data, &error) == FAILURE) {
    1161             : 
    1162          68 :                 if (fname == arch && fname != save_fname) {
    1163          61 :                         efree(arch);
    1164          61 :                         fname = save_fname;
    1165             :                 }
    1166             : 
    1167          68 :                 if (entry) {
    1168          61 :                         efree(entry);
    1169             :                 }
    1170             : 
    1171          68 :                 if (error) {
    1172          68 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    1173             :                                 "%s", error);
    1174          68 :                         efree(error);
    1175             :                 } else {
    1176           0 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    1177             :                                 "Phar creation or opening failed");
    1178             :                 }
    1179             : 
    1180          68 :                 return;
    1181             :         }
    1182             : 
    1183         451 :         if (is_data && phar_data->is_tar && phar_data->is_brandnew && format == PHAR_FORMAT_ZIP) {
    1184           0 :                 phar_data->is_zip = 1;
    1185           0 :                 phar_data->is_tar = 0;
    1186             :         }
    1187             : 
    1188         451 :         if (fname == arch) {
    1189         451 :                 efree(arch);
    1190         451 :                 fname = save_fname;
    1191             :         }
    1192             : 
    1193         451 :         if ((is_data && !phar_data->is_data) || (!is_data && phar_data->is_data)) {
    1194           0 :                 if (is_data) {
    1195           0 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    1196             :                                 "PharData class can only be used for non-executable tar and zip archives");
    1197             :                 } else {
    1198           0 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    1199             :                                 "Phar class can only be used for executable tar and zip archives");
    1200             :                 }
    1201           0 :                 efree(entry);
    1202           0 :                 return;
    1203             :         }
    1204             : 
    1205         451 :         is_data = phar_data->is_data;
    1206             : 
    1207         451 :         if (!phar_data->is_persistent) {
    1208         449 :                 ++(phar_data->refcount);
    1209             :         }
    1210             : 
    1211         451 :         phar_obj->archive = phar_data;
    1212         451 :         phar_obj->spl.oth_handler = &phar_spl_foreign_handler;
    1213             : 
    1214         451 :         if (entry) {
    1215         451 :                 fname_len = spprintf(&fname, 0, "phar://%s%s", phar_data->fname, entry);
    1216         451 :                 efree(entry);
    1217             :         } else {
    1218           0 :                 fname_len = spprintf(&fname, 0, "phar://%s", phar_data->fname);
    1219             :         }
    1220             : 
    1221         902 :         ZVAL_STRINGL(&arg1, fname, fname_len);
    1222         451 :         ZVAL_LONG(&arg2, flags);
    1223             : 
    1224         451 :         zend_call_method_with_2_params(zobj, Z_OBJCE_P(zobj),
    1225             :                 &spl_ce_RecursiveDirectoryIterator->constructor, "__construct", NULL, &arg1, &arg2);
    1226             : 
    1227         451 :         zval_ptr_dtor(&arg1);
    1228             : 
    1229         451 :         if (!phar_data->is_persistent) {
    1230         449 :                 phar_obj->archive->is_data = is_data;
    1231           2 :         } else if (!EG(exception)) {
    1232             :                 /* register this guy so we can modify if necessary */
    1233           2 :                 zend_hash_str_add_ptr(&PHAR_G(phar_persist_map), (const char *) phar_obj->archive, sizeof(phar_obj->archive), phar_obj);
    1234             :         }
    1235             : 
    1236         451 :         phar_obj->spl.info_class = phar_ce_entry;
    1237         451 :         efree(fname);
    1238             : }
    1239             : /* }}} */
    1240             : 
    1241             : /* {{{ proto array Phar::getSupportedSignatures()
    1242             :  * Return array of supported signature types
    1243             :  */
    1244          10 : PHP_METHOD(Phar, getSupportedSignatures)
    1245             : {
    1246          10 :         if (zend_parse_parameters_none() == FAILURE) {
    1247           0 :                 return;
    1248             :         }
    1249             : 
    1250          10 :         array_init(return_value);
    1251             : 
    1252          10 :         add_next_index_stringl(return_value, "MD5", 3);
    1253          10 :         add_next_index_stringl(return_value, "SHA-1", 5);
    1254             : #ifdef PHAR_HASH_OK
    1255          10 :         add_next_index_stringl(return_value, "SHA-256", 7);
    1256          10 :         add_next_index_stringl(return_value, "SHA-512", 7);
    1257             : #endif
    1258             : #if PHAR_HAVE_OPENSSL
    1259          10 :         add_next_index_stringl(return_value, "OpenSSL", 7);
    1260             : #else
    1261             :         if (zend_hash_str_exists(&module_registry, "openssl", sizeof("openssl")-1)) {
    1262             :                 add_next_index_stringl(return_value, "OpenSSL", 7);
    1263             :         }
    1264             : #endif
    1265             : }
    1266             : /* }}} */
    1267             : 
    1268             : /* {{{ proto array Phar::getSupportedCompression()
    1269             :  * Return array of supported comparession algorithms
    1270             :  */
    1271           1 : PHP_METHOD(Phar, getSupportedCompression)
    1272             : {
    1273           1 :         if (zend_parse_parameters_none() == FAILURE) {
    1274           0 :                 return;
    1275             :         }
    1276             : 
    1277           1 :         array_init(return_value);
    1278           1 :         phar_request_initialize();
    1279             : 
    1280           1 :         if (PHAR_G(has_zlib)) {
    1281           1 :                 add_next_index_stringl(return_value, "GZ", 2);
    1282             :         }
    1283             : 
    1284           1 :         if (PHAR_G(has_bz2)) {
    1285           1 :                 add_next_index_stringl(return_value, "BZIP2", 5);
    1286             :         }
    1287             : }
    1288             : /* }}} */
    1289             : 
    1290             : /* {{{ proto array Phar::unlinkArchive(string archive)
    1291             :  * Completely remove a phar archive from memory and disk
    1292             :  */
    1293          13 : PHP_METHOD(Phar, unlinkArchive)
    1294             : {
    1295             :         char *fname, *error, *zname, *arch, *entry;
    1296             :         size_t fname_len;
    1297             :         int zname_len, arch_len, entry_len;
    1298             :         phar_archive_data *phar;
    1299             : 
    1300          13 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) {
    1301           1 :                 RETURN_FALSE;
    1302             :         }
    1303             : 
    1304          12 :         if (!fname_len) {
    1305           1 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "Unknown phar archive \"\"");
    1306           1 :                 return;
    1307             :         }
    1308             : 
    1309          11 :         if (FAILURE == phar_open_from_filename(fname, fname_len, NULL, 0, REPORT_ERRORS, &phar, &error)) {
    1310           2 :                 if (error) {
    1311           2 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "Unknown phar archive \"%s\": %s", fname, error);
    1312           2 :                         efree(error);
    1313             :                 } else {
    1314           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "Unknown phar archive \"%s\"", fname);
    1315             :                 }
    1316           2 :                 return;
    1317             :         }
    1318             : 
    1319           9 :         zname = (char*)zend_get_executed_filename();
    1320           9 :         zname_len = strlen(zname);
    1321             : 
    1322           9 :         if (zname_len > 7 && !memcmp(zname, "phar://", 7) && SUCCESS == phar_split_fname(zname, zname_len, &arch, &arch_len, &entry, &entry_len, 2, 0)) {
    1323           1 :                 if ((size_t)arch_len == fname_len && !memcmp(arch, fname, arch_len)) {
    1324           1 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar archive \"%s\" cannot be unlinked from within itself", fname);
    1325           1 :                         efree(arch);
    1326           1 :                         efree(entry);
    1327           1 :                         return;
    1328             :                 }
    1329           0 :                 efree(arch);
    1330           0 :                 efree(entry);
    1331             :         }
    1332             : 
    1333           8 :         if (phar->is_persistent) {
    1334           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar archive \"%s\" is in phar.cache_list, cannot unlinkArchive()", fname);
    1335           0 :                 return;
    1336             :         }
    1337             : 
    1338           8 :         if (phar->refcount) {
    1339           1 :                 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);
    1340           1 :                 return;
    1341             :         }
    1342             : 
    1343           7 :         fname = estrndup(phar->fname, phar->fname_len);
    1344             : 
    1345             :         /* invalidate phar cache */
    1346           7 :         PHAR_G(last_phar) = NULL;
    1347           7 :         PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
    1348             : 
    1349           7 :         phar_archive_delref(phar);
    1350           7 :         unlink(fname);
    1351           7 :         efree(fname);
    1352           7 :         RETURN_TRUE;
    1353             : }
    1354             : /* }}} */
    1355             : 
    1356             : #define PHAR_ARCHIVE_OBJECT() \
    1357             :         zval *zobj = getThis(); \
    1358             :         phar_archive_object *phar_obj = (phar_archive_object*)((char*)Z_OBJ_P(zobj) - Z_OBJ_P(zobj)->handlers->offset); \
    1359             :         if (!phar_obj->archive) { \
    1360             :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    1361             :                         "Cannot call method on an uninitialized Phar object"); \
    1362             :                 return; \
    1363             :         }
    1364             : 
    1365             : /* {{{ proto void Phar::__destruct()
    1366             :  * if persistent, remove from the cache
    1367             :  */
    1368         450 : PHP_METHOD(Phar, __destruct)
    1369             : {
    1370         900 :         zval *zobj = getThis();
    1371         450 :         phar_archive_object *phar_obj = (phar_archive_object*)((char*)Z_OBJ_P(zobj) - Z_OBJ_P(zobj)->handlers->offset);
    1372             : 
    1373         450 :         if (phar_obj->archive && phar_obj->archive->is_persistent) {
    1374           0 :                 zend_hash_str_del(&PHAR_G(phar_persist_map), (const char *) phar_obj->archive, sizeof(phar_obj->archive));
    1375             :         }
    1376         450 : }
    1377             : /* }}} */
    1378             : 
    1379             : struct _phar_t {
    1380             :         phar_archive_object *p;
    1381             :         zend_class_entry *c;
    1382             :         char *b;
    1383             :         zval *ret;
    1384             :         php_stream *fp;
    1385             :         uint l;
    1386             :         int count;
    1387             : };
    1388             : 
    1389        3151 : static int phar_build(zend_object_iterator *iter, void *puser) /* {{{ */
    1390             : {
    1391             :         zval *value;
    1392        3151 :         zend_bool close_fp = 1;
    1393        3151 :         struct _phar_t *p_obj = (struct _phar_t*) puser;
    1394        3151 :         uint str_key_len, base_len = p_obj->l, fname_len;
    1395             :         phar_entry_data *data;
    1396             :         php_stream *fp;
    1397             :         size_t contents_len;
    1398        3151 :         char *fname, *error = NULL, *base = p_obj->b, *save = NULL, *temp = NULL;
    1399             :         zend_string *opened;
    1400             :         char *str_key;
    1401        3151 :         zend_class_entry *ce = p_obj->c;
    1402        3151 :         phar_archive_object *phar_obj = p_obj->p;
    1403             : 
    1404        3151 :         value = iter->funcs->get_current_data(iter);
    1405             : 
    1406        3151 :         if (EG(exception)) {
    1407           0 :                 return ZEND_HASH_APPLY_STOP;
    1408             :         }
    1409             : 
    1410        3151 :         if (!value) {
    1411             :                 /* failure in get_current_data */
    1412           0 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned no value", ZSTR_VAL(ce->name));
    1413           0 :                 return ZEND_HASH_APPLY_STOP;
    1414             :         }
    1415             : 
    1416        3151 :         switch (Z_TYPE_P(value)) {
    1417             :                 case IS_STRING:
    1418        3011 :                         break;
    1419             :                 case IS_RESOURCE:
    1420           3 :                         php_stream_from_zval_no_verify(fp, value);
    1421             : 
    1422           3 :                         if (!fp) {
    1423           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Iterator %s returned an invalid stream handle", ZSTR_VAL(ce->name));
    1424           0 :                                 return ZEND_HASH_APPLY_STOP;
    1425             :                         }
    1426             : 
    1427           3 :                         if (iter->funcs->get_current_key) {
    1428             :                                 zval key;
    1429           3 :                                 iter->funcs->get_current_key(iter, &key);
    1430             : 
    1431           3 :                                 if (EG(exception)) {
    1432           0 :                                         return ZEND_HASH_APPLY_STOP;
    1433             :                                 }
    1434             : 
    1435           3 :                                 if (Z_TYPE(key) != IS_STRING) {
    1436             :                                         zval_dtor(&key);
    1437           0 :                                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned an invalid key (must return a string)", ZSTR_VAL(ce->name));
    1438           0 :                                         return ZEND_HASH_APPLY_STOP;
    1439             :                                 }
    1440             : 
    1441           3 :                                 str_key_len = Z_STRLEN(key);
    1442           3 :                                 str_key = estrndup(Z_STRVAL(key), str_key_len);
    1443             : 
    1444           3 :                                 save = str_key;
    1445             :                                 zval_dtor(&key);
    1446             :                         } else {
    1447           0 :                                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned an invalid key (must return a string)", ZSTR_VAL(ce->name));
    1448           0 :                                 return ZEND_HASH_APPLY_STOP;
    1449             :                         }
    1450             : 
    1451           3 :                         close_fp = 0;
    1452           3 :                         opened = zend_string_init("[stream]", sizeof("[stream]") - 1, 0);
    1453           3 :                         goto after_open_fp;
    1454             :                 case IS_OBJECT:
    1455         137 :                         if (instanceof_function(Z_OBJCE_P(value), spl_ce_SplFileInfo)) {
    1456         134 :                                 char *test = NULL;
    1457             :                                 zval dummy;
    1458         134 :                                 spl_filesystem_object *intern = (spl_filesystem_object*)((char*)Z_OBJ_P(value) - Z_OBJ_P(value)->handlers->offset);
    1459             : 
    1460         134 :                                 if (!base_len) {
    1461           0 :                                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Iterator %s returns an SplFileInfo object, so base directory must be specified", ZSTR_VAL(ce->name));
    1462           0 :                                         return ZEND_HASH_APPLY_STOP;
    1463             :                                 }
    1464             : 
    1465         134 :                                 switch (intern->type) {
    1466             :                                         case SPL_FS_DIR:
    1467          81 :                                                 test = spl_filesystem_object_get_path(intern, NULL);
    1468          81 :                                                 fname_len = spprintf(&fname, 0, "%s%c%s", test, DEFAULT_SLASH, intern->u.dir.entry.d_name);
    1469          81 :                                                 php_stat(fname, fname_len, FS_IS_DIR, &dummy);
    1470             : 
    1471          81 :                                                 if (Z_TYPE(dummy) == IS_TRUE) {
    1472             :                                                         /* ignore directories */
    1473           6 :                                                         efree(fname);
    1474           6 :                                                         return ZEND_HASH_APPLY_KEEP;
    1475             :                                                 }
    1476             : 
    1477          75 :                                                 test = expand_filepath(fname, NULL);
    1478          75 :                                                 efree(fname);
    1479             : 
    1480          75 :                                                 if (test) {
    1481          75 :                                                         fname = test;
    1482          75 :                                                         fname_len = strlen(fname);
    1483             :                                                 } else {
    1484           0 :                                                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Could not resolve file path");
    1485           0 :                                                         return ZEND_HASH_APPLY_STOP;
    1486             :                                                 }
    1487             : 
    1488          75 :                                                 save = fname;
    1489          75 :                                                 goto phar_spl_fileinfo;
    1490             :                                         case SPL_FS_INFO:
    1491             :                                         case SPL_FS_FILE:
    1492          53 :                                                 fname = expand_filepath(intern->file_name, NULL);
    1493          53 :                                                 if (!fname) {
    1494           0 :                                                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Could not resolve file path");
    1495           0 :                                                         return ZEND_HASH_APPLY_STOP;
    1496             :                                                 }
    1497             : 
    1498          53 :                                                 fname_len = strlen(fname);
    1499          53 :                                                 save = fname;
    1500          53 :                                                 goto phar_spl_fileinfo;
    1501             :                                 }
    1502             :                         }
    1503             :                         /* fall-through */
    1504             :                 default:
    1505           3 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned an invalid value (must return a string)", ZSTR_VAL(ce->name));
    1506           3 :                         return ZEND_HASH_APPLY_STOP;
    1507             :         }
    1508             : 
    1509        3011 :         fname = Z_STRVAL_P(value);
    1510        3011 :         fname_len = Z_STRLEN_P(value);
    1511             : 
    1512             : phar_spl_fileinfo:
    1513        3139 :         if (base_len) {
    1514         128 :                 temp = expand_filepath(base, NULL);
    1515         128 :                 if (!temp) {
    1516           0 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Could not resolve file path");
    1517           0 :                         if (save) {
    1518           0 :                                 efree(save);
    1519             :                         }
    1520           0 :                         return ZEND_HASH_APPLY_STOP;
    1521             :                 }
    1522             : 
    1523         128 :                 base = temp;
    1524         128 :                 base_len = strlen(base);
    1525             : 
    1526         128 :                 if (strstr(fname, base)) {
    1527         128 :                         str_key_len = fname_len - base_len;
    1528             : 
    1529         128 :                         if (str_key_len <= 0) {
    1530           0 :                                 if (save) {
    1531           0 :                                         efree(save);
    1532           0 :                                         efree(temp);
    1533             :                                 }
    1534           0 :                                 return ZEND_HASH_APPLY_KEEP;
    1535             :                         }
    1536             : 
    1537         128 :                         str_key = fname + base_len;
    1538             : 
    1539         128 :                         if (*str_key == '/' || *str_key == '\\') {
    1540          15 :                                 str_key++;
    1541          15 :                                 str_key_len--;
    1542             :                         }
    1543             : 
    1544             :                 } else {
    1545           0 :                         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);
    1546             : 
    1547           0 :                         if (save) {
    1548           0 :                                 efree(save);
    1549           0 :                                 efree(temp);
    1550             :                         }
    1551             : 
    1552           0 :                         return ZEND_HASH_APPLY_STOP;
    1553             :                 }
    1554             :         } else {
    1555        3011 :                 if (iter->funcs->get_current_key) {
    1556             :                         zval key;
    1557        3011 :                         iter->funcs->get_current_key(iter, &key);
    1558             : 
    1559        3011 :                         if (EG(exception)) {
    1560           0 :                                 return ZEND_HASH_APPLY_STOP;
    1561             :                         }
    1562             : 
    1563        3011 :                         if (Z_TYPE(key) != IS_STRING) {
    1564             :                                 zval_dtor(&key);
    1565           3 :                                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned an invalid key (must return a string)", ZSTR_VAL(ce->name));
    1566           3 :                                 return ZEND_HASH_APPLY_STOP;
    1567             :                         }
    1568             : 
    1569        3008 :                         str_key_len = Z_STRLEN(key);
    1570        3008 :                         str_key = estrndup(Z_STRVAL(key), str_key_len);
    1571             : 
    1572        3008 :                         save = str_key;
    1573             :                         zval_dtor(&key);
    1574             :                 } else {
    1575           0 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned an invalid key (must return a string)", ZSTR_VAL(ce->name));
    1576           0 :                         return ZEND_HASH_APPLY_STOP;
    1577             :                 }
    1578             :         }
    1579             : 
    1580        3136 :         if (php_check_open_basedir(fname)) {
    1581           0 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned a path \"%s\" that open_basedir prevents opening", ZSTR_VAL(ce->name), fname);
    1582             : 
    1583           0 :                 if (save) {
    1584           0 :                         efree(save);
    1585             :                 }
    1586             : 
    1587           0 :                 if (temp) {
    1588           0 :                         efree(temp);
    1589             :                 }
    1590             : 
    1591           0 :                 return ZEND_HASH_APPLY_STOP;
    1592             :         }
    1593             : 
    1594             :         /* try to open source file, then create internal phar file and copy contents */
    1595        3136 :         fp = php_stream_open_wrapper(fname, "rb", STREAM_MUST_SEEK|0, &opened);
    1596             : 
    1597        3136 :         if (!fp) {
    1598           3 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned a file that could not be opened \"%s\"", ZSTR_VAL(ce->name), fname);
    1599             : 
    1600           3 :                 if (save) {
    1601           3 :                         efree(save);
    1602             :                 }
    1603             : 
    1604           3 :                 if (temp) {
    1605           0 :                         efree(temp);
    1606             :                 }
    1607             : 
    1608           3 :                 return ZEND_HASH_APPLY_STOP;
    1609             :         }
    1610             : after_open_fp:
    1611        3136 :         if (str_key_len >= sizeof(".phar")-1 && !memcmp(str_key, ".phar", sizeof(".phar")-1)) {
    1612             :                 /* silently skip any files that would be added to the magic .phar directory */
    1613           3 :                 if (save) {
    1614           3 :                         efree(save);
    1615             :                 }
    1616             : 
    1617           3 :                 if (temp) {
    1618           0 :                         efree(temp);
    1619             :                 }
    1620             : 
    1621           3 :                 if (opened) {
    1622           3 :                         zend_string_release(opened);
    1623             :                 }
    1624             : 
    1625           3 :                 if (close_fp) {
    1626           3 :                         php_stream_close(fp);
    1627             :                 }
    1628             : 
    1629           3 :                 return ZEND_HASH_APPLY_KEEP;
    1630             :         }
    1631             : 
    1632        3133 :         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))) {
    1633           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s cannot be created: %s", str_key, error);
    1634           0 :                 efree(error);
    1635             : 
    1636           0 :                 if (save) {
    1637           0 :                         efree(save);
    1638             :                 }
    1639             : 
    1640           0 :                 if (opened) {
    1641           0 :                         zend_string_release(opened);
    1642             :                 }
    1643             : 
    1644           0 :                 if (temp) {
    1645           0 :                         efree(temp);
    1646             :                 }
    1647             : 
    1648           0 :                 if (close_fp) {
    1649           0 :                         php_stream_close(fp);
    1650             :                 }
    1651             : 
    1652           0 :                 return ZEND_HASH_APPLY_STOP;
    1653             : 
    1654             :         } else {
    1655        3133 :                 if (error) {
    1656           0 :                         efree(error);
    1657             :                 }
    1658             :                 /* convert to PHAR_UFP */
    1659        3133 :                 if (data->internal_file->fp_type == PHAR_MOD) {
    1660        3133 :                         php_stream_close(data->internal_file->fp);
    1661             :                 }
    1662             : 
    1663        3133 :                 data->internal_file->fp = NULL;
    1664        3133 :                 data->internal_file->fp_type = PHAR_UFP;
    1665        3133 :                 data->internal_file->offset_abs = data->internal_file->offset = php_stream_tell(p_obj->fp);
    1666        3133 :                 data->fp = NULL;
    1667        3133 :                 php_stream_copy_to_stream_ex(fp, p_obj->fp, PHP_STREAM_COPY_ALL, &contents_len);
    1668        6266 :                 data->internal_file->uncompressed_filesize = data->internal_file->compressed_filesize =
    1669        3133 :                         php_stream_tell(p_obj->fp) - data->internal_file->offset;
    1670             :         }
    1671             : 
    1672        3133 :         if (close_fp) {
    1673        3130 :                 php_stream_close(fp);
    1674             :         }
    1675             : 
    1676        3133 :         add_assoc_str(p_obj->ret, str_key, opened);
    1677             : 
    1678        3133 :         if (save) {
    1679        3133 :                 efree(save);
    1680             :         }
    1681             : 
    1682        3133 :         if (temp) {
    1683         128 :                 efree(temp);
    1684             :         }
    1685             : 
    1686        3133 :         data->internal_file->compressed_filesize = data->internal_file->uncompressed_filesize = contents_len;
    1687        3133 :         phar_entry_delref(data);
    1688             : 
    1689        3133 :         return ZEND_HASH_APPLY_KEEP;
    1690             : }
    1691             : /* }}} */
    1692             : 
    1693             : /* {{{ proto array Phar::buildFromDirectory(string base_dir[, string regex])
    1694             :  * Construct a phar archive from an existing directory, recursively.
    1695             :  * Optional second parameter is a regular expression for filtering directory contents.
    1696             :  *
    1697             :  * Return value is an array mapping phar index to actual files added.
    1698             :  */
    1699          10 : PHP_METHOD(Phar, buildFromDirectory)
    1700             : {
    1701          10 :         char *dir, *error, *regex = NULL;
    1702          10 :         size_t dir_len, regex_len = 0;
    1703          10 :         zend_bool apply_reg = 0;
    1704             :         zval arg, arg2, iter, iteriter, regexiter;
    1705             :         struct _phar_t pass;
    1706             : 
    1707          20 :         PHAR_ARCHIVE_OBJECT();
    1708             : 
    1709          10 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    1710           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    1711             :                         "Cannot write to archive - write operations restricted by INI setting");
    1712           1 :                 return;
    1713             :         }
    1714             : 
    1715           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|s", &dir, &dir_len, &regex, &regex_len) == FAILURE) {
    1716           1 :                 RETURN_FALSE;
    1717             :         }
    1718             : 
    1719           8 :         if (SUCCESS != object_init_ex(&iter, spl_ce_RecursiveDirectoryIterator)) {
    1720           0 :                 zval_ptr_dtor(&iter);
    1721           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Unable to instantiate directory iterator for %s", phar_obj->archive->fname);
    1722           0 :                 RETURN_FALSE;
    1723             :         }
    1724             : 
    1725          16 :         ZVAL_STRINGL(&arg, dir, dir_len);
    1726           8 :         ZVAL_LONG(&arg2, SPL_FILE_DIR_SKIPDOTS|SPL_FILE_DIR_UNIXPATHS);
    1727             : 
    1728           8 :         zend_call_method_with_2_params(&iter, spl_ce_RecursiveDirectoryIterator,
    1729             :                         &spl_ce_RecursiveDirectoryIterator->constructor, "__construct", NULL, &arg, &arg2);
    1730             : 
    1731           8 :         zval_ptr_dtor(&arg);
    1732           8 :         if (EG(exception)) {
    1733           1 :                 zval_ptr_dtor(&iter);
    1734           1 :                 RETURN_FALSE;
    1735             :         }
    1736             : 
    1737           7 :         if (SUCCESS != object_init_ex(&iteriter, spl_ce_RecursiveIteratorIterator)) {
    1738           0 :                 zval_ptr_dtor(&iter);
    1739           0 :                 zval_ptr_dtor(&iteriter);
    1740           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Unable to instantiate directory iterator for %s", phar_obj->archive->fname);
    1741           0 :                 RETURN_FALSE;
    1742             :         }
    1743             : 
    1744           7 :         zend_call_method_with_1_params(&iteriter, spl_ce_RecursiveIteratorIterator,
    1745             :                         &spl_ce_RecursiveIteratorIterator->constructor, "__construct", NULL, &iter);
    1746             : 
    1747           7 :         if (EG(exception)) {
    1748           0 :                 zval_ptr_dtor(&iter);
    1749           0 :                 zval_ptr_dtor(&iteriter);
    1750           0 :                 RETURN_FALSE;
    1751             :         }
    1752             : 
    1753           7 :         zval_ptr_dtor(&iter);
    1754             : 
    1755           7 :         if (regex_len > 0) {
    1756           3 :                 apply_reg = 1;
    1757             : 
    1758           3 :                 if (SUCCESS != object_init_ex(&regexiter, spl_ce_RegexIterator)) {
    1759           0 :                         zval_ptr_dtor(&iteriter);
    1760             :                         zval_dtor(&regexiter);
    1761           0 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Unable to instantiate regex iterator for %s", phar_obj->archive->fname);
    1762           0 :                         RETURN_FALSE;
    1763             :                 }
    1764             : 
    1765           6 :                 ZVAL_STRINGL(&arg2, regex, regex_len);
    1766             : 
    1767           3 :                 zend_call_method_with_2_params(&regexiter, spl_ce_RegexIterator,
    1768             :                         &spl_ce_RegexIterator->constructor, "__construct", NULL, &iteriter, &arg2);
    1769           3 :                 zval_ptr_dtor(&arg2);
    1770             :         }
    1771             : 
    1772           7 :         array_init(return_value);
    1773             : 
    1774           7 :         pass.c = apply_reg ? Z_OBJCE(regexiter) : Z_OBJCE(iteriter);
    1775           7 :         pass.p = phar_obj;
    1776           7 :         pass.b = dir;
    1777           7 :         pass.l = dir_len;
    1778           7 :         pass.count = 0;
    1779           7 :         pass.ret = return_value;
    1780           7 :         pass.fp = php_stream_fopen_tmpfile();
    1781           7 :         if (pass.fp == NULL) {
    1782           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" unable to create temporary file", phar_obj->archive->fname);
    1783           0 :                 return;
    1784             :         }
    1785             : 
    1786           7 :         if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    1787           0 :                 zval_ptr_dtor(&iteriter);
    1788           0 :                 if (apply_reg) {
    1789           0 :                         zval_ptr_dtor(&regexiter);
    1790             :                 }
    1791           0 :                 php_stream_close(pass.fp);
    1792           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    1793           0 :                 return;
    1794             :         }
    1795             : 
    1796           7 :         if (SUCCESS == spl_iterator_apply((apply_reg ? &regexiter : &iteriter), (spl_iterator_apply_func_t) phar_build, (void *) &pass)) {
    1797           7 :                 zval_ptr_dtor(&iteriter);
    1798             : 
    1799           7 :                 if (apply_reg) {
    1800           3 :                         zval_ptr_dtor(&regexiter);
    1801             :                 }
    1802             : 
    1803           7 :                 phar_obj->archive->ufp = pass.fp;
    1804           7 :                 phar_flush(phar_obj->archive, 0, 0, 0, &error);
    1805             : 
    1806           7 :                 if (error) {
    1807           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    1808           0 :                         efree(error);
    1809             :                 }
    1810             : 
    1811             :         } else {
    1812           0 :                 zval_ptr_dtor(&iteriter);
    1813           0 :                 if (apply_reg) {
    1814           0 :                         zval_ptr_dtor(&regexiter);
    1815             :                 }
    1816           0 :                 php_stream_close(pass.fp);
    1817             :         }
    1818             : }
    1819             : /* }}} */
    1820             : 
    1821             : /* {{{ proto array Phar::buildFromIterator(Iterator iter[, string base_directory])
    1822             :  * Construct a phar archive from an iterator.  The iterator must return a series of strings
    1823             :  * that are full paths to files that should be added to the phar.  The iterator key should
    1824             :  * be the path that the file will have within the phar archive.
    1825             :  *
    1826             :  * If base directory is specified, then the key will be ignored, and instead the portion of
    1827             :  * the current value minus the base directory will be used
    1828             :  *
    1829             :  * Returned is an array mapping phar index to actual file added
    1830             :  */
    1831          24 : PHP_METHOD(Phar, buildFromIterator)
    1832             : {
    1833             :         zval *obj;
    1834             :         char *error;
    1835          24 :         size_t base_len = 0;
    1836          24 :         char *base = NULL;
    1837             :         struct _phar_t pass;
    1838             : 
    1839          48 :         PHAR_ARCHIVE_OBJECT();
    1840             : 
    1841          24 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    1842           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    1843             :                         "Cannot write out phar archive, phar is read-only");
    1844           1 :                 return;
    1845             :         }
    1846             : 
    1847          23 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|s", &obj, zend_ce_traversable, &base, &base_len) == FAILURE) {
    1848           2 :                 RETURN_FALSE;
    1849             :         }
    1850             : 
    1851          21 :         if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    1852           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    1853           0 :                 return;
    1854             :         }
    1855             : 
    1856          21 :         array_init(return_value);
    1857             : 
    1858          21 :         pass.c = Z_OBJCE_P(obj);
    1859          21 :         pass.p = phar_obj;
    1860          21 :         pass.b = base;
    1861          21 :         pass.l = base_len;
    1862          21 :         pass.ret = return_value;
    1863          21 :         pass.count = 0;
    1864          21 :         pass.fp = php_stream_fopen_tmpfile();
    1865          21 :         if (pass.fp == NULL) {
    1866           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\": unable to create temporary file", phar_obj->archive->fname);
    1867           0 :                 return;
    1868             :         }
    1869             : 
    1870          21 :         if (SUCCESS == spl_iterator_apply(obj, (spl_iterator_apply_func_t) phar_build, (void *) &pass)) {
    1871          12 :                 phar_obj->archive->ufp = pass.fp;
    1872          12 :                 phar_flush(phar_obj->archive, 0, 0, 0, &error);
    1873          12 :                 if (error) {
    1874           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    1875           0 :                         efree(error);
    1876             :                 }
    1877             :         } else {
    1878           9 :                 php_stream_close(pass.fp);
    1879             :         }
    1880             : }
    1881             : /* }}} */
    1882             : 
    1883             : /* {{{ proto int Phar::count()
    1884             :  * Returns the number of entries in the Phar archive
    1885             :  */
    1886           2 : PHP_METHOD(Phar, count)
    1887             : {
    1888             :         /* mode can be ignored, maximum depth is 1 */
    1889             :         zend_long mode;
    1890           4 :         PHAR_ARCHIVE_OBJECT();
    1891             : 
    1892           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &mode) == FAILURE) {
    1893           0 :                 RETURN_FALSE;
    1894             :         }
    1895             : 
    1896           2 :         RETURN_LONG(zend_hash_num_elements(&phar_obj->archive->manifest));
    1897             : }
    1898             : /* }}} */
    1899             : 
    1900             : /* {{{ proto bool Phar::isFileFormat(int format)
    1901             :  * Returns true if the phar archive is based on the tar/zip/phar file format depending
    1902             :  * on whether Phar::TAR, Phar::ZIP or Phar::PHAR was passed in
    1903             :  */
    1904          97 : PHP_METHOD(Phar, isFileFormat)
    1905             : {
    1906             :         zend_long type;
    1907         194 :         PHAR_ARCHIVE_OBJECT();
    1908             : 
    1909          97 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &type) == FAILURE) {
    1910           1 :                 RETURN_FALSE;
    1911             :         }
    1912             : 
    1913          96 :         switch (type) {
    1914             :                 case PHAR_FORMAT_TAR:
    1915          43 :                         RETURN_BOOL(phar_obj->archive->is_tar);
    1916             :                 case PHAR_FORMAT_ZIP:
    1917          28 :                         RETURN_BOOL(phar_obj->archive->is_zip);
    1918             :                 case PHAR_FORMAT_PHAR:
    1919          24 :                         RETURN_BOOL(!phar_obj->archive->is_tar && !phar_obj->archive->is_zip);
    1920             :                 default:
    1921           1 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "Unknown file format specified");
    1922             :         }
    1923             : }
    1924             : /* }}} */
    1925             : 
    1926         167 : static int phar_copy_file_contents(phar_entry_info *entry, php_stream *fp) /* {{{ */
    1927             : {
    1928             :         char *error;
    1929             :         zend_off_t offset;
    1930             :         phar_entry_info *link;
    1931             : 
    1932         167 :         if (FAILURE == phar_open_entry_fp(entry, &error, 1)) {
    1933           0 :                 if (error) {
    1934           0 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    1935           0 :                                 "Cannot convert phar archive \"%s\", unable to open entry \"%s\" contents: %s", entry->phar->fname, entry->filename, error);
    1936           0 :                         efree(error);
    1937             :                 } else {
    1938           0 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    1939           0 :                                 "Cannot convert phar archive \"%s\", unable to open entry \"%s\" contents", entry->phar->fname, entry->filename);
    1940             :                 }
    1941           0 :                 return FAILURE;
    1942             :         }
    1943             : 
    1944             :         /* copy old contents in entirety */
    1945         167 :         phar_seek_efp(entry, 0, SEEK_SET, 0, 1);
    1946         167 :         offset = php_stream_tell(fp);
    1947         167 :         link = phar_get_link_source(entry);
    1948             : 
    1949         167 :         if (!link) {
    1950           0 :                 link = entry;
    1951             :         }
    1952             : 
    1953         167 :         if (SUCCESS != php_stream_copy_to_stream_ex(phar_get_efp(link, 0), fp, link->uncompressed_filesize, NULL)) {
    1954           0 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    1955           0 :                         "Cannot convert phar archive \"%s\", unable to copy entry \"%s\" contents", entry->phar->fname, entry->filename);
    1956           0 :                 return FAILURE;
    1957             :         }
    1958             : 
    1959         167 :         if (entry->fp_type == PHAR_MOD) {
    1960             :                 /* save for potential restore on error */
    1961           0 :                 entry->cfp = entry->fp;
    1962           0 :                 entry->fp = NULL;
    1963             :         }
    1964             : 
    1965             :         /* set new location of file contents */
    1966         167 :         entry->fp_type = PHAR_FP;
    1967         167 :         entry->offset = offset;
    1968         167 :         return SUCCESS;
    1969             : }
    1970             : /* }}} */
    1971             : 
    1972          53 : static zend_object *phar_rename_archive(phar_archive_data **sphar, char *ext, zend_bool compress) /* {{{ */
    1973             : {
    1974          53 :         const char *oldname = NULL;
    1975          53 :         phar_archive_data *phar = *sphar;
    1976          53 :         char *oldpath = NULL;
    1977          53 :         char *basename = NULL, *basepath = NULL;
    1978          53 :         char *newname = NULL, *newpath = NULL;
    1979             :         zval ret, arg1;
    1980             :         zend_class_entry *ce;
    1981             :         char *error;
    1982             :         const char *pcr_error;
    1983          53 :         int ext_len = ext ? strlen(ext) : 0;
    1984             :         int oldname_len;
    1985          53 :         phar_archive_data *pphar = NULL;
    1986             :         php_stream_statbuf ssb;
    1987             : 
    1988          53 :         if (!ext) {
    1989          37 :                 if (phar->is_zip) {
    1990             : 
    1991           8 :                         if (phar->is_data) {
    1992           3 :                                 ext = "zip";
    1993             :                         } else {
    1994           5 :                                 ext = "phar.zip";
    1995             :                         }
    1996             : 
    1997          29 :                 } else if (phar->is_tar) {
    1998             : 
    1999          21 :                         switch (phar->flags) {
    2000             :                                 case PHAR_FILE_COMPRESSED_GZ:
    2001           5 :                                         if (phar->is_data) {
    2002           1 :                                                 ext = "tar.gz";
    2003             :                                         } else {
    2004           4 :                                                 ext = "phar.tar.gz";
    2005             :                                         }
    2006           5 :                                         break;
    2007             :                                 case PHAR_FILE_COMPRESSED_BZ2:
    2008           3 :                                         if (phar->is_data) {
    2009           1 :                                                 ext = "tar.bz2";
    2010             :                                         } else {
    2011           2 :                                                 ext = "phar.tar.bz2";
    2012             :                                         }
    2013           3 :                                         break;
    2014             :                                 default:
    2015          13 :                                         if (phar->is_data) {
    2016           5 :                                                 ext = "tar";
    2017             :                                         } else {
    2018           8 :                                                 ext = "phar.tar";
    2019             :                                         }
    2020             :                         }
    2021             :                 } else {
    2022             : 
    2023           8 :                         switch (phar->flags) {
    2024             :                                 case PHAR_FILE_COMPRESSED_GZ:
    2025           3 :                                         ext = "phar.gz";
    2026           3 :                                         break;
    2027             :                                 case PHAR_FILE_COMPRESSED_BZ2:
    2028           1 :                                         ext = "phar.bz2";
    2029           1 :                                         break;
    2030             :                                 default:
    2031           4 :                                         ext = "phar";
    2032             :                         }
    2033             :                 }
    2034          16 :         } else if (phar_path_check(&ext, &ext_len, &pcr_error) > pcr_is_ok) {
    2035             : 
    2036           0 :                 if (phar->is_data) {
    2037           0 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "data phar converted from \"%s\" has invalid extension %s", phar->fname, ext);
    2038             :                 } else {
    2039           0 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "phar converted from \"%s\" has invalid extension %s", phar->fname, ext);
    2040             :                 }
    2041           0 :                 return NULL;
    2042             :         }
    2043             : 
    2044          53 :         if (ext[0] == '.') {
    2045          11 :                 ++ext;
    2046             :         }
    2047             : 
    2048          53 :         oldpath = estrndup(phar->fname, phar->fname_len);
    2049         106 :         if ((oldname = zend_memrchr(phar->fname, '/', phar->fname_len))) {
    2050          53 :                 ++oldname;
    2051             :         } else {
    2052           0 :                 oldname = phar->fname;
    2053             :         }
    2054          53 :         oldname_len = strlen(oldname);
    2055             : 
    2056          53 :         basename = estrndup(oldname, oldname_len);
    2057          53 :         spprintf(&newname, 0, "%s.%s", strtok(basename, "."), ext);
    2058          53 :         efree(basename);
    2059             : 
    2060             : 
    2061             : 
    2062          53 :         basepath = estrndup(oldpath, (strlen(oldpath) - oldname_len));
    2063          53 :         phar->fname_len = spprintf(&newpath, 0, "%s%s", basepath, newname);
    2064          53 :         phar->fname = newpath;
    2065          53 :         phar->ext = newpath + phar->fname_len - strlen(ext) - 1;
    2066          53 :         efree(basepath);
    2067          53 :         efree(newname);
    2068             : 
    2069          53 :         if (PHAR_G(manifest_cached) && NULL != (pphar = zend_hash_str_find_ptr(&cached_phars, newpath, phar->fname_len))) {
    2070           0 :                 efree(oldpath);
    2071           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Unable to add newly converted phar \"%s\" to the list of phars, new phar name is in phar.cache_list", phar->fname);
    2072           0 :                 return NULL;
    2073             :         }
    2074             : 
    2075         106 :         if (NULL != (pphar = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), newpath, phar->fname_len))) {
    2076           2 :                 if (pphar->fname_len == phar->fname_len && !memcmp(pphar->fname, phar->fname, phar->fname_len)) {
    2077           2 :                         if (!zend_hash_num_elements(&phar->manifest)) {
    2078           1 :                                 pphar->is_tar = phar->is_tar;
    2079           1 :                                 pphar->is_zip = phar->is_zip;
    2080           1 :                                 pphar->is_data = phar->is_data;
    2081           1 :                                 pphar->flags = phar->flags;
    2082           1 :                                 pphar->fp = phar->fp;
    2083           1 :                                 phar->fp = NULL;
    2084           1 :                                 phar_destroy_phar_data(phar);
    2085           1 :                                 *sphar = NULL;
    2086           1 :                                 phar = pphar;
    2087           1 :                                 phar->refcount++;
    2088           1 :                                 newpath = oldpath;
    2089           1 :                                 goto its_ok;
    2090             :                         }
    2091             :                 }
    2092             : 
    2093           1 :                 efree(oldpath);
    2094           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Unable to add newly converted phar \"%s\" to the list of phars, a phar with that name already exists", phar->fname);
    2095           1 :                 return NULL;
    2096             :         }
    2097             : its_ok:
    2098          52 :         if (SUCCESS == php_stream_stat_path(newpath, &ssb)) {
    2099           2 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "phar \"%s\" exists and must be unlinked prior to conversion", newpath);
    2100           2 :                 efree(oldpath);
    2101           2 :                 return NULL;
    2102             :         }
    2103          50 :         if (!phar->is_data) {
    2104          35 :                 if (SUCCESS != phar_detect_phar_fname_ext(newpath, phar->fname_len, (const char **) &(phar->ext), &(phar->ext_len), 1, 1, 1)) {
    2105           1 :                         efree(oldpath);
    2106           1 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "phar \"%s\" has invalid extension %s", phar->fname, ext);
    2107           1 :                         return NULL;
    2108             :                 }
    2109             : 
    2110          34 :                 if (phar->alias) {
    2111          22 :                         if (phar->is_temporary_alias) {
    2112          15 :                                 phar->alias = NULL;
    2113          15 :                                 phar->alias_len = 0;
    2114             :                         } else {
    2115           7 :                                 phar->alias = estrndup(newpath, strlen(newpath));
    2116           7 :                                 phar->alias_len = strlen(newpath);
    2117           7 :                                 phar->is_temporary_alias = 1;
    2118           7 :                                 zend_hash_str_update_ptr(&(PHAR_G(phar_alias_map)), newpath, phar->fname_len, phar);
    2119             :                         }
    2120             :                 }
    2121             : 
    2122             :         } else {
    2123             : 
    2124          15 :                 if (SUCCESS != phar_detect_phar_fname_ext(newpath, phar->fname_len, (const char **) &(phar->ext), &(phar->ext_len), 0, 1, 1)) {
    2125           3 :                         efree(oldpath);
    2126           3 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "data phar \"%s\" has invalid extension %s", phar->fname, ext);
    2127           3 :                         return NULL;
    2128             :                 }
    2129             : 
    2130          12 :                 phar->alias = NULL;
    2131          12 :                 phar->alias_len = 0;
    2132             :         }
    2133             : 
    2134          92 :         if ((!pphar || phar == pphar) && NULL == zend_hash_str_update_ptr(&(PHAR_G(phar_fname_map)), newpath, phar->fname_len, phar)) {
    2135           0 :                 efree(oldpath);
    2136           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Unable to add newly converted phar \"%s\" to the list of phars", phar->fname);
    2137           0 :                 return NULL;
    2138             :         }
    2139             : 
    2140          46 :         phar_flush(phar, 0, 0, 1, &error);
    2141             : 
    2142          46 :         if (error) {
    2143           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "%s", error);
    2144           0 :                 efree(error);
    2145           0 :                 efree(oldpath);
    2146           0 :                 return NULL;
    2147             :         }
    2148             : 
    2149          46 :         efree(oldpath);
    2150             : 
    2151          46 :         if (phar->is_data) {
    2152          12 :                 ce = phar_ce_data;
    2153             :         } else {
    2154          34 :                 ce = phar_ce_archive;
    2155             :         }
    2156             : 
    2157          46 :         ZVAL_NULL(&ret);
    2158          46 :         if (SUCCESS != object_init_ex(&ret, ce)) {
    2159             :                 zval_dtor(&ret);
    2160           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Unable to instantiate phar object when converting archive \"%s\"", phar->fname);
    2161           0 :                 return NULL;
    2162             :         }
    2163             : 
    2164          92 :         ZVAL_STRINGL(&arg1, phar->fname, phar->fname_len);
    2165             : 
    2166          46 :         zend_call_method_with_1_params(&ret, ce, &ce->constructor, "__construct", NULL, &arg1);
    2167          46 :         zval_ptr_dtor(&arg1);
    2168          46 :         return Z_OBJ(ret);
    2169             : }
    2170             : /* }}} */
    2171             : 
    2172          53 : static zend_object *phar_convert_to_other(phar_archive_data *source, int convert, char *ext, uint32_t flags) /* {{{ */
    2173             : {
    2174             :         phar_archive_data *phar;
    2175             :         phar_entry_info *entry, newentry;
    2176             :         zend_object *ret;
    2177             : 
    2178             :         /* invalidate phar cache */
    2179          53 :         PHAR_G(last_phar) = NULL;
    2180          53 :         PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
    2181             : 
    2182          53 :         phar = (phar_archive_data *) ecalloc(1, sizeof(phar_archive_data));
    2183             :         /* set whole-archive compression and type from parameter */
    2184          53 :         phar->flags = flags;
    2185          53 :         phar->is_data = source->is_data;
    2186             : 
    2187          53 :         switch (convert) {
    2188             :                 case PHAR_FORMAT_TAR:
    2189          28 :                         phar->is_tar = 1;
    2190          28 :                         break;
    2191             :                 case PHAR_FORMAT_ZIP:
    2192          13 :                         phar->is_zip = 1;
    2193          13 :                         break;
    2194             :                 default:
    2195          12 :                         phar->is_data = 0;
    2196             :                         break;
    2197             :         }
    2198             : 
    2199          53 :         zend_hash_init(&(phar->manifest), sizeof(phar_entry_info),
    2200             :                 zend_get_hash_value, destroy_phar_manifest_entry, 0);
    2201          53 :         zend_hash_init(&phar->mounted_dirs, sizeof(char *),
    2202             :                 zend_get_hash_value, NULL, 0);
    2203          53 :         zend_hash_init(&phar->virtual_dirs, sizeof(char *),
    2204             :                 zend_get_hash_value, NULL, 0);
    2205             : 
    2206          53 :         phar->fp = php_stream_fopen_tmpfile();
    2207          53 :         if (phar->fp == NULL) {
    2208           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "unable to create temporary file");
    2209           0 :                 return NULL;
    2210             :         }
    2211          53 :         phar->fname = source->fname;
    2212          53 :         phar->fname_len = source->fname_len;
    2213          53 :         phar->is_temporary_alias = source->is_temporary_alias;
    2214          53 :         phar->alias = source->alias;
    2215             : 
    2216         106 :         if (Z_TYPE(source->metadata) != IS_UNDEF) {
    2217           2 :                 ZVAL_DUP(&phar->metadata, &source->metadata);
    2218           2 :                 phar->metadata_len = 0;
    2219             :         }
    2220             : 
    2221             :         /* first copy each file's uncompressed contents to a temporary file and set per-file flags */
    2222         397 :         ZEND_HASH_FOREACH_PTR(&source->manifest, entry) {
    2223             : 
    2224         172 :                 newentry = *entry;
    2225             : 
    2226         172 :                 if (newentry.link) {
    2227           3 :                         newentry.link = estrdup(newentry.link);
    2228           3 :                         goto no_copy;
    2229             :                 }
    2230             : 
    2231         169 :                 if (newentry.tmp) {
    2232           2 :                         newentry.tmp = estrdup(newentry.tmp);
    2233           2 :                         goto no_copy;
    2234             :                 }
    2235             : 
    2236         167 :                 newentry.metadata_str.s = NULL;
    2237             : 
    2238         167 :                 if (FAILURE == phar_copy_file_contents(&newentry, phar->fp)) {
    2239           0 :                         zend_hash_destroy(&(phar->manifest));
    2240           0 :                         php_stream_close(phar->fp);
    2241           0 :                         efree(phar);
    2242             :                         /* exception already thrown */
    2243           0 :                         return NULL;
    2244             :                 }
    2245             : no_copy:
    2246         172 :                 newentry.filename = estrndup(newentry.filename, newentry.filename_len);
    2247             : 
    2248         172 :                 if (Z_TYPE(newentry.metadata) != IS_UNDEF) {
    2249             :                         zval_copy_ctor(&newentry.metadata);
    2250           8 :                         newentry.metadata_str.s = NULL;
    2251             :                 }
    2252             : 
    2253         172 :                 newentry.is_zip = phar->is_zip;
    2254         172 :                 newentry.is_tar = phar->is_tar;
    2255             : 
    2256         172 :                 if (newentry.is_tar) {
    2257          97 :                         newentry.tar_type = (entry->is_dir ? TAR_DIR : TAR_FILE);
    2258             :                 }
    2259             : 
    2260         172 :                 newentry.is_modified = 1;
    2261         172 :                 newentry.phar = phar;
    2262         172 :                 newentry.old_flags = newentry.flags & ~PHAR_ENT_COMPRESSION_MASK; /* remove compression from old_flags */
    2263         172 :                 phar_set_inode(&newentry);
    2264         172 :                 zend_hash_str_add_mem(&(phar->manifest), newentry.filename, newentry.filename_len, (void*)&newentry, sizeof(phar_entry_info));
    2265         172 :                 phar_add_virtual_dirs(phar, newentry.filename, newentry.filename_len);
    2266             :         } ZEND_HASH_FOREACH_END();
    2267             : 
    2268          53 :         if ((ret = phar_rename_archive(&phar, ext, 0))) {
    2269          46 :                 return ret;
    2270             :         } else {
    2271           7 :                 if(phar != NULL) {
    2272           6 :                         zend_hash_destroy(&(phar->manifest));
    2273           6 :                         zend_hash_destroy(&(phar->mounted_dirs));
    2274           6 :                         zend_hash_destroy(&(phar->virtual_dirs));
    2275           6 :                         if (phar->fp) {
    2276           6 :                                 php_stream_close(phar->fp);
    2277             :                         }
    2278           6 :                         efree(phar->fname);
    2279           6 :                         efree(phar);
    2280             :                 }
    2281           7 :                 return NULL;
    2282             :         }
    2283             : }
    2284             : /* }}} */
    2285             : 
    2286             : /* {{{ proto object Phar::convertToExecutable([int format[, int compression [, string file_ext]]])
    2287             :  * Convert a phar.tar or phar.zip archive to the phar file format. The
    2288             :  * optional parameter allows the user to determine the new
    2289             :  * filename extension (default is phar).
    2290             :  */
    2291          37 : PHP_METHOD(Phar, convertToExecutable)
    2292             : {
    2293          37 :         char *ext = NULL;
    2294             :         int is_data;
    2295          37 :         size_t ext_len = 0;
    2296             :         uint32_t flags;
    2297             :         zend_object *ret;
    2298             :         /* a number that is not 0, 1 or 2 (Which is also Greg's birthday, so there) */
    2299          37 :         zend_long format = 9021976, method = 9021976;
    2300          74 :         PHAR_ARCHIVE_OBJECT();
    2301             : 
    2302          37 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|lls", &format, &method, &ext, &ext_len) == FAILURE) {
    2303           1 :                 return;
    2304             :         }
    2305             : 
    2306          36 :         if (PHAR_G(readonly)) {
    2307           2 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2308             :                         "Cannot write out executable phar archive, phar is read-only");
    2309           2 :                 return;
    2310             :         }
    2311             : 
    2312          34 :         switch (format) {
    2313             :                 case 9021976:
    2314             :                 case PHAR_FORMAT_SAME: /* null is converted to 0 */
    2315             :                         /* by default, use the existing format */
    2316           2 :                         if (phar_obj->archive->is_tar) {
    2317           2 :                                 format = PHAR_FORMAT_TAR;
    2318           0 :                         } else if (phar_obj->archive->is_zip) {
    2319           0 :                                 format = PHAR_FORMAT_ZIP;
    2320             :                         } else {
    2321           0 :                                 format = PHAR_FORMAT_PHAR;
    2322             :                         }
    2323           2 :                         break;
    2324             :                 case PHAR_FORMAT_PHAR:
    2325             :                 case PHAR_FORMAT_TAR:
    2326             :                 case PHAR_FORMAT_ZIP:
    2327          31 :                         break;
    2328             :                 default:
    2329           1 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2330             :                                 "Unknown file format specified, please pass one of Phar::PHAR, Phar::TAR or Phar::ZIP");
    2331           1 :                         return;
    2332             :         }
    2333             : 
    2334          33 :         switch (method) {
    2335             :                 case 9021976:
    2336          14 :                         flags = phar_obj->archive->flags & PHAR_FILE_COMPRESSION_MASK;
    2337          14 :                         break;
    2338             :                 case 0:
    2339           9 :                         flags = PHAR_FILE_COMPRESSED_NONE;
    2340           9 :                         break;
    2341             :                 case PHAR_ENT_COMPRESSED_GZ:
    2342           6 :                         if (format == PHAR_FORMAT_ZIP) {
    2343           1 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2344             :                                         "Cannot compress entire archive with gzip, zip archives do not support whole-archive compression");
    2345           1 :                                 return;
    2346             :                         }
    2347             : 
    2348           5 :                         if (!PHAR_G(has_zlib)) {
    2349           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2350             :                                         "Cannot compress entire archive with gzip, enable ext/zlib in php.ini");
    2351           0 :                                 return;
    2352             :                         }
    2353             : 
    2354           5 :                         flags = PHAR_FILE_COMPRESSED_GZ;
    2355           5 :                         break;
    2356             :                 case PHAR_ENT_COMPRESSED_BZ2:
    2357           3 :                         if (format == PHAR_FORMAT_ZIP) {
    2358           1 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2359             :                                         "Cannot compress entire archive with bz2, zip archives do not support whole-archive compression");
    2360           1 :                                 return;
    2361             :                         }
    2362             : 
    2363           2 :                         if (!PHAR_G(has_bz2)) {
    2364           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2365             :                                         "Cannot compress entire archive with bz2, enable ext/bz2 in php.ini");
    2366           0 :                                 return;
    2367             :                         }
    2368             : 
    2369           2 :                         flags = PHAR_FILE_COMPRESSED_BZ2;
    2370           2 :                         break;
    2371             :                 default:
    2372           1 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2373             :                                 "Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2");
    2374           1 :                         return;
    2375             :         }
    2376             : 
    2377          30 :         is_data = phar_obj->archive->is_data;
    2378          30 :         phar_obj->archive->is_data = 0;
    2379          30 :         ret = phar_convert_to_other(phar_obj->archive, format, ext, flags);
    2380          30 :         phar_obj->archive->is_data = is_data;
    2381             : 
    2382          30 :         if (ret) {
    2383          28 :                 ZVAL_OBJ(return_value, ret);
    2384             :         } else {
    2385           2 :                 RETURN_NULL();
    2386             :         }
    2387             : }
    2388             : /* }}} */
    2389             : 
    2390             : /* {{{ proto object Phar::convertToData([int format[, int compression [, string file_ext]]])
    2391             :  * Convert an archive to a non-executable .tar or .zip.
    2392             :  * The optional parameter allows the user to determine the new
    2393             :  * filename extension (default is .zip or .tar).
    2394             :  */
    2395          23 : PHP_METHOD(Phar, convertToData)
    2396             : {
    2397          23 :         char *ext = NULL;
    2398             :         int is_data;
    2399          23 :         size_t ext_len = 0;
    2400             :         uint32_t flags;
    2401             :         zend_object *ret;
    2402             :         /* a number that is not 0, 1 or 2 (Which is also Greg's birthday so there) */
    2403          23 :         zend_long format = 9021976, method = 9021976;
    2404          46 :         PHAR_ARCHIVE_OBJECT();
    2405             : 
    2406          23 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|lls", &format, &method, &ext, &ext_len) == FAILURE) {
    2407           1 :                 return;
    2408             :         }
    2409             : 
    2410          22 :         switch (format) {
    2411             :                 case 9021976:
    2412             :                 case PHAR_FORMAT_SAME: /* null is converted to 0 */
    2413             :                         /* by default, use the existing format */
    2414           3 :                         if (phar_obj->archive->is_tar) {
    2415           1 :                                 format = PHAR_FORMAT_TAR;
    2416           2 :                         } else if (phar_obj->archive->is_zip) {
    2417           1 :                                 format = PHAR_FORMAT_ZIP;
    2418             :                         } else {
    2419           1 :                                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2420             :                                         "Cannot write out data phar archive, use Phar::TAR or Phar::ZIP");
    2421           1 :                                 return;
    2422             :                         }
    2423           2 :                         break;
    2424             :                 case PHAR_FORMAT_PHAR:
    2425           1 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2426             :                                 "Cannot write out data phar archive, use Phar::TAR or Phar::ZIP");
    2427           1 :                         return;
    2428             :                 case PHAR_FORMAT_TAR:
    2429             :                 case PHAR_FORMAT_ZIP:
    2430          17 :                         break;
    2431             :                 default:
    2432           1 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2433             :                                 "Unknown file format specified, please pass one of Phar::TAR or Phar::ZIP");
    2434           1 :                         return;
    2435             :         }
    2436             : 
    2437          19 :         switch (method) {
    2438             :                 case 9021976:
    2439           8 :                         flags = phar_obj->archive->flags & PHAR_FILE_COMPRESSION_MASK;
    2440           8 :                         break;
    2441             :                 case 0:
    2442           3 :                         flags = PHAR_FILE_COMPRESSED_NONE;
    2443           3 :                         break;
    2444             :                 case PHAR_ENT_COMPRESSED_GZ:
    2445           5 :                         if (format == PHAR_FORMAT_ZIP) {
    2446           1 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2447             :                                         "Cannot compress entire archive with gzip, zip archives do not support whole-archive compression");
    2448           1 :                                 return;
    2449             :                         }
    2450             : 
    2451           4 :                         if (!PHAR_G(has_zlib)) {
    2452           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2453             :                                         "Cannot compress entire archive with gzip, enable ext/zlib in php.ini");
    2454           0 :                                 return;
    2455             :                         }
    2456             : 
    2457           4 :                         flags = PHAR_FILE_COMPRESSED_GZ;
    2458           4 :                         break;
    2459             :                 case PHAR_ENT_COMPRESSED_BZ2:
    2460           2 :                         if (format == PHAR_FORMAT_ZIP) {
    2461           1 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2462             :                                         "Cannot compress entire archive with bz2, zip archives do not support whole-archive compression");
    2463           1 :                                 return;
    2464             :                         }
    2465             : 
    2466           1 :                         if (!PHAR_G(has_bz2)) {
    2467           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2468             :                                         "Cannot compress entire archive with bz2, enable ext/bz2 in php.ini");
    2469           0 :                                 return;
    2470             :                         }
    2471             : 
    2472           1 :                         flags = PHAR_FILE_COMPRESSED_BZ2;
    2473           1 :                         break;
    2474             :                 default:
    2475           1 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2476             :                                 "Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2");
    2477           1 :                         return;
    2478             :         }
    2479             : 
    2480          16 :         is_data = phar_obj->archive->is_data;
    2481          16 :         phar_obj->archive->is_data = 1;
    2482          16 :         ret = phar_convert_to_other(phar_obj->archive, format, ext, flags);
    2483          16 :         phar_obj->archive->is_data = is_data;
    2484             : 
    2485          16 :         if (ret) {
    2486          11 :                 ZVAL_OBJ(return_value, ret);
    2487             :         } else {
    2488           5 :                 RETURN_NULL();
    2489             :         }
    2490             : }
    2491             : /* }}} */
    2492             : 
    2493             : /* {{{ proto int|false Phar::isCompressed()
    2494             :  * Returns Phar::GZ or PHAR::BZ2 if the entire archive is compressed
    2495             :  * (.tar.gz/tar.bz2 and so on), or FALSE otherwise.
    2496             :  */
    2497          19 : PHP_METHOD(Phar, isCompressed)
    2498             : {
    2499          38 :         PHAR_ARCHIVE_OBJECT();
    2500             : 
    2501          19 :         if (zend_parse_parameters_none() == FAILURE) {
    2502           0 :                 return;
    2503             :         }
    2504             : 
    2505          19 :         if (phar_obj->archive->flags & PHAR_FILE_COMPRESSED_GZ) {
    2506          10 :                 RETURN_LONG(PHAR_ENT_COMPRESSED_GZ);
    2507             :         }
    2508             : 
    2509           9 :         if (phar_obj->archive->flags & PHAR_FILE_COMPRESSED_BZ2) {
    2510           7 :                 RETURN_LONG(PHAR_ENT_COMPRESSED_BZ2);
    2511             :         }
    2512             : 
    2513           2 :         RETURN_FALSE;
    2514             : }
    2515             : /* }}} */
    2516             : 
    2517             : /* {{{ proto bool Phar::isWritable()
    2518             :  * Returns true if phar.readonly=0 or phar is a PharData AND the actual file is writable.
    2519             :  */
    2520           8 : PHP_METHOD(Phar, isWritable)
    2521             : {
    2522             :         php_stream_statbuf ssb;
    2523          16 :         PHAR_ARCHIVE_OBJECT();
    2524             : 
    2525           8 :         if (zend_parse_parameters_none() == FAILURE) {
    2526           0 :                 return;
    2527             :         }
    2528             : 
    2529           8 :         if (!phar_obj->archive->is_writeable) {
    2530           3 :                 RETURN_FALSE;
    2531             :         }
    2532             : 
    2533           5 :         if (SUCCESS != php_stream_stat_path(phar_obj->archive->fname, &ssb)) {
    2534           1 :                 if (phar_obj->archive->is_brandnew) {
    2535             :                         /* assume it works if the file doesn't exist yet */
    2536           1 :                         RETURN_TRUE;
    2537             :                 }
    2538           0 :                 RETURN_FALSE;
    2539             :         }
    2540             : 
    2541           4 :         RETURN_BOOL((ssb.sb.st_mode & (S_IWOTH | S_IWGRP | S_IWUSR)) != 0);
    2542             : }
    2543             : /* }}} */
    2544             : 
    2545             : /* {{{ proto bool Phar::delete(string entry)
    2546             :  * Deletes a named file within the archive.
    2547             :  */
    2548           7 : PHP_METHOD(Phar, delete)
    2549             : {
    2550             :         char *fname;
    2551             :         size_t fname_len;
    2552             :         char *error;
    2553             :         phar_entry_info *entry;
    2554          14 :         PHAR_ARCHIVE_OBJECT();
    2555             : 
    2556           7 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    2557           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2558             :                         "Cannot write out phar archive, phar is read-only");
    2559           1 :                 return;
    2560             :         }
    2561             : 
    2562           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) {
    2563           1 :                 RETURN_FALSE;
    2564             :         }
    2565             : 
    2566           5 :         if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    2567           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    2568           0 :                 return;
    2569             :         }
    2570           5 :         if (zend_hash_str_exists(&phar_obj->archive->manifest, fname, (uint) fname_len)) {
    2571           8 :                 if (NULL != (entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, (uint) fname_len))) {
    2572           4 :                         if (entry->is_deleted) {
    2573             :                                 /* entry is deleted, but has not been flushed to disk yet */
    2574           0 :                                 RETURN_TRUE;
    2575             :                         } else {
    2576           4 :                                 entry->is_deleted = 1;
    2577           4 :                                 entry->is_modified = 1;
    2578           4 :                                 phar_obj->archive->is_modified = 1;
    2579             :                         }
    2580             :                 }
    2581             :         } else {
    2582           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist and cannot be deleted", fname);
    2583           1 :                 RETURN_FALSE;
    2584             :         }
    2585             : 
    2586           4 :         phar_flush(phar_obj->archive, NULL, 0, 0, &error);
    2587           4 :         if (error) {
    2588           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    2589           0 :                 efree(error);
    2590             :         }
    2591             : 
    2592           4 :         RETURN_TRUE;
    2593             : }
    2594             : /* }}} */
    2595             : 
    2596             : /* {{{ proto int Phar::getAlias()
    2597             :  * Returns the alias for the Phar or NULL.
    2598             :  */
    2599          42 : PHP_METHOD(Phar, getAlias)
    2600             : {
    2601          84 :         PHAR_ARCHIVE_OBJECT();
    2602             : 
    2603          42 :         if (zend_parse_parameters_none() == FAILURE) {
    2604           0 :                 return;
    2605             :         }
    2606             : 
    2607          42 :         if (phar_obj->archive->alias && phar_obj->archive->alias != phar_obj->archive->fname) {
    2608          66 :                 RETURN_STRINGL(phar_obj->archive->alias, phar_obj->archive->alias_len);
    2609             :         }
    2610             : }
    2611             : /* }}} */
    2612             : 
    2613             : /* {{{ proto int Phar::getPath()
    2614             :  * Returns the real path to the phar archive on disk
    2615             :  */
    2616          15 : PHP_METHOD(Phar, getPath)
    2617             : {
    2618          30 :         PHAR_ARCHIVE_OBJECT();
    2619             : 
    2620          15 :         if (zend_parse_parameters_none() == FAILURE) {
    2621           0 :                 return;
    2622             :         }
    2623             : 
    2624          30 :         RETURN_STRINGL(phar_obj->archive->fname, phar_obj->archive->fname_len);
    2625             : }
    2626             : /* }}} */
    2627             : 
    2628             : /* {{{ proto bool Phar::setAlias(string alias)
    2629             :  * Sets the alias for a Phar archive. The default value is the full path
    2630             :  * to the archive.
    2631             :  */
    2632          53 : PHP_METHOD(Phar, setAlias)
    2633             : {
    2634             :         char *alias, *error, *oldalias;
    2635             :         phar_archive_data *fd_ptr;
    2636             :         size_t alias_len, oldalias_len;
    2637          53 :         int old_temp, readd = 0;
    2638             : 
    2639         106 :         PHAR_ARCHIVE_OBJECT();
    2640             : 
    2641          53 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    2642           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2643             :                         "Cannot write out phar archive, phar is read-only");
    2644           1 :                 RETURN_FALSE;
    2645             :         }
    2646             : 
    2647             :         /* invalidate phar cache */
    2648          52 :         PHAR_G(last_phar) = NULL;
    2649          52 :         PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
    2650             : 
    2651          52 :         if (phar_obj->archive->is_data) {
    2652           3 :                 if (phar_obj->archive->is_tar) {
    2653           2 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2654             :                                 "A Phar alias cannot be set in a plain tar archive");
    2655             :                 } else {
    2656           1 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2657             :                                 "A Phar alias cannot be set in a plain zip archive");
    2658             :                 }
    2659           3 :                 RETURN_FALSE;
    2660             :         }
    2661             : 
    2662          49 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &alias, &alias_len) == SUCCESS) {
    2663          48 :                 if (alias_len == (size_t)phar_obj->archive->alias_len && memcmp(phar_obj->archive->alias, alias, alias_len) == 0) {
    2664           0 :                         RETURN_TRUE;
    2665             :                 }
    2666          96 :                 if (alias_len && NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len))) {
    2667           4 :                         spprintf(&error, 0, "alias \"%s\" is already used for archive \"%s\" and cannot be used for other archives", alias, fd_ptr->fname);
    2668           4 :                         if (SUCCESS == phar_free_alias(fd_ptr, alias, alias_len)) {
    2669           1 :                                 efree(error);
    2670           1 :                                 goto valid_alias;
    2671             :                         }
    2672           3 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    2673           3 :                         efree(error);
    2674           3 :                         RETURN_FALSE;
    2675             :                 }
    2676          44 :                 if (!phar_validate_alias(alias, alias_len)) {
    2677           4 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2678           4 :                                 "Invalid alias \"%s\" specified for phar \"%s\"", alias, phar_obj->archive->fname);
    2679           4 :                         RETURN_FALSE;
    2680             :                 }
    2681             : valid_alias:
    2682          41 :                 if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    2683           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    2684           0 :                         return;
    2685             :                 }
    2686          82 :                 if (phar_obj->archive->alias_len && NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), phar_obj->archive->alias, phar_obj->archive->alias_len))) {
    2687          23 :                         zend_hash_str_del(&(PHAR_G(phar_alias_map)), phar_obj->archive->alias, phar_obj->archive->alias_len);
    2688          23 :                         readd = 1;
    2689             :                 }
    2690             : 
    2691          41 :                 oldalias = phar_obj->archive->alias;
    2692          41 :                 oldalias_len = phar_obj->archive->alias_len;
    2693          41 :                 old_temp = phar_obj->archive->is_temporary_alias;
    2694             : 
    2695          41 :                 if (alias_len) {
    2696          41 :                         phar_obj->archive->alias = estrndup(alias, alias_len);
    2697             :                 } else {
    2698           0 :                         phar_obj->archive->alias = NULL;
    2699             :                 }
    2700             : 
    2701          41 :                 phar_obj->archive->alias_len = alias_len;
    2702          41 :                 phar_obj->archive->is_temporary_alias = 0;
    2703          41 :                 phar_flush(phar_obj->archive, NULL, 0, 0, &error);
    2704             : 
    2705          41 :                 if (error) {
    2706           0 :                         phar_obj->archive->alias = oldalias;
    2707           0 :                         phar_obj->archive->alias_len = oldalias_len;
    2708           0 :                         phar_obj->archive->is_temporary_alias = old_temp;
    2709           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    2710           0 :                         if (readd) {
    2711           0 :                                 zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), oldalias, oldalias_len, phar_obj->archive);
    2712             :                         }
    2713           0 :                         efree(error);
    2714           0 :                         RETURN_FALSE;
    2715             :                 }
    2716             : 
    2717          41 :                 zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len, phar_obj->archive);
    2718             : 
    2719          41 :                 if (oldalias) {
    2720          41 :                         efree(oldalias);
    2721             :                 }
    2722             : 
    2723          41 :                 RETURN_TRUE;
    2724             :         }
    2725             : 
    2726           1 :         RETURN_FALSE;
    2727             : }
    2728             : /* }}} */
    2729             : 
    2730             : /* {{{ proto string Phar::getVersion()
    2731             :  * Return version info of Phar archive
    2732             :  */
    2733           2 : PHP_METHOD(Phar, getVersion)
    2734             : {
    2735           4 :         PHAR_ARCHIVE_OBJECT();
    2736             : 
    2737           1 :         if (zend_parse_parameters_none() == FAILURE) {
    2738           0 :                 return;
    2739             :         }
    2740             : 
    2741           2 :         RETURN_STRING(phar_obj->archive->version);
    2742             : }
    2743             : /* }}} */
    2744             : 
    2745             : /* {{{ proto void Phar::startBuffering()
    2746             :  * Do not flush a writeable phar (save its contents) until explicitly requested
    2747             :  */
    2748           9 : PHP_METHOD(Phar, startBuffering)
    2749             : {
    2750          18 :         PHAR_ARCHIVE_OBJECT();
    2751             : 
    2752           9 :         if (zend_parse_parameters_none() == FAILURE) {
    2753           0 :                 return;
    2754             :         }
    2755             : 
    2756           9 :         phar_obj->archive->donotflush = 1;
    2757             : }
    2758             : /* }}} */
    2759             : 
    2760             : /* {{{ proto bool Phar::isBuffering()
    2761             :  * Returns whether write operations are flushing to disk immediately.
    2762             :  */
    2763           9 : PHP_METHOD(Phar, isBuffering)
    2764             : {
    2765          18 :         PHAR_ARCHIVE_OBJECT();
    2766             : 
    2767           9 :         if (zend_parse_parameters_none() == FAILURE) {
    2768           0 :                 return;
    2769             :         }
    2770             : 
    2771           9 :         RETURN_BOOL(phar_obj->archive->donotflush);
    2772             : }
    2773             : /* }}} */
    2774             : 
    2775             : /* {{{ proto bool Phar::stopBuffering()
    2776             :  * Saves the contents of a modified archive to disk.
    2777             :  */
    2778          69 : PHP_METHOD(Phar, stopBuffering)
    2779             : {
    2780             :         char *error;
    2781             : 
    2782         138 :         PHAR_ARCHIVE_OBJECT();
    2783             : 
    2784          69 :         if (zend_parse_parameters_none() == FAILURE) {
    2785           0 :                 return;
    2786             :         }
    2787             : 
    2788          69 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    2789           0 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2790             :                         "Cannot write out phar archive, phar is read-only");
    2791           0 :                 return;
    2792             :         }
    2793             : 
    2794          69 :         phar_obj->archive->donotflush = 0;
    2795          69 :         phar_flush(phar_obj->archive, 0, 0, 0, &error);
    2796             : 
    2797          69 :         if (error) {
    2798           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    2799           0 :                 efree(error);
    2800             :         }
    2801             : }
    2802             : /* }}} */
    2803             : 
    2804             : /* {{{ proto bool Phar::setStub(string|stream stub [, int len])
    2805             :  * Change the stub in a phar, phar.tar or phar.zip archive to something other
    2806             :  * than the default. The stub *must* end with a call to __HALT_COMPILER().
    2807             :  */
    2808          87 : PHP_METHOD(Phar, setStub)
    2809             : {
    2810             :         zval *zstub;
    2811             :         char *stub, *error;
    2812             :         size_t stub_len;
    2813          87 :         zend_long len = -1;
    2814             :         php_stream *stream;
    2815         174 :         PHAR_ARCHIVE_OBJECT();
    2816             : 
    2817          87 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    2818           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2819             :                         "Cannot change stub, phar is read-only");
    2820           1 :                 return;
    2821             :         }
    2822             : 
    2823          86 :         if (phar_obj->archive->is_data) {
    2824           3 :                 if (phar_obj->archive->is_tar) {
    2825           2 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2826             :                                 "A Phar stub cannot be set in a plain tar archive");
    2827             :                 } else {
    2828           1 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2829             :                                 "A Phar stub cannot be set in a plain zip archive");
    2830             :                 }
    2831           3 :                 return;
    2832             :         }
    2833             : 
    2834          83 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "r|l", &zstub, &len) == SUCCESS) {
    2835          10 :                 if ((php_stream_from_zval_no_verify(stream, zstub)) != NULL) {
    2836          10 :                         if (len > 0) {
    2837           4 :                                 len = -len;
    2838             :                         } else {
    2839           6 :                                 len = -1;
    2840             :                         }
    2841          10 :                         if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    2842           0 :                                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    2843           0 :                                 return;
    2844             :                         }
    2845          10 :                         phar_flush(phar_obj->archive, (char *) zstub, len, 0, &error);
    2846          10 :                         if (error) {
    2847           0 :                                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    2848           0 :                                 efree(error);
    2849             :                         }
    2850          10 :                         RETURN_TRUE;
    2851             :                 } else {
    2852           0 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2853             :                                 "Cannot change stub, unable to read from input stream");
    2854             :                 }
    2855          73 :         } else if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &stub, &stub_len) == SUCCESS) {
    2856          72 :                 if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    2857           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    2858           0 :                         return;
    2859             :                 }
    2860          72 :                 phar_flush(phar_obj->archive, stub, stub_len, 0, &error);
    2861             : 
    2862          72 :                 if (error) {
    2863           3 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    2864           3 :                         efree(error);
    2865             :                 }
    2866             : 
    2867          72 :                 RETURN_TRUE;
    2868             :         }
    2869             : 
    2870           1 :         RETURN_FALSE;
    2871             : }
    2872             : /* }}} */
    2873             : 
    2874             : /* {{{ proto bool Phar::setDefaultStub([string index[, string webindex]])
    2875             :  * In a pure phar archive, sets a stub that can be used to run the archive
    2876             :  * regardless of whether the phar extension is available. The first parameter
    2877             :  * is the CLI startup filename, which defaults to "index.php". The second
    2878             :  * parameter is the web startup filename and also defaults to "index.php"
    2879             :  * (falling back to CLI behaviour).
    2880             :  * Both parameters are optional.
    2881             :  * In a phar.zip or phar.tar archive, the default stub is used only to
    2882             :  * identify the archive to the extension as a Phar object. This allows the
    2883             :  * extension to treat phar.zip and phar.tar types as honorary phars. Since
    2884             :  * files cannot be loaded via this kind of stub, no parameters are accepted
    2885             :  * when the Phar object is zip- or tar-based.
    2886             :  */
    2887          19 : PHP_METHOD(Phar, setDefaultStub)
    2888             : {
    2889          19 :         char *index = NULL, *webindex = NULL, *error = NULL;
    2890          19 :         zend_string *stub = NULL;
    2891          19 :         size_t index_len = 0, webindex_len = 0;
    2892          19 :         int created_stub = 0;
    2893          38 :         PHAR_ARCHIVE_OBJECT();
    2894             : 
    2895          19 :         if (phar_obj->archive->is_data) {
    2896           3 :                 if (phar_obj->archive->is_tar) {
    2897           2 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2898             :                                 "A Phar stub cannot be set in a plain tar archive");
    2899             :                 } else {
    2900           1 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2901             :                                 "A Phar stub cannot be set in a plain zip archive");
    2902             :                 }
    2903           3 :                 return;
    2904             :         }
    2905             : 
    2906          16 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s", &index, &index_len, &webindex, &webindex_len) == FAILURE) {
    2907           1 :                 RETURN_FALSE;
    2908             :         }
    2909             : 
    2910          15 :         if (ZEND_NUM_ARGS() > 0 && (phar_obj->archive->is_tar || phar_obj->archive->is_zip)) {
    2911           4 :                 php_error_docref(NULL, E_WARNING, "method accepts no arguments for a tar- or zip-based phar stub, %d given", ZEND_NUM_ARGS());
    2912           4 :                 RETURN_FALSE;
    2913             :         }
    2914             : 
    2915          11 :         if (PHAR_G(readonly)) {
    2916           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2917             :                         "Cannot change stub: phar.readonly=1");
    2918           1 :                 RETURN_FALSE;
    2919             :         }
    2920             : 
    2921          10 :         if (!phar_obj->archive->is_tar && !phar_obj->archive->is_zip) {
    2922           8 :                 stub = phar_create_default_stub(index, webindex, &error);
    2923             : 
    2924           8 :                 if (error) {
    2925           1 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "%s", error);
    2926           1 :                         efree(error);
    2927           1 :                         if (stub) {
    2928             :                                 zend_string_free(stub);
    2929             :                         }
    2930           1 :                         RETURN_FALSE;
    2931             :                 }
    2932             : 
    2933           7 :                 created_stub = 1;
    2934             :         }
    2935             : 
    2936           9 :         if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    2937           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    2938           0 :                 return;
    2939             :         }
    2940           9 :         phar_flush(phar_obj->archive, stub ? ZSTR_VAL(stub) : 0, stub ? ZSTR_LEN(stub) : 0, 1, &error);
    2941             : 
    2942           9 :         if (created_stub) {
    2943             :                 zend_string_free(stub);
    2944             :         }
    2945             : 
    2946           9 :         if (error) {
    2947           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    2948           0 :                 efree(error);
    2949           0 :                 RETURN_FALSE;
    2950             :         }
    2951             : 
    2952           9 :         RETURN_TRUE;
    2953             : }
    2954             : /* }}} */
    2955             : 
    2956             : /* {{{ proto array Phar::setSignatureAlgorithm(int sigtype[, string privatekey])
    2957             :  * Sets the signature algorithm for a phar and applies it. The signature
    2958             :  * algorithm must be one of Phar::MD5, Phar::SHA1, Phar::SHA256,
    2959             :  * Phar::SHA512, or Phar::OPENSSL. Note that zip- based phar archives
    2960             :  * cannot support signatures.
    2961             :  */
    2962          19 : PHP_METHOD(Phar, setSignatureAlgorithm)
    2963             : {
    2964             :         zend_long algo;
    2965          19 :         char *error, *key = NULL;
    2966          19 :         size_t key_len = 0;
    2967             : 
    2968          38 :         PHAR_ARCHIVE_OBJECT();
    2969             : 
    2970          19 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    2971           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2972             :                         "Cannot set signature algorithm, phar is read-only");
    2973           1 :                 return;
    2974             :         }
    2975             : 
    2976          18 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "l|s", &algo, &key, &key_len) != SUCCESS) {
    2977           0 :                 return;
    2978             :         }
    2979             : 
    2980          18 :         switch (algo) {
    2981             :                 case PHAR_SIG_SHA256:
    2982             :                 case PHAR_SIG_SHA512:
    2983             : #ifndef PHAR_HASH_OK
    2984             :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2985             :                                 "SHA-256 and SHA-512 signatures are only supported if the hash extension is enabled and built non-shared");
    2986             :                         return;
    2987             : #endif
    2988             :                 case PHAR_SIG_MD5:
    2989             :                 case PHAR_SIG_SHA1:
    2990             :                 case PHAR_SIG_OPENSSL:
    2991          18 :                         if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    2992           0 :                                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    2993           0 :                                 return;
    2994             :                         }
    2995          18 :                         phar_obj->archive->sig_flags = algo;
    2996          18 :                         phar_obj->archive->is_modified = 1;
    2997          18 :                         PHAR_G(openssl_privatekey) = key;
    2998          18 :                         PHAR_G(openssl_privatekey_len) = key_len;
    2999             : 
    3000          18 :                         phar_flush(phar_obj->archive, 0, 0, 0, &error);
    3001          18 :                         if (error) {
    3002           0 :                                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    3003           0 :                                 efree(error);
    3004             :                         }
    3005          18 :                         break;
    3006             :                 default:
    3007           0 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3008             :                                 "Unknown signature algorithm specified");
    3009             :         }
    3010             : }
    3011             : /* }}} */
    3012             : 
    3013             : /* {{{ proto array|false Phar::getSignature()
    3014             :  * Returns a hash signature, or FALSE if the archive is unsigned.
    3015             :  */
    3016          31 : PHP_METHOD(Phar, getSignature)
    3017             : {
    3018          62 :         PHAR_ARCHIVE_OBJECT();
    3019             : 
    3020          31 :         if (zend_parse_parameters_none() == FAILURE) {
    3021           0 :                 return;
    3022             :         }
    3023             : 
    3024          31 :         if (phar_obj->archive->signature) {
    3025             :                 zend_string *unknown;
    3026             : 
    3027          29 :                 array_init(return_value);
    3028          29 :                 add_assoc_stringl(return_value, "hash", phar_obj->archive->signature, phar_obj->archive->sig_len);
    3029          29 :                 switch(phar_obj->archive->sig_flags) {
    3030             :                         case PHAR_SIG_MD5:
    3031           6 :                                 add_assoc_stringl(return_value, "hash_type", "MD5", 3);
    3032           6 :                                 break;
    3033             :                         case PHAR_SIG_SHA1:
    3034          11 :                                 add_assoc_stringl(return_value, "hash_type", "SHA-1", 5);
    3035          11 :                                 break;
    3036             :                         case PHAR_SIG_SHA256:
    3037           4 :                                 add_assoc_stringl(return_value, "hash_type", "SHA-256", 7);
    3038           4 :                                 break;
    3039             :                         case PHAR_SIG_SHA512:
    3040           4 :                                 add_assoc_stringl(return_value, "hash_type", "SHA-512", 7);
    3041           4 :                                 break;
    3042             :                         case PHAR_SIG_OPENSSL:
    3043           4 :                                 add_assoc_stringl(return_value, "hash_type", "OpenSSL", 7);
    3044           4 :                                 break;
    3045             :                         default:
    3046           0 :                                 unknown = strpprintf(0, "Unknown (%u)", phar_obj->archive->sig_flags);
    3047           0 :                                 add_assoc_str(return_value, "hash_type", unknown);
    3048             :                                 break;
    3049             :                 }
    3050             :         } else {
    3051           2 :                 RETURN_FALSE;
    3052             :         }
    3053             : }
    3054             : /* }}} */
    3055             : 
    3056             : /* {{{ proto bool Phar::getModified()
    3057             :  * Return whether phar was modified
    3058             :  */
    3059           2 : PHP_METHOD(Phar, getModified)
    3060             : {
    3061           4 :         PHAR_ARCHIVE_OBJECT();
    3062             : 
    3063           2 :         if (zend_parse_parameters_none() == FAILURE) {
    3064           0 :                 return;
    3065             :         }
    3066             : 
    3067           2 :         RETURN_BOOL(phar_obj->archive->is_modified);
    3068             : }
    3069             : /* }}} */
    3070             : 
    3071          40 : static int phar_set_compression(zval *zv, void *argument) /* {{{ */
    3072             : {
    3073          40 :         phar_entry_info *entry = (phar_entry_info *)Z_PTR_P(zv);
    3074          40 :         uint32_t compress = *(uint32_t *)argument;
    3075             : 
    3076          40 :         if (entry->is_deleted) {
    3077           0 :                 return ZEND_HASH_APPLY_KEEP;
    3078             :         }
    3079             : 
    3080          40 :         entry->old_flags = entry->flags;
    3081          40 :         entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
    3082          40 :         entry->flags |= compress;
    3083          40 :         entry->is_modified = 1;
    3084          40 :         return ZEND_HASH_APPLY_KEEP;
    3085             : }
    3086             : /* }}} */
    3087             : 
    3088          40 : static int phar_test_compression(zval *zv, void *argument) /* {{{ */
    3089             : {
    3090          40 :         phar_entry_info *entry = (phar_entry_info *)Z_PTR_P(zv);
    3091             : 
    3092          40 :         if (entry->is_deleted) {
    3093           0 :                 return ZEND_HASH_APPLY_KEEP;
    3094             :         }
    3095             : 
    3096          40 :         if (!PHAR_G(has_bz2)) {
    3097           0 :                 if (entry->flags & PHAR_ENT_COMPRESSED_BZ2) {
    3098           0 :                         *(int *) argument = 0;
    3099             :                 }
    3100             :         }
    3101             : 
    3102          40 :         if (!PHAR_G(has_zlib)) {
    3103           0 :                 if (entry->flags & PHAR_ENT_COMPRESSED_GZ) {
    3104           0 :                         *(int *) argument = 0;
    3105             :                 }
    3106             :         }
    3107             : 
    3108          40 :         return ZEND_HASH_APPLY_KEEP;
    3109             : }
    3110             : /* }}} */
    3111             : 
    3112          14 : static void pharobj_set_compression(HashTable *manifest, uint32_t compress) /* {{{ */
    3113             : {
    3114          14 :         zend_hash_apply_with_argument(manifest, phar_set_compression, &compress);
    3115          14 : }
    3116             : /* }}} */
    3117             : 
    3118          14 : static int pharobj_cancompress(HashTable *manifest) /* {{{ */
    3119             : {
    3120             :         int test;
    3121             : 
    3122          14 :         test = 1;
    3123          14 :         zend_hash_apply_with_argument(manifest, phar_test_compression, &test);
    3124          14 :         return test;
    3125             : }
    3126             : /* }}} */
    3127             : 
    3128             : /* {{{ proto object Phar::compress(int method[, string extension])
    3129             :  * Compress a .tar, or .phar.tar with whole-file compression
    3130             :  * The parameter can be one of Phar::GZ or Phar::BZ2 to specify
    3131             :  * the kind of compression desired
    3132             :  */
    3133           6 : PHP_METHOD(Phar, compress)
    3134             : {
    3135             :         zend_long method;
    3136           6 :         char *ext = NULL;
    3137           6 :         size_t ext_len = 0;
    3138             :         uint32_t flags;
    3139             :         zend_object *ret;
    3140          12 :         PHAR_ARCHIVE_OBJECT();
    3141             : 
    3142           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|s", &method, &ext, &ext_len) == FAILURE) {
    3143           1 :                 return;
    3144             :         }
    3145             : 
    3146           5 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    3147           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3148             :                         "Cannot compress phar archive, phar is read-only");
    3149           1 :                 return;
    3150             :         }
    3151             : 
    3152           4 :         if (phar_obj->archive->is_zip) {
    3153           0 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3154             :                         "Cannot compress zip-based archives with whole-archive compression");
    3155           0 :                 return;
    3156             :         }
    3157             : 
    3158           4 :         switch (method) {
    3159             :                 case 0:
    3160           1 :                         flags = PHAR_FILE_COMPRESSED_NONE;
    3161           1 :                         break;
    3162             :                 case PHAR_ENT_COMPRESSED_GZ:
    3163           2 :                         if (!PHAR_G(has_zlib)) {
    3164           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3165             :                                         "Cannot compress entire archive with gzip, enable ext/zlib in php.ini");
    3166           0 :                                 return;
    3167             :                         }
    3168           2 :                         flags = PHAR_FILE_COMPRESSED_GZ;
    3169           2 :                         break;
    3170             : 
    3171             :                 case PHAR_ENT_COMPRESSED_BZ2:
    3172           1 :                         if (!PHAR_G(has_bz2)) {
    3173           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3174             :                                         "Cannot compress entire archive with bz2, enable ext/bz2 in php.ini");
    3175           0 :                                 return;
    3176             :                         }
    3177           1 :                         flags = PHAR_FILE_COMPRESSED_BZ2;
    3178           1 :                         break;
    3179             :                 default:
    3180           0 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3181             :                                 "Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2");
    3182           0 :                         return;
    3183             :         }
    3184             : 
    3185           4 :         if (phar_obj->archive->is_tar) {
    3186           2 :                 ret = phar_convert_to_other(phar_obj->archive, PHAR_FORMAT_TAR, ext, flags);
    3187             :         } else {
    3188           2 :                 ret = phar_convert_to_other(phar_obj->archive, PHAR_FORMAT_PHAR, ext, flags);
    3189             :         }
    3190             : 
    3191           4 :         if (ret) {
    3192           4 :                 ZVAL_OBJ(return_value, ret);
    3193             :         } else {
    3194           0 :                 RETURN_NULL();
    3195             :         }
    3196             : }
    3197             : /* }}} */
    3198             : 
    3199             : /* {{{ proto object Phar::decompress([string extension])
    3200             :  * Decompress a .tar, or .phar.tar with whole-file compression
    3201             :  */
    3202           6 : PHP_METHOD(Phar, decompress)
    3203             : {
    3204           6 :         char *ext = NULL;
    3205           6 :         size_t ext_len = 0;
    3206             :         zend_object *ret;
    3207          12 :         PHAR_ARCHIVE_OBJECT();
    3208             : 
    3209           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &ext, &ext_len) == FAILURE) {
    3210           1 :                 return;
    3211             :         }
    3212             : 
    3213           5 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    3214           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3215             :                         "Cannot decompress phar archive, phar is read-only");
    3216           1 :                 return;
    3217             :         }
    3218             : 
    3219           4 :         if (phar_obj->archive->is_zip) {
    3220           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3221             :                         "Cannot decompress zip-based archives with whole-archive compression");
    3222           1 :                 return;
    3223             :         }
    3224             : 
    3225           3 :         if (phar_obj->archive->is_tar) {
    3226           1 :                 ret = phar_convert_to_other(phar_obj->archive, PHAR_FORMAT_TAR, ext, PHAR_FILE_COMPRESSED_NONE);
    3227             :         } else {
    3228           2 :                 ret = phar_convert_to_other(phar_obj->archive, PHAR_FORMAT_PHAR, ext, PHAR_FILE_COMPRESSED_NONE);
    3229             :         }
    3230             : 
    3231           3 :         if (ret) {
    3232           3 :                 ZVAL_OBJ(return_value, ret);
    3233             :         } else {
    3234           0 :                 RETURN_NULL();
    3235             :         }
    3236             : }
    3237             : /* }}} */
    3238             : 
    3239             : /* {{{ proto object Phar::compressFiles(int method)
    3240             :  * Compress all files within a phar or zip archive using the specified compression
    3241             :  * The parameter can be one of Phar::GZ or Phar::BZ2 to specify
    3242             :  * the kind of compression desired
    3243             :  */
    3244          13 : PHP_METHOD(Phar, compressFiles)
    3245             : {
    3246             :         char *error;
    3247             :         uint32_t flags;
    3248             :         zend_long method;
    3249          26 :         PHAR_ARCHIVE_OBJECT();
    3250             : 
    3251          13 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &method) == FAILURE) {
    3252           1 :                 return;
    3253             :         }
    3254             : 
    3255          12 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    3256           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3257             :                         "Phar is readonly, cannot change compression");
    3258           0 :                 return;
    3259             :         }
    3260             : 
    3261          12 :         switch (method) {
    3262             :                 case PHAR_ENT_COMPRESSED_GZ:
    3263           8 :                         if (!PHAR_G(has_zlib)) {
    3264           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3265             :                                         "Cannot compress files within archive with gzip, enable ext/zlib in php.ini");
    3266           0 :                                 return;
    3267             :                         }
    3268           8 :                         flags = PHAR_ENT_COMPRESSED_GZ;
    3269           8 :                         break;
    3270             : 
    3271             :                 case PHAR_ENT_COMPRESSED_BZ2:
    3272           3 :                         if (!PHAR_G(has_bz2)) {
    3273           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3274             :                                         "Cannot compress files within archive with bz2, enable ext/bz2 in php.ini");
    3275           0 :                                 return;
    3276             :                         }
    3277           3 :                         flags = PHAR_ENT_COMPRESSED_BZ2;
    3278           3 :                         break;
    3279             :                 default:
    3280           1 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3281             :                                 "Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2");
    3282           1 :                         return;
    3283             :         }
    3284             : 
    3285          11 :         if (phar_obj->archive->is_tar) {
    3286           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3287             :                         "Cannot compress with Gzip compression, tar archives cannot compress individual files, use compress() to compress the whole archive");
    3288           0 :                 return;
    3289             :         }
    3290             : 
    3291          11 :         if (!pharobj_cancompress(&phar_obj->archive->manifest)) {
    3292           0 :                 if (flags == PHAR_FILE_COMPRESSED_GZ) {
    3293           0 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3294             :                                 "Cannot compress all files as Gzip, some are compressed as bzip2 and cannot be decompressed");
    3295             :                 } else {
    3296           0 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3297             :                                 "Cannot compress all files as Bzip2, some are compressed as gzip and cannot be decompressed");
    3298             :                 }
    3299           0 :                 return;
    3300             :         }
    3301             : 
    3302          11 :         if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    3303           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    3304           0 :                 return;
    3305             :         }
    3306          11 :         pharobj_set_compression(&phar_obj->archive->manifest, flags);
    3307          11 :         phar_obj->archive->is_modified = 1;
    3308          11 :         phar_flush(phar_obj->archive, 0, 0, 0, &error);
    3309             : 
    3310          11 :         if (error) {
    3311           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "%s", error);
    3312           0 :                 efree(error);
    3313             :         }
    3314             : }
    3315             : /* }}} */
    3316             : 
    3317             : /* {{{ proto bool Phar::decompressFiles()
    3318             :  * decompress every file
    3319             :  */
    3320           4 : PHP_METHOD(Phar, decompressFiles)
    3321             : {
    3322             :         char *error;
    3323           8 :         PHAR_ARCHIVE_OBJECT();
    3324             : 
    3325           4 :         if (zend_parse_parameters_none() == FAILURE) {
    3326           0 :                 return;
    3327             :         }
    3328             : 
    3329           4 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    3330           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3331             :                         "Phar is readonly, cannot change compression");
    3332           1 :                 return;
    3333             :         }
    3334             : 
    3335           3 :         if (!pharobj_cancompress(&phar_obj->archive->manifest)) {
    3336           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3337             :                         "Cannot decompress all files, some are compressed as bzip2 or gzip and cannot be decompressed");
    3338           0 :                 return;
    3339             :         }
    3340             : 
    3341           3 :         if (phar_obj->archive->is_tar) {
    3342           0 :                 RETURN_TRUE;
    3343             :         } else {
    3344           3 :                 if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    3345           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    3346           0 :                         return;
    3347             :                 }
    3348           3 :                 pharobj_set_compression(&phar_obj->archive->manifest, PHAR_ENT_COMPRESSED_NONE);
    3349             :         }
    3350             : 
    3351           3 :         phar_obj->archive->is_modified = 1;
    3352           3 :         phar_flush(phar_obj->archive, 0, 0, 0, &error);
    3353             : 
    3354           3 :         if (error) {
    3355           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "%s", error);
    3356           0 :                 efree(error);
    3357             :         }
    3358             : 
    3359           3 :         RETURN_TRUE;
    3360             : }
    3361             : /* }}} */
    3362             : 
    3363             : /* {{{ proto bool Phar::copy(string oldfile, string newfile)
    3364             :  * copy a file internal to the phar archive to another new file within the phar
    3365             :  */
    3366          17 : PHP_METHOD(Phar, copy)
    3367             : {
    3368             :         char *oldfile, *newfile, *error;
    3369             :         const char *pcr_error;
    3370             :         size_t oldfile_len, newfile_len;
    3371          17 :         phar_entry_info *oldentry, newentry = {0}, *temp;
    3372          17 :         int tmp_len = 0;
    3373             : 
    3374          34 :         PHAR_ARCHIVE_OBJECT();
    3375             : 
    3376          17 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "pp", &oldfile, &oldfile_len, &newfile, &newfile_len) == FAILURE) {
    3377           1 :                 return;
    3378             :         }
    3379             : 
    3380          16 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    3381           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3382             :                         "Cannot copy \"%s\" to \"%s\", phar is read-only", oldfile, newfile);
    3383           1 :                 RETURN_FALSE;
    3384             :         }
    3385             : 
    3386          15 :         if (oldfile_len >= sizeof(".phar")-1 && !memcmp(oldfile, ".phar", sizeof(".phar")-1)) {
    3387             :                 /* can't copy a meta file */
    3388           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3389           1 :                         "file \"%s\" cannot be copied to file \"%s\", cannot copy Phar meta-file in %s", oldfile, newfile, phar_obj->archive->fname);
    3390           1 :                 RETURN_FALSE;
    3391             :         }
    3392             : 
    3393          14 :         if (newfile_len >= sizeof(".phar")-1 && !memcmp(newfile, ".phar", sizeof(".phar")-1)) {
    3394             :                 /* can't copy a meta file */
    3395           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3396           1 :                         "file \"%s\" cannot be copied to file \"%s\", cannot copy to Phar meta-file in %s", oldfile, newfile, phar_obj->archive->fname);
    3397           1 :                 RETURN_FALSE;
    3398             :         }
    3399             : 
    3400          25 :         if (!zend_hash_str_exists(&phar_obj->archive->manifest, oldfile, (uint) oldfile_len) || NULL == (oldentry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, oldfile, (uint) oldfile_len)) || oldentry->is_deleted) {
    3401           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3402           1 :                         "file \"%s\" cannot be copied to file \"%s\", file does not exist in %s", oldfile, newfile, phar_obj->archive->fname);
    3403           1 :                 RETURN_FALSE;
    3404             :         }
    3405             : 
    3406          12 :         if (zend_hash_str_exists(&phar_obj->archive->manifest, newfile, (uint) newfile_len)) {
    3407           2 :                 if (NULL != (temp = zend_hash_str_find_ptr(&phar_obj->archive->manifest, newfile, (uint) newfile_len)) || !temp->is_deleted) {
    3408           1 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3409           1 :                                 "file \"%s\" cannot be copied to file \"%s\", file must not already exist in phar %s", oldfile, newfile, phar_obj->archive->fname);
    3410           1 :                         RETURN_FALSE;
    3411             :                 }
    3412             :         }
    3413             : 
    3414          11 :         tmp_len = (int)newfile_len;
    3415          11 :         if (phar_path_check(&newfile, &tmp_len, &pcr_error) > pcr_is_ok) {
    3416           3 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3417           3 :                                 "file \"%s\" contains invalid characters %s, cannot be copied from \"%s\" in phar %s", newfile, pcr_error, oldfile, phar_obj->archive->fname);
    3418           3 :                 RETURN_FALSE;
    3419             :         }
    3420           8 :         newfile_len = tmp_len;
    3421             : 
    3422           8 :         if (phar_obj->archive->is_persistent) {
    3423           0 :                 if (FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    3424           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    3425           0 :                         return;
    3426             :                 }
    3427             :                 /* re-populate with copied-on-write entry */
    3428           0 :                 oldentry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, oldfile, (uint) oldfile_len);
    3429             :         }
    3430             : 
    3431           8 :         memcpy((void *) &newentry, oldentry, sizeof(phar_entry_info));
    3432             : 
    3433           8 :         if (Z_TYPE(newentry.metadata) != IS_UNDEF) {
    3434             :                 zval_copy_ctor(&newentry.metadata);
    3435           1 :                 newentry.metadata_str.s = NULL;
    3436             :         }
    3437             : 
    3438           8 :         newentry.filename = estrndup(newfile, newfile_len);
    3439           8 :         newentry.filename_len = newfile_len;
    3440           8 :         newentry.fp_refcount = 0;
    3441             : 
    3442           8 :         if (oldentry->fp_type != PHAR_FP) {
    3443           1 :                 if (FAILURE == phar_copy_entry_fp(oldentry, &newentry, &error)) {
    3444           0 :                         efree(newentry.filename);
    3445           0 :                         php_stream_close(newentry.fp);
    3446           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    3447           0 :                         efree(error);
    3448           0 :                         return;
    3449             :                 }
    3450             :         }
    3451             : 
    3452           8 :         zend_hash_str_add_mem(&oldentry->phar->manifest, newfile, newfile_len, &newentry, sizeof(phar_entry_info));
    3453           8 :         phar_obj->archive->is_modified = 1;
    3454           8 :         phar_flush(phar_obj->archive, 0, 0, 0, &error);
    3455             : 
    3456           8 :         if (error) {
    3457           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    3458           0 :                 efree(error);
    3459             :         }
    3460             : 
    3461           8 :         RETURN_TRUE;
    3462             : }
    3463             : /* }}} */
    3464             : 
    3465             : /* {{{ proto int Phar::offsetExists(string entry)
    3466             :  * determines whether a file exists in the phar
    3467             :  */
    3468          34 : PHP_METHOD(Phar, offsetExists)
    3469             : {
    3470             :         char *fname;
    3471             :         size_t fname_len;
    3472             :         phar_entry_info *entry;
    3473             : 
    3474          68 :         PHAR_ARCHIVE_OBJECT();
    3475             : 
    3476          34 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) {
    3477           1 :                 return;
    3478             :         }
    3479             : 
    3480          33 :         if (zend_hash_str_exists(&phar_obj->archive->manifest, fname, (uint) fname_len)) {
    3481          38 :                 if (NULL != (entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, (uint) fname_len))) {
    3482          19 :                         if (entry->is_deleted) {
    3483             :                                 /* entry is deleted, but has not been flushed to disk yet */
    3484           0 :                                 RETURN_FALSE;
    3485             :                         }
    3486             :                 }
    3487             : 
    3488          19 :                 if (fname_len >= sizeof(".phar")-1 && !memcmp(fname, ".phar", sizeof(".phar")-1)) {
    3489             :                         /* none of these are real files, so they don't exist */
    3490           2 :                         RETURN_FALSE;
    3491             :                 }
    3492          17 :                 RETURN_TRUE;
    3493             :         } else {
    3494          14 :                 if (zend_hash_str_exists(&phar_obj->archive->virtual_dirs, fname, (uint) fname_len)) {
    3495           3 :                         RETURN_TRUE;
    3496             :                 }
    3497          11 :                 RETURN_FALSE;
    3498             :         }
    3499             : }
    3500             : /* }}} */
    3501             : 
    3502             : /* {{{ proto int Phar::offsetGet(string entry)
    3503             :  * get a PharFileInfo object for a specific file
    3504             :  */
    3505         333 : PHP_METHOD(Phar, offsetGet)
    3506             : {
    3507             :         char *fname, *error;
    3508             :         size_t fname_len;
    3509             :         zval zfname;
    3510             :         phar_entry_info *entry;
    3511             :         zend_string *sfname;
    3512         666 :         PHAR_ARCHIVE_OBJECT();
    3513             : 
    3514         333 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) {
    3515           1 :                 return;
    3516             :         }
    3517             : 
    3518             :         /* security is 0 here so that we can get a better error message than "entry doesn't exist" */
    3519         332 :         if (!(entry = phar_get_entry_info_dir(phar_obj->archive, fname, fname_len, 1, &error, 0))) {
    3520           3 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist%s%s", fname, error?", ":"", error?error:"");
    3521             :         } else {
    3522         329 :                 if (fname_len == sizeof(".phar/stub.php")-1 && !memcmp(fname, ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
    3523           0 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot get stub \".phar/stub.php\" directly in phar \"%s\", use getStub", phar_obj->archive->fname);
    3524           0 :                         return;
    3525             :                 }
    3526             : 
    3527         329 :                 if (fname_len == sizeof(".phar/alias.txt")-1 && !memcmp(fname, ".phar/alias.txt", sizeof(".phar/alias.txt")-1)) {
    3528           0 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot get alias \".phar/alias.txt\" directly in phar \"%s\", use getAlias", phar_obj->archive->fname);
    3529           0 :                         return;
    3530             :                 }
    3531             : 
    3532         329 :                 if (fname_len >= sizeof(".phar")-1 && !memcmp(fname, ".phar", sizeof(".phar")-1)) {
    3533           1 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot directly get any files or directories in magic \".phar\" directory");
    3534           1 :                         return;
    3535             :                 }
    3536             : 
    3537         328 :                 if (entry->is_temp_dir) {
    3538           6 :                         efree(entry->filename);
    3539           6 :                         efree(entry);
    3540             :                 }
    3541             : 
    3542         328 :                 sfname = strpprintf(0, "phar://%s/%s", phar_obj->archive->fname, fname);
    3543         328 :                 ZVAL_NEW_STR(&zfname, sfname);
    3544         328 :                 spl_instantiate_arg_ex1(phar_obj->spl.info_class, return_value, &zfname);
    3545         328 :                 zval_ptr_dtor(&zfname);
    3546             :         }
    3547             : }
    3548             : /* }}} */
    3549             : 
    3550             : /* {{{ add a file within the phar archive from a string or resource
    3551             :  */
    3552        4466 : static void phar_add_file(phar_archive_data **pphar, char *filename, int filename_len, char *cont_str, size_t cont_len, zval *zresource)
    3553             : {
    3554             :         char *error;
    3555             :         size_t contents_len;
    3556             :         phar_entry_data *data;
    3557             :         php_stream *contents_file;
    3558             : 
    3559        4466 :         if (filename_len >= (int)sizeof(".phar")-1 && !memcmp(filename, ".phar", sizeof(".phar")-1) && (filename[5] == '/' || filename[5] == '\\' || filename[5] == '\0')) {
    3560           6 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot create any files in magic \".phar\" directory");
    3561           6 :                 return;
    3562             :         }
    3563             : 
    3564        4460 :         if (!(data = phar_get_or_create_entry_data((*pphar)->fname, (*pphar)->fname_len, filename, filename_len, "w+b", 0, &error, 1))) {
    3565           3 :                 if (error) {
    3566           3 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist and cannot be created: %s", filename, error);
    3567           3 :                         efree(error);
    3568             :                 } else {
    3569           0 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist and cannot be created", filename);
    3570             :                 }
    3571           3 :                 return;
    3572             :         } else {
    3573        4457 :                 if (error) {
    3574           0 :                         efree(error);
    3575             :                 }
    3576             : 
    3577        4457 :                 if (!data->internal_file->is_dir) {
    3578        4457 :                         if (cont_str) {
    3579         359 :                                 contents_len = php_stream_write(data->fp, cont_str, cont_len);
    3580         359 :                                 if (contents_len != cont_len) {
    3581           0 :                                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s could not be written to", filename);
    3582           0 :                                         return;
    3583             :                                 }
    3584             :                         } else {
    3585        4098 :                                 if (!(php_stream_from_zval_no_verify(contents_file, zresource))) {
    3586           0 :                                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s could not be written to", filename);
    3587           0 :                                         return;
    3588             :                                 }
    3589        4098 :                                 php_stream_copy_to_stream_ex(contents_file, data->fp, PHP_STREAM_COPY_ALL, &contents_len);
    3590             :                         }
    3591             : 
    3592        4457 :                         data->internal_file->compressed_filesize = data->internal_file->uncompressed_filesize = contents_len;
    3593             :                 }
    3594             : 
    3595             :                 /* check for copy-on-write */
    3596        4457 :                 if (pphar[0] != data->phar) {
    3597           0 :                         *pphar = data->phar;
    3598             :                 }
    3599        4457 :                 phar_entry_delref(data);
    3600        4457 :                 phar_flush(*pphar, 0, 0, 0, &error);
    3601             : 
    3602        4457 :                 if (error) {
    3603           4 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    3604           4 :                         efree(error);
    3605             :                 }
    3606             :         }
    3607             : }
    3608             : /* }}} */
    3609             : 
    3610             : /* {{{ create a directory within the phar archive
    3611             :  */
    3612          18 : static void phar_mkdir(phar_archive_data **pphar, char *dirname, int dirname_len)
    3613             : {
    3614             :         char *error;
    3615             :         phar_entry_data *data;
    3616             : 
    3617          18 :         if (!(data = phar_get_or_create_entry_data((*pphar)->fname, (*pphar)->fname_len, dirname, dirname_len, "w+b", 2, &error, 1))) {
    3618           0 :                 if (error) {
    3619           0 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Directory %s does not exist and cannot be created: %s", dirname, error);
    3620           0 :                         efree(error);
    3621             :                 } else {
    3622           0 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Directory %s does not exist and cannot be created", dirname);
    3623             :                 }
    3624             : 
    3625           0 :                 return;
    3626             :         } else {
    3627          18 :                 if (error) {
    3628           0 :                         efree(error);
    3629             :                 }
    3630             : 
    3631             :                 /* check for copy on write */
    3632          18 :                 if (data->phar != *pphar) {
    3633           0 :                         *pphar = data->phar;
    3634             :                 }
    3635          18 :                 phar_entry_delref(data);
    3636          18 :                 phar_flush(*pphar, 0, 0, 0, &error);
    3637             : 
    3638          18 :                 if (error) {
    3639           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    3640           0 :                         efree(error);
    3641             :                 }
    3642             :         }
    3643             : }
    3644             : /* }}} */
    3645             : 
    3646             : /* {{{ proto int Phar::offsetSet(string entry, string value)
    3647             :  * set the contents of an internal file to those of an external file
    3648             :  */
    3649         255 : PHP_METHOD(Phar, offsetSet)
    3650             : {
    3651         255 :         char *fname, *cont_str = NULL;
    3652             :         size_t fname_len, cont_len;
    3653             :         zval *zresource;
    3654         510 :         PHAR_ARCHIVE_OBJECT();
    3655             : 
    3656         255 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    3657           3 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Write operations disabled by the php.ini setting phar.readonly");
    3658           3 :                 return;
    3659             :         }
    3660             : 
    3661         756 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "pr", &fname, &fname_len, &zresource) == FAILURE
    3662         504 :         && zend_parse_parameters(ZEND_NUM_ARGS(), "ps", &fname, &fname_len, &cont_str, &cont_len) == FAILURE) {
    3663           2 :                 return;
    3664             :         }
    3665             : 
    3666         250 :         if (fname_len == sizeof(".phar/stub.php")-1 && !memcmp(fname, ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
    3667           3 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot set stub \".phar/stub.php\" directly in phar \"%s\", use setStub", phar_obj->archive->fname);
    3668           3 :                 return;
    3669             :         }
    3670             : 
    3671         247 :         if (fname_len == sizeof(".phar/alias.txt")-1 && !memcmp(fname, ".phar/alias.txt", sizeof(".phar/alias.txt")-1)) {
    3672           4 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot set alias \".phar/alias.txt\" directly in phar \"%s\", use setAlias", phar_obj->archive->fname);
    3673           4 :                 return;
    3674             :         }
    3675             : 
    3676         243 :         if (fname_len >= sizeof(".phar")-1 && !memcmp(fname, ".phar", sizeof(".phar")-1)) {
    3677           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot set any files or directories in magic \".phar\" directory");
    3678           1 :                 return;
    3679             :         }
    3680             : 
    3681         242 :         phar_add_file(&(phar_obj->archive), fname, fname_len, cont_str, cont_len, zresource);
    3682             : }
    3683             : /* }}} */
    3684             : 
    3685             : /* {{{ proto int Phar::offsetUnset(string entry)
    3686             :  * remove a file from a phar
    3687             :  */
    3688           7 : PHP_METHOD(Phar, offsetUnset)
    3689             : {
    3690             :         char *fname, *error;
    3691             :         size_t fname_len;
    3692             :         phar_entry_info *entry;
    3693          14 :         PHAR_ARCHIVE_OBJECT();
    3694             : 
    3695           7 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    3696           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Write operations disabled by the php.ini setting phar.readonly");
    3697           1 :                 return;
    3698             :         }
    3699             : 
    3700           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) {
    3701           1 :                 return;
    3702             :         }
    3703             : 
    3704           5 :         if (zend_hash_str_exists(&phar_obj->archive->manifest, fname, (uint) fname_len)) {
    3705           6 :                 if (NULL != (entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, (uint) fname_len))) {
    3706           3 :                         if (entry->is_deleted) {
    3707             :                                 /* entry is deleted, but has not been flushed to disk yet */
    3708           0 :                                 return;
    3709             :                         }
    3710             : 
    3711           3 :                         if (phar_obj->archive->is_persistent) {
    3712           0 :                                 if (FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    3713           0 :                                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    3714           0 :                                         return;
    3715             :                                 }
    3716             :                                 /* re-populate entry after copy on write */
    3717           0 :                                 entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, (uint) fname_len);
    3718             :                         }
    3719           3 :                         entry->is_modified = 0;
    3720           3 :                         entry->is_deleted = 1;
    3721             :                         /* we need to "flush" the stream to save the newly deleted file on disk */
    3722           3 :                         phar_flush(phar_obj->archive, 0, 0, 0, &error);
    3723             : 
    3724           3 :                         if (error) {
    3725           0 :                                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    3726           0 :                                 efree(error);
    3727             :                         }
    3728             : 
    3729           3 :                         RETURN_TRUE;
    3730             :                 }
    3731             :         } else {
    3732           2 :                 RETURN_FALSE;
    3733             :         }
    3734             : }
    3735             : /* }}} */
    3736             : 
    3737             : /* {{{ proto string Phar::addEmptyDir(string dirname)
    3738             :  * Adds an empty directory to the phar archive
    3739             :  */
    3740          20 : PHP_METHOD(Phar, addEmptyDir)
    3741             : {
    3742             :         char *dirname;
    3743             :         size_t dirname_len;
    3744             : 
    3745          40 :         PHAR_ARCHIVE_OBJECT();
    3746             : 
    3747          20 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &dirname, &dirname_len) == FAILURE) {
    3748           1 :                 return;
    3749             :         }
    3750             : 
    3751          19 :         if (dirname_len >= sizeof(".phar")-1 && !memcmp(dirname, ".phar", sizeof(".phar")-1)) {
    3752           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot create a directory in magic \".phar\" directory");
    3753           1 :                 return;
    3754             :         }
    3755             : 
    3756          18 :         phar_mkdir(&phar_obj->archive, dirname, dirname_len);
    3757             : }
    3758             : /* }}} */
    3759             : 
    3760             : /* {{{ proto string Phar::addFile(string filename[, string localname])
    3761             :  * Adds a file to the archive using the filename, or the second parameter as the name within the archive
    3762             :  */
    3763        4104 : PHP_METHOD(Phar, addFile)
    3764             : {
    3765        4104 :         char *fname, *localname = NULL;
    3766        4104 :         size_t fname_len, localname_len = 0;
    3767             :         php_stream *resource;
    3768             :         zval zresource;
    3769             : 
    3770        8208 :         PHAR_ARCHIVE_OBJECT();
    3771             : 
    3772        4104 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|s", &fname, &fname_len, &localname, &localname_len) == FAILURE) {
    3773           1 :                 return;
    3774             :         }
    3775             : 
    3776        4103 :         if (!strstr(fname, "://") && php_check_open_basedir(fname)) {
    3777           0 :                 zend_throw_exception_ex(spl_ce_RuntimeException, 0, "phar error: unable to open file \"%s\" to add to phar archive, open_basedir restrictions prevent this", fname);
    3778           0 :                 return;
    3779             :         }
    3780             : 
    3781        4103 :         if (!(resource = php_stream_open_wrapper(fname, "rb", 0, NULL))) {
    3782           1 :                 zend_throw_exception_ex(spl_ce_RuntimeException, 0, "phar error: unable to open file \"%s\" to add to phar archive", fname);
    3783           1 :                 return;
    3784             :         }
    3785             : 
    3786        4102 :         if (localname) {
    3787        4101 :                 fname = localname;
    3788        4101 :                 fname_len = localname_len;
    3789             :         }
    3790             : 
    3791        4102 :         php_stream_to_zval(resource, &zresource);
    3792        4102 :         phar_add_file(&(phar_obj->archive), fname, fname_len, NULL, 0, &zresource);
    3793        4102 :         zval_ptr_dtor(&zresource);
    3794             : }
    3795             : /* }}} */
    3796             : 
    3797             : /* {{{ proto string Phar::addFromString(string localname, string contents)
    3798             :  * Adds a file to the archive using its contents as a string
    3799             :  */
    3800         124 : PHP_METHOD(Phar, addFromString)
    3801             : {
    3802             :         char *localname, *cont_str;
    3803             :         size_t localname_len, cont_len;
    3804             : 
    3805         248 :         PHAR_ARCHIVE_OBJECT();
    3806             : 
    3807         124 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "ps", &localname, &localname_len, &cont_str, &cont_len) == FAILURE) {
    3808           2 :                 return;
    3809             :         }
    3810             : 
    3811         122 :         phar_add_file(&(phar_obj->archive), localname, localname_len, cont_str, cont_len, NULL);
    3812             : }
    3813             : /* }}} */
    3814             : 
    3815             : /* {{{ proto string Phar::getStub()
    3816             :  * Returns the stub at the head of a phar archive as a string.
    3817             :  */
    3818         137 : PHP_METHOD(Phar, getStub)
    3819             : {
    3820             :         size_t len;
    3821             :         zend_string *buf;
    3822             :         php_stream *fp;
    3823         137 :         php_stream_filter *filter = NULL;
    3824             :         phar_entry_info *stub;
    3825             : 
    3826         274 :         PHAR_ARCHIVE_OBJECT();
    3827             : 
    3828         137 :         if (zend_parse_parameters_none() == FAILURE) {
    3829           0 :                 return;
    3830             :         }
    3831             : 
    3832         137 :         if (phar_obj->archive->is_tar || phar_obj->archive->is_zip) {
    3833             : 
    3834         140 :                 if (NULL != (stub = zend_hash_str_find_ptr(&(phar_obj->archive->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1))) {
    3835         128 :                         if (phar_obj->archive->fp && !phar_obj->archive->is_brandnew && !(stub->flags & PHAR_ENT_COMPRESSION_MASK)) {
    3836          64 :                                 fp = phar_obj->archive->fp;
    3837             :                         } else {
    3838           0 :                                 if (!(fp = php_stream_open_wrapper(phar_obj->archive->fname, "rb", 0, NULL))) {
    3839           0 :                                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "phar error: unable to open phar \"%s\"", phar_obj->archive->fname);
    3840           0 :                                         return;
    3841             :                                 }
    3842           0 :                                 if (stub->flags & PHAR_ENT_COMPRESSION_MASK) {
    3843             :                                         char *filter_name;
    3844             : 
    3845           0 :                                         if ((filter_name = phar_decompress_filter(stub, 0)) != NULL) {
    3846           0 :                                                 filter = php_stream_filter_create(filter_name, NULL, php_stream_is_persistent(fp));
    3847             :                                         } else {
    3848           0 :                                                 filter = NULL;
    3849             :                                         }
    3850           0 :                                         if (!filter) {
    3851           0 :                                                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "phar error: unable to read stub of phar \"%s\" (cannot create %s filter)", phar_obj->archive->fname, phar_decompress_filter(stub, 1));
    3852           0 :                                                 return;
    3853             :                                         }
    3854           0 :                                         php_stream_filter_append(&fp->readfilters, filter);
    3855             :                                 }
    3856             :                         }
    3857             : 
    3858          64 :                         if (!fp)  {
    3859           0 :                                 zend_throw_exception_ex(spl_ce_RuntimeException, 0,
    3860             :                                         "Unable to read stub");
    3861           0 :                                 return;
    3862             :                         }
    3863             : 
    3864          64 :                         php_stream_seek(fp, stub->offset_abs, SEEK_SET);
    3865          64 :                         len = stub->uncompressed_filesize;
    3866          64 :                         goto carry_on;
    3867             :                 } else {
    3868           6 :                         RETURN_EMPTY_STRING();
    3869             :                 }
    3870             :         }
    3871          67 :         len = phar_obj->archive->halt_offset;
    3872             : 
    3873         134 :         if (phar_obj->archive->fp && !phar_obj->archive->is_brandnew) {
    3874          67 :                 fp = phar_obj->archive->fp;
    3875             :         } else {
    3876           0 :                 fp = php_stream_open_wrapper(phar_obj->archive->fname, "rb", 0, NULL);
    3877             :         }
    3878             : 
    3879          67 :         if (!fp)  {
    3880           0 :                 zend_throw_exception_ex(spl_ce_RuntimeException, 0,
    3881             :                         "Unable to read stub");
    3882           0 :                 return;
    3883             :         }
    3884             : 
    3885          67 :         php_stream_rewind(fp);
    3886             : carry_on:
    3887         131 :         buf = zend_string_alloc(len, 0);
    3888             : 
    3889         131 :         if (len != php_stream_read(fp, ZSTR_VAL(buf), len)) {
    3890           0 :                 if (fp != phar_obj->archive->fp) {
    3891           0 :                         php_stream_close(fp);
    3892             :                 }
    3893           0 :                 zend_throw_exception_ex(spl_ce_RuntimeException, 0,
    3894             :                         "Unable to read stub");
    3895             :                 zend_string_release(buf);
    3896           0 :                 return;
    3897             :         }
    3898             : 
    3899         131 :         if (filter) {
    3900           0 :                 php_stream_filter_flush(filter, 1);
    3901           0 :                 php_stream_filter_remove(filter, 1);
    3902             :         }
    3903             : 
    3904         131 :         if (fp != phar_obj->archive->fp) {
    3905           0 :                 php_stream_close(fp);
    3906             :         }
    3907             : 
    3908         131 :         ZSTR_VAL(buf)[len] = '\0';
    3909         131 :         ZSTR_LEN(buf) = len;
    3910         131 :         RETVAL_STR(buf);
    3911             : }
    3912             : /* }}}*/
    3913             : 
    3914             : /* {{{ proto int Phar::hasMetaData()
    3915             :  * Returns TRUE if the phar has global metadata, FALSE otherwise.
    3916             :  */
    3917           1 : PHP_METHOD(Phar, hasMetadata)
    3918             : {
    3919           2 :         PHAR_ARCHIVE_OBJECT();
    3920             : 
    3921           2 :         RETURN_BOOL(Z_TYPE(phar_obj->archive->metadata) != IS_UNDEF);
    3922             : }
    3923             : /* }}} */
    3924             : 
    3925             : /* {{{ proto int Phar::getMetaData()
    3926             :  * Returns the global metadata of the phar
    3927             :  */
    3928          19 : PHP_METHOD(Phar, getMetadata)
    3929             : {
    3930          38 :         PHAR_ARCHIVE_OBJECT();
    3931             : 
    3932          19 :         if (zend_parse_parameters_none() == FAILURE) {
    3933           0 :                 return;
    3934             :         }
    3935             : 
    3936          38 :         if (Z_TYPE(phar_obj->archive->metadata) != IS_UNDEF) {
    3937          13 :                 if (phar_obj->archive->is_persistent) {
    3938           1 :                         char *buf = estrndup((char *) Z_PTR(phar_obj->archive->metadata), phar_obj->archive->metadata_len);
    3939             :                         /* assume success, we would have failed before */
    3940           1 :                         phar_parse_metadata(&buf, return_value, phar_obj->archive->metadata_len);
    3941           1 :                         efree(buf);
    3942             :                 } else {
    3943          12 :                         ZVAL_COPY(return_value, &phar_obj->archive->metadata);
    3944             :                 }
    3945             :         }
    3946             : }
    3947             : /* }}} */
    3948             : 
    3949             : /* {{{ proto int Phar::setMetaData(mixed $metadata)
    3950             :  * Sets the global metadata of the phar
    3951             :  */
    3952          10 : PHP_METHOD(Phar, setMetadata)
    3953             : {
    3954             :         char *error;
    3955             :         zval *metadata;
    3956             : 
    3957          20 :         PHAR_ARCHIVE_OBJECT();
    3958             : 
    3959          10 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    3960           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Write operations disabled by the php.ini setting phar.readonly");
    3961           1 :                 return;
    3962             :         }
    3963             : 
    3964           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &metadata) == FAILURE) {
    3965           1 :                 return;
    3966             :         }
    3967             : 
    3968           8 :         if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    3969           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    3970           0 :                 return;
    3971             :         }
    3972          16 :         if (Z_TYPE(phar_obj->archive->metadata) != IS_UNDEF) {
    3973           1 :                 zval_ptr_dtor(&phar_obj->archive->metadata);
    3974           1 :                 ZVAL_UNDEF(&phar_obj->archive->metadata);
    3975             :         }
    3976             : 
    3977           8 :         ZVAL_COPY(&phar_obj->archive->metadata, metadata);
    3978           8 :         phar_obj->archive->is_modified = 1;
    3979           8 :         phar_flush(phar_obj->archive, 0, 0, 0, &error);
    3980             : 
    3981           8 :         if (error) {
    3982           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    3983           0 :                 efree(error);
    3984             :         }
    3985             : }
    3986             : /* }}} */
    3987             : 
    3988             : /* {{{ proto int Phar::delMetadata()
    3989             :  * Deletes the global metadata of the phar
    3990             :  */
    3991           4 : PHP_METHOD(Phar, delMetadata)
    3992             : {
    3993             :         char *error;
    3994             : 
    3995           8 :         PHAR_ARCHIVE_OBJECT();
    3996             : 
    3997           4 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    3998           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Write operations disabled by the php.ini setting phar.readonly");
    3999           1 :                 return;
    4000             :         }
    4001             : 
    4002           6 :         if (Z_TYPE(phar_obj->archive->metadata) != IS_UNDEF) {
    4003           2 :                 zval_ptr_dtor(&phar_obj->archive->metadata);
    4004           2 :                 ZVAL_UNDEF(&phar_obj->archive->metadata);
    4005           2 :                 phar_obj->archive->is_modified = 1;
    4006           2 :                 phar_flush(phar_obj->archive, 0, 0, 0, &error);
    4007             : 
    4008           2 :                 if (error) {
    4009           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    4010           0 :                         efree(error);
    4011           0 :                         RETURN_FALSE;
    4012             :                 } else {
    4013           2 :                         RETURN_TRUE;
    4014             :                 }
    4015             : 
    4016             :         } else {
    4017           1 :                 RETURN_TRUE;
    4018             :         }
    4019             : }
    4020             : /* }}} */
    4021             : 
    4022          29 : static int phar_extract_file(zend_bool overwrite, phar_entry_info *entry, char *dest, int dest_len, char **error) /* {{{ */
    4023             : {
    4024             :         php_stream_statbuf ssb;
    4025             :         int len;
    4026             :         php_stream *fp;
    4027             :         char *fullpath;
    4028             :         const char *slash;
    4029             :         mode_t mode;
    4030             :         cwd_state new_state;
    4031             :         char *filename;
    4032             :         size_t filename_len;
    4033             : 
    4034          29 :         if (entry->is_mounted) {
    4035             :                 /* silently ignore mounted entries */
    4036           4 :                 return SUCCESS;
    4037             :         }
    4038             : 
    4039          25 :         if (entry->filename_len >= sizeof(".phar")-1 && !memcmp(entry->filename, ".phar", sizeof(".phar")-1)) {
    4040           0 :                 return SUCCESS;
    4041             :         }
    4042             :         /* strip .. from path and restrict it to be under dest directory */
    4043          25 :         new_state.cwd = (char*)emalloc(2);
    4044          25 :         new_state.cwd[0] = DEFAULT_SLASH;
    4045          25 :         new_state.cwd[1] = '\0';
    4046          25 :         new_state.cwd_length = 1;
    4047          49 :         if (virtual_file_ex(&new_state, entry->filename, NULL, CWD_EXPAND) != 0 ||
    4048          24 :                         new_state.cwd_length <= 1) {
    4049           2 :                 if (EINVAL == errno && entry->filename_len > 50) {
    4050           1 :                         char *tmp = estrndup(entry->filename, 50);
    4051           1 :                         spprintf(error, 4096, "Cannot extract \"%s...\" to \"%s...\", extracted filename is too long for filesystem", tmp, dest);
    4052           1 :                         efree(tmp);
    4053             :                 } else {
    4054           0 :                         spprintf(error, 4096, "Cannot extract \"%s\", internal error", entry->filename);
    4055             :                 }
    4056           1 :                 efree(new_state.cwd);
    4057           1 :                 return FAILURE;
    4058             :         }
    4059          24 :         filename = new_state.cwd + 1;
    4060          24 :         filename_len = new_state.cwd_length - 1;
    4061             : #ifdef PHP_WIN32
    4062             :         /* unixify the path back, otherwise non zip formats might be broken */
    4063             :         {
    4064             :                 int cnt = filename_len;
    4065             : 
    4066             :                 do {
    4067             :                         if ('\\' == filename[cnt]) {
    4068             :                                 filename[cnt] = '/';
    4069             :                         }
    4070             :                 } while (cnt-- >= 0);
    4071             :         }
    4072             : #endif
    4073             : 
    4074          24 :         len = spprintf(&fullpath, 0, "%s/%s", dest, filename);
    4075             : 
    4076          24 :         if (len >= MAXPATHLEN) {
    4077             :                 char *tmp;
    4078             :                 /* truncate for error message */
    4079           0 :                 fullpath[50] = '\0';
    4080           0 :                 if (entry->filename_len > 50) {
    4081           0 :                         tmp = estrndup(entry->filename, 50);
    4082           0 :                         spprintf(error, 4096, "Cannot extract \"%s...\" to \"%s...\", extracted filename is too long for filesystem", tmp, fullpath);
    4083           0 :                         efree(tmp);
    4084             :                 } else {
    4085           0 :                         spprintf(error, 4096, "Cannot extract \"%s\" to \"%s...\", extracted filename is too long for filesystem", entry->filename, fullpath);
    4086             :                 }
    4087           0 :                 efree(fullpath);
    4088           0 :                 efree(new_state.cwd);
    4089           0 :                 return FAILURE;
    4090             :         }
    4091             : 
    4092          24 :         if (!len) {
    4093           0 :                 spprintf(error, 4096, "Cannot extract \"%s\", internal error", entry->filename);
    4094           0 :                 efree(fullpath);
    4095           0 :                 efree(new_state.cwd);
    4096           0 :                 return FAILURE;
    4097             :         }
    4098             : 
    4099          24 :         if (php_check_open_basedir(fullpath)) {
    4100           0 :                 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", openbasedir/safe mode restrictions in effect", entry->filename, fullpath);
    4101           0 :                 efree(fullpath);
    4102           0 :                 efree(new_state.cwd);
    4103           0 :                 return FAILURE;
    4104             :         }
    4105             : 
    4106             :         /* let see if the path already exists */
    4107          24 :         if (!overwrite && SUCCESS == php_stream_stat_path(fullpath, &ssb)) {
    4108           1 :                 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", path already exists", entry->filename, fullpath);
    4109           1 :                 efree(fullpath);
    4110           1 :                 efree(new_state.cwd);
    4111           1 :                 return FAILURE;
    4112             :         }
    4113             : 
    4114             :         /* perform dirname */
    4115          23 :         slash = zend_memrchr(filename, '/', filename_len);
    4116             : 
    4117          23 :         if (slash) {
    4118           7 :                 fullpath[dest_len + (slash - filename) + 1] = '\0';
    4119             :         } else {
    4120          16 :                 fullpath[dest_len] = '\0';
    4121             :         }
    4122             : 
    4123          23 :         if (FAILURE == php_stream_stat_path(fullpath, &ssb)) {
    4124           7 :                 if (entry->is_dir) {
    4125           3 :                         if (!php_stream_mkdir(fullpath, entry->flags & PHAR_ENT_PERM_MASK,  PHP_STREAM_MKDIR_RECURSIVE, NULL)) {
    4126           0 :                                 spprintf(error, 4096, "Cannot extract \"%s\", could not create directory \"%s\"", entry->filename, fullpath);
    4127           0 :                                 efree(fullpath);
    4128           0 :                                 efree(new_state.cwd);
    4129           0 :                                 return FAILURE;
    4130             :                         }
    4131             :                 } else {
    4132           4 :                         if (!php_stream_mkdir(fullpath, 0777,  PHP_STREAM_MKDIR_RECURSIVE, NULL)) {
    4133           1 :                                 spprintf(error, 4096, "Cannot extract \"%s\", could not create directory \"%s\"", entry->filename, fullpath);
    4134           1 :                                 efree(fullpath);
    4135           1 :                                 efree(new_state.cwd);
    4136           1 :                                 return FAILURE;
    4137             :                         }
    4138             :                 }
    4139             :         }
    4140             : 
    4141          22 :         if (slash) {
    4142           6 :                 fullpath[dest_len + (slash - filename) + 1] = '/';
    4143             :         } else {
    4144          16 :                 fullpath[dest_len] = '/';
    4145             :         }
    4146             : 
    4147          22 :         filename = NULL;
    4148          22 :         efree(new_state.cwd);
    4149             :         /* it is a standalone directory, job done */
    4150          22 :         if (entry->is_dir) {
    4151           3 :                 efree(fullpath);
    4152           3 :                 return SUCCESS;
    4153             :         }
    4154             : 
    4155          19 :         fp = php_stream_open_wrapper(fullpath, "w+b", REPORT_ERRORS, NULL);
    4156             : 
    4157          19 :         if (!fp) {
    4158           0 :                 spprintf(error, 4096, "Cannot extract \"%s\", could not open for writing \"%s\"", entry->filename, fullpath);
    4159           0 :                 efree(fullpath);
    4160           0 :                 return FAILURE;
    4161             :         }
    4162             : 
    4163          19 :         if (!phar_get_efp(entry, 0)) {
    4164           0 :                 if (FAILURE == phar_open_entry_fp(entry, error, 1)) {
    4165           0 :                         if (error) {
    4166           0 :                                 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", unable to open internal file pointer: %s", entry->filename, fullpath, *error);
    4167             :                         } else {
    4168           0 :                                 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", unable to open internal file pointer", entry->filename, fullpath);
    4169             :                         }
    4170           0 :                         efree(fullpath);
    4171           0 :                         php_stream_close(fp);
    4172           0 :                         return FAILURE;
    4173             :                 }
    4174             :         }
    4175             : 
    4176          19 :         if (FAILURE == phar_seek_efp(entry, 0, SEEK_SET, 0, 0)) {
    4177           0 :                 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", unable to seek internal file pointer", entry->filename, fullpath);
    4178           0 :                 efree(fullpath);
    4179           0 :                 php_stream_close(fp);
    4180           0 :                 return FAILURE;
    4181             :         }
    4182             : 
    4183          19 :         if (SUCCESS != php_stream_copy_to_stream_ex(phar_get_efp(entry, 0), fp, entry->uncompressed_filesize, NULL)) {
    4184           0 :                 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", copying contents failed", entry->filename, fullpath);
    4185           0 :                 efree(fullpath);
    4186           0 :                 php_stream_close(fp);
    4187           0 :                 return FAILURE;
    4188             :         }
    4189             : 
    4190          19 :         php_stream_close(fp);
    4191          19 :         mode = (mode_t) entry->flags & PHAR_ENT_PERM_MASK;
    4192             : 
    4193          19 :         if (FAILURE == VCWD_CHMOD(fullpath, mode)) {
    4194           0 :                 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", setting file permissions failed", entry->filename, fullpath);
    4195           0 :                 efree(fullpath);
    4196           0 :                 return FAILURE;
    4197             :         }
    4198             : 
    4199          19 :         efree(fullpath);
    4200          19 :         return SUCCESS;
    4201             : }
    4202             : /* }}} */
    4203             : 
    4204             : /* {{{ proto bool Phar::extractTo(string pathto[[, mixed files], bool overwrite])
    4205             :  * Extract one or more file from a phar archive, optionally overwriting existing files
    4206             :  */
    4207          22 : PHP_METHOD(Phar, extractTo)
    4208             : {
    4209          22 :         char *error = NULL;
    4210             :         php_stream *fp;
    4211             :         php_stream_statbuf ssb;
    4212             :         phar_entry_info *entry;
    4213             :         char *pathto, *filename;
    4214             :         size_t pathto_len, filename_len;
    4215             :         int ret, i;
    4216             :         int nelems;
    4217          22 :         zval *zval_files = NULL;
    4218          22 :         zend_bool overwrite = 0;
    4219             : 
    4220          44 :         PHAR_ARCHIVE_OBJECT();
    4221             : 
    4222          22 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|z!b", &pathto, &pathto_len, &zval_files, &overwrite) == FAILURE) {
    4223           1 :                 return;
    4224             :         }
    4225             : 
    4226          21 :         fp = php_stream_open_wrapper(phar_obj->archive->fname, "rb", IGNORE_URL|STREAM_MUST_SEEK, NULL);
    4227             : 
    4228          21 :         if (!fp) {
    4229           1 :                 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
    4230           1 :                         "Invalid argument, %s cannot be found", phar_obj->archive->fname);
    4231           1 :                 return;
    4232             :         }
    4233             : 
    4234          20 :         php_stream_close(fp);
    4235             : 
    4236          20 :         if (pathto_len < 1) {
    4237           1 :                 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
    4238             :                         "Invalid argument, extraction path must be non-zero length");
    4239           1 :                 return;
    4240             :         }
    4241             : 
    4242          19 :         if (pathto_len >= MAXPATHLEN) {
    4243           1 :                 char *tmp = estrndup(pathto, 50);
    4244             :                 /* truncate for error message */
    4245           1 :                 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Cannot extract to \"%s...\", destination directory is too long for filesystem", tmp);
    4246           1 :                 efree(tmp);
    4247           1 :                 return;
    4248             :         }
    4249             : 
    4250          18 :         if (php_stream_stat_path(pathto, &ssb) < 0) {
    4251           7 :                 ret = php_stream_mkdir(pathto, 0777,  PHP_STREAM_MKDIR_RECURSIVE, NULL);
    4252           7 :                 if (!ret) {
    4253           0 :                         zend_throw_exception_ex(spl_ce_RuntimeException, 0,
    4254             :                                 "Unable to create path \"%s\" for extraction", pathto);
    4255           0 :                         return;
    4256             :                 }
    4257          11 :         } else if (!(ssb.sb.st_mode & S_IFDIR)) {
    4258           1 :                 zend_throw_exception_ex(spl_ce_RuntimeException, 0,
    4259             :                         "Unable to use path \"%s\" for extraction, it is a file, must be a directory", pathto);
    4260           1 :                 return;
    4261             :         }
    4262             : 
    4263          17 :         if (zval_files) {
    4264          20 :                 switch (Z_TYPE_P(zval_files)) {
    4265             :                         case IS_NULL:
    4266           0 :                                 goto all_files;
    4267             :                         case IS_STRING:
    4268           7 :                                 filename = Z_STRVAL_P(zval_files);
    4269           7 :                                 filename_len = Z_STRLEN_P(zval_files);
    4270           7 :                                 break;
    4271             :                         case IS_ARRAY:
    4272           2 :                                 nelems = zend_hash_num_elements(Z_ARRVAL_P(zval_files));
    4273           2 :                                 if (nelems == 0 ) {
    4274           0 :                                         RETURN_FALSE;
    4275             :                                 }
    4276           4 :                                 for (i = 0; i < nelems; i++) {
    4277             :                                         zval *zval_file;
    4278           3 :                                         if ((zval_file = zend_hash_index_find(Z_ARRVAL_P(zval_files), i)) != NULL) {
    4279           3 :                                                 switch (Z_TYPE_P(zval_file)) {
    4280             :                                                         case IS_STRING:
    4281           2 :                                                                 break;
    4282             :                                                         default:
    4283           1 :                                                                 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
    4284             :                                                                         "Invalid argument, array of filenames to extract contains non-string value");
    4285           1 :                                                                 return;
    4286             :                                                 }
    4287           4 :                                                 if (NULL == (entry = zend_hash_find_ptr(&phar_obj->archive->manifest, Z_STR_P(zval_file)))) {
    4288           0 :                                                         zend_throw_exception_ex(phar_ce_PharException, 0,
    4289           0 :                                                                 "Phar Error: attempted to extract non-existent file \"%s\" from phar \"%s\"", Z_STRVAL_P(zval_file), phar_obj->archive->fname);
    4290             :                                                 }
    4291           2 :                                                 if (FAILURE == phar_extract_file(overwrite, entry, pathto, pathto_len, &error)) {
    4292           0 :                                                         zend_throw_exception_ex(phar_ce_PharException, 0,
    4293           0 :                                                                 "Extraction from phar \"%s\" failed: %s", phar_obj->archive->fname, error);
    4294           0 :                                                         efree(error);
    4295           0 :                                                         return;
    4296             :                                                 }
    4297             :                                         }
    4298             :                                 }
    4299           1 :                                 RETURN_TRUE;
    4300             :                         default:
    4301           1 :                                 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
    4302             :                                         "Invalid argument, expected a filename (string) or array of filenames");
    4303           1 :                                 return;
    4304             :                 }
    4305             : 
    4306          14 :                 if (NULL == (entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, filename, filename_len))) {
    4307           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0,
    4308           0 :                                 "Phar Error: attempted to extract non-existent file \"%s\" from phar \"%s\"", filename, phar_obj->archive->fname);
    4309           0 :                         return;
    4310             :                 }
    4311             : 
    4312           7 :                 if (FAILURE == phar_extract_file(overwrite, entry, pathto, pathto_len, &error)) {
    4313           4 :                         zend_throw_exception_ex(phar_ce_PharException, 0,
    4314           2 :                                 "Extraction from phar \"%s\" failed: %s", phar_obj->archive->fname, error);
    4315           2 :                         efree(error);
    4316           2 :                         return;
    4317             :                 }
    4318             :         } else {
    4319             :                 phar_archive_data *phar;
    4320             : all_files:
    4321           7 :                 phar = phar_obj->archive;
    4322             :                 /* Extract all files */
    4323           7 :                 if (!zend_hash_num_elements(&(phar->manifest))) {
    4324           0 :                         RETURN_TRUE;
    4325             :                 }
    4326             : 
    4327          46 :                 ZEND_HASH_FOREACH_PTR(&phar->manifest, entry) {
    4328          20 :                         if (FAILURE == phar_extract_file(overwrite, entry, pathto, pathto_len, &error)) {
    4329           1 :                                 zend_throw_exception_ex(phar_ce_PharException, 0,
    4330             :                                         "Extraction from phar \"%s\" failed: %s", phar->fname, error);
    4331           1 :                                 efree(error);
    4332           1 :                                 return;
    4333             :                         }
    4334             :                 } ZEND_HASH_FOREACH_END();
    4335             :         }
    4336          11 :         RETURN_TRUE;
    4337             : }
    4338             : /* }}} */
    4339             : 
    4340             : 
    4341             : /* {{{ proto void PharFileInfo::__construct(string entry)
    4342             :  * Construct a Phar entry object
    4343             :  */
    4344         404 : PHP_METHOD(PharFileInfo, __construct)
    4345             : {
    4346             :         char *fname, *arch, *entry, *error;
    4347             :         size_t fname_len;
    4348             :         int arch_len, entry_len;
    4349             :         phar_entry_object *entry_obj;
    4350             :         phar_entry_info *entry_info;
    4351             :         phar_archive_data *phar_data;
    4352         808 :         zval *zobj = getThis(), arg1;
    4353             : 
    4354         404 :         if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) {
    4355           1 :                 return;
    4356             :         }
    4357             : 
    4358         403 :         entry_obj = (phar_entry_object*)((char*)Z_OBJ_P(zobj) - Z_OBJ_P(zobj)->handlers->offset);
    4359             : 
    4360         403 :         if (entry_obj->entry) {
    4361           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot call constructor twice");
    4362           1 :                 return;
    4363             :         }
    4364             : 
    4365         402 :         if (fname_len < 7 || memcmp(fname, "phar://", 7) || phar_split_fname(fname, (int)fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0) == FAILURE) {
    4366           1 :                 zend_throw_exception_ex(spl_ce_RuntimeException, 0,
    4367             :                         "'%s' is not a valid phar archive URL (must have at least phar://filename.phar)", fname);
    4368           1 :                 return;
    4369             :         }
    4370             : 
    4371         401 :         if (phar_open_from_filename(arch, arch_len, NULL, 0, REPORT_ERRORS, &phar_data, &error) == FAILURE) {
    4372           1 :                 efree(arch);
    4373           1 :                 efree(entry);
    4374           1 :                 if (error) {
    4375           1 :                         zend_throw_exception_ex(spl_ce_RuntimeException, 0,
    4376             :                                 "Cannot open phar file '%s': %s", fname, error);
    4377           1 :                         efree(error);
    4378             :                 } else {
    4379           0 :                         zend_throw_exception_ex(spl_ce_RuntimeException, 0,
    4380             :                                 "Cannot open phar file '%s'", fname);
    4381             :                 }
    4382           1 :                 return;
    4383             :         }
    4384             : 
    4385         400 :         if ((entry_info = phar_get_entry_info_dir(phar_data, entry, entry_len, 1, &error, 1)) == NULL) {
    4386           2 :                 zend_throw_exception_ex(spl_ce_RuntimeException, 0,
    4387           2 :                         "Cannot access phar file entry '%s' in archive '%s'%s%s", entry, arch, error ? ", " : "", error ? error : "");
    4388           1 :                 efree(arch);
    4389           1 :                 efree(entry);
    4390           1 :                 return;
    4391             :         }
    4392             : 
    4393         399 :         efree(arch);
    4394         399 :         efree(entry);
    4395             : 
    4396         399 :         entry_obj->entry = entry_info;
    4397             : 
    4398         798 :         ZVAL_STRINGL(&arg1, fname, fname_len);
    4399             : 
    4400         399 :         zend_call_method_with_1_params(zobj, Z_OBJCE_P(zobj),
    4401             :                 &spl_ce_SplFileInfo->constructor, "__construct", NULL, &arg1);
    4402             : 
    4403         399 :         zval_ptr_dtor(&arg1);
    4404             : }
    4405             : /* }}} */
    4406             : 
    4407             : #define PHAR_ENTRY_OBJECT() \
    4408             :         zval *zobj = getThis(); \
    4409             :         phar_entry_object *entry_obj = (phar_entry_object*)((char*)Z_OBJ_P(zobj) - Z_OBJ_P(zobj)->handlers->offset); \
    4410             :         if (!entry_obj->entry) { \
    4411             :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    4412             :                         "Cannot call method on an uninitialized PharFileInfo object"); \
    4413             :                 return; \
    4414             :         }
    4415             : 
    4416             : /* {{{ proto void PharFileInfo::__destruct()
    4417             :  * clean up directory-based entry objects
    4418             :  */
    4419         399 : PHP_METHOD(PharFileInfo, __destruct)
    4420             : {
    4421         798 :         zval *zobj = getThis();
    4422         399 :         phar_entry_object *entry_obj = (phar_entry_object*)((char*)Z_OBJ_P(zobj) - Z_OBJ_P(zobj)->handlers->offset);
    4423             : 
    4424         399 :         if (entry_obj->entry && entry_obj->entry->is_temp_dir) {
    4425          10 :                 if (entry_obj->entry->filename) {
    4426          10 :                         efree(entry_obj->entry->filename);
    4427          10 :                         entry_obj->entry->filename = NULL;
    4428             :                 }
    4429             : 
    4430          10 :                 efree(entry_obj->entry);
    4431          10 :                 entry_obj->entry = NULL;
    4432             :         }
    4433         399 : }
    4434             : /* }}} */
    4435             : 
    4436             : /* {{{ proto int PharFileInfo::getCompressedSize()
    4437             :  * Returns the compressed size
    4438             :  */
    4439           5 : PHP_METHOD(PharFileInfo, getCompressedSize)
    4440             : {
    4441          10 :         PHAR_ENTRY_OBJECT();
    4442             : 
    4443           5 :         if (zend_parse_parameters_none() == FAILURE) {
    4444           0 :                 return;
    4445             :         }
    4446             : 
    4447           5 :         RETURN_LONG(entry_obj->entry->compressed_filesize);
    4448             : }
    4449             : /* }}} */
    4450             : 
    4451             : /* {{{ proto bool PharFileInfo::isCompressed([int compression_type])
    4452             :  * Returns whether the entry is compressed, and whether it is compressed with Phar::GZ or Phar::BZ2 if specified
    4453             :  */
    4454         102 : PHP_METHOD(PharFileInfo, isCompressed)
    4455             : {
    4456             :         /* a number that is not Phar::GZ or Phar::BZ2 */
    4457         102 :         zend_long method = 9021976;
    4458         204 :         PHAR_ENTRY_OBJECT();
    4459             : 
    4460         102 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &method) == FAILURE) {
    4461           1 :                 return;
    4462             :         }
    4463             : 
    4464         101 :         switch (method) {
    4465             :                 case 9021976:
    4466          68 :                         RETURN_BOOL(entry_obj->entry->flags & PHAR_ENT_COMPRESSION_MASK);
    4467             :                 case PHAR_ENT_COMPRESSED_GZ:
    4468          16 :                         RETURN_BOOL(entry_obj->entry->flags & PHAR_ENT_COMPRESSED_GZ);
    4469             :                 case PHAR_ENT_COMPRESSED_BZ2:
    4470          16 :                         RETURN_BOOL(entry_obj->entry->flags & PHAR_ENT_COMPRESSED_BZ2);
    4471             :                 default:
    4472           1 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    4473             :                                 "Unknown compression type specified"); \
    4474             :         }
    4475             : }
    4476             : /* }}} */
    4477             : 
    4478             : /* {{{ proto int PharFileInfo::getCRC32()
    4479             :  * Returns CRC32 code or throws an exception if not CRC checked
    4480             :  */
    4481           3 : PHP_METHOD(PharFileInfo, getCRC32)
    4482             : {
    4483           6 :         PHAR_ENTRY_OBJECT();
    4484             : 
    4485           3 :         if (zend_parse_parameters_none() == FAILURE) {
    4486           0 :                 return;
    4487             :         }
    4488             : 
    4489           3 :         if (entry_obj->entry->is_dir) {
    4490           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    4491             :                         "Phar entry is a directory, does not have a CRC"); \
    4492           1 :                 return;
    4493             :         }
    4494             : 
    4495           2 :         if (entry_obj->entry->is_crc_checked) {
    4496           1 :                 RETURN_LONG(entry_obj->entry->crc32);
    4497             :         } else {
    4498           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    4499             :                         "Phar entry was not CRC checked"); \
    4500             :         }
    4501             : }
    4502             : /* }}} */
    4503             : 
    4504             : /* {{{ proto int PharFileInfo::isCRCChecked()
    4505             :  * Returns whether file entry is CRC checked
    4506             :  */
    4507          10 : PHP_METHOD(PharFileInfo, isCRCChecked)
    4508             : {
    4509          20 :         PHAR_ENTRY_OBJECT();
    4510             : 
    4511          10 :         if (zend_parse_parameters_none() == FAILURE) {
    4512           0 :                 return;
    4513             :         }
    4514             : 
    4515          10 :         RETURN_BOOL(entry_obj->entry->is_crc_checked);
    4516             : }
    4517             : /* }}} */
    4518             : 
    4519             : /* {{{ proto int PharFileInfo::getPharFlags()
    4520             :  * Returns the Phar file entry flags
    4521             :  */
    4522           5 : PHP_METHOD(PharFileInfo, getPharFlags)
    4523             : {
    4524          10 :         PHAR_ENTRY_OBJECT();
    4525             : 
    4526           5 :         if (zend_parse_parameters_none() == FAILURE) {
    4527           0 :                 return;
    4528             :         }
    4529             : 
    4530           5 :         RETURN_LONG(entry_obj->entry->flags & ~(PHAR_ENT_PERM_MASK|PHAR_ENT_COMPRESSION_MASK));
    4531             : }
    4532             : /* }}} */
    4533             : 
    4534             : /* {{{ proto int PharFileInfo::chmod()
    4535             :  * set the file permissions for the Phar.  This only allows setting execution bit, read/write
    4536             :  */
    4537          23 : PHP_METHOD(PharFileInfo, chmod)
    4538             : {
    4539             :         char *error;
    4540             :         zend_long perms;
    4541          46 :         PHAR_ENTRY_OBJECT();
    4542             : 
    4543          23 :         if (entry_obj->entry->is_temp_dir) {
    4544           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    4545           1 :                         "Phar entry \"%s\" is a temporary directory (not an actual entry in the archive), cannot chmod", entry_obj->entry->filename); \
    4546           1 :                 return;
    4547             :         }
    4548             : 
    4549          22 :         if (PHAR_G(readonly) && !entry_obj->entry->phar->is_data) {
    4550           3 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "Cannot modify permissions for file \"%s\" in phar \"%s\", write operations are prohibited", entry_obj->entry->filename, entry_obj->entry->phar->fname);
    4551           3 :                 return;
    4552             :         }
    4553             : 
    4554          19 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &perms) == FAILURE) {
    4555           1 :                 return;
    4556             :         }
    4557             : 
    4558          18 :         if (entry_obj->entry->is_persistent) {
    4559           0 :                 phar_archive_data *phar = entry_obj->entry->phar;
    4560             : 
    4561           0 :                 if (FAILURE == phar_copy_on_write(&phar)) {
    4562           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
    4563           0 :                         return;
    4564             :                 }
    4565             :                 /* re-populate after copy-on-write */
    4566           0 :                 entry_obj->entry = zend_hash_str_find_ptr(&phar->manifest, entry_obj->entry->filename, entry_obj->entry->filename_len);
    4567             :         }
    4568             :         /* clear permissions */
    4569          18 :         entry_obj->entry->flags &= ~PHAR_ENT_PERM_MASK;
    4570          18 :         perms &= 0777;
    4571          18 :         entry_obj->entry->flags |= perms;
    4572          18 :         entry_obj->entry->old_flags = entry_obj->entry->flags;
    4573          18 :         entry_obj->entry->phar->is_modified = 1;
    4574          18 :         entry_obj->entry->is_modified = 1;
    4575             : 
    4576             :         /* hackish cache in php_stat needs to be cleared */
    4577             :         /* if this code fails to work, check main/streams/streams.c, _php_stream_stat_path */
    4578          18 :         if (BG(CurrentLStatFile)) {
    4579           1 :                 efree(BG(CurrentLStatFile));
    4580             :         }
    4581             : 
    4582          18 :         if (BG(CurrentStatFile)) {
    4583          18 :                 efree(BG(CurrentStatFile));
    4584             :         }
    4585             : 
    4586          18 :         BG(CurrentLStatFile) = NULL;
    4587          18 :         BG(CurrentStatFile) = NULL;
    4588          18 :         phar_flush(entry_obj->entry->phar, 0, 0, 0, &error);
    4589             : 
    4590          18 :         if (error) {
    4591           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    4592           0 :                 efree(error);
    4593             :         }
    4594             : }
    4595             : /* }}} */
    4596             : 
    4597             : /* {{{ proto int PharFileInfo::hasMetaData()
    4598             :  * Returns the metadata of the entry
    4599             :  */
    4600           4 : PHP_METHOD(PharFileInfo, hasMetadata)
    4601             : {
    4602           8 :         PHAR_ENTRY_OBJECT();
    4603             : 
    4604           4 :         if (zend_parse_parameters_none() == FAILURE) {
    4605           0 :                 return;
    4606             :         }
    4607             : 
    4608           8 :         RETURN_BOOL(Z_TYPE(entry_obj->entry->metadata) != IS_UNDEF);
    4609             : }
    4610             : /* }}} */
    4611             : 
    4612             : /* {{{ proto int PharFileInfo::getMetaData()
    4613             :  * Returns the metadata of the entry
    4614             :  */
    4615          52 : PHP_METHOD(PharFileInfo, getMetadata)
    4616             : {
    4617         104 :         PHAR_ENTRY_OBJECT();
    4618             : 
    4619          52 :         if (zend_parse_parameters_none() == FAILURE) {
    4620           0 :                 return;
    4621             :         }
    4622             : 
    4623         104 :         if (Z_TYPE(entry_obj->entry->metadata) != IS_UNDEF) {
    4624          41 :                 if (entry_obj->entry->is_persistent) {
    4625           0 :                         char *buf = estrndup((char *) Z_PTR(entry_obj->entry->metadata), entry_obj->entry->metadata_len);
    4626             :                         /* assume success, we would have failed before */
    4627           0 :                         phar_parse_metadata(&buf, return_value, entry_obj->entry->metadata_len);
    4628           0 :                         efree(buf);
    4629             :                 } else {
    4630          41 :                         ZVAL_COPY(return_value, &entry_obj->entry->metadata);
    4631             :                 }
    4632             :         }
    4633             : }
    4634             : /* }}} */
    4635             : 
    4636             : /* {{{ proto int PharFileInfo::setMetaData(mixed $metadata)
    4637             :  * Sets the metadata of the entry
    4638             :  */
    4639          24 : PHP_METHOD(PharFileInfo, setMetadata)
    4640             : {
    4641             :         char *error;
    4642             :         zval *metadata;
    4643             : 
    4644          48 :         PHAR_ENTRY_OBJECT();
    4645             : 
    4646          24 :         if (PHAR_G(readonly) && !entry_obj->entry->phar->is_data) {
    4647           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Write operations disabled by the php.ini setting phar.readonly");
    4648           1 :                 return;
    4649             :         }
    4650             : 
    4651          23 :         if (entry_obj->entry->is_temp_dir) {
    4652           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    4653             :                         "Phar entry is a temporary directory (not an actual entry in the archive), cannot set metadata"); \
    4654           1 :                 return;
    4655             :         }
    4656             : 
    4657          22 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &metadata) == FAILURE) {
    4658           1 :                 return;
    4659             :         }
    4660             : 
    4661          21 :         if (entry_obj->entry->is_persistent) {
    4662           0 :                 phar_archive_data *phar = entry_obj->entry->phar;
    4663             : 
    4664           0 :                 if (FAILURE == phar_copy_on_write(&phar)) {
    4665           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
    4666           0 :                         return;
    4667             :                 }
    4668             :                 /* re-populate after copy-on-write */
    4669           0 :                 entry_obj->entry = zend_hash_str_find_ptr(&phar->manifest, entry_obj->entry->filename, entry_obj->entry->filename_len);
    4670             :         }
    4671          42 :         if (Z_TYPE(entry_obj->entry->metadata) != IS_UNDEF) {
    4672          13 :                 zval_ptr_dtor(&entry_obj->entry->metadata);
    4673          13 :                 ZVAL_UNDEF(&entry_obj->entry->metadata);
    4674             :         }
    4675             : 
    4676          21 :         ZVAL_COPY(&entry_obj->entry->metadata, metadata);
    4677             : 
    4678          21 :         entry_obj->entry->is_modified = 1;
    4679          21 :         entry_obj->entry->phar->is_modified = 1;
    4680          21 :         phar_flush(entry_obj->entry->phar, 0, 0, 0, &error);
    4681             : 
    4682          21 :         if (error) {
    4683           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    4684           0 :                 efree(error);
    4685             :         }
    4686             : }
    4687             : /* }}} */
    4688             : 
    4689             : /* {{{ proto bool PharFileInfo::delMetaData()
    4690             :  * Deletes the metadata of the entry
    4691             :  */
    4692           7 : PHP_METHOD(PharFileInfo, delMetadata)
    4693             : {
    4694             :         char *error;
    4695             : 
    4696          14 :         PHAR_ENTRY_OBJECT();
    4697             : 
    4698           7 :         if (zend_parse_parameters_none() == FAILURE) {
    4699           0 :                 return;
    4700             :         }
    4701             : 
    4702           7 :         if (PHAR_G(readonly) && !entry_obj->entry->phar->is_data) {
    4703           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Write operations disabled by the php.ini setting phar.readonly");
    4704           1 :                 return;
    4705             :         }
    4706             : 
    4707           6 :         if (entry_obj->entry->is_temp_dir) {
    4708           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    4709             :                         "Phar entry is a temporary directory (not an actual entry in the archive), cannot delete metadata"); \
    4710           1 :                 return;
    4711             :         }
    4712             : 
    4713          10 :         if (Z_TYPE(entry_obj->entry->metadata) != IS_UNDEF) {
    4714           3 :                 if (entry_obj->entry->is_persistent) {
    4715           0 :                         phar_archive_data *phar = entry_obj->entry->phar;
    4716             : 
    4717           0 :                         if (FAILURE == phar_copy_on_write(&phar)) {
    4718           0 :                                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
    4719           0 :                                 return;
    4720             :                         }
    4721             :                         /* re-populate after copy-on-write */
    4722           0 :                         entry_obj->entry = zend_hash_str_find_ptr(&phar->manifest, entry_obj->entry->filename, entry_obj->entry->filename_len);
    4723             :                 }
    4724           3 :                 zval_ptr_dtor(&entry_obj->entry->metadata);
    4725           3 :                 ZVAL_UNDEF(&entry_obj->entry->metadata);
    4726           3 :                 entry_obj->entry->is_modified = 1;
    4727           3 :                 entry_obj->entry->phar->is_modified = 1;
    4728             : 
    4729           3 :                 phar_flush(entry_obj->entry->phar, 0, 0, 0, &error);
    4730             : 
    4731           3 :                 if (error) {
    4732           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    4733           0 :                         efree(error);
    4734           0 :                         RETURN_FALSE;
    4735             :                 } else {
    4736           3 :                         RETURN_TRUE;
    4737             :                 }
    4738             : 
    4739             :         } else {
    4740           2 :                 RETURN_TRUE;
    4741             :         }
    4742             : }
    4743             : /* }}} */
    4744             : 
    4745             : /* {{{ proto string PharFileInfo::getContent()
    4746             :  * return the complete file contents of the entry (like file_get_contents)
    4747             :  */
    4748          38 : PHP_METHOD(PharFileInfo, getContent)
    4749             : {
    4750             :         char *error;
    4751             :         php_stream *fp;
    4752             :         phar_entry_info *link;
    4753             :         zend_string *str;
    4754             : 
    4755          76 :         PHAR_ENTRY_OBJECT();
    4756             : 
    4757          38 :         if (zend_parse_parameters_none() == FAILURE) {
    4758           0 :                 return;
    4759             :         }
    4760             : 
    4761          38 :         if (entry_obj->entry->is_dir) {
    4762           4 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4763           4 :                         "Phar error: Cannot retrieve contents, \"%s\" in phar \"%s\" is a directory", entry_obj->entry->filename, entry_obj->entry->phar->fname);
    4764           2 :                 return;
    4765             :         }
    4766             : 
    4767          36 :         link = phar_get_link_source(entry_obj->entry);
    4768             : 
    4769          36 :         if (!link) {
    4770           0 :                 link = entry_obj->entry;
    4771             :         }
    4772             : 
    4773          36 :         if (SUCCESS != phar_open_entry_fp(link, &error, 0)) {
    4774           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4775           0 :                         "Phar error: Cannot retrieve contents, \"%s\" in phar \"%s\": %s", entry_obj->entry->filename, entry_obj->entry->phar->fname, error);
    4776           0 :                 efree(error);
    4777           0 :                 return;
    4778             :         }
    4779             : 
    4780          36 :         if (!(fp = phar_get_efp(link, 0))) {
    4781           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4782           0 :                         "Phar error: Cannot retrieve contents of \"%s\" in phar \"%s\"", entry_obj->entry->filename, entry_obj->entry->phar->fname);
    4783           0 :                 return;
    4784             :         }
    4785             : 
    4786          36 :         phar_seek_efp(link, 0, SEEK_SET, 0, 0);
    4787          36 :         str = php_stream_copy_to_mem(fp, link->uncompressed_filesize, 0);
    4788          36 :         if (str) {
    4789          36 :                 RETURN_STR(str);
    4790             :         } else {
    4791           0 :                 RETURN_EMPTY_STRING();
    4792             :         }
    4793             : }
    4794             : /* }}} */
    4795             : 
    4796             : /* {{{ proto int PharFileInfo::compress(int compression_type)
    4797             :  * Instructs the Phar class to compress the current file using zlib or bzip2 compression
    4798             :  */
    4799          16 : PHP_METHOD(PharFileInfo, compress)
    4800             : {
    4801             :         zend_long method;
    4802             :         char *error;
    4803          32 :         PHAR_ENTRY_OBJECT();
    4804             : 
    4805          16 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &method) == FAILURE) {
    4806           0 :                 return;
    4807             :         }
    4808             : 
    4809          16 :         if (entry_obj->entry->is_tar) {
    4810           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4811             :                         "Cannot compress with Gzip compression, not possible with tar-based phar archives");
    4812           1 :                 return;
    4813             :         }
    4814             : 
    4815          15 :         if (entry_obj->entry->is_dir) {
    4816           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    4817             :                         "Phar entry is a directory, cannot set compression"); \
    4818           1 :                 return;
    4819             :         }
    4820             : 
    4821          14 :         if (PHAR_G(readonly) && !entry_obj->entry->phar->is_data) {
    4822           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4823             :                         "Phar is readonly, cannot change compression");
    4824           1 :                 return;
    4825             :         }
    4826             : 
    4827          13 :         if (entry_obj->entry->is_deleted) {
    4828           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4829             :                         "Cannot compress deleted file");
    4830           0 :                 return;
    4831             :         }
    4832             : 
    4833          13 :         if (entry_obj->entry->is_persistent) {
    4834           0 :                 phar_archive_data *phar = entry_obj->entry->phar;
    4835             : 
    4836           0 :                 if (FAILURE == phar_copy_on_write(&phar)) {
    4837           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
    4838           0 :                         return;
    4839             :                 }
    4840             :                 /* re-populate after copy-on-write */
    4841           0 :                 entry_obj->entry = zend_hash_str_find_ptr(&phar->manifest, entry_obj->entry->filename, entry_obj->entry->filename_len);
    4842             :         }
    4843          13 :         switch (method) {
    4844             :                 case PHAR_ENT_COMPRESSED_GZ:
    4845           8 :                         if (entry_obj->entry->flags & PHAR_ENT_COMPRESSED_GZ) {
    4846           2 :                                 RETURN_TRUE;
    4847             :                         }
    4848             : 
    4849           6 :                         if ((entry_obj->entry->flags & PHAR_ENT_COMPRESSED_BZ2) != 0) {
    4850           0 :                                 if (!PHAR_G(has_bz2)) {
    4851           0 :                                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4852             :                                                 "Cannot compress with gzip compression, file is already compressed with bzip2 compression and bz2 extension is not enabled, cannot decompress");
    4853           0 :                                         return;
    4854             :                                 }
    4855             : 
    4856             :                                 /* decompress this file indirectly */
    4857           0 :                                 if (SUCCESS != phar_open_entry_fp(entry_obj->entry, &error, 1)) {
    4858           0 :                                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4859           0 :                                                 "Phar error: Cannot decompress bzip2-compressed file \"%s\" in phar \"%s\" in order to compress with gzip: %s", entry_obj->entry->filename, entry_obj->entry->phar->fname, error);
    4860           0 :                                         efree(error);
    4861           0 :                                         return;
    4862             :                                 }
    4863             :                         }
    4864             : 
    4865           6 :                         if (!PHAR_G(has_zlib)) {
    4866           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4867             :                                         "Cannot compress with gzip compression, zlib extension is not enabled");
    4868           0 :                                 return;
    4869             :                         }
    4870             : 
    4871           6 :                         entry_obj->entry->old_flags = entry_obj->entry->flags;
    4872           6 :                         entry_obj->entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
    4873           6 :                         entry_obj->entry->flags |= PHAR_ENT_COMPRESSED_GZ;
    4874           6 :                         break;
    4875             :                 case PHAR_ENT_COMPRESSED_BZ2:
    4876           4 :                         if (entry_obj->entry->flags & PHAR_ENT_COMPRESSED_BZ2) {
    4877           1 :                                 RETURN_TRUE;
    4878             :                         }
    4879             : 
    4880           3 :                         if ((entry_obj->entry->flags & PHAR_ENT_COMPRESSED_GZ) != 0) {
    4881           1 :                                 if (!PHAR_G(has_zlib)) {
    4882           0 :                                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4883             :                                                 "Cannot compress with bzip2 compression, file is already compressed with gzip compression and zlib extension is not enabled, cannot decompress");
    4884           0 :                                         return;
    4885             :                                 }
    4886             : 
    4887             :                                 /* decompress this file indirectly */
    4888           1 :                                 if (SUCCESS != phar_open_entry_fp(entry_obj->entry, &error, 1)) {
    4889           0 :                                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4890           0 :                                                 "Phar error: Cannot decompress gzip-compressed file \"%s\" in phar \"%s\" in order to compress with bzip2: %s", entry_obj->entry->filename, entry_obj->entry->phar->fname, error);
    4891           0 :                                         efree(error);
    4892           0 :                                         return;
    4893             :                                 }
    4894             :                         }
    4895             : 
    4896           3 :                         if (!PHAR_G(has_bz2)) {
    4897           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4898             :                                         "Cannot compress with bzip2 compression, bz2 extension is not enabled");
    4899           0 :                                 return;
    4900             :                         }
    4901           3 :                         entry_obj->entry->old_flags = entry_obj->entry->flags;
    4902           3 :                         entry_obj->entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
    4903           3 :                         entry_obj->entry->flags |= PHAR_ENT_COMPRESSED_BZ2;
    4904           3 :                         break;
    4905             :                 default:
    4906           1 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    4907             :                                 "Unknown compression type specified"); \
    4908             :         }
    4909             : 
    4910          10 :         entry_obj->entry->phar->is_modified = 1;
    4911          10 :         entry_obj->entry->is_modified = 1;
    4912          10 :         phar_flush(entry_obj->entry->phar, 0, 0, 0, &error);
    4913             : 
    4914          10 :         if (error) {
    4915           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    4916           0 :                 efree(error);
    4917             :         }
    4918             : 
    4919          10 :         RETURN_TRUE;
    4920             : }
    4921             : /* }}} */
    4922             : 
    4923             : /* {{{ proto int PharFileInfo::decompress()
    4924             :  * Instructs the Phar class to decompress the current file
    4925             :  */
    4926           7 : PHP_METHOD(PharFileInfo, decompress)
    4927             : {
    4928             :         char *error;
    4929          14 :         PHAR_ENTRY_OBJECT();
    4930             : 
    4931           7 :         if (zend_parse_parameters_none() == FAILURE) {
    4932           0 :                 return;
    4933             :         }
    4934             : 
    4935           7 :         if (entry_obj->entry->is_dir) {
    4936           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    4937             :                         "Phar entry is a directory, cannot set compression"); \
    4938           1 :                 return;
    4939             :         }
    4940             : 
    4941           6 :         if ((entry_obj->entry->flags & PHAR_ENT_COMPRESSION_MASK) == 0) {
    4942           3 :                 RETURN_TRUE;
    4943             :         }
    4944             : 
    4945           3 :         if (PHAR_G(readonly) && !entry_obj->entry->phar->is_data) {
    4946           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4947             :                         "Phar is readonly, cannot decompress");
    4948           1 :                 return;
    4949             :         }
    4950             : 
    4951           2 :         if (entry_obj->entry->is_deleted) {
    4952           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4953             :                         "Cannot compress deleted file");
    4954           0 :                 return;
    4955             :         }
    4956             : 
    4957           2 :         if ((entry_obj->entry->flags & PHAR_ENT_COMPRESSED_GZ) != 0 && !PHAR_G(has_zlib)) {
    4958           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4959             :                         "Cannot decompress Gzip-compressed file, zlib extension is not enabled");
    4960           0 :                 return;
    4961             :         }
    4962             : 
    4963           2 :         if ((entry_obj->entry->flags & PHAR_ENT_COMPRESSED_BZ2) != 0 && !PHAR_G(has_bz2)) {
    4964           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4965             :                         "Cannot decompress Bzip2-compressed file, bz2 extension is not enabled");
    4966           0 :                 return;
    4967             :         }
    4968             : 
    4969           2 :         if (entry_obj->entry->is_persistent) {
    4970           0 :                 phar_archive_data *phar = entry_obj->entry->phar;
    4971             : 
    4972           0 :                 if (FAILURE == phar_copy_on_write(&phar)) {
    4973           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
    4974           0 :                         return;
    4975             :                 }
    4976             :                 /* re-populate after copy-on-write */
    4977           0 :                 entry_obj->entry = zend_hash_str_find_ptr(&phar->manifest, entry_obj->entry->filename, entry_obj->entry->filename_len);
    4978             :         }
    4979           2 :         if (!entry_obj->entry->fp) {
    4980           2 :                 if (FAILURE == phar_open_archive_fp(entry_obj->entry->phar)) {
    4981           0 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot decompress entry \"%s\", phar error: Cannot open phar archive \"%s\" for reading", entry_obj->entry->filename, entry_obj->entry->phar->fname);
    4982           0 :                         return;
    4983             :                 }
    4984           2 :                 entry_obj->entry->fp_type = PHAR_FP;
    4985             :         }
    4986             : 
    4987           2 :         entry_obj->entry->old_flags = entry_obj->entry->flags;
    4988           2 :         entry_obj->entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
    4989           2 :         entry_obj->entry->phar->is_modified = 1;
    4990           2 :         entry_obj->entry->is_modified = 1;
    4991           2 :         phar_flush(entry_obj->entry->phar, 0, 0, 0, &error);
    4992             : 
    4993           2 :         if (error) {
    4994           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    4995           0 :                 efree(error);
    4996             :         }
    4997           2 :         RETURN_TRUE;
    4998             : }
    4999             : /* }}} */
    5000             : 
    5001             : /* {{{ phar methods */
    5002             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar___construct, 0, 0, 1)
    5003             :         ZEND_ARG_INFO(0, filename)
    5004             :         ZEND_ARG_INFO(0, flags)
    5005             :         ZEND_ARG_INFO(0, alias)
    5006             :         ZEND_ARG_INFO(0, fileformat)
    5007             : ZEND_END_ARG_INFO()
    5008             : 
    5009             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_createDS, 0, 0, 0)
    5010             :         ZEND_ARG_INFO(0, index)
    5011             :         ZEND_ARG_INFO(0, webindex)
    5012             : ZEND_END_ARG_INFO()
    5013             : 
    5014             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_cancompress, 0, 0, 0)
    5015             :         ZEND_ARG_INFO(0, method)
    5016             : ZEND_END_ARG_INFO()
    5017             : 
    5018             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_isvalidpharfilename, 0, 0, 1)
    5019             :         ZEND_ARG_INFO(0, filename)
    5020             :         ZEND_ARG_INFO(0, executable)
    5021             : ZEND_END_ARG_INFO()
    5022             : 
    5023             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_loadPhar, 0, 0, 1)
    5024             :         ZEND_ARG_INFO(0, filename)
    5025             :         ZEND_ARG_INFO(0, alias)
    5026             : ZEND_END_ARG_INFO()
    5027             : 
    5028             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_mapPhar, 0, 0, 0)
    5029             :         ZEND_ARG_INFO(0, alias)
    5030             :         ZEND_ARG_INFO(0, offset)
    5031             : ZEND_END_ARG_INFO()
    5032             : 
    5033             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_mount, 0, 0, 2)
    5034             :         ZEND_ARG_INFO(0, inphar)
    5035             :         ZEND_ARG_INFO(0, externalfile)
    5036             : ZEND_END_ARG_INFO()
    5037             : 
    5038             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_mungServer, 0, 0, 1)
    5039             :         ZEND_ARG_INFO(0, munglist)
    5040             : ZEND_END_ARG_INFO()
    5041             : 
    5042             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_webPhar, 0, 0, 0)
    5043             :         ZEND_ARG_INFO(0, alias)
    5044             :         ZEND_ARG_INFO(0, index)
    5045             :         ZEND_ARG_INFO(0, f404)
    5046             :         ZEND_ARG_INFO(0, mimetypes)
    5047             :         ZEND_ARG_INFO(0, rewrites)
    5048             : ZEND_END_ARG_INFO()
    5049             : 
    5050             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_running, 0, 0, 1)
    5051             :         ZEND_ARG_INFO(0, retphar)
    5052             : ZEND_END_ARG_INFO()
    5053             : 
    5054             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_ua, 0, 0, 1)
    5055             :         ZEND_ARG_INFO(0, archive)
    5056             : ZEND_END_ARG_INFO()
    5057             : 
    5058             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_build, 0, 0, 1)
    5059             :         ZEND_ARG_INFO(0, iterator)
    5060             :         ZEND_ARG_INFO(0, base_directory)
    5061             : ZEND_END_ARG_INFO()
    5062             : 
    5063             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_conv, 0, 0, 0)
    5064             :         ZEND_ARG_INFO(0, format)
    5065             :         ZEND_ARG_INFO(0, compression_type)
    5066             :         ZEND_ARG_INFO(0, file_ext)
    5067             : ZEND_END_ARG_INFO()
    5068             : 
    5069             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_comps, 0, 0, 1)
    5070             :         ZEND_ARG_INFO(0, compression_type)
    5071             :         ZEND_ARG_INFO(0, file_ext)
    5072             : ZEND_END_ARG_INFO()
    5073             : 
    5074             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_decomp, 0, 0, 0)
    5075             :         ZEND_ARG_INFO(0, file_ext)
    5076             : ZEND_END_ARG_INFO()
    5077             : 
    5078             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_comp, 0, 0, 1)
    5079             :         ZEND_ARG_INFO(0, compression_type)
    5080             : ZEND_END_ARG_INFO()
    5081             : 
    5082             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_compo, 0, 0, 0)
    5083             :         ZEND_ARG_INFO(0, compression_type)
    5084             : ZEND_END_ARG_INFO()
    5085             : 
    5086             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_copy, 0, 0, 2)
    5087             :         ZEND_ARG_INFO(0, newfile)
    5088             :         ZEND_ARG_INFO(0, oldfile)
    5089             : ZEND_END_ARG_INFO()
    5090             : 
    5091             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_delete, 0, 0, 1)
    5092             :         ZEND_ARG_INFO(0, entry)
    5093             : ZEND_END_ARG_INFO()
    5094             : 
    5095             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_fromdir, 0, 0, 1)
    5096             :         ZEND_ARG_INFO(0, base_dir)
    5097             :         ZEND_ARG_INFO(0, regex)
    5098             : ZEND_END_ARG_INFO()
    5099             : 
    5100             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_offsetExists, 0, 0, 1)
    5101             :         ZEND_ARG_INFO(0, entry)
    5102             : ZEND_END_ARG_INFO()
    5103             : 
    5104             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_offsetSet, 0, 0, 2)
    5105             :         ZEND_ARG_INFO(0, entry)
    5106             :         ZEND_ARG_INFO(0, value)
    5107             : ZEND_END_ARG_INFO()
    5108             : 
    5109             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_setAlias, 0, 0, 1)
    5110             :         ZEND_ARG_INFO(0, alias)
    5111             : ZEND_END_ARG_INFO()
    5112             : 
    5113             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_setMetadata, 0, 0, 1)
    5114             :         ZEND_ARG_INFO(0, metadata)
    5115             : ZEND_END_ARG_INFO()
    5116             : 
    5117             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_setSigAlgo, 0, 0, 1)
    5118             :         ZEND_ARG_INFO(0, algorithm)
    5119             :         ZEND_ARG_INFO(0, privatekey)
    5120             : ZEND_END_ARG_INFO()
    5121             : 
    5122             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_setStub, 0, 0, 1)
    5123             :         ZEND_ARG_INFO(0, newstub)
    5124             :         ZEND_ARG_INFO(0, maxlen)
    5125             : ZEND_END_ARG_INFO()
    5126             : 
    5127             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_emptydir, 0, 0, 0)
    5128             :         ZEND_ARG_INFO(0, dirname)
    5129             : ZEND_END_ARG_INFO()
    5130             : 
    5131             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_extract, 0, 0, 1)
    5132             :         ZEND_ARG_INFO(0, pathto)
    5133             :         ZEND_ARG_INFO(0, files)
    5134             :         ZEND_ARG_INFO(0, overwrite)
    5135             : ZEND_END_ARG_INFO()
    5136             : 
    5137             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_addfile, 0, 0, 1)
    5138             :         ZEND_ARG_INFO(0, filename)
    5139             :         ZEND_ARG_INFO(0, localname)
    5140             : ZEND_END_ARG_INFO()
    5141             : 
    5142             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_fromstring, 0, 0, 1)
    5143             :         ZEND_ARG_INFO(0, localname)
    5144             :         ZEND_ARG_INFO(0, contents)
    5145             : ZEND_END_ARG_INFO()
    5146             : 
    5147             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_isff, 0, 0, 1)
    5148             :         ZEND_ARG_INFO(0, fileformat)
    5149             : ZEND_END_ARG_INFO()
    5150             : 
    5151             : ZEND_BEGIN_ARG_INFO(arginfo_phar__void, 0)
    5152             : ZEND_END_ARG_INFO()
    5153             : 
    5154             : 
    5155             : zend_function_entry php_archive_methods[] = {
    5156             :         PHP_ME(Phar, __construct,           arginfo_phar___construct,  ZEND_ACC_PUBLIC)
    5157             :         PHP_ME(Phar, __destruct,            arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5158             :         PHP_ME(Phar, addEmptyDir,           arginfo_phar_emptydir,     ZEND_ACC_PUBLIC)
    5159             :         PHP_ME(Phar, addFile,               arginfo_phar_addfile,      ZEND_ACC_PUBLIC)
    5160             :         PHP_ME(Phar, addFromString,         arginfo_phar_fromstring,   ZEND_ACC_PUBLIC)
    5161             :         PHP_ME(Phar, buildFromDirectory,    arginfo_phar_fromdir,      ZEND_ACC_PUBLIC)
    5162             :         PHP_ME(Phar, buildFromIterator,     arginfo_phar_build,        ZEND_ACC_PUBLIC)
    5163             :         PHP_ME(Phar, compressFiles,         arginfo_phar_comp,         ZEND_ACC_PUBLIC)
    5164             :         PHP_ME(Phar, decompressFiles,       arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5165             :         PHP_ME(Phar, compress,              arginfo_phar_comps,        ZEND_ACC_PUBLIC)
    5166             :         PHP_ME(Phar, decompress,            arginfo_phar_decomp,       ZEND_ACC_PUBLIC)
    5167             :         PHP_ME(Phar, convertToExecutable,   arginfo_phar_conv,         ZEND_ACC_PUBLIC)
    5168             :         PHP_ME(Phar, convertToData,         arginfo_phar_conv,         ZEND_ACC_PUBLIC)
    5169             :         PHP_ME(Phar, copy,                  arginfo_phar_copy,         ZEND_ACC_PUBLIC)
    5170             :         PHP_ME(Phar, count,                 arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5171             :         PHP_ME(Phar, delete,                arginfo_phar_delete,       ZEND_ACC_PUBLIC)
    5172             :         PHP_ME(Phar, delMetadata,           arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5173             :         PHP_ME(Phar, extractTo,             arginfo_phar_extract,      ZEND_ACC_PUBLIC)
    5174             :         PHP_ME(Phar, getAlias,              arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5175             :         PHP_ME(Phar, getPath,               arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5176             :         PHP_ME(Phar, getMetadata,           arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5177             :         PHP_ME(Phar, getModified,           arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5178             :         PHP_ME(Phar, getSignature,          arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5179             :         PHP_ME(Phar, getStub,               arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5180             :         PHP_ME(Phar, getVersion,            arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5181             :         PHP_ME(Phar, hasMetadata,           arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5182             :         PHP_ME(Phar, isBuffering,           arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5183             :         PHP_ME(Phar, isCompressed,          arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5184             :         PHP_ME(Phar, isFileFormat,          arginfo_phar_isff,         ZEND_ACC_PUBLIC)
    5185             :         PHP_ME(Phar, isWritable,            arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5186             :         PHP_ME(Phar, offsetExists,          arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
    5187             :         PHP_ME(Phar, offsetGet,             arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
    5188             :         PHP_ME(Phar, offsetSet,             arginfo_phar_offsetSet,    ZEND_ACC_PUBLIC)
    5189             :         PHP_ME(Phar, offsetUnset,           arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
    5190             :         PHP_ME(Phar, setAlias,              arginfo_phar_setAlias,     ZEND_ACC_PUBLIC)
    5191             :         PHP_ME(Phar, setDefaultStub,        arginfo_phar_createDS,     ZEND_ACC_PUBLIC)
    5192             :         PHP_ME(Phar, setMetadata,           arginfo_phar_setMetadata,  ZEND_ACC_PUBLIC)
    5193             :         PHP_ME(Phar, setSignatureAlgorithm, arginfo_phar_setSigAlgo,   ZEND_ACC_PUBLIC)
    5194             :         PHP_ME(Phar, setStub,               arginfo_phar_setStub,      ZEND_ACC_PUBLIC)
    5195             :         PHP_ME(Phar, startBuffering,        arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5196             :         PHP_ME(Phar, stopBuffering,         arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5197             :         /* static member functions */
    5198             :         PHP_ME(Phar, apiVersion,            arginfo_phar__void,        ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5199             :         PHP_ME(Phar, canCompress,           arginfo_phar_cancompress,  ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5200             :         PHP_ME(Phar, canWrite,              arginfo_phar__void,        ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5201             :         PHP_ME(Phar, createDefaultStub,     arginfo_phar_createDS,     ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5202             :         PHP_ME(Phar, getSupportedCompression,arginfo_phar__void,       ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5203             :         PHP_ME(Phar, getSupportedSignatures,arginfo_phar__void,        ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5204             :         PHP_ME(Phar, interceptFileFuncs,    arginfo_phar__void,        ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5205             :         PHP_ME(Phar, isValidPharFilename,   arginfo_phar_isvalidpharfilename, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5206             :         PHP_ME(Phar, loadPhar,              arginfo_phar_loadPhar,     ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5207             :         PHP_ME(Phar, mapPhar,               arginfo_phar_mapPhar,      ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5208             :         PHP_ME(Phar, running,               arginfo_phar_running,      ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5209             :         PHP_ME(Phar, mount,                 arginfo_phar_mount,        ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5210             :         PHP_ME(Phar, mungServer,            arginfo_phar_mungServer,   ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5211             :         PHP_ME(Phar, unlinkArchive,         arginfo_phar_ua,           ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5212             :         PHP_ME(Phar, webPhar,               arginfo_phar_webPhar,      ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5213             :         PHP_FE_END
    5214             : };
    5215             : 
    5216             : ZEND_BEGIN_ARG_INFO_EX(arginfo_entry___construct, 0, 0, 1)
    5217             :         ZEND_ARG_INFO(0, filename)
    5218             : ZEND_END_ARG_INFO()
    5219             : 
    5220             : ZEND_BEGIN_ARG_INFO_EX(arginfo_entry_chmod, 0, 0, 1)
    5221             :         ZEND_ARG_INFO(0, perms)
    5222             : ZEND_END_ARG_INFO()
    5223             : 
    5224             : zend_function_entry php_entry_methods[] = {
    5225             :         PHP_ME(PharFileInfo, __construct,        arginfo_entry___construct,  ZEND_ACC_PUBLIC)
    5226             :         PHP_ME(PharFileInfo, __destruct,         arginfo_phar__void,         ZEND_ACC_PUBLIC)
    5227             :         PHP_ME(PharFileInfo, chmod,              arginfo_entry_chmod,        ZEND_ACC_PUBLIC)
    5228             :         PHP_ME(PharFileInfo, compress,           arginfo_phar_comp,          ZEND_ACC_PUBLIC)
    5229             :         PHP_ME(PharFileInfo, decompress,         arginfo_phar__void,         ZEND_ACC_PUBLIC)
    5230             :         PHP_ME(PharFileInfo, delMetadata,        arginfo_phar__void,         ZEND_ACC_PUBLIC)
    5231             :         PHP_ME(PharFileInfo, getCompressedSize,  arginfo_phar__void,         ZEND_ACC_PUBLIC)
    5232             :         PHP_ME(PharFileInfo, getCRC32,           arginfo_phar__void,         ZEND_ACC_PUBLIC)
    5233             :         PHP_ME(PharFileInfo, getContent,         arginfo_phar__void,         ZEND_ACC_PUBLIC)
    5234             :         PHP_ME(PharFileInfo, getMetadata,        arginfo_phar__void,         ZEND_ACC_PUBLIC)
    5235             :         PHP_ME(PharFileInfo, getPharFlags,       arginfo_phar__void,         ZEND_ACC_PUBLIC)
    5236             :         PHP_ME(PharFileInfo, hasMetadata,        arginfo_phar__void,         ZEND_ACC_PUBLIC)
    5237             :         PHP_ME(PharFileInfo, isCompressed,       arginfo_phar_compo,         ZEND_ACC_PUBLIC)
    5238             :         PHP_ME(PharFileInfo, isCRCChecked,       arginfo_phar__void,         ZEND_ACC_PUBLIC)
    5239             :         PHP_ME(PharFileInfo, setMetadata,        arginfo_phar_setMetadata,   ZEND_ACC_PUBLIC)
    5240             :         PHP_FE_END
    5241             : };
    5242             : 
    5243             : zend_function_entry phar_exception_methods[] = {
    5244             :         PHP_FE_END
    5245             : };
    5246             : /* }}} */
    5247             : 
    5248             : #define REGISTER_PHAR_CLASS_CONST_LONG(class_name, const_name, value) \
    5249             :         zend_declare_class_constant_long(class_name, const_name, sizeof(const_name)-1, (zend_long)value);
    5250             : 
    5251       23950 : void phar_object_init(void) /* {{{ */
    5252             : {
    5253             :         zend_class_entry ce;
    5254             : 
    5255       23950 :         INIT_CLASS_ENTRY(ce, "PharException", phar_exception_methods);
    5256       23950 :         phar_ce_PharException = zend_register_internal_class_ex(&ce, zend_ce_exception);
    5257             : 
    5258       23950 :         INIT_CLASS_ENTRY(ce, "Phar", php_archive_methods);
    5259       23950 :         phar_ce_archive = zend_register_internal_class_ex(&ce, spl_ce_RecursiveDirectoryIterator);
    5260             : 
    5261       23950 :         zend_class_implements(phar_ce_archive, 2, spl_ce_Countable, zend_ce_arrayaccess);
    5262             : 
    5263       23950 :         INIT_CLASS_ENTRY(ce, "PharData", php_archive_methods);
    5264       23950 :         phar_ce_data = zend_register_internal_class_ex(&ce, spl_ce_RecursiveDirectoryIterator);
    5265             : 
    5266       23950 :         zend_class_implements(phar_ce_data, 2, spl_ce_Countable, zend_ce_arrayaccess);
    5267             : 
    5268       23950 :         INIT_CLASS_ENTRY(ce, "PharFileInfo", php_entry_methods);
    5269       23950 :         phar_ce_entry = zend_register_internal_class_ex(&ce, spl_ce_SplFileInfo);
    5270             : 
    5271       23950 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "BZ2", PHAR_ENT_COMPRESSED_BZ2)
    5272       23950 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "GZ", PHAR_ENT_COMPRESSED_GZ)
    5273       23950 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "NONE", PHAR_ENT_COMPRESSED_NONE)
    5274       23950 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "PHAR", PHAR_FORMAT_PHAR)
    5275       23950 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "TAR", PHAR_FORMAT_TAR)
    5276       23950 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "ZIP", PHAR_FORMAT_ZIP)
    5277       23950 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "COMPRESSED", PHAR_ENT_COMPRESSION_MASK)
    5278       23950 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "PHP", PHAR_MIME_PHP)
    5279       23950 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "PHPS", PHAR_MIME_PHPS)
    5280       23950 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "MD5", PHAR_SIG_MD5)
    5281       23950 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "OPENSSL", PHAR_SIG_OPENSSL)
    5282       23950 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA1", PHAR_SIG_SHA1)
    5283       23950 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA256", PHAR_SIG_SHA256)
    5284       23950 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA512", PHAR_SIG_SHA512)
    5285       23950 : }
    5286             : /* }}} */
    5287             : 
    5288             : /*
    5289             :  * Local variables:
    5290             :  * tab-width: 4
    5291             :  * c-basic-offset: 4
    5292             :  * End:
    5293             :  * vim600: noet sw=4 ts=4 fdm=marker
    5294             :  * vim<600: noet sw=4 ts=4
    5295             :  */

Generated by: LCOV version 1.10

Generated at Tue, 27 Sep 2016 10:26:05 +0000 (40 hours ago)

Copyright © 2005-2016 The PHP Group
All rights reserved.