File: oo_encryption.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (66 lines) | stat: -rw-r--r-- 1,703 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
--TEST--
ZipArchive::setEncryption*() functions
--EXTENSIONS--
zip
--SKIPIF--
<?php
if (!method_exists('ZipArchive', 'setEncryptionName')) die('skip encryption not supported');
?>
--FILE--
<?php

$name = __DIR__ . '/encrypted.zip';
$pass = 'secret';

echo "== Write\n";
$zip  = new ZipArchive;
$r = $zip->open($name, ZIPARCHIVE::CREATE);
// Clear
$zip->addFromString('foo.txt', 'foo');
// Encrypted
$zip->addFromString('bar.txt', 'bar');
var_dump($zip->setEncryptionName('bar.txt', 9999, $pass)); // Fails
var_dump($zip->setEncryptionName('bar.txt', ZipArchive::EM_AES_256, $pass));
$zip->close();

echo "== Read\n";
$r = $zip->open($name);
$s = $zip->statName('foo.txt');
var_dump($s['encryption_method'] === ZipArchive::EM_NONE);
$s = $zip->statName('bar.txt');
var_dump($s['encryption_method'] === ZipArchive::EM_AES_256);
var_dump($zip->getFromName('foo.txt')); // Clear, ok
var_dump($zip->getFromName('bar.txt')); // Encrypted, fails
$zip->setPassword($pass);
var_dump($zip->getFromName('bar.txt')); // Ecnrypted, ok
$zip->close();

echo "== Stream\n";
var_dump(file_get_contents("zip://$name#foo.txt")); // Clear, ok
var_dump(file_get_contents("zip://$name#bar.txt")); // Encrypted, fails
$ctx = stream_context_create(array('zip' => array('password' => $pass)));
var_dump(file_get_contents("zip://$name#bar.txt", false, $ctx)); // Ecnrypted, ok
?>
== Done
--CLEAN--
<?php
$name = __DIR__ . '/encrypted.zip';
@unlink($name);
?>
--EXPECTF--
== Write
bool(false)
bool(true)
== Read
bool(true)
bool(true)
string(3) "foo"
bool(false)
string(3) "bar"
== Stream
string(3) "foo"

Warning: file_get_contents(%s): Failed to open stream: operation failed in %s on line %d
bool(false)
string(3) "bar"
== Done