Beispiel #1
0
	/**
	 * Applies the size and count quotas
	 * @return bool
	 */
	private function get_remote_quotas()
	{
		// Get all records with a remote filename
		$allRecords = AEPlatform::get_valid_remote_records();
		
		// Bail out if no records found
		if(empty($allRecords)) return array();

		// Try to find the files to be deleted due to quota settings
		$statistics =& AEFactory::getStatistics();
		$latestBackupId = $statistics->getId();
		
		// Filter out the current record
		$temp = array();
		foreach($allRecords as $item)
		{
			if($item['id'] == $latestBackupId) continue;
			$item['files'] = $this->get_remote_files($item['remote_filename'], $item['multipart']);
			$temp[] = $item;
		}
		$allRecords = $temp;
		
		// Bail out if only the current backup was included in the list
		if(count($allRecords) == 0) return array();
		
		// Get quota values
		$registry =& AEFactory::getConfiguration();
		$countQuota = $registry->get('akeeba.quota.count_quota');
		$sizeQuota = $registry->get('akeeba.quota.size_quota');
		$useCountQuotas = $registry->get('akeeba.quota.enable_count_quota');
		$useSizeQuotas = $registry->get('akeeba.quota.enable_size_quota');
		$useDayQuotas = $registry->get('akeeba.quota.maxage.enable');
		$daysQuota = $registry->get('akeeba.quota.maxage.maxdays');
		$preserveDay = $registry->get('akeeba.quota.maxage.keepday');

		$leftover = array();
		$ret = array();
		$killids = array();
		
		if($useDayQuotas) {
			$killDatetime = new DateTime();
			$killDatetime->sub(new DateInterval('P'.$daysQuota.'D'));
			$killTS = $killDatetime->format('U');
			
			foreach($allRecords as $def) {
				$backupstart = new DateTime($def['backupstart']);
				$backupTS = $backupstart->format('U');
				$backupDay = $backupstart->format('d');

				// Is this on a preserve day?
				if($preserveDay > 0) {
					if($preserveDay == $backupDay) {
						$leftover[] = $def;
						continue;
					}
				}
				// Otherwise, check the timestamp
				if($backupTS < $killTS) {
					$ret[] = $def['files'];
					$killids[] = $def['id'];
				} else {
					$leftover[] = $def;
				}

			}
		}
		
		// Do we need to apply count quotas?
		if($useCountQuotas && ($countQuota >= 1) && !$useDayQuotas )
		{
			$countQuota--;
			// Are there more files than the quota limit?
			if( !(count($allRecords) > $countQuota) )
			{
				// No, effectively skip the quota checking
				$leftover = $allRecords;
			}
			else
			{
				AEUtilLogger::WriteLog(_AE_LOG_DEBUG, "Processing remote count quotas" );
				// Yes, apply the quota setting.
				$totalRecords = count($allRecords);
				for($count = 0; $count <= $totalRecords; $count++)
				{
					$def = array_pop($allRecords);
					if(count($leftover) >= $countQuota)
					{
						$ret[] = $def['files'];
						$killids[] = $def['id'];
					}
					else
					{
						$leftover[] = $def;
					}
				}
				unset($allRecords);
			}
		}
		else
		{
			// No count quotas are applied
			$leftover = $allRecords;
		}

		// Do we need to apply size quotas?
		if( $useSizeQuotas && ($sizeQuota > 0) && (count($leftover) > 0) && !$useDayQuotas )
		{			
			AEUtilLogger::WriteLog(_AE_LOG_DEBUG, "Processing remote size quotas" );
			// OK, let's start counting bytes!
			$runningSize = 0;
			while(count($leftover) > 0)
			{
				// Each time, remove the last element of the backup array and calculate
				// running size. If it's over the limit, add the archive to the $ret array.
				$def = array_pop($leftover);
				$runningSize += $def['total_size'];
				if($runningSize >= $sizeQuota)
				{
					$ret[] = $def['files'];
					$killids[] = $def['id'];
				}
			}
		}

		// Convert the $ret 2-dimensional array to single dimensional
		$quotaFiles = array();
		foreach($ret as $temp)
		{
			if(!is_array($temp) || empty($temp)) continue;
			foreach($temp as $filename)
			{
				$quotaFiles[] = $filename;
			}
		}
		
		// Update the statistics record with the removed remote files
		if(!empty($killids)) foreach($killids as $id) {
			if(empty($id)) continue;
			$data = array('remote_filename' => '');
			AEPlatform::set_or_update_statistics($id, $data, $this);
		}
		
		return $quotaFiles;
	}