PluginProbe
User Access Manager / trunk
User Access Manager vtrunk
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / Controller / Backend / Administration / SetupController.php

SetupController.php in User Access Manager trunk, at src/Controller/Backend/Administration/SetupController.php

153 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace UserAccessManager\Controller\Backend\Administration;
6
7 use UserAccessManager\Config\WordpressConfig;
8 use UserAccessManager\Controller\Controller;
9 use UserAccessManager\Database\Database;
10 use UserAccessManager\Setup\Database\MissingColumnsException;
11 use UserAccessManager\Setup\SetupHandler;
12 use UserAccessManager\UserAccessManager;
13 use UserAccessManager\Wrapper\Php;
14 use UserAccessManager\Wrapper\Wordpress;
15
16 class SetupController extends Controller
17 {
18 public const SETUP_UPDATE_NONCE = 'uamSetupUpdate';
19 public const SETUP_REVERT_NONCE = 'uamSetupRevert';
20 public const SETUP_REPAIR_NONCE = 'uamSetupRepair';
21 public const SETUP_DELETE_BACKUP_NONCE = 'uamSetupDeleteBackup';
22 public const SETUP_RESET_NONCE = 'uamSetupReset';
23 public const UPDATE_BLOG = 'blog';
24 public const UPDATE_NETWORK = 'network';
25
26 protected ?string $template = 'AdminSetup.php';
27
28 public function __construct(
29 Php $php,
30 Wordpress $wordpress,
31 WordpressConfig $wordpressConfig,
32 private Database $database,
33 private SetupHandler $setupHandler
34 ) {
35 parent::__construct($php, $wordpress, $wordpressConfig);
36 }
37
38 /**
39 * @throws MissingColumnsException
40 */
41 public function isDatabaseUpdateNecessary(): bool
42 {
43 return $this->setupHandler->getDatabaseHandler()->isDatabaseUpdateNecessary();
44 }
45
46 public function showNetworkUpdate(): bool
47 {
48 return $this->wordpress->isSuperAdmin() === true
49 && defined('MULTISITE') === true && MULTISITE === true
50 && defined('WP_ALLOW_MULTISITE') === true && WP_ALLOW_MULTISITE === true;
51 }
52
53 public function getBackups(): array
54 {
55 return $this->setupHandler->getDatabaseHandler()->getBackups();
56 }
57
58 public function updateDatabaseAction(): void
59 {
60 $success = true;
61 $this->verifyNonce(self::SETUP_UPDATE_NONCE);
62 $update = $this->getRequestParameter('uam_update_db');
63 $backup = (bool) $this->getRequestParameter('uam_backup_db', false);
64
65 if ($update !== self::UPDATE_BLOG && $update !== self::UPDATE_NETWORK) {
66 return;
67 }
68
69 $currentBlogId = $this->database->getCurrentBlogId();
70 $blogIds = ($update === self::UPDATE_NETWORK) ? $this->setupHandler->getBlogIds() : [$currentBlogId];
71
72 foreach ($blogIds as $blogId) {
73 $this->wordpress->switchToBlog($blogId);
74
75 if ($backup === true) {
76 $this->setupHandler->getDatabaseHandler()->backupDatabase();
77 }
78
79 $success = $success && $this->setupHandler->update();
80 $this->wordpress->restoreCurrentBlog();
81 }
82
83 $this->setUpdateMessage(
84 $success === true ? TXT_UAM_UAM_DB_UPDATE_SUCCESS : TXT_UAM_UAM_DB_UPDATE_FAILURE
85 );
86 }
87
88 public function revertDatabaseAction(): void
89 {
90 $this->verifyNonce(self::SETUP_REVERT_NONCE);
91 $version = $this->getRequestParameter('uam_revert_database');
92
93 if ($this->setupHandler->getDatabaseHandler()->revertDatabase($version) === true) {
94 $this->setUpdateMessage(TXT_UAM_REVERT_DATABASE_SUCCESS);
95 }
96 }
97
98 /**
99 * @throws MissingColumnsException
100 */
101 public function isDatabaseBroken(): bool
102 {
103 $information = $this->setupHandler->getDatabaseHandler()->getCorruptedDatabaseInformation();
104
105 return array_sum(array_map('count', $information)) > 0;
106 }
107
108 /**
109 * @throws MissingColumnsException
110 */
111 public function repairDatabaseAction(): void
112 {
113 $this->verifyNonce(self::SETUP_REPAIR_NONCE);
114
115 $databaseHandler = $this->setupHandler->getDatabaseHandler();
116 $backups = $databaseHandler->getBackups();
117
118 if (isset($backups[UserAccessManager::DB_VERSION]) === false) {
119 $databaseHandler->backupDatabase();
120 }
121
122 if ($databaseHandler->repairDatabase() === true) {
123 $this->setUpdateMessage(TXT_UAM_REPAIR_DATABASE_SUCCESS);
124 $this->wordpress->updateOption('uam_db_version', UserAccessManager::DB_VERSION);
125 }
126 }
127
128 public function deleteDatabaseBackupAction(): void
129 {
130 $this->verifyNonce(self::SETUP_DELETE_BACKUP_NONCE);
131 $version = (string) $this->getRequestParameter('uam_delete_backup');
132
133 if ($this->setupHandler->getDatabaseHandler()->deleteBackup($version) === true) {
134 $this->setUpdateMessage(TXT_UAM_DELETE_DATABASE_BACKUP_SUCCESS);
135 }
136 }
137
138 /**
139 * @throws MissingColumnsException
140 */
141 public function resetUamAction(): void
142 {
143 $this->verifyNonce(self::SETUP_RESET_NONCE);
144 $reset = $this->getRequestParameter('uam_reset');
145
146 if ($reset === 'reset') {
147 $this->setupHandler->uninstall();
148 $this->setupHandler->install(true);
149 $this->setUpdateMessage(TXT_UAM_UAM_RESET_SUCCESS);
150 }
151 }
152 }
153