| 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\Config; |
| 18 |
use UserAccessManager\Wrapper\Php; |
| 19 |
use UserAccessManager\Wrapper\Wordpress; |
| 20 |
|
| 21 |
/** |
| 22 |
* Class Controller |
| 23 |
* |
| 24 |
* @package UserAccessManager\Controller |
| 25 |
*/ |
| 26 |
abstract class Controller |
| 27 |
{ |
| 28 |
const ACTION_PARAMETER = 'uam_action'; |
| 29 |
const ACTION_SUFFIX = 'Action'; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
protected $template = null; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var Php |
| 38 |
*/ |
| 39 |
protected $php; |
| 40 |
|
| 41 |
/** |
| 42 |
* @var Wordpress |
| 43 |
*/ |
| 44 |
protected $wordpress; |
| 45 |
|
| 46 |
/** |
| 47 |
* @var Config |
| 48 |
*/ |
| 49 |
protected $config; |
| 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 Config $config |
| 62 |
*/ |
| 63 |
public function __construct(Php $php, Wordpress $wordpress, Config $config) |
| 64 |
{ |
| 65 |
$this->php = $php; |
| 66 |
$this->wordpress = $wordpress; |
| 67 |
$this->config = $config; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Returns the request parameter. |
| 72 |
* |
| 73 |
* @param string $name |
| 74 |
* @param mixed $default |
| 75 |
* |
| 76 |
* @return mixed |
| 77 |
*/ |
| 78 |
public function getRequestParameter($name, $default = null) |
| 79 |
{ |
| 80 |
$return = (isset($_POST[$name]) === true) ? $_POST[$name] : null; |
| 81 |
|
| 82 |
if ($return === null) { |
| 83 |
$return = (isset($_GET[$name]) === true) ? $_GET[$name] : $default; |
| 84 |
} |
| 85 |
|
| 86 |
return $return; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Returns the current request url. |
| 91 |
* |
| 92 |
* @return string |
| 93 |
*/ |
| 94 |
public function getRequestUrl() |
| 95 |
{ |
| 96 |
return htmlspecialchars($_SERVER['REQUEST_URI']); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Returns the nonce field. |
| 101 |
* |
| 102 |
* @param string $name |
| 103 |
* |
| 104 |
* @return string |
| 105 |
*/ |
| 106 |
public function createNonceField($name) |
| 107 |
{ |
| 108 |
return $this->wordpress->getNonceField($name, $name.'Nonce'); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Returns the nonce. |
| 113 |
* |
| 114 |
* @param string $name |
| 115 |
* |
| 116 |
* @return string |
| 117 |
*/ |
| 118 |
public function getNonce($name) |
| 119 |
{ |
| 120 |
return $this->wordpress->createNonce($name); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Verifies the nonce and terminates the application if the nonce is wrong. |
| 125 |
* |
| 126 |
* @param string $name |
| 127 |
*/ |
| 128 |
protected function verifyNonce($name) |
| 129 |
{ |
| 130 |
$nonce = $this->getRequestParameter($name.'Nonce'); |
| 131 |
|
| 132 |
if ($this->wordpress->verifyNonce($nonce, $name) === false) { |
| 133 |
$this->wordpress->wpDie(TXT_UAM_NONCE_FAILURE); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Sets the update message. |
| 139 |
* |
| 140 |
* @param $message |
| 141 |
*/ |
| 142 |
protected function setUpdateMessage($message) |
| 143 |
{ |
| 144 |
$this->updateMessage = $message; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Returns the update message. |
| 149 |
* |
| 150 |
* @return string |
| 151 |
*/ |
| 152 |
public function getUpdateMessage() |
| 153 |
{ |
| 154 |
return $this->updateMessage; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Returns true if a update message is set. |
| 159 |
* |
| 160 |
* @return bool |
| 161 |
*/ |
| 162 |
public function hasUpdateMessage() |
| 163 |
{ |
| 164 |
return $this->updateMessage !== null; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Process the action. |
| 169 |
*/ |
| 170 |
protected function processAction() |
| 171 |
{ |
| 172 |
$postAction = $this->getRequestParameter(self::ACTION_PARAMETER); |
| 173 |
$postActionSplit = explode('_', $postAction); |
| 174 |
$postAction = array_shift($postActionSplit); |
| 175 |
$postAction .= implode('', array_map('ucfirst', $postActionSplit)); |
| 176 |
$actionMethod = $postAction.self::ACTION_SUFFIX; |
| 177 |
|
| 178 |
if (method_exists($this, $actionMethod) === true) { |
| 179 |
$this->{$actionMethod}(); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Returns the content of the excluded php file. |
| 185 |
* |
| 186 |
* @param string $fileName The view file name |
| 187 |
* |
| 188 |
* @return string |
| 189 |
*/ |
| 190 |
protected function getIncludeContents($fileName) |
| 191 |
{ |
| 192 |
$contents = ''; |
| 193 |
$realPath = $this->config->getRealPath(); |
| 194 |
$path = [$realPath, 'src', 'UserAccessManager', 'View']; |
| 195 |
$path = implode(DIRECTORY_SEPARATOR, $path).DIRECTORY_SEPARATOR; |
| 196 |
$fileWithPath = $path.$fileName; |
| 197 |
|
| 198 |
if (is_file($fileWithPath) === true) { |
| 199 |
ob_start(); |
| 200 |
$this->php->includeFile($this, $fileWithPath); |
| 201 |
$contents = ob_get_contents(); |
| 202 |
ob_end_clean(); |
| 203 |
} |
| 204 |
|
| 205 |
return $contents; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Renders the given template |
| 210 |
*/ |
| 211 |
public function render() |
| 212 |
{ |
| 213 |
$this->processAction(); |
| 214 |
|
| 215 |
if ($this->template !== null) { |
| 216 |
echo $this->getIncludeContents($this->template); |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|