@@ -68,7 +68,7 @@ public function verifyMessage(string $message): bool {
68
68
// smime/pkcs7 module. Unfortunately, it is only supported since php 8.
69
69
// Ref https://www.php.net/manual/en/function.openssl-cms-verify.php
70
70
71
- $ messageTemp = $ this ->tempManager -> getTemporaryFile ();
71
+ $ messageTemp = $ this ->getTemporaryFileOrThrow ();
72
72
$ messageTempHandle = fopen ($ messageTemp , 'wb ' );
73
73
fwrite ($ messageTempHandle , $ message );
74
74
fclose ($ messageTempHandle );
@@ -106,8 +106,8 @@ public function extractSignedContent(string $message): string {
106
106
// smime/pkcs7 module. Unfortunately, it is only supported since php 8.
107
107
// Ref https://www.php.net/manual/en/function.openssl-cms-verify.php
108
108
109
- $ verifiedContentTemp = $ this ->tempManager -> getTemporaryFile ();
110
- $ messageTemp = $ this ->tempManager -> getTemporaryFile ();
109
+ $ verifiedContentTemp = $ this ->getTemporaryFileOrThrow ();
110
+ $ messageTemp = $ this ->getTemporaryFileOrThrow ();
111
111
$ messageTempHandle = fopen ($ messageTemp , 'wb ' );
112
112
fwrite ($ messageTempHandle , $ message );
113
113
fclose ($ messageTempHandle );
@@ -166,7 +166,7 @@ public function parseCertificate(string $certificate): SmimeCertificateInfo {
166
166
}
167
167
}
168
168
169
- $ decryptedCertificateFile = $ this ->tempManager -> getTemporaryFile ();
169
+ $ decryptedCertificateFile = $ this ->getTemporaryFileOrThrow ();
170
170
file_put_contents ($ decryptedCertificateFile , $ certificate );
171
171
172
172
$ caBundle = [$ this ->certificateManager ->getAbsoluteBundlePath ()];
@@ -363,11 +363,11 @@ public function signMimePart(Horde_Mime_Part $part,
363
363
);
364
364
}
365
365
366
- $ decryptedCertificateFile = $ this ->tempManager -> getTemporaryFile ();
366
+ $ decryptedCertificateFile = $ this ->getTemporaryFileOrThrow ();
367
367
file_put_contents ($ decryptedCertificateFile , $ decryptedCertificate );
368
368
369
- $ inPath = $ this ->tempManager -> getTemporaryFile ();
370
- $ outPath = $ this ->tempManager -> getTemporaryFile ();
369
+ $ inPath = $ this ->getTemporaryFileOrThrow ();
370
+ $ outPath = $ this ->getTemporaryFileOrThrow ();
371
371
file_put_contents ($ inPath , $ part ->toString ([
372
372
'canonical ' => true ,
373
373
'headers ' => true ,
@@ -425,8 +425,8 @@ public function decryptMimePartText(string $mimePartText,
425
425
);
426
426
}
427
427
428
- $ inPath = $ this ->tempManager -> getTemporaryFile ();
429
- $ outPath = $ this ->tempManager -> getTemporaryFile ();
428
+ $ inPath = $ this ->getTemporaryFileOrThrow ();
429
+ $ outPath = $ this ->getTemporaryFileOrThrow ();
430
430
file_put_contents ($ inPath , $ mimePartText );
431
431
if (!openssl_pkcs7_decrypt ($ inPath , $ outPath , $ decryptedCertificate , $ decryptedKey )) {
432
432
throw new SmimeDecryptException ('Failed to decrypt MIME part text ' );
@@ -581,8 +581,8 @@ public function encryptMimePart(Horde_Mime_Part $part, array $certificates): Hor
581
581
throw new ServiceException ('Failed to decrypt certificate: ' . $ e ->getMessage (), 0 , $ e );
582
582
}
583
583
584
- $ inPath = $ this ->tempManager -> getTemporaryFile ();
585
- $ outPath = $ this ->tempManager -> getTemporaryFile ();
584
+ $ inPath = $ this ->getTemporaryFileOrThrow ();
585
+ $ outPath = $ this ->getTemporaryFileOrThrow ();
586
586
file_put_contents ($ inPath , $ part ->toString ([
587
587
'canonical ' => true ,
588
588
'headers ' => true ,
@@ -610,4 +610,18 @@ public function encryptMimePart(Horde_Mime_Part $part, array $certificates): Hor
610
610
611
611
return $ parsedPart ;
612
612
}
613
+
614
+ /**
615
+ * Create a temporary file and return the path or throw if it could not be created.
616
+ *
617
+ * @throws ServiceException If the temporary file could not be created
618
+ */
619
+ private function getTemporaryFileOrThrow (): string {
620
+ $ file = $ this ->tempManager ->getTemporaryFile ();
621
+ if ($ file === false ) {
622
+ throw new ServiceException ('Failed to create temporary file ' );
623
+ }
624
+
625
+ return $ file ;
626
+ }
613
627
}
0 commit comments