diff --git a/MaarchREST.class.php b/MaarchREST.class.php index f64460b6582f9560aa8fdbea9df00e733e22a2f2..4250d882fe2a6ce1fead59d51d7986fcc8a4edaf 100644 --- a/MaarchREST.class.php +++ b/MaarchREST.class.php @@ -32,7 +32,6 @@ class MaarchREST extends SAEConnecteur { $tmpFile = new TmpFile(); $bordereau_file = $tmpFile->create(); file_put_contents($bordereau_file, $bordereauSEDA); - try { $this->callSedaMessage($bordereau_file, $archivePath); } catch (Exception $e){ @@ -55,11 +54,88 @@ class MaarchREST extends SAEConnecteur { public function callSedaMessage($seda_message_path, $attachments_path){ $curlWrapper = $this->curlWrapperFactory->getInstance(); - $bodyPackage = $this->createObjectPackage($seda_message_path, $attachments_path); - + $bodyPackage = $this->createPackageSource($seda_message_path, $attachments_path); + $curlWrapper->setJsonPostData($bodyPackage); - return $this->getWS('/medona/archiveTransfer', "application/json", $curlWrapper); + return $this->getWS('/medona/archiveTransfer/source', "application/json", $curlWrapper); + } + + public function createPackageSource($seda_message_path, $attachments_path) { + + $tmpFolder = dirname($attachments_path); + + mkdir($tmpFolder."/tmpFiles"); + try { + $phar = new PharData($attachments_path); + $phar->extractTo($tmpFolder."/tmpFiles", null, true); + } catch (Exception $e) { + return ($e); + } + copy($seda_message_path, $tmpFolder."/tmpFiles/bordereau.xml"); + + $directoryToZip = $tmpFolder."/tmpFiles"; // Path to the directory to be zipped + $zipFilePath = $tmpFolder."/archive.zip"; // Path where the ZIP file will be saved + + if (!$this->zipDirectory($directoryToZip, $zipFilePath)) { + echo "Error creating ZIP archive."; + } + + $sourcePackage = []; + $package = base64_encode(file_get_contents($zipFilePath)); + $sourcePackage['package'] = new stdClass(); + $sourcePackage['package']->data = $package; + $sourcePackage['package']->encoding = "base64"; + $sourcePackage['params'] = []; + $sourcePackage['params']['manifest'] = "bordereau.xml"; + $sourcePackage['connector'] = "seda2zip"; + + + unset($zipFilePath); + + return $sourcePackage; + } + + public function zipDirectory($sourcePath, $zipPath) { + $zip = new ZipArchive(); + if ($zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE) === TRUE) { + // Normalize the source path to ensure proper directory traversal + $sourcePath = realpath($sourcePath); + if ($sourcePath === false) { + // Could not determine the real path, possibly because the source directory does not exist + return false; + } + + // Create recursive directory iterator + $files = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($sourcePath, RecursiveDirectoryIterator::SKIP_DOTS), + RecursiveIteratorIterator::SELF_FIRST + ); + + foreach ($files as $file) { + $fileRealPath = $file->getRealPath(); + if ($fileRealPath === false) { + // Continue upon realpath errors, but this could indicate a deeper problem + continue; + } + + // Preserve folder structure in the ZIP archive + $relativePath = substr($fileRealPath, strlen($sourcePath) + 1); + if ($file->isDir()) { + // Add empty directory into the archive + $zip->addEmptyDir($relativePath); + } else { + // Add file into the archive + $zip->addFile($fileRealPath, $relativePath); + } + } + + // Close the ZIP archive + $zip->close(); + return true; // Success + } else { + return false; // Failure + } } public function createObjectPackage($seda_message_path, $attachments_path) { @@ -79,21 +155,45 @@ class MaarchREST extends SAEConnecteur { $bodyPackage['format'] = "seda"; $bodyPackage['attachments'] = []; - foreach(scandir($tmpFolder."/tmpFiles") as $filename) { - if ($filename != ".." && $filename != ".") { - $attachment = new stdClass(); - $attachment->filename = $filename; - $attachment->data = base64_encode(file_get_contents($tmpFolder."/tmpFiles/".$filename)); - unlink($tmpFolder."/tmpFiles/".$filename); - array_push($bodyPackage['attachments'], $attachment); - } - } + // foreach(scandir($tmpFolder."/tmpFiles") as $filename) { + // if ($filename != ".." && $filename != ".") { + // $attachment = new stdClass(); + // $attachment->filename = $filename; + // $attachment->data = base64_encode(file_get_contents($tmpFolder."/tmpFiles/".$filename)); + // unlink($tmpFolder."/tmpFiles/".$filename); + // array_push($bodyPackage['attachments'], $attachment); + // } + // } + + // Chemin de base où les fichiers et dossiers sont stockés + $baseDirectory = $tmpFolder . "/tmpFiles"; + + $this->addFilesFromDirectory($baseDirectory, $bodyPackage); rmdir($tmpFolder."/tmpFiles"); return $bodyPackage; } + public function addFilesFromDirectory($directory, &$bodyPackage, $root="") { + foreach (scandir($directory) as $filename) { + if ($filename != ".." && $filename != ".") { + $filePath = $directory . "/" . $filename; + if (is_dir($filePath)) { + // Si c'est un dossier, appel récursif pour traiter les fichiers dans ce dossier + $this->addFilesFromDirectory($filePath, $bodyPackage, $root . $filename . "/"); + } else { + // Traiter le fichier + $attachment = new stdClass(); + $attachment->filename = $root . $filename; + $attachment->data = base64_encode(file_get_contents($filePath)); + unlink($filePath); // Supprimer le fichier après traitement + array_push($bodyPackage['attachments'], $attachment); + } + } + } + } + public function getErrorString($number){ return "Erreur non identifié"; } @@ -104,7 +204,7 @@ class MaarchREST extends SAEConnecteur { * @throws Exception */ public function getAcuseReception($id_transfert) { - return $this->getAck($id_transfert, $this->originatingAgency); + return $this->getAck($id_transfert, "test"); } /** @@ -258,6 +358,7 @@ class MaarchREST extends SAEConnecteur { $curlWrapper->dontVerifySSLCACert(); $result = $curlWrapper->get($this->url.$url); + if (! $result){ throw new Exception($curlWrapper->getLastError()); }