-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
358 lines (290 loc) · 8.67 KB
/
index.php
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
<?php
/**
* The Azuriom installer.
*
* This file is not a part of Azuriom itself,
* and can be removed when Azuriom is installed.
*
* @author Azuriom
*/
$installerVersion = '1.0.0';
$minPhpVersion = '8.0';
$requiredExtensions = [
'bcmath', 'ctype', 'json', 'mbstring', 'openssl', 'PDO', 'tokenizer', 'xml', 'xmlwriter', 'curl', 'fileinfo', 'zip',
];
set_error_handler(function ($level, $message, $file = 'unknown', $line = 0) {
http_response_code(500);
exit(json_encode(['message' => "A fatal error occurred: {$message} ({$file}:{$line})"]));
});
//
// Some helper functions
//
/**
* Parse the PHP version to x.x format.
*
* @return string
*/
function parse_php_version()
{
preg_match('/^(\d+)\.(\d+)/', PHP_VERSION, $matches);
if (count($matches) > 2) {
return "{$matches[1]}.{$matches[2]}";
}
return PHP_VERSION;
}
/**
* Get an item from an array using "dot" notation.
*
* @param array $array
* @param int|string $key
* @param mixed $default
*
* @return mixed
*/
function array_get($array, $key, $default = null)
{
if (array_key_exists($key, $array)) {
return $array[$key];
}
if (strpos($key, '.') === false) {
return isset($array[$key]) ? $array[$key] : $default;
}
foreach (explode('.', $key) as $segment) {
if (! array_key_exists($segment, $array)) {
return $default;
}
$array = $array[$segment];
}
return $array;
}
/**
* Get the HTTP method of the request.
*
* @return string
*/
function request_method()
{
return strtoupper(array_get($_SERVER, 'REQUEST_METHOD', 'GET'));
}
/**
* Get the base url of the request.
*
* @return string
*/
function request_url()
{
$scheme = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http';
$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
$path = ! empty($_SERVER['REQUEST_URI']) ? explode('?', $_SERVER['REQUEST_URI'])[0] : '';
return "{$scheme}://{$host}{$path}";
}
$requestContent = null;
/**
* Get an input from the request.
*
* @param string $key
* @param mixed $default
*
* @return null|string
*/
function request_input($key, $default = null)
{
global $requestContent;
if (! in_array(request_method(), ['GET', 'HEAD'], true)) {
if ($requestContent === null) {
$requestContent = json_decode(file_get_contents('php://input'), true);
}
if ($requestContent) {
$value = array_get($requestContent, $key);
if ($value !== null) {
return $value;
}
}
}
return array_get($_GET, $key, $default);
}
/**
* Send the response as JSON and exit.
*
* @param array $data
* @param int $status
*/
function send_json_response($data = null, $status = 200)
{
if ($data === null && $status === 200) {
$status = 204;
}
if ($status !== 200) {
http_response_code($status);
}
header('Content-Type: application/json');
if ($data === null) {
exit();
}
exit(json_encode($data));
}
/**
* Read the given url as a string.
*
* @param string $url
* @param null|array $curlOptions
*
* @return string
*/
function read_url($url, $curlOptions = null)
{
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_CONNECTTIMEOUT => 150,
CURLOPT_HTTPHEADER => [
'User-Agent: Azuriom Installer v1',
],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
]);
if ($curlOptions !== null) {
curl_setopt_array($ch, $curlOptions);
}
$response = curl_exec($ch);
$errno = curl_errno($ch);
if ($errno || $response === false) {
$error = curl_error($ch);
throw new RuntimeException("cURL error {$errno}: {$error}");
}
$statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if ($statusCode >= 400) {
throw new RuntimeException("HTTP code {$statusCode} returned for '{$url}'.", $statusCode);
}
curl_close($ch);
return $response;
}
/**
* Download a file from the given url and save it to the given path.
*
* @param string $url
* @param string $path
*
* @return string
*/
function download_file($url, $path)
{
return read_url($url, [CURLOPT_FILE => fopen($path, 'wb+')]);
}
/**
* Determines if a function exists and is not disabled.
*
* @param string $function
*
* @return bool
*/
function has_function($function)
{
if (! function_exists($function)) {
return false;
}
try {
return strpos(ini_get('disable_functions'), $function) === false;
} catch (Exception $e) {
return false;
}
}
if (array_get($_GET, 'phpinfo') === '') {
phpinfo();
exit();
}
//
// Give the requested data if the request is from AJAX.
//
if (array_get($_SERVER, 'HTTP_X_REQUESTED_WITH') === 'XMLHttpRequest'
|| array_get($_GET, 'execute') === 'php') {
try {
$data = [
'installerVersion' => $installerVersion,
'minPhpVersion' => $minPhpVersion,
'phpVersion' => parse_php_version(),
'phpFullVersion' => PHP_VERSION,
'phpIniPath' => php_ini_loaded_file(),
'path' => __DIR__,
'file' => __FILE__,
'htaccess' => file_exists(__DIR__.'/.htaccess') && file_exists(__DIR__.'/public/.htaccess'),
];
$step = 'check';
$writable = is_writable(__DIR__) && is_writable(__DIR__.'/public');
$requirements = [
'php' => version_compare(PHP_VERSION, $minPhpVersion, '>='),
'writable' => $writable,
'function-symlink' => has_function('symlink'),
'rewrite' => isset($validInstallationUrlRewrite),
];
$extracted = file_exists(__DIR__.'/vendor');
foreach ($requiredExtensions as $extension) {
$requirements['extension-'.$extension] = extension_loaded($extension);
}
$data['requirements'] = $requirements;
$data['compatible'] = ! in_array(false, $requirements, true);
$data['downloaded'] = file_exists(__DIR__.'/Azuriom.zip');
$data['extracted'] = $extracted;
$action = request_input('action');
if (request_method() !== 'POST') {
send_json_response($data);
}
if ($action === 'download') {
// Get the latest download url
$json = read_url('https://market.azuriom.com/api/download');
$response = json_decode($json);
if (! $response) {
throw new RuntimeException('The response from Azuriom API is not a valid JSON.');
}
$file = __DIR__.'/'.$response->file;
$needDownload = true;
if (file_exists($file)) {
// File was already downloaded before, if it's valid we don't
// need to download it again.
if (hash_equals($response->hash, hash_file('sha256', $file))) {
$needDownload = false;
} else {
unlink($file);
}
}
if ($needDownload) {
download_file($response->url, $file);
}
if (! file_exists($file)) {
throw new RuntimeException('The file was not downloaded.');
}
if (! hash_equals($response->hash, hash_file('sha256', $file))) {
throw new RuntimeException('File size don\'t match the expected size.');
}
$zip = new ZipArchive();
if (($status = $zip->open($file)) !== true) {
throw new RuntimeException('Unable to open zip: '.$status.'.');
}
if (! $zip->extractTo(__DIR__)) {
throw new RuntimeException('Unable to extract zip');
}
$zip->close();
send_json_response($data);
}
send_json_response('Unexpected action: '.$action, 403);
} catch (Throwable $t) {
http_response_code(500);
exit(json_encode(['message' => $t->getMessage()]));
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="https://azuriom.com/assets/img/logo.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Installation - Azuriom</title>
<script type="module" crossorigin src="https://cdn.jsdelivr.net/gh/Azuriom/AzuriomInstaller@1.1.0/build/assets/index.1adc9a4a.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Azuriom/AzuriomInstaller@1.1.0/build/assets/index.3019197c.css">
</head>
<body>
<div id="app"></div>
</body>
</html>