From 550bf1691c1533589066108d9468aced8e2ba41b Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Fri, 11 Dec 2020 15:54:41 +0100
Subject: [PATCH] FIX #14565 TIME 0:00 Workflow rgs + docaposte + timestamp

---
 .../CertificateSignatureController.php        |  3 +-
 .../DigitalSignatureController.php            | 34 +++++++++++++++++--
 2 files changed, 34 insertions(+), 3 deletions(-)

diff --git a/src/app/document/controllers/CertificateSignatureController.php b/src/app/document/controllers/CertificateSignatureController.php
index fe22328330..ec58cff50a 100755
--- a/src/app/document/controllers/CertificateSignatureController.php
+++ b/src/app/document/controllers/CertificateSignatureController.php
@@ -238,7 +238,8 @@ class CertificateSignatureController
                 'config'             => $config,
                 'signedDocumentPath' => $signedDocumentPath,
                 'transactionId'      => $document['digital_signature_transaction_id'],
-                'fieldName'          => $args['signatureFieldName']
+                'fieldName'          => $args['signatureFieldName'],
+                'extraCertificate'   => $args['certificate']
             ]);
             DigitalSignatureController::terminate(['config' => $config, 'transactionId' => $document['digital_signature_transaction_id']]);
         }
diff --git a/src/app/document/controllers/DigitalSignatureController.php b/src/app/document/controllers/DigitalSignatureController.php
index e8c61b70ab..bcdfd92fbb 100755
--- a/src/app/document/controllers/DigitalSignatureController.php
+++ b/src/app/document/controllers/DigitalSignatureController.php
@@ -30,8 +30,17 @@ class DigitalSignatureController
 
     public static function createTransaction($args = [])
     {
-        $config         = DigitalSignatureController::getConfig();
-        $transactionId  = DigitalSignatureController::initiate(['config' => $config, 'documentId' => $args['documentId'], 'signatoriesCount' => count($args['workflow'])]);
+        $config = DigitalSignatureController::getConfig();
+
+        $nbSignatories = 0;
+        foreach ($args['workflow'] as $key => $currentUserId) {
+            if (in_array($currentUserId['signature_mode'], ['eidas', 'inca_card_eidas'])) {
+                $nbSignatories++;
+            } else {
+                unset($args['workflow'][$key]);
+            }
+        }
+        $transactionId  = DigitalSignatureController::initiate(['config' => $config, 'documentId' => $args['documentId'], 'signatoriesCount' => $nbSignatories]);
         DocumentModel::update([
             'set'   => ['digital_signature_transaction_id' => $transactionId],
             'where' => ['id = ?'],
@@ -355,6 +364,27 @@ class DigitalSignatureController
 
             // Create a collection of trusted certificats:
             $trustedCertificates = new \SetaPDF_Signer_X509_Collection(\SetaPDF_Signer_Pem::extractFromFile($tmpTimestampPEM));
+
+            if (!empty($args['extraCertificate'])) {
+                $certificate = new \SetaPDF_Signer_X509_Certificate($args['extraCertificate']);
+        
+                $informationResolverManager = new \SetaPDF_Signer_InformationResolver_Manager();
+                $informationResolverManager->addResolver(new \SetaPDF_Signer_InformationResolver_HttpCurlResolver());
+        
+                $certificates = [$certificate];
+                while (count($certificates) > 0) {
+                    $currentCertificate = array_pop($certificates);
+
+                    $aia = $currentCertificate->getExtensions()->get(\SetaPDF_Signer_X509_Extension_AuthorityInformationAccess::OID);
+                    if ($aia instanceof \SetaPDF_Signer_X509_Extension_AuthorityInformationAccess) {
+                        foreach ($aia->fetchIssuers($informationResolverManager)->getAll() as $issuer) {
+                            $trustedCertificates->add($issuer);
+                            $certificates[] = $issuer;
+                        }
+                    }
+                }
+            }
+
             // Create a collector instance
             $collector = new \SetaPDF_Signer_ValidationRelatedInfo_Collector($trustedCertificates);
             $vriData = $collector->getByFieldName($document, $args['fieldName']);
-- 
GitLab