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.
582 lines
24 KiB
PHP
582 lines
24 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Models\CloudBackupSubscription;
|
|
use App\Models\CloudBackupPeriodEntitlement;
|
|
use App\Models\CloudInstance;
|
|
use App\Models\CloudInstanceBackup;
|
|
use App\Models\CloudJob;
|
|
use App\Models\CloudOffer;
|
|
|
|
/** Owner-facing paid automatic-backup policy and bounded scheduler. */
|
|
final class CloudAutomaticBackupService
|
|
{
|
|
public const TIMEZONE = 'America/Los_Angeles';
|
|
public const RUN_AT = '00:00';
|
|
public const MAX_RETENTION = 2;
|
|
|
|
private const RETRY_SECONDS = 300;
|
|
|
|
public static function activatePurchased(CloudInstance $instance): ?CloudBackupSubscription
|
|
{
|
|
$spec = self::spec($instance);
|
|
if (($spec['automatic_backup'] ?? null) !== true) {
|
|
return null;
|
|
}
|
|
$price = self::moneySnapshot($spec['backup_price'] ?? null);
|
|
// Validate the complete commercial snapshot. This deliberately does
|
|
// not infer legacy aliases or the offer's current mutable price.
|
|
self::moneySnapshot($spec['instance_price'] ?? null);
|
|
$version = filter_var($spec['price_version'] ?? null,
|
|
FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
|
|
$currency = strtoupper((string) ($spec['currency'] ?? ''));
|
|
if ($version === false || $currency !== 'USD'
|
|
|| (int) $instance->offer_id < 1) {
|
|
throw new CloudError('实例缺少可信的自动备份计费快照', 409);
|
|
}
|
|
|
|
return $instance->getConnection()->transaction(function () use (
|
|
$instance, $price, $version, $currency
|
|
): CloudBackupSubscription {
|
|
$locked = CloudInstance::where('id', (int) $instance->id)
|
|
->lockForUpdate()->first();
|
|
if ($locked === null || (string) $locked->status === 'deleted'
|
|
|| (int) $locked->user_id !== (int) $instance->user_id) {
|
|
throw new CloudError('虚拟机不存在', 404);
|
|
}
|
|
$subscription = CloudBackupSubscription::where(
|
|
'instance_id', (int) $locked->id
|
|
)->lockForUpdate()->first();
|
|
if ($subscription !== null) {
|
|
if ((int) $subscription->user_id !== (int) $locked->user_id
|
|
|| (int) $subscription->offer_id !== (int) $locked->offer_id
|
|
|| CloudMoney::cents((string) $subscription->price)
|
|
!== CloudMoney::cents($price)
|
|
|| strtoupper((string) $subscription->currency)
|
|
!== $currency
|
|
|| (int) $subscription->price_version !== (int) $version) {
|
|
throw new CloudError('自动备份订阅与实例计费账本不一致', 409);
|
|
}
|
|
return $subscription;
|
|
}
|
|
$now = time();
|
|
$subscription = new CloudBackupSubscription();
|
|
$subscription->instance_id = (int) $locked->id;
|
|
$subscription->user_id = (int) $locked->user_id;
|
|
$subscription->offer_id = (int) $locked->offer_id;
|
|
$subscription->enabled = true;
|
|
$subscription->renew_next_period = true;
|
|
$subscription->frequency = 'daily';
|
|
$subscription->weekday = 0;
|
|
$subscription->retention_count = self::MAX_RETENTION;
|
|
$subscription->timezone = self::TIMEZONE;
|
|
$subscription->price = $price;
|
|
$subscription->currency = $currency;
|
|
$subscription->price_version = (int) $version;
|
|
$subscription->next_run_at = self::nextRunAt(
|
|
$now, 'daily', 0
|
|
);
|
|
$subscription->last_scheduled_at = null;
|
|
$subscription->last_backup_id = null;
|
|
$subscription->created_at = $subscription->updated_at = $now;
|
|
$subscription->save();
|
|
return $subscription;
|
|
}, 3);
|
|
}
|
|
|
|
public static function settings(CloudInstance $instance, array $actor): array
|
|
{
|
|
self::assertOwner($instance, $actor);
|
|
$subscription = CloudBackupSubscription::where(
|
|
'instance_id', (int) $instance->id
|
|
)->first();
|
|
$entitled = self::entitled($instance, $subscription, time());
|
|
if ($entitled && $subscription === null) {
|
|
throw new CloudError('自动备份权益缺少订阅账本,请联系管理员', 409);
|
|
}
|
|
if ($subscription !== null
|
|
&& (int) $subscription->user_id !== (int) $instance->user_id) {
|
|
throw new CloudError('自动备份订阅归属不一致', 409);
|
|
}
|
|
return [
|
|
'instance_id' => (string) $instance->id,
|
|
'entitled' => $entitled,
|
|
'subscription' => $subscription === null ? null
|
|
: self::displaySubscription($subscription),
|
|
'timezone' => self::TIMEZONE,
|
|
'run_at' => self::RUN_AT,
|
|
'max_retention' => self::MAX_RETENTION,
|
|
'backups' => self::backupsForInstance($instance),
|
|
];
|
|
}
|
|
|
|
/** Keep the next-cycle backup fee aligned with a completed plan upgrade. */
|
|
public static function refreshPriceAfterUpgradeLocked(
|
|
CloudInstance $instance,
|
|
CloudOffer $offer
|
|
): void {
|
|
$subscription = CloudBackupSubscription::where(
|
|
'instance_id', (int) $instance->id
|
|
)->lockForUpdate()->first();
|
|
if ($subscription === null) {
|
|
return;
|
|
}
|
|
if ((int) $offer->id !== (int) $instance->offer_id
|
|
|| (int) $subscription->user_id !== (int) $instance->user_id
|
|
|| strtoupper((string) $offer->currency) !== 'USD') {
|
|
throw new CloudError('升级后的自动备份价格账本不一致', 409);
|
|
}
|
|
$price = self::moneySnapshot($offer->backup_price);
|
|
$subscription->offer_id = (int) $offer->id;
|
|
$subscription->price = $price;
|
|
$subscription->price_version = (int) $offer->price_version;
|
|
$subscription->updated_at = time();
|
|
$subscription->save();
|
|
}
|
|
|
|
public static function update(CloudInstance $instance, array $actor,
|
|
array $input): array
|
|
{
|
|
self::assertOwner($instance, $actor);
|
|
CloudInput::only($input, [
|
|
'frequency', 'weekday', 'retention_count',
|
|
]);
|
|
$frequency = $input['frequency'] ?? null;
|
|
if (!is_string($frequency)
|
|
|| !in_array($frequency, ['daily', 'weekly'], true)) {
|
|
throw new CloudError('备份频率仅支持每天或每周');
|
|
}
|
|
$retention = filter_var($input['retention_count'] ?? null,
|
|
FILTER_VALIDATE_INT);
|
|
if ($retention === false || !in_array((int) $retention, [1, 2], true)) {
|
|
throw new CloudError('自动备份最多可保留两份');
|
|
}
|
|
$weekday = $input['weekday'] ?? 0;
|
|
if ($frequency === 'weekly') {
|
|
$weekday = filter_var($weekday, FILTER_VALIDATE_INT);
|
|
if ($weekday === false || (int) $weekday < 0
|
|
|| (int) $weekday > 6) {
|
|
throw new CloudError('每周备份日必须为 0 至 6');
|
|
}
|
|
} elseif (array_key_exists('weekday', $input)) {
|
|
$weekday = filter_var($weekday, FILTER_VALIDATE_INT);
|
|
if ($weekday === false || (int) $weekday < 0
|
|
|| (int) $weekday > 6) {
|
|
throw new CloudError('备份日必须为 0 至 6');
|
|
}
|
|
}
|
|
|
|
$now = time();
|
|
$subscription = $instance->getConnection()->transaction(
|
|
function () use (
|
|
$instance, $frequency, $weekday, $retention, $now
|
|
): CloudBackupSubscription {
|
|
$locked = CloudBackupSubscription::where(
|
|
'instance_id', (int) $instance->id
|
|
)->lockForUpdate()->first();
|
|
if ($locked === null
|
|
|| (int) $locked->user_id !== (int) $instance->user_id) {
|
|
throw new CloudError('自动备份订阅尚未激活,请联系管理员', 409);
|
|
}
|
|
if (!(bool) $locked->enabled
|
|
|| !CloudBackupPeriodEntitlement::where(
|
|
'subscription_id', (int) $locked->id
|
|
)->where('instance_id', (int) $instance->id)
|
|
->where('user_id', (int) $instance->user_id)
|
|
->where('starts_at', '<=', $now)
|
|
->where('ends_at', '>', $now)
|
|
->lockForUpdate()->exists()) {
|
|
throw new CloudError('该实例当前没有已启用的自动备份权益', 409);
|
|
}
|
|
$changedSchedule = (string) $locked->frequency !== $frequency
|
|
|| (int) $locked->weekday !== (int) $weekday;
|
|
$locked->frequency = $frequency;
|
|
$locked->weekday = (int) $weekday;
|
|
$locked->retention_count = (int) $retention;
|
|
$locked->timezone = self::TIMEZONE;
|
|
if ($changedSchedule) {
|
|
$locked->next_run_at = self::nextRunAt(
|
|
$now, $frequency, (int) $weekday
|
|
);
|
|
}
|
|
$locked->updated_at = $now;
|
|
$locked->save();
|
|
return $locked;
|
|
}, 3
|
|
);
|
|
|
|
return [
|
|
'instance_id' => (string) $instance->id,
|
|
'entitled' => true,
|
|
'subscription' => self::displaySubscription($subscription),
|
|
'timezone' => self::TIMEZONE,
|
|
'run_at' => self::RUN_AT,
|
|
'max_retention' => self::MAX_RETENTION,
|
|
'backups' => self::backupsForInstance($instance),
|
|
];
|
|
}
|
|
|
|
public static function library(array $actor, array $input): array
|
|
{
|
|
CloudInput::only($input, ['page', 'per_page']);
|
|
$userId = (int) ($actor['user_id'] ?? 0);
|
|
if ($userId < 1) {
|
|
throw new CloudError('请先登录', 401);
|
|
}
|
|
$page = self::positiveInteger($input['page'] ?? 1, 1, 1000000);
|
|
$perPage = self::positiveInteger($input['per_page'] ?? 20, 1, 50);
|
|
if (!in_array($perPage, [20, 50], true)) {
|
|
throw new CloudError('备份库每页条数仅支持 20 或 50');
|
|
}
|
|
$query = CloudInstanceBackup::where('user_id', $userId)
|
|
->where('purpose', 'portable')->where('source', 'automatic')
|
|
->where('state', '<>', 'deleted');
|
|
$total = (int) (clone $query)->count('id');
|
|
$pages = max(1, (int) ceil($total / $perPage));
|
|
$page = min($page, $pages);
|
|
$rows = $query->orderByDesc('id')
|
|
->offset(($page - 1) * $perPage)->limit($perPage)->get();
|
|
$instances = [];
|
|
$ids = array_values(array_unique(array_map(
|
|
static fn (CloudInstanceBackup $backup): int =>
|
|
(int) $backup->source_instance_id,
|
|
$rows->all()
|
|
)));
|
|
if ($ids !== []) {
|
|
foreach (CloudInstance::whereIn('id', $ids)
|
|
->get(['id', 'name', 'status']) as $instance) {
|
|
$instances[(int) $instance->id] = $instance;
|
|
}
|
|
}
|
|
$items = [];
|
|
foreach ($rows as $backup) {
|
|
$items[] = self::displayBackup(
|
|
$backup,
|
|
$instances[(int) $backup->source_instance_id] ?? null
|
|
);
|
|
}
|
|
$from = $total === 0 ? 0 : (($page - 1) * $perPage) + 1;
|
|
return [
|
|
'items' => $items,
|
|
'pagination' => [
|
|
'page' => $page,
|
|
'per_page' => $perPage,
|
|
'total' => $total,
|
|
'total_pages' => $pages,
|
|
'from' => $from,
|
|
'to' => $from === 0 ? 0 : $from + count($items) - 1,
|
|
],
|
|
];
|
|
}
|
|
|
|
/** Queue at most $limit due backups; one bad subscription cannot starve later rows. */
|
|
public static function scheduleDue(int $limit = 10): int
|
|
{
|
|
$limit = max(1, min(50, $limit));
|
|
$now = time();
|
|
$ids = CloudBackupSubscription::where('enabled', 1)
|
|
->where('next_run_at', '<=', $now)->orderBy('next_run_at')
|
|
->orderBy('id')->limit($limit)->pluck('id')->all();
|
|
$queued = 0;
|
|
foreach ($ids as $id) {
|
|
try {
|
|
if (self::scheduleOne((int) $id, $now)) {
|
|
$queued++;
|
|
}
|
|
} catch (\Throwable $error) {
|
|
CloudBackupSubscription::where('id', (int) $id)
|
|
->where('enabled', 1)
|
|
->where('next_run_at', '<=', $now)
|
|
->update([
|
|
'next_run_at' => $now + self::RETRY_SECONDS,
|
|
'updated_at' => time(),
|
|
]);
|
|
error_log('cloud automatic backup schedule unavailable: '
|
|
. get_class($error));
|
|
}
|
|
}
|
|
return $queued;
|
|
}
|
|
|
|
/** Delete at most $limit oldest ready objects beyond each subscription's cap. */
|
|
public static function pruneRetention(int $limit = 10): int
|
|
{
|
|
$limit = max(1, min(50, $limit));
|
|
$actorFor = static fn (int $userId): array => [
|
|
'user_id' => $userId,
|
|
'is_admin' => true,
|
|
'is_super_admin' => true,
|
|
'can_manage' => true,
|
|
'session_id' => 'automatic-backup-retention',
|
|
];
|
|
$processed = 0;
|
|
$removed = 0;
|
|
// Ambiguous object deletion is reconciled by HEAD only. Keep it in a
|
|
// bounded system path; otherwise it drops out of the ready-count
|
|
// query and can never converge after the first uncertain DELETE.
|
|
$pending = CloudInstanceBackup::where('source', 'automatic')
|
|
->where('purpose', 'portable')
|
|
->whereIn('state', ['deleting', 'delete_uncertain'])
|
|
->orderBy('id')->limit($limit)->get();
|
|
foreach ($pending as $backup) {
|
|
try {
|
|
$result = CloudBackupService::delete(
|
|
$backup, $actorFor((int) $backup->user_id)
|
|
);
|
|
if (($result['deleted'] ?? false) === true) {
|
|
$removed++;
|
|
}
|
|
} catch (\Throwable $error) {
|
|
error_log('cloud automatic backup retention unavailable: '
|
|
. get_class($error));
|
|
}
|
|
$processed++;
|
|
}
|
|
if ($processed >= $limit) {
|
|
return $removed;
|
|
}
|
|
$subscriptions = CloudBackupSubscription::whereRaw(
|
|
'(SELECT COUNT(*) FROM cloud_instance_backups '
|
|
. 'WHERE cloud_instance_backups.subscription_id = '
|
|
. 'cloud_backup_subscriptions.id '
|
|
. "AND cloud_instance_backups.source = 'automatic' "
|
|
. "AND cloud_instance_backups.purpose = 'portable' "
|
|
. "AND cloud_instance_backups.state = 'ready') "
|
|
. '> cloud_backup_subscriptions.retention_count'
|
|
)->orderBy('id')->limit($limit - $processed)->get();
|
|
foreach ($subscriptions as $subscription) {
|
|
if ($processed >= $limit) {
|
|
break;
|
|
}
|
|
$ready = CloudInstanceBackup::where(
|
|
'subscription_id', (int) $subscription->id
|
|
)->where('source', 'automatic')->where('purpose', 'portable')
|
|
->where('state', 'ready')->orderBy('scheduled_for')
|
|
->orderBy('id')->get();
|
|
$excess = max(0, $ready->count()
|
|
- min(self::MAX_RETENTION,
|
|
max(1, (int) $subscription->retention_count)));
|
|
foreach ($ready->take($excess) as $backup) {
|
|
if ($processed >= $limit) {
|
|
break 2;
|
|
}
|
|
try {
|
|
$result = CloudBackupService::delete(
|
|
$backup,
|
|
$actorFor((int) $subscription->user_id)
|
|
);
|
|
if (($result['deleted'] ?? false) === true) {
|
|
$removed++;
|
|
}
|
|
} catch (\Throwable $error) {
|
|
error_log('cloud automatic backup retention unavailable: '
|
|
. get_class($error));
|
|
}
|
|
$processed++;
|
|
}
|
|
}
|
|
return $removed;
|
|
}
|
|
|
|
/** First Pacific midnight strictly after $after. Weekday uses PHP w: 0=Sunday. */
|
|
public static function nextRunAt(int $after, string $frequency,
|
|
int $weekday): int
|
|
{
|
|
if (!in_array($frequency, ['daily', 'weekly'], true)
|
|
|| $weekday < 0 || $weekday > 6) {
|
|
throw new \InvalidArgumentException('Invalid automatic backup schedule');
|
|
}
|
|
$zone = new \DateTimeZone(self::TIMEZONE);
|
|
$local = (new \DateTimeImmutable('@' . max(0, $after)))
|
|
->setTimezone($zone);
|
|
$candidate = $local->setTime(0, 0, 0);
|
|
if ($candidate->getTimestamp() <= $after) {
|
|
$candidate = $candidate->modify('+1 day');
|
|
}
|
|
if ($frequency === 'weekly') {
|
|
while ((int) $candidate->format('w') !== $weekday) {
|
|
$candidate = $candidate->modify('+1 day');
|
|
}
|
|
}
|
|
return $candidate->getTimestamp();
|
|
}
|
|
|
|
public static function advanceAfterScheduled(
|
|
CloudBackupSubscription $subscription,
|
|
int $scheduledFor,
|
|
int $backupId
|
|
): void {
|
|
$subscription->last_scheduled_at = $scheduledFor;
|
|
$subscription->last_backup_id = $backupId;
|
|
$subscription->next_run_at = self::nextRunAt(
|
|
max(time(), $scheduledFor),
|
|
(string) $subscription->frequency,
|
|
(int) $subscription->weekday
|
|
);
|
|
$subscription->updated_at = time();
|
|
$subscription->save();
|
|
}
|
|
|
|
private static function scheduleOne(int $subscriptionId, int $now): bool
|
|
{
|
|
$subscription = CloudBackupSubscription::find($subscriptionId);
|
|
if ($subscription === null || !(bool) $subscription->enabled
|
|
|| (int) $subscription->next_run_at > $now) {
|
|
return false;
|
|
}
|
|
$instance = CloudInstance::find((int) $subscription->instance_id);
|
|
if ($instance === null || (string) $instance->status !== 'active'
|
|
|| (string) $instance->service_status !== 'active'
|
|
|| (int) $instance->user_id !== (int) $subscription->user_id) {
|
|
throw new CloudError('实例当前不可创建自动备份', 409);
|
|
}
|
|
$scheduledFor = (int) $subscription->next_run_at;
|
|
if (!CloudBackupPeriodEntitlement::where(
|
|
'subscription_id', (int) $subscription->id
|
|
)->where('instance_id', (int) $instance->id)
|
|
->where('user_id', (int) $instance->user_id)
|
|
->where('starts_at', '<=', $scheduledFor)
|
|
->where('ends_at', '>', $scheduledFor)->exists()) {
|
|
throw new CloudError('自动备份执行时间不在已付权益周期内', 409);
|
|
}
|
|
$actor = [
|
|
'user_id' => (int) $instance->user_id,
|
|
'is_admin' => false,
|
|
'is_super_admin' => false,
|
|
'can_manage' => true,
|
|
'session_id' => 'automatic-backup-scheduler',
|
|
'request_idempotency_key' => 'automatic-backup-'
|
|
. (int) $subscription->id . '-' . $scheduledFor,
|
|
];
|
|
$queued = CloudBackupService::createAutomatic(
|
|
$instance, $subscription, $actor, $scheduledFor
|
|
);
|
|
return $queued['job'] instanceof CloudJob;
|
|
}
|
|
|
|
private static function backupsForInstance(CloudInstance $instance): array
|
|
{
|
|
$items = [];
|
|
foreach (CloudInstanceBackup::where(
|
|
'source_instance_id', (int) $instance->id
|
|
)->where('user_id', (int) $instance->user_id)
|
|
->where('purpose', 'portable')->where('source', 'automatic')
|
|
->where('state', '<>', 'deleted')->orderByDesc('id')->get()
|
|
as $backup) {
|
|
$items[] = self::displayBackup($backup, $instance);
|
|
}
|
|
return $items;
|
|
}
|
|
|
|
private static function displayBackup(CloudInstanceBackup $backup,
|
|
?CloudInstance $instance): array
|
|
{
|
|
$state = in_array((string) $backup->state, [
|
|
'creating', 'ready', 'failed', 'uncertain', 'deleting',
|
|
'delete_uncertain',
|
|
], true) ? (string) $backup->state : 'uncertain';
|
|
return [
|
|
'id' => (string) $backup->id,
|
|
'name' => (string) $backup->name,
|
|
'state' => $state,
|
|
'consistency' => in_array((string) $backup->consistency,
|
|
['filesystem', 'crash'], true)
|
|
? (string) $backup->consistency : 'unknown',
|
|
'object_size_bytes' => max(0, (int) $backup->object_size_bytes),
|
|
'virtual_size_bytes' => max(0, (int) $backup->virtual_size_bytes),
|
|
'scheduled_for' => $backup->scheduled_for === null ? null
|
|
: max(0, (int) $backup->scheduled_for),
|
|
'created_at' => max(0, (int) $backup->created_at),
|
|
'ready_at' => $backup->ready_at === null ? null
|
|
: max(0, (int) $backup->ready_at),
|
|
'source_instance' => [
|
|
'id' => (string) $backup->source_instance_id,
|
|
'name' => $instance === null ? '' : (string) $instance->name,
|
|
'status' => $instance === null ? 'missing'
|
|
: (string) $instance->status,
|
|
],
|
|
'can_restore_original' => $state === 'ready'
|
|
&& $instance !== null
|
|
&& (string) $instance->status !== 'deleted',
|
|
'can_create_instance' => $state === 'ready',
|
|
];
|
|
}
|
|
|
|
private static function displaySubscription(
|
|
CloudBackupSubscription $subscription
|
|
): array {
|
|
return [
|
|
'enabled' => (bool) $subscription->enabled,
|
|
'renew_next_period' => (bool) $subscription->renew_next_period,
|
|
'frequency' => (string) $subscription->frequency,
|
|
'weekday' => (int) $subscription->weekday,
|
|
'retention_count' => (int) $subscription->retention_count,
|
|
'timezone' => self::TIMEZONE,
|
|
'run_at' => self::RUN_AT,
|
|
'next_run_at' => max(0, (int) $subscription->next_run_at),
|
|
'last_scheduled_at' => $subscription->last_scheduled_at === null
|
|
? null : max(0, (int) $subscription->last_scheduled_at),
|
|
'price' => (string) $subscription->price,
|
|
'currency' => (string) $subscription->currency,
|
|
'price_version' => (int) $subscription->price_version,
|
|
];
|
|
}
|
|
|
|
private static function entitled(
|
|
CloudInstance $instance,
|
|
?CloudBackupSubscription $subscription,
|
|
int $at
|
|
): bool
|
|
{
|
|
if ($subscription === null) {
|
|
return false;
|
|
}
|
|
return CloudBackupPeriodEntitlement::where(
|
|
'subscription_id', (int) $subscription->id
|
|
)->where('instance_id', (int) $instance->id)
|
|
->where('user_id', (int) $instance->user_id)
|
|
->where('starts_at', '<=', $at)
|
|
->where('ends_at', '>', $at)->exists();
|
|
}
|
|
|
|
private static function spec(CloudInstance $instance): array
|
|
{
|
|
$spec = json_decode((string) $instance->spec_json, true);
|
|
if (!is_array($spec) || array_is_list($spec)) {
|
|
throw new CloudError('实例计费快照损坏', 409);
|
|
}
|
|
return $spec;
|
|
}
|
|
|
|
private static function moneySnapshot($value): string
|
|
{
|
|
return CloudMoney::decimal(CloudMoney::cents($value));
|
|
}
|
|
|
|
private static function assertOwner(CloudInstance $instance,
|
|
array $actor): void
|
|
{
|
|
if ((int) ($actor['user_id'] ?? 0) !== (int) $instance->user_id) {
|
|
throw new CloudError('无权查看或修改该实例的自动备份', 403);
|
|
}
|
|
}
|
|
|
|
private static function positiveInteger($value, int $minimum,
|
|
int $maximum): int
|
|
{
|
|
if (is_int($value)) {
|
|
$parsed = $value;
|
|
} elseif (is_string($value)
|
|
&& preg_match('/^[1-9][0-9]{0,9}$/D', $value) === 1) {
|
|
$parsed = (int) $value;
|
|
} else {
|
|
throw new CloudError('备份库分页参数无效');
|
|
}
|
|
if ($parsed < $minimum || $parsed > $maximum) {
|
|
throw new CloudError('备份库分页参数超出范围');
|
|
}
|
|
return $parsed;
|
|
}
|
|
}
|