PluginProbe
User Access Manager / 2.3.17
User Access Manager v2.3.17
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 / SetupController.php

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

158 lines 5.0 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;
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 $currentBlogId = $this->database->getCurrentBlogId();
67 $blogIds = ($update === self::UPDATE_NETWORK) ? $this->setupHandler->getBlogIds() : [$currentBlogId];
68
69 foreach ($blogIds as $blogId) {
70 $this->wordpress->switchToBlog($blogId);
71
72 if ($backup === true) {
73 $this->setupHandler->getDatabaseHandler()->backupDatabase();
74 }
75
76 $success = $success && $this->setupHandler->update();
77 $this->wordpress->restoreCurrentBlog();
78 }
79
80 $message = $success === true ? TXT_UAM_UAM_DB_UPDATE_SUCCESS : TXT_UAM_UAM_DB_UPDATE_FAILURE;
81 $this->setUpdateMessage($message);
82 }
83 }
84
85 public function revertDatabaseAction(): void
86 {
87 $this->verifyNonce(self::SETUP_REVERT_NONCE);
88 $version = $this->getRequestParameter('uam_revert_database');
89
90 if ($this->setupHandler->getDatabaseHandler()->revertDatabase($version) === true) {
91 $this->setUpdateMessage(TXT_UAM_REVERT_DATABASE_SUCCESS);
92 }
93 }
94
95 /**
96 * @throws MissingColumnsException
97 */
98 public function isDatabaseBroken(): bool
99 {
100 $information = $this->setupHandler->getDatabaseHandler()->getCorruptedDatabaseInformation();
101
102 $numberOfIssues = array_reduce(
103 $information,
104 function ($carry, $item) {
105 $carry += count($item);
106 return $carry;
107 }
108 );
109
110 return $numberOfIssues > 0;
111 }
112
113 /**
114 * @throws MissingColumnsException
115 */
116 public function repairDatabaseAction(): void
117 {
118 $this->verifyNonce(self::SETUP_REPAIR_NONCE);
119
120 $databaseHandler = $this->setupHandler->getDatabaseHandler();
121 $backups = $databaseHandler->getBackups();
122
123 if (isset($backups[UserAccessManager::DB_VERSION]) === false) {
124 $databaseHandler->backupDatabase();
125 }
126
127 if ($databaseHandler->repairDatabase() === true) {
128 $this->setUpdateMessage(TXT_UAM_REPAIR_DATABASE_SUCCESS);
129 $this->wordpress->updateOption('uam_db_version', UserAccessManager::DB_VERSION);
130 }
131 }
132
133 public function deleteDatabaseBackupAction(): void
134 {
135 $this->verifyNonce(self::SETUP_DELETE_BACKUP_NONCE);
136 $version = (string) $this->getRequestParameter('uam_delete_backup');
137
138 if ($this->setupHandler->getDatabaseHandler()->deleteBackup($version) === true) {
139 $this->setUpdateMessage(TXT_UAM_DELETE_DATABASE_BACKUP_SUCCESS);
140 }
141 }
142
143 /**
144 * @throws MissingColumnsException
145 */
146 public function resetUamAction(): void
147 {
148 $this->verifyNonce(self::SETUP_RESET_NONCE);
149 $reset = $this->getRequestParameter('uam_reset');
150
151 if ($reset === 'reset') {
152 $this->setupHandler->uninstall();
153 $this->setupHandler->install(true);
154 $this->setUpdateMessage(TXT_UAM_UAM_RESET_SUCCESS);
155 }
156 }
157 }
158