@@ -315,19 +315,25 @@ private function loadFileMetadata(): void {
315315 $ this ->fileData ->pdfVersion = $ pdfParserService ->getPdfVersion ();
316316 }
317317
318- private function loadCertDataFromLibreSignFile (?File $ file = null ): void {
319- if (!empty ($ this ->certData ) || !$ this ->options ->isValidateFile () || !$ file instanceof File || !$ file ->getSignedNodeId ()) {
320- return ;
318+
319+ private function loadCertificateChain (\OCP \Files \File $ fileNode , File $ libreSignFile ): array {
320+ if (!$ this ->options ->isValidateFile () || !$ libreSignFile ->getSignedNodeId ()) {
321+ return [];
321322 }
322323
323- $ fileNode = $ this ->getFile ();
324- $ resource = $ fileNode ->fopen ('rb ' );
325- $ sha256 = $ this ->getSha256FromResource ($ resource );
326- if ($ sha256 === $ file ->getSignedHash ()) {
327- $ this ->pkcs12Handler ->setIsLibreSignFile ();
324+ try {
325+ $ resource = $ fileNode ->fopen ('rb ' );
326+ $ sha256 = $ this ->getSha256FromResource ($ resource );
327+ rewind ($ resource );
328+ if ($ sha256 === $ libreSignFile ->getSignedHash ()) {
329+ $ this ->pkcs12Handler ->setIsLibreSignFile ();
330+ }
331+ $ certData = $ this ->pkcs12Handler ->getCertificateChain ($ resource );
332+ fclose ($ resource );
333+ return $ certData ;
334+ } catch (\Exception $ e ) {
335+ return [];
328336 }
329- $ this ->certData = $ this ->pkcs12Handler ->getCertificateChain ($ resource );
330- fclose ($ resource );
331337 }
332338
333339 private function getSha256FromResource ($ resource ): string {
@@ -348,8 +354,15 @@ private function loadSigners(): void {
348354 if (!$ this ->options ->isShowSigners ()) {
349355 return ;
350356 }
351- $ this ->loadCertDataFromLibreSignFile ($ this ->file );
352- $ this ->signersLoader ->loadSignersFromCertData ($ this ->fileData , $ this ->certData , $ this ->options );
357+
358+ if (!$ this ->options ->isValidateFile () || !$ this ->file instanceof File || !$ this ->file ->getSignedNodeId ()) {
359+ return ;
360+ }
361+ $ fileNode = $ this ->getFile ();
362+ $ certData = $ this ->loadCertificateChain ($ fileNode , $ this ->file );
363+ if ($ certData ) {
364+ $ this ->signersLoader ->loadSignersFromCertData ($ this ->fileData , $ certData , $ this ->options ->getHost ());
365+ }
353366 $ this ->loadLibreSignSigners ();
354367 }
355368
@@ -565,16 +578,15 @@ private function loadEnvelopeFiles(): void {
565578 * @return LibresignEnvelopeChildFile
566579 * @psalm-return LibresignEnvelopeChildFile
567580 */
568- private function buildEnvelopeChildData (File $ childFile ): array {
569- $ fileData = [
570- 'id ' => $ childFile ->getId (),
571- 'uuid ' => $ childFile ->getUuid (),
572- 'name ' => $ childFile ->getName (),
573- 'status ' => $ childFile ->getStatus (),
574- 'statusText ' => $ this ->fileMapper ->getTextOfStatus ($ childFile ->getStatus ()),
575- 'nodeId ' => $ childFile ->getNodeId (),
576- 'signers ' => [],
577- ];
581+ private function buildEnvelopeChildData (File $ childFile ): stdClass {
582+ $ fileData = new stdClass ();
583+ $ fileData ->id = $ childFile ->getId ();
584+ $ fileData ->uuid = $ childFile ->getUuid ();
585+ $ fileData ->name = $ childFile ->getName ();
586+ $ fileData ->status = $ childFile ->getStatus ();
587+ $ fileData ->statusText = $ this ->fileMapper ->getTextOfStatus ($ childFile ->getStatus ());
588+ $ fileData ->nodeId = $ childFile ->getNodeId ();
589+ $ fileData ->signers = [];
578590
579591 $ signRequests = $ this ->signRequestMapper ->getByFileId ($ childFile ->getId ());
580592 foreach ($ signRequests as $ signRequest ) {
@@ -601,14 +613,24 @@ private function buildEnvelopeChildData(File $childFile): array {
601613 $ displayName = $ email ;
602614 }
603615
604- $ fileData ['signers ' ][] = [
605- 'signRequestId ' => $ signRequest ->getId (),
606- 'displayName ' => $ displayName ,
607- 'email ' => $ email ,
608- 'signed ' => $ signed ,
609- 'status ' => $ signRequest ->getStatus (),
610- 'statusText ' => $ this ->signRequestMapper ->getTextOfSignerStatus ($ signRequest ->getStatus ()),
611- ];
616+ $ signer = new stdClass ();
617+ $ signer ->signRequestId = $ signRequest ->getId ();
618+ $ signer ->displayName = $ displayName ;
619+ $ signer ->email = $ email ;
620+ $ signer ->signed = $ signed ;
621+ $ signer ->status = $ signRequest ->getStatus ();
622+ $ signer ->statusText = $ this ->signRequestMapper ->getTextOfSignerStatus ($ signRequest ->getStatus ());
623+ $ fileData ->signers [] = $ signer ;
624+ }
625+
626+ if ($ this ->options ->isValidateFile () && $ childFile ->getSignedNodeId ()) {
627+ $ fileNode = $ this ->root ->getUserFolder ($ childFile ->getUserId ())->getFirstNodeById ($ childFile ->getSignedNodeId ());
628+ if ($ fileNode instanceof \OCP \Files \File) {
629+ $ certData = $ this ->loadCertificateChain ($ fileNode , $ childFile );
630+ if (!empty ($ certData )) {
631+ $ this ->signersLoader ->loadSignersFromCertData ($ fileData , $ certData , $ this ->options ->getHost ());
632+ }
633+ }
612634 }
613635
614636 return $ fileData ;
0 commit comments