Files
nexusphp/nexus/Install/update/update.php

266 lines
11 KiB
PHP
Raw Normal View History

2021-02-01 02:33:45 +08:00
<?php
$rootpath = dirname(dirname(__DIR__)) . '/';
define('ROOT_PATH', $rootpath);
require ROOT_PATH . 'nexus/Install/install_update_start.php';
2021-02-01 02:33:45 +08:00
$isPost = $_SERVER['REQUEST_METHOD'] == 'POST';
2021-02-01 02:33:45 +08:00
$update = new \Nexus\Install\Update();
$currentStep = $update->currentStep();
$maxStep = $update->maxStep();
if (!$update->canAccessStep($currentStep)) {
$update->gotoStep(1);
}
$error = $copy = '';
2021-06-05 17:04:53 +08:00
$pass = true;
2021-02-01 02:33:45 +08:00
//step 1
if ($currentStep == 1) {
$requirements = $update->listRequirementTableRows();
$pass = $requirements['pass'];
if ($isPost) {
$update->nextStep();
}
}
if ($currentStep == 2) {
2021-06-17 20:07:22 +08:00
$tableRows = [];
$versionTable = $versions = [];
2022-04-08 02:44:30 +08:00
$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' => '---',
];
2021-06-17 20:07:22 +08:00
try {
2022-04-18 23:46:00 +08:00
//Unnecessary
// $versions = $update->listVersions();
$versions = [];
2021-06-17 20:07:22 +08:00
} catch (\Exception $exception) {
2022-04-08 02:44:30 +08:00
$update->doLog("can not fetch versions from github: " . $exception->getMessage());
2021-06-17 20:07:22 +08:00
}
2022-02-12 19:46:04 +08:00
if (!$isPost) {
$versionHeader = [
2022-03-08 15:08:56 +08:00
'checkbox' => 'Check',
'tag_name' => 'Version(tag)',
'name' => 'Description',
'published_at' => 'Release at',
2022-02-12 19:46:04 +08:00
];
2022-03-08 21:01:12 +08:00
try {
$timezone = nexus_env('TIMEZONE');
} catch (\Exception $exception) {
$update->doLog("no .env file, release time is github original");
$timezone = null;
}
2022-04-08 02:44:30 +08:00
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());
2022-03-08 21:01:12 +08:00
}
2022-02-12 19:46:04 +08:00
foreach ($versions as $version) {
if ($version['draft']) {
continue;
}
$time = \Carbon\Carbon::parse($version['published_at']);
2022-03-08 21:01:12 +08:00
if ($timezone) {
$time->tz = $timezone;
}
2022-02-12 19:46:04 +08:00
$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'),
];
}
2021-06-17 20:07:22 +08:00
}
2022-02-12 19:46:04 +08:00
2021-06-22 12:56:03 +08:00
// dd($tableRows);
2021-06-17 20:07:22 +08:00
while ($isPost) {
try {
if (empty($_REQUEST['version_url'])) {
2022-03-08 15:08:56 +08:00
throw new \RuntimeException("No version selected yet");
2021-06-17 20:07:22 +08:00
}
2022-02-12 19:46:04 +08:00
$downloadUrl = '';
2021-06-22 12:56:03 +08:00
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]);
2022-02-12 19:46:04 +08:00
} 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, '<=')) {
2022-03-08 15:08:56 +08:00
throw new \RuntimeException("Must select a version higher than the current one(" . VERSION_NUMBER . ")");
2022-02-12 19:46:04 +08:00
}
2021-06-17 20:07:22 +08:00
}
$update->downAndExtractCode($downloadUrl);
$update->nextStep();
} catch (\Exception $exception) {
$update->doLog($exception->getMessage() . $exception->getTraceAsString());
$error = $exception->getMessage();
break;
}
break;
}
}
if ($currentStep == 3) {
2021-05-01 02:02:01 +08:00
$envExampleFile = $rootpath . ".env.example";
2021-02-01 02:33:45 +08:00
$envExampleData = readEnvFile($envExampleFile);
$envFormControls = $update->listEnvFormControls();
$newData = array_column($envFormControls, 'value', 'name');
while ($isPost) {
try {
2022-03-04 16:16:56 +08:00
$update->createEnvFile($_POST, 'update');
2021-02-01 02:33:45 +08:00
$update->nextStep();
} catch (\Exception $exception) {
$error = $exception->getMessage();
break;
}
break;
}
$tableRows = [
[
2021-05-01 02:02:01 +08:00
'label' => basename($envExampleFile),
2021-02-01 02:33:45 +08:00
'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'];
2022-04-17 16:38:44 +08:00
$mysqlInfo = $update->getMysqlVersionInfo();
2021-02-01 02:33:45 +08:00
while ($isPost) {
2022-03-10 21:45:23 +08:00
set_time_limit(300);
2021-02-01 02:33:45 +08:00
try {
// $update->updateDependencies();
2021-02-01 02:33:45 +08:00
$update->createSymbolicLinks($symbolicLinks);
$update->saveSettings($settings);
2021-06-17 20:07:22 +08:00
$update->runExtraQueries();
2022-02-10 22:30:26 +08:00
$update->runMigrate();
2022-03-08 15:08:56 +08:00
$update->runExtraMigrate();
2021-02-01 02:33:45 +08:00
$update->nextStep();
} catch (\Exception $e) {
$error = $e->getMessage();
break;
}
break;
}
}
2021-06-04 10:26:34 +08:00
2022-04-17 16:38:44 +08:00
if (!empty($error) || (isset($mysqlInfo) && !$mysqlInfo['match'])) {
2021-06-04 10:26:34 +08:00
$pass = false;
}
2021-02-01 02:33:45 +08:00
?>
<!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">
2021-02-04 17:57:53 +08:00
<title>Update NexusPHP | step <?php echo $currentStep?></title>
2021-02-01 02:33:45 +08:00
</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">';
2021-06-17 20:07:22 +08:00
$header = [
2022-03-08 15:08:56 +08:00
'label' => 'Item',
'required' => 'Require',
'current'=> 'Current',
'result'=> 'Result'
2021-06-17 20:07:22 +08:00
];
2021-02-01 02:33:45 +08:00
if ($currentStep == 1) {
echo $update->renderTable($header, $requirements['table_rows']);
2021-06-17 20:07:22 +08:00
} elseif ($currentStep == 3) {
2021-02-01 02:33:45 +08:00
echo $update->renderTable($header, $tableRows);
2022-03-08 15:08:56 +08:00
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>';
2021-02-01 02:33:45 +08:00
echo $update->renderForm($envFormControls);
2021-06-17 20:07:22 +08:00
} elseif ($currentStep == 2) {
2021-02-01 20:19:39 +08:00
if (empty($tableRows)) {
2022-03-08 15:08:56 +08:00
echo '<div class="text-green-600 text-center">Sorry, there is no version to choose from at this time!</div>';
2021-02-01 02:33:45 +08:00
} else {
2021-06-17 20:07:22 +08:00
echo $update->renderTable($versionHeader, $tableRows);
2021-02-01 02:33:45 +08:00
}
} elseif ($currentStep == 4) {
echo $update->renderTable($header, $tableRows);
echo '<div class="text-blue-500 pt-10">';
2022-03-08 15:08:56 +08:00
echo sprintf('This step will merge <code>%s</code> to <code>%s</code>, then insert into database.', $tableRows[1]['label'], $tableRows[0]['label']);
2021-02-01 02:33:45 +08:00
echo '</div>';
2022-04-17 16:38:44 +08:00
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']);
}
2021-02-01 02:33:45 +08:00
} elseif ($currentStep > $maxStep) {
2022-03-08 15:08:56 +08:00
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>';
2021-02-01 20:19:39 +08:00
echo '<div class="text-red-500"><code>' . $update->getUpdateDirectory() . '</code></div>';
2022-04-25 02:30:15 +08:00
$update->setLock();
2021-02-01 02:33:45 +08:00
}
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);
}
?>
2022-04-19 16:27:30 +08:00
<div class="mt-2 text-center">
2022-03-08 15:08:56 +08:00
<button class="bg-blue-500 p-2 m-4 text-white rounded" type="button" onclick="goBack()">Prev</button>
2021-02-01 02:33:45 +08:00
<?php if ($currentStep <= $maxStep) {?>
2022-03-08 15:08:56 +08:00
<button class="bg-blue-<?php echo $pass ? 500 : 200;?> p-2 m-4 text-white rounded" type="submit" <?php echo $pass ? '' : 'disabled';?>>Next</button>
2021-02-01 02:33:45 +08:00
<?php } else {?>
2022-03-08 15:08:56 +08:00
<a class="bg-blue-500 p-2 m-4 text-white rounded" href="<?php echo getSchemeAndHttpHost()?>">Go to homepage</a>
2021-02-01 02:33:45 +08:00
<?php }?>
</div>
</form>
</div>
</div>
2022-04-19 16:27:30 +08:00
<div class="m-2 text-center">
2022-03-08 15:08:56 +08:00
Welcome to the NexusPHP updater, if you have any questions, click<a href="https://nexusphp.org/" target="_blank" class="text-blue-500 p-1">here</a>for help.
2021-02-01 02:33:45 +08:00
</div>
</body>
<script>
function goBack() {
window.location.search="step=<?php echo $currentStep == 1 ? 1 : $currentStep - 1?>"
}
</script>
2021-04-30 01:27:29 +08:00
</html>