PluginProbe
User Access Manager / 2.1.3
User Access Manager v2.1.3
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 / Setup / SetupHandler.php

SetupHandler.php in User Access Manager 2.1.3, at src/Setup/SetupHandler.php

421 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * SetupHandler.php
4 *
5 * The SetupHandler 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\Setup;
16
17 use UserAccessManager\Config\MainConfig;
18 use UserAccessManager\Database\Database;
19 use UserAccessManager\File\FileHandler;
20 use UserAccessManager\Object\ObjectHandler;
21 use UserAccessManager\Setup\Update\UpdateFactory;
22 use UserAccessManager\Setup\Update\UpdateInterface;
23 use UserAccessManager\UserAccessManager;
24 use UserAccessManager\Wrapper\Wordpress;
25
26 /**
27 * Class SetupHandler
28 *
29 * @package UserAccessManager\SetupHandler
30 */
31 class SetupHandler
32 {
33 /**
34 * @var Wordpress
35 */
36 private $wordpress;
37
38 /**
39 * @var Database
40 */
41 private $database;
42
43 /**
44 * @var ObjectHandler
45 */
46 private $objectHandler;
47
48 /**
49 * @var FileHandler
50 */
51 private $fileHandler;
52
53 /**
54 * @var UpdateFactory
55 */
56 private $updateFactory;
57
58 /**
59 * SetupHandler constructor.
60 *
61 * @param Wordpress $wordpress
62 * @param Database $database
63 * @param ObjectHandler $objectHandler
64 * @param FileHandler $fileHandler
65 * @param UpdateFactory $updateFactory
66 */
67 public function __construct(
68 Wordpress $wordpress,
69 Database $database,
70 ObjectHandler $objectHandler,
71 FileHandler $fileHandler,
72 UpdateFactory $updateFactory
73 ) {
74 $this->wordpress = $wordpress;
75 $this->database = $database;
76 $this->objectHandler = $objectHandler;
77 $this->fileHandler = $fileHandler;
78 $this->updateFactory = $updateFactory;
79 }
80
81 /**
82 * Returns all blog of the network.
83 *
84 * @return integer[]
85 */
86 public function getBlogIds()
87 {
88 $currentBlogId = $this->database->getCurrentBlogId();
89 $blogIds = [$currentBlogId => $currentBlogId];
90 $sites = $this->wordpress->getSites();
91
92 foreach ($sites as $site) {
93 $blogIds[$site->blog_id] = $site->blog_id;
94 }
95
96 return $blogIds;
97 }
98
99 /**
100 * Installs the user access manager.
101 *
102 * @param bool $networkWide
103 */
104 public function install($networkWide = false)
105 {
106 if ($networkWide === true) {
107 $blogIds = $this->getBlogIds();
108 $currentBlogId = $this->database->getCurrentBlogId();
109
110 foreach ($blogIds as $blogId) {
111 $this->wordpress->switchToBlog($blogId);
112 $this->runInstall();
113 }
114
115 $this->wordpress->switchToBlog($currentBlogId);
116 } else {
117 $this->runInstall();
118 }
119 }
120
121 /**
122 * Creates the needed tables at the database and adds the options
123 */
124 private function runInstall()
125 {
126 $charsetCollate = $this->database->getCharset();
127 $dbAccessGroupTable = $this->database->getUserGroupTable();
128
129 $dbUserGroup = $this->database->getVariable(
130 "SHOW TABLES
131 LIKE '{$dbAccessGroupTable}'"
132 );
133
134 if ($dbUserGroup !== $dbAccessGroupTable) {
135 $this->database->dbDelta(
136 "CREATE TABLE {$dbAccessGroupTable} (
137 ID INT(11) NOT NULL AUTO_INCREMENT,
138 groupname TINYTEXT NOT NULL,
139 groupdesc TEXT NOT NULL,
140 read_access TINYTEXT NOT NULL,
141 write_access TINYTEXT NOT NULL,
142 ip_range MEDIUMTEXT NULL,
143 PRIMARY KEY (ID)
144 ) {$charsetCollate};"
145 );
146 }
147
148 $dbAccessGroupToObjectTable = $this->database->getUserGroupToObjectTable();
149
150 $dbAccessGroupToObject = (string)$this->database->getVariable(
151 "SHOW TABLES
152 LIKE '".$dbAccessGroupToObjectTable."'"
153 );
154
155 if ($dbAccessGroupToObject !== $dbAccessGroupToObjectTable) {
156 $this->database->dbDelta(
157 "CREATE TABLE {$dbAccessGroupToObjectTable} (
158 object_id VARCHAR(32) NOT NULL,
159 general_object_type VARCHAR(64) NOT NULL,
160 object_type VARCHAR(32) NOT NULL,
161 group_id VARCHAR(32) NOT NULL,
162 group_type VARCHAR(32) NOT NULL,
163 from_date DATETIME NULL DEFAULT NULL,
164 to_date DATETIME NULL DEFAULT NULL,
165 PRIMARY KEY (object_id, object_type, group_id, group_type)
166 ) {$charsetCollate};"
167 );
168 }
169
170 $this->wordpress->addOption('uam_db_version', UserAccessManager::DB_VERSION);
171 }
172
173 /**
174 * Checks if a database update is necessary.
175 *
176 * @return bool
177 */
178 public function isDatabaseUpdateNecessary()
179 {
180 $blogIds = $this->getBlogIds();
181
182 if ($this->wordpress->isSuperAdmin() === true) {
183 foreach ($blogIds as $blogId) {
184 $table = $this->database->getBlogPrefix($blogId).'options';
185 $select = "SELECT option_value FROM {$table} WHERE option_name = '%s' LIMIT 1";
186 $select = $this->database->prepare($select, 'uam_db_version');
187 $currentDbVersion = $this->database->getVariable($select);
188
189 if ($currentDbVersion !== null
190 && version_compare($currentDbVersion, UserAccessManager::DB_VERSION, '<') === true
191 ) {
192 return true;
193 }
194 }
195 }
196
197 $currentDbVersion = $this->wordpress->getOption('uam_db_version');
198 return version_compare($currentDbVersion, UserAccessManager::DB_VERSION, '<');
199 }
200
201 /**
202 * Creates a database backup.
203 *
204 * @return bool
205 */
206 public function backupDatabase()
207 {
208 $currentDbVersion = $this->wordpress->getOption('uam_db_version');
209
210 if (empty($currentDbVersion) === true
211 || version_compare($currentDbVersion, '1.2', '<') === true
212 ) {
213 return false;
214 }
215
216 $tables = [
217 $this->database->getUserGroupTable(),
218 $this->database->getUserGroupToObjectTable()
219 ];
220
221 $currentDbVersion = str_replace('.', '-', $currentDbVersion);
222 $success = true;
223
224 foreach ($tables as $table) {
225 $createQuery = "CREATE TABLE `{$table}_{$currentDbVersion}` LIKE `{$table}`";
226 $success = $success && ($this->database->query($createQuery) !== false);
227 $insertQuery = "INSERT `{$table}_{$currentDbVersion}` SELECT * FROM `{$table}`";
228 $success = $success && ($this->database->query($insertQuery) !== false);
229 }
230
231 return $success;
232 }
233
234 /**
235 * Returns the version for which a backup was created.
236 *
237 * @return array
238 */
239 public function getBackups()
240 {
241 $versions = [];
242 $tables = (array)$this->database->getColumn(
243 "SHOW TABLES LIKE '{$this->database->getPrefix()}uam_%'"
244 );
245
246 foreach ($tables as $table) {
247 if (preg_match('/.*\_([0-9\-]+)/i', $table, $matches) === 1) {
248 $version = str_replace('-', '.', $matches[1]);
249 $versions[$version] = $version;
250 }
251 }
252
253 return $versions;
254 }
255
256 /**
257 * Returns the backup tables for the given version.
258 *
259 * @param string $version
260 *
261 * @return array
262 */
263 private function getBackupTables($version)
264 {
265 $backupTables = [];
266 $tables = [
267 $this->database->getUserGroupTable(),
268 $this->database->getUserGroupToObjectTable()
269 ];
270
271 $versionForDb = str_replace('.', '-', $version);
272
273 foreach ($tables as $table) {
274 $backupTable = (string)$this->database->getVariable(
275 "SHOW TABLES LIKE '{$table}_{$versionForDb}'"
276 );
277
278 if ($backupTable !== '') {
279 $backupTables[$table] = $backupTable;
280 }
281 }
282
283 return $backupTables;
284 }
285
286 /**
287 * Reverts the database to the given version.
288 *
289 * @param string $version
290 *
291 * @return bool
292 */
293 public function revertDatabase($version)
294 {
295 $success = true;
296 $tables = $this->getBackupTables($version);
297
298 foreach ($tables as $table => $backupTable) {
299 $dropQuery = "DROP TABLE IF EXISTS `{$table}`";
300 $success = $success && ($this->database->query($dropQuery) !== false);
301 $renameQuery = "RENAME TABLE `{$backupTable}` TO `{$table}`";
302 $success = $success && ($this->database->query($renameQuery) !== false);
303 }
304
305 if ($success === true) {
306 $this->wordpress->updateOption('uam_db_version', $version);
307 }
308
309 return $success;
310 }
311
312 /**
313 * Deletes the given database backup.
314 *
315 * @param string $version
316 *
317 * @return bool
318 */
319 public function deleteBackup($version)
320 {
321 $success = true;
322 $tables = $this->getBackupTables($version);
323
324 foreach ($tables as $table => $backupTable) {
325 $dropQuery = "DROP TABLE IF EXISTS `{$backupTable}`";
326 $success = $success && ($this->database->query($dropQuery) !== false);
327 }
328
329 return $success;
330 }
331
332 /**
333 * Returns the ordered updates.
334 *
335 * @return UpdateInterface[]
336 */
337 private function getOrderedUpdates()
338 {
339 $rawUpdates = $this->updateFactory->getUpdates();
340 $updates = [];
341
342 foreach ($rawUpdates as $rawUpdate) {
343 $updates[$rawUpdate->getVersion()] = $rawUpdate;
344 }
345
346 uksort($updates, 'version_compare');
347 return $updates;
348 }
349
350 /**
351 * Updates the user access manager if an old version was installed.
352 *
353 * @return bool
354 */
355 public function update()
356 {
357 $currentDbVersion = $this->wordpress->getOption('uam_db_version');
358
359 if (empty($currentDbVersion) === true) {
360 return false;
361 }
362
363 $uamVersion = $this->wordpress->getOption('uam_version', '0');
364
365 if (version_compare($uamVersion, '1.0', '<') === true) {
366 $this->wordpress->deleteOption('allow_comments_locked');
367 }
368
369 $success = true;
370
371 if (version_compare($currentDbVersion, UserAccessManager::DB_VERSION, '<') === true) {
372 foreach ($this->getOrderedUpdates() as $orderedUpdate) {
373 if (version_compare($currentDbVersion, $orderedUpdate->getVersion(), '<=') === true) {
374 $success = $success && $orderedUpdate->update();
375 }
376 }
377
378 if ($success === true) {
379 $this->wordpress->updateOption('uam_db_version', UserAccessManager::DB_VERSION);
380 }
381 }
382
383 return $success;
384 }
385
386 /**
387 * Clean up wordpress if the plugin will be uninstalled.
388 */
389 public function uninstall()
390 {
391 $currentBlogId = $this->database->getCurrentBlogId();
392 $blogIds = $this->getBlogIds();
393
394 foreach ($blogIds as $blogId) {
395 $this->wordpress->switchToBlog($blogId);
396 $userGroupTable = $this->database->getUserGroupTable();
397 $userGroupToObjectTable = $this->database->getUserGroupToObjectTable();
398
399 $dropQuery = "DROP TABLE IF EXISTS {$userGroupTable}, {$userGroupToObjectTable}";
400 $this->database->query($dropQuery);
401
402 $this->wordpress->deleteOption(MainConfig::MAIN_CONFIG_KEY);
403 $this->wordpress->deleteOption('uam_version');
404 $this->wordpress->deleteOption('uam_db_version');
405 }
406
407 $this->wordpress->switchToBlog($currentBlogId);
408 $this->fileHandler->deleteFileProtection();
409 }
410
411 /**
412 * Remove the htaccess file if the plugin is deactivated.
413 *
414 * @return bool
415 */
416 public function deactivate()
417 {
418 return $this->fileHandler->deleteFileProtection();
419 }
420 }
421