PluginProbe
User Access Manager / 2.2.6
User Access Manager v2.2.6
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.2.6, at src/Controller/Backend/SetupController.php

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