<?php
/**
 * All-in-One WP Migration
 * Copyright (C) 2014-2018 ServMask Inc.
 *
 * Modified Copyright (C) 2025 Lead Connector
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

if (!defined('ABSPATH')) {
	die('Well Sir You Cannot Jump Here...haha');
}

class Lcm_Export_Database_File
{

	public static function execute($params)
	{

		// Set exclude database
		if (isset($params['options']['no_database'])) {
			return $params;
		}

		$database_bytes_read = 0;

		// Set encrypt password
		$encrypt_password = null;
		if (isset($params['options']['encrypt_backups'], $params['options']['encrypt_password'])) {
			$encrypt_password = $params['options']['encrypt_password'];
		}

		// Set compression type
		$compression_type = null;
		if (isset($params['options']['compression_type'])) {
			$compression_type = $params['options']['compression_type'];
		}

		// Set archive bytes offset
		if (isset($params['archive_bytes_offset'])) {
			$archive_bytes_offset = (int) $params['archive_bytes_offset'];
		} else {
			$archive_bytes_offset = lcm_archive_bytes($params);
		}

		// Set database bytes offset
		if (isset($params['database_bytes_offset'])) {
			$database_bytes_offset = (int) $params['database_bytes_offset'];
		} else {
			$database_bytes_offset = 0;
		}

		// Set database bytes written
		if (isset($params['database_bytes_written'])) {
			$database_bytes_written = (int) $params['database_bytes_written'];
		} else {
			$database_bytes_written = 0;
		}

		// Set database CRC
		if (isset($params['database_crc'])) {
			$database_crc = $params['database_crc'];
		} else {
			$database_crc = null;
		}

		// Get total database size
		if (isset($params['total_database_size'])) {
			$total_database_size = (int) $params['total_database_size'];
		} else {
			$total_database_size = lcm_database_bytes($params);
		}

		// What percent of database have we processed?
		$progress = (int) min(($database_bytes_offset / $total_database_size) * 100, 100);

		// Set progress
		/* translators: Progress. */
		Lcm_Status::info(sprintf(__('Archiving database... %d%% complete', 'lc-migrator'), $progress), Lcm_Stages::$STAGES['ARCHIVING_CONTENT']);

		// Open the archive file for writing
		$archive = new Lcm_Compressor(lcm_archive_path($params), $encrypt_password, $compression_type);

		// Set the file pointer to the one that we have saved
		$archive->set_file_pointer($archive_bytes_offset);

		// Add database.sql to archive
		if ($archive->add_file(lcm_database_path($params), LC_MIGRATOR_DATABASE_BACKUP_FILE, $database_bytes_read, $database_bytes_offset, $database_bytes_written, $database_crc)) {

			// Set progress
			Lcm_Status::info(__('Database archived.', 'lc-migrator'), Lcm_Stages::$STAGES['ARCHIVING_CONTENT']);

			// Unset archive bytes offset
			unset($params['archive_bytes_offset']);

			// Unset database bytes offset
			unset($params['database_bytes_offset']);

			// Unset database bytes written
			unset($params['database_bytes_written']);

			// Unset database CRC
			unset($params['database_crc']);

			// Unset total database size
			unset($params['total_database_size']);

			// Unset completed flag
			unset($params['completed']);

		} else {

			// Get archive bytes offset
			$archive_bytes_offset = $archive->get_file_pointer();

			// What percent of database have we processed?
			$progress = (int) min(($database_bytes_offset / $total_database_size) * 100, 100);

			// Set progress
			/* translators: Progress. */
			Lcm_Status::info(sprintf(__('Archiving database... %d%% complete', 'lc-migrator'), $progress), Lcm_Stages::$STAGES['ARCHIVING_CONTENT']);

			// Set archive bytes offset
			$params['archive_bytes_offset'] = $archive_bytes_offset;

			// Set database bytes offset
			$params['database_bytes_offset'] = $database_bytes_offset;

			// Set database bytes written
			$params['database_bytes_written'] = $database_bytes_written;

			// Set database CRC
			$params['database_crc'] = $database_crc;

			// Set total database size
			$params['total_database_size'] = $total_database_size;

			// Set completed flag
			$params['completed'] = false;
		}

		// Truncate the archive file
		$archive->truncate();

		// Close the archive file
		$archive->close();

		return $params;
	}
}
