|
1 | 1 | <?php |
2 | 2 | /** |
3 | 3 | * This file is part of Backup plugin for FacturaScripts |
4 | | - * Copyright (C) 2021-2025 Carlos Garcia Gomez <carlos@facturascripts.com> |
| 4 | + * Copyright (C) 2021-2026 Carlos Garcia Gomez <carlos@facturascripts.com> |
5 | 5 | * |
6 | 6 | * This program is free software: you can redistribute it and/or modify |
7 | 7 | * it under the terms of the GNU Lesser General Public License as |
@@ -47,20 +47,20 @@ class Backup extends Controller |
47 | 47 | /** @var array */ |
48 | 48 | public $backup_list = []; |
49 | 49 |
|
50 | | - /** @var string */ |
51 | | - public $current_charset = ''; |
52 | | - |
53 | 50 | /** @var string */ |
54 | 51 | public $cron_frequency = ''; |
55 | 52 |
|
56 | 53 | /** @var int */ |
57 | 54 | public $cron_hour = 3; |
58 | 55 |
|
59 | 56 | /** @var int */ |
60 | | - public $cron_weekly_day = 1; |
| 57 | + public $cron_monthly_day = 1; |
61 | 58 |
|
62 | 59 | /** @var int */ |
63 | | - public $cron_monthly_day = 1; |
| 60 | + public $cron_weekly_day = 1; |
| 61 | + |
| 62 | + /** @var string */ |
| 63 | + public $current_charset = ''; |
64 | 64 |
|
65 | 65 | /** |
66 | 66 | * Return the max file size that can be uploaded. |
@@ -204,6 +204,26 @@ private function checkDbBackupCharset(string $filePath): bool |
204 | 204 | return false; |
205 | 205 | } |
206 | 206 |
|
| 207 | + private function createAdminUserAfterRestore(string $nick, string $password): void |
| 208 | + { |
| 209 | + $user = new User(); |
| 210 | + $user->nick = $nick; |
| 211 | + $user->admin = true; |
| 212 | + $user->enabled = true; |
| 213 | + |
| 214 | + if (false === $user->setPassword($password)) { |
| 215 | + Tools::log()->error('restore-admin-password-weak'); |
| 216 | + return; |
| 217 | + } |
| 218 | + |
| 219 | + if (false === $user->save()) { |
| 220 | + Tools::log()->error('record-save-error'); |
| 221 | + return; |
| 222 | + } |
| 223 | + |
| 224 | + Tools::log()->notice('restore-admin-user-created', ['%nick%' => $nick]); |
| 225 | + } |
| 226 | + |
207 | 227 | protected function createSqlAction(): void |
208 | 228 | { |
209 | 229 | if ($this->permissions->allowExport === false) { |
@@ -429,6 +449,12 @@ protected function loadBackupFiles(): void |
429 | 449 | $grouped = []; |
430 | 450 |
|
431 | 451 | foreach (Tools::folderScan($folder) as $file) { |
| 452 | + // solo procesamos archivos .zip o .sql, el resto los ignoramos |
| 453 | + $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION)); |
| 454 | + if (false === in_array($extension, ['zip', 'sql'], true)) { |
| 455 | + continue; |
| 456 | + } |
| 457 | + |
432 | 458 | // la clave del día es la parte antes del primer guion bajo (YYYYMMDD) |
433 | 459 | $underscorePos = strpos($file, '_'); |
434 | 460 | if (false === $underscorePos) { |
@@ -635,69 +661,6 @@ private function restoreBackupAction(): void |
635 | 661 | $this->redirect('login'); |
636 | 662 | } |
637 | 663 |
|
638 | | - private function createAdminUserAfterRestore(string $nick, string $password): void |
639 | | - { |
640 | | - $user = new User(); |
641 | | - $user->nick = $nick; |
642 | | - $user->admin = true; |
643 | | - $user->enabled = true; |
644 | | - |
645 | | - if (false === $user->setPassword($password)) { |
646 | | - Tools::log()->error('restore-admin-password-weak'); |
647 | | - return; |
648 | | - } |
649 | | - |
650 | | - if (false === $user->save()) { |
651 | | - Tools::log()->error('record-save-error'); |
652 | | - return; |
653 | | - } |
654 | | - |
655 | | - Tools::log()->notice('restore-admin-user-created', ['%nick%' => $nick]); |
656 | | - } |
657 | | - |
658 | | - private function updateAdminPasswordAfterRestore(string $newPassword, bool $disable2fa = false): void |
659 | | - { |
660 | | - // buscamos primero el usuario con nick 'admin' |
661 | | - $user = new User(); |
662 | | - if (false === $user->load('admin')) { |
663 | | - // si no existe, buscamos cualquier usuario con permisos de administrador |
664 | | - $users = $user->all(); |
665 | | - $adminUser = null; |
666 | | - foreach ($users as $u) { |
667 | | - if ($u->admin) { |
668 | | - $adminUser = $u; |
669 | | - break; |
670 | | - } |
671 | | - } |
672 | | - |
673 | | - if (null === $adminUser) { |
674 | | - Tools::log()->warning('restore-no-admin-found'); |
675 | | - return; |
676 | | - } |
677 | | - |
678 | | - $user = $adminUser; |
679 | | - } |
680 | | - |
681 | | - // actualizamos la contraseña |
682 | | - if (false === $user->setPassword($newPassword)) { |
683 | | - Tools::log()->error('restore-admin-password-weak'); |
684 | | - return; |
685 | | - } |
686 | | - |
687 | | - // desactivamos la autenticación en dos pasos si se ha solicitado |
688 | | - if ($disable2fa) { |
689 | | - $user->two_factor_enabled = false; |
690 | | - $user->two_factor_secret_key = null; |
691 | | - } |
692 | | - |
693 | | - if (false === $user->save()) { |
694 | | - Tools::log()->error('record-save-error'); |
695 | | - return; |
696 | | - } |
697 | | - |
698 | | - Tools::log()->notice('restore-admin-password-updated'); |
699 | | - } |
700 | | - |
701 | 664 | private function restoreFilesAction(): void |
702 | 665 | { |
703 | 666 | if (false === $this->validateFormToken()) { |
@@ -821,4 +784,47 @@ private function unzipDatabase(string $gzFilePath): string |
821 | 784 |
|
822 | 785 | return $name; |
823 | 786 | } |
| 787 | + |
| 788 | + private function updateAdminPasswordAfterRestore(string $newPassword, bool $disable2fa = false): void |
| 789 | + { |
| 790 | + // buscamos primero el usuario con nick 'admin' |
| 791 | + $user = new User(); |
| 792 | + if (false === $user->load('admin')) { |
| 793 | + // si no existe, buscamos cualquier usuario con permisos de administrador |
| 794 | + $users = $user->all(); |
| 795 | + $adminUser = null; |
| 796 | + foreach ($users as $u) { |
| 797 | + if ($u->admin) { |
| 798 | + $adminUser = $u; |
| 799 | + break; |
| 800 | + } |
| 801 | + } |
| 802 | + |
| 803 | + if (null === $adminUser) { |
| 804 | + Tools::log()->warning('restore-no-admin-found'); |
| 805 | + return; |
| 806 | + } |
| 807 | + |
| 808 | + $user = $adminUser; |
| 809 | + } |
| 810 | + |
| 811 | + // actualizamos la contraseña |
| 812 | + if (false === $user->setPassword($newPassword)) { |
| 813 | + Tools::log()->error('restore-admin-password-weak'); |
| 814 | + return; |
| 815 | + } |
| 816 | + |
| 817 | + // desactivamos la autenticación en dos pasos si se ha solicitado |
| 818 | + if ($disable2fa) { |
| 819 | + $user->two_factor_enabled = false; |
| 820 | + $user->two_factor_secret_key = null; |
| 821 | + } |
| 822 | + |
| 823 | + if (false === $user->save()) { |
| 824 | + Tools::log()->error('record-save-error'); |
| 825 | + return; |
| 826 | + } |
| 827 | + |
| 828 | + Tools::log()->notice('restore-admin-password-updated'); |
| 829 | + } |
824 | 830 | } |
0 commit comments