-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.php
285 lines (270 loc) · 14.5 KB
/
start.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
<?php !session_id() && session_start();
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
ini_set('display_startup_errors', true);
ini_set('html_errors', 1);
define('D', DIRECTORY_SEPARATOR);
define('DEV', false);
define('MIN_VERSION_APACHE', 'v2.4.0');
define('MIN_VERSION_PHP', 'v7.3.0');
define('STABLE_VERSION', 'v3.1.1');
define('STABLE_VERSION_ALERT', 'v3.0.0');
define('STABLE_VERSION_FORM', 'v2.0.0');
define('STABLE_VERSION_PANEL', 'v3.0.1');
define('STABLE_VERSION_USER', 'v2.0.2');
function ping(string $link) {
try {
$h = get_headers($link);
if (!$h || !isset($h[0])) {
return false;
}
$status = (int) (explode(' ', $h[0])[1] ?? 404);
return $status >= 200 && $status < 300;
} catch (Throwable $e) {}
return false;
}
function pull(string $from, string $to) {
if (is_file($to)) {
unlink($to);
}
$status = file_put_contents($to, fopen($from, 'r', false, stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
]
])));
if (is_int($status) && $status > 0) {
$zip = new ZipArchive;
if (true === $zip->open($to)) {
$zip->extractTo(dirname($to));
$zip->close();
unlink($to);
return true;
}
$zip->close();
}
return false;
}
$content = ($prev = $_SESSION['prev'] ?? "");
$error = $prev && false !== strpos($prev, '✘') ? 1 : 0;
$sub = "";
unset($_SESSION['prev']); // Clear the flash alert(s)
if (!is_file(__DIR__ . D . 'index.php')) {
ob_start();
phpinfo();
$info = ob_get_clean();
if (false !== stripos($info, '</body>') && preg_match('/<body(\s[^>]*)?>([\s\S]*?)<\/body>/i', $info, $m)) {
$info = $m[2];
}
$info = strip_tags($info);
$apache_can_rewrite = false;
$version_apache = $version_lite = $version_php = null;
if (false !== stripos($info, 'apache version ') && preg_match('/apache version \s*(?:apache\/)?(v?\S+)/i', $info, $m)) {
$version_apache = $m[1];
}
if (false !== stripos($info, 'litespeed')) {
$version_lite = true;
}
if (false !== stripos($info, 'php version ') && preg_match('/php version \s*(?:php\/)?(v?\S+)/i', $info, $m)) {
$version_php = $m[1];
}
$loaded_modules = stripos($info, 'loaded modules ');
if (false !== $loaded_modules) {
if (false !== stripos(explode("\n", substr($info, $loaded_modules))[0], 'mod_rewrite')) {
$apache_can_rewrite = true;
}
}
if (!$version_apache && $version_lite) {
$content .= '<p aria-live="polite" role="alert">✔ It looks like you are using a LiteSpeed web server. This web server is usually compatible with Apache web server configuration.</p>';
} else if (!$version_apache) {
$content .= '<p role="alert">✘ Your PHP application does not seem to be running on Apache web server.</p>';
++$error;
} else {
if (version_compare($version_apache, $version = preg_replace('/^v/', "", MIN_VERSION_APACHE), '<')) {
$content .= '<p role="alert">✘ Mecha requires at least Apache version ' . $version . '. Your current Apache version is ' . $version_apache . '.</p>';
++$error;
} else {
$content .= '<p aria-live="polite" role="alert">✔ Minimum Apache version is ' . $version . '. Your current Apache version is ' . $version_apache . '.</p>';
if (!$apache_can_rewrite) {
$content .= '<p role="alert">✘ Apache <a href="https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html" target="_blank"><code>mod_rewrite</code></a> extension is disabled or is not available.</p>';
++$error;
} else {
$content .= '<p aria-live="polite" role="alert">✔ Apache <a href="https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html" target="_blank"><code>mod_rewrite</code></a> extension is enabled.</p>';
}
}
}
if (version_compare($version_php, $version = preg_replace('/^v/', "", MIN_VERSION_PHP), '<')) {
$content .= '<p role="alert">✘ Mecha requires at least PHP version ' . $version . '. Your current PHP version is ' . $version_php . '.</p>';
++$error;
} else {
$content .= '<p aria-live="polite" role="alert">✔ Minimum PHP version required is ' . $version . '. Your current PHP version is ' . $version_php . '.</p>';
if (!extension_loaded('json')) {
$content .= '<p role="alert">✘ PHP <a href="https://www.php.net/book.json" target="_blank"><code>json</code></a> extension is disabled or is not available.</p>';
++$error;
} else {
$content .= '<p aria-live="polite" role="alert">✔ PHP <a href="https://www.php.net/book.json" target="_blank"><code>json</code></a> extension is enabled.</p>';
}
if (!extension_loaded('curl') && !filter_var(ini_get('allow_url_fopen'), FILTER_VALIDATE_BOOLEAN)) {
$content .= '<p role="alert">✘ The <code>allow_url_fopen</code> configuration must be enabled to allow PHP functions to retrieve data from remote locations over <abbr title="File Transfer Protocol">FTP</abbr> or <abbr title="Hyper Text Transfer Protocol">HTTP</abbr>.</p>';
++$error;
}
if ('GET' === $_SERVER['REQUEST_METHOD'] && !ping($link = 'https://mecha-cms.com')) {
$content .= '<p role="alert">✘ Could not connect to <code>' . $link . '</code>. The site may be down right now or there may be some problem with your internet connection.</p>';
++$error;
}
}
}
if (!extension_loaded('zip')) {
$content .= '<p role="alert">✘ PHP <a href="https://www.php.net/book.zip" target="_blank"><code>zip</code></a> extension is disabled or is not available. The core application does not require this extension to be enabled, but it is needed to extract the package during the installation process.</p>';
++$error;
}
if ('POST' === $_SERVER['REQUEST_METHOD']) {
$folder = rtrim(strtr($_POST['folder'] ?? __DIR__, '/', D), D) ?: __DIR__;
if (!empty($_POST['sub'])) {
$sub = strtr(substr($folder, strlen(__DIR__ . D)), D, '/');
if ("" === $sub || !preg_match('/^[a-z\d]+(-[a-z\d]+)*(\/[a-z\d]+(-[a-z\d]+)*)*$/', $sub)) {
$_SESSION['prev'] = '<p role="alert">✘ Folder name <code>' . $sub . '</code> must follow the <code>^[a-z\d]+(-[a-z\d]+)*(/[a-z\d]+(-[a-z\d]+)*)*$</code> pattern.</p>';
header('location: ' . $_SERVER['PHP_SELF']);
exit;
}
if (!is_dir($folder) && !mkdir($folder, 0777, true)) {
$_SESSION['prev'] = '<p role="alert">✘ Could not create folder <code>' . $folder . '</code> due to file system error.</p>';
header('location: ' . $_SERVER['PHP_SELF']);
exit;
}
}
// Check if folder does not exist
if (!is_dir($folder)) {
$_SESSION['prev'] = '<p role="alert">✘ Folder <code>' . $folder . '</code> does not exist.</p>';
header('location: ' . $_SERVER['PHP_SELF']);
exit;
}
// Check if folder not write-able
if (!is_writable($folder)) {
$_SESSION['prev'] = '<p role="alert">✘ Folder <code>' . $folder . '</code> is not write-able.</p>';
header('location: ' . $_SERVER['PHP_SELF']);
exit;
}
// Check if folder is not empty
foreach (glob($folder . D . '*', GLOB_NOSORT) as $v) {
if (__FILE__ === $v) {
continue;
}
$_SESSION['prev'] = '<p role="alert">✘ Folder <code>' . $folder . '</code> is not empty.</p>';
header('location: ' . $_SERVER['PHP_SELF']);
exit;
}
$minify = (int) ($_POST['minify'] ?? 1);
$panel = (int) ($_POST['panel'] ?? 1);
$status = (int) ($_POST['status'] ?? 1);
if (!pull('https://' . (DEV ? 'dev.' : "") . 'mecha-cms.com/git/zip/mecha-cms/mecha?minify=' . ($minify ? '1' : '0') . '&target=' . PHP_VERSION . (0 !== $status ? '&version=' . STABLE_VERSION : ""), $folder . D . 'mecha.zip')) {
$_SESSION['prev'] = '<p role="alert">✘ Could not pull <code>mecha-cms/mecha</code> due to network error.</p>';
header('location: ' . $_SERVER['PHP_SELF']);
exit;
}
if (0 !== $panel) {
foreach (['alert', 'form', 'user', 'panel'] as $v) {
if (!is_dir($d = $folder . D . 'lot' . D . 'x' . D . $v) && !mkdir($d, 0777, true)) {
$_SESSION['prev'] = '<p role="alert">✘ Could not create folder <code>' . $d . '</code> due to file system error.</p>';
header('location: ' . $_SERVER['PHP_SELF']);
exit;
}
if (!pull('https://' . (DEV ? 'dev.' : "") . 'mecha-cms.com/git/zip/mecha-cms/x.' . $v . '?minify=' . ($minify ? '1' : '0') . '&target=' . PHP_VERSION . (0 !== $status ? '&version=' . constant('STABLE_VERSION_' . strtoupper($v)) : ""), $d . D . $v . '.zip')) {
$_SESSION['prev'] = '<p role="alert">✘ Could not pull <code>mecha-cms/x.alert</code> due to network error.</p>';
header('location: ' . $_SERVER['PHP_SELF']);
exit;
}
}
}
unlink(__FILE__); // Done!
header('location: ' . dirname($_SERVER['PHP_SELF']) . ("" !== $sub ? '/' . $sub : ""));
exit;
}
header('Content-Type: text/html');
http_response_code(200);
echo '<!DOCTYPE html>';
echo '<html dir="ltr">';
echo '<head>';
echo '<meta charset="utf-8">';
echo '<title>' . ($error > 0 ? 'Error (' . $error . ')' : 'Start') . '</title>';
echo '<link href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAzMzP/MzMz/zMzM/8zMzP/MzMz/////wAzMzP/MzMz/zMzM/8zMzP/////ADMzM/8zMzP/MzMz/zMzM/8zMzP/MzMz/zMzM/8zMzP/MzMz/zMzM/////8AMzMz/zMzM/8zMzP/MzMz/////wAzMzP/MzMz/zMzM/8zMzP/MzMz/zMzM/8zMzP/MzMz/zMzM/8zMzP/////ADMzM/8zMzP/MzMz/zMzM/////8AMzMz/zMzM/8zMzP/MzMz/zMzM/8zMzP/MzMz/zMzM/8zMzP/MzMz/////wAzMzP/MzMz/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AAQgAAAEIAAABCAAAAQgAAAEIAAABCAAAAQgAAA8PAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==" rel="icon">';
echo '<style>';
echo '*{box-sizing:border-box}a{color:#00f;text-decoration:none}button,label,select{cursor:pointer}hr{margin:1em 0}[role=alert]{color:#900;margin:0}[role=alert][aria-live=polite]{color:#060}';
echo '</style>';
echo '</head>';
echo '<body style="max-width: 600px; margin-right: auto; margin-left: auto;">';
echo '<h1>Start</h1>';
echo '<p>This simple interface will help you carry out the installation process based on your goals. Before starting the installation process, I need to make sure that all application requirements are met:</p>';
echo $content;
if (!$prev) {
echo '<p>Your system is ' . ($error > 0 ? 'not ready' : 'ready') . ' to perform the installation!</p>';
}
if (0 === $error) {
echo '<hr>';
echo '<form method="post">';
echo '<p>Specify the installation directory (make sure that this folder exists and is empty):</p>';
echo '<p>';
echo '<input autofocus list="folders" name="folder" placeholder="' . __DIR__ . '" style="display: block; width: 100%;" type="text" value="' . __DIR__ . '">';
echo '<datalist id="folders">';
foreach (glob(__DIR__ . D . '*', GLOB_ONLYDIR) as $v) {
echo '<option>' . $v . '</option>';
}
echo '</datalist>';
echo '</p>';
echo '<p>';
echo '<label>';
echo '<input name="sub" type="checkbox" value="1">';
echo ' ';
echo 'Create the folder if it does not exist';
echo ' ';
echo '(current installation directory is <code>' . __DIR__ . '</code>. Adding a sub-folder path will instruct the installer to create that sub-folder and will install the application there).';
echo '</label>';
echo ' ';
echo '</p>';
echo '<p>What is your goal after the installation is complete?</p>';
echo '<p>';
echo '<label style="display: block;">';
echo '<input checked name="status" type="radio" value="1">';
echo ' ';
echo 'I want to use this application to create a stable web site.';
echo '</label>';
echo '<label style="display: block;">';
echo '<input name="status" type="radio" value="0">';
echo ' ';
echo 'I want to try the latest features of this application before the stable version is released.';
echo '</label>';
echo '</p>';
echo '<p>Would you like to install the control panel feature?</p>';
echo '<p>';
echo '<label style="display: block;">';
echo '<input checked name="panel" type="radio" value="1">';
echo ' ';
echo 'Yes, I want to manage my web site content with a control panel.';
echo '</label>';
echo '<label style="display: block;">';
echo '<input name="panel" type="radio" value="0">';
echo ' ';
echo 'No, I am fine with a source code editor to manage my web site content.';
echo '</label>';
echo '</p>';
echo '<p>Would you like to optimize the source code for production? This action will reduce the file size, but will make the source code unreadable.</p>';
echo '<p>';
echo '<label style="display: block;">';
echo '<input checked name="minify" type="radio" value="1">';
echo ' ';
echo 'Yes, optimize the source code. I don’t care.';
echo '</label>';
echo '<label style="display: block;">';
echo '<input name="minify" type="radio" value="0">';
echo ' ';
echo 'No, keep the source code as it is.';
echo '</label>';
echo '</p>';
echo '<p>';
echo '<button type="submit">Install</button>';
echo '</p>';
echo '</form>';
}
echo '</body>';
echo '</html>';
exit;