-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfn-updater.php
More file actions
299 lines (244 loc) · 9.75 KB
/
fn-updater.php
File metadata and controls
299 lines (244 loc) · 9.75 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
require_once __DIR__ . "/fn.php";
/**
* fn-updater.php
*
* Script to download and update MagicServer with the latest release from GitHub.
* It will skip user data and configuration files.
*/
// --- Configuration ---
$repoOwner = 'Planetbiru';
$repoName = 'MagicServer';
$apiUrl = "https://api.github.com/repos/$repoOwner/$repoName/releases/latest";
// Directories and files to skip during the update
$skipPaths = [
'data/',
'mysql/',
'redis/',
'logs/',
'sessions/',
'tmp/',
'www/',
'config/httpd.conf',
'config/my.ini',
'config/php.ini',
'config/redis.windows.conf',
'config/redis.windows-service.conf',
'backups/' // Don't include the backup directory in backups
];
// --- Main Script ---
$targetDir = __DIR__;
$backupDir = $targetDir . '/backups';
$updateTempDir = $targetDir . '/update-temp';
$tempZip = $targetDir . '/magicserver-update.zip';
echo COLOR_BLUE . "=== MagicServer Updater ===\n" . COLOR_NC;
// --- Stop all services before proceeding ---
echo COLOR_YELLOW . "Attempting to stop all running services before update...\n" . COLOR_NC;
require_once __DIR__ . "/stop.php";
echo COLOR_YELLOW . "-----------------------------------------------------\n" . COLOR_NC;
// 1. Fetch latest release info from GitHub
echo "Fetching latest release info from GitHub...\n";
$releaseInfo = fetchJson($apiUrl);
if (!$releaseInfo || !isset($releaseInfo['zipball_url'])) {
echo COLOR_RED . "❌ Failed to fetch release information. Please check your internet connection.\n" . COLOR_NC;
exit(1);
}
$tagName = $releaseInfo['tag_name'];
// --- Find the correct download URL and size from assets ---
$zipUrl = null;
if (!empty($releaseInfo['assets'])) {
foreach ($releaseInfo['assets'] as $asset) {
// Find the first asset that is a zip file
if ($asset['content_type'] === 'application/zip' || pathinfo($asset['name'], PATHINFO_EXTENSION) === 'zip') {
$zipUrl = $asset['browser_download_url'];
break;
}
}
}
// Fallback to zipball_url if no suitable asset is found
if (!$zipUrl) {
$zipUrl = $releaseInfo['zipball_url'];
}
echo "Found latest release: " . COLOR_YELLOW . $tagName . COLOR_NC . "\n";
// Ask for user confirmation before proceeding
echo "Do you want to proceed with the update to version " . COLOR_YELLOW . $tagName . COLOR_NC . "? (y/n): ";
$handle = fopen("php://stdin", "r");
$line = fgets($handle);
fclose($handle);
if (trim(strtolower($line)) !== 'y') {
echo COLOR_RED . "Update cancelled by user.\n" . COLOR_NC;
exit(0);
}
// 2. Download the release ZIP file
echo "Confirmation received. Downloading update... (This may take a few moments)\n";
$fileContent = fetchStream($zipUrl);
if ($fileContent === false || file_put_contents($tempZip, $fileContent) === false) {
echo COLOR_RED . "❌ Failed to download the update archive.\n" . COLOR_NC;
@unlink($tempZip);
exit(1);
}
if (!file_exists($tempZip)) {
echo COLOR_RED . "❌ Failed to save the update archive.\n" . COLOR_NC;
exit(1);
}
$currentBackupPath = $backupDir . '/update-backup-' . date('Ymd-His');
try {
// 3. Create a backup before updating
echo "Creating backup at: $currentBackupPath\n";
ensureDirectory($backupDir);
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($targetDir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($files as $file) {
$relativePath = str_replace($targetDir . DIRECTORY_SEPARATOR, '', $file->getRealPath());
// Check if the path should be skipped from backup
$shouldSkip = false;
foreach ($skipPaths as $skip) {
// Normalize slashes for comparison
if (strpos(str_replace('\\', '/', $relativePath), $skip) === 0) {
$shouldSkip = true;
break;
}
}
if ($shouldSkip) continue;
$backupFilePath = $currentBackupPath . DIRECTORY_SEPARATOR . $relativePath;
if ($file->isDir()) {
ensureDirectory($backupFilePath);
} else {
ensureDirectory(dirname($backupFilePath));
copy($file->getRealPath(), $backupFilePath);
}
}
echo COLOR_GREEN . "✅ Backup created successfully.\n" . COLOR_NC;
// 4. Open the ZIP and copy files one by one
echo "Applying update...\n";
$zip = new ZipArchive();
if ($zip->open($tempZip) !== true) {
throw new Exception("Failed to open the update archive.");
}
// Find the root directory name inside the zip (e.g., 'Planetbiru-MagicServer-abcdef/')
$rootPrefix = $zip->getNameIndex(0);
$rootPrefix = strpos($rootPrefix, '/') !== false ? substr($rootPrefix, 0, strpos($rootPrefix, '/') + 1) : '';
// Extract all new files to a temporary directory first
echo "Extracting update to a temporary location...\n";
$zip->extractTo($updateTempDir);
$updatedFiles = 0;
$skippedFiles = 0;
for ($i = 0; $i < $zip->numFiles; $i++) {
$entryName = $zip->getNameIndex($i);
$relativePath = str_replace($rootPrefix, '', $entryName);
if (empty($relativePath)) continue;
// Check if the path should be skipped
$shouldSkip = false;
foreach ($skipPaths as $skip) {
if (strpos($relativePath, $skip) === 0) {
$shouldSkip = true;
break;
}
}
if ($shouldSkip) {
$skippedFiles++;
continue;
}
$destinationPath = $targetDir . '/' . $relativePath;
// Special handling for the PHP directory itself
if (strpos($relativePath, 'php/') === 0) {
continue; // Skip PHP files for now, they will be handled by the batch script
}
$sourcePath = $updateTempDir . '/' . $entryName;
// If it's a directory, ensure it exists
if (is_dir($sourcePath)) {
ensureDirectory($destinationPath);
continue;
}
// It's a file, copy it
ensureDirectory(dirname($destinationPath));
if (copy($sourcePath, $destinationPath)) {
$updatedFiles++;
} else {
throw new Exception("Could not copy file: $relativePath");
}
}
$zip->close();
// Check if there is a PHP update
$phpNeedsUpdate = false;
$newPhpDir = $updateTempDir . '/' . $rootPrefix . 'php';
if (is_dir($newPhpDir)) {
$phpNeedsUpdate = true;
echo COLOR_YELLOW . "PHP runtime update detected.\n" . COLOR_NC;
}
if ($phpNeedsUpdate) {
echo COLOR_YELLOW . "A separate script will complete the final update steps.\n" . COLOR_NC;
$batchScriptPath = $targetDir . '/finalize-update-temp.bat';
$batchScriptContent = <<<BAT
@echo off
setlocal
echo Waiting for the main process to release files...
rem Initial delay to allow the PHP process to terminate.
ping 127.0.0.1 -n 3 > nul
BAT;
if ($phpNeedsUpdate) {
$batchScriptContent .= "echo Updating PHP runtime...\r\n";
$batchScriptContent .= "robocopy \"$newPhpDir\" \"$targetDir\\php\" /E /IS /MOVE > NUL\r\n";
}
$batchScriptContent .= <<<BAT
echo Cleaning up temporary update files...
if exist "$updateTempDir" (
rmdir /s /q "$updateTempDir"
)
echo Update finished. Deleting this script...
(endlocal)
(goto) 2>nul & del "%~f0"
BAT;
file_put_contents($batchScriptPath, $batchScriptContent);
// Execute the batch script in a new, detached process
// Redirect output to NUL to prevent "The process tried to write to a nonexistent pipe" error.
// The process is started in the background and fully detached.
$command = "start /B \"\" \"$batchScriptPath\" > NUL 2>&1";
pclose(popen($command, "r"));
}
echo COLOR_GREEN . "✅ Update applied successfully.\n" . COLOR_NC;
echo " - Files updated: $updatedFiles\n";
echo " - Files/directories skipped: $skippedFiles\n";
// 5. Success: Clean up backup
echo "Update successful. Removing temporary files...\n";
deleteFolder($currentBackupPath);
if (!$phpNeedsUpdate) { // Only clean up if no batch script is running
deleteFolder($updateTempDir);
}
echo COLOR_GREEN . "✅ MagicServer has been updated to version " . COLOR_YELLOW . $tagName . COLOR_NC . ".\n";
} catch (Exception $e) {
echo COLOR_RED . "\n❌ AN ERROR OCCURRED: " . $e->getMessage() . "\n" . COLOR_NC;
echo COLOR_YELLOW . "Attempting to roll back changes...\n" . COLOR_NC;
if (!is_dir($currentBackupPath)) {
echo COLOR_RED . "❌ Rollback failed: Backup directory not found.\n" . COLOR_NC;
} else {
// Restore from backup
$backupFiles = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($currentBackupPath, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($backupFiles as $file) {
$relativePath = str_replace($currentBackupPath . DIRECTORY_SEPARATOR, '', $file->getRealPath());
$livePath = $targetDir . DIRECTORY_SEPARATOR . $relativePath;
if ($file->isDir()) {
ensureDirectory($livePath);
} else {
copy($file->getRealPath(), $livePath);
}
}
echo COLOR_GREEN . "✅ Rollback complete. Your previous version has been restored.\n" . COLOR_NC;
// Clean up the backup directory after successful rollback
deleteFolder($currentBackupPath);
}
} finally {
// 6. Always clean up the temporary ZIP file
if (file_exists($tempZip)) {
echo "Cleaning up temporary files...\n";
@unlink($tempZip);
if (!$phpNeedsUpdate) {
deleteFolder($updateTempDir); // Clean up only if the batch script is not responsible for it
}
}
}