HEX
Server: Apache/2.4.67 (Debian)
System: Linux hosting103 6.1.0-49-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.174-1 (2026-05-26) x86_64
User: muzikaecom (1013)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: //usr/local/share/hostingplatform/scripts/__purgeSnapshots
#!/usr/bin/env php
<?php
require_once dirname(__FILE__) . '/Utils.class.php';
require_once("/usr/local/share/hostingplatform/esrpc.php");
if (is_file('/etc/hostingplatform/settings.php')) include '/etc/hostingplatform/settings.php';

$server = new ESRPCConnection();
$worker = new Worker($server->get());
$server->setTarget($worker);
$server->connect();
$server->serve();


class Worker {
	var $api;
	var $snapshots = '/var/local/hostingplatform/snapshots';
	var $skip = array(
		".",
		"..",
		"current"
	);

	const SECOND = 1;
	const MINUTE = 60;
	const HOUR   = 3600;
	const DAY    = 86400;
	const WEEK   = 604800;
	const MONTH  = 2678400;
	const YEAR   = 31536000;

	function __construct($api) {
		global $PURGE_SNAPSHOTS_KEEP;

		$this->api = $api;
		$this->keep = array(
			0               => self::SECOND,
			self::DAY * 3   => self::DAY,
			self::MONTH     => self::WEEK,
			self::YEAR      => self::MONTH,
			self::YEAR * 2  => self::YEAR
		);

		if (isset($PURGE_SNAPSHOTS_KEEP) && is_array($PURGE_SNAPSHOTS_KEEP))
			$this->keep = $PURGE_SNAPSHOTS_KEEP;

		krsort($this->keep, SORT_NUMERIC);
	}

	function run($packagename) {
		// Check hostingpackage existence...
		if (!$this->api->__checkIfHostingPackageExists($packagename))
			throw new Exception(
				"Hostingpackage '$packagename' does not exist!"
			);

		$snapshots  = array();
		$partitions = array();

		$base = $this->snapshots . '/' . $packagename;
		if (!is_dir($base)) return true;
		$items = scandir($base);

		foreach ($items as $item) {
			if (in_array($item, $this->skip))
				continue;

			$t = strptime(
				$item,
				"%Y_%m_%d-%H_%M_%S"
			);

			$time = mktime(
				$t['tm_hour'],
				$t['tm_min'],
				$t['tm_sec'],
				$t['tm_mon'] + 1,
				$t['tm_mday'],
				$t['tm_year'] + 1900
			);

			$snapshots[] = $time;

			$age = time() - $time;

			$period = self::YEAR * 10;

			foreach ($this->keep as $min_age => $period_length) {
				if ($age > $min_age && $period_length < $period) {
					$period = $period_length;
					break;
				}
			}

			$index = (int) floor($age / $period) * $period;

			if (!array_key_exists($index, $partitions) || $time > $partitions[$index])
				$partitions[$index] = $time;
		}
		

		foreach ($snapshots as $snapshot) {
			if (!in_array($snapshot, $partitions))
				Utils::rm($base . '/' . date("Y_m_d-H_i_s", $snapshot));
		}

		return true;
	}
}