Files
MokoSuiteBackup/src/packages/com_mokobackup/src/Controller/BackupsController.php
T

117 lines
3.2 KiB
PHP
Raw Normal View History

<?php
/**
* @package MokoJoomBackup
* @subpackage com_mokobackup
* @author Moko Consulting <hello@mokoconsulting.tech>
* @copyright Copyright (C) 2026 Moko Consulting. All rights reserved.
* @license GNU General Public License version 3 or later; see LICENSE
*/
namespace Joomla\Component\MokoBackup\Administrator\Controller;
defined('_JEXEC') or die;
use Joomla\CMS\MVC\Controller\AdminController;
use Joomla\CMS\Router\Route;
use Joomla\Component\MokoBackup\Administrator\Engine\BackupEngine;
use Joomla\Component\MokoBackup\Administrator\Engine\RestoreEngine;
class BackupsController extends AdminController
{
protected $text_prefix = 'COM_MOKOBACKUP_BACKUPS';
public function getModel($name = 'Backup', $prefix = 'Administrator', $config = ['ignore_request' => true])
{
return parent::getModel($name, $prefix, $config);
}
/**
* Start a new backup using the specified profile.
*
* @return void
*/
public function start(): void
{
$this->checkToken();
$profileId = $this->input->getInt('profile_id', 1);
$description = $this->input->getString('description', '');
$engine = new BackupEngine();
$result = $engine->run($profileId, $description, 'backend');
if ($result['success']) {
$this->setMessage($result['message']);
} else {
$this->setMessage($result['message'], 'error');
}
$this->setRedirect(Route::_('index.php?option=com_mokobackup&view=backups', false));
}
/**
* Download a backup archive.
*
* @return void
*/
public function download(): void
{
$id = $this->input->getInt('id', 0);
$model = $this->getModel('Backup');
$item = $model->getItem($id);
if (!$item || !$item->id || !$item->filesexist || !is_file($item->absolute_path)) {
$this->setMessage('COM_MOKOBACKUP_ERROR_FILE_NOT_FOUND', 'error');
$this->setRedirect(Route::_('index.php?option=com_mokobackup&view=backups', false));
return;
}
$app = $this->app;
$app->clearHeaders();
$app->setHeader('Content-Type', 'application/zip');
$app->setHeader('Content-Disposition', 'attachment; filename="' . basename($item->archivename) . '"');
$app->setHeader('Content-Length', (string) filesize($item->absolute_path));
$app->setHeader('Cache-Control', 'no-cache, must-revalidate');
$app->sendHeaders();
readfile($item->absolute_path);
$app->close();
}
/**
* Restore from a backup record.
*
* @return void
*/
public function restore(): void
{
$this->checkToken();
$id = $this->input->getInt('id', 0);
$restoreFiles = (bool) $this->input->getInt('restore_files', 1);
$restoreDb = (bool) $this->input->getInt('restore_db', 1);
$preserveConfig = (bool) $this->input->getInt('preserve_config', 1);
if (!$id) {
$this->setMessage('COM_MOKOBACKUP_ERROR_NO_RECORD_SELECTED', 'error');
$this->setRedirect(Route::_('index.php?option=com_mokobackup&view=backups', false));
return;
}
$engine = new RestoreEngine();
$result = $engine->restore($id, $restoreFiles, $restoreDb, $preserveConfig);
if ($result['success']) {
$this->setMessage($result['message']);
} else {
$this->setMessage($result['message'], 'error');
}
$this->setRedirect(Route::_('index.php?option=com_mokobackup&view=backups', false));
}
}