| 1 |
<?php |
| 2 |
/** |
| 3 |
* Controller.php |
| 4 |
* |
| 5 |
* The Controller 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\WordpressConfig; |
| 18 |
use UserAccessManager\Controller\Backend\BackendController; |
| 19 |
use UserAccessManager\Wrapper\Php; |
| 20 |
use UserAccessManager\Wrapper\Wordpress; |
| 21 |
|
| 22 |
/** |
| 23 |
* Class Controller |
| 24 |
* |
| 25 |
* @package UserAccessManager\Controller |
| 26 |
*/ |
| 27 |
abstract class Controller |
| 28 |
{ |
| 29 |
use BaseControllerTrait { |
| 30 |
render as traitRender; |
| 31 |
} |
| 32 |
|
| 33 |
const ACTION_PARAMETER = 'uam_action'; |
| 34 |
const ACTION_SUFFIX = 'Action'; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var Php |
| 38 |
*/ |
| 39 |
protected $php; |
| 40 |
|
| 41 |
/** |
| 42 |
* @var WordpressConfig |
| 43 |
*/ |
| 44 |
protected $wordpressConfig; |
| 45 |
|
| 46 |
/** |
| 47 |
* @var Wordpress |
| 48 |
*/ |
| 49 |
protected $wordpress; |
| 50 |
|
| 51 |
/** |
| 52 |
* @var string |
| 53 |
*/ |
| 54 |
protected $updateMessage = null; |
| 55 |
|
| 56 |
/** |
| 57 |
* Controller constructor. |
| 58 |
* |
| 59 |
* @param Php $php |
| 60 |
* @param Wordpress $wordpress |
| 61 |
* @param WordpressConfig $wordpressConfig |
| 62 |
*/ |
| 63 |
public function __construct(Php $php, Wordpress $wordpress, WordpressConfig $wordpressConfig) |
| 64 |
{ |
| 65 |
$this->php = $php; |
| 66 |
$this->wordpress = $wordpress; |
| 67 |
$this->wordpressConfig = $wordpressConfig; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* @return Php |
| 72 |
*/ |
| 73 |
protected function getPhp() |
| 74 |
{ |
| 75 |
return $this->php; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* @return WordpressConfig |
| 80 |
*/ |
| 81 |
protected function getWordpressConfig() |
| 82 |
{ |
| 83 |
return $this->wordpressConfig; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Returns the nonce field. |
| 88 |
* |
| 89 |
* @param string $name |
| 90 |
* |
| 91 |
* @return string |
| 92 |
*/ |
| 93 |
public function createNonceField($name) |
| 94 |
{ |
| 95 |
return $this->wordpress->getNonceField($name, $name.'Nonce'); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Returns the nonce. |
| 100 |
* |
| 101 |
* @param string $name |
| 102 |
* |
| 103 |
* @return string |
| 104 |
*/ |
| 105 |
public function getNonce($name) |
| 106 |
{ |
| 107 |
return $this->wordpress->createNonce($name); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Verifies the nonce and terminates the application if the nonce is wrong. |
| 112 |
* |
| 113 |
* @param string $name |
| 114 |
*/ |
| 115 |
protected function verifyNonce($name) |
| 116 |
{ |
| 117 |
$nonce = $this->getRequestParameter($name.'Nonce'); |
| 118 |
|
| 119 |
if ($this->wordpress->verifyNonce($nonce, $name) === false) { |
| 120 |
$this->wordpress->wpDie(TXT_UAM_NONCE_FAILURE_MESSAGE, TXT_UAM_NONCE_FAILURE_TITLE, ['response' => 401]); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Sets the update message. |
| 126 |
* |
| 127 |
* @param string $message |
| 128 |
*/ |
| 129 |
protected function setUpdateMessage($message) |
| 130 |
{ |
| 131 |
$this->updateMessage = $message; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Adds an error message. |
| 136 |
* |
| 137 |
* @param string $message |
| 138 |
*/ |
| 139 |
protected function addErrorMessage($message) |
| 140 |
{ |
| 141 |
if (isset($_SESSION[BackendController::UAM_ERRORS]) === false) { |
| 142 |
$_SESSION[BackendController::UAM_ERRORS] = []; |
| 143 |
} |
| 144 |
|
| 145 |
$_SESSION[BackendController::UAM_ERRORS][] = $message; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Returns the update message. |
| 150 |
* |
| 151 |
* @return string |
| 152 |
*/ |
| 153 |
public function getUpdateMessage() |
| 154 |
{ |
| 155 |
return $this->updateMessage; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Returns true if a update message is set. |
| 160 |
* |
| 161 |
* @return bool |
| 162 |
*/ |
| 163 |
public function hasUpdateMessage() |
| 164 |
{ |
| 165 |
return $this->updateMessage !== null; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Process the action. |
| 170 |
*/ |
| 171 |
protected function processAction() |
| 172 |
{ |
| 173 |
$postAction = $this->getRequestParameter(self::ACTION_PARAMETER); |
| 174 |
$postActionSplit = explode('_', $postAction); |
| 175 |
$postAction = array_shift($postActionSplit); |
| 176 |
$postAction .= implode('', array_map('ucfirst', $postActionSplit)); |
| 177 |
$actionMethod = $postAction.self::ACTION_SUFFIX; |
| 178 |
|
| 179 |
if (method_exists($this, $actionMethod) === true) { |
| 180 |
$this->{$actionMethod}(); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Renders the given template |
| 186 |
*/ |
| 187 |
public function render() |
| 188 |
{ |
| 189 |
$this->processAction(); |
| 190 |
$this->traitRender(); |
| 191 |
} |
| 192 |
} |
| 193 |
|