Skip to content

Commit d515979

Browse files
committed
Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0: NEWS + bump zip version Fix #80833 ZipArchive::getStream doesn't use setPassword
2 parents c7fdf9c + fde24e4 commit d515979

File tree

4 files changed

+66
-31
lines changed

4 files changed

+66
-31
lines changed

ext/zip/php_zip.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2887,7 +2887,6 @@ PHP_METHOD(ZipArchive, getStream)
28872887
char *mode = "rb";
28882888
zend_string *filename;
28892889
php_stream *stream;
2890-
ze_zip_object *obj;
28912890

28922891
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &filename) == FAILURE) {
28932892
RETURN_THROWS();
@@ -2899,9 +2898,7 @@ PHP_METHOD(ZipArchive, getStream)
28992898
RETURN_FALSE;
29002899
}
29012900

2902-
obj = Z_ZIP_P(self);
2903-
2904-
stream = php_stream_zip_open(obj->filename, ZSTR_VAL(filename), mode STREAMS_CC);
2901+
stream = php_stream_zip_open(intern, ZSTR_VAL(filename), mode STREAMS_CC);
29052902
if (stream) {
29062903
php_stream_to_zval(stream, return_value);
29072904
} else {

ext/zip/php_zip.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extern zend_module_entry zip_module_entry;
3131
#define ZIP_OVERWRITE ZIP_TRUNCATE
3232
#endif
3333

34-
#define PHP_ZIP_VERSION "1.19.3"
34+
#define PHP_ZIP_VERSION "1.19.4"
3535

3636
#define ZIP_OPENBASEDIR_CHECKPATH(filename) php_check_open_basedir(filename)
3737

@@ -75,7 +75,7 @@ static inline ze_zip_object *php_zip_fetch_object(zend_object *obj) {
7575
#define Z_ZIP_P(zv) php_zip_fetch_object(Z_OBJ_P((zv)))
7676

7777
php_stream *php_stream_zip_opener(php_stream_wrapper *wrapper, const char *path, const char *mode, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC);
78-
php_stream *php_stream_zip_open(const char *filename, const char *path, const char *mode STREAMS_DC);
78+
php_stream *php_stream_zip_open(struct zip *arch, const char *path, const char *mode STREAMS_DC);
7979

8080
extern const php_stream_wrapper php_stream_zip_wrapper;
8181

ext/zip/tests/bug80833.phpt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
--TEST--
2+
Bug #80833 (ZipArchive::getStream doesn't use setPassword)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('zip')) die("skip zip extension not available");
6+
if (!method_exists('ZipArchive', 'setEncryptionName')) die('skip encryption not supported');
7+
?>
8+
--FILE--
9+
<?php
10+
$create_zip = new ZipArchive();
11+
$create_zip->open(__DIR__ . "/80833.zip", ZipArchive::CREATE);
12+
$create_zip->setPassword("default_password");
13+
$create_zip->addFromString("test.txt", "This is a test string.");
14+
$create_zip->setEncryptionName("test.txt", ZipArchive::EM_AES_256, "first_password");
15+
$create_zip->addFromString("test2.txt", "This is another test string.");
16+
$create_zip->setEncryptionName("test2.txt", ZipArchive::EM_AES_256, "second_password");
17+
$create_zip->close();
18+
19+
// Stream API
20+
$o = [
21+
'zip' => [
22+
'password' => "first_password",
23+
],
24+
];
25+
$c = stream_context_create($o);
26+
var_dump(file_get_contents("zip://" . __DIR__ . "/80833.zip#test.txt", false, $c));
27+
28+
// getStream method
29+
$extract_zip = new ZipArchive();
30+
$extract_zip->open(__DIR__ . "/80833.zip", ZipArchive::RDONLY);
31+
$extract_zip->setPassword("first_password");
32+
$file_stream = $extract_zip->getStream("test.txt");
33+
var_dump(stream_get_contents($file_stream));
34+
fclose($file_stream);
35+
$extract_zip->setPassword("second_password");
36+
$file_stream = $extract_zip->getStream("test2.txt");
37+
var_dump(stream_get_contents($file_stream));
38+
fclose($file_stream);
39+
40+
// Archive close before the stream
41+
$extract_zip->setPassword("first_password");
42+
$file_stream = $extract_zip->getStream("test.txt");
43+
$extract_zip->close();
44+
var_dump(stream_get_contents($file_stream));
45+
fclose($file_stream);
46+
?>
47+
--CLEAN--
48+
<?php
49+
@unlink(__DIR__ . "/80833.zip");
50+
?>
51+
--EXPECTF--
52+
string(22) "This is a test string."
53+
string(22) "This is a test string."
54+
string(28) "This is another test string."
55+
56+
Warning: stream_get_contents(): Zip stream error: Containing zip archive was closed in %s
57+
string(0) ""

ext/zip/zip_stream.c

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static ssize_t php_zip_ops_read(php_stream *stream, char *buf, size_t count)
4848
ssize_t n = 0;
4949
STREAM_DATA_FROM_STREAM();
5050

51-
if (self->za && self->zf) {
51+
if (self->zf) {
5252
n = zip_fread(self->zf, buf, count);
5353
if (n < 0) {
5454
#if LIBZIP_VERSION_MAJOR < 1
@@ -208,51 +208,32 @@ const php_stream_ops php_stream_zipio_ops = {
208208
};
209209

210210
/* {{{ php_stream_zip_open */
211-
php_stream *php_stream_zip_open(const char *filename, const char *path, const char *mode STREAMS_DC)
211+
php_stream *php_stream_zip_open(struct zip *arch, const char *path, const char *mode STREAMS_DC)
212212
{
213213
struct zip_file *zf = NULL;
214-
int err = 0;
215214

216215
php_stream *stream = NULL;
217216
struct php_zip_stream_data_t *self;
218-
struct zip *stream_za;
219217

220218
if (strncmp(mode,"r", strlen("r")) != 0) {
221219
return NULL;
222220
}
223221

224-
if (filename) {
225-
if (ZIP_OPENBASEDIR_CHECKPATH(filename)) {
226-
return NULL;
227-
}
228-
229-
/* duplicate to make the stream za independent (esp. for MSHUTDOWN) */
230-
stream_za = zip_open(filename, ZIP_CREATE, &err);
231-
if (!stream_za) {
232-
return NULL;
233-
}
234-
235-
zf = zip_fopen(stream_za, path, 0);
222+
if (arch) {
223+
zf = zip_fopen(arch, path, 0);
236224
if (zf) {
237225
self = emalloc(sizeof(*self));
238226

239-
self->za = stream_za;
227+
self->za = NULL; /* to keep it open on stream close */
240228
self->zf = zf;
241229
self->stream = NULL;
242230
self->cursor = 0;
243231
stream = php_stream_alloc(&php_stream_zipio_ops, self, NULL, mode);
244232
stream->orig_path = estrdup(path);
245-
} else {
246-
zip_close(stream_za);
247233
}
248234
}
249235

250-
if (!stream) {
251-
return NULL;
252-
} else {
253-
return stream;
254-
}
255-
236+
return stream;
256237
}
257238
/* }}} */
258239

0 commit comments

Comments
 (0)