From 565e6a48731a3b387fe47b967fbf1837e462465b Mon Sep 17 00:00:00 2001
From: Cyril Vazquez <cyril.vazquez@maarch.org>
Date: Wed, 22 Mar 2023 11:08:50 +0100
Subject: [PATCH] fix profile not found when another extension is there

---
 bundle/seda/Controller/ArchiveTransfer.php | 93 ++++++++--------------
 1 file changed, 34 insertions(+), 59 deletions(-)

diff --git a/bundle/seda/Controller/ArchiveTransfer.php b/bundle/seda/Controller/ArchiveTransfer.php
index 33b8aad1..bc1cfa63 100644
--- a/bundle/seda/Controller/ArchiveTransfer.php
+++ b/bundle/seda/Controller/ArchiveTransfer.php
@@ -177,6 +177,7 @@ class ArchiveTransfer extends abstractMessage implements \bundle\medona\Controll
     public function validate($message, $archivalAgreement = null)
     {
         $this->errors = array();
+        $this->infos = array();
         $this->replyCode = null;
 
         $this->loadMessage($message);
@@ -277,72 +278,46 @@ class ArchiveTransfer extends abstractMessage implements \bundle\medona\Controll
 
     protected function validateProfile($message, $archivalProfileReference)
     {
-        /*if (!isset($message->object->archive[0]->archivalProfile) || $message->object->archive[0]->archivalProfile->value == "") {
-            $this->sendError("203", "Le profil d'archive n'est pas renseigné");
-
-            return;
-        }*/
         $errors = array();
 
-        $profileFiles = glob($this->profilesDirectory.DIRECTORY_SEPARATOR.$archivalProfileReference.'.*');
-        if (count($profileFiles) == 0) {
-            $this->sendError("203", "Le profil d'archive identifié par '$archivalProfileReference' n'est pas référencé dans le système.");
-
-            return;
-        }
-
-        foreach ($profileFiles as $profileFile) {
-            $extension = pathinfo($profileFile, \PATHINFO_EXTENSION);
+        $profilePath = $this->profilesDirectory.DIRECTORY_SEPARATOR.$archivalProfileReference;
 
-            switch ($extension) {
-                case 'rng':
-                    //if ($validationMode == 'jing') {
-                    if (true) {
-                        $jing = \laabs::newService('dependency/xml/plugins/jing/jing');
-                        $xmlFile = $this->message->path;
-                        $valid = $jing->validate($profileFile, $xmlFile);
-                        if (!$valid) {
-                            $this->sendError("203");
+        if (file_exists($profilePath . '.rng')) {
+            //if ($validationMode == 'jing') {
+            //if (true) {
+                $jing = \laabs::newService('dependency/xml/plugins/jing/jing');
+                $valid = $jing->validate($profilePath.'.rng', $this->message->path);
+                if (!$valid) {
+                    $this->sendError("203");
 
-                            foreach ($jing->getErrors() as $errorMessage) {
-                                $this->errors[] = new \core\Error($errorMessage);
-                            }
-                        }
-                    } else {
-                        libxml_use_internal_errors(true);
-                        $valid = $message->xml->relaxNGValidate($profileFile);
-                        if ($valid == false) {
-                            $this->sendError("203");
-                            foreach (libxml_get_errors() as $libxmlError) {
-                                $this->errors[] = new \core\Error($libxmlError->message, null, $libxmlError->code);
-                            }
-                        }
-                        libxml_clear_errors();
-                        libxml_use_internal_errors(false);
+                    foreach ($jing->getErrors() as $errorMessage) {
+                        $this->errors[] = new \core\Error($errorMessage);
                     }
-                    break;
-
-                case 'xsd':
-                    libxml_use_internal_errors(true);
-                    $valid = $message->xml->schemaValidate($profileFile);
-                    if ($valid == false) {
-                        foreach (libxml_get_errors() as $libxmlError) {
-                            $this->errors[] = new \core\Error($libxmlError->message, null, $libxmlError->code);
-                        }
-                    }
-                    libxml_clear_errors();
-                    libxml_use_internal_errors(false);
-                    break;
-
-                default:
-                    $path = $this->profilesDirectory.DIRECTORY_SEPARATOR.$archivalprofileReference;
-                    // if a profile were previously saved without extension
-                    if (empty($extension) && (file_exists($path . '.rng') || file_exists($path . '.xsd'))) {
-                        break;
+                }
+            /*} else {
+                libxml_use_internal_errors(true);
+                $valid = $message->xml->relaxNGValidate($profileFile);
+                if ($valid == false) {
+                    $this->sendError("203");
+                    foreach (libxml_get_errors() as $libxmlError) {
+                        $this->errors[] = new \core\Error($libxmlError->message, null, $libxmlError->code);
                     }
-                    $this->sendError("203", "Le profil d'archive identifié par '$archivalProfileReference' est dans un format non géré par le système.");
-                    break;
+                }
+                libxml_clear_errors();
+                libxml_use_internal_errors(false);
+            }*/
+        } elseif (file_exists($profilePath . '.xsd')) {
+            libxml_use_internal_errors(true);
+            $valid = $message->xml->schemaValidate($profilePath.'.xsd');
+            if ($valid == false) {
+                foreach (libxml_get_errors() as $libxmlError) {
+                    $this->errors[] = new \core\Error($libxmlError->message, null, $libxmlError->code);
+                }
             }
+            libxml_clear_errors();
+            libxml_use_internal_errors(false);
+        } else {
+            $this->sendError("203", "Le fichier de profil d'archive identifié par '$archivalProfileReference' n'existe pas.");
         }
     }
 
-- 
GitLab