-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.php
More file actions
498 lines (415 loc) · 14.1 KB
/
install.php
File metadata and controls
498 lines (415 loc) · 14.1 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
<?php
// Usage: php install.php <repository> [<token>]
if ($argc < 2) {
die("Usage: php install.php <repository> [<token>]\n");
}
// Get command line arguments
$repository = $argv[1];
$token = isset($argv[2]) ? $argv[2] : null;
// Check if required extensions are loaded
if (!extension_loaded('curl')) {
die("cURL extension is not loaded.");
}
if (!class_exists('ZipArchive')) {
die("ZipArchive class is not available.");
}
// Configure PHP Settings - increase execution time and memory limit
ini_set('max_execution_time', '600'); // 10 minutes
set_time_limit(600); // 10 minutes
ini_set('memory_limit', '1024M');
// Check if required commands are available
function checkCommand($command): bool
{
exec("which $command", $output, $returnVar);
return $returnVar === 0;
}
$commands = ['php', 'wget', 'git'];
foreach ($commands as $cmd) {
if (!checkCommand($cmd)) {
die($cmd . " is not installed or not in the PATH.");
}
}
// Create a temporary directory for installation
$tmp = __DIR__ . '/tmp';
if (!is_dir($tmp)) {
mkdir($tmp, 0755, true);
}
// Always call the same PHP that runs this script
function php_bin(): string {
static $bin = null;
if ($bin !== null) return $bin;
$bin = PHP_BINARY; // exact path, e.g. /usr/bin/php8.2
if (!is_executable($bin)) {
// conservative fallback (optional)
foreach (['/usr/bin/php8.4','/usr/bin/php8.3','/usr/bin/php8.2','/usr/bin/php'] as $cand) {
if (is_executable($cand)) { $bin = $cand; break; }
}
}
return $bin;
}
// Step 1: Get the latest release from GitHub API
function getReleases(): array
{
// Import global variables
global $repository, $token;
// Set the API endpoint
$endpoint = "https://api.github.com/repos/$repository/releases";
// Initialize curl
$cURL = curl_init($endpoint);
// Set Headers
$headers = [
'User-Agent: ' . $repository,
'Accept: application/vnd.github.v3+json'
];
// Check if a token is set
if (!is_null($token) && !empty($token)) {
$headers[] = 'Authorization: token ' . $token;
}
// Set cURL options
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, $headers);
// Execute the request
$response = curl_exec($cURL);
$status = curl_getinfo($cURL, CURLINFO_HTTP_CODE);
// Close the cURL session
curl_close($cURL);
// Check if the response is valid
if($status == 200){
// Decode the response
return json_decode($response, true);
}
return [];
}
$releases = getReleases();
if(empty($releases)){
die("Could not retrieve the releases.");
}
$latest = $releases[array_key_first($releases)];
$assets = $latest['assets'];
$version = $latest['tag_name'];
// Step 2: Retrieve the assets
foreach($assets as $asset){
if($asset['name'] == $version.".zip"){
$archive = $asset['url'];
}
if($asset['name'] == $version.".sha256"){
$checksum = $asset['url'];
}
}
if(!isset($archive) || !isset($checksum)){
die("Could not find the archive and/or checksum.");
}
// Step 3: Download the release ZIP file
function download(string $url, string $destination): bool
{
// Import global variables
global $repository, $token;
// Check if the URL is valid
if (!filter_var($url, FILTER_VALIDATE_URL)) {
return false;
}
// Check if the destination directory exists
if(!is_dir(dirname($destination))){
mkdir(dirname($destination), 0755, true);
}
// Check if the destination file exists
if(file_exists($destination)){
unlink($destination);
}
// Initialize curl
$cURL = curl_init($url);
// Set Headers
$headers = [
'User-Agent: ' . $repository,
'Accept: application/octet-stream',
];
if (!is_null($token) && !empty($token)) {
$headers[] = 'Authorization: token ' . $token;
}
// Set options for the cURL request
$cURLOptions = [
// Provide metadata
CURLOPT_USERAGENT => $repository,
// Insert Headers
CURLOPT_HEADER => 0,
CURLOPT_HTTPHEADER => $headers,
// Return the transfer as a string
CURLOPT_RETURNTRANSFER => true,
// Handle Redirections
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
// Handle Connection Timeout
CURLOPT_TIMEOUT => 30,
// Disable SSL Verification
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
];
// Set the cURL options
curl_setopt_array($cURL, $cURLOptions);
// Execute the request
$stream = curl_exec($cURL);
$status = curl_getinfo($cURL, CURLINFO_HTTP_CODE);
$error = curl_error($cURL);
// Close cURL session
curl_close($cURL);
// Check if the request was successful
if ($status !== 200) {
return false;
}
// Create the file using file_put_contents
$result = file_put_contents($destination, $stream);
if ($result === false) {
return false;
}
return true;
}
download($archive, $tmp . DIRECTORY_SEPARATOR . $version . '.zip');
download($checksum, $tmp . DIRECTORY_SEPARATOR . $version . '.sha256');
if(!file_exists($tmp . DIRECTORY_SEPARATOR . $version . '.zip')){
die("Could not download the file(s).");
}
// Step 4: Validate the checksum
function getChecksum(string $path): string
{
// Check if the file exists
if (!file_exists($path)) {
return '';
}
// Get the checksum from the file
$checksum = file_get_contents($path);
if ($checksum === false) {
return '';
}
// Return the checksum
return trim(explode(" ", $checksum)[0]);
}
function validate(string $path, string $checksum): bool
{
// Check if the file exists
if (!file_exists($path)) {
return false;
}
// Calculate the checksum of the file
$fileChecksum = hash_file('sha256', $path);
// Compare the checksums
return hash_equals($fileChecksum, $checksum);
}
if(!validate($tmp . DIRECTORY_SEPARATOR . $version . '.zip', getChecksum($tmp . DIRECTORY_SEPARATOR . $version . '.sha256'))){
die("Checksum validation failed.");
}
// Step 5: Extract the ZIP file
function extractArchive(string $source, string $destination): bool
{
// Check if the archive file exists
if (!file_exists($source) || !is_file($source)) {
return false;
}
// Attempt to create the destination directory if it doesn't exist
if (!is_dir($destination) && !mkdir($destination, 0755, true) && !is_dir($destination)) {
return false;
}
// Initialize a new ZipArchive instance
$zip = new ZipArchive();
// Try opening the ZIP file
if ($zip->open($source) !== true) {
return false;
}
// Extract the contents to the specified destination
if (!$zip->extractTo($destination)) {
$zip->close();
return false;
}
// Close the ZIP
$zip->close();
// Done
return true;
}
if(!extractArchive($tmp . DIRECTORY_SEPARATOR . $version . '.zip', __DIR__)){
die("Could not extract the archive.");
}
// Step 6: Setup the environment
function setupEnvironment(): bool
{
// Set the path to the Composer executable
$Path = __DIR__ . DIRECTORY_SEPARATOR . '.composer';
// Set the user home directory
putenv('HOME=' . $Path);
putenv('COMPOSER_HOME=' . $Path);
// Create the home directory if it doesn't exist
if (!is_dir($Path)) {
mkdir($Path, 0755, true);
}
// Check if the auth.json file exists
$authFile = $Path . DIRECTORY_SEPARATOR . 'auth.json';
if(!is_file($authFile)){
// Create the auth.json file with default content
$config = json_decode(file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'installer.cfg') ?? '[]', true);
$defaultContent = json_encode($config['composer']['auth'] ?? [], JSON_PRETTY_PRINT);
file_put_contents($authFile, $defaultContent);
}
return is_file($authFile);
}
if(!setupEnvironment()){
die("Could not setup the environment.");
}
// Step 7: Install Composer
function installComposer(string $destination): bool
{
try {
// Download the latest Composer installer
$installerPath = $destination . DIRECTORY_SEPARATOR . 'composer-setup.php';
file_put_contents($installerPath, file_get_contents('https://getcomposer.org/installer'));
// Verify the installer's signature (optional but recommended)
$signature = file_get_contents('https://composer.github.io/installer.sig');
if (!hash_equals(hash_file('sha384', $installerPath), trim($signature))) {
unlink($installerPath);
return false;
}
// Run the Composer installer
chdir($destination);
$command = escapeshellarg(php_bin()) . ' ' . escapeshellarg(basename($installerPath)) . ' --install-dir=' . escapeshellarg($destination) . ' --filename=composer.phar';
exec($command, $output, $exitCode);
chdir(__DIR__);
// Create a symlink to the Composer executable
$composerPath = '.composer' . DIRECTORY_SEPARATOR . 'composer.phar';
$symlinkPath = __DIR__ . DIRECTORY_SEPARATOR . 'composer';
if (file_exists($symlinkPath)) {
unlink($symlinkPath);
}
symlink($composerPath, $symlinkPath);
return $exitCode === 0;
} catch (\Exception $e) {
return false;
}
}
if(!installComposer(__DIR__ . DIRECTORY_SEPARATOR . '.composer')){
die("Could not install Composer.");
}
// Step 8: Install Dependencies
function installDependencies(string $path): bool
{
try {
chdir(__DIR__);
// Install dependencies using Composer
$command = escapeshellarg(php_bin()) . ' ' . escapeshellarg(basename($path)) . ' install --no-dev --no-interaction --prefer-dist';
exec($command, $output, $exitCode);
return $exitCode === 0;
} catch (\Exception $e) {
return false;
}
}
if(!installDependencies(__DIR__ . DIRECTORY_SEPARATOR . 'composer')){
die("Could not install dependencies.");
}
// Step 9: Cleanup
function cleanup(string $directory): bool
{
// If it doesn't exist, treat it as an error or success depending on your preference
if (!file_exists($directory)) {
// Option 1: Treat as an error
return false;
}
// If it's a file or symlink, just unlink it
if (!is_dir($directory)) {
if (!@unlink($directory)) {
return false;
}
return true;
}
// Otherwise, recursively remove contents
$items = scandir($directory);
if ($items === false) {
return false;
}
foreach ($items as $item) {
// Skip pointers
if ($item === '.' || $item === '..') {
continue;
}
$path = $directory . DIRECTORY_SEPARATOR . $item;
// Recursively call delete on each item
if (!cleanup($path)) {
// If any item fails to be deleted, return false
return false;
}
}
// Finally, remove the now-empty directory
if (!@rmdir($directory)) {
return false;
}
return true;
}
if(!cleanup(__DIR__ . DIRECTORY_SEPARATOR . '.composer' . DIRECTORY_SEPARATOR . 'composer-setup.php')){
die("Could not delete the composer installer.");
}
if(!cleanup($tmp)){
die("Could not delete the temporary directory.");
}
// Step 10: Execute the initialization script
function executeCMD(string $cmd): bool
{
// Run the command
exec($cmd, $output, $exitCode);
// Show output when something goes wrong
if ($exitCode !== 0) {
echo implode("\n", $output) . "\n";
}
return $exitCode === 0;
}
if(!executeCMD(escapeshellarg(php_bin()) . ' cli core init')){
die("Could not execute the initialization script.");
}
// Step 11: Install the required extensions
function installExtensions(): bool
{
// Load the extensions to install from the config file
$configPath = __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'laswitchtech' . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'requirement.cfg';
// Check if the config file exists
if (!file_exists($configPath)) {
echo "Configuration file not found: $configPath\n";
return false;
}
// Load the config file
$config = json_decode(file_get_contents($configPath), true);
// Check if the config is valid
if (json_last_error() !== JSON_ERROR_NONE) {
echo "Error parsing the configuration file: " . json_last_error_msg() . "\n";
return false;
}
// Loop through the extensions (modules) and install them
foreach ($config['modules'] as $extension) {
// Show the extension being installed
echo "Installing module: $extension" . PHP_EOL;
// Execute the command to install the extension
if(!executeCMD(escapeshellarg(php_bin()) . ' cli core extension install modules ' . escapeshellarg($extension))){
echo "Failed to install module: $extension" . PHP_EOL;
return false;
}
}
// Loop through the extensions (plugins) and install them
foreach ($config['plugins'] as $extension) {
// Show the extension being installed
echo "Installing plugin: $extension" . PHP_EOL;
// Execute the command to install the extension
if(!executeCMD(escapeshellarg(php_bin()) . ' cli core extension install plugins ' . escapeshellarg($extension))){
echo "Failed to install plugin: $extension" . PHP_EOL;
return false;
}
}
// Loop through the extensions (themes) and install them
foreach ($config['themes'] as $extension) {
// Show the extension being installed
echo "Installing theme: $extension" . PHP_EOL;
// Execute the command to install the extension
if(!executeCMD(escapeshellarg(php_bin()) . ' cli core extension install themes ' . escapeshellarg($extension))){
echo "Failed to install theme: $extension" . PHP_EOL;
return false;
}
}
// Return true if all extensions were installed successfully
return true;
}
if(!installExtensions()){
die("Could not install the required extensions.");
}
echo "Installation completed successfully.\n";