You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
293 lines
12 KiB
PHP
293 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
use App\Controllers\AdminController;
|
|
use App\Services\Cloud\CloudAccess;
|
|
use App\Services\Cloud\CloudAdminBillingService;
|
|
use App\Services\Cloud\CloudAdminInstanceService;
|
|
use App\Services\Cloud\CloudAdminOperationsService;
|
|
use App\Services\Cloud\CloudAdminResourceService;
|
|
use App\Services\Cloud\CloudCountryCatalog;
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudPlatformDefaultNetworkPolicy;
|
|
use App\Services\Cloud\CloudPlatformTestNetworkPolicy;
|
|
|
|
/** Admin pages use local or database-only first paints; JSON APIs repeat authorisation. */
|
|
final class CloudPageController extends AdminController
|
|
{
|
|
/**
|
|
* Fabric status is loaded through the PHP admin API after the page shell
|
|
* is rendered. Keeping the first paint free of engine SSH calls means a
|
|
* slow or unavailable host cannot block the administrator navigation.
|
|
*/
|
|
public function fabric($request, $response, $args)
|
|
{
|
|
try {
|
|
$actor = CloudAccess::actor(false);
|
|
} catch (CloudError $exception) {
|
|
return $response->withStatus($exception->httpStatus)
|
|
->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
if (!($actor['is_admin'] ?? false)) {
|
|
return $response->withStatus(403)
|
|
->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
$response->getBody()->write($this->view()->assign([
|
|
'fabric_can_manage' => ($actor['is_super_admin'] ?? false)
|
|
&& ($actor['can_manage'] ?? false),
|
|
'fabric_bootstrap_json' => 'null',
|
|
])->fetch('admin/cloud/fabric.tpl'));
|
|
return $response->withStatus(200)
|
|
->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
public function resources($request, $response, $args)
|
|
{
|
|
try {
|
|
$actor = CloudAccess::actor(false);
|
|
} catch (CloudError $exception) {
|
|
return $response->withStatus($exception->httpStatus)
|
|
->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
if (!($actor['is_admin'] ?? false)) {
|
|
return $response->withStatus(403)->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
$payload = null;
|
|
$error = '';
|
|
$status = 200;
|
|
try {
|
|
$payload = CloudAdminResourceService::page($request->getQueryParams());
|
|
} catch (CloudError $exception) {
|
|
$error = $exception->getMessage();
|
|
$status = $exception->httpStatus;
|
|
} catch (\Throwable $exception) {
|
|
error_log('Admin resource snapshot failed: ' . get_class($exception));
|
|
$error = '资源调度数据暂时无法读取,请稍后重试。';
|
|
$status = 503;
|
|
}
|
|
|
|
$smarty = $this->view()->assign([
|
|
'resource_page' => $payload,
|
|
'resource_error' => $error,
|
|
'resource_bootstrap_json' => json_encode($payload,
|
|
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
|
|
| JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE | JSON_THROW_ON_ERROR),
|
|
]);
|
|
$response->getBody()->write($smarty->fetch('admin/cloud/resources.tpl'));
|
|
return $response->withStatus($status)->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
public function hosts($request, $response, $args)
|
|
{
|
|
return $this->view()->assign('cloud_country_options', CloudCountryCatalog::options())
|
|
->display('admin/hosts/index.tpl');
|
|
}
|
|
|
|
public function templates($request, $response, $args)
|
|
{
|
|
return $this->view()->display('admin/cloud/templates.tpl');
|
|
}
|
|
|
|
public function rescue($request, $response, $args)
|
|
{
|
|
return $this->view()->display('admin/cloud/rescue.tpl');
|
|
}
|
|
|
|
public function instances($request, $response, $args)
|
|
{
|
|
$actor = CloudAccess::actor(false);
|
|
if (!($actor['is_admin'] ?? false)) {
|
|
return $response->withStatus(403)->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
$payload = null;
|
|
$error = '';
|
|
$status = 200;
|
|
try {
|
|
$payload = CloudAdminInstanceService::page($actor, $request->getQueryParams());
|
|
} catch (CloudError $exception) {
|
|
$error = $exception->getMessage();
|
|
$status = $exception->httpStatus;
|
|
} catch (\Throwable $exception) {
|
|
error_log('Admin instance snapshot failed: ' . get_class($exception));
|
|
$error = '实例数据暂时无法读取,请稍后重试。';
|
|
$status = 503;
|
|
}
|
|
$smarty = $this->view()->assign([
|
|
'instance_page' => $payload,
|
|
'instance_error' => $error,
|
|
'instance_bootstrap_json' => json_encode($payload,
|
|
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
|
|
| JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE | JSON_THROW_ON_ERROR),
|
|
]);
|
|
$response->getBody()->write($smarty->fetch('admin/cloud/instances.tpl'));
|
|
return $response->withStatus($status)->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
public function orders($request, $response, $args)
|
|
{
|
|
return $response->withRedirect('/admin/billing?view=orders', 302);
|
|
}
|
|
|
|
public function billing($request, $response, $args)
|
|
{
|
|
try {
|
|
$actor = CloudAccess::actor(false);
|
|
} catch (CloudError $exception) {
|
|
return $response->withStatus($exception->httpStatus)
|
|
->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
if (!($actor['is_admin'] ?? false)) {
|
|
return $response->withStatus(403)->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
$payload = null;
|
|
$error = '';
|
|
$status = 200;
|
|
$requestedView = $request->getQueryParams()['view'] ?? 'invoices';
|
|
$view = is_string($requestedView) && in_array($requestedView, ['invoices', 'orders', 'payments'], true)
|
|
? $requestedView : 'invoices';
|
|
try {
|
|
$payload = CloudAdminBillingService::page($request->getQueryParams());
|
|
} catch (CloudError $exception) {
|
|
$error = $exception->getMessage();
|
|
$status = $exception->httpStatus;
|
|
} catch (\Throwable $exception) {
|
|
error_log('Admin billing snapshot failed: ' . get_class($exception));
|
|
$error = '账单数据暂时无法读取,请稍后重试。';
|
|
$status = 503;
|
|
}
|
|
$smarty = $this->view()->assign([
|
|
'billing_page' => $payload, 'billing_error' => $error, 'billing_view' => $view,
|
|
'billing_bootstrap_json' => json_encode($payload,
|
|
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
|
|
| JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE | JSON_THROW_ON_ERROR),
|
|
]);
|
|
$response->getBody()->write($smarty->fetch('admin/billing.tpl'));
|
|
return $response->withStatus($status)->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
public function operations($request, $response, $args)
|
|
{
|
|
try {
|
|
$actor = CloudAccess::actor(false);
|
|
} catch (CloudError $exception) {
|
|
return $response->withStatus($exception->httpStatus)
|
|
->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
if (!($actor['is_admin'] ?? false)) {
|
|
return $response->withStatus(403)->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
$payload = null;
|
|
$error = '';
|
|
$status = 200;
|
|
try {
|
|
$payload = CloudAdminOperationsService::page($actor, $request->getQueryParams());
|
|
} catch (CloudError $exception) {
|
|
$error = $exception->getMessage();
|
|
$status = $exception->httpStatus;
|
|
} catch (\Throwable $exception) {
|
|
error_log('Admin operations snapshot failed: ' . get_class($exception));
|
|
$error = '任务记录暂时无法读取,请稍后重试。';
|
|
$status = 503;
|
|
}
|
|
$smarty = $this->view()->assign([
|
|
'operations_page' => $payload, 'operations_error' => $error,
|
|
'operations_bootstrap_json' => json_encode($payload,
|
|
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
|
|
| JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE | JSON_THROW_ON_ERROR),
|
|
]);
|
|
$response->getBody()->write($smarty->fetch('admin/cloud/operations.tpl'));
|
|
return $response->withStatus($status)->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
public function platformDefaultPolicy($request, $response, $args)
|
|
{
|
|
try {
|
|
$actor = CloudAccess::actor(false);
|
|
} catch (CloudError $exception) {
|
|
return $response->withStatus($exception->httpStatus)
|
|
->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
if (!($actor['is_admin'] ?? false)) {
|
|
return $response->withStatus(403)
|
|
->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
$payload = null;
|
|
$testPayload = null;
|
|
$error = '';
|
|
$testError = '';
|
|
$status = 200;
|
|
try {
|
|
$payload = CloudPlatformDefaultNetworkPolicy::page();
|
|
} catch (\Throwable $exception) {
|
|
error_log('Admin platform policy manifest failed: '
|
|
. get_class($exception));
|
|
$error = '平台默认策略暂时无法读取,请检查部署文件。';
|
|
$status = 503;
|
|
}
|
|
try {
|
|
$testPayload = CloudPlatformTestNetworkPolicy::page();
|
|
} catch (\Throwable $exception) {
|
|
error_log('Admin platform test policy failed: '
|
|
. get_class($exception));
|
|
$testError = '手动测试配置暂时无法读取,请确认数据库已升级。';
|
|
$status = 503;
|
|
}
|
|
$smarty = $this->view()->assign([
|
|
'platform_policy' => $payload,
|
|
'platform_policy_error' => $error,
|
|
'platform_test_policy' => $testPayload,
|
|
'platform_test_policy_error' => $testError,
|
|
'platform_test_policy_bootstrap_json' => json_encode(
|
|
$testPayload,
|
|
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
|
|
| JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE
|
|
| JSON_THROW_ON_ERROR
|
|
),
|
|
]);
|
|
$response->getBody()->write(
|
|
$smarty->fetch('admin/cloud/platform_default_policy.tpl')
|
|
);
|
|
return $response->withStatus($status)
|
|
->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
|
|
public function update($request, $response, $args)
|
|
{
|
|
return $this->view()->display('admin/update.tpl');
|
|
}
|
|
|
|
public function systemLogs($request, $response, $args)
|
|
{
|
|
try {
|
|
$actor = CloudAccess::actor(false);
|
|
} catch (CloudError $exception) {
|
|
return $response->withStatus($exception->httpStatus)
|
|
->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
if (!($actor['is_admin'] ?? false)) {
|
|
return $response->withStatus(403)->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
// Journal reads run asynchronously through the API. Keeping the
|
|
// first paint database-only prevents a slow or wedged journalctl child
|
|
// from holding the whole administrator page open.
|
|
$payload = null;
|
|
$error = '';
|
|
$status = 200;
|
|
$smarty = $this->view()->assign([
|
|
'system_logs_page' => $payload,
|
|
'system_logs_error' => $error,
|
|
'system_logs_timezone' => date_default_timezone_get(),
|
|
'system_logs_bootstrap_json' => json_encode($payload,
|
|
JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
|
|
| JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE
|
|
| JSON_THROW_ON_ERROR),
|
|
]);
|
|
$response->getBody()->write($smarty->fetch('admin/cloud/system_logs.tpl'));
|
|
return $response->withStatus($status)->withHeader('Cache-Control', 'private, no-store');
|
|
}
|
|
}
|