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

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