|
|
|
|
<?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;
|
|
|
|
|
|
|
|
|
|
/** Admin pages use database-only first paints; JSON APIs repeat authorisation. */
|
|
|
|
|
final class CloudPageController extends AdminController
|
|
|
|
|
{
|
|
|
|
|
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 update($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
return $this->view()->display('admin/update.tpl');
|
|
|
|
|
}
|
|
|
|
|
}
|