Skip to content

Commit dedb89d

Browse files
committed
New Update Change Logs
1 parent 429c292 commit dedb89d

4 files changed

Lines changed: 186 additions & 38 deletions

File tree

Compress/Compress.php

Lines changed: 137 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
<?php
22
namespace 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
*/
1010
class 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
}

README.md

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Compress
22
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fmitmelon%2FCompress.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fmitmelon%2FCompress?ref=badge_shield)
33

4-
Advanced file compresser which reduces large files to smaller chunks by saving them as binary for later use. Many applications supports file uploads which consumes large memory sizes on their systems. This library has been designed to reduce both zip, rar, images, pdf, words sizes or any kinds of files or documents into smaller pieces.
4+
Advanced file compresser which reduces and encrypts large files to smaller chunks by saving them as binary for later use. Many applications supports file uploads which consumes large memory sizes on their systems. This library has been designed to reduce both zip, rar, images, pdf, words sizes or any kinds of files or documents into smaller pieces.
55

66
## Install:
77
Use composer to install
@@ -18,32 +18,32 @@ require_once __DIR__."/vendor/autoload.php";
1818
$compress = new Compress\Compress;
1919

2020
/**
21-
* @param $filePath string
21+
* @param String $filePath
2222
* File location to be compressed
23-
*
24-
* @param $storePath string
23+
* @param String $storePath
2524
* Path to output compressed binary file to
26-
*
25+
* @param Array optional $options ["removeMeta" => false, "encrypt" => false, "key" => "password"]
26+
* Options to remove meta and encrypt file
2727
*/
28-
$compress::compressFile($filePath, $storePath, $removeMeta);
28+
$compress::compressFile($filePath, $storePath, $options = []);
2929

3030
//Compress Image file
31-
$compress::compressFile(__DIR__.'/image.png', __DIR__.'/image.txt', true));
31+
$compress::compressFile(__DIR__.'/image.png', __DIR__.'/image.txt'));
3232

3333
// Compress PDF
3434
$compress::compressFile(__DIR__.'/file.pdf', __DIR__.'/file.txt', true));
35-
//Compress as lot files you want including zip files
35+
//Compress as lot of files you want including zip files
3636

3737
/**
3838
* UnCompress Image file [Get original file back from stored binary]
39-
* @param $storePath string
39+
* @param String $storePath
4040
* Path containing binary file which was compressed
41-
*
42-
* @param $fileOutputPath string
41+
* @param String $fileOutputPath
4342
* Path to output original file to
44-
*
43+
* @param String $encrypt_key
44+
* If your file was encrypted then provide the key for decryption as third argument
4545
*/
46-
$compress::uncompressFile($storePath, $fileOutputPath);
46+
$compress::uncompressFile($storePath, $fileOutputPath, $encrypt_key = null);
4747

4848
//Uncompress Image
4949
$compress::uncompressFile(__DIR__.'/image.txt', __DIR__.'/image.png');
@@ -53,9 +53,38 @@ $compress::uncompressFile( __DIR__.'/file.txt', __DIR__.'/file.pdf')
5353

5454
```
5555

56+
57+
# Changelog
58+
59+
All notable changes to this project will be documented here.
60+
61+
## [2.0.0] - 2022-03-12
62+
63+
We're super excited to announce `encryption` version of compress
64+
65+
When you upgrade, consider updating compressFile() parameter. The third argument is now array
66+
67+
### New features
68+
69+
- 🌟 Added AES 256 CBC Mode encryption
70+
5671
# Future Update
5772

58-
Adding binary file encryptions for security purposes.
73+
* Store files on centralize and decentralize cloud providers
74+
* Add key vaults to safely store your file keys
75+
* Add file signing digest key using SHA-3 Algorithmn
76+
* Add asymmetric file encryption protocol with Diffie Helman Key Exchange
77+
78+
# Support
79+
80+
If you love my project and wish to assist me to keep working on this project. Below is my Wallet Address.
81+
82+
BTC Wallet : 14PAtDFHTwH6SWdhGXRHGWDWYWDgVfHr6R
83+
ETH Wallet : 0x8c26549052667A0b77327505D2Ea1fe4c207630e
84+
85+
If you donated, please send me a mail at manomitehq@gmail.com with your name so I can list you here among my supporters
86+
I will really appreciate your donations. Thanks for using Compress.
87+
5988

6089
# License
6190

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
22
"name": "mitmelon/compress",
3-
"description": "Advanced file compresser which reduces large files to smaller chunks by saving them as binary for later use.",
4-
"keywords": ["archive","zip","compress","compress files", "advance file compresser", "save memory", "file compresser", "file"],
5-
"homepage": "https://mitnets.com",
3+
"description": "Advanced file compresser which reduces and encrypt large files to smaller chunks by saving them as binary for later use.",
4+
"keywords": ["archive","zip","compress","compress files", "advance file compresser", "save memory", "file compresser", "file", "encryption", "encrypt file", "remove meta"],
5+
"homepage": "https://manomite.net",
66
"type": "library",
77
"license": "MIT",
88
"authors": [
99
{
10-
"name": "Mitnets Technologies",
11-
"email": "support@mitnets.com"
10+
"name": "Manomite Limited",
11+
"email": "manomitehq@gmail.com"
1212
}
1313
],
14-
"minimum-stability": "beta",
14+
"minimum-stability": "stable",
1515
"require": {
1616
"php": ">=5.6.0"
1717
},

version.watermelon

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)