-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.php
More file actions
118 lines (100 loc) · 2.64 KB
/
install.php
File metadata and controls
118 lines (100 loc) · 2.64 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
<?php
/**
* Check if the current request is running in a test environment.
*
* Test environment is detected by:
* - User agent containing "PostmanRuntime"
* - Host is "localhost:8003"
* - `debug=true` query string parameter
*
* @return bool True if running in test environment, false otherwise.
*/
function checkIfIsTestEnvironment()
{
$postman = isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], "PostmanRuntime") !== false;
$localhost = $_SERVER["HTTP_HOST"] === "localhost:8003";
$debug = isset($_GET["debug"]) && $_GET["debug"] == "true";
if ($postman || $localhost || $debug) {
return true;
}
return false;
}
/**
* Extract a ZIP archive to the given destination.
*
* @param string $file Path to the ZIP file.
* @param string $destination Directory where files should be extracted.
*
* @return bool True if extraction was successful, false otherwise.
*/
function unzip($file, $destination)
{
$zip = new ZipArchive();
$res = $zip->open($file);
if ($res !== true) {
return false;
}
$zip->extractTo($destination);
$zip->close();
return true;
}
/**
* Delete all files and directories in the current directory,
* except those listed in the exceptions array.
*
* @param array $exceptions List of filenames to exclude from deletion.
*
* @return void
*/
function deleteAllExcept(array $exceptions)
{
$items = scandir(__DIR__);
foreach ($items as $item) {
if (in_array($item, array_merge(['.', '..'], $exceptions))) {
continue;
}
$path = __DIR__ . DIRECTORY_SEPARATOR . $item;
if (is_dir($path)) {
deleteDirectory($path);
} else {
unlink($path);
}
}
}
/**
* Recursively delete a directory and all its contents.
*
* @param string $dir Path to the directory.
*
* @return void
*/
function deleteDirectory($dir)
{
$items = scandir($dir);
foreach ($items as $item) {
if ($item === '.' || $item === '..') {
continue;
}
$path = $dir . DIRECTORY_SEPARATOR . $item;
if (is_dir($path)) {
deleteDirectory($path);
} else {
unlink($path);
}
}
rmdir($dir);
}
if (checkIfIsTestEnvironment()) {
http_response_code(400);
die("This is a test environment. Please use the production environment.");
}
$deployFile = "deploy.zip";
$installFile = "install.php";
if (!file_exists($deployFile)) {
http_response_code(404);
die("Deploy file not found.");
}
deleteAllExcept([$deployFile, $installFile]);
unzip($deployFile, "./");
unlink($deployFile);
unlink($installFile);