forked from xiaomlove/nexusphp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.php
More file actions
273 lines (260 loc) · 11.1 KB
/
update.php
File metadata and controls
273 lines (260 loc) · 11.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
<?php
$rootpath = dirname(dirname(__DIR__)) . '/';
define('ROOT_PATH', $rootpath);
require ROOT_PATH . 'nexus/Install/install_update_start.php';
$isPost = $_SERVER['REQUEST_METHOD'] == 'POST';
$update = new \Nexus\Install\Update();
$currentStep = $update->currentStep();
$maxStep = $update->maxStep();
if (!$update->canAccessStep($currentStep)) {
$update->gotoStep(1);
}
$error = $copy = '';
$pass = true;
//step 1
if ($currentStep == 1) {
$requirements = $update->listRequirementTableRows();
$pass = $requirements['pass'];
if ($isPost) {
$update->nextStep();
}
}
if ($currentStep == 2) {
$tableRows = [];
$versionTable = $versions = [];
$tableRows[] = [
'checkbox' => sprintf('<input type="radio" name="version_url" value="manual"/>'),
'tag_name' => 'Manual',
'name' => 'If there are changes that are not suitable for full coverage, please check this box and make sure you have updated the code manually',
'published_at' => '---',
];
try {
//Unnecessary
// $versions = $update->listVersions();
$versions = [];
} catch (\Exception $exception) {
$update->doLog("can not fetch versions from github: " . $exception->getMessage());
}
if (!$isPost) {
$versionHeader = [
'checkbox' => 'Check',
'tag_name' => 'Version(tag)',
'name' => 'Description',
'published_at' => 'Release at',
];
try {
$timezone = nexus_env('TIMEZONE');
} catch (\Exception $exception) {
$update->doLog("no .env file, release time is github original");
$timezone = null;
}
try {
$latestCommit = $update->getLatestCommit();
$time = \Carbon\Carbon::parse($latestCommit['commit']['committer']['date']);
if ($timezone) {
$time->tz = $timezone;
}
$tableRows[] = [
'checkbox' => sprintf('<input type="radio" name="version_url" value="development|%s"/>', $latestCommit['sha']),
'tag_name' => 'Latest development code',
'name' => "Development testing only! Latest commit:" . $latestCommit['commit']['message'],
'published_at' => $time->format('Y-m-d H:i:s'),
];
} catch (\Exception $exception) {
$update->doLog("can not fetch latest commit from github: " . $exception->getMessage());
}
foreach ($versions as $version) {
if ($version['draft']) {
continue;
}
$time = \Carbon\Carbon::parse($version['published_at']);
if ($timezone) {
$time->tz = $timezone;
}
$versionUrl = $version['tag_name'] . '|' . $version['tarball_url'];
$checked = !empty($_REQUEST['version_url']) && $_REQUEST['version_url'] == $versionUrl ? ' checked' : '';
$tableRows[] = [
'checkbox' => sprintf('<input type="radio" name="version_url" value="%s"%s/>', $versionUrl, $checked),
'tag_name' => $version['tag_name'],
'name' => $version['name'],
'published_at' => $time->format('Y-m-d H:i:s'),
];
}
}
// dd($tableRows);
while ($isPost) {
try {
if (empty($_REQUEST['version_url'])) {
throw new \RuntimeException("No version selected yet");
}
$downloadUrl = '';
if ($_REQUEST['version_url'] == 'manual') {
$update->nextStep();
} elseif (\Illuminate\Support\Str::startsWith($_REQUEST['version_url'], 'development')) {
$downloadUrlArr = explode('|', $_REQUEST['version_url']);
$downloadUrl = sprintf('https://github.com/xiaomlove/nexusphp/archive/%s.zip', $downloadUrlArr[1]);
} else {
$versionUrlArr = explode('|', $_REQUEST['version_url']);
$version = strtolower($versionUrlArr[0]);
$downloadUrl = $versionUrlArr[1];
if (\Illuminate\Support\Str::startsWith($version, 'v')) {
$version = substr($version, 1);
}
$update->doLog("version: $version, downloadUrl: $downloadUrl, currentVersion: " . VERSION_NUMBER);
if (version_compare($version, VERSION_NUMBER, '<=')) {
throw new \RuntimeException("Must select a version higher than the current one(" . VERSION_NUMBER . ")");
}
}
$update->downAndExtractCode($downloadUrl);
$update->nextStep();
} catch (\Exception $exception) {
$update->doLog($exception->getMessage() . $exception->getTraceAsString());
$error = $exception->getMessage();
break;
}
break;
}
}
if ($currentStep == 3) {
$envExampleFile = $rootpath . ".env.example";
$envExampleData = readEnvFile($envExampleFile);
$envFormControls = $update->listEnvFormControls();
$newData = array_column($envFormControls, 'value', 'name');
while ($isPost) {
try {
$update->createEnvFile($_POST, 'update');
$update->nextStep();
} catch (\Exception $exception) {
$error = $exception->getMessage();
break;
}
break;
}
$tableRows = [
[
'label' => basename($envExampleFile),
'required' => 'exists && readable',
'current' => $envExampleFile,
'result' => $update->yesOrNo(file_exists($envExampleFile) && is_readable($envExampleFile)),
],
];
$fails = array_filter($tableRows, function ($value) {return $value['result'] == 'NO';});
$pass = empty($fails);
}
if ($currentStep == 4) {
$settingTableRows = $update->listSettingTableRows();
$settings = $settingTableRows['settings'];
$symbolicLinks = $settingTableRows['symbolic_links'];
$tableRows = $settingTableRows['table_rows'];
$pass = $settingTableRows['pass'];
$mysqlInfo = $update->getMysqlVersionInfo();
$redisInfo = $update->getREdisVersionInfo();
while ($isPost) {
set_time_limit(300);
try {
// $update->updateDependencies();
$update->createSymbolicLinks($symbolicLinks);
$update->saveSettings($settings);
$update->runExtraQueries();
$update->runMigrate();
$update->runExtraMigrate();
$update->nextStep();
} catch (\Exception $e) {
$error = $e->getMessage();
break;
}
break;
}
}
if (
!empty($error)
|| (isset($mysqlInfo) && !$mysqlInfo['match'])
|| (isset($redisInfo) && !$redisInfo['match'])
) {
$pass = false;
}
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
<title>Update NexusPHP | step <?php echo $currentStep?></title>
</head>
<body>
<div class="container mx-auto">
<?php echo $update->renderSteps()?>
<div class="mt-10">
<form method="post" action="<?php echo getBaseUrl() . '?step=' . $currentStep?>">
<input type="hidden" name="step" value="<?php echo $currentStep?>">
<?php
echo'<div class="step-' . $currentStep . ' text-center">';
$header = [
'label' => 'Item',
'required' => 'Require',
'current'=> 'Current',
'result'=> 'Result'
];
if ($currentStep == 1) {
echo $update->renderTable($header, $requirements['table_rows']);
} elseif ($currentStep == 3) {
echo $update->renderTable($header, $tableRows);
echo '<div class="text-gray-700 p-4 text-red-400">Before the next step, make sure that you have executed <code>composer install</code> in the root directory to update the dependencies.</div>';
echo $update->renderForm($envFormControls);
} elseif ($currentStep == 2) {
if (empty($tableRows)) {
echo '<div class="text-green-600 text-center">Sorry, there is no version to choose from at this time!</div>';
} else {
echo $update->renderTable($versionHeader, $tableRows);
}
} elseif ($currentStep == 4) {
echo $update->renderTable($header, $tableRows);
echo '<div class="text-blue-500 pt-10">';
echo sprintf('This step will merge <code>%s</code> to <code>%s</code>, then insert into database.', $tableRows[1]['label'], $tableRows[0]['label']);
echo '</div>';
if (!$mysqlInfo['match']) {
echo sprintf('<div class="text-red-700 pt-10">MySQL version: %s is too low, please use the newest version of 5.7 or above.</div>', $mysqlInfo['version']);
}
if (!$redisInfo['match']) {
echo sprintf('<div class="text-red-700 pt-10">Redis version: %s is too low, please use 2.0.0 or above.</div>', $redisInfo['version']);
}
} elseif ($currentStep > $maxStep) {
echo '<div class="text-green-900 text-6xl p-10">Congratulations, everything is ready!</div>';
echo '<div class="mb-6">For questions, consult the upgrade log at: <code>' . $update->getLogFile() . '</code></div>';
echo '<div class="text-red-500">For security reasons, please delete the following directories</div>';
echo '<div class="text-red-500"><code>' . $update->getUpdateDirectory() . '</code></div>';
$update->setLock();
}
echo'</div>';
if (!empty($error)) {
echo sprintf('<div class="text-center text-red-500 p-4">Error: %s</div>', nl2br($error));
unset($error);
}
if (!empty($copy)) {
echo sprintf('<div class="text-center"><textarea class="w-1/2 h-40 border">%s</textarea></div>', $copy);
unset($copy);
}
?>
<div class="mt-2 text-center">
<button class="bg-blue-500 p-2 m-4 text-white rounded" type="button" onclick="goBack()">Prev</button>
<?php if ($currentStep <= $maxStep) {?>
<button class="bg-blue-<?php echo $pass ? 500 : 200;?> p-2 m-4 text-white rounded" type="submit" <?php echo $pass ? '' : 'disabled';?>>Next</button>
<?php } else {?>
<a class="bg-blue-500 p-2 m-4 text-white rounded" href="<?php echo getSchemeAndHttpHost()?>">Go to homepage</a>
<?php }?>
</div>
</form>
</div>
</div>
<div class="m-2 text-center">
Welcome to the NexusPHP updater, if you have any questions, click<a href="https://nexusphp.org/" target="_blank" rel="noopener" class="text-blue-500 p-1">here</a>for help.
</div>
</body>
<script>
function goBack() {
window.location.search="step=<?php echo $currentStep == 1 ? 1 : $currentStep - 1?>"
}
</script>
</html>