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