1 /*
2 +----------------------------------------------------------------------+
3 | phar php single-file executable PHP extension |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2005-2015 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(entry, entry_len);
344 PHPWRITE(" Access Denied</h1>\n </body>\n</html>", sizeof(" Access Denied</h1>\n </body>\n</html>") - 1);
345 }
346 /* }}} */
347
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 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) /* {{{ */
349 {
350 sapi_header_line ctr = {0};
351 phar_entry_info *info;
352
353 if (phar && f404_len) {
354 info = phar_get_entry_info(phar, f404, f404_len, NULL, 1 TSRMLS_CC);
355
356 if (info) {
357 phar_file_action(phar, info, "text/html", PHAR_MIME_PHP, f404, f404_len, fname, NULL, NULL, 0 TSRMLS_CC);
358 return;
359 }
360 }
361
362 ctr.response_code = 404;
363 ctr.line_len = sizeof("HTTP/1.0 404 Not Found")-1;
364 ctr.line = "HTTP/1.0 404 Not Found";
365 sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
366 sapi_send_headers(TSRMLS_C);
367 PHPWRITE("<html>\n <head>\n <title>File Not Found</title>\n </head>\n <body>\n <h1>404 - File ", sizeof("<html>\n <head>\n <title>File Not Found</title>\n </head>\n <body>\n <h1>404 - File ") - 1);
368 PHPWRITE(entry, entry_len);
369 PHPWRITE(" Not Found</h1>\n </body>\n</html>", sizeof(" Not Found</h1>\n </body>\n</html>") - 1);
370 }
371 /* }}} */
372
373 /* post-process REQUEST_URI and retrieve the actual request URI. This is for
374 cases like http://localhost/blah.phar/path/to/file.php/extra/stuff
375 which calls "blah.phar" file "path/to/file.php" with PATH_INFO "/extra/stuff" */
phar_postprocess_ru_web(char * fname,int fname_len,char ** entry,int * entry_len,char ** ru,int * ru_len TSRMLS_DC)376 static void phar_postprocess_ru_web(char *fname, int fname_len, char **entry, int *entry_len, char **ru, int *ru_len TSRMLS_DC) /* {{{ */
377 {
378 char *e = *entry + 1, *u = NULL, *u1 = NULL, *saveu = NULL;
379 int e_len = *entry_len - 1, u_len = 0;
380 phar_archive_data **pphar = NULL;
381
382 /* we already know we can retrieve the phar if we reach here */
383 zend_hash_find(&(PHAR_GLOBALS->phar_fname_map), fname, fname_len, (void **) &pphar);
384
385 if (!pphar && PHAR_G(manifest_cached)) {
386 zend_hash_find(&cached_phars, fname, fname_len, (void **) &pphar);
387 }
388
389 do {
390 if (zend_hash_exists(&((*pphar)->manifest), e, e_len)) {
391 if (u) {
392 u[0] = '/';
393 *ru = estrndup(u, u_len+1);
394 ++u_len;
395 u[0] = '\0';
396 } else {
397 *ru = NULL;
398 }
399 *ru_len = u_len;
400 *entry_len = e_len + 1;
401 return;
402 }
403
404 if (u) {
405 u1 = strrchr(e, '/');
406 u[0] = '/';
407 saveu = u;
408 e_len += u_len + 1;
409 u = u1;
410 if (!u) {
411 return;
412 }
413 } else {
414 u = strrchr(e, '/');
415 if (!u) {
416 if (saveu) {
417 saveu[0] = '/';
418 }
419 return;
420 }
421 }
422
423 u[0] = '\0';
424 u_len = strlen(u + 1);
425 e_len -= u_len + 1;
426
427 if (e_len < 0) {
428 if (saveu) {
429 saveu[0] = '/';
430 }
431 return;
432 }
433 } while (1);
434 }
435 /* }}} */
436
437 /* {{{ proto void Phar::running([bool retphar = true])
438 * return the name of the currently running phar archive. If the optional parameter
439 * is set to true, return the phar:// URL to the currently running phar
440 */
PHP_METHOD(Phar,running)441 PHP_METHOD(Phar, running)
442 {
443 char *fname, *arch, *entry;
444 int fname_len, arch_len, entry_len;
445 zend_bool retphar = 1;
446
447 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &retphar) == FAILURE) {
448 return;
449 }
450
451 fname = (char*)zend_get_executed_filename(TSRMLS_C);
452 fname_len = strlen(fname);
453
454 if (fname_len > 7 && !memcmp(fname, "phar://", 7) && SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0 TSRMLS_CC)) {
455 efree(entry);
456 if (retphar) {
457 RETVAL_STRINGL(fname, arch_len + 7, 1);
458 efree(arch);
459 return;
460 } else {
461 RETURN_STRINGL(arch, arch_len, 0);
462 }
463 }
464
465 RETURN_STRINGL("", 0, 1);
466 }
467 /* }}} */
468
469 /* {{{ proto void Phar::mount(string pharpath, string externalfile)
470 * mount an external file or path to a location within the phar. This maps
471 * an external file or directory to a location within the phar archive, allowing
472 * reference to an external location as if it were within the phar archive. This
473 * is useful for writable temp files like databases
474 */
PHP_METHOD(Phar,mount)475 PHP_METHOD(Phar, mount)
476 {
477 char *fname, *arch = NULL, *entry = NULL, *path, *actual;
478 int fname_len, arch_len, entry_len, path_len, actual_len;
479 phar_archive_data **pphar;
480
481 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "pp", &path, &path_len, &actual, &actual_len) == FAILURE) {
482 return;
483 }
484
485 fname = (char*)zend_get_executed_filename(TSRMLS_C);
486 fname_len = strlen(fname);
487
488 #ifdef PHP_WIN32
489 phar_unixify_path_separators(fname, fname_len);
490 #endif
491
492 if (fname_len > 7 && !memcmp(fname, "phar://", 7) && SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0 TSRMLS_CC)) {
493 efree(entry);
494 entry = NULL;
495
496 if (path_len > 7 && !memcmp(path, "phar://", 7)) {
497 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Can only mount internal paths within a phar archive, use a relative path instead of \"%s\"", path);
498 efree(arch);
499 return;
500 }
501 carry_on2:
502 if (SUCCESS != zend_hash_find(&(PHAR_GLOBALS->phar_fname_map), arch, arch_len, (void **)&pphar)) {
503 if (PHAR_G(manifest_cached) && SUCCESS == zend_hash_find(&cached_phars, arch, arch_len, (void **)&pphar)) {
504 if (SUCCESS == phar_copy_on_write(pphar TSRMLS_CC)) {
505 goto carry_on;
506 }
507 }
508
509 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s is not a phar archive, cannot mount", arch);
510
511 if (arch) {
512 efree(arch);
513 }
514 return;
515 }
516 carry_on:
517 if (SUCCESS != phar_mount_entry(*pphar, actual, actual_len, path, path_len TSRMLS_CC)) {
518 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Mounting of %s to %s within phar %s failed", path, actual, arch);
519 if (path && path == entry) {
520 efree(entry);
521 }
522
523 if (arch) {
524 efree(arch);
525 }
526
527 return;
528 }
529
530 if (entry && path && path == entry) {
531 efree(entry);
532 }
533
534 if (arch) {
535 efree(arch);
536 }
537
538 return;
539 } else if (PHAR_GLOBALS->phar_fname_map.arBuckets && SUCCESS == zend_hash_find(&(PHAR_GLOBALS->phar_fname_map), fname, fname_len, (void **)&pphar)) {
540 goto carry_on;
541 } else if (PHAR_G(manifest_cached) && SUCCESS == zend_hash_find(&cached_phars, fname, fname_len, (void **)&pphar)) {
542 if (SUCCESS == phar_copy_on_write(pphar TSRMLS_CC)) {
543 goto carry_on;
544 }
545
546 goto carry_on;
547 } else if (SUCCESS == phar_split_fname(path, path_len, &arch, &arch_len, &entry, &entry_len, 2, 0 TSRMLS_CC)) {
548 path = entry;
549 path_len = entry_len;
550 goto carry_on2;
551 }
552
553 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Mounting of %s to %s failed", path, actual);
554 }
555 /* }}} */
556
557 /* {{{ proto void Phar::webPhar([string alias, [string index, [string f404, [array mimetypes, [callback rewrites]]]]])
558 * mapPhar for web-based phars. Reads the currently executed file (a phar)
559 * and registers its manifest. When executed in the CLI or CGI command-line sapi,
560 * this works exactly like mapPhar(). When executed by a web-based sapi, this
561 * reads $_SERVER['REQUEST_URI'] (the actual original value) and parses out the
562 * intended internal file.
563 */
PHP_METHOD(Phar,webPhar)564 PHP_METHOD(Phar, webPhar)
565 {
566 zval *mimeoverride = NULL, *rewrite = NULL;
567 char *alias = NULL, *error, *index_php = NULL, *f404 = NULL, *ru = NULL;
568 int alias_len = 0, ret, f404_len = 0, free_pathinfo = 0, ru_len = 0;
569 char *fname, *path_info, *mime_type = NULL, *entry, *pt;
570 const char *basename;
571 int fname_len, entry_len, code, index_php_len = 0, not_cgi;
572 phar_archive_data *phar = NULL;
573 phar_entry_info *info = NULL;
574
575 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!saz", &alias, &alias_len, &index_php, &index_php_len, &f404, &f404_len, &mimeoverride, &rewrite) == FAILURE) {
576 return;
577 }
578
579 phar_request_initialize(TSRMLS_C);
580 fname = (char*)zend_get_executed_filename(TSRMLS_C);
581 fname_len = strlen(fname);
582
583 if (phar_open_executed_filename(alias, alias_len, &error TSRMLS_CC) != SUCCESS) {
584 if (error) {
585 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
586 efree(error);
587 }
588 return;
589 }
590
591 /* retrieve requested file within phar */
592 if (!(SG(request_info).request_method && SG(request_info).request_uri && (!strcmp(SG(request_info).request_method, "GET") || !strcmp(SG(request_info).request_method, "POST")))) {
593 return;
594 }
595
596 #ifdef PHP_WIN32
597 fname = estrndup(fname, fname_len);
598 phar_unixify_path_separators(fname, fname_len);
599 #endif
600 basename = zend_memrchr(fname, '/', fname_len);
601
602 if (!basename) {
603 basename = fname;
604 } else {
605 ++basename;
606 }
607
608 if ((strlen(sapi_module.name) == sizeof("cgi-fcgi")-1 && !strncmp(sapi_module.name, "cgi-fcgi", sizeof("cgi-fcgi")-1))
609 || (strlen(sapi_module.name) == sizeof("fpm-fcgi")-1 && !strncmp(sapi_module.name, "fpm-fcgi", sizeof("fpm-fcgi")-1))
610 || (strlen(sapi_module.name) == sizeof("cgi")-1 && !strncmp(sapi_module.name, "cgi", sizeof("cgi")-1))) {
611
612 if (PG(http_globals)[TRACK_VARS_SERVER]) {
613 HashTable *_server = Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]);
614 zval **z_script_name, **z_path_info;
615
616 if (SUCCESS != zend_hash_find(_server, "SCRIPT_NAME", sizeof("SCRIPT_NAME"), (void**)&z_script_name) ||
617 IS_STRING != Z_TYPE_PP(z_script_name) ||
618 !strstr(Z_STRVAL_PP(z_script_name), basename)) {
619 return;
620 }
621
622 if (SUCCESS == zend_hash_find(_server, "PATH_INFO", sizeof("PATH_INFO"), (void**)&z_path_info) &&
623 IS_STRING == Z_TYPE_PP(z_path_info)) {
624 entry_len = Z_STRLEN_PP(z_path_info);
625 entry = estrndup(Z_STRVAL_PP(z_path_info), entry_len);
626 path_info = emalloc(Z_STRLEN_PP(z_script_name) + entry_len + 1);
627 memcpy(path_info, Z_STRVAL_PP(z_script_name), Z_STRLEN_PP(z_script_name));
628 memcpy(path_info + Z_STRLEN_PP(z_script_name), entry, entry_len + 1);
629 free_pathinfo = 1;
630 } else {
631 entry_len = 0;
632 entry = estrndup("", 0);
633 path_info = Z_STRVAL_PP(z_script_name);
634 }
635
636 pt = estrndup(Z_STRVAL_PP(z_script_name), Z_STRLEN_PP(z_script_name));
637
638 } else {
639 char *testit;
640
641 testit = sapi_getenv("SCRIPT_NAME", sizeof("SCRIPT_NAME")-1 TSRMLS_CC);
642 if (!(pt = strstr(testit, basename))) {
643 efree(testit);
644 return;
645 }
646
647 path_info = sapi_getenv("PATH_INFO", sizeof("PATH_INFO")-1 TSRMLS_CC);
648
649 if (path_info) {
650 entry = path_info;
651 entry_len = strlen(entry);
652 spprintf(&path_info, 0, "%s%s", testit, path_info);
653 free_pathinfo = 1;
654 } else {
655 path_info = testit;
656 free_pathinfo = 1;
657 entry = estrndup("", 0);
658 entry_len = 0;
659 }
660
661 pt = estrndup(testit, (pt - testit) + (fname_len - (basename - fname)));
662 }
663 not_cgi = 0;
664 } else {
665 path_info = SG(request_info).request_uri;
666
667 if (!(pt = strstr(path_info, basename))) {
668 /* this can happen with rewrite rules - and we have no idea what to do then, so return */
669 return;
670 }
671
672 entry_len = strlen(path_info);
673 entry_len -= (pt - path_info) + (fname_len - (basename - fname));
674 entry = estrndup(pt + (fname_len - (basename - fname)), entry_len);
675
676 pt = estrndup(path_info, (pt - path_info) + (fname_len - (basename - fname)));
677 not_cgi = 1;
678 }
679
680 if (rewrite) {
681 zend_fcall_info fci;
682 zend_fcall_info_cache fcc;
683 zval *params, *retval_ptr, **zp[1];
684
685 MAKE_STD_ZVAL(params);
686 ZVAL_STRINGL(params, entry, entry_len, 1);
687 zp[0] = ¶ms;
688
689 if (FAILURE == zend_fcall_info_init(rewrite, 0, &fci, &fcc, NULL, NULL TSRMLS_CC)) {
690 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar error: invalid rewrite callback");
691
692 if (free_pathinfo) {
693 efree(path_info);
694 }
695
696 return;
697 }
698
699 fci.param_count = 1;
700 fci.params = zp;
701 Z_ADDREF_P(params);
702 fci.retval_ptr_ptr = &retval_ptr;
703
704 if (FAILURE == zend_call_function(&fci, &fcc TSRMLS_CC)) {
705 if (!EG(exception)) {
706 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar error: failed to call rewrite callback");
707 }
708
709 if (free_pathinfo) {
710 efree(path_info);
711 }
712
713 return;
714 }
715
716 if (!fci.retval_ptr_ptr || !retval_ptr) {
717 if (free_pathinfo) {
718 efree(path_info);
719 }
720 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar error: rewrite callback must return a string or false");
721 return;
722 }
723
724 switch (Z_TYPE_P(retval_ptr)) {
725 case IS_STRING:
726 efree(entry);
727
728 if (fci.retval_ptr_ptr != &retval_ptr) {
729 entry = estrndup(Z_STRVAL_PP(fci.retval_ptr_ptr), Z_STRLEN_PP(fci.retval_ptr_ptr));
730 entry_len = Z_STRLEN_PP(fci.retval_ptr_ptr);
731 } else {
732 entry = Z_STRVAL_P(retval_ptr);
733 entry_len = Z_STRLEN_P(retval_ptr);
734 }
735
736 break;
737 case IS_BOOL:
738 phar_do_403(entry, entry_len TSRMLS_CC);
739
740 if (free_pathinfo) {
741 efree(path_info);
742 }
743
744 zend_bailout();
745 return;
746 default:
747 efree(retval_ptr);
748
749 if (free_pathinfo) {
750 efree(path_info);
751 }
752
753 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar error: rewrite callback must return a string or false");
754 return;
755 }
756 }
757
758 if (entry_len) {
759 phar_postprocess_ru_web(fname, fname_len, &entry, &entry_len, &ru, &ru_len TSRMLS_CC);
760 }
761
762 if (!entry_len || (entry_len == 1 && entry[0] == '/')) {
763 efree(entry);
764 /* direct request */
765 if (index_php_len) {
766 entry = index_php;
767 entry_len = index_php_len;
768 if (entry[0] != '/') {
769 spprintf(&entry, 0, "/%s", index_php);
770 ++entry_len;
771 }
772 } else {
773 /* assume "index.php" is starting point */
774 entry = estrndup("/index.php", sizeof("/index.php"));
775 entry_len = sizeof("/index.php")-1;
776 }
777
778 if (FAILURE == phar_get_archive(&phar, fname, fname_len, NULL, 0, NULL TSRMLS_CC) ||
779 (info = phar_get_entry_info(phar, entry, entry_len, NULL, 0 TSRMLS_CC)) == NULL) {
780 phar_do_404(phar, fname, fname_len, f404, f404_len, entry, entry_len TSRMLS_CC);
781
782 if (free_pathinfo) {
783 efree(path_info);
784 }
785
786 zend_bailout();
787 } else {
788 char *tmp = NULL, sa = '\0';
789 sapi_header_line ctr = {0};
790 ctr.response_code = 301;
791 ctr.line_len = sizeof("HTTP/1.1 301 Moved Permanently")-1;
792 ctr.line = "HTTP/1.1 301 Moved Permanently";
793 sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
794
795 if (not_cgi) {
796 tmp = strstr(path_info, basename) + fname_len;
797 sa = *tmp;
798 *tmp = '\0';
799 }
800
801 ctr.response_code = 0;
802
803 if (path_info[strlen(path_info)-1] == '/') {
804 ctr.line_len = spprintf(&(ctr.line), 4096, "Location: %s%s", path_info, entry + 1);
805 } else {
806 ctr.line_len = spprintf(&(ctr.line), 4096, "Location: %s%s", path_info, entry);
807 }
808
809 if (not_cgi) {
810 *tmp = sa;
811 }
812
813 if (free_pathinfo) {
814 efree(path_info);
815 }
816
817 sapi_header_op(SAPI_HEADER_REPLACE, &ctr TSRMLS_CC);
818 sapi_send_headers(TSRMLS_C);
819 efree(ctr.line);
820 zend_bailout();
821 }
822 }
823
824 if (FAILURE == phar_get_archive(&phar, fname, fname_len, NULL, 0, NULL TSRMLS_CC) ||
825 (info = phar_get_entry_info(phar, entry, entry_len, NULL, 0 TSRMLS_CC)) == NULL) {
826 phar_do_404(phar, fname, fname_len, f404, f404_len, entry, entry_len TSRMLS_CC);
827 #ifdef PHP_WIN32
828 efree(fname);
829 #endif
830 zend_bailout();
831 }
832
833 if (mimeoverride && zend_hash_num_elements(Z_ARRVAL_P(mimeoverride))) {
834 const char *ext = zend_memrchr(entry, '.', entry_len);
835 zval **val;
836
837 if (ext) {
838 ++ext;
839
840 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(mimeoverride), ext, strlen(ext)+1, (void **) &val)) {
841 switch (Z_TYPE_PP(val)) {
842 case IS_LONG:
843 if (Z_LVAL_PP(val) == PHAR_MIME_PHP || Z_LVAL_PP(val) == PHAR_MIME_PHPS) {
844 mime_type = "";
845 code = Z_LVAL_PP(val);
846 } else {
847 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");
848 #ifdef PHP_WIN32
849 efree(fname);
850 #endif
851 RETURN_FALSE;
852 }
853 break;
854 case IS_STRING:
855 mime_type = Z_STRVAL_PP(val);
856 code = PHAR_MIME_OTHER;
857 break;
858 default:
859 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");
860 #ifdef PHP_WIN32
861 efree(fname);
862 #endif
863 RETURN_FALSE;
864 }
865 }
866 }
867 }
868
869 if (!mime_type) {
870 code = phar_file_type(&PHAR_G(mime_types), entry, &mime_type TSRMLS_CC);
871 }
872 ret = phar_file_action(phar, info, mime_type, code, entry, entry_len, fname, pt, ru, ru_len TSRMLS_CC);
873 }
874 /* }}} */
875
876 /* {{{ proto void Phar::mungServer(array munglist)
877 * Defines a list of up to 4 $_SERVER variables that should be modified for execution
878 * to mask the presence of the phar archive. This should be used in conjunction with
879 * Phar::webPhar(), and has no effect otherwise
880 * SCRIPT_NAME, PHP_SELF, REQUEST_URI and SCRIPT_FILENAME
881 */
PHP_METHOD(Phar,mungServer)882 PHP_METHOD(Phar, mungServer)
883 {
884 zval *mungvalues;
885
886 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &mungvalues) == FAILURE) {
887 return;
888 }
889
890 if (!zend_hash_num_elements(Z_ARRVAL_P(mungvalues))) {
891 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");
892 return;
893 }
894
895 if (zend_hash_num_elements(Z_ARRVAL_P(mungvalues)) > 4) {
896 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");
897 return;
898 }
899
900 phar_request_initialize(TSRMLS_C);
901
902 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))) {
903 zval **data = NULL;
904
905 if (SUCCESS != zend_hash_get_current_data(Z_ARRVAL_P(mungvalues), (void **) &data)) {
906 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "unable to retrieve array value in Phar::mungServer()");
907 return;
908 }
909
910 if (Z_TYPE_PP(data) != IS_STRING) {
911 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");
912 return;
913 }
914
915 if (Z_STRLEN_PP(data) == sizeof("PHP_SELF")-1 && !strncmp(Z_STRVAL_PP(data), "PHP_SELF", sizeof("PHP_SELF")-1)) {
916 PHAR_GLOBALS->phar_SERVER_mung_list |= PHAR_MUNG_PHP_SELF;
917 }
918
919 if (Z_STRLEN_PP(data) == sizeof("REQUEST_URI")-1) {
920 if (!strncmp(Z_STRVAL_PP(data), "REQUEST_URI", sizeof("REQUEST_URI")-1)) {
921 PHAR_GLOBALS->phar_SERVER_mung_list |= PHAR_MUNG_REQUEST_URI;
922 }
923 if (!strncmp(Z_STRVAL_PP(data), "SCRIPT_NAME", sizeof("SCRIPT_NAME")-1)) {
924 PHAR_GLOBALS->phar_SERVER_mung_list |= PHAR_MUNG_SCRIPT_NAME;
925 }
926 }
927
928 if (Z_STRLEN_PP(data) == sizeof("SCRIPT_FILENAME")-1 && !strncmp(Z_STRVAL_PP(data), "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME")-1)) {
929 PHAR_GLOBALS->phar_SERVER_mung_list |= PHAR_MUNG_SCRIPT_FILENAME;
930 }
931 }
932 }
933 /* }}} */
934
935 /* {{{ proto void Phar::interceptFileFuncs()
936 * instructs phar to intercept fopen, file_get_contents, opendir, and all of the stat-related functions
937 * and return stat on files within the phar for relative paths
938 *
939 * Once called, this cannot be reversed, and continue until the end of the request.
940 *
941 * This allows legacy scripts to be pharred unmodified
942 */
PHP_METHOD(Phar,interceptFileFuncs)943 PHP_METHOD(Phar, interceptFileFuncs)
944 {
945 if (zend_parse_parameters_none() == FAILURE) {
946 return;
947 }
948 phar_intercept_functions(TSRMLS_C);
949 }
950 /* }}} */
951
952 /* {{{ proto array Phar::createDefaultStub([string indexfile[, string webindexfile]])
953 * Return a stub that can be used to run a phar-based archive without the phar extension
954 * indexfile is the CLI startup filename, which defaults to "index.php", webindexfile
955 * is the web startup filename, and also defaults to "index.php"
956 */
PHP_METHOD(Phar,createDefaultStub)957 PHP_METHOD(Phar, createDefaultStub)
958 {
959 char *index = NULL, *webindex = NULL, *stub, *error;
960 int index_len = 0, webindex_len = 0;
961 size_t stub_len;
962
963 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|pp", &index, &index_len, &webindex, &webindex_len) == FAILURE) {
964 return;
965 }
966
967 stub = phar_create_default_stub(index, webindex, &stub_len, &error TSRMLS_CC);
968
969 if (error) {
970 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
971 efree(error);
972 return;
973 }
974 RETURN_STRINGL(stub, stub_len, 0);
975 }
976 /* }}} */
977
978 /* {{{ proto mixed Phar::mapPhar([string alias, [int dataoffset]])
979 * Reads the currently executed file (a phar) and registers its manifest */
PHP_METHOD(Phar,mapPhar)980 PHP_METHOD(Phar, mapPhar)
981 {
982 char *alias = NULL, *error;
983 int alias_len = 0;
984 long dataoffset = 0;
985
986 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!l", &alias, &alias_len, &dataoffset) == FAILURE) {
987 return;
988 }
989
990 phar_request_initialize(TSRMLS_C);
991
992 RETVAL_BOOL(phar_open_executed_filename(alias, alias_len, &error TSRMLS_CC) == SUCCESS);
993
994 if (error) {
995 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
996 efree(error);
997 }
998 } /* }}} */
999
1000 /* {{{ proto mixed Phar::loadPhar(string filename [, string alias])
1001 * Loads any phar archive with an alias */
PHP_METHOD(Phar,loadPhar)1002 PHP_METHOD(Phar, loadPhar)
1003 {
1004 char *fname, *alias = NULL, *error;
1005 int fname_len, alias_len = 0;
1006
1007 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|s!", &fname, &fname_len, &alias, &alias_len) == FAILURE) {
1008 return;
1009 }
1010
1011 phar_request_initialize(TSRMLS_C);
1012
1013 RETVAL_BOOL(phar_open_from_filename(fname, fname_len, alias, alias_len, REPORT_ERRORS, NULL, &error TSRMLS_CC) == SUCCESS);
1014
1015 if (error) {
1016 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
1017 efree(error);
1018 }
1019 } /* }}} */
1020
1021 /* {{{ proto string Phar::apiVersion()
1022 * Returns the api version */
PHP_METHOD(Phar,apiVersion)1023 PHP_METHOD(Phar, apiVersion)
1024 {
1025 if (zend_parse_parameters_none() == FAILURE) {
1026 return;
1027 }
1028 RETURN_STRINGL(PHP_PHAR_API_VERSION, sizeof(PHP_PHAR_API_VERSION)-1, 1);
1029 }
1030 /* }}}*/
1031
1032 /* {{{ proto bool Phar::canCompress([int method])
1033 * Returns whether phar extension supports compression using zlib/bzip2 */
PHP_METHOD(Phar,canCompress)1034 PHP_METHOD(Phar, canCompress)
1035 {
1036 long method = 0;
1037
1038 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &method) == FAILURE) {
1039 return;
1040 }
1041
1042 phar_request_initialize(TSRMLS_C);
1043 switch (method) {
1044 case PHAR_ENT_COMPRESSED_GZ:
1045 if (PHAR_G(has_zlib)) {
1046 RETURN_TRUE;
1047 } else {
1048 RETURN_FALSE;
1049 }
1050 case PHAR_ENT_COMPRESSED_BZ2:
1051 if (PHAR_G(has_bz2)) {
1052 RETURN_TRUE;
1053 } else {
1054 RETURN_FALSE;
1055 }
1056 default:
1057 if (PHAR_G(has_zlib) || PHAR_G(has_bz2)) {
1058 RETURN_TRUE;
1059 } else {
1060 RETURN_FALSE;
1061 }
1062 }
1063 }
1064 /* }}} */
1065
1066 /* {{{ proto bool Phar::canWrite()
1067 * Returns whether phar extension supports writing and creating phars */
PHP_METHOD(Phar,canWrite)1068 PHP_METHOD(Phar, canWrite)
1069 {
1070 if (zend_parse_parameters_none() == FAILURE) {
1071 return;
1072 }
1073 RETURN_BOOL(!PHAR_G(readonly));
1074 }
1075 /* }}} */
1076
1077 /* {{{ proto bool Phar::isValidPharFilename(string filename[, bool executable = true])
1078 * Returns whether the given filename is a valid phar filename */
PHP_METHOD(Phar,isValidPharFilename)1079 PHP_METHOD(Phar, isValidPharFilename)
1080 {
1081 char *fname;
1082 const char *ext_str;
1083 int fname_len, ext_len, is_executable;
1084 zend_bool executable = 1;
1085
1086 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|b", &fname, &fname_len, &executable) == FAILURE) {
1087 return;
1088 }
1089
1090 is_executable = executable;
1091 RETVAL_BOOL(phar_detect_phar_fname_ext(fname, fname_len, &ext_str, &ext_len, is_executable, 2, 1 TSRMLS_CC) == SUCCESS);
1092 }
1093 /* }}} */
1094
1095 #if HAVE_SPL
1096 /**
1097 * from spl_directory
1098 */
phar_spl_foreign_dtor(spl_filesystem_object * object TSRMLS_DC)1099 static void phar_spl_foreign_dtor(spl_filesystem_object *object TSRMLS_DC) /* {{{ */
1100 {
1101 phar_archive_data *phar = (phar_archive_data *) object->oth;
1102
1103 if (!phar->is_persistent) {
1104 phar_archive_delref(phar TSRMLS_CC);
1105 }
1106
1107 object->oth = NULL;
1108 }
1109 /* }}} */
1110
1111 /**
1112 * from spl_directory
1113 */
phar_spl_foreign_clone(spl_filesystem_object * src,spl_filesystem_object * dst TSRMLS_DC)1114 static void phar_spl_foreign_clone(spl_filesystem_object *src, spl_filesystem_object *dst TSRMLS_DC) /* {{{ */
1115 {
1116 phar_archive_data *phar_data = (phar_archive_data *) dst->oth;
1117
1118 if (!phar_data->is_persistent) {
1119 ++(phar_data->refcount);
1120 }
1121 }
1122 /* }}} */
1123
1124 static spl_other_handler phar_spl_foreign_handler = {
1125 phar_spl_foreign_dtor,
1126 phar_spl_foreign_clone
1127 };
1128 #endif /* HAVE_SPL */
1129
1130 /* {{{ proto void Phar::__construct(string fname [, int flags [, string alias]])
1131 * Construct a Phar archive object
1132 *
1133 * proto void PharData::__construct(string fname [[, int flags [, string alias]], int file format = Phar::TAR])
1134 * Construct a PharData archive object
1135 *
1136 * This function is used as the constructor for both the Phar and PharData
1137 * classes, hence the two prototypes above.
1138 */
PHP_METHOD(Phar,__construct)1139 PHP_METHOD(Phar, __construct)
1140 {
1141 #if !HAVE_SPL
1142 zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Cannot instantiate Phar object without SPL extension");
1143 #else
1144 char *fname, *alias = NULL, *error, *arch = NULL, *entry = NULL, *save_fname;
1145 int fname_len, alias_len = 0, arch_len, entry_len, is_data;
1146 long flags = SPL_FILE_DIR_SKIPDOTS|SPL_FILE_DIR_UNIXPATHS;
1147 long format = 0;
1148 phar_archive_object *phar_obj;
1149 phar_archive_data *phar_data;
1150 zval *zobj = getThis(), arg1, arg2;
1151
1152 phar_obj = (phar_archive_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
1153
1154 is_data = instanceof_function(Z_OBJCE_P(zobj), phar_ce_data TSRMLS_CC);
1155
1156 if (is_data) {
1157 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|ls!l", &fname, &fname_len, &flags, &alias, &alias_len, &format) == FAILURE) {
1158 return;
1159 }
1160 } else {
1161 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|ls!", &fname, &fname_len, &flags, &alias, &alias_len) == FAILURE) {
1162 return;
1163 }
1164 }
1165
1166 if (phar_obj->arc.archive) {
1167 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Cannot call constructor twice");
1168 return;
1169 }
1170
1171 save_fname = fname;
1172 if (SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, !is_data, 2 TSRMLS_CC)) {
1173 /* use arch (the basename for the archive) for fname instead of fname */
1174 /* this allows support for RecursiveDirectoryIterator of subdirectories */
1175 #ifdef PHP_WIN32
1176 phar_unixify_path_separators(arch, arch_len);
1177 #endif
1178 fname = arch;
1179 fname_len = arch_len;
1180 #ifdef PHP_WIN32
1181 } else {
1182 arch = estrndup(fname, fname_len);
1183 arch_len = fname_len;
1184 fname = arch;
1185 phar_unixify_path_separators(arch, arch_len);
1186 #endif
1187 }
1188
1189 if (phar_open_or_create_filename(fname, fname_len, alias, alias_len, is_data, REPORT_ERRORS, &phar_data, &error TSRMLS_CC) == FAILURE) {
1190
1191 if (fname == arch && fname != save_fname) {
1192 efree(arch);
1193 fname = save_fname;
1194 }
1195
1196 if (entry) {
1197 efree(entry);
1198 }
1199
1200 if (error) {
1201 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
1202 "%s", error);
1203 efree(error);
1204 } else {
1205 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
1206 "Phar creation or opening failed");
1207 }
1208
1209 return;
1210 }
1211
1212 if (is_data && phar_data->is_tar && phar_data->is_brandnew && format == PHAR_FORMAT_ZIP) {
1213 phar_data->is_zip = 1;
1214 phar_data->is_tar = 0;
1215 }
1216
1217 if (fname == arch) {
1218 efree(arch);
1219 fname = save_fname;
1220 }
1221
1222 if ((is_data && !phar_data->is_data) || (!is_data && phar_data->is_data)) {
1223 if (is_data) {
1224 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
1225 "PharData class can only be used for non-executable tar and zip archives");
1226 } else {
1227 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
1228 "Phar class can only be used for executable tar and zip archives");
1229 }
1230 efree(entry);
1231 return;
1232 }
1233
1234 is_data = phar_data->is_data;
1235
1236 if (!phar_data->is_persistent) {
1237 ++(phar_data->refcount);
1238 }
1239
1240 phar_obj->arc.archive = phar_data;
1241 phar_obj->spl.oth_handler = &phar_spl_foreign_handler;
1242
1243 if (entry) {
1244 fname_len = spprintf(&fname, 0, "phar://%s%s", phar_data->fname, entry);
1245 efree(entry);
1246 } else {
1247 fname_len = spprintf(&fname, 0, "phar://%s", phar_data->fname);
1248 }
1249
1250 INIT_PZVAL(&arg1);
1251 ZVAL_STRINGL(&arg1, fname, fname_len, 0);
1252 INIT_PZVAL(&arg2);
1253 ZVAL_LONG(&arg2, flags);
1254
1255 zend_call_method_with_2_params(&zobj, Z_OBJCE_P(zobj),
1256 &spl_ce_RecursiveDirectoryIterator->constructor, "__construct", NULL, &arg1, &arg2);
1257
1258 if (!phar_data->is_persistent) {
1259 phar_obj->arc.archive->is_data = is_data;
1260 } else if (!EG(exception)) {
1261 /* register this guy so we can modify if necessary */
1262 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);
1263 }
1264
1265 phar_obj->spl.info_class = phar_ce_entry;
1266 efree(fname);
1267 #endif /* HAVE_SPL */
1268 }
1269 /* }}} */
1270
1271 /* {{{ proto array Phar::getSupportedSignatures()
1272 * Return array of supported signature types
1273 */
PHP_METHOD(Phar,getSupportedSignatures)1274 PHP_METHOD(Phar, getSupportedSignatures)
1275 {
1276 if (zend_parse_parameters_none() == FAILURE) {
1277 return;
1278 }
1279
1280 array_init(return_value);
1281
1282 add_next_index_stringl(return_value, "MD5", 3, 1);
1283 add_next_index_stringl(return_value, "SHA-1", 5, 1);
1284 #ifdef PHAR_HASH_OK
1285 add_next_index_stringl(return_value, "SHA-256", 7, 1);
1286 add_next_index_stringl(return_value, "SHA-512", 7, 1);
1287 #endif
1288 #if PHAR_HAVE_OPENSSL
1289 add_next_index_stringl(return_value, "OpenSSL", 7, 1);
1290 #else
1291 if (zend_hash_exists(&module_registry, "openssl", sizeof("openssl"))) {
1292 add_next_index_stringl(return_value, "OpenSSL", 7, 1);
1293 }
1294 #endif
1295 }
1296 /* }}} */
1297
1298 /* {{{ proto array Phar::getSupportedCompression()
1299 * Return array of supported comparession algorithms
1300 */
PHP_METHOD(Phar,getSupportedCompression)1301 PHP_METHOD(Phar, getSupportedCompression)
1302 {
1303 if (zend_parse_parameters_none() == FAILURE) {
1304 return;
1305 }
1306
1307 array_init(return_value);
1308 phar_request_initialize(TSRMLS_C);
1309
1310 if (PHAR_G(has_zlib)) {
1311 add_next_index_stringl(return_value, "GZ", 2, 1);
1312 }
1313
1314 if (PHAR_G(has_bz2)) {
1315 add_next_index_stringl(return_value, "BZIP2", 5, 1);
1316 }
1317 }
1318 /* }}} */
1319
1320 /* {{{ proto array Phar::unlinkArchive(string archive)
1321 * Completely remove a phar archive from memory and disk
1322 */
PHP_METHOD(Phar,unlinkArchive)1323 PHP_METHOD(Phar, unlinkArchive)
1324 {
1325 char *fname, *error, *zname, *arch, *entry;
1326 int fname_len, zname_len, arch_len, entry_len;
1327 phar_archive_data *phar;
1328
1329 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p", &fname, &fname_len) == FAILURE) {
1330 RETURN_FALSE;
1331 }
1332
1333 if (!fname_len) {
1334 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Unknown phar archive \"\"");
1335 return;
1336 }
1337
1338 if (FAILURE == phar_open_from_filename(fname, fname_len, NULL, 0, REPORT_ERRORS, &phar, &error TSRMLS_CC)) {
1339 if (error) {
1340 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Unknown phar archive \"%s\": %s", fname, error);
1341 efree(error);
1342 } else {
1343 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "Unknown phar archive \"%s\"", fname);
1344 }
1345 return;
1346 }
1347
1348 zname = (char*)zend_get_executed_filename(TSRMLS_C);
1349 zname_len = strlen(zname);
1350
1351 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)) {
1352 if (arch_len == fname_len && !memcmp(arch, fname, arch_len)) {
1353 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar archive \"%s\" cannot be unlinked from within itself", fname);
1354 efree(arch);
1355 efree(entry);
1356 return;
1357 }
1358 efree(arch);
1359 efree(entry);
1360 }
1361
1362 if (phar->is_persistent) {
1363 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar archive \"%s\" is in phar.cache_list, cannot unlinkArchive()", fname);
1364 return;
1365 }
1366
1367 if (phar->refcount) {
1368 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);
1369 return;
1370 }
1371
1372 fname = estrndup(phar->fname, phar->fname_len);
1373
1374 /* invalidate phar cache */
1375 PHAR_G(last_phar) = NULL;
1376 PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
1377
1378 phar_archive_delref(phar TSRMLS_CC);
1379 unlink(fname);
1380 efree(fname);
1381 RETURN_TRUE;
1382 }
1383 /* }}} */
1384
1385 #if HAVE_SPL
1386
1387 #define PHAR_ARCHIVE_OBJECT() \
1388 phar_archive_object *phar_obj = (phar_archive_object*)zend_object_store_get_object(getThis() TSRMLS_CC); \
1389 if (!phar_obj->arc.archive) { \
1390 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
1391 "Cannot call method on an uninitialized Phar object"); \
1392 return; \
1393 }
1394
1395 /* {{{ proto void Phar::__destruct()
1396 * if persistent, remove from the cache
1397 */
PHP_METHOD(Phar,__destruct)1398 PHP_METHOD(Phar, __destruct)
1399 {
1400 phar_archive_object *phar_obj = (phar_archive_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
1401
1402 if (phar_obj->arc.archive && phar_obj->arc.archive->is_persistent) {
1403 zend_hash_del(&PHAR_GLOBALS->phar_persist_map, (const char *) phar_obj->arc.archive, sizeof(phar_obj->arc.archive));
1404 }
1405 }
1406 /* }}} */
1407
1408 struct _phar_t {
1409 phar_archive_object *p;
1410 zend_class_entry *c;
1411 char *b;
1412 uint l;
1413 zval *ret;
1414 int count;
1415 php_stream *fp;
1416 };
1417
phar_build(zend_object_iterator * iter,void * puser TSRMLS_DC)1418 static int phar_build(zend_object_iterator *iter, void *puser TSRMLS_DC) /* {{{ */
1419 {
1420 zval **value;
1421 zend_bool close_fp = 1;
1422 struct _phar_t *p_obj = (struct _phar_t*) puser;
1423 uint str_key_len, base_len = p_obj->l, fname_len;
1424 phar_entry_data *data;
1425 php_stream *fp;
1426 size_t contents_len;
1427 char *fname, *error = NULL, *base = p_obj->b, *opened, *save = NULL, *temp = NULL;
1428 char *str_key;
1429 zend_class_entry *ce = p_obj->c;
1430 phar_archive_object *phar_obj = p_obj->p;
1431 char *str = "[stream]";
1432
1433 iter->funcs->get_current_data(iter, &value TSRMLS_CC);
1434
1435 if (EG(exception)) {
1436 return ZEND_HASH_APPLY_STOP;
1437 }
1438
1439 if (!value) {
1440 /* failure in get_current_data */
1441 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned no value", ce->name);
1442 return ZEND_HASH_APPLY_STOP;
1443 }
1444
1445 switch (Z_TYPE_PP(value)) {
1446 case IS_STRING:
1447 break;
1448 case IS_RESOURCE:
1449 php_stream_from_zval_no_verify(fp, value);
1450
1451 if (!fp) {
1452 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Iterator %v returned an invalid stream handle", ce->name);
1453 return ZEND_HASH_APPLY_STOP;
1454 }
1455
1456 if (iter->funcs->get_current_key) {
1457 zval key;
1458 iter->funcs->get_current_key(iter, &key TSRMLS_CC);
1459
1460 if (EG(exception)) {
1461 return ZEND_HASH_APPLY_STOP;
1462 }
1463
1464 if (Z_TYPE(key) != IS_STRING) {
1465 zval_dtor(&key);
1466 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned an invalid key (must return a string)", ce->name);
1467 return ZEND_HASH_APPLY_STOP;
1468 }
1469
1470 str_key_len = Z_STRLEN(key);
1471 str_key = estrndup(Z_STRVAL(key), str_key_len);
1472
1473 save = str_key;
1474 zval_dtor(&key);
1475 } else {
1476 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned an invalid key (must return a string)", ce->name);
1477 return ZEND_HASH_APPLY_STOP;
1478 }
1479
1480 close_fp = 0;
1481 opened = (char *) estrndup(str, sizeof("[stream]") - 1);
1482 goto after_open_fp;
1483 case IS_OBJECT:
1484 if (instanceof_function(Z_OBJCE_PP(value), spl_ce_SplFileInfo TSRMLS_CC)) {
1485 char *test = NULL;
1486 zval dummy;
1487 spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(*value TSRMLS_CC);
1488
1489 if (!base_len) {
1490 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Iterator %v returns an SplFileInfo object, so base directory must be specified", ce->name);
1491 return ZEND_HASH_APPLY_STOP;
1492 }
1493
1494 switch (intern->type) {
1495 case SPL_FS_DIR:
1496 test = spl_filesystem_object_get_path(intern, NULL TSRMLS_CC);
1497 fname_len = spprintf(&fname, 0, "%s%c%s", test, DEFAULT_SLASH, intern->u.dir.entry.d_name);
1498 php_stat(fname, fname_len, FS_IS_DIR, &dummy TSRMLS_CC);
1499
1500 if (Z_BVAL(dummy)) {
1501 /* ignore directories */
1502 efree(fname);
1503 return ZEND_HASH_APPLY_KEEP;
1504 }
1505
1506 test = expand_filepath(fname, NULL TSRMLS_CC);
1507 efree(fname);
1508
1509 if (test) {
1510 fname = test;
1511 fname_len = strlen(fname);
1512 } else {
1513 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Could not resolve file path");
1514 return ZEND_HASH_APPLY_STOP;
1515 }
1516
1517 save = fname;
1518 goto phar_spl_fileinfo;
1519 case SPL_FS_INFO:
1520 case SPL_FS_FILE:
1521 fname = expand_filepath(intern->file_name, NULL TSRMLS_CC);
1522 if (!fname) {
1523 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Could not resolve file path");
1524 return ZEND_HASH_APPLY_STOP;
1525 }
1526
1527 fname_len = strlen(fname);
1528 save = fname;
1529 goto phar_spl_fileinfo;
1530 }
1531 }
1532 /* fall-through */
1533 default:
1534 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned an invalid value (must return a string)", ce->name);
1535 return ZEND_HASH_APPLY_STOP;
1536 }
1537
1538 fname = Z_STRVAL_PP(value);
1539 fname_len = Z_STRLEN_PP(value);
1540
1541 phar_spl_fileinfo:
1542 if (base_len) {
1543 temp = expand_filepath(base, NULL TSRMLS_CC);
1544 if (!temp) {
1545 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Could not resolve file path");
1546 if (save) {
1547 efree(save);
1548 }
1549 return ZEND_HASH_APPLY_STOP;
1550 }
1551
1552 base = temp;
1553 base_len = strlen(base);
1554
1555 if (strstr(fname, base)) {
1556 str_key_len = fname_len - base_len;
1557
1558 if (str_key_len <= 0) {
1559 if (save) {
1560 efree(save);
1561 efree(temp);
1562 }
1563 return ZEND_HASH_APPLY_KEEP;
1564 }
1565
1566 str_key = fname + base_len;
1567
1568 if (*str_key == '/' || *str_key == '\\') {
1569 str_key++;
1570 str_key_len--;
1571 }
1572
1573 } else {
1574 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);
1575
1576 if (save) {
1577 efree(save);
1578 efree(temp);
1579 }
1580
1581 return ZEND_HASH_APPLY_STOP;
1582 }
1583 } else {
1584 if (iter->funcs->get_current_key) {
1585 zval key;
1586 iter->funcs->get_current_key(iter, &key TSRMLS_CC);
1587
1588 if (EG(exception)) {
1589 return ZEND_HASH_APPLY_STOP;
1590 }
1591
1592 if (Z_TYPE(key) != IS_STRING) {
1593 zval_dtor(&key);
1594 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned an invalid key (must return a string)", ce->name);
1595 return ZEND_HASH_APPLY_STOP;
1596 }
1597
1598 str_key_len = Z_STRLEN(key);
1599 str_key = estrndup(Z_STRVAL(key), str_key_len);
1600
1601 save = str_key;
1602 zval_dtor(&key);
1603 } else {
1604 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned an invalid key (must return a string)", ce->name);
1605 return ZEND_HASH_APPLY_STOP;
1606 }
1607 }
1608 #if PHP_API_VERSION < 20100412
1609 if (PG(safe_mode) && (!php_checkuid(fname, NULL, CHECKUID_ALLOW_ONLY_FILE))) {
1610 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned a path \"%s\" that safe mode prevents opening", ce->name, fname);
1611
1612 if (save) {
1613 efree(save);
1614 }
1615
1616 if (temp) {
1617 efree(temp);
1618 }
1619
1620 return ZEND_HASH_APPLY_STOP;
1621 }
1622 #endif
1623
1624 if (php_check_open_basedir(fname TSRMLS_CC)) {
1625 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned a path \"%s\" that open_basedir prevents opening", ce->name, fname);
1626
1627 if (save) {
1628 efree(save);
1629 }
1630
1631 if (temp) {
1632 efree(temp);
1633 }
1634
1635 return ZEND_HASH_APPLY_STOP;
1636 }
1637
1638 /* try to open source file, then create internal phar file and copy contents */
1639 fp = php_stream_open_wrapper(fname, "rb", STREAM_MUST_SEEK|0, &opened);
1640
1641 if (!fp) {
1642 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "Iterator %v returned a file that could not be opened \"%s\"", ce->name, fname);
1643
1644 if (save) {
1645 efree(save);
1646 }
1647
1648 if (temp) {
1649 efree(temp);
1650 }
1651
1652 return ZEND_HASH_APPLY_STOP;
1653 }
1654 after_open_fp:
1655 if (str_key_len >= sizeof(".phar")-1 && !memcmp(str_key, ".phar", sizeof(".phar")-1)) {
1656 /* silently skip any files that would be added to the magic .phar directory */
1657 if (save) {
1658 efree(save);
1659 }
1660
1661 if (temp) {
1662 efree(temp);
1663 }
1664
1665 if (opened) {
1666 efree(opened);
1667 }
1668
1669 if (close_fp) {
1670 php_stream_close(fp);
1671 }
1672
1673 return ZEND_HASH_APPLY_KEEP;
1674 }
1675
1676 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))) {
1677 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Entry %s cannot be created: %s", str_key, error);
1678 efree(error);
1679
1680 if (save) {
1681 efree(save);
1682 }
1683
1684 if (opened) {
1685 efree(opened);
1686 }
1687
1688 if (temp) {
1689 efree(temp);
1690 }
1691
1692 if (close_fp) {
1693 php_stream_close(fp);
1694 }
1695
1696 return ZEND_HASH_APPLY_STOP;
1697
1698 } else {
1699 if (error) {
1700 efree(error);
1701 }
1702 /* convert to PHAR_UFP */
1703 if (data->internal_file->fp_type == PHAR_MOD) {
1704 php_stream_close(data->internal_file->fp);
1705 }
1706
1707 data->internal_file->fp = NULL;
1708 data->internal_file->fp_type = PHAR_UFP;
1709 data->internal_file->offset_abs = data->internal_file->offset = php_stream_tell(p_obj->fp);
1710 data->fp = NULL;
1711 phar_stream_copy_to_stream(fp, p_obj->fp, PHP_STREAM_COPY_ALL, &contents_len);
1712 data->internal_file->uncompressed_filesize = data->internal_file->compressed_filesize =
1713 php_stream_tell(p_obj->fp) - data->internal_file->offset;
1714 }
1715
1716 if (close_fp) {
1717 php_stream_close(fp);
1718 }
1719
1720 add_assoc_string(p_obj->ret, str_key, opened, 0);
1721
1722 if (save) {
1723 efree(save);
1724 }
1725
1726 if (temp) {
1727 efree(temp);
1728 }
1729
1730 data->internal_file->compressed_filesize = data->internal_file->uncompressed_filesize = contents_len;
1731 phar_entry_delref(data TSRMLS_CC);
1732
1733 return ZEND_HASH_APPLY_KEEP;
1734 }
1735 /* }}} */
1736
1737 /* {{{ proto array Phar::buildFromDirectory(string base_dir[, string regex])
1738 * Construct a phar archive from an existing directory, recursively.
1739 * Optional second parameter is a regular expression for filtering directory contents.
1740 *
1741 * Return value is an array mapping phar index to actual files added.
1742 */
PHP_METHOD(Phar,buildFromDirectory)1743 PHP_METHOD(Phar, buildFromDirectory)
1744 {
1745 char *dir, *error, *regex = NULL;
1746 int dir_len, regex_len = 0;
1747 zend_bool apply_reg = 0;
1748 zval arg, arg2, *iter, *iteriter, *regexiter = NULL;
1749 struct _phar_t pass;
1750
1751 PHAR_ARCHIVE_OBJECT();
1752
1753 if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
1754 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
1755 "Cannot write to archive - write operations restricted by INI setting");
1756 return;
1757 }
1758
1759 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|s", &dir, &dir_len, ®ex, ®ex_len) == FAILURE) {
1760 RETURN_FALSE;
1761 }
1762
1763 MAKE_STD_ZVAL(iter);
1764
1765 if (SUCCESS != object_init_ex(iter, spl_ce_RecursiveDirectoryIterator)) {
1766 zval_ptr_dtor(&iter);
1767 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Unable to instantiate directory iterator for %s", phar_obj->arc.archive->fname);
1768 RETURN_FALSE;
1769 }
1770
1771 INIT_PZVAL(&arg);
1772 ZVAL_STRINGL(&arg, dir, dir_len, 0);
1773 INIT_PZVAL(&arg2);
1774 ZVAL_LONG(&arg2, SPL_FILE_DIR_SKIPDOTS|SPL_FILE_DIR_UNIXPATHS);
1775
1776 zend_call_method_with_2_params(&iter, spl_ce_RecursiveDirectoryIterator,
1777 &spl_ce_RecursiveDirectoryIterator->constructor, "__construct", NULL, &arg, &arg2);
1778
1779 if (EG(exception)) {
1780 zval_ptr_dtor(&iter);
1781 RETURN_FALSE;
1782 }
1783
1784 MAKE_STD_ZVAL(iteriter);
1785
1786 if (SUCCESS != object_init_ex(iteriter, spl_ce_RecursiveIteratorIterator)) {
1787 zval_ptr_dtor(&iter);
1788 zval_ptr_dtor(&iteriter);
1789 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Unable to instantiate directory iterator for %s", phar_obj->arc.archive->fname);
1790 RETURN_FALSE;
1791 }
1792
1793 zend_call_method_with_1_params(&iteriter, spl_ce_RecursiveIteratorIterator,
1794 &spl_ce_RecursiveIteratorIterator->constructor, "__construct", NULL, iter);
1795
1796 if (EG(exception)) {
1797 zval_ptr_dtor(&iter);
1798 zval_ptr_dtor(&iteriter);
1799 RETURN_FALSE;
1800 }
1801
1802 zval_ptr_dtor(&iter);
1803
1804 if (regex_len > 0) {
1805 apply_reg = 1;
1806 MAKE_STD_ZVAL(regexiter);
1807
1808 if (SUCCESS != object_init_ex(regexiter, spl_ce_RegexIterator)) {
1809 zval_ptr_dtor(&iteriter);
1810 zval_dtor(regexiter);
1811 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Unable to instantiate regex iterator for %s", phar_obj->arc.archive->fname);
1812 RETURN_FALSE;
1813 }
1814
1815 INIT_PZVAL(&arg2);
1816 ZVAL_STRINGL(&arg2, regex, regex_len, 0);
1817
1818 zend_call_method_with_2_params(®exiter, spl_ce_RegexIterator,
1819 &spl_ce_RegexIterator->constructor, "__construct", NULL, iteriter, &arg2);
1820 }
1821
1822 array_init(return_value);
1823
1824 pass.c = apply_reg ? Z_OBJCE_P(regexiter) : Z_OBJCE_P(iteriter);
1825 pass.p = phar_obj;
1826 pass.b = dir;
1827 pass.l = dir_len;
1828 pass.count = 0;
1829 pass.ret = return_value;
1830 pass.fp = php_stream_fopen_tmpfile();
1831 if (pass.fp == NULL) {
1832 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" unable to create temporary file", phar_obj->arc.archive->fname);
1833 return;
1834 }
1835
1836 if (phar_obj->arc.archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->arc.archive) TSRMLS_CC)) {
1837 zval_ptr_dtor(&iteriter);
1838 if (apply_reg) {
1839 zval_ptr_dtor(®exiter);
1840 }
1841 php_stream_close(pass.fp);
1842 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar_obj->arc.archive->fname);
1843 return;
1844 }
1845
1846 if (SUCCESS == spl_iterator_apply((apply_reg ? regexiter : iteriter), (spl_iterator_apply_func_t) phar_build, (void *) &pass TSRMLS_CC)) {
1847 zval_ptr_dtor(&iteriter);
1848
1849 if (apply_reg) {
1850 zval_ptr_dtor(®exiter);
1851 }
1852
1853 phar_obj->arc.archive->ufp = pass.fp;
1854 phar_flush(phar_obj->arc.archive, 0, 0, 0, &error TSRMLS_CC);
1855
1856 if (error) {
1857 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
1858 efree(error);
1859 }
1860
1861 } else {
1862 zval_ptr_dtor(&iteriter);
1863 if (apply_reg) {
1864 zval_ptr_dtor(®exiter);
1865 }
1866 php_stream_close(pass.fp);
1867 }
1868 }
1869 /* }}} */
1870
1871 /* {{{ proto array Phar::buildFromIterator(Iterator iter[, string base_directory])
1872 * Construct a phar archive from an iterator. The iterator must return a series of strings
1873 * that are full paths to files that should be added to the phar. The iterator key should
1874 * be the path that the file will have within the phar archive.
1875 *
1876 * If base directory is specified, then the key will be ignored, and instead the portion of
1877 * the current value minus the base directory will be used
1878 *
1879 * Returned is an array mapping phar index to actual file added
1880 */
PHP_METHOD(Phar,buildFromIterator)1881 PHP_METHOD(Phar, buildFromIterator)
1882 {
1883 zval *obj;
1884 char *error;
1885 uint base_len = 0;
1886 char *base = NULL;
1887 struct _phar_t pass;
1888
1889 PHAR_ARCHIVE_OBJECT();
1890
1891 if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
1892 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC,
1893 "Cannot write out phar archive, phar is read-only");
1894 return;
1895 }
1896
1897 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|s", &obj, zend_ce_traversable, &base, &base_len) == FAILURE) {
1898 RETURN_FALSE;
1899 }
1900
1901 if (phar_obj->arc.archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->arc.archive) TSRMLS_CC)) {
1902 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar_obj->arc.archive->fname);
1903 return;
1904 }
1905
1906 array_init(return_value);
1907
1908 pass.c = Z_OBJCE_P(obj);
1909 pass.p = phar_obj;
1910 pass.b = base;
1911 pass.l = base_len;
1912 pass.ret = return_value;
1913 pass.count = 0;
1914 pass.fp = php_stream_fopen_tmpfile();
1915 if (pass.fp == NULL) {
1916 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\": unable to create temporary file", phar_obj->arc.archive->fname);
1917 return;
1918 }
1919
1920 if (SUCCESS == spl_iterator_apply(obj, (spl_iterator_apply_func_t) phar_build, (void *) &pass TSRMLS_CC)) {
1921 phar_obj->arc.archive->ufp = pass.fp;
1922 phar_flush(phar_obj->arc.archive, 0, 0, 0, &error TSRMLS_CC);
1923 if (error) {
1924 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
1925 efree(error);
1926 }
1927 } else {
1928 php_stream_close(pass.fp);
1929 }
1930 }
1931 /* }}} */
1932
1933 /* {{{ proto int Phar::count()
1934 * Returns the number of entries in the Phar archive
1935 */
PHP_METHOD(Phar,count)1936 PHP_METHOD(Phar, count)
1937 {
1938 PHAR_ARCHIVE_OBJECT();
1939
1940 if (zend_parse_parameters_none() == FAILURE) {
1941 return;
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 != phar_stream_copy_to_stream(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;
3631
3632 if (filename_len >= sizeof(".phar")-1 && !memcmp(filename, ".phar", sizeof(".phar")-1) && (filename[5] == '/' || filename[5] == '\\' || filename[5] == '\0')) {
3633 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Cannot create any files in magic \".phar\" directory", (*pphar)->fname);
3634 return;
3635 }
3636
3637 if (!(data = phar_get_or_create_entry_data((*pphar)->fname, (*pphar)->fname_len, filename, filename_len, "w+b", 0, &error, 1 TSRMLS_CC))) {
3638 if (error) {
3639 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Entry %s does not exist and cannot be created: %s", filename, error);
3640 efree(error);
3641 } else {
3642 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Entry %s does not exist and cannot be created", filename);
3643 }
3644 return;
3645 } else {
3646 if (error) {
3647 efree(error);
3648 }
3649
3650 if (!data->internal_file->is_dir) {
3651 if (cont_str) {
3652 contents_len = php_stream_write(data->fp, cont_str, cont_len);
3653 if (contents_len != cont_len) {
3654 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Entry %s could not be written to", filename);
3655 return;
3656 }
3657 } else {
3658 if (!(php_stream_from_zval_no_verify(contents_file, &zresource))) {
3659 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Entry %s could not be written to", filename);
3660 return;
3661 }
3662 phar_stream_copy_to_stream(contents_file, data->fp, PHP_STREAM_COPY_ALL, &contents_len);
3663 }
3664
3665 data->internal_file->compressed_filesize = data->internal_file->uncompressed_filesize = contents_len;
3666 }
3667
3668 /* check for copy-on-write */
3669 if (pphar[0] != data->phar) {
3670 *pphar = data->phar;
3671 }
3672 phar_entry_delref(data TSRMLS_CC);
3673 phar_flush(*pphar, 0, 0, 0, &error TSRMLS_CC);
3674
3675 if (error) {
3676 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
3677 efree(error);
3678 }
3679 }
3680 }
3681 /* }}} */
3682
3683 /* {{{ create a directory within the phar archive
3684 */
phar_mkdir(phar_archive_data ** pphar,char * dirname,int dirname_len TSRMLS_DC)3685 static void phar_mkdir(phar_archive_data **pphar, char *dirname, int dirname_len TSRMLS_DC)
3686 {
3687 char *error;
3688 phar_entry_data *data;
3689
3690 if (!(data = phar_get_or_create_entry_data((*pphar)->fname, (*pphar)->fname_len, dirname, dirname_len, "w+b", 2, &error, 1 TSRMLS_CC))) {
3691 if (error) {
3692 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Directory %s does not exist and cannot be created: %s", dirname, error);
3693 efree(error);
3694 } else {
3695 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Directory %s does not exist and cannot be created", dirname);
3696 }
3697
3698 return;
3699 } else {
3700 if (error) {
3701 efree(error);
3702 }
3703
3704 /* check for copy on write */
3705 if (data->phar != *pphar) {
3706 *pphar = data->phar;
3707 }
3708 phar_entry_delref(data TSRMLS_CC);
3709 phar_flush(*pphar, 0, 0, 0, &error TSRMLS_CC);
3710
3711 if (error) {
3712 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
3713 efree(error);
3714 }
3715 }
3716 }
3717 /* }}} */
3718
3719 /* {{{ proto int Phar::offsetSet(string entry, string value)
3720 * set the contents of an internal file to those of an external file
3721 */
PHP_METHOD(Phar,offsetSet)3722 PHP_METHOD(Phar, offsetSet)
3723 {
3724 char *fname, *cont_str = NULL;
3725 int fname_len, cont_len;
3726 zval *zresource;
3727 PHAR_ARCHIVE_OBJECT();
3728
3729 if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
3730 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Write operations disabled by the php.ini setting phar.readonly");
3731 return;
3732 }
3733
3734 if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "pr", &fname, &fname_len, &zresource) == FAILURE
3735 && zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ps", &fname, &fname_len, &cont_str, &cont_len) == FAILURE) {
3736 return;
3737 }
3738
3739 if (fname_len == sizeof(".phar/stub.php")-1 && !memcmp(fname, ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
3740 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);
3741 return;
3742 }
3743
3744 if (fname_len == sizeof(".phar/alias.txt")-1 && !memcmp(fname, ".phar/alias.txt", sizeof(".phar/alias.txt")-1)) {
3745 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);
3746 return;
3747 }
3748
3749 if (fname_len >= sizeof(".phar")-1 && !memcmp(fname, ".phar", sizeof(".phar")-1)) {
3750 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);
3751 return;
3752 }
3753
3754 phar_add_file(&(phar_obj->arc.archive), fname, fname_len, cont_str, cont_len, zresource TSRMLS_CC);
3755 }
3756 /* }}} */
3757
3758 /* {{{ proto int Phar::offsetUnset(string entry)
3759 * remove a file from a phar
3760 */
PHP_METHOD(Phar,offsetUnset)3761 PHP_METHOD(Phar, offsetUnset)
3762 {
3763 char *fname, *error;
3764 int fname_len;
3765 phar_entry_info *entry;
3766 PHAR_ARCHIVE_OBJECT();
3767
3768 if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
3769 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Write operations disabled by the php.ini setting phar.readonly");
3770 return;
3771 }
3772
3773 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p", &fname, &fname_len) == FAILURE) {
3774 return;
3775 }
3776
3777 if (zend_hash_exists(&phar_obj->arc.archive->manifest, fname, (uint) fname_len)) {
3778 if (SUCCESS == zend_hash_find(&phar_obj->arc.archive->manifest, fname, (uint) fname_len, (void**)&entry)) {
3779 if (entry->is_deleted) {
3780 /* entry is deleted, but has not been flushed to disk yet */
3781 return;
3782 }
3783
3784 if (phar_obj->arc.archive->is_persistent) {
3785 if (FAILURE == phar_copy_on_write(&(phar_obj->arc.archive) TSRMLS_CC)) {
3786 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar_obj->arc.archive->fname);
3787 return;
3788 }
3789 /* re-populate entry after copy on write */
3790 zend_hash_find(&phar_obj->arc.archive->manifest, fname, (uint) fname_len, (void **)&entry);
3791 }
3792 entry->is_modified = 0;
3793 entry->is_deleted = 1;
3794 /* we need to "flush" the stream to save the newly deleted file on disk */
3795 phar_flush(phar_obj->arc.archive, 0, 0, 0, &error TSRMLS_CC);
3796
3797 if (error) {
3798 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
3799 efree(error);
3800 }
3801
3802 RETURN_TRUE;
3803 }
3804 } else {
3805 RETURN_FALSE;
3806 }
3807 }
3808 /* }}} */
3809
3810 /* {{{ proto string Phar::addEmptyDir(string dirname)
3811 * Adds an empty directory to the phar archive
3812 */
PHP_METHOD(Phar,addEmptyDir)3813 PHP_METHOD(Phar, addEmptyDir)
3814 {
3815 char *dirname;
3816 int dirname_len;
3817
3818 PHAR_ARCHIVE_OBJECT();
3819
3820 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p", &dirname, &dirname_len) == FAILURE) {
3821 return;
3822 }
3823
3824 if (dirname_len >= sizeof(".phar")-1 && !memcmp(dirname, ".phar", sizeof(".phar")-1)) {
3825 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Cannot create a directory in magic \".phar\" directory");
3826 return;
3827 }
3828
3829 phar_mkdir(&phar_obj->arc.archive, dirname, dirname_len TSRMLS_CC);
3830 }
3831 /* }}} */
3832
3833 /* {{{ proto string Phar::addFile(string filename[, string localname])
3834 * Adds a file to the archive using the filename, or the second parameter as the name within the archive
3835 */
PHP_METHOD(Phar,addFile)3836 PHP_METHOD(Phar, addFile)
3837 {
3838 char *fname, *localname = NULL;
3839 int fname_len, localname_len = 0;
3840 php_stream *resource;
3841 zval *zresource;
3842
3843 PHAR_ARCHIVE_OBJECT();
3844
3845 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|s", &fname, &fname_len, &localname, &localname_len) == FAILURE) {
3846 return;
3847 }
3848
3849 #if PHP_API_VERSION < 20100412
3850 if (PG(safe_mode) && (!php_checkuid(fname, NULL, CHECKUID_ALLOW_ONLY_FILE))) {
3851 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);
3852 return;
3853 }
3854 #endif
3855
3856 if (!strstr(fname, "://") && php_check_open_basedir(fname TSRMLS_CC)) {
3857 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);
3858 return;
3859 }
3860
3861 if (!(resource = php_stream_open_wrapper(fname, "rb", 0, NULL))) {
3862 zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC, "phar error: unable to open file \"%s\" to add to phar archive", fname);
3863 return;
3864 }
3865
3866 if (localname) {
3867 fname = localname;
3868 fname_len = localname_len;
3869 }
3870
3871 MAKE_STD_ZVAL(zresource);
3872 php_stream_to_zval(resource, zresource);
3873 phar_add_file(&(phar_obj->arc.archive), fname, fname_len, NULL, 0, zresource TSRMLS_CC);
3874 efree(zresource);
3875 php_stream_close(resource);
3876 }
3877 /* }}} */
3878
3879 /* {{{ proto string Phar::addFromString(string localname, string contents)
3880 * Adds a file to the archive using its contents as a string
3881 */
PHP_METHOD(Phar,addFromString)3882 PHP_METHOD(Phar, addFromString)
3883 {
3884 char *localname, *cont_str;
3885 int localname_len, cont_len;
3886
3887 PHAR_ARCHIVE_OBJECT();
3888
3889 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ps", &localname, &localname_len, &cont_str, &cont_len) == FAILURE) {
3890 return;
3891 }
3892
3893 phar_add_file(&(phar_obj->arc.archive), localname, localname_len, cont_str, cont_len, NULL TSRMLS_CC);
3894 }
3895 /* }}} */
3896
3897 /* {{{ proto string Phar::getStub()
3898 * Returns the stub at the head of a phar archive as a string.
3899 */
PHP_METHOD(Phar,getStub)3900 PHP_METHOD(Phar, getStub)
3901 {
3902 size_t len;
3903 char *buf;
3904 php_stream *fp;
3905 php_stream_filter *filter = NULL;
3906 phar_entry_info *stub;
3907
3908 PHAR_ARCHIVE_OBJECT();
3909
3910 if (zend_parse_parameters_none() == FAILURE) {
3911 return;
3912 }
3913
3914 if (phar_obj->arc.archive->is_tar || phar_obj->arc.archive->is_zip) {
3915
3916 if (SUCCESS == zend_hash_find(&(phar_obj->arc.archive->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1, (void **)&stub)) {
3917 if (phar_obj->arc.archive->fp && !phar_obj->arc.archive->is_brandnew && !(stub->flags & PHAR_ENT_COMPRESSION_MASK)) {
3918 fp = phar_obj->arc.archive->fp;
3919 } else {
3920 if (!(fp = php_stream_open_wrapper(phar_obj->arc.archive->fname, "rb", 0, NULL))) {
3921 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0 TSRMLS_CC, "phar error: unable to open phar \"%s\"", phar_obj->arc.archive->fname);
3922 return;
3923 }
3924 if (stub->flags & PHAR_ENT_COMPRESSION_MASK) {
3925 char *filter_name;
3926
3927 if ((filter_name = phar_decompress_filter(stub, 0)) != NULL) {
3928 filter = php_stream_filter_create(filter_name, NULL, php_stream_is_persistent(fp) TSRMLS_CC);
3929 } else {
3930 filter = NULL;
3931 }
3932 if (!filter) {
3933 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));
3934 return;
3935 }
3936 php_stream_filter_append(&fp->readfilters, filter);
3937 }
3938 }
3939
3940 if (!fp) {
3941 zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC,
3942 "Unable to read stub");
3943 return;
3944 }
3945
3946 php_stream_seek(fp, stub->offset_abs, SEEK_SET);
3947 len = stub->uncompressed_filesize;
3948 goto carry_on;
3949 } else {
3950 RETURN_STRINGL("", 0, 1);
3951 }
3952 }
3953 len = phar_obj->arc.archive->halt_offset;
3954
3955 if (phar_obj->arc.archive->fp && !phar_obj->arc.archive->is_brandnew) {
3956 fp = phar_obj->arc.archive->fp;
3957 } else {
3958 fp = php_stream_open_wrapper(phar_obj->arc.archive->fname, "rb", 0, NULL);
3959 }
3960
3961 if (!fp) {
3962 zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC,
3963 "Unable to read stub");
3964 return;
3965 }
3966
3967 php_stream_rewind(fp);
3968 carry_on:
3969 buf = safe_emalloc(len, 1, 1);
3970
3971 if (len != php_stream_read(fp, buf, len)) {
3972 if (fp != phar_obj->arc.archive->fp) {
3973 php_stream_close(fp);
3974 }
3975 zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC,
3976 "Unable to read stub");
3977 efree(buf);
3978 return;
3979 }
3980
3981 if (filter) {
3982 php_stream_filter_flush(filter, 1);
3983 php_stream_filter_remove(filter, 1 TSRMLS_CC);
3984 }
3985
3986 if (fp != phar_obj->arc.archive->fp) {
3987 php_stream_close(fp);
3988 }
3989
3990 buf[len] = '\0';
3991 RETURN_STRINGL(buf, len, 0);
3992 }
3993 /* }}}*/
3994
3995 /* {{{ proto int Phar::hasMetaData()
3996 * Returns TRUE if the phar has global metadata, FALSE otherwise.
3997 */
PHP_METHOD(Phar,hasMetadata)3998 PHP_METHOD(Phar, hasMetadata)
3999 {
4000 PHAR_ARCHIVE_OBJECT();
4001
4002 RETURN_BOOL(phar_obj->arc.archive->metadata != NULL);
4003 }
4004 /* }}} */
4005
4006 /* {{{ proto int Phar::getMetaData()
4007 * Returns the global metadata of the phar
4008 */
PHP_METHOD(Phar,getMetadata)4009 PHP_METHOD(Phar, getMetadata)
4010 {
4011 PHAR_ARCHIVE_OBJECT();
4012
4013 if (zend_parse_parameters_none() == FAILURE) {
4014 return;
4015 }
4016
4017 if (phar_obj->arc.archive->metadata) {
4018 if (phar_obj->arc.archive->is_persistent) {
4019 zval *ret;
4020 char *buf = estrndup((char *) phar_obj->arc.archive->metadata, phar_obj->arc.archive->metadata_len);
4021 /* assume success, we would have failed before */
4022 phar_parse_metadata(&buf, &ret, phar_obj->arc.archive->metadata_len TSRMLS_CC);
4023 efree(buf);
4024 RETURN_ZVAL(ret, 0, 1);
4025 }
4026 RETURN_ZVAL(phar_obj->arc.archive->metadata, 1, 0);
4027 }
4028 }
4029 /* }}} */
4030
4031 /* {{{ proto int Phar::setMetaData(mixed $metadata)
4032 * Sets the global metadata of the phar
4033 */
PHP_METHOD(Phar,setMetadata)4034 PHP_METHOD(Phar, setMetadata)
4035 {
4036 char *error;
4037 zval *metadata;
4038
4039 PHAR_ARCHIVE_OBJECT();
4040
4041 if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
4042 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Write operations disabled by the php.ini setting phar.readonly");
4043 return;
4044 }
4045
4046 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &metadata) == FAILURE) {
4047 return;
4048 }
4049
4050 if (phar_obj->arc.archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->arc.archive) TSRMLS_CC)) {
4051 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar_obj->arc.archive->fname);
4052 return;
4053 }
4054 if (phar_obj->arc.archive->metadata) {
4055 zval_ptr_dtor(&phar_obj->arc.archive->metadata);
4056 phar_obj->arc.archive->metadata = NULL;
4057 }
4058
4059 MAKE_STD_ZVAL(phar_obj->arc.archive->metadata);
4060 ZVAL_ZVAL(phar_obj->arc.archive->metadata, metadata, 1, 0);
4061 phar_obj->arc.archive->is_modified = 1;
4062 phar_flush(phar_obj->arc.archive, 0, 0, 0, &error TSRMLS_CC);
4063
4064 if (error) {
4065 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
4066 efree(error);
4067 }
4068 }
4069 /* }}} */
4070
4071 /* {{{ proto int Phar::delMetadata()
4072 * Deletes the global metadata of the phar
4073 */
PHP_METHOD(Phar,delMetadata)4074 PHP_METHOD(Phar, delMetadata)
4075 {
4076 char *error;
4077
4078 PHAR_ARCHIVE_OBJECT();
4079
4080 if (PHAR_G(readonly) && !phar_obj->arc.archive->is_data) {
4081 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Write operations disabled by the php.ini setting phar.readonly");
4082 return;
4083 }
4084
4085 if (phar_obj->arc.archive->metadata) {
4086 zval_ptr_dtor(&phar_obj->arc.archive->metadata);
4087 phar_obj->arc.archive->metadata = NULL;
4088 phar_obj->arc.archive->is_modified = 1;
4089 phar_flush(phar_obj->arc.archive, 0, 0, 0, &error TSRMLS_CC);
4090
4091 if (error) {
4092 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
4093 efree(error);
4094 RETURN_FALSE;
4095 } else {
4096 RETURN_TRUE;
4097 }
4098
4099 } else {
4100 RETURN_TRUE;
4101 }
4102 }
4103 /* }}} */
4104 #if PHP_API_VERSION < 20100412
4105 #define PHAR_OPENBASEDIR_CHECKPATH(filename) \
4106 (PG(safe_mode) && (!php_checkuid(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR))) || php_check_open_basedir(filename TSRMLS_CC)
4107 #else
4108 #define PHAR_OPENBASEDIR_CHECKPATH(filename) \
4109 php_check_open_basedir(filename TSRMLS_CC)
4110 #endif
4111
phar_extract_file(zend_bool overwrite,phar_entry_info * entry,char * dest,int dest_len,char ** error TSRMLS_DC)4112 static int phar_extract_file(zend_bool overwrite, phar_entry_info *entry, char *dest, int dest_len, char **error TSRMLS_DC) /* {{{ */
4113 {
4114 php_stream_statbuf ssb;
4115 int len;
4116 php_stream *fp;
4117 char *fullpath;
4118 const char *slash;
4119 mode_t mode;
4120 cwd_state new_state;
4121 char *filename;
4122 size_t filename_len;
4123
4124 if (entry->is_mounted) {
4125 /* silently ignore mounted entries */
4126 return SUCCESS;
4127 }
4128
4129 if (entry->filename_len >= sizeof(".phar")-1 && !memcmp(entry->filename, ".phar", sizeof(".phar")-1)) {
4130 return SUCCESS;
4131 }
4132 /* strip .. from path and restrict it to be under dest directory */
4133 new_state.cwd = (char*)malloc(2);
4134 new_state.cwd[0] = DEFAULT_SLASH;
4135 new_state.cwd[1] = '\0';
4136 new_state.cwd_length = 1;
4137 if (virtual_file_ex(&new_state, entry->filename, NULL, CWD_EXPAND TSRMLS_CC) != 0 ||
4138 new_state.cwd_length <= 1) {
4139 if (EINVAL == errno && entry->filename_len > 50) {
4140 char *tmp = estrndup(entry->filename, 50);
4141 spprintf(error, 4096, "Cannot extract \"%s...\" to \"%s...\", extracted filename is too long for filesystem", tmp, dest);
4142 efree(tmp);
4143 } else {
4144 spprintf(error, 4096, "Cannot extract \"%s\", internal error", entry->filename);
4145 }
4146 free(new_state.cwd);
4147 return FAILURE;
4148 }
4149 filename = new_state.cwd + 1;
4150 filename_len = new_state.cwd_length - 1;
4151 #ifdef PHP_WIN32
4152 /* unixify the path back, otherwise non zip formats might be broken */
4153 {
4154 int cnt = filename_len;
4155
4156 do {
4157 if ('\\' == filename[cnt]) {
4158 filename[cnt] = '/';
4159 }
4160 } while (cnt-- >= 0);
4161 }
4162 #endif
4163
4164 len = spprintf(&fullpath, 0, "%s/%s", dest, filename);
4165
4166 if (len >= MAXPATHLEN) {
4167 char *tmp;
4168 /* truncate for error message */
4169 fullpath[50] = '\0';
4170 if (entry->filename_len > 50) {
4171 tmp = estrndup(entry->filename, 50);
4172 spprintf(error, 4096, "Cannot extract \"%s...\" to \"%s...\", extracted filename is too long for filesystem", tmp, fullpath);
4173 efree(tmp);
4174 } else {
4175 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s...\", extracted filename is too long for filesystem", entry->filename, fullpath);
4176 }
4177 efree(fullpath);
4178 free(new_state.cwd);
4179 return FAILURE;
4180 }
4181
4182 if (!len) {
4183 spprintf(error, 4096, "Cannot extract \"%s\", internal error", entry->filename);
4184 efree(fullpath);
4185 free(new_state.cwd);
4186 return FAILURE;
4187 }
4188
4189 if (PHAR_OPENBASEDIR_CHECKPATH(fullpath)) {
4190 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", openbasedir/safe mode restrictions in effect", entry->filename, fullpath);
4191 efree(fullpath);
4192 free(new_state.cwd);
4193 return FAILURE;
4194 }
4195
4196 /* let see if the path already exists */
4197 if (!overwrite && SUCCESS == php_stream_stat_path(fullpath, &ssb)) {
4198 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", path already exists", entry->filename, fullpath);
4199 efree(fullpath);
4200 free(new_state.cwd);
4201 return FAILURE;
4202 }
4203
4204 /* perform dirname */
4205 slash = zend_memrchr(filename, '/', filename_len);
4206
4207 if (slash) {
4208 fullpath[dest_len + (slash - filename) + 1] = '\0';
4209 } else {
4210 fullpath[dest_len] = '\0';
4211 }
4212
4213 if (FAILURE == php_stream_stat_path(fullpath, &ssb)) {
4214 if (entry->is_dir) {
4215 if (!php_stream_mkdir(fullpath, entry->flags & PHAR_ENT_PERM_MASK, PHP_STREAM_MKDIR_RECURSIVE, NULL)) {
4216 spprintf(error, 4096, "Cannot extract \"%s\", could not create directory \"%s\"", entry->filename, fullpath);
4217 efree(fullpath);
4218 free(new_state.cwd);
4219 return FAILURE;
4220 }
4221 } else {
4222 if (!php_stream_mkdir(fullpath, 0777, PHP_STREAM_MKDIR_RECURSIVE, NULL)) {
4223 spprintf(error, 4096, "Cannot extract \"%s\", could not create directory \"%s\"", entry->filename, fullpath);
4224 efree(fullpath);
4225 free(new_state.cwd);
4226 return FAILURE;
4227 }
4228 }
4229 }
4230
4231 if (slash) {
4232 fullpath[dest_len + (slash - filename) + 1] = '/';
4233 } else {
4234 fullpath[dest_len] = '/';
4235 }
4236
4237 filename = NULL;
4238 free(new_state.cwd);
4239 /* it is a standalone directory, job done */
4240 if (entry->is_dir) {
4241 efree(fullpath);
4242 return SUCCESS;
4243 }
4244
4245 #if PHP_API_VERSION < 20100412
4246 fp = php_stream_open_wrapper(fullpath, "w+b", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
4247 #else
4248 fp = php_stream_open_wrapper(fullpath, "w+b", REPORT_ERRORS, NULL);
4249 #endif
4250
4251 if (!fp) {
4252 spprintf(error, 4096, "Cannot extract \"%s\", could not open for writing \"%s\"", entry->filename, fullpath);
4253 efree(fullpath);
4254 return FAILURE;
4255 }
4256
4257 if (!phar_get_efp(entry, 0 TSRMLS_CC)) {
4258 if (FAILURE == phar_open_entry_fp(entry, error, 1 TSRMLS_CC)) {
4259 if (error) {
4260 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", unable to open internal file pointer: %s", entry->filename, fullpath, *error);
4261 } else {
4262 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", unable to open internal file pointer", entry->filename, fullpath);
4263 }
4264 efree(fullpath);
4265 php_stream_close(fp);
4266 return FAILURE;
4267 }
4268 }
4269
4270 if (FAILURE == phar_seek_efp(entry, 0, SEEK_SET, 0, 0 TSRMLS_CC)) {
4271 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", unable to seek internal file pointer", entry->filename, fullpath);
4272 efree(fullpath);
4273 php_stream_close(fp);
4274 return FAILURE;
4275 }
4276
4277 if (SUCCESS != phar_stream_copy_to_stream(phar_get_efp(entry, 0 TSRMLS_CC), fp, entry->uncompressed_filesize, NULL)) {
4278 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", copying contents failed", entry->filename, fullpath);
4279 efree(fullpath);
4280 php_stream_close(fp);
4281 return FAILURE;
4282 }
4283
4284 php_stream_close(fp);
4285 mode = (mode_t) entry->flags & PHAR_ENT_PERM_MASK;
4286
4287 if (FAILURE == VCWD_CHMOD(fullpath, mode)) {
4288 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", setting file permissions failed", entry->filename, fullpath);
4289 efree(fullpath);
4290 return FAILURE;
4291 }
4292
4293 efree(fullpath);
4294 return SUCCESS;
4295 }
4296 /* }}} */
4297
4298 /* {{{ proto bool Phar::extractTo(string pathto[[, mixed files], bool overwrite])
4299 * Extract one or more file from a phar archive, optionally overwriting existing files
4300 */
PHP_METHOD(Phar,extractTo)4301 PHP_METHOD(Phar, extractTo)
4302 {
4303 char *error = NULL;
4304 php_stream *fp;
4305 php_stream_statbuf ssb;
4306 phar_entry_info *entry;
4307 char *pathto, *filename, *actual;
4308 int pathto_len, filename_len;
4309 int ret, i;
4310 int nelems;
4311 zval *zval_files = NULL;
4312 zend_bool overwrite = 0;
4313
4314 PHAR_ARCHIVE_OBJECT();
4315
4316 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|z!b", &pathto, &pathto_len, &zval_files, &overwrite) == FAILURE) {
4317 return;
4318 }
4319
4320 fp = php_stream_open_wrapper(phar_obj->arc.archive->fname, "rb", IGNORE_URL|STREAM_MUST_SEEK, &actual);
4321
4322 if (!fp) {
4323 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC,
4324 "Invalid argument, %s cannot be found", phar_obj->arc.archive->fname);
4325 return;
4326 }
4327
4328 efree(actual);
4329 php_stream_close(fp);
4330
4331 if (pathto_len < 1) {
4332 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC,
4333 "Invalid argument, extraction path must be non-zero length");
4334 return;
4335 }
4336
4337 if (pathto_len >= MAXPATHLEN) {
4338 char *tmp = estrndup(pathto, 50);
4339 /* truncate for error message */
4340 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC, "Cannot extract to \"%s...\", destination directory is too long for filesystem", tmp);
4341 efree(tmp);
4342 return;
4343 }
4344
4345 if (php_stream_stat_path(pathto, &ssb) < 0) {
4346 ret = php_stream_mkdir(pathto, 0777, PHP_STREAM_MKDIR_RECURSIVE, NULL);
4347 if (!ret) {
4348 zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC,
4349 "Unable to create path \"%s\" for extraction", pathto);
4350 return;
4351 }
4352 } else if (!(ssb.sb.st_mode & S_IFDIR)) {
4353 zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC,
4354 "Unable to use path \"%s\" for extraction, it is a file, must be a directory", pathto);
4355 return;
4356 }
4357
4358 if (zval_files) {
4359 switch (Z_TYPE_P(zval_files)) {
4360 case IS_NULL:
4361 goto all_files;
4362 case IS_STRING:
4363 filename = Z_STRVAL_P(zval_files);
4364 filename_len = Z_STRLEN_P(zval_files);
4365 break;
4366 case IS_ARRAY:
4367 nelems = zend_hash_num_elements(Z_ARRVAL_P(zval_files));
4368 if (nelems == 0 ) {
4369 RETURN_FALSE;
4370 }
4371 for (i = 0; i < nelems; i++) {
4372 zval **zval_file;
4373 if (zend_hash_index_find(Z_ARRVAL_P(zval_files), i, (void **) &zval_file) == SUCCESS) {
4374 switch (Z_TYPE_PP(zval_file)) {
4375 case IS_STRING:
4376 break;
4377 default:
4378 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC,
4379 "Invalid argument, array of filenames to extract contains non-string value");
4380 return;
4381 }
4382 if (FAILURE == zend_hash_find(&phar_obj->arc.archive->manifest, Z_STRVAL_PP(zval_file), Z_STRLEN_PP(zval_file), (void **)&entry)) {
4383 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC,
4384 "Phar Error: attempted to extract non-existent file \"%s\" from phar \"%s\"", Z_STRVAL_PP(zval_file), phar_obj->arc.archive->fname);
4385 }
4386 if (FAILURE == phar_extract_file(overwrite, entry, pathto, pathto_len, &error TSRMLS_CC)) {
4387 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC,
4388 "Extraction from phar \"%s\" failed: %s", phar_obj->arc.archive->fname, error);
4389 efree(error);
4390 return;
4391 }
4392 }
4393 }
4394 RETURN_TRUE;
4395 default:
4396 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0 TSRMLS_CC,
4397 "Invalid argument, expected a filename (string) or array of filenames");
4398 return;
4399 }
4400
4401 if (FAILURE == zend_hash_find(&phar_obj->arc.archive->manifest, filename, filename_len, (void **)&entry)) {
4402 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC,
4403 "Phar Error: attempted to extract non-existent file \"%s\" from phar \"%s\"", filename, phar_obj->arc.archive->fname);
4404 return;
4405 }
4406
4407 if (FAILURE == phar_extract_file(overwrite, entry, pathto, pathto_len, &error TSRMLS_CC)) {
4408 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC,
4409 "Extraction from phar \"%s\" failed: %s", phar_obj->arc.archive->fname, error);
4410 efree(error);
4411 return;
4412 }
4413 } else {
4414 phar_archive_data *phar;
4415 all_files:
4416 phar = phar_obj->arc.archive;
4417 /* Extract all files */
4418 if (!zend_hash_num_elements(&(phar->manifest))) {
4419 RETURN_TRUE;
4420 }
4421
4422 for (zend_hash_internal_pointer_reset(&phar->manifest);
4423 zend_hash_has_more_elements(&phar->manifest) == SUCCESS;
4424 zend_hash_move_forward(&phar->manifest)) {
4425
4426 if (zend_hash_get_current_data(&phar->manifest, (void **)&entry) == FAILURE) {
4427 continue;
4428 }
4429
4430 if (FAILURE == phar_extract_file(overwrite, entry, pathto, pathto_len, &error TSRMLS_CC)) {
4431 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC,
4432 "Extraction from phar \"%s\" failed: %s", phar->fname, error);
4433 efree(error);
4434 return;
4435 }
4436 }
4437 }
4438 RETURN_TRUE;
4439 }
4440 /* }}} */
4441
4442
4443 /* {{{ proto void PharFileInfo::__construct(string entry)
4444 * Construct a Phar entry object
4445 */
PHP_METHOD(PharFileInfo,__construct)4446 PHP_METHOD(PharFileInfo, __construct)
4447 {
4448 char *fname, *arch, *entry, *error;
4449 int fname_len, arch_len, entry_len;
4450 phar_entry_object *entry_obj;
4451 phar_entry_info *entry_info;
4452 phar_archive_data *phar_data;
4453 zval *zobj = getThis(), arg1;
4454
4455 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p", &fname, &fname_len) == FAILURE) {
4456 return;
4457 }
4458
4459 entry_obj = (phar_entry_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
4460
4461 if (entry_obj->ent.entry) {
4462 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Cannot call constructor twice");
4463 return;
4464 }
4465
4466 if (fname_len < 7 || memcmp(fname, "phar://", 7) || phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0 TSRMLS_CC) == FAILURE) {
4467 zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC,
4468 "'%s' is not a valid phar archive URL (must have at least phar://filename.phar)", fname);
4469 return;
4470 }
4471
4472 if (phar_open_from_filename(arch, arch_len, NULL, 0, REPORT_ERRORS, &phar_data, &error TSRMLS_CC) == FAILURE) {
4473 efree(arch);
4474 efree(entry);
4475 if (error) {
4476 zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC,
4477 "Cannot open phar file '%s': %s", fname, error);
4478 efree(error);
4479 } else {
4480 zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC,
4481 "Cannot open phar file '%s'", fname);
4482 }
4483 return;
4484 }
4485
4486 if ((entry_info = phar_get_entry_info_dir(phar_data, entry, entry_len, 1, &error, 1 TSRMLS_CC)) == NULL) {
4487 zend_throw_exception_ex(spl_ce_RuntimeException, 0 TSRMLS_CC,
4488 "Cannot access phar file entry '%s' in archive '%s'%s%s", entry, arch, error ? ", " : "", error ? error : "");
4489 efree(arch);
4490 efree(entry);
4491 return;
4492 }
4493
4494 efree(arch);
4495 efree(entry);
4496
4497 entry_obj->ent.entry = entry_info;
4498
4499 INIT_PZVAL(&arg1);
4500 ZVAL_STRINGL(&arg1, fname, fname_len, 0);
4501
4502 zend_call_method_with_1_params(&zobj, Z_OBJCE_P(zobj),
4503 &spl_ce_SplFileInfo->constructor, "__construct", NULL, &arg1);
4504 }
4505 /* }}} */
4506
4507 #define PHAR_ENTRY_OBJECT() \
4508 phar_entry_object *entry_obj = (phar_entry_object*)zend_object_store_get_object(getThis() TSRMLS_CC); \
4509 if (!entry_obj->ent.entry) { \
4510 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
4511 "Cannot call method on an uninitialized PharFileInfo object"); \
4512 return; \
4513 }
4514
4515 /* {{{ proto void PharFileInfo::__destruct()
4516 * clean up directory-based entry objects
4517 */
PHP_METHOD(PharFileInfo,__destruct)4518 PHP_METHOD(PharFileInfo, __destruct)
4519 {
4520 phar_entry_object *entry_obj = (phar_entry_object*)zend_object_store_get_object(getThis() TSRMLS_CC); \
4521
4522 if (entry_obj->ent.entry && entry_obj->ent.entry->is_temp_dir) {
4523 if (entry_obj->ent.entry->filename) {
4524 efree(entry_obj->ent.entry->filename);
4525 entry_obj->ent.entry->filename = NULL;
4526 }
4527
4528 efree(entry_obj->ent.entry);
4529 entry_obj->ent.entry = NULL;
4530 }
4531 }
4532 /* }}} */
4533
4534 /* {{{ proto int PharFileInfo::getCompressedSize()
4535 * Returns the compressed size
4536 */
PHP_METHOD(PharFileInfo,getCompressedSize)4537 PHP_METHOD(PharFileInfo, getCompressedSize)
4538 {
4539 PHAR_ENTRY_OBJECT();
4540
4541 if (zend_parse_parameters_none() == FAILURE) {
4542 return;
4543 }
4544
4545 RETURN_LONG(entry_obj->ent.entry->compressed_filesize);
4546 }
4547 /* }}} */
4548
4549 /* {{{ proto bool PharFileInfo::isCompressed([int compression_type])
4550 * Returns whether the entry is compressed, and whether it is compressed with Phar::GZ or Phar::BZ2 if specified
4551 */
PHP_METHOD(PharFileInfo,isCompressed)4552 PHP_METHOD(PharFileInfo, isCompressed)
4553 {
4554 /* a number that is not Phar::GZ or Phar::BZ2 */
4555 long method = 9021976;
4556 PHAR_ENTRY_OBJECT();
4557
4558 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &method) == FAILURE) {
4559 return;
4560 }
4561
4562 switch (method) {
4563 case 9021976:
4564 RETURN_BOOL(entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSION_MASK);
4565 case PHAR_ENT_COMPRESSED_GZ:
4566 RETURN_BOOL(entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_GZ);
4567 case PHAR_ENT_COMPRESSED_BZ2:
4568 RETURN_BOOL(entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_BZ2);
4569 default:
4570 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
4571 "Unknown compression type specified"); \
4572 }
4573 }
4574 /* }}} */
4575
4576 /* {{{ proto int PharFileInfo::getCRC32()
4577 * Returns CRC32 code or throws an exception if not CRC checked
4578 */
PHP_METHOD(PharFileInfo,getCRC32)4579 PHP_METHOD(PharFileInfo, getCRC32)
4580 {
4581 PHAR_ENTRY_OBJECT();
4582
4583 if (zend_parse_parameters_none() == FAILURE) {
4584 return;
4585 }
4586
4587 if (entry_obj->ent.entry->is_dir) {
4588 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
4589 "Phar entry is a directory, does not have a CRC"); \
4590 return;
4591 }
4592
4593 if (entry_obj->ent.entry->is_crc_checked) {
4594 RETURN_LONG(entry_obj->ent.entry->crc32);
4595 } else {
4596 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
4597 "Phar entry was not CRC checked"); \
4598 }
4599 }
4600 /* }}} */
4601
4602 /* {{{ proto int PharFileInfo::isCRCChecked()
4603 * Returns whether file entry is CRC checked
4604 */
PHP_METHOD(PharFileInfo,isCRCChecked)4605 PHP_METHOD(PharFileInfo, isCRCChecked)
4606 {
4607 PHAR_ENTRY_OBJECT();
4608
4609 if (zend_parse_parameters_none() == FAILURE) {
4610 return;
4611 }
4612
4613 RETURN_BOOL(entry_obj->ent.entry->is_crc_checked);
4614 }
4615 /* }}} */
4616
4617 /* {{{ proto int PharFileInfo::getPharFlags()
4618 * Returns the Phar file entry flags
4619 */
PHP_METHOD(PharFileInfo,getPharFlags)4620 PHP_METHOD(PharFileInfo, getPharFlags)
4621 {
4622 PHAR_ENTRY_OBJECT();
4623
4624 if (zend_parse_parameters_none() == FAILURE) {
4625 return;
4626 }
4627
4628 RETURN_LONG(entry_obj->ent.entry->flags & ~(PHAR_ENT_PERM_MASK|PHAR_ENT_COMPRESSION_MASK));
4629 }
4630 /* }}} */
4631
4632 /* {{{ proto int PharFileInfo::chmod()
4633 * set the file permissions for the Phar. This only allows setting execution bit, read/write
4634 */
PHP_METHOD(PharFileInfo,chmod)4635 PHP_METHOD(PharFileInfo, chmod)
4636 {
4637 char *error;
4638 long perms;
4639 PHAR_ENTRY_OBJECT();
4640
4641 if (entry_obj->ent.entry->is_temp_dir) {
4642 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
4643 "Phar entry \"%s\" is a temporary directory (not an actual entry in the archive), cannot chmod", entry_obj->ent.entry->filename); \
4644 return;
4645 }
4646
4647 if (PHAR_G(readonly) && !entry_obj->ent.entry->phar->is_data) {
4648 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);
4649 return;
4650 }
4651
4652 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &perms) == FAILURE) {
4653 return;
4654 }
4655
4656 if (entry_obj->ent.entry->is_persistent) {
4657 phar_archive_data *phar = entry_obj->ent.entry->phar;
4658
4659 if (FAILURE == phar_copy_on_write(&phar TSRMLS_CC)) {
4660 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
4661 return;
4662 }
4663 /* re-populate after copy-on-write */
4664 zend_hash_find(&phar->manifest, entry_obj->ent.entry->filename, entry_obj->ent.entry->filename_len, (void **)&entry_obj->ent.entry);
4665 }
4666 /* clear permissions */
4667 entry_obj->ent.entry->flags &= ~PHAR_ENT_PERM_MASK;
4668 perms &= 0777;
4669 entry_obj->ent.entry->flags |= perms;
4670 entry_obj->ent.entry->old_flags = entry_obj->ent.entry->flags;
4671 entry_obj->ent.entry->phar->is_modified = 1;
4672 entry_obj->ent.entry->is_modified = 1;
4673
4674 /* hackish cache in php_stat needs to be cleared */
4675 /* if this code fails to work, check main/streams/streams.c, _php_stream_stat_path */
4676 if (BG(CurrentLStatFile)) {
4677 efree(BG(CurrentLStatFile));
4678 }
4679
4680 if (BG(CurrentStatFile)) {
4681 efree(BG(CurrentStatFile));
4682 }
4683
4684 BG(CurrentLStatFile) = NULL;
4685 BG(CurrentStatFile) = NULL;
4686 phar_flush(entry_obj->ent.entry->phar, 0, 0, 0, &error TSRMLS_CC);
4687
4688 if (error) {
4689 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
4690 efree(error);
4691 }
4692 }
4693 /* }}} */
4694
4695 /* {{{ proto int PharFileInfo::hasMetaData()
4696 * Returns the metadata of the entry
4697 */
PHP_METHOD(PharFileInfo,hasMetadata)4698 PHP_METHOD(PharFileInfo, hasMetadata)
4699 {
4700 PHAR_ENTRY_OBJECT();
4701
4702 if (zend_parse_parameters_none() == FAILURE) {
4703 return;
4704 }
4705
4706 RETURN_BOOL(entry_obj->ent.entry->metadata != NULL);
4707 }
4708 /* }}} */
4709
4710 /* {{{ proto int PharFileInfo::getMetaData()
4711 * Returns the metadata of the entry
4712 */
PHP_METHOD(PharFileInfo,getMetadata)4713 PHP_METHOD(PharFileInfo, getMetadata)
4714 {
4715 PHAR_ENTRY_OBJECT();
4716
4717 if (zend_parse_parameters_none() == FAILURE) {
4718 return;
4719 }
4720
4721 if (entry_obj->ent.entry->metadata) {
4722 if (entry_obj->ent.entry->is_persistent) {
4723 zval *ret;
4724 char *buf = estrndup((char *) entry_obj->ent.entry->metadata, entry_obj->ent.entry->metadata_len);
4725 /* assume success, we would have failed before */
4726 phar_parse_metadata(&buf, &ret, entry_obj->ent.entry->metadata_len TSRMLS_CC);
4727 efree(buf);
4728 RETURN_ZVAL(ret, 0, 1);
4729 }
4730 RETURN_ZVAL(entry_obj->ent.entry->metadata, 1, 0);
4731 }
4732 }
4733 /* }}} */
4734
4735 /* {{{ proto int PharFileInfo::setMetaData(mixed $metadata)
4736 * Sets the metadata of the entry
4737 */
PHP_METHOD(PharFileInfo,setMetadata)4738 PHP_METHOD(PharFileInfo, setMetadata)
4739 {
4740 char *error;
4741 zval *metadata;
4742
4743 PHAR_ENTRY_OBJECT();
4744
4745 if (PHAR_G(readonly) && !entry_obj->ent.entry->phar->is_data) {
4746 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Write operations disabled by the php.ini setting phar.readonly");
4747 return;
4748 }
4749
4750 if (entry_obj->ent.entry->is_temp_dir) {
4751 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
4752 "Phar entry is a temporary directory (not an actual entry in the archive), cannot set metadata"); \
4753 return;
4754 }
4755
4756 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &metadata) == FAILURE) {
4757 return;
4758 }
4759
4760 if (entry_obj->ent.entry->is_persistent) {
4761 phar_archive_data *phar = entry_obj->ent.entry->phar;
4762
4763 if (FAILURE == phar_copy_on_write(&phar TSRMLS_CC)) {
4764 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
4765 return;
4766 }
4767 /* re-populate after copy-on-write */
4768 zend_hash_find(&phar->manifest, entry_obj->ent.entry->filename, entry_obj->ent.entry->filename_len, (void **)&entry_obj->ent.entry);
4769 }
4770 if (entry_obj->ent.entry->metadata) {
4771 zval_ptr_dtor(&entry_obj->ent.entry->metadata);
4772 entry_obj->ent.entry->metadata = NULL;
4773 }
4774
4775 MAKE_STD_ZVAL(entry_obj->ent.entry->metadata);
4776 ZVAL_ZVAL(entry_obj->ent.entry->metadata, metadata, 1, 0);
4777
4778 entry_obj->ent.entry->is_modified = 1;
4779 entry_obj->ent.entry->phar->is_modified = 1;
4780 phar_flush(entry_obj->ent.entry->phar, 0, 0, 0, &error TSRMLS_CC);
4781
4782 if (error) {
4783 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
4784 efree(error);
4785 }
4786 }
4787 /* }}} */
4788
4789 /* {{{ proto bool PharFileInfo::delMetaData()
4790 * Deletes the metadata of the entry
4791 */
PHP_METHOD(PharFileInfo,delMetadata)4792 PHP_METHOD(PharFileInfo, delMetadata)
4793 {
4794 char *error;
4795
4796 PHAR_ENTRY_OBJECT();
4797
4798 if (zend_parse_parameters_none() == FAILURE) {
4799 return;
4800 }
4801
4802 if (PHAR_G(readonly) && !entry_obj->ent.entry->phar->is_data) {
4803 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, "Write operations disabled by the php.ini setting phar.readonly");
4804 return;
4805 }
4806
4807 if (entry_obj->ent.entry->is_temp_dir) {
4808 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
4809 "Phar entry is a temporary directory (not an actual entry in the archive), cannot delete metadata"); \
4810 return;
4811 }
4812
4813 if (entry_obj->ent.entry->metadata) {
4814 if (entry_obj->ent.entry->is_persistent) {
4815 phar_archive_data *phar = entry_obj->ent.entry->phar;
4816
4817 if (FAILURE == phar_copy_on_write(&phar TSRMLS_CC)) {
4818 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
4819 return;
4820 }
4821 /* re-populate after copy-on-write */
4822 zend_hash_find(&phar->manifest, entry_obj->ent.entry->filename, entry_obj->ent.entry->filename_len, (void **)&entry_obj->ent.entry);
4823 }
4824 zval_ptr_dtor(&entry_obj->ent.entry->metadata);
4825 entry_obj->ent.entry->metadata = NULL;
4826 entry_obj->ent.entry->is_modified = 1;
4827 entry_obj->ent.entry->phar->is_modified = 1;
4828
4829 phar_flush(entry_obj->ent.entry->phar, 0, 0, 0, &error TSRMLS_CC);
4830
4831 if (error) {
4832 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
4833 efree(error);
4834 RETURN_FALSE;
4835 } else {
4836 RETURN_TRUE;
4837 }
4838
4839 } else {
4840 RETURN_TRUE;
4841 }
4842 }
4843 /* }}} */
4844
4845 /* {{{ proto string PharFileInfo::getContent()
4846 * return the complete file contents of the entry (like file_get_contents)
4847 */
PHP_METHOD(PharFileInfo,getContent)4848 PHP_METHOD(PharFileInfo, getContent)
4849 {
4850 char *error;
4851 php_stream *fp;
4852 phar_entry_info *link;
4853
4854 PHAR_ENTRY_OBJECT();
4855
4856 if (zend_parse_parameters_none() == FAILURE) {
4857 return;
4858 }
4859
4860 if (entry_obj->ent.entry->is_dir) {
4861 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
4862 "Phar error: Cannot retrieve contents, \"%s\" in phar \"%s\" is a directory", entry_obj->ent.entry->filename, entry_obj->ent.entry->phar->fname);
4863 return;
4864 }
4865
4866 link = phar_get_link_source(entry_obj->ent.entry TSRMLS_CC);
4867
4868 if (!link) {
4869 link = entry_obj->ent.entry;
4870 }
4871
4872 if (SUCCESS != phar_open_entry_fp(link, &error, 0 TSRMLS_CC)) {
4873 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
4874 "Phar error: Cannot retrieve contents, \"%s\" in phar \"%s\": %s", entry_obj->ent.entry->filename, entry_obj->ent.entry->phar->fname, error);
4875 efree(error);
4876 return;
4877 }
4878
4879 if (!(fp = phar_get_efp(link, 0 TSRMLS_CC))) {
4880 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
4881 "Phar error: Cannot retrieve contents of \"%s\" in phar \"%s\"", entry_obj->ent.entry->filename, entry_obj->ent.entry->phar->fname);
4882 return;
4883 }
4884
4885 phar_seek_efp(link, 0, SEEK_SET, 0, 0 TSRMLS_CC);
4886 Z_TYPE_P(return_value) = IS_STRING;
4887 Z_STRVAL_P(return_value) = NULL;
4888 Z_STRLEN_P(return_value) = php_stream_copy_to_mem(fp, &(Z_STRVAL_P(return_value)), link->uncompressed_filesize, 0);
4889
4890 if (!Z_STRVAL_P(return_value)) {
4891 Z_STRVAL_P(return_value) = estrndup("", 0);
4892 }
4893 }
4894 /* }}} */
4895
4896 /* {{{ proto int PharFileInfo::compress(int compression_type)
4897 * Instructs the Phar class to compress the current file using zlib or bzip2 compression
4898 */
PHP_METHOD(PharFileInfo,compress)4899 PHP_METHOD(PharFileInfo, compress)
4900 {
4901 long method;
4902 char *error;
4903 PHAR_ENTRY_OBJECT();
4904
4905 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &method) == FAILURE) {
4906 return;
4907 }
4908
4909 if (entry_obj->ent.entry->is_tar) {
4910 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
4911 "Cannot compress with Gzip compression, not possible with tar-based phar archives");
4912 return;
4913 }
4914
4915 if (entry_obj->ent.entry->is_dir) {
4916 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
4917 "Phar entry is a directory, cannot set compression"); \
4918 return;
4919 }
4920
4921 if (PHAR_G(readonly) && !entry_obj->ent.entry->phar->is_data) {
4922 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
4923 "Phar is readonly, cannot change compression");
4924 return;
4925 }
4926
4927 if (entry_obj->ent.entry->is_deleted) {
4928 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
4929 "Cannot compress deleted file");
4930 return;
4931 }
4932
4933 if (entry_obj->ent.entry->is_persistent) {
4934 phar_archive_data *phar = entry_obj->ent.entry->phar;
4935
4936 if (FAILURE == phar_copy_on_write(&phar TSRMLS_CC)) {
4937 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
4938 return;
4939 }
4940 /* re-populate after copy-on-write */
4941 zend_hash_find(&phar->manifest, entry_obj->ent.entry->filename, entry_obj->ent.entry->filename_len, (void **)&entry_obj->ent.entry);
4942 }
4943 switch (method) {
4944 case PHAR_ENT_COMPRESSED_GZ:
4945 if (entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_GZ) {
4946 RETURN_TRUE;
4947 }
4948
4949 if ((entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_BZ2) != 0) {
4950 if (!PHAR_G(has_bz2)) {
4951 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
4952 "Cannot compress with gzip compression, file is already compressed with bzip2 compression and bz2 extension is not enabled, cannot decompress");
4953 return;
4954 }
4955
4956 /* decompress this file indirectly */
4957 if (SUCCESS != phar_open_entry_fp(entry_obj->ent.entry, &error, 1 TSRMLS_CC)) {
4958 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
4959 "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);
4960 efree(error);
4961 return;
4962 }
4963 }
4964
4965 if (!PHAR_G(has_zlib)) {
4966 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
4967 "Cannot compress with gzip compression, zlib extension is not enabled");
4968 return;
4969 }
4970
4971 entry_obj->ent.entry->old_flags = entry_obj->ent.entry->flags;
4972 entry_obj->ent.entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
4973 entry_obj->ent.entry->flags |= PHAR_ENT_COMPRESSED_GZ;
4974 break;
4975 case PHAR_ENT_COMPRESSED_BZ2:
4976 if (entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_BZ2) {
4977 RETURN_TRUE;
4978 }
4979
4980 if ((entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_GZ) != 0) {
4981 if (!PHAR_G(has_zlib)) {
4982 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
4983 "Cannot compress with bzip2 compression, file is already compressed with gzip compression and zlib extension is not enabled, cannot decompress");
4984 return;
4985 }
4986
4987 /* decompress this file indirectly */
4988 if (SUCCESS != phar_open_entry_fp(entry_obj->ent.entry, &error, 1 TSRMLS_CC)) {
4989 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
4990 "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);
4991 efree(error);
4992 return;
4993 }
4994 }
4995
4996 if (!PHAR_G(has_bz2)) {
4997 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
4998 "Cannot compress with bzip2 compression, bz2 extension is not enabled");
4999 return;
5000 }
5001 entry_obj->ent.entry->old_flags = entry_obj->ent.entry->flags;
5002 entry_obj->ent.entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
5003 entry_obj->ent.entry->flags |= PHAR_ENT_COMPRESSED_BZ2;
5004 break;
5005 default:
5006 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
5007 "Unknown compression type specified"); \
5008 }
5009
5010 entry_obj->ent.entry->phar->is_modified = 1;
5011 entry_obj->ent.entry->is_modified = 1;
5012 phar_flush(entry_obj->ent.entry->phar, 0, 0, 0, &error TSRMLS_CC);
5013
5014 if (error) {
5015 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
5016 efree(error);
5017 }
5018
5019 RETURN_TRUE;
5020 }
5021 /* }}} */
5022
5023 /* {{{ proto int PharFileInfo::decompress()
5024 * Instructs the Phar class to decompress the current file
5025 */
PHP_METHOD(PharFileInfo,decompress)5026 PHP_METHOD(PharFileInfo, decompress)
5027 {
5028 char *error;
5029 PHAR_ENTRY_OBJECT();
5030
5031 if (zend_parse_parameters_none() == FAILURE) {
5032 return;
5033 }
5034
5035 if (entry_obj->ent.entry->is_dir) {
5036 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC, \
5037 "Phar entry is a directory, cannot set compression"); \
5038 return;
5039 }
5040
5041 if ((entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSION_MASK) == 0) {
5042 RETURN_TRUE;
5043 }
5044
5045 if (PHAR_G(readonly) && !entry_obj->ent.entry->phar->is_data) {
5046 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
5047 "Phar is readonly, cannot decompress");
5048 return;
5049 }
5050
5051 if (entry_obj->ent.entry->is_deleted) {
5052 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
5053 "Cannot compress deleted file");
5054 return;
5055 }
5056
5057 if ((entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_GZ) != 0 && !PHAR_G(has_zlib)) {
5058 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
5059 "Cannot decompress Gzip-compressed file, zlib extension is not enabled");
5060 return;
5061 }
5062
5063 if ((entry_obj->ent.entry->flags & PHAR_ENT_COMPRESSED_BZ2) != 0 && !PHAR_G(has_bz2)) {
5064 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0 TSRMLS_CC,
5065 "Cannot decompress Bzip2-compressed file, bz2 extension is not enabled");
5066 return;
5067 }
5068
5069 if (entry_obj->ent.entry->is_persistent) {
5070 phar_archive_data *phar = entry_obj->ent.entry->phar;
5071
5072 if (FAILURE == phar_copy_on_write(&phar TSRMLS_CC)) {
5073 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
5074 return;
5075 }
5076 /* re-populate after copy-on-write */
5077 zend_hash_find(&phar->manifest, entry_obj->ent.entry->filename, entry_obj->ent.entry->filename_len, (void **)&entry_obj->ent.entry);
5078 }
5079 if (!entry_obj->ent.entry->fp) {
5080 if (FAILURE == phar_open_archive_fp(entry_obj->ent.entry->phar TSRMLS_CC)) {
5081 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);
5082 return;
5083 }
5084 entry_obj->ent.entry->fp_type = PHAR_FP;
5085 }
5086
5087 entry_obj->ent.entry->old_flags = entry_obj->ent.entry->flags;
5088 entry_obj->ent.entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
5089 entry_obj->ent.entry->phar->is_modified = 1;
5090 entry_obj->ent.entry->is_modified = 1;
5091 phar_flush(entry_obj->ent.entry->phar, 0, 0, 0, &error TSRMLS_CC);
5092
5093 if (error) {
5094 zend_throw_exception_ex(phar_ce_PharException, 0 TSRMLS_CC, "%s", error);
5095 efree(error);
5096 }
5097 RETURN_TRUE;
5098 }
5099 /* }}} */
5100
5101 #endif /* HAVE_SPL */
5102
5103 /* {{{ phar methods */
5104 PHAR_ARG_INFO
5105 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar___construct, 0, 0, 1)
5106 ZEND_ARG_INFO(0, filename)
5107 ZEND_ARG_INFO(0, flags)
5108 ZEND_ARG_INFO(0, alias)
5109 ZEND_ARG_INFO(0, fileformat)
5110 ZEND_END_ARG_INFO()
5111
5112 PHAR_ARG_INFO
5113 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_createDS, 0, 0, 0)
5114 ZEND_ARG_INFO(0, index)
5115 ZEND_ARG_INFO(0, webindex)
5116 ZEND_END_ARG_INFO()
5117
5118 PHAR_ARG_INFO
5119 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_cancompress, 0, 0, 0)
5120 ZEND_ARG_INFO(0, method)
5121 ZEND_END_ARG_INFO()
5122
5123 PHAR_ARG_INFO
5124 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_isvalidpharfilename, 0, 0, 1)
5125 ZEND_ARG_INFO(0, filename)
5126 ZEND_ARG_INFO(0, executable)
5127 ZEND_END_ARG_INFO()
5128
5129 PHAR_ARG_INFO
5130 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_loadPhar, 0, 0, 1)
5131 ZEND_ARG_INFO(0, filename)
5132 ZEND_ARG_INFO(0, alias)
5133 ZEND_END_ARG_INFO()
5134
5135 PHAR_ARG_INFO
5136 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_mapPhar, 0, 0, 0)
5137 ZEND_ARG_INFO(0, alias)
5138 ZEND_ARG_INFO(0, offset)
5139 ZEND_END_ARG_INFO()
5140
5141 PHAR_ARG_INFO
5142 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_mount, 0, 0, 2)
5143 ZEND_ARG_INFO(0, inphar)
5144 ZEND_ARG_INFO(0, externalfile)
5145 ZEND_END_ARG_INFO()
5146
5147 PHAR_ARG_INFO
5148 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_mungServer, 0, 0, 1)
5149 ZEND_ARG_INFO(0, munglist)
5150 ZEND_END_ARG_INFO()
5151
5152 PHAR_ARG_INFO
5153 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_webPhar, 0, 0, 0)
5154 ZEND_ARG_INFO(0, alias)
5155 ZEND_ARG_INFO(0, index)
5156 ZEND_ARG_INFO(0, f404)
5157 ZEND_ARG_INFO(0, mimetypes)
5158 ZEND_ARG_INFO(0, rewrites)
5159 ZEND_END_ARG_INFO()
5160
5161 PHAR_ARG_INFO
5162 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_running, 0, 0, 1)
5163 ZEND_ARG_INFO(0, retphar)
5164 ZEND_END_ARG_INFO()
5165
5166 PHAR_ARG_INFO
5167 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_ua, 0, 0, 1)
5168 ZEND_ARG_INFO(0, archive)
5169 ZEND_END_ARG_INFO()
5170
5171 #if HAVE_SPL
5172 PHAR_ARG_INFO
5173 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_build, 0, 0, 1)
5174 ZEND_ARG_INFO(0, iterator)
5175 ZEND_ARG_INFO(0, base_directory)
5176 ZEND_END_ARG_INFO()
5177
5178 PHAR_ARG_INFO
5179 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_conv, 0, 0, 0)
5180 ZEND_ARG_INFO(0, format)
5181 ZEND_ARG_INFO(0, compression_type)
5182 ZEND_ARG_INFO(0, file_ext)
5183 ZEND_END_ARG_INFO()
5184
5185 PHAR_ARG_INFO
5186 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_comps, 0, 0, 1)
5187 ZEND_ARG_INFO(0, compression_type)
5188 ZEND_ARG_INFO(0, file_ext)
5189 ZEND_END_ARG_INFO()
5190
5191 PHAR_ARG_INFO
5192 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_decomp, 0, 0, 0)
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_comp, 0, 0, 1)
5198 ZEND_ARG_INFO(0, compression_type)
5199 ZEND_END_ARG_INFO()
5200
5201 PHAR_ARG_INFO
5202 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_compo, 0, 0, 0)
5203 ZEND_ARG_INFO(0, compression_type)
5204 ZEND_END_ARG_INFO()
5205
5206 PHAR_ARG_INFO
5207 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_copy, 0, 0, 2)
5208 ZEND_ARG_INFO(0, newfile)
5209 ZEND_ARG_INFO(0, oldfile)
5210 ZEND_END_ARG_INFO()
5211
5212 PHAR_ARG_INFO
5213 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_delete, 0, 0, 1)
5214 ZEND_ARG_INFO(0, entry)
5215 ZEND_END_ARG_INFO()
5216
5217 PHAR_ARG_INFO
5218 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_fromdir, 0, 0, 1)
5219 ZEND_ARG_INFO(0, base_dir)
5220 ZEND_ARG_INFO(0, regex)
5221 ZEND_END_ARG_INFO()
5222
5223 PHAR_ARG_INFO
5224 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_offsetExists, 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_offsetSet, 0, 0, 2)
5230 ZEND_ARG_INFO(0, entry)
5231 ZEND_ARG_INFO(0, value)
5232 ZEND_END_ARG_INFO()
5233
5234 PHAR_ARG_INFO
5235 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_setAlias, 0, 0, 1)
5236 ZEND_ARG_INFO(0, alias)
5237 ZEND_END_ARG_INFO()
5238
5239 PHAR_ARG_INFO
5240 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_setMetadata, 0, 0, 1)
5241 ZEND_ARG_INFO(0, metadata)
5242 ZEND_END_ARG_INFO()
5243
5244 PHAR_ARG_INFO
5245 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_setSigAlgo, 0, 0, 1)
5246 ZEND_ARG_INFO(0, algorithm)
5247 ZEND_ARG_INFO(0, privatekey)
5248 ZEND_END_ARG_INFO()
5249
5250 PHAR_ARG_INFO
5251 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_setStub, 0, 0, 1)
5252 ZEND_ARG_INFO(0, newstub)
5253 ZEND_ARG_INFO(0, maxlen)
5254 ZEND_END_ARG_INFO()
5255
5256 PHAR_ARG_INFO
5257 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_emptydir, 0, 0, 0)
5258 ZEND_ARG_INFO(0, dirname)
5259 ZEND_END_ARG_INFO()
5260
5261 PHAR_ARG_INFO
5262 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_extract, 0, 0, 1)
5263 ZEND_ARG_INFO(0, pathto)
5264 ZEND_ARG_INFO(0, files)
5265 ZEND_ARG_INFO(0, overwrite)
5266 ZEND_END_ARG_INFO()
5267
5268 PHAR_ARG_INFO
5269 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_addfile, 0, 0, 1)
5270 ZEND_ARG_INFO(0, filename)
5271 ZEND_ARG_INFO(0, localname)
5272 ZEND_END_ARG_INFO()
5273
5274 PHAR_ARG_INFO
5275 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_fromstring, 0, 0, 1)
5276 ZEND_ARG_INFO(0, localname)
5277 ZEND_ARG_INFO(0, contents)
5278 ZEND_END_ARG_INFO()
5279
5280 PHAR_ARG_INFO
5281 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_isff, 0, 0, 1)
5282 ZEND_ARG_INFO(0, fileformat)
5283 ZEND_END_ARG_INFO()
5284
5285 PHAR_ARG_INFO
5286 ZEND_BEGIN_ARG_INFO(arginfo_phar__void, 0)
5287 ZEND_END_ARG_INFO()
5288
5289
5290 #endif /* HAVE_SPL */
5291
5292 zend_function_entry php_archive_methods[] = {
5293 #if !HAVE_SPL
5294 PHP_ME(Phar, __construct, arginfo_phar___construct, ZEND_ACC_PRIVATE)
5295 #else
5296 PHP_ME(Phar, __construct, arginfo_phar___construct, ZEND_ACC_PUBLIC)
5297 PHP_ME(Phar, __destruct, arginfo_phar__void, ZEND_ACC_PUBLIC)
5298 PHP_ME(Phar, addEmptyDir, arginfo_phar_emptydir, ZEND_ACC_PUBLIC)
5299 PHP_ME(Phar, addFile, arginfo_phar_addfile, ZEND_ACC_PUBLIC)
5300 PHP_ME(Phar, addFromString, arginfo_phar_fromstring, ZEND_ACC_PUBLIC)
5301 PHP_ME(Phar, buildFromDirectory, arginfo_phar_fromdir, ZEND_ACC_PUBLIC)
5302 PHP_ME(Phar, buildFromIterator, arginfo_phar_build, ZEND_ACC_PUBLIC)
5303 PHP_ME(Phar, compressFiles, arginfo_phar_comp, ZEND_ACC_PUBLIC)
5304 PHP_ME(Phar, decompressFiles, arginfo_phar__void, ZEND_ACC_PUBLIC)
5305 PHP_ME(Phar, compress, arginfo_phar_comps, ZEND_ACC_PUBLIC)
5306 PHP_ME(Phar, decompress, arginfo_phar_decomp, ZEND_ACC_PUBLIC)
5307 PHP_ME(Phar, convertToExecutable, arginfo_phar_conv, ZEND_ACC_PUBLIC)
5308 PHP_ME(Phar, convertToData, arginfo_phar_conv, ZEND_ACC_PUBLIC)
5309 PHP_ME(Phar, copy, arginfo_phar_copy, ZEND_ACC_PUBLIC)
5310 PHP_ME(Phar, count, arginfo_phar__void, ZEND_ACC_PUBLIC)
5311 PHP_ME(Phar, delete, arginfo_phar_delete, ZEND_ACC_PUBLIC)
5312 PHP_ME(Phar, delMetadata, arginfo_phar__void, ZEND_ACC_PUBLIC)
5313 PHP_ME(Phar, extractTo, arginfo_phar_extract, ZEND_ACC_PUBLIC)
5314 PHP_ME(Phar, getAlias, arginfo_phar__void, ZEND_ACC_PUBLIC)
5315 PHP_ME(Phar, getPath, arginfo_phar__void, ZEND_ACC_PUBLIC)
5316 PHP_ME(Phar, getMetadata, arginfo_phar__void, ZEND_ACC_PUBLIC)
5317 PHP_ME(Phar, getModified, arginfo_phar__void, ZEND_ACC_PUBLIC)
5318 PHP_ME(Phar, getSignature, arginfo_phar__void, ZEND_ACC_PUBLIC)
5319 PHP_ME(Phar, getStub, arginfo_phar__void, ZEND_ACC_PUBLIC)
5320 PHP_ME(Phar, getVersion, arginfo_phar__void, ZEND_ACC_PUBLIC)
5321 PHP_ME(Phar, hasMetadata, arginfo_phar__void, ZEND_ACC_PUBLIC)
5322 PHP_ME(Phar, isBuffering, arginfo_phar__void, ZEND_ACC_PUBLIC)
5323 PHP_ME(Phar, isCompressed, arginfo_phar__void, ZEND_ACC_PUBLIC)
5324 PHP_ME(Phar, isFileFormat, arginfo_phar_isff, ZEND_ACC_PUBLIC)
5325 PHP_ME(Phar, isWritable, arginfo_phar__void, ZEND_ACC_PUBLIC)
5326 PHP_ME(Phar, offsetExists, arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
5327 PHP_ME(Phar, offsetGet, arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
5328 PHP_ME(Phar, offsetSet, arginfo_phar_offsetSet, ZEND_ACC_PUBLIC)
5329 PHP_ME(Phar, offsetUnset, arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
5330 PHP_ME(Phar, setAlias, arginfo_phar_setAlias, ZEND_ACC_PUBLIC)
5331 PHP_ME(Phar, setDefaultStub, arginfo_phar_createDS, ZEND_ACC_PUBLIC)
5332 PHP_ME(Phar, setMetadata, arginfo_phar_setMetadata, ZEND_ACC_PUBLIC)
5333 PHP_ME(Phar, setSignatureAlgorithm, arginfo_phar_setSigAlgo, ZEND_ACC_PUBLIC)
5334 PHP_ME(Phar, setStub, arginfo_phar_setStub, ZEND_ACC_PUBLIC)
5335 PHP_ME(Phar, startBuffering, arginfo_phar__void, ZEND_ACC_PUBLIC)
5336 PHP_ME(Phar, stopBuffering, arginfo_phar__void, ZEND_ACC_PUBLIC)
5337 #endif
5338 /* static member functions */
5339 PHP_ME(Phar, apiVersion, arginfo_phar__void, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5340 PHP_ME(Phar, canCompress, arginfo_phar_cancompress, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5341 PHP_ME(Phar, canWrite, arginfo_phar__void, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5342 PHP_ME(Phar, createDefaultStub, arginfo_phar_createDS, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5343 PHP_ME(Phar, getSupportedCompression,arginfo_phar__void, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5344 PHP_ME(Phar, getSupportedSignatures,arginfo_phar__void, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5345 PHP_ME(Phar, interceptFileFuncs, arginfo_phar__void, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5346 PHP_ME(Phar, isValidPharFilename, arginfo_phar_isvalidpharfilename, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5347 PHP_ME(Phar, loadPhar, arginfo_phar_loadPhar, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5348 PHP_ME(Phar, mapPhar, arginfo_phar_mapPhar, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5349 PHP_ME(Phar, running, arginfo_phar_running, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5350 PHP_ME(Phar, mount, arginfo_phar_mount, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5351 PHP_ME(Phar, mungServer, arginfo_phar_mungServer, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5352 PHP_ME(Phar, unlinkArchive, arginfo_phar_ua, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5353 PHP_ME(Phar, webPhar, arginfo_phar_webPhar, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5354 PHP_FE_END
5355 };
5356
5357 #if HAVE_SPL
5358 PHAR_ARG_INFO
5359 ZEND_BEGIN_ARG_INFO_EX(arginfo_entry___construct, 0, 0, 1)
5360 ZEND_ARG_INFO(0, filename)
5361 ZEND_END_ARG_INFO()
5362
5363 PHAR_ARG_INFO
5364 ZEND_BEGIN_ARG_INFO_EX(arginfo_entry_chmod, 0, 0, 1)
5365 ZEND_ARG_INFO(0, perms)
5366 ZEND_END_ARG_INFO()
5367
5368 zend_function_entry php_entry_methods[] = {
5369 PHP_ME(PharFileInfo, __construct, arginfo_entry___construct, ZEND_ACC_PUBLIC)
5370 PHP_ME(PharFileInfo, __destruct, arginfo_phar__void, ZEND_ACC_PUBLIC)
5371 PHP_ME(PharFileInfo, chmod, arginfo_entry_chmod, ZEND_ACC_PUBLIC)
5372 PHP_ME(PharFileInfo, compress, arginfo_phar_comp, ZEND_ACC_PUBLIC)
5373 PHP_ME(PharFileInfo, decompress, arginfo_phar__void, ZEND_ACC_PUBLIC)
5374 PHP_ME(PharFileInfo, delMetadata, arginfo_phar__void, ZEND_ACC_PUBLIC)
5375 PHP_ME(PharFileInfo, getCompressedSize, arginfo_phar__void, ZEND_ACC_PUBLIC)
5376 PHP_ME(PharFileInfo, getCRC32, arginfo_phar__void, ZEND_ACC_PUBLIC)
5377 PHP_ME(PharFileInfo, getContent, arginfo_phar__void, ZEND_ACC_PUBLIC)
5378 PHP_ME(PharFileInfo, getMetadata, arginfo_phar__void, ZEND_ACC_PUBLIC)
5379 PHP_ME(PharFileInfo, getPharFlags, arginfo_phar__void, ZEND_ACC_PUBLIC)
5380 PHP_ME(PharFileInfo, hasMetadata, arginfo_phar__void, ZEND_ACC_PUBLIC)
5381 PHP_ME(PharFileInfo, isCompressed, arginfo_phar_compo, ZEND_ACC_PUBLIC)
5382 PHP_ME(PharFileInfo, isCRCChecked, arginfo_phar__void, ZEND_ACC_PUBLIC)
5383 PHP_ME(PharFileInfo, setMetadata, arginfo_phar_setMetadata, ZEND_ACC_PUBLIC)
5384 PHP_FE_END
5385 };
5386 #endif /* HAVE_SPL */
5387
5388 zend_function_entry phar_exception_methods[] = {
5389 PHP_FE_END
5390 };
5391 /* }}} */
5392
5393 #define REGISTER_PHAR_CLASS_CONST_LONG(class_name, const_name, value) \
5394 zend_declare_class_constant_long(class_name, const_name, sizeof(const_name)-1, (long)value TSRMLS_CC);
5395
5396 #define phar_exception_get_default() zend_exception_get_default(TSRMLS_C)
5397
phar_object_init(TSRMLS_D)5398 void phar_object_init(TSRMLS_D) /* {{{ */
5399 {
5400 zend_class_entry ce;
5401
5402 INIT_CLASS_ENTRY(ce, "PharException", phar_exception_methods);
5403 phar_ce_PharException = zend_register_internal_class_ex(&ce, phar_exception_get_default(), NULL TSRMLS_CC);
5404
5405 #if HAVE_SPL
5406 INIT_CLASS_ENTRY(ce, "Phar", php_archive_methods);
5407 phar_ce_archive = zend_register_internal_class_ex(&ce, spl_ce_RecursiveDirectoryIterator, NULL TSRMLS_CC);
5408
5409 zend_class_implements(phar_ce_archive TSRMLS_CC, 2, spl_ce_Countable, zend_ce_arrayaccess);
5410
5411 INIT_CLASS_ENTRY(ce, "PharData", php_archive_methods);
5412 phar_ce_data = zend_register_internal_class_ex(&ce, spl_ce_RecursiveDirectoryIterator, NULL TSRMLS_CC);
5413
5414 zend_class_implements(phar_ce_data TSRMLS_CC, 2, spl_ce_Countable, zend_ce_arrayaccess);
5415
5416 INIT_CLASS_ENTRY(ce, "PharFileInfo", php_entry_methods);
5417 phar_ce_entry = zend_register_internal_class_ex(&ce, spl_ce_SplFileInfo, NULL TSRMLS_CC);
5418 #else
5419 INIT_CLASS_ENTRY(ce, "Phar", php_archive_methods);
5420 phar_ce_archive = zend_register_internal_class(&ce TSRMLS_CC);
5421 phar_ce_archive->ce_flags |= ZEND_ACC_FINAL_CLASS;
5422
5423 INIT_CLASS_ENTRY(ce, "PharData", php_archive_methods);
5424 phar_ce_data = zend_register_internal_class(&ce TSRMLS_CC);
5425 phar_ce_data->ce_flags |= ZEND_ACC_FINAL_CLASS;
5426 #endif
5427
5428 REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "BZ2", PHAR_ENT_COMPRESSED_BZ2)
5429 REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "GZ", PHAR_ENT_COMPRESSED_GZ)
5430 REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "NONE", PHAR_ENT_COMPRESSED_NONE)
5431 REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "PHAR", PHAR_FORMAT_PHAR)
5432 REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "TAR", PHAR_FORMAT_TAR)
5433 REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "ZIP", PHAR_FORMAT_ZIP)
5434 REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "COMPRESSED", PHAR_ENT_COMPRESSION_MASK)
5435 REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "PHP", PHAR_MIME_PHP)
5436 REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "PHPS", PHAR_MIME_PHPS)
5437 REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "MD5", PHAR_SIG_MD5)
5438 REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "OPENSSL", PHAR_SIG_OPENSSL)
5439 REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA1", PHAR_SIG_SHA1)
5440 REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA256", PHAR_SIG_SHA256)
5441 REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA512", PHAR_SIG_SHA512)
5442 }
5443 /* }}} */
5444
5445 /*
5446 * Local variables:
5447 * tab-width: 4
5448 * c-basic-offset: 4
5449 * End:
5450 * vim600: noet sw=4 ts=4 fdm=marker
5451 * vim<600: noet sw=4 ts=4
5452 */
5453