Skip to content

Commit

Permalink
Merge branch 'main' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
ewilan-riviere committed Jan 18, 2025
2 parents 610dde5 + 2abe7c2 commit ac4deae
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dependabot-auto-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@v1.6.0
uses: dependabot/fetch-metadata@v2.2.0
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/fix-php-code-style-issues.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
ref: ${{ github.head_ref }}

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

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v5
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

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

## v2.3.0 - 2024-03-20

- 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.
- Add new `Archive::class` method `readFromString(string $contents, ?string $password = null, ?string $extension = null)` to read an archive from a string
- When you read RAR or 7z archives with `p7zip` binary, you can set manually the path to the binary using `overrideBinaryPath(string $path)` method.
- `getFiles()` method is now deprecated. Use `getFileItems()` instead.
- New method `getFileItem(string $path)` to get a single file item.

## v2.2.0 - 2023-12-06

Drop `symfony/process` from dependencies.
Expand Down
26 changes: 25 additions & 1 deletion src/Archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,31 @@ public static function read(string $path, ?string $password = null): BaseArchive
ArchiveEnum::pdf => ArchivePdf::class,
};

return $archive::read($self->path, $self->password);
try {
return $archive::read($self->path, $self->password);
} catch (\Throwable $originalException) {
if ($self->type === ArchiveEnum::zip && $extension === 'cbz') {
try {
// Sometimes files with cbz extension are actually misnamed rar files
return ArchiveRar::read($self->path, $self->password);
} catch (\Throwable) {
// If it's not a rar file, throw the original exception
throw $originalException;
}
}

if ($self->type === ArchiveEnum::rar && $extension === 'cbr') {
try {
// Sometimes files with cbr extension are actually misnamed zip files
return ArchiveZip::read($self->path, $self->password);
} catch (\Throwable) {
// If it's not a zip file, throw the original exception
throw $originalException;
}
}

throw $originalException;
}
}

/**
Expand Down
24 changes: 23 additions & 1 deletion src/Processes/SevenZipProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,28 @@ public static function test(bool $exception = true): bool
return true;
}

/**
* Escapes a string to be used as a shell argument.
*/
private function escapeArgument(?string $argument): string
{
if ($argument === '' || $argument === null) {
return '""';
}
if ('\\' !== \DIRECTORY_SEPARATOR) {
return "'".str_replace("'", "'\\''", $argument)."'";
}
if (str_contains($argument, "\0")) {
$argument = str_replace("\0", '?', $argument);
}
if (! preg_match('/[()%!^"<>&|\s]/', $argument)) {
return $argument;
}
$argument = preg_replace('/(\\\\+)$/', '$1$1', $argument);

return '"'.str_replace(['"', '^', '%', '!', "\n"], ['""', '"^^"', '"^%"', '"^!"', '!LF!'], $argument).'"';
}

/**
* @param string[] $args
* @return string[]
Expand All @@ -81,7 +103,7 @@ public function execute(string $command, array $args): array
$command = $this->binaryPath;
}

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

try {
exec($command, $output, $res);
Expand Down
3 changes: 1 addition & 2 deletions src/Readers/ArchivePdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ public function getContents(?ArchiveItem $file, bool $toBase64 = false): ?string
$imagick->clear();
$imagick->destroy();
} catch (\Throwable $th) {
// throw new \Exception("Error, {$file->getFilename()} is not an image");
error_log("Error, {$file->getFilename()} is not an image");
error_log("Error, {$file->getFilename()} Failed to extract page: {$th->getMessage()}");
}

if (! $content) {
Expand Down

0 comments on commit ac4deae

Please sign in to comment.