Skip to content

Commit

Permalink
Merge branch 'MDL-75488-master' of https://github.com/stevandoMoodle/…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Oct 6, 2022
2 parents 59ae667 + 33285ac commit 11df52d
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/thirdpartylibs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ All rights reserved.</copyright>
<location>zipstream</location>
<name>ZipStream-PHP</name>
<description>PHP ZIP Streaming Library</description>
<version>2.1.0</version>
<version>2.2.0</version>
<license>MIT</license>
<repository>https://github.com/maennchen/ZipStream-PHP</repository>
<copyrights>
Expand Down
1 change: 1 addition & 0 deletions lib/zipstream/src/Bigint.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public function getLowFF(bool $force = false): float
/**
* Check if is over 32
*
* @psalm-suppress ArgumentTypeCoercion
* @param bool $force
* @return bool
*/
Expand Down
14 changes: 12 additions & 2 deletions lib/zipstream/src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace ZipStream;

use Psr\Http\Message\StreamInterface;
use RuntimeException;
use ZipStream\Exception\EncodingException;
use ZipStream\Exception\FileNotFoundException;
use ZipStream\Exception\FileNotReadableException;
Expand Down Expand Up @@ -75,8 +76,9 @@ class File
* @var resource
*/
private $deflate;

/**
* @var resource
* @var \HashContext
*/
private $hash;

Expand Down Expand Up @@ -291,6 +293,13 @@ protected function buildZip64ExtraBlock(bool $force = false): string
$this->version = Version::ZIP64();
}

if ($this->bits & self::BIT_EFS_UTF8) {
// Put the tricky entry to
// force Linux unzip to lookup EFS flag.
$fields[] = ['v', 0x5653]; // Choose 'ZS' for proprietary usage
$fields[] = ['v', 0x0000]; // zero length
}

return ZipStream::packFields($fields);
}

Expand Down Expand Up @@ -366,7 +375,8 @@ protected function readStream(StreamInterface $stream, ?int $options = null): vo

protected function deflateInit(): void
{
$this->hash = hash_init(self::HASH_ALGORITHM);
$hash = hash_init(self::HASH_ALGORITHM);
$this->hash = $hash;
if ($this->method->equals(Method::DEFLATE())) {
$this->deflate = deflate_init(
ZLIB_ENCODING_RAW,
Expand Down
8 changes: 5 additions & 3 deletions lib/zipstream/src/Option/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace ZipStream\Option;

use Psr\Http\Message\StreamInterface;

final class Archive
{
const DEFAULT_DEFLATE_LEVEL = 6;
Expand Down Expand Up @@ -104,7 +106,7 @@ final class Archive
private $deflateLevel = 6;

/**
* @var resource
* @var StreamInterface|resource
*/
private $outputStream;

Expand Down Expand Up @@ -228,15 +230,15 @@ public function setContentType(string $contentType): void
}

/**
* @return resource
* @return StreamInterface|resource
*/
public function getOutputStream()
{
return $this->outputStream;
}

/**
* @param resource $outputStream
* @param StreamInterface|resource $outputStream
*/
public function setOutputStream($outputStream): void
{
Expand Down
11 changes: 6 additions & 5 deletions lib/zipstream/src/Option/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace ZipStream\Option;

use DateTime;
use DateTimeInterface;

final class File
{
Expand All @@ -20,7 +21,7 @@ final class File
*/
private $deflateLevel;
/**
* @var DateTime
* @var DateTimeInterface
*/
private $time;
/**
Expand Down Expand Up @@ -83,17 +84,17 @@ public function setDeflateLevel(int $deflateLevel): void
}

/**
* @return DateTime
* @return DateTimeInterface
*/
public function getTime(): DateTime
public function getTime(): DateTimeInterface
{
return $this->time;
}

/**
* @param DateTime $time
* @param DateTimeInterface $time
*/
public function setTime(DateTime $time): void
public function setTime(DateTimeInterface $time): void
{
$this->time = $time;
}
Expand Down
22 changes: 15 additions & 7 deletions lib/zipstream/src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function __toString(): string
{
try {
$this->seek(0);
} catch (\RuntimeException $e) {}
} catch (RuntimeException $e) {}
return (string) stream_get_contents($this->stream);
}

Expand All @@ -81,7 +81,7 @@ public function __toString(): string
* PHP $whence values for `fseek()`. SEEK_SET: Set position equal to
* offset bytes SEEK_CUR: Set position to current location plus offset
* SEEK_END: Set position to end-of-stream plus offset.
* @throws \RuntimeException on failure.
* @throws RuntimeException on failure.
*/
public function seek($offset, $whence = SEEK_SET): void
{
Expand Down Expand Up @@ -136,7 +136,7 @@ public function getSize(): ?int
* Returns the current position of the file read/write pointer
*
* @return int Position of the file pointer
* @throws \RuntimeException on error.
* @throws RuntimeException on error.
*/
public function tell(): int
{
Expand Down Expand Up @@ -165,7 +165,7 @@ public function eof(): bool
*
* @see seek()
* @link http://www.php.net/manual/en/function.fseek.php
* @throws \RuntimeException on failure.
* @throws RuntimeException on failure.
*/
public function rewind(): void
{
Expand All @@ -177,7 +177,7 @@ public function rewind(): void
*
* @param string $string The string that is to be written.
* @return int Returns the number of bytes written to the stream.
* @throws \RuntimeException on failure.
* @throws RuntimeException on failure.
*/
public function write($string): int
{
Expand All @@ -197,7 +197,11 @@ public function write($string): int
*/
public function isWritable(): bool
{
return preg_match('/[waxc+]/', $this->getMetadata('mode')) === 1;
$mode = $this->getMetadata('mode');
if (!is_string($mode)) {
throw new RuntimeException('Could not get stream mode from metadata!');
}
return preg_match('/[waxc+]/', $mode) === 1;
}

/**
Expand Down Expand Up @@ -229,7 +233,11 @@ public function read($length): string
*/
public function isReadable(): bool
{
return preg_match('/[r+]/', $this->getMetadata('mode')) === 1;
$mode = $this->getMetadata('mode');
if (!is_string($mode)) {
throw new RuntimeException('Could not get stream mode from metadata!');
}
return preg_match('/[r+]/', $mode) === 1;
}

/**
Expand Down
15 changes: 9 additions & 6 deletions lib/zipstream/src/ZipStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,9 @@ public function addFileFromStream(string $name, $stream, ?FileOptions $options =
*
* Examples:
*
* // create a temporary file stream and write text to it
* $fp = tmpfile();
* fwrite($fp, 'The quick brown fox jumped over the lazy dog.');
*
* $stream = $response->getBody();
* // add a file named 'streamfile.txt' from the content of the stream
* $x->addFileFromPsr7Stream('streamfile.txt', $fp);
* $x->addFileFromPsr7Stream('streamfile.txt', $stream);
*
* @return void
*/
Expand Down Expand Up @@ -459,7 +456,13 @@ public function send(string $str): void
}
$this->need_headers = false;

fwrite($this->opt->getOutputStream(), $str);
$outputStream = $this->opt->getOutputStream();

if ($outputStream instanceof StreamInterface) {
$outputStream->write($str);
} else {
fwrite($outputStream, $str);
}

if ($this->opt->isFlushOutput()) {
// flush output buffer if it is on and flushable
Expand Down

0 comments on commit 11df52d

Please sign in to comment.