setLogger($logger); $this->setBaseCommand($base_command); $this->setPasswords($passwords); } protected array $passwords; protected string $base_command; protected LoggerInterface $logger; public function getPasswords(): array { return $this->passwords; } public function getBaseCommand(): string { return $this->base_command; } public function getLogger(): LoggerInterface { return $this->logger; } public function addPassword(string $password): Decrypt { $this->passwords []= $password; return $this; } public function setPasswords(array $passwords): Decrypt { foreach ($passwords as $password) { $this->addPassword($password); } return $this; } public function setBaseCommand(string $command): Decrypt { $this->base_command = $command; return $this; } public function setLogger(LoggerInterface $logger): Decrypt { $this->logger = $logger; return $this; } public function isEncrypted(string $filename): bool { if (!file_exists($filename)) { throw new \InvalidArgumentException("File not found {$filename}"); } $escaped_filename = escapeshellarg($filename); $cmd = "{$this->getBaseCommand()} --is-encrypted {$escaped_filename}"; exec($cmd, $output, $retcode); return $retcode == 0; } public function buildCommand(string $in_file, string $out_file, string $password): string { return $this->getBaseCommand() . ' -password=' . escapeshellarg($password) . ' -decrypt ' . escapeshellarg($in_file) . ' ' . escapeshellarg($out_file); } public function runCommand(string $in_file, string $out_file): bool { if (file_exists($out_file)) { return true; } foreach ($this->getPasswords() as $password) { $cmd = $this->buildCommand($in_file, $out_file, $password); exec($cmd, $output, $retcode); $success = $retcode == 0; if ($success) { return true; } if (file_exists($out_file)) { unlink($out_file); } unset($output); } return false; } }