| 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 |
|
| 121 |
foreach ($blogIds as $blogId) { |
| 122 |
$this->wordpress->switchToBlog($blogId); |
| 123 |
|
| 124 |
if ($backup === true) { |
| 125 |
$this->setupHandler->getDatabaseHandler()->backupDatabase(); |
| 126 |
} |
| 127 |
|
| 128 |
$this->setupHandler->update(); |
| 129 |
$this->wordpress->restoreCurrentBlog(); |
| 130 |
} |
| 131 |
|
| 132 |
$this->setUpdateMessage(TXT_UAM_UAM_DB_UPDATE_SUCCESS); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Reverts the database to the given version. |
| 138 |
*/ |
| 139 |
public function revertDatabaseAction() |
| 140 |
{ |
| 141 |
$this->verifyNonce(self::SETUP_REVERT_NONCE); |
| 142 |
$version = $this->getRequestParameter('uam_revert_database'); |
| 143 |
|
| 144 |
if ($this->setupHandler->getDatabaseHandler()->revertDatabase($version) === true) { |
| 145 |
$this->setUpdateMessage(TXT_UAM_REVERT_DATABASE_SUCCESS); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Checks if the database is broken. |
| 151 |
* |
| 152 |
* @return bool |
| 153 |
*/ |
| 154 |
public function isDatabaseBroken() |
| 155 |
{ |
| 156 |
$information = $this->setupHandler->getDatabaseHandler()->getCorruptedDatabaseInformation(); |
| 157 |
|
| 158 |
$numberOfIssues = array_reduce( |
| 159 |
$information, |
| 160 |
function ($carry, $item) { |
| 161 |
$carry += count($item); |
| 162 |
return $carry; |
| 163 |
} |
| 164 |
); |
| 165 |
|
| 166 |
return $numberOfIssues > 0; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Repairs the database. |
| 171 |
*/ |
| 172 |
public function repairDatabaseAction() |
| 173 |
{ |
| 174 |
$this->verifyNonce(self::SETUP_REPAIR_NONCE); |
| 175 |
|
| 176 |
$databaseHandler = $this->setupHandler->getDatabaseHandler(); |
| 177 |
$backups = $databaseHandler->getBackups(); |
| 178 |
|
| 179 |
if (isset($backups[UserAccessManager::DB_VERSION]) === false) { |
| 180 |
$databaseHandler->backupDatabase(); |
| 181 |
} |
| 182 |
|
| 183 |
if ($databaseHandler->repairDatabase() === true) { |
| 184 |
$this->setUpdateMessage(TXT_UAM_REPAIR_DATABASE_SUCCESS); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Deletes the given database backup. |
| 190 |
*/ |
| 191 |
public function deleteDatabaseBackupAction() |
| 192 |
{ |
| 193 |
$this->verifyNonce(self::SETUP_DELETE_BACKUP_NONCE); |
| 194 |
$version = $this->getRequestParameter('uam_delete_backup'); |
| 195 |
|
| 196 |
if ($this->setupHandler->getDatabaseHandler()->deleteBackup($version) === true) { |
| 197 |
$this->setUpdateMessage(TXT_UAM_DELETE_DATABASE_BACKUP_SUCCESS); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* The reset action. |
| 203 |
*/ |
| 204 |
public function resetUamAction() |
| 205 |
{ |
| 206 |
$this->verifyNonce(self::SETUP_RESET_NONCE); |
| 207 |
$reset = $this->getRequestParameter('uam_reset'); |
| 208 |
|
| 209 |
if ($reset === 'reset') { |
| 210 |
$this->setupHandler->uninstall(); |
| 211 |
$this->setupHandler->install(true); |
| 212 |
$this->setUpdateMessage(TXT_UAM_UAM_RESET_SUCCESS); |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
|