-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.php
More file actions
255 lines (223 loc) · 9.18 KB
/
Copy pathinstall.php
File metadata and controls
255 lines (223 loc) · 9.18 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
<?php
const REQUIRED_PHP_VERSION = '8.2.0';
const REQUIRED_EXTENSIONS = ['pdo', 'mbstring', 'tokenizer', 'xml', 'ctype', 'json', 'PCRE', 'Session', 'zip', 'curl'];
const OWNER = 'SyncEngine';
const REPO = 'SyncEngine';
const GITHUB_API = 'https://api.github.com/repos/' . OWNER . '/' . REPO . '/releases';
$currentStep = $_GET['step'] ?? 1;
$targetDir = realpath(__DIR__ . '/../') . '/';
$expectedDirName = 'public';
if (basename(__DIR__) !== $expectedDirName) {
echo "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><title>Error</title></head><body>
<div style='margin:50px auto;max-width:600px;font-family:sans-serif;text-align:center;'>
<h2 style='color:red;'>Error: install.php must be located inside the /public directory.</h2>
<p>Please move <code>install.php</code> to the correct location and try again.</p>
</div></body></html>";
exit;
}
function headerHtml($title)
{
echo "<!DOCTYPE html><html lang='en'><head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<title>$title</title>
<link href='https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css' rel='stylesheet'>
</head><body class='bg-light'>
<div class='container py-5' style='max-width: 700px;'>
<div class='card'><div class='card-body'><h3 class='mb-4'>$title</h3>";
}
function footerHtml()
{
echo "</div></div></div></body></html>";
}
function formatBytes($bytes, $decimals = 2)
{
$size = ['B', 'KB', 'MB', 'GB', 'TB'];
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . ' ' . $size[$factor];
}
function githubApiRequest($url)
{
$headers = [
"User-Agent: install-script",
"Accept: application/vnd.github.v3+json",
];
if (extension_loaded('curl')) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$response = curl_exec($ch);
$curlError = curl_error($ch);
curl_close($ch);
if ($response === false) {
return [null, "Request to GitHub failed (curl): $curlError"];
}
return [$response, null];
}
if (ini_get('allow_url_fopen')) {
$opts = [
'http' => [
'header' => implode("\r\n", $headers) . "\r\n",
'timeout' => 15,
'ignore_errors' => true,
],
];
$context = stream_context_create($opts);
$response = @file_get_contents($url, false, $context);
if ($response === false) {
$reason = error_get_last()['message'] ?? 'unknown error';
return [null, "Request to GitHub failed: $reason"];
}
return [$response, null];
}
return [null, "Neither the curl extension nor allow_url_fopen is available on this server. Enable one of them (curl is recommended) to continue."];
}
function downloadReleaseAsset($downloadUrl, $targetFile)
{
$ch = curl_init($downloadUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["User-Agent: install-script"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$data = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code !== 200) return "Failed to download asset, HTTP $code";
file_put_contents($targetFile, $data);
return true;
}
if ($currentStep == 1) {
headerHtml("Step 1: Server Requirements");
$ok = true;
echo "<ul class='list-group'>";
echo "<li class='list-group-item'>Required PHP: " . REQUIRED_PHP_VERSION . "</li>";
echo "<li class='list-group-item'>Current PHP: " . PHP_VERSION . "</li>";
if (version_compare(PHP_VERSION, REQUIRED_PHP_VERSION, '<')) {
echo "<li class='list-group-item text-danger'>PHP version too old</li>";
$ok = false;
}
foreach (REQUIRED_EXTENSIONS as $ext) {
if (!extension_loaded($ext)) {
echo "<li class='list-group-item text-danger'>Missing extension: $ext</li>";
$ok = false;
} else {
echo "<li class='list-group-item text-success'>$ext loaded</li>";
}
}
echo "</ul>";
if ($ok) {
echo "<div class='mt-4 text-success'>Server environment looks good.</div>";
echo "<a href='?step=2' class='btn btn-primary mt-3'>Next Step</a>";
} else {
echo "<div class='mt-4 text-danger'>Fix the above issues to continue.</div>";
}
footerHtml();
exit;
}
if ($currentStep == 2) {
$error = '';
$done = false;
$releases = [];
$selectedReleaseId = $_POST['release'] ?? null;
$selectedAssetId = $_POST['asset'] ?? null;
[$json, $requestFailure] = githubApiRequest(GITHUB_API);
if ($requestFailure) {
$error = $requestFailure;
} else {
$data = json_decode($json, true);
if (is_array($data) && array_is_list($data)) {
$releases = $data;
} elseif (is_array($data) && isset($data['message'])) {
$error = "GitHub API error: " . $data['message'];
} else {
$error = "Failed to parse GitHub releases (unexpected response: " . htmlspecialchars(substr((string) $json, 0, 200)) . ")";
}
}
if (!$error && $_SERVER['REQUEST_METHOD'] === 'POST' && $selectedAssetId && $selectedReleaseId) {
$downloadUrl = null;
foreach ($releases as $r) {
if ($r['id'] == $selectedReleaseId) {
foreach ($r['assets'] ?? [] as $a) {
if ($a['id'] == $selectedAssetId) {
$downloadUrl = $a['browser_download_url'];
break 2;
}
}
}
}
if (!$downloadUrl) {
$error = "Could not resolve the selected asset's download URL.";
} else {
$tmpZip = tempnam(sys_get_temp_dir(), 'gh_') . '.zip';
$result = downloadReleaseAsset($downloadUrl, $tmpZip);
if ($result === true) {
$zip = new ZipArchive();
if ($zip->open($tmpZip) === true) {
$zip->extractTo($targetDir);
$zip->close();
unlink($tmpZip);
$done = true;
} else {
$error = "Failed to open ZIP archive.";
}
} else {
$error = $result;
}
}
}
headerHtml("Step 2: Download from GitHub");
if ($error) echo "<div class='alert alert-danger'>$error</div>";
elseif ($done) {
echo "<div class='alert alert-success'>Extraction complete.</div>";
echo "<a href='?step=3' class='btn btn-success mt-3'>Next Step</a>";
footerHtml(); exit;
}
if (!$error && $releases && !$selectedReleaseId) {
echo "<form method='post' class='mb-3'>
<label for='release' class='form-label'>Select Release</label>
<select name='release' id='release' class='form-select' required onchange='this.form.submit()'>
<option value=''>-- Choose Release --</option>";
foreach ($releases as $release) {
echo "<option value='{$release['id']}'>" . htmlspecialchars($release['name'] ?? $release['tag_name']) . "</option>";
}
echo "</select>
<noscript><button class='btn btn-primary mt-3'>Continue</button></noscript>
</form>";
} elseif (!$error && $selectedReleaseId) {
$assets = [];
foreach ($releases as $r) {
if ($r['id'] == $selectedReleaseId) {
$assets = $r['assets'] ?? [];
break;
}
}
if (!$assets) {
echo "<div class='alert alert-warning'>No downloadable assets found in this release.</div>";
} else {
echo "<form method='post' class='mb-3'>
<input type='hidden' name='release' value='" . htmlspecialchars($selectedReleaseId) . "'>
<label for='asset' class='form-label'>Select Asset</label>
<select name='asset' id='asset' class='form-select' required>";
foreach ($assets as $a) {
echo "<option value='{$a['id']}'>" . htmlspecialchars($a['name']) . " (" . formatBytes($a['size']) . ")</option>";
}
echo "</select>
<button class='btn btn-success mt-3'>Download and Extract</button>
</form>";
}
}
footerHtml();
exit;
}
if ($currentStep == 3) {
headerHtml("Step 3: Finalize Installation");
echo "<p>Installation successful. Redirecting to <code>database setup</code>...</p>";
$basePath = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$installUrl = $basePath . '/index.php';
echo '<script>setTimeout(() => { window.location.href = "' . $installUrl . '"; }, 1500);</script>';
echo '<a href="' . $installUrl . '" class="btn btn-success mt-3">Go to Installer</a>';
footerHtml();
exit;
}