_* */ protected $flags = []; /** @var array Persisted telemetry for the current build or manual transfer operation */ protected array $telemetryOperation = []; /** @var bool */ protected $flagUpdatedAfterLoad = true; /** @var ?PackageExecutionInfo Classification of the Backup creation context; null only on legacy rows */ protected ?PackageExecutionInfo $execInfo = null; /** @var ?PackageBuildOptions Immutable build configuration, frozen when the backup starts, null before */ protected ?PackageBuildOptions $buildOptions = null; /** @var ?PackageEnvironmentSnapshot Active WordPress environment captured during the build */ protected ?PackageEnvironmentSnapshot $environmentSnapshot = null; /** * Class contructor * The constructor is final to prevent PHP stan error * Unsafe usage of new static(). See: https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static * For now I have solved it this way but if in the future it is necessary to expand the builders there are other ways to handle this * * @param int[] $storageIds Storages id * @param ?TemplateEntity $template Template for Backup or null * @param ?PackageExecutionInfo $execInfo Classification of the Backup creation context */ final public function __construct( $storageIds = [], ?TemplateEntity $template = null, ?PackageExecutionInfo $execInfo = null ) { global $wp_version; $this->VersionOS = defined('PHP_OS') ? PHP_OS : 'unknown'; $this->VersionWP = $wp_version; $this->VersionPHP = phpversion(); $dbversion = WpDbUtils::getVersion(); $this->VersionDB = (empty($dbversion) ? '- unknown -' : $dbversion); $timestamp = time(); $this->created = gmdate("Y-m-d H:i:s", $timestamp); $this->name = $this->getNameFromFormat($template, $timestamp); $this->hash = $this->makeHash(); $this->components = BuildComponents::COMPONENTS_DEFAULT; $this->Database = new DatabasePkg($this); $this->Archive = new PackageArchive($this); $this->Multisite = new PackMultisite(); $this->Installer = new PackInstaller($this); $this->build_progress = new BuildProgress(); $this->db_build_progress = new DbBuildProgress(); $this->setByTemplate($template); if (empty($storageIds)) { $storageIds = [StoragesUtil::getDefaultStorageId()]; } $this->addUploadInfos($storageIds); $this->execInfo = $execInfo ?? new PackageExecutionInfo(static::DEFAULT_EXECUTION_TYPE); $this->updatePackageFlags(); $this->switchStateTimer(null, self::STATUS_PRE_PROCESS); } /** * Clone * * @return void */ public function __clone() { $this->Database = clone $this->Database; $this->Archive = clone $this->Archive; $this->Multisite = clone $this->Multisite; $this->Installer = clone $this->Installer; $this->build_progress = clone $this->build_progress; $this->db_build_progress = clone $this->db_build_progress; if ($this->buildOptions !== null) { $this->buildOptions = clone $this->buildOptions; } $cloneInfo = []; foreach ($this->upload_infos as $key => $obj) { $cloneInfo[$key] = clone $obj; } $this->upload_infos = $cloneInfo; } /** * Save the active WordPress environment captured during the build. * * @param PackageEnvironmentSnapshot $snapshot Environment snapshot * * @return void */ public function setEnvironmentSnapshot(PackageEnvironmentSnapshot $snapshot): void { $this->environmentSnapshot = $snapshot; } /** * Active WordPress environment captured during the build. * * @return ?PackageEnvironmentSnapshot */ public function getEnvironmentSnapshot(): ?PackageEnvironmentSnapshot { return $this->environmentSnapshot; } /** * Get package id * * @return int */ public function getId(): int { return $this->ID; } /** * Get package status * * @return int self::STATUS_* enum */ public function getStatus(): int { return $this->status; } /** * Check whether a status represents a cancellation (user-initiated or an * automatic skip) rather than a failure. * * @param int $status self::STATUS_* enum * * @return bool */ public static function isCancellationStatus(int $status): bool { return in_array( $status, [ self::STATUS_STORAGE_CANCELLED, self::STATUS_PENDING_CANCEL, self::STATUS_BUILD_CANCELLED, ], true ); } /** @return array */ public function getTelemetryOperation(): array { return $this->telemetryOperation; } /** * @param array $operation Current telemetry operation * * @return void */ public function setTelemetryOperation(array $operation): void { $this->telemetryOperation = $operation; } /** * Return Backup flags * * @return string[] ENUM self::FLAG_* */ protected function getFlags() { if ($this->flagUpdatedAfterLoad == false) { $this->updatePackageFlags(); $this->flagUpdatedAfterLoad = true; } return $this->flags; } /** * Add a flag to this package * * @param string $flag Flag value * * @return bool True if flag was added, false if already set */ public function addFlag(string $flag): bool { if (in_array($flag, $this->flags)) { return false; } $this->flags[] = $flag; return true; } /** * Check if package have flag * * Checks against the loaded flags array — works for both registered * and unregistered flags (preserving flags from disabled addons). * * @param string $flag Flag value to check * * @return bool */ public function hasFlag($flag) { return in_array($flag, $this->getFlags()); } /** * Whether the Backup can be deployed through automatic Restore flows. * * @return bool */ public function isDeployable(): bool { return !$this->hasFlag(self::FLAG_NON_DEPLOYABLE); } /** * Update the Backup migration flag * * @return void */ public function updateMigrateAfterInstallFlag(): void { $this->updatePackageFlags(); $this->flags = array_diff( $this->flags, [self::FLAG_CREATED_AFTER_RESTORE] ); $data = MigrationMng::getMigrationData(); // check if package id is set for old versions before 4.5.14 if ($data->restoreBackupMode && $data->packageId > 0) { $installTime = strtotime($data->installTime); $created = strtotime($this->created); if ( $this->getId() > $data->packageId && // If Backup is create after installer Backup $created < $installTime // But berore the installer time ) { $this->flags[] = self::FLAG_CREATED_AFTER_RESTORE; } } $this->flags = array_values($this->flags); } /** * Returns true if this is a DB only Backup * * @return bool */ public function isDBOnly() { return BuildComponents::isDBOnly($this->components) || $this->Archive->ExportOnlyDB; } /** * Returns true if this is a File only Backup * * @return bool */ public function isDBExcluded() { return BuildComponents::isDBExcluded($this->components); } /** * Sets the status to log the state of the build and save in database * * @param int $status The status self::STATUS_* enum * * @return void */ final public function setStatus(int $status): void { if ( $status < self::STATUS_REQUIREMENTS_FAILED || $status > self::STATUS_COMPLETE ) { throw new DupliException("Package SetStatus did not receive a proper code."); } $previousStatus = $this->status; $hasChanged = ($previousStatus != $status); if ($hasChanged) { // Execute hooks only if status has changed do_action('duplicator_package_before_set_status', $this, $status); $this->switchStateTimer($previousStatus, $status); $this->status = $status; } $this->update(); // Always update Backup if ($hasChanged) { do_action('duplicator_package_after_set_status', $this, $status); // Add log event after update only if status has changed $this->addLogEvent($previousStatus); } } /** * Close the timer of the previous status (if any) and open the timer for the new status. * * A single microtime() is used for both the previous end and the new start so * durations are contiguous with no gap between consecutive states. * Terminal statuses (COMPLETE or any failure/cancel state) are recorded with * start == end since no time is spent in them. * * @param ?int $previousStatus self::STATUS_* enum, or null on initial registration * @param int $newStatus self::STATUS_* enum * * @return void */ private function switchStateTimer(?int $previousStatus, int $newStatus): void { $now = microtime(true); if ($previousStatus !== null && isset($this->state_times[$previousStatus])) { $this->state_times[$previousStatus]['end'] = $now; } $isTerminal = ($newStatus === self::STATUS_COMPLETE || $newStatus < self::STATUS_PRE_PROCESS); $this->state_times[$newStatus] = [ 'start' => $now, 'end' => $isTerminal ? $now : 0.0, ]; } /** * Restart the timer of the given status as if it had just been entered. * Used after a runner blackout (e.g. maintenance mode) so the paused time * is not counted by the stuck watchdog. * * @param int $status self::STATUS_* enum * * @return void */ public function restartStateTimer(int $status): void { if (!isset($this->state_times[$status])) { return; } $this->state_times[$status] = [ 'start' => microtime(true), 'end' => 0.0, ]; } /** * Get how long the package has spent in the given status, in seconds * * - Returns -1 if no start time was recorded for the status. * - If start is recorded but end is not, returns elapsed seconds since start (ongoing). * - If both are recorded, returns end - start. * * @param int $status self::STATUS_* enum * * @return float Duration in seconds, or -1 if no start time recorded */ public function getStateDuration(int $status): float { if (!isset($this->state_times[$status]) || $this->state_times[$status]['start'] <= 0) { return -1; } $start = $this->state_times[$status]['start']; $end = $this->state_times[$status]['end']; return $end <= 0 ? microtime(true) - $start : $end - $start; } /** * Get the total time spent in the statuses of the given inclusive range, in seconds. * Statuses without a recorded timing are skipped; -1 when none of them was recorded. * * @param int $fromStatus self::STATUS_* enum, lower bound * @param int $toStatus self::STATUS_* enum, upper bound * * @return float Duration in seconds, or -1 if no status of the range was recorded */ public function getStatesRangeDuration(int $fromStatus, int $toStatus): float { return self::statesRangeDurationFromTimes($this->state_times, $fromStatus, $toStatus); } /** * Get the time spent in a build phase (enum self::PHASE_*), in seconds, * -1 when no status of the phase range was recorded * * @param string $phase Build phase, enum self::PHASE_* * * @return float Duration in seconds, or -1 if the phase was never entered */ public function getPhaseDuration(string $phase): float { return self::phaseDurationFromTimes($this->state_times, $phase); } /** * Compute a build phase duration from a raw state-times array (enum self::PHASE_*). * Shared by the package instance and by the activity logs, which persist * their own state-times snapshot so they survive the package deletion. * * @param array $stateTimes State times, as stored in state_times * @param string $phase Build phase, enum self::PHASE_* * * @return float Duration in seconds, or -1 if the phase was never entered */ public static function phaseDurationFromTimes(array $stateTimes, string $phase): float { if (!isset(self::BUILD_PHASE_RANGES[$phase])) { return -1; } $range = self::BUILD_PHASE_RANGES[$phase]; return self::statesRangeDurationFromTimes($stateTimes, $range[0], $range[1]); } /** * Sum the time spent in the statuses of the given inclusive range from a * raw state-times array; ongoing statuses count up to now, unrecorded * statuses are skipped, -1 when none of the range was recorded * * @param array $stateTimes State times, as stored in state_times * @param int $fromStatus self::STATUS_* enum, lower bound * @param int $toStatus self::STATUS_* enum, upper bound * * @return float Duration in seconds, or -1 if no status of the range was recorded */ public static function statesRangeDurationFromTimes(array $stateTimes, int $fromStatus, int $toStatus): float { $total = 0.0; $recorded = false; foreach ($stateTimes as $status => $times) { $status = (int) $status; if ($status < $fromStatus || $status > $toStatus) { continue; } $start = (float) ($times['start'] ?? 0); $end = (float) ($times['end'] ?? 0); if ($start <= 0) { continue; } $recorded = true; $total += ($end <= 0 ? microtime(true) - $start : $end - $start); } return $recorded ? $total : -1; } /** * Get a copy of the per-status timers * * @return array */ public function getStateTimes(): array { return $this->state_times; } /** * Get name * * @return string */ public function getName(): string { return $this->name; } /** * Get hash * * @return string */ public function getHash(): string { return $this->hash; } /** * Get the backup's descriptor manager * * @return InstallerDescriptors The descriptor manager */ public function getDescriptorMng() { if (!$this->descriptorsMng instanceof InstallerDescriptors) { $this->descriptorsMng = new InstallerDescriptors( $this->getPrimaryInternalHash(), date(self::PACKAGE_HASH_DATE_FORMAT, strtotime($this->created)) ); } return $this->descriptorsMng; } /** * Get version of Backups stored in DB * * @return string */ public function getVersion(): string { return $this->version; } /** * How the Backup creation is classified (e.g. manual or an * addon-injected type), as resolved at creation * * @return string */ public function getExecutionType(): string { return $this->getExecutionInfo()->getType(); } /** * Detail about the creation context (e.g. the schedule name) * * @return ?string */ public function getExecutionSource(): ?string { return $this->getExecutionInfo()->getSource(); } /** * Free-form note on why the backup was created, as resolved at creation * * @return ?string */ public function getExecutionReason(): ?string { return $this->getExecutionInfo()->getReason(); } /** * Complete execution metadata. The fallback initializes packages stored * before execution metadata was introduced. * * @return PackageExecutionInfo */ public function getExecutionInfo(): PackageExecutionInfo { if ($this->execInfo === null) { $this->execInfo = new PackageExecutionInfo(static::DEFAULT_EXECUTION_TYPE); } return $this->execInfo; } /** * The immutable build configuration, or null when the backup hasn't * started yet (see freezeBuildOptions()) * * @return ?PackageBuildOptions */ public function getBuildOptions(): ?PackageBuildOptions { return $this->buildOptions; } /** * The immutable build configuration, the single source of truth of the * build. Callable only after the backup started: a build flow reaching * this point without the gate having frozen the options is a bug. * * @return PackageBuildOptions */ public function requireBuildOptions(): PackageBuildOptions { if ($this->buildOptions === null) { throw new DupliException( 'Package build options not frozen: the pre-backup gate did not run for this package.', DupliException::CODE_ERROR, __('The backup could not start because its configuration was not initialized. Please try again.', 'duplicator') ); } return $this->buildOptions; } /** * Freeze the resolved build configuration into the package. * * Called by OptionsManager::validatePackageConfig() on the one-time gate * pass that runs when the backup starts. From this moment the frozen * options are the single source of truth of the build: the dependent * state set provisionally at construction (archive format and file name, * database mode and its descriptor copy, zip thread mode, baked build progress * engine) is aligned * here and later changes to the global settings never affect the build. * A package already frozen is left untouched. * * @param PackageBuildOptions $buildOptions Resolved build configuration * * @return void */ public function freezeBuildOptions(PackageBuildOptions $buildOptions): void { if ($this->buildOptions !== null) { return; } $this->buildOptions = $buildOptions; $this->Archive->setFormatFromEngine($this->buildOptions->getArchiveEngine()); $this->Database->DBMode = $this->buildOptions->getDbBuildMode(); $this->Database->info->buildMode = $this->buildOptions->getDbBuildMode(); $this->ziparchive_mode = $this->buildOptions->getZipArchiveMode(); $this->build_progress->setBuildMode( $this->buildOptions->getArchiveEngine(), $this->buildOptions->isCompressionEnabled() ); } /** * Validates the inputs from the UI for correct data input * * @return InputValidator */ public function validateInputs() { $validator = new InputValidator(); if ($this->Archive->FilterOn) { $validator->explodeFilterCustom( $this->Archive->FilterDirs, ';', InputValidator::FILTER_VALIDATE_FOLDER_WITH_COMMENT, [ 'valkey' => 'FilterDirs', 'errmsg' => __( 'Directory: %1$s is an invalid path. Please remove the value from the Archive > Files Tab > Folders input box and apply only valid paths.', 'duplicator' ), ] ); $validator->explodeFilterCustom( $this->Archive->FilterExts, ';', InputValidator::FILTER_VALIDATE_FILE_EXT, [ 'valkey' => 'FilterExts', 'errmsg' => __( 'File extension: %1$s is an invalid extension name. Please remove the value from the Archive > Files Tab > File Extensions input box and apply only valid extensions. For example \'jpg\'', 'duplicator' ), ] ); $validator->explodeFilterCustom( $this->Archive->FilterFiles, ';', InputValidator::FILTER_VALIDATE_FILE_WITH_COMMENT, [ 'valkey' => 'FilterFiles', 'errmsg' => __( 'File: %1$s is an invalid file name. Please remove the value from the Archive > Files Tab > Files input box and apply only valid file names.', 'duplicator' ), ] ); } $validator->filterVar($this->Installer->OptsDBHost, FILTER_VALIDATE_DOMAIN, [ 'valkey' => 'OptsDBHost', 'errmsg' => __('MySQL Server Host: %1$s isn\'t a valid host', 'duplicator'), 'acc_vals' => [ '', 'localhost', ], ]); return $validator; } /** * Get created date * * @return string */ public function getCreated(): string { return $this->created; } /** * Get whether this backup uses client-side kickoff. * * @return bool True if client-side kickoff is needed, false if server can self-request */ public function isClientSideKickoff(): bool { return ClientSideKick::isClientSideKickoffMode(); } }