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