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