SetupController.php
6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
* SetupController.php
*
* The SetupController class file.
*
* PHP versions 5
*
* @author Alexander Schneider <alexanderschneider85@gmail.com>
* @copyright 2008-2017 Alexander Schneider
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
* @version SVN: $id$
* @link http://wordpress.org/extend/plugins/user-access-manager/
*/
declare(strict_types=1);
namespace UserAccessManager\Controller\Backend;
use UserAccessManager\Config\WordpressConfig;
use UserAccessManager\Controller\Controller;
use UserAccessManager\Database\Database;
use UserAccessManager\Setup\Database\MissingColumnsException;
use UserAccessManager\Setup\SetupHandler;
use UserAccessManager\UserAccessManager;
use UserAccessManager\Wrapper\Php;
use UserAccessManager\Wrapper\Wordpress;
/**
* Class SetupController
*
* @package UserAccessManager\Controller
*/
class SetupController extends Controller
{
const SETUP_UPDATE_NONCE = 'uamSetupUpdate';
const SETUP_REVERT_NONCE = 'uamSetupRevert';
const SETUP_REPAIR_NONCE = 'uamSetupRepair';
const SETUP_DELETE_BACKUP_NONCE = 'uamSetupDeleteBackup';
const SETUP_RESET_NONCE = 'uamSetupReset';
const UPDATE_BLOG = 'blog';
const UPDATE_NETWORK = 'network';
/**
* @var string
*/
protected $template = 'AdminSetup.php';
/**
* @var SetupHandler
*/
private $setupHandler;
/**
* @var Database
*/
private $database;
/**
* SetupController constructor.
* @param Php $php
* @param Wordpress $wordpress
* @param WordpressConfig $wordpressConfig
* @param Database $database
* @param SetupHandler $setupHandler
*/
public function __construct(
Php $php,
Wordpress $wordpress,
WordpressConfig $wordpressConfig,
Database $database,
SetupHandler $setupHandler
) {
parent::__construct($php, $wordpress, $wordpressConfig);
$this->database = $database;
$this->setupHandler = $setupHandler;
}
/**
* Returns if a database update is necessary.
* @return bool
*/
public function isDatabaseUpdateNecessary(): bool
{
return $this->setupHandler->getDatabaseHandler()->isDatabaseUpdateNecessary();
}
/**
* Checks if a network update is nessary.
* @return bool
*/
public function showNetworkUpdate(): bool
{
return $this->wordpress->isSuperAdmin() === true
&& defined('MULTISITE') === true && MULTISITE === true
&& defined('WP_ALLOW_MULTISITE') === true && WP_ALLOW_MULTISITE === true;
}
/**
* Returns the existing backups
* @return array
*/
public function getBackups(): array
{
return $this->setupHandler->getDatabaseHandler()->getBackups();
}
/**
* The database update action.
*/
public function updateDatabaseAction()
{
$success = true;
$this->verifyNonce(self::SETUP_UPDATE_NONCE);
$update = $this->getRequestParameter('uam_update_db');
$backup = (bool) $this->getRequestParameter('uam_backup_db', false);
if ($update === self::UPDATE_BLOG || $update === self::UPDATE_NETWORK) {
$currentBlogId = $this->database->getCurrentBlogId();
$blogIds = ($update === self::UPDATE_NETWORK) ? $this->setupHandler->getBlogIds() : [$currentBlogId];
foreach ($blogIds as $blogId) {
$this->wordpress->switchToBlog($blogId);
if ($backup === true) {
$this->setupHandler->getDatabaseHandler()->backupDatabase();
}
$success = $success && $this->setupHandler->update();
$this->wordpress->restoreCurrentBlog();
}
$message = $success === true ? TXT_UAM_UAM_DB_UPDATE_SUCCESS : TXT_UAM_UAM_DB_UPDATE_FAILURE;
$this->setUpdateMessage($message);
}
}
/**
* Reverts the database to the given version.
*/
public function revertDatabaseAction()
{
$this->verifyNonce(self::SETUP_REVERT_NONCE);
$version = $this->getRequestParameter('uam_revert_database');
if ($this->setupHandler->getDatabaseHandler()->revertDatabase($version) === true) {
$this->setUpdateMessage(TXT_UAM_REVERT_DATABASE_SUCCESS);
}
}
/**
* Checks if the database is broken.
* @return bool
* @throws MissingColumnsException
*/
public function isDatabaseBroken(): bool
{
$information = $this->setupHandler->getDatabaseHandler()->getCorruptedDatabaseInformation();
$numberOfIssues = array_reduce(
$information,
function ($carry, $item) {
$carry += count($item);
return $carry;
}
);
return $numberOfIssues > 0;
}
/**
* Repairs the database.
* @throws MissingColumnsException
*/
public function repairDatabaseAction()
{
$this->verifyNonce(self::SETUP_REPAIR_NONCE);
$databaseHandler = $this->setupHandler->getDatabaseHandler();
$backups = $databaseHandler->getBackups();
if (isset($backups[UserAccessManager::DB_VERSION]) === false) {
$databaseHandler->backupDatabase();
}
if ($databaseHandler->repairDatabase() === true) {
$this->setUpdateMessage(TXT_UAM_REPAIR_DATABASE_SUCCESS);
$this->wordpress->updateOption('uam_db_version', UserAccessManager::DB_VERSION);
}
}
/**
* Deletes the given database backup.
*/
public function deleteDatabaseBackupAction()
{
$this->verifyNonce(self::SETUP_DELETE_BACKUP_NONCE);
$version = (string) $this->getRequestParameter('uam_delete_backup');
if ($this->setupHandler->getDatabaseHandler()->deleteBackup($version) === true) {
$this->setUpdateMessage(TXT_UAM_DELETE_DATABASE_BACKUP_SUCCESS);
}
}
/**
* The reset action.
* @throws MissingColumnsException
*/
public function resetUamAction()
{
$this->verifyNonce(self::SETUP_RESET_NONCE);
$reset = $this->getRequestParameter('uam_reset');
if ($reset === 'reset') {
$this->setupHandler->uninstall();
$this->setupHandler->install(true);
$this->setUpdateMessage(TXT_UAM_UAM_RESET_SUCCESS);
}
}
}