Skip to content

Commit ac4deae

Browse files
Merge branch 'main' into develop
2 parents 610dde5 + 2abe7c2 commit ac4deae

File tree

6 files changed

+59
-6
lines changed

6 files changed

+59
-6
lines changed

.github/workflows/dependabot-auto-merge.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313

1414
- name: Dependabot metadata
1515
id: metadata
16-
uses: dependabot/fetch-metadata@v1.6.0
16+
uses: dependabot/fetch-metadata@v2.2.0
1717
with:
1818
github-token: "${{ secrets.GITHUB_TOKEN }}"
1919

.github/workflows/fix-php-code-style-issues.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
ref: ${{ github.head_ref }}
2020

2121
- name: Fix PHP code style issues
22-
uses: aglipanci/laravel-pint-action@2.3.1
22+
uses: aglipanci/laravel-pint-action@2.4
2323

2424
- name: Commit changes
2525
uses: stefanzweifel/git-auto-commit-action@v5

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
All notable changes to `php-archive` will be documented in this file.
44

5+
## v2.3.0 - 2024-03-20
6+
7+
- Add password option for ZIP, RAR and 7z files, using `read(string $path, ?string $password = null)` and `readFromString(string $contents, ?string $password = null, ?string $extension = null)` methods.
8+
- Add new `Archive::class` method `readFromString(string $contents, ?string $password = null, ?string $extension = null)` to read an archive from a string
9+
- When you read RAR or 7z archives with `p7zip` binary, you can set manually the path to the binary using `overrideBinaryPath(string $path)` method.
10+
- `getFiles()` method is now deprecated. Use `getFileItems()` instead.
11+
- New method `getFileItem(string $path)` to get a single file item.
12+
513
## v2.2.0 - 2023-12-06
614

715
Drop `symfony/process` from dependencies.

src/Archive.php

+25-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,31 @@ public static function read(string $path, ?string $password = null): BaseArchive
4242
ArchiveEnum::pdf => ArchivePdf::class,
4343
};
4444

45-
return $archive::read($self->path, $self->password);
45+
try {
46+
return $archive::read($self->path, $self->password);
47+
} catch (\Throwable $originalException) {
48+
if ($self->type === ArchiveEnum::zip && $extension === 'cbz') {
49+
try {
50+
// Sometimes files with cbz extension are actually misnamed rar files
51+
return ArchiveRar::read($self->path, $self->password);
52+
} catch (\Throwable) {
53+
// If it's not a rar file, throw the original exception
54+
throw $originalException;
55+
}
56+
}
57+
58+
if ($self->type === ArchiveEnum::rar && $extension === 'cbr') {
59+
try {
60+
// Sometimes files with cbr extension are actually misnamed zip files
61+
return ArchiveZip::read($self->path, $self->password);
62+
} catch (\Throwable) {
63+
// If it's not a zip file, throw the original exception
64+
throw $originalException;
65+
}
66+
}
67+
68+
throw $originalException;
69+
}
4670
}
4771

4872
/**

src/Processes/SevenZipProcess.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@ public static function test(bool $exception = true): bool
6565
return true;
6666
}
6767

68+
/**
69+
* Escapes a string to be used as a shell argument.
70+
*/
71+
private function escapeArgument(?string $argument): string
72+
{
73+
if ($argument === '' || $argument === null) {
74+
return '""';
75+
}
76+
if ('\\' !== \DIRECTORY_SEPARATOR) {
77+
return "'".str_replace("'", "'\\''", $argument)."'";
78+
}
79+
if (str_contains($argument, "\0")) {
80+
$argument = str_replace("\0", '?', $argument);
81+
}
82+
if (! preg_match('/[()%!^"<>&|\s]/', $argument)) {
83+
return $argument;
84+
}
85+
$argument = preg_replace('/(\\\\+)$/', '$1$1', $argument);
86+
87+
return '"'.str_replace(['"', '^', '%', '!', "\n"], ['""', '"^^"', '"^%"', '"^!"', '!LF!'], $argument).'"';
88+
}
89+
6890
/**
6991
* @param string[] $args
7092
* @return string[]
@@ -81,7 +103,7 @@ public function execute(string $command, array $args): array
81103
$command = $this->binaryPath;
82104
}
83105

84-
$command = "{$command} ".implode(' ', $args);
106+
$command = "{$command} ".implode(' ', array_map($this->escapeArgument(...), $args));
85107

86108
try {
87109
exec($command, $output, $res);

src/Readers/ArchivePdf.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ public function getContents(?ArchiveItem $file, bool $toBase64 = false): ?string
8484
$imagick->clear();
8585
$imagick->destroy();
8686
} catch (\Throwable $th) {
87-
// throw new \Exception("Error, {$file->getFilename()} is not an image");
88-
error_log("Error, {$file->getFilename()} is not an image");
87+
error_log("Error, {$file->getFilename()} Failed to extract page: {$th->getMessage()}");
8988
}
9089

9190
if (! $content) {

0 commit comments

Comments
 (0)