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