11<?php
22namespace Compress ;
33/**
4- * Compresses files including zip, image, pdf and lots more
4+ * Compresses and encrypt files including zip, image, pdf and lots more
55 *
6- * @link https://manomiteblog.com for Programming Tutorials
7- * @author Manomite <manomitehq@gmail.com>
8- * @version 1 .0.1
6+ * @link https://blog.manomite.net for Programming Tutorials
7+ * @author Manomite <manomitehq@gmail.com>
8+ * @version 2 .0.0
99 */
1010class Compress
1111{
12+ /**
13+ * Encryption block chunk size
14+ *
15+ * @param Int $block
16+ */
17+ public int $ block = 1024 ;
18+ /**
19+ * Encryption cipher
20+ *
21+ * @default aes-256-cbc
22+ */
23+ public $ cipher = 'aes-256-cbc ' ;
24+ //////////////////////////////////////////////////////////////////////////////////////////////////////////////
25+ //Encryption file temporary storage. //
26+ //Do not touch here please. //
27+ private $ temp = __DIR__ .'/compress_tmp_file ' ; //
28+ private $ dest_temp = __DIR__ .'/dest_compress_tmp_file ' ; //
29+ private $ encrypt_key = null ; //
30+ /////////////////////////////////////////////////////////////////////////////////////////////////////////
1231 /**
1332 * Text compress
1433 *
@@ -32,25 +51,44 @@ public static function uncompressor(String $content)
3251 *
3352 * @param String $filePath
3453 * @param String $storePath
35- * @param Bolean $ removeMeta
54+ * @param Array $options [" removeMeta" => false, "encrypt" => false, "key" => "password"]
3655 */
37- public static function compressFile (String $ filePath , String $ storePath , $ removeMeta = false )
56+ public static function compressFile (String $ filePath , String $ storePath , array $ options = [] )
3857 {
39-
4058 try {
4159 $ file = @fopen ($ filePath , "rb " );
4260 $ content = fread ($ file , filesize ($ filePath ));
43- //Meta heads are removed in new version which cannot be reversed
44- if ($ removeMeta ) {
45- $ replace = '@<meta[^>]*?> ' ;
46- $ with = ' ' ;
47- $ content = str_replace ($ replace , $ with , $ content );
48- $ content = preg_replace ("/ $ replace/ " , $ with , $ content );
61+ //Check if option is not empty
62+ if (!empty ($ options )) {
63+ //Meta heads are removed in new version which cannot be reversed
64+ $ isMeta = isset ($ options ["removeMeta " ]) ? ($ options ["removeMeta " ] === true ? true : false ) : false ;
65+ if ($ isMeta ) {
66+ $ replace = '@<meta[^>]*?> ' ;
67+ $ with = ' ' ;
68+ $ content = str_replace ($ replace , $ with , $ content );
69+ $ content = preg_replace ("/ $ replace/ " , $ with , $ content );
70+ }
71+ //Check if encryption is set
72+ $ isEncrypt = isset ($ options ["encrypt " ]) ? ($ options ["encrypt " ] === true ? true : false ) : false ;
73+ if ($ isEncrypt ) {
74+ //mt_rand() was used to avoid file collition among many users compressing at the same time.
75+ $ t = $ this ->temp .'_ ' .mt_rand ().'.encrypt ' ;
76+ $ d = $ this ->dest_temp .'_ ' .mt_rand ().'.encrypt ' ;
77+ file_put_contents ($ t , $ content );
78+ $ key = isset ($ options ["key " ]) ? $ options ["key " ] : null ;
79+ $ encrypt_key = $ this ->encryptFile ($ t , $ d , $ key );
80+ $ file_s = @fopen ($ d , "rb " );
81+ $ content = fread ($ file_s , filesize ($ d ));
82+ fclose ($ file_s );
83+ unlink ($ t );
84+ unlink ($ d );
85+ }
4986 }
50- $ content = self :: compressor (serialize ($ content ));
87+ $ content = $ this -> compressor (serialize ($ content ));
5188 fclose ($ file );
5289 file_put_contents ($ storePath , $ content );
53- return true ;
90+ //Make sure you store your encryption key for this file if you used the encryption option
91+ return $ encrypt_key ;
5492 } catch (\Extension $ e ) {
5593 return $ e ->getMessage ();
5694 }
@@ -60,17 +98,99 @@ public static function compressFile(String $filePath, String $storePath, $remove
6098 *
6199 * @param String $fileInputPath
62100 * @param String $fileOutputPath
101+ * @param String $encrypt_key
63102 */
64- public static function uncompressFile ($ fileInputPath , $ fileOutputPath )
103+ public static function uncompressFile ($ fileInputPath , $ fileOutputPath, $ encrypt_key = null )
65104 {
66105 try {
67106 $ file = fopen ($ fileInputPath , "rb " );
68- $ contents = unserialize (self ::uncompressor (fread ($ file , filesize ($ fileInputPath ))));
107+ $ contents = unserialize ($ this ->uncompressor (fread ($ file , filesize ($ fileInputPath ))));
108+ //Check if key is given; File might be encrypted
109+ if (!empty ($ encrypt_key )){
110+ $ t = $ this ->temp .'_ ' .mt_rand ().'.decrypt ' ;
111+ $ d = $ this ->dest_temp .'_ ' .mt_rand ().'.decrypt ' ;
112+ file_put_contents ($ t , $ contents );
113+ $ this ->decryptFile ($ t , $ d , $ encrypt_key );
114+ $ file_s = @fopen ($ d , "rb " );
115+ $ contents = fread ($ file_s , filesize ($ d ));
116+ fclose ($ file_s );
117+ unlink ($ t );
118+ unlink ($ d );
119+ }
69120 file_put_contents ($ fileOutputPath , $ contents );
70121 fclose ($ file );
71122 return true ;
72123 } catch (\Extension $ e ) {
73124 return $ e ->getMessage ();
74125 }
75126 }
127+ /**
128+ * @param $source Path of the unencrypted file
129+ * @param $dest Path of the encrypted file to created
130+ * @param $key Encryption key
131+ */
132+ private function encryptFile ($ source , $ dest , $ key = null )
133+ {
134+ try {
135+ if (!extension_loaded ('openssl ' )) {
136+ return "Sorry! openssl extension is not installed on your system. Please install it using sudo apt-get install openssl " ;
137+ }
138+
139+ if (empty ($ key )) {
140+ $ key = openssl_random_pseudo_bytes (120 );
141+ }
142+
143+ $ ivLenght = openssl_cipher_iv_length ($ this ->cipher );
144+ $ iv = openssl_random_pseudo_bytes ($ ivLenght );
145+
146+ $ fpSource = fopen ($ source , 'rb ' );
147+ $ fpDest = fopen ($ dest , 'w ' );
148+
149+ fwrite ($ fpDest , $ iv );
150+
151+ while (! feof ($ fpSource )) {
152+ $ plaintext = fread ($ fpSource , $ ivLenght * $ this ->block );
153+ $ ciphertext = openssl_encrypt ($ plaintext , $ this ->cipher , $ key , OPENSSL_RAW_DATA , $ iv );
154+ $ iv = substr ($ ciphertext , 0 , $ ivLenght );
155+
156+ fwrite ($ fpDest , $ ciphertext );
157+ }
158+
159+ fclose ($ fpSource );
160+ fclose ($ fpDest );
161+ return $ key ;
162+ } catch (\Exception $ e ){
163+ return $ e ->getMessage ();
164+ }
165+ }
166+ /**
167+ * @param $source Path of the encrypted file
168+ * @param $dest Path of the decrypted file
169+ * @param $key Encryption key
170+ */
171+ private function decryptFile ($ source , $ dest , $ key )
172+ {
173+ try {
174+ $ ivLenght = openssl_cipher_iv_length ($ this ->cipher );
175+
176+ $ fpSource = fopen ($ source , 'rb ' );
177+ $ fpDest = fopen ($ dest , 'w ' );
178+
179+ $ iv = fread ($ fpSource , $ ivLenght );
180+
181+ while (! feof ($ fpSource )) {
182+ $ ciphertext = fread ($ fpSource , $ ivLenght * ($ this ->block + 1 ));
183+ $ plaintext = openssl_decrypt ($ ciphertext , $ this ->cipher , $ key , OPENSSL_RAW_DATA , $ iv );
184+ $ iv = substr ($ plaintext , 0 , $ ivLenght );
185+
186+ fwrite ($ fpDest , $ plaintext );
187+ }
188+
189+ fclose ($ fpSource );
190+ fclose ($ fpDest );
191+ return true ;
192+ } catch (\Exception $ e ){
193+ return $ e ->getMessage ();
194+ }
195+ }
76196}
0 commit comments