66use OLE ;
77use OLE_PPS_File ;
88use OLE_PPS_Root ;
9+ use RuntimeException ;
910
1011class Encrypt
1112{
1213 private $ data ;
14+
1315 private $ password ;
16+
1417 private $ noFile = false ;
1518
19+ private $ tmpPathFolder = null ;
20+
1621 public function __construct (bool $ nofile = false )
1722 {
1823 $ this ->noFile = $ nofile ;
@@ -29,10 +34,10 @@ public function input(string $data)
2934 } else {
3035 $ this ->data = function () use ($ data ) {
3136 $ fp = fopen ($ data , 'rb ' );
32- if (!$ fp ) {
37+ if (! $ fp ) {
3338 throw new Exception ('file not found ' );
3439 }
35- while (!feof ($ fp )) {
40+ while (! feof ($ fp )) {
3641 yield unpack ('C* ' , fread ($ fp , 4096 ));
3742 }
3843 fclose ($ fp );
@@ -45,12 +50,13 @@ public function input(string $data)
4550 public function password (string $ password )
4651 {
4752 $ this ->password = $ password ;
53+
4854 return $ this ;
4955 }
5056
5157 public function output (?string $ filePath = null )
5258 {
53- if (!$ this ->noFile && is_null ($ filePath )) {
59+ if (! $ this ->noFile && is_null ($ filePath )) {
5460 throw new Exception ('Output Filepath cannot be NULL when NOFILE is False ' );
5561 }
5662
@@ -64,7 +70,8 @@ public function output(?string $filePath = null)
6470 $ encryptionInfo ['package ' ]['blockSize ' ],
6571 $ encryptionInfo ['package ' ]['saltValue ' ],
6672 $ packageKey ,
67- $ this ->data
73+ $ this ->data ,
74+ $ this ->tmpPathFolder
6875 );
6976
7077 $ encryptionInfo ['dataIntegrity ' ] = $ this ->createDataIntegrity ($ encryptionInfo , $ packageKey , $ encryptedPackage ['tmpFile ' ]);
@@ -103,12 +110,11 @@ public function output(?string $filePath = null)
103110 $ OLE2 ->append (pack ('C* ' , ...$ unpackEncryptedPackage ));
104111 }
105112
106- unlink ($ encryptedPackage ['tmpFile ' ]);
107-
108113 $ root = new OLE_PPS_Root (1000000000 , 1000000000 , [$ OLE , $ OLE2 ]);
109114
110115 if ($ this ->noFile ) {
111- $ filePath = tempnam (sys_get_temp_dir (), 'NOFILE ' );
116+ $ tmp = new TempFileManager ($ this ->tmpPathFolder );
117+ $ filePath = $ tmp ->path ('NOFILE ' );
112118 }
113119
114120 $ root ->save ($ filePath );
@@ -201,4 +207,15 @@ private function addVerifierHash(array &$encryptionInfo)
201207 $ verifierHashValue
202208 );
203209 }
204- }
210+
211+ public function setTempPathFolder (string $ path )
212+ {
213+ if (! is_writable ($ path )) {
214+ throw new RuntimeException ('Temp dir not writable. ' );
215+ }
216+
217+ $ this ->tmpPathFolder = $ path ;
218+
219+ return $ this ;
220+ }
221+ }
0 commit comments