-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity.php
More file actions
120 lines (99 loc) · 4.19 KB
/
security.php
File metadata and controls
120 lines (99 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
//How It Works
//Compressing and Splitting:
//The script gzips a directory into a .tar.gz archive.
//
//If the archive size exceeds 20 MB, the file is split using the split command.
// ********** Gmail accepts actually 25MB attachments
//The split files are named using the pattern:
// directory_backup_<timestamp>.tar.gz_part_aa,
// directory_backup_<timestamp>.tar.gz_part_ab,
// etc.
//
// Emailing Split Files:
//
// Each split part is sent in a separate email via PHPMailer,
// with one attachment per email.
//
// Leveraging Gmail for Virus Scanning:
//
// Gmail automatically scans attachments for viruses, malware, and rootkits.
// If a file is infected, Gmail will discard the attachment.
// As a result, the recipient gets a virus-free backup,
// ensuring that no infected files are retained from the server.
//
// In simple words the last email contain the last not infected situation
// Function to gzip and split the directory
function gzipAndSplitDirectory($sourceDir) {
$timestamp = time(); // Unix timestamp
$archiveName = "directory_backup_$timestamp.tar.gz"; // Name with timestamp
$splitSize = 20 * 1024 * 1024; // 20 MB in bytes
$sourceDir = escapeshellarg($sourceDir); // Escape input for safety
$archiveName = escapeshellarg($archiveName);
// Create the tar.gz file
$command = "tar -czf $archiveName -C $sourceDir .";
shell_exec($command);
// Check if the tar.gz file exists
if (!file_exists($archiveName)) {
return false;
}
// Get file size
$fileSize = filesize($archiveName);
// If file size exceeds the split size, split it into smaller chunks
if ($fileSize > $splitSize) {
$splitCommand = "split -b {$splitSize} $archiveName {$archiveName}_part_";
shell_exec($splitCommand);
// Return array of split files
return glob("{$archiveName}_part_*");
}
// Return the original tar.gz file if splitting wasn't necessary
return [$archiveName];
}
// Function to send an email with a single attachment
function sendEmail($filePath, $partNumber) {
$mail = new PHPMailer(true);
try {
// Server settings
$mail->isSMTP(); // Send using SMTP
$mail->Host = 'smtp.example.com'; // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'your_email@example.com'; // SMTP username
$mail->Password = 'your_password'; // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Enable SSL encryption
$mail->Port = 465; // TCP port to connect to
// Recipients
$mail->setFrom('from_email@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name'); // Add a recipient
// Attach the single file part
$mail->addAttachment($filePath); // Attach the file
// Email content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = "Backup Part $partNumber";
$mail->Body = "Attached is part $partNumber of your backup file.";
$mail->AltBody = "Attached is part $partNumber of your backup file.";
// Send the email
$mail->send();
echo "Part $partNumber has been sent.\n";
} catch (Exception $e) {
echo "Part $partNumber could not be sent. Mailer Error: {$mail->ErrorInfo}\n";
}
}
// Define source directory
$directoryToGzip = 'path/to/your/directory';
// Gzip and split the directory
$filesToAttach = gzipAndSplitDirectory($directoryToGzip);
if ($filesToAttach) {
echo "Directory gzipped and split successfully!\n";
// Send each part in a separate email
foreach ($filesToAttach as $index => $file) {
sendEmail($file, $index + 1); // Send email with each part as an attachment
}
} else {
echo "Failed to gzip the directory.\n";
}
?>