| 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 |
* Sanitize the given value. |
| 72 |
* |
| 73 |
* @param mixed $value |
| 74 |
* |
| 75 |
* @return mixed |
| 76 |
*/ |
| 77 |
private function sanitizeValue($value) |
| 78 |
{ |
| 79 |
if (is_object($value) === true) { |
| 80 |
return $value; |
| 81 |
} elseif (is_array($value) === true) { |
| 82 |
$newValue = []; |
| 83 |
|
| 84 |
foreach ($value as $key => $arrayValue) { |
| 85 |
$sanitizedKey = $this->sanitizeValue($key); |
| 86 |
$newValue[$sanitizedKey] = $this->sanitizeValue($arrayValue); |
| 87 |
} |
| 88 |
|
| 89 |
$value = $newValue; |
| 90 |
} elseif (is_string($value) === true) { |
| 91 |
$value = htmlspecialchars($value); |
| 92 |
} |
| 93 |
|
| 94 |
return $value; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Returns the request parameter. |
| 99 |
* |
| 100 |
* @param string $name |
| 101 |
* @param mixed $default |
| 102 |
* |
| 103 |
* @return mixed |
| 104 |
*/ |
| 105 |
public function getRequestParameter($name, $default = null) |
| 106 |
{ |
| 107 |
$return = (isset($_POST[$name]) === true) ? $this->sanitizeValue($_POST[$name]) : null; |
| 108 |
|
| 109 |
if ($return === null) { |
| 110 |
$return = (isset($_GET[$name]) === true) ? $this->sanitizeValue($_GET[$name]) : $default; |
| 111 |
} |
| 112 |
|
| 113 |
return $return; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Returns the current request url. |
| 118 |
* |
| 119 |
* @return string |
| 120 |
*/ |
| 121 |
public function getRequestUrl() |
| 122 |
{ |
| 123 |
return htmlentities($_SERVER['REQUEST_URI']); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Returns the nonce field. |
| 128 |
* |
| 129 |
* @param string $name |
| 130 |
* |
| 131 |
* @return string |
| 132 |
*/ |
| 133 |
public function createNonceField($name) |
| 134 |
{ |
| 135 |
return $this->wordpress->getNonceField($name, $name.'Nonce'); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Returns the nonce. |
| 140 |
* |
| 141 |
* @param string $name |
| 142 |
* |
| 143 |
* @return string |
| 144 |
*/ |
| 145 |
public function getNonce($name) |
| 146 |
{ |
| 147 |
return $this->wordpress->createNonce($name); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Verifies the nonce and terminates the application if the nonce is wrong. |
| 152 |
* |
| 153 |
* @param string $name |
| 154 |
*/ |
| 155 |
protected function verifyNonce($name) |
| 156 |
{ |
| 157 |
$nonce = $this->getRequestParameter($name.'Nonce'); |
| 158 |
|
| 159 |
if ($this->wordpress->verifyNonce($nonce, $name) === false) { |
| 160 |
$this->wordpress->wpDie(TXT_UAM_NONCE_FAILURE_MESSAGE, TXT_UAM_NONCE_FAILURE_TITLE, ['response' => 401]); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Sets the update message. |
| 166 |
* |
| 167 |
* @param $message |
| 168 |
*/ |
| 169 |
protected function setUpdateMessage($message) |
| 170 |
{ |
| 171 |
$this->updateMessage = $message; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Returns the update message. |
| 176 |
* |
| 177 |
* @return string |
| 178 |
*/ |
| 179 |
public function getUpdateMessage() |
| 180 |
{ |
| 181 |
return $this->updateMessage; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Returns true if a update message is set. |
| 186 |
* |
| 187 |
* @return bool |
| 188 |
*/ |
| 189 |
public function hasUpdateMessage() |
| 190 |
{ |
| 191 |
return $this->updateMessage !== null; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Process the action. |
| 196 |
*/ |
| 197 |
protected function processAction() |
| 198 |
{ |
| 199 |
$postAction = $this->getRequestParameter(self::ACTION_PARAMETER); |
| 200 |
$postActionSplit = explode('_', $postAction); |
| 201 |
$postAction = array_shift($postActionSplit); |
| 202 |
$postAction .= implode('', array_map('ucfirst', $postActionSplit)); |
| 203 |
$actionMethod = $postAction.self::ACTION_SUFFIX; |
| 204 |
|
| 205 |
if (method_exists($this, $actionMethod) === true) { |
| 206 |
$this->{$actionMethod}(); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Returns the content of the excluded php file. |
| 212 |
* |
| 213 |
* @param string $fileName The view file name |
| 214 |
* |
| 215 |
* @return string |
| 216 |
*/ |
| 217 |
protected function getIncludeContents($fileName) |
| 218 |
{ |
| 219 |
$contents = ''; |
| 220 |
$realPath = $this->config->getRealPath(); |
| 221 |
$path = [$realPath, 'src', 'UserAccessManager', 'View']; |
| 222 |
$path = implode(DIRECTORY_SEPARATOR, $path).DIRECTORY_SEPARATOR; |
| 223 |
$fileWithPath = $path.$fileName; |
| 224 |
|
| 225 |
if (is_file($fileWithPath) === true) { |
| 226 |
ob_start(); |
| 227 |
$this->php->includeFile($this, $fileWithPath); |
| 228 |
$contents = ob_get_contents(); |
| 229 |
ob_end_clean(); |
| 230 |
} |
| 231 |
|
| 232 |
return $contents; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Renders the given template |
| 237 |
*/ |
| 238 |
public function render() |
| 239 |
{ |
| 240 |
$this->processAction(); |
| 241 |
|
| 242 |
if ($this->template !== null) { |
| 243 |
echo $this->getIncludeContents($this->template); |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
|